-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c56e33
commit 7cbdfca
Showing
14 changed files
with
260 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#ifndef PARAMETERS_HPP_ | ||
#define PARAMETERS_HPP_ | ||
|
||
#include <exception> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
struct ParametersException : public std::runtime_error | ||
{ | ||
explicit ParametersException(const char * s) : std::runtime_error(s) | ||
{ | ||
} | ||
|
||
explicit ParametersException(const std::string & s) : std::runtime_error(s) | ||
{ | ||
} | ||
}; | ||
|
||
class AppParameters | ||
{ | ||
public: | ||
AppParameters() : address{""}, steps{default_steps} | ||
{ | ||
} | ||
|
||
std::string address; | ||
size_t steps; | ||
|
||
private: | ||
static constexpr size_t default_steps = 32; | ||
}; | ||
|
||
AppParameters parse_args(int argc, char * argv[]); | ||
|
||
#endif |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#ifndef IP4_ADDRESS_HPP_ | ||
#define IP4_ADDRESS_HPP_ | ||
|
||
#include <cinttypes> | ||
#include <cstdlib> | ||
#include <iostream> | ||
#include <string> | ||
#include <vector> | ||
|
||
class Ip4Address | ||
{ | ||
public: | ||
using addr_t = uint32_t; | ||
|
||
explicit Ip4Address(addr_t address) : address{address} | ||
{ | ||
} | ||
|
||
explicit Ip4Address(const std::string & str); | ||
|
||
Ip4Address(const Ip4Address &) = default; | ||
Ip4Address(Ip4Address &&) = default; | ||
Ip4Address & operator=(const Ip4Address &) = default; | ||
Ip4Address & operator=(Ip4Address &&) = default; | ||
|
||
friend bool operator==(const Ip4Address & a1, const Ip4Address & a2); | ||
friend bool operator<(const Ip4Address & a1, const Ip4Address & a2); | ||
friend std::ostream & operator<<(std::ostream & os, const Ip4Address & a); | ||
|
||
explicit operator addr_t() const | ||
{ | ||
return address; | ||
} | ||
|
||
explicit operator std::string() const; | ||
|
||
private: | ||
std::vector<addr_t> quadruple() const; | ||
std::vector<std::string> split(const std::string & str) const; | ||
|
||
addr_t address; | ||
}; | ||
|
||
inline bool operator==(const Ip4Address & a1, const Ip4Address & a2) | ||
{ | ||
return a1.address == a2.address; | ||
} | ||
|
||
inline bool operator!=(const Ip4Address & a1, const Ip4Address & a2) | ||
{ | ||
return !(a1 == a2); | ||
} | ||
|
||
inline bool operator<(const Ip4Address & a1, const Ip4Address & a2) | ||
{ | ||
return a1.address < a2.address; | ||
} | ||
|
||
inline bool operator<=(const Ip4Address & a1, const Ip4Address & a2) | ||
{ | ||
return (a1 < a2) || (a1 == a2); | ||
} | ||
|
||
inline bool operator>(const Ip4Address & a1, const Ip4Address & a2) | ||
{ | ||
return !(a1 <= a2); | ||
} | ||
|
||
inline bool operator>=(const Ip4Address & a1, const Ip4Address & a2) | ||
{ | ||
return !(a1 < a2); | ||
} | ||
|
||
inline std::ostream & operator<<(std::ostream & os, const Ip4Address & addr) | ||
{ | ||
std::vector<Ip4Address::addr_t> q = addr.quadruple(); | ||
|
||
os << q[0] << "." << q[1] << "." << q[2] << "." << q[3]; | ||
return os; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "AppParameters.hpp" | ||
#include <unistd.h> | ||
|
||
using namespace std::string_literals; | ||
|
||
size_t parse_number(const std::string & s, const std::string & arg_name) | ||
{ | ||
size_t pos, value; | ||
|
||
try | ||
{ | ||
value = std::stoul(s, &pos); | ||
} | ||
catch(const std::invalid_argument & e) | ||
{ | ||
throw ParametersException("Given "s + arg_name + " is not a number"s); | ||
} | ||
|
||
if(pos < s.length()) | ||
throw ParametersException("Given "s + arg_name + " is not a number"s); | ||
|
||
return value; | ||
} | ||
|
||
AppParameters parse_args(int argc, char * argv[]) | ||
{ | ||
AppParameters params; | ||
const std::string optstring = ":L:"s; | ||
int option = getopt(argc, argv, optstring.c_str()); | ||
|
||
opterr = 0; | ||
|
||
while(option != -1) | ||
{ | ||
switch(option) | ||
{ | ||
case 'L': | ||
params.steps = parse_number(optarg, "limit"s); | ||
break; | ||
|
||
case '?': | ||
throw ParametersException("Unknown option -"s + static_cast<char>(optopt)); | ||
|
||
case ':': | ||
throw ParametersException("Option -"s + static_cast<char>(optopt) | ||
+ " requires an argument"s); | ||
|
||
default: | ||
break; | ||
} | ||
|
||
option = getopt(argc, argv, optstring.c_str()); | ||
} | ||
|
||
int index = optind; | ||
|
||
if(index >= argc) | ||
throw ParametersException("No destination IP specified"); | ||
|
||
params.address = argv[index]; | ||
return params; | ||
} |
Oops, something went wrong.