-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInetAddress.h
More file actions
203 lines (172 loc) · 4.26 KB
/
Copy pathInetAddress.h
File metadata and controls
203 lines (172 loc) · 4.26 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#ifndef INETADDRESS_H
#define INETADDRESS_H
#include <cctype>
#include <cassert>
#include <cstring>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
namespace Tube
{
class InetAddress
{
public:
InetAddress();
InetAddress(
const InetAddress& other);
InetAddress(
const struct sockaddr* address,
socklen_t addressLength);
InetAddress&
operator=(
const InetAddress& other);
~InetAddress();
bool
operator==(
const InetAddress& other) const;
bool
init(
const std::string& ipStr);
bool
init(
const char* ipStr);
bool
init(
const struct sockaddr_storage* address,
socklen_t addressLength);
void
set_port(
unsigned short port);
const struct sockaddr*
get_address() const;
socklen_t
get_address_length() const;
void
get_socket_address(
int& inetFamily,
sockaddr_storage& address,
unsigned short& port,
socklen_t& addressLength) const;
private:
void
skip_spaces(
const char*& str);
private:
union Address
{
struct sockaddr_in addrIn4;
struct sockaddr_in6 addrIn6;
struct sockaddr addrRaw;
}addressM;
};
inline
InetAddress::InetAddress()
{
memset(&addressM, 0, sizeof(addressM));
}
inline
InetAddress::InetAddress(
const InetAddress& other)
: addressM(other.addressM)
{
// Empty
}
inline
InetAddress::InetAddress(
const struct sockaddr* address,
socklen_t addressLength)
{
assert(addressLength <= sizeof(addressM));
memcpy(&addressM, address, addressLength);
}
inline
InetAddress&
InetAddress::operator=(
const InetAddress& other)
{
addressM = other.addressM;
return *this;
}
inline
InetAddress::~InetAddress()
{
// Empty
}
inline
bool
InetAddress::operator==(
const InetAddress& other) const
{
return (addressM.addrRaw.sa_family == AF_INET) ?
memcmp(&addressM.addrIn4,
&other.addressM.addrIn4,
sizeof(addressM.addrIn4)) == 0 :
memcmp(&addressM.addrIn6,
&other.addressM.addrIn6,
sizeof(addressM.addrIn6)) == 0;
}
inline
bool
InetAddress::init(
const std::string& ipStr)
{
return init(ipStr.c_str());
}
inline
void
InetAddress::set_port(
unsigned short port)
{
(addressM.addrRaw.sa_family == AF_INET) ?
addressM.addrIn4.sin_port = htons(port) :
addressM.addrIn6.sin6_port = htons(port);
}
inline
const struct sockaddr*
InetAddress::get_address() const
{
return &addressM.addrRaw;
}
inline
socklen_t
InetAddress::get_address_length() const
{
return (addressM.addrRaw.sa_family == AF_INET) ?
sizeof(struct sockaddr_in) :
sizeof(struct sockaddr_in6);
}
inline
void
InetAddress::get_socket_address(
int& inetFamily,
sockaddr_storage& address,
unsigned short& port,
socklen_t& addressLength) const
{
inetFamily = addressM.addrRaw.sa_family;
if (inetFamily == AF_INET)
{
addressLength = sizeof(addressM.addrIn4);
memcpy(&address, &addressM.addrIn4, addressLength);
port = ntohs(((struct sockaddr_in*)&address)->sin_port);
}
else
{
addressLength = sizeof(addressM.addrIn6);
memcpy(&address, &addressM.addrIn6, addressLength);
port = ntohs(((struct sockaddr_in6*)&address)->sin6_port);
}
}
inline
void
InetAddress::skip_spaces(
const char*& str)
{
while (isspace(*str))
{
str++;
}
}
}
#endif