-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathip_addr.cpp
More file actions
76 lines (67 loc) · 1.38 KB
/
ip_addr.cpp
File metadata and controls
76 lines (67 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <string>
#include <sstream>
#include <stdexcept>
#include <arpa/inet.h>
#include "ip_addr.hpp"
using namespace std;
void
ip_addr::set_addr(in_addr_t ip, eorder etype)
{
switch(etype) {
case HOST_ORDER:
addr = htonl(ip);
break;
case NET_ORDER:
addr = ip;
break;
}
}
void
ip_addr::set_addr(const string& str)
{
addr = 0;
unsigned int octets[4];
size_t pos1=0, pos2, i, dots=0;
for(i=0; i < str.length(); i++) {
if(str.at(i) == '.') {
dots++;
continue;
}
if(str.at(i) < '0' || str.at(i) > '9')
throw invalid_argument("illegal character");
}
if(dots != 3)
throw invalid_argument("wrong address format");
pos2 = str.find('.');
i = 3;
do {
octets[i] = strtoul(str.substr(pos1, pos2).c_str(), NULL, 10);
i--;
pos1 = pos2+1;
pos2 = str.find('.', pos1);
} while(i>0);
pos2 = str.size();
octets[i] = strtoul(str.substr(pos1, pos2).c_str(), NULL, 10);
for(i=0; i<4; i++) {
if(octets[i] <= 255)
addr |= octets[i] << (i*8);
else
throw invalid_argument("octet is out of range");
}
}
string
ip_addr::as_string() const
{
ostringstream ss;
for(int i=0; i<4; i++)
ss << ((addr >> i*8) & 0xff) << (i<3?".":"");
return ss.str();
}
in_addr_t
ip_addr::as_in_addr(eorder etype) const
{
in_addr_t ret=addr;
if(etype == HOST_ORDER)
ret = ntohl(ret);
return ret;
}