-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocklib_posix.cpp
More file actions
257 lines (208 loc) · 6.31 KB
/
socklib_posix.cpp
File metadata and controls
257 lines (208 loc) · 6.31 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "socklib.h"
#include <arpa/inet.h>
#include <cmath>
#include <cstring>
#include <cassert>
#include <memory>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sstream>
#include <stdexcept>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <vector>
#include <fcntl.h>
void SockLibInit() {}
void SockLibShutdown() {}
union PosixAddress {
Address::AddressData generic_data;
sockaddr_in address;
};
static sockaddr_in to_native_address(Address generic_address) {
PosixAddress posix_address;
posix_address.generic_data = generic_address._data;
return posix_address.address;
}
Address::Address(const std::string &name, int port) {
PosixAddress posix_address;
int result;
if ((result = inet_pton(AF_INET, name.c_str(), &posix_address.address.sin_addr)) != 1) {
if (result == 0) {
throw std::runtime_error(
std::string("Failed to parse IP address '" + name + "'\n"));
}
throw std::runtime_error(std::string("inet_pton(): ") + strerror(errno));
}
posix_address.address.sin_port = htons(port);
posix_address.address.sin_family = AF_INET;
assert(sizeof(_data) >= sizeof(posix_address.address));
memcpy(&_data, &posix_address, sizeof(posix_address));
}
union PosixSocket {
Socket::SocketData generic_data;
int posix_socket;
};
static int to_native_socket(const Socket &generic_socket) {
PosixSocket posix_socket;
posix_socket.generic_data = generic_socket._data;
return posix_socket.posix_socket;
}
// Socket Class
void Socket::native_destroy(Socket& socket) {
close(to_native_socket(socket));
}
int Socket::SetNonBlockingMode(bool shouldBeNonBlocking) {
if (!_has_socket) {
throw std::runtime_error(std::string("Socket has not yet been created"));
}
int sock = to_native_socket(*this);
int flags = fcntl(sock, F_GETFL, 0);
if (flags == -1) {
throw std::runtime_error(std::string("fcntl(): ") + strerror(errno));
}
if (shouldBeNonBlocking) {
flags |= O_NONBLOCK;
} else {
flags &= ~O_NONBLOCK;
}
int result = fcntl(sock, F_SETFL, flags);
if (result == -1) {
throw std::runtime_error(std::string("fcntl(): ") + strerror(errno));
}
return 0;
}
int Socket::SetTimeout(float seconds) {
float i, f;
f = modff(seconds, &i);
timeval tv = {0};
tv.tv_sec = (int)i;
tv.tv_usec = f * (int)1e6;
int result = setsockopt(to_native_socket(*this),
SOL_SOCKET, SO_RCVTIMEO,
&tv, sizeof(tv));
if (result == -1)
throw std::runtime_error(std::string("setsockopt():") + strerror(errno));
return result;
}
void Socket::Create(Socket::Family family, Socket::Type type) {
if (_has_socket)
throw std::runtime_error("Socket already has an associated system socket.");
int native_family;
int native_type;
int native_protocol;
switch (family) {
case Socket::Family::INET:
native_family = AF_INET;
break;
case Socket::Family::INET6:
native_family = AF_INET6;
break;
default:
exit(1);
}
switch (type) {
case Socket::Type::STREAM:
native_type = SOCK_STREAM;
native_protocol = IPPROTO_TCP;
break;
case Socket::Type::DGRAM:
native_type = SOCK_DGRAM;
native_protocol = IPPROTO_UDP;
break;
default:
exit(1);
}
PosixSocket sock;
sock.posix_socket = socket(native_family, native_type, native_protocol);
if (sock.posix_socket == -1) {
throw std::runtime_error(std::string("socket(): ") + strerror(errno));
}
_data = sock.generic_data;
_has_socket = true;
}
int Socket::Bind(const Address &address) {
sockaddr_in native_addr = to_native_address(address);
if (bind(to_native_socket(*this), (sockaddr *)&native_addr,
sizeof(native_addr)) == -1) {
throw std::runtime_error(std::string("bind(): ") + strerror(errno));
}
return 0;
}
int Socket::Listen(int backlog) {
if (listen(to_native_socket(*this), backlog) == -1) {
throw std::runtime_error(std::string("listen(): ") + strerror(errno));
}
return 0;
}
Socket Socket::Accept() {
Socket conn_sock(Socket::Family::INET, Socket::Type::STREAM);
AcceptInto(conn_sock);
return conn_sock;
}
void Socket::AcceptInto(Socket& conn_sock) {
sockaddr conn_addr;
socklen_t conn_addr_len;
int connection = accept(to_native_socket(*this), &conn_addr, &conn_addr_len);
if (connection == -1) {
throw std::runtime_error(std::string("accept(): ") + strerror(errno));
}
PosixSocket sock;
sock.posix_socket = connection;
conn_sock._data = sock.generic_data;
}
int Socket::Connect(const Address &address) {
sockaddr_in native_addr = to_native_address(address);
if (connect(to_native_socket(*this), (sockaddr *)&native_addr,
sizeof(native_addr)) == -1) {
throw std::runtime_error(std::string("connect(): ") + strerror(errno));
}
return 0;
}
int Socket::Recv(char *buffer, int size) {
ssize_t len = recv(to_native_socket(*this), buffer, size, 0);
if (len == -1) {
if (errno == EAGAIN) {
_last_error = SOCKLIB_ETIMEDOUT;
return -1;
}
throw std::runtime_error(std::string("recv(): ") + strerror(errno));
}
return (int)len;
}
int Socket::RecvFrom(char* buffer, int size, Address& src) {
PosixAddress native_addr;
socklen_t socklen = sizeof(native_addr.address);
ssize_t count = recvfrom(to_native_socket(*this), buffer, size, 0, (sockaddr*)&native_addr.address, &socklen);
if (count == -1) {
if (errno == EAGAIN) {
_last_error = SOCKLIB_ETIMEDOUT;
return -1;
}
throw std::runtime_error(std::string("recvfrom(): ") + strerror(errno));
}
src._data = native_addr.generic_data;
return count;
}
size_t Socket::Send(const char *data, size_t len) {
ssize_t count = send(to_native_socket(*this), data, len, 0);
if (count == -1) {
throw std::runtime_error(std::string("send(): ") + strerror(errno));
}
return count;
}
size_t Socket::SendTo(const char* data, size_t len, const Address& dst) {
sockaddr_in native_addr = to_native_address(dst);
ssize_t count = sendto(to_native_socket(*this), data, len, 0, (sockaddr*)&native_addr, sizeof(native_addr));
if (count == -1) {
throw std::runtime_error(std::string("sendto(): ") + strerror(errno));
}
return count;
}
std::ostream& operator<<(std::ostream& s, const Address& a) {
sockaddr_in nat_addr = to_native_address(a);
s << inet_ntoa(nat_addr.sin_addr);
s << ":";
s << ntohs(nat_addr.sin_port);
return s;
}