-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux.cpp
More file actions
252 lines (207 loc) · 6.41 KB
/
linux.cpp
File metadata and controls
252 lines (207 loc) · 6.41 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
#ifdef __unix__
#include "sockportable.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <chrono>
#include <thread>
#include <cstring>
#include <vector>
namespace sockets {
sockaddr_in buildAddress(const AddressIn& address);
AddressIn fillAddress(const sockaddr_in& addr);
OperationResult parseReturnValue(int value, int expectedValue);
int zeroesToDrop(char* buff, int buffSize);
void init() {}
Socket createSocket(int domain, int type, int protocol)
{
int sock = socket(domain, type, protocol);
Socket result;
result.isValid = sock != -1;
result.socket = sock;
return result;
}
bool bindSocket(Socket& socket, const AddressIn& address)
{
sockaddr_in addr = buildAddress(address);
int err = bind(socket.socket, (sockaddr*)&addr, sizeof(addr));
if (err < 0)
return false;
return true;
}
bool listen(Socket& socket, int numberOfConnections)
{
if (::listen(socket.socket, numberOfConnections) < 0)
return false;
return true;
}
Socket accept(const Socket& socket, AddressIn& outputAddress)
{
sockaddr_in output;
size_t sz = sizeof(output);
Socket sock;
memset(&output, 0, sizeof(output));
int result = ::accept(socket.socket, (sockaddr*)&output, (unsigned*)&sz);
outputAddress = fillAddress(output);
sock.isValid = result != -1;
sock.socket = result;
sock.address = outputAddress;
return sock;
}
bool connect(const Socket& socket, const AddressIn& outputAddress)
{
sockaddr_in output = buildAddress(outputAddress);
size_t sz = sizeof(output);
Socket sock;
int result = ::connect(socket.socket, (sockaddr*)&output, (unsigned)sz);
return result != -1;
}
sockaddr_in buildAddress(const AddressIn& address)
{
sockaddr_in addr;
in_addr p;
memset(&addr, 0, sizeof(addr));
p.s_addr = inet_addr(address.ip);
addr.sin_family = address.domain;
addr.sin_port = htons(address.port);
addr.sin_addr = p;
return addr;
}
AddressIn fillAddress(const sockaddr_in& addr)
{
AddressIn result;
result.domain = addr.sin_family;
std::string str = inet_ntoa(addr.sin_addr);
memcpy(result.ip, str.c_str(), str.length());
result.port = ntohs(addr.sin_port);
return result;
}
void closeSocket(const Socket& socket)
{
shutdown(socket.socket, SHUT_RDWR);
close(socket.socket);
}
bool isConnectionAlive(const Socket& socket, bool OOB)
{
int error = 0;
size_t sz = sizeof(int);
char buff = 0;
int flag = OOB ? MSG_OOB : 0;
errno = 0;
error = recv(socket.socket, &buff, 1, MSG_PEEK + flag);
if (error <= 0 && (errno != EWOULDBLOCK && errno != ETIMEDOUT && errno != EINTR))
return false;
getsockopt(socket.socket, SOL_SOCKET, SO_ERROR, &error, (socklen_t*)&sz);
return error == 0;
}
void setReadTimeout(const Socket& socket, int timeout)
{
timeval t;
t.tv_usec = 0;
t.tv_sec = timeout;
setsockopt(socket.socket, SOL_SOCKET, SO_RCVTIMEO, &t, (socklen_t)sizeof(timeval));
}
void setWriteTimeout(const Socket& socket, int timeout)
{
timeval t;
t.tv_sec = 0;
t.tv_usec = timeout;
setsockopt(socket.socket, SOL_SOCKET, SO_SNDTIMEO, &t, (socklen_t)sizeof(timeval));
}
unsigned getSendBuffSize(const Socket& socket)
{
unsigned result;
size_t sz = sizeof(result);
getsockopt(socket.socket, SOL_SOCKET, SO_SNDBUF, &result, (socklen_t*)&sz);
return result;
}
void setSendBuffSize(const Socket& socket, unsigned size)
{
setsockopt(socket.socket, SOL_SOCKET, SO_SNDBUF, &size, (socklen_t)sizeof(size));
}
unsigned getRecieveBuffSize(const Socket& socket)
{
unsigned result;
size_t sz = sizeof(result);
getsockopt(socket.socket, SOL_SOCKET, SO_RCVBUF, &result, (socklen_t*)&sz);
return result;
}
void setRecieveBuffSize(const Socket& socket, unsigned size)
{
setsockopt(socket.socket, SOL_SOCKET, SO_RCVBUF, &size, (socklen_t)sizeof(size));
}
void enableKeepAlive(const Socket& socket)
{
int val = 1;
setsockopt(socket.socket, SOL_SOCKET, SO_KEEPALIVE, &val, (socklen_t)sizeof(val));
}
void enableAddressReusing(const Socket& socket)
{
int val = 1;
setsockopt(socket.socket, SOL_SOCKET, SO_REUSEADDR, &val, (socklen_t)sizeof(val));
}
OperationResult getData(const Socket& socket, Buffer& buff, size_t size, bool peek)
{
if (buff.getSize() < size)
return OperationResult::Error;
int result = recv(socket.socket, buff.getWritePointer(), size, peek ? MSG_PEEK : 0);
if (result > 0)
buff.setWriteOffset(result + buff.getWriteOffset());
return parseReturnValue(result, size);
}
OperationResult getDataFrom(const Socket& socket, Buffer& buff, size_t size, AddressIn& addr, bool peek)
{
if (buff.getSize() < size)
return OperationResult::Error;
sockaddr_in ad;
size_t sz = sizeof(ad);
memset(&ad, 0, sz);
int result = recvfrom(socket.socket, buff.getWritePointer(), size, peek ? MSG_PEEK : 0, (sockaddr*)&ad, (socklen_t*)&sz);
if (result > 0)
{
addr = fillAddress(ad);
buff.setWriteOffset(result + buff.getWriteOffset());
}
return parseReturnValue(result, size);
}
OperationResult sendData(const Socket& socket, Buffer& data, size_t size)
{
if (data.getSize() < size)
return OperationResult::Error;
int result = send(socket.socket, data.getReadPointer(), size, MSG_NOSIGNAL);
if (result > 0)
data.setReadOffset(result + data.getReadOffset());
return parseReturnValue(result, size);
}
OperationResult sendDataTo(const Socket &socket, Buffer &data, size_t size, const AddressIn &addr)
{
if (data.getSize() < size)
return OperationResult::Error;
sockaddr_in ad = buildAddress(addr);
int result = sendto(socket.socket, data.getReadPointer(), size, 0, (sockaddr*)&ad, sizeof(ad));
if (result > 0)
data.setReadOffset(result + data.getReadOffset());
return parseReturnValue(result, size);
}
OperationResult parseReturnValue(int value, int expectedValue)
{
if (value >= 0)
{
if (value == 0)
return OperationResult::ConnectionClosed;
if (value != expectedValue)
return OperationResult::PartiallyFinished;
return OperationResult::Success;
}
int err = errno;
if (err == EAGAIN || err == ETIMEDOUT)
return OperationResult::Timeout;
std::cerr << "Error: " << errno << std::endl;
return OperationResult::Error;
}
void end() {}
}
#endif