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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
/**
* xrdp: A Remote Desktop Protocol server.
*
* Copyright (C) Jay Sorg 2013
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "gtcp.h"
/**
* Return a newly created socket or -1 on error
*****************************************************************************/
int tcp_socket_create(void)
{
int rv;
int option_value;
#if defined(_WIN32)
int option_len;
#else
unsigned int option_len;
#endif
/* in win32 a socket is an unsigned int, in linux, its an int */
if ((rv = (int) socket(PF_INET, SOCK_STREAM, 0)) < 0)
return -1;
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *) &option_value,
&option_len) == 0)
{
if (option_value == 0)
{
option_value = 1;
option_len = sizeof(option_value);
setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *) &option_value,
option_len);
}
}
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *) &option_value,
&option_len) == 0)
{
if (option_value < (1024 * 32))
{
option_value = 1024 * 32;
option_len = sizeof(option_value);
setsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *) &option_value,
option_len);
}
}
return rv;
}
/**
* Place specifed socket in non blocking mode
*****************************************************************************/
void tcp_set_non_blocking(int skt)
{
unsigned long i;
#if defined(_WIN32)
i = 1;
ioctlsocket(skt, FIONBIO, &i);
#else
i = fcntl(skt, F_GETFL);
i = i | O_NONBLOCK;
fcntl(skt, F_SETFL, i);
#endif
}
/**
* Assign name to socket
*
* @param skt the socket to bind
* @param port the port to bind to
*
* @return 0 on success, -1 on error
*****************************************************************************/
int tcp_bind(int skt, char *port)
{
struct sockaddr_in s;
memset(&s, 0, sizeof(struct sockaddr_in));
s.sin_family = AF_INET;
s.sin_port = htons((uint16_t) atoi(port));
s.sin_addr.s_addr = INADDR_ANY;
return bind(skt, (struct sockaddr *) &s, sizeof(struct sockaddr_in));
}
/**
* Listen for incoming connections
*
* @param skt the socket to listen on
*
* @return 0 on success, -1 on error
*****************************************************************************/
int tcp_listen(int skt)
{
return listen(skt, 2);
}
/**
* Accept incoming connection
*
* @param skt socket to accept incoming connection on
*
* @return 0 on success, -1 on error
*****************************************************************************/
int tcp_accept(int skt)
{
int ret ;
char ipAddr[256] ;
struct sockaddr_in s;
#if defined(_WIN32)
int i;
#else
unsigned int i;
#endif
i = sizeof(struct sockaddr_in);
memset(&s, 0, i);
return accept(skt, (struct sockaddr *)&s, &i);
}
/**
* Check if the socket would block
*
* @return TRUE if would block, else FALSE
*****************************************************************************/
int tcp_last_error_would_block()
{
#if defined(_WIN32)
return WSAGetLastError() == WSAEWOULDBLOCK;
#else
return (errno == EWOULDBLOCK) || (errno == EAGAIN) || (errno == EINPROGRESS);
#endif
}
/**
* Close specified socket
*****************************************************************************/
void tcp_close(int skt)
{
if (skt <= 0)
return;
#if defined(_WIN32)
closesocket(skt);
#else
close(skt);
#endif
}
/**
* Create a new socket
*
* @return new socket or -1 on error
*****************************************************************************/
int tcp_socket(void)
{
int rv;
int option_value;
#if defined(_WIN32)
int option_len;
#else
unsigned int option_len;
#endif
/* in win32 a socket is an unsigned int, in linux, its an int */
if ((rv = (int) socket(PF_INET, SOCK_STREAM, 0)) < 0)
return -1;
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *) &option_value,
&option_len) == 0)
{
if (option_value == 0)
{
option_value = 1;
option_len = sizeof(option_value);
setsockopt(rv, SOL_SOCKET, SO_REUSEADDR, (char *) &option_value,
option_len);
}
}
option_len = sizeof(option_value);
if (getsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *) &option_value,
&option_len) == 0)
{
if (option_value < (1024 * 32))
{
option_value = 1024 * 32;
option_len = sizeof(option_value);
setsockopt(rv, SOL_SOCKET, SO_SNDBUF, (char *) &option_value,
option_len);
}
}
return rv;
}
/**
* Connect to a server
*
* @param skt opaque socket obj
* @param address connect to this server
* @param port using this port
*
* @return 0 on success, -1 on error
*****************************************************************************/
int tcp_connect(int skt, const char *hostname, const char *port)
{
struct sockaddr_in s;
struct hostent *h;
memset(&s, 0, sizeof(struct sockaddr_in));
s.sin_family = AF_INET;
s.sin_port = htons((uint16_t) atoi(port));
s.sin_addr.s_addr = inet_addr(hostname);
if (s.sin_addr.s_addr == INADDR_NONE)
{
h = gethostbyname(hostname);
if (h != 0)
{
if (h->h_name != 0)
{
if (h->h_addr_list != 0)
{
if ((*(h->h_addr_list)) != 0)
{
s.sin_addr.s_addr = *((int *)(*(h->h_addr_list)));
}
}
}
}
}
return connect(skt, (struct sockaddr *) &s, sizeof(struct sockaddr_in));
}
/**
* Return 1 if we can write to the socket, 0 otherwise
*****************************************************************************/
int tcp_can_send(int skt, int millis)
{
fd_set wfds;
struct timeval time;
int rv;
time.tv_sec = millis / 1000;
time.tv_usec = (millis * 1000) % 1000000;
FD_ZERO(&wfds);
if (skt > 0)
{
FD_SET(((unsigned int) skt), &wfds);
rv = select(skt + 1, 0, &wfds, 0, &time);
if (rv > 0)
{
return tcp_socket_ok(skt);
}
}
return 0;
}
/**
* Return 1 if socket is OK, 0 otherwise
*****************************************************************************/
int tcp_socket_ok(int skt)
{
int opt;
#if defined(_WIN32)
int opt_len;
#else
unsigned int opt_len;
#endif
opt_len = sizeof(opt);
if (getsockopt(skt, SOL_SOCKET, SO_ERROR, (char *) (&opt), &opt_len) == 0)
{
if (opt == 0)
return 1;
}
return 0;
}
/**
* Check if specified sockets can be operated on without blocking
*
* @return 1 if they can be operated on or 0 if blocking would occur
*****************************************************************************/
int tcp_select(int sck1, int sck2)
{
fd_set rfds;
struct timeval time;
int max = 0;
int rv = 0;
memset(&rfds, 0, sizeof(fd_set));
memset(&time, 0, sizeof(struct timeval));
time.tv_sec = 0;
time.tv_usec = 0;
FD_ZERO(&rfds);
if (sck1 > 0)
FD_SET(((unsigned int) sck1), &rfds);
if (sck2 > 0)
FD_SET(((unsigned int) sck2), &rfds);
max = sck1;
if (sck2 > max)
max = sck2;
rv = select(max + 1, &rfds, 0, 0, &time);
if (rv > 0)
{
rv = 0;
if (FD_ISSET(((unsigned int) sck1), &rfds))
rv = rv | 1;
if (FD_ISSET(((unsigned int)sck2), &rfds))
rv = rv | 2;
}
else
{
rv = 0;
}
return rv;
}
int tcp_recv(int skt, void *ptr, int len, int flags)
{
#if defined(_WIN32)
return recv(skt, (char *) ptr, len, flags);
#else
return recv(skt, ptr, len, flags);
#endif
}
int tcp_send(int skt, const void *ptr, int len, int flags)
{
#if defined(_WIN32)
return send(skt, (const char *)ptr, len, flags);
#else
return send(skt, ptr, len, flags);
#endif
}
|