-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_fd.cpp
395 lines (320 loc) · 11.9 KB
/
net_fd.cpp
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
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (April 2006)
Copyright (c) 2006, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
//*********************************************************************
//*********************************************************************
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <sys/uio.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include "network.h"
#include "net_fd.h"
#include "debug.h"
#include "log.h"
#include "dns_cache.h"
#include "fmttypes.h"
//*********************************************************************
// set_timeval - Initializes the timeval data structure
//*********************************************************************
struct timeval *set_timeval(struct timeval *tm, int sec, int us)
{
tm->tv_sec = sec;
tm->tv_usec = us;
return(tm);
}
//*********************************************************************
// fd_connection_request - Waits for a connection request or times out
// If a request is made then 1 is returned otherwise 0 for timeout.
// -1 signifies an error.
//*********************************************************************
int fd_connection_request(int fd, int timeout)
{
struct timeval dt;
fd_set rfd;
if (fd == -1) return(-1);
set_timeval(&dt, timeout, 0);
FD_ZERO(&rfd); FD_SET(fd, &rfd);
return(select(fd+1, &rfd, NULL, NULL, &dt));
}
//*********************************************************************
// fd_accept - Accepts a socket connection request
//*********************************************************************
int fd_accept(int monfd)
{
int fd, i;
struct sockaddr addr;
socklen_t len = sizeof(addr);
memset(&addr, 0, sizeof(addr));
fd = accept(monfd, &addr, &len);
if (fd == -1) {
log_printf(0, "fd_accept: Accept error!");
return(-1);
}
//Configure the socket for non-blocking I/O
if ((i = fcntl(fd, F_SETFL, O_NONBLOCK)) == -1) {
log_printf(0, "fd_accept: Can't configure connection for non-blocking I/O!");
}
return(fd);
}
//*********************************************************************
// fd_bind - Binds a socket to a port
//*********************************************************************
int fd_bind(char *address, int port)
{
int fd;
struct addrinfo hints;
struct addrinfo *res;
char sport[12];
int flag, err;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_family = AF_UNSPEC;
memset(sport, 0, sizeof(sport));
snprintf(sport, sizeof(sport)-1, "%d", port);
if (getaddrinfo(address, sport, &hints, &res)) {
log_printf(0, "fd_bind: getaddrinfo() error!\n");
return(-1);
}
if ((fd = socket(res->ai_family, SOCK_STREAM, 0)) < 1) {
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 1) {
log_printf(0, "fd_bind: socket() error!\n");
return(-1);
}
}
flag=1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(int));
//Configure the socket for non-blocking I/O
if ((err = fcntl(fd, F_SETFL, O_NONBLOCK)) == -1) {
log_printf(0, "fd_bind: Can't configure connection for non-blocking I/O!");
}
if((err = bind(fd, res->ai_addr, res->ai_addrlen)) == -1) {
log_printf(0, "fd_bind: hostname: %s", address);
log_printf(0, "fd_bind: %s", strerror(err));
return(-1);
}
freeaddrinfo(res);
return(fd);
}
//*********************************************************************
// fd_listen
//*********************************************************************
int fd_listen(int fd, int max_pending)
{
return(listen(fd, max_pending));
}
//*********************************************************************
// fd_set_peer - Gets the remote sockets hostname
//*********************************************************************
void fd_set_peer(int fd, char *address, int add_size)
{
union sock_u {
struct sockaddr s;
struct sockaddr_in i;
};
union sock_u psa;
address[0] = '\0';
socklen_t plen = sizeof(struct sockaddr);
if (getpeername(fd, &(psa.s), &plen) != 0) {
char errmsg[1024];
strerror_r(errno, errmsg, sizeof(errmsg));
log_printf(15, "fd_set_peer: Can't get the peers socket! Error(%d):%s\n", errno, errmsg);
return;
}
inet_ntop(AF_INET, (void *)&(psa.i.sin_addr), address, add_size);
address[add_size-1] = '\0';
return;
}
//*********************************************************************
// fd_status - Returns 1 if the socket is connected and 0 otherwise
//*********************************************************************
int fd_status(int fd)
{
if (fd != -1) return(1);
return(0);
}
//*********************************************************************
// fd_close - Base socket close call
//*********************************************************************
int fd_close(int fd)
{
if (fd == -1) return(0);
log_printf(15, "fd_close: closing fd=%d\n", fd);
int err = close(fd);
return(err);
}
//*********************************************************************
// fd_write
//*********************************************************************
long int fd_write(int fd, const void *buf, size_t count, Net_timeout_t tm)
{
long int n;
int err;
fd_set wfd;
if (fd == -1) return(-1); //** If closed return
n = write(fd, buf, count);
//log_printf(15, "fd_write: fd=%d n=%ld errno=%d\n", fd, n, errno);
if ((n==-1) && ((errno == EAGAIN) || (errno == EINTR))) n = 0;
if (n == 0) { //** Nothing written to let's try and wait
FD_ZERO(&wfd);
FD_SET(fd, &wfd);
err = select(fd+1, NULL, &wfd, NULL, &tm);
if (err > 0) {
n = write(fd, buf, count);
//log_printf(15, "fd_write2: fd=%d n=%ld select=%d errno=%d\n", fd, n, err,errno);
if (n == 0) {
n = -1; //** Dead connection
} else if ((n==-1) && ((errno == EAGAIN) || (errno == EINTR))) {
n = 0; //** Try again later
}
}
}
return(n);
}
//*********************************************************************
// fd_read
//*********************************************************************
long int fd_read(int fd, void *buf, size_t count, Net_timeout_t tm)
{
long int n;
int err;
fd_set rfd;
if (fd == -1) return(-1); //** If closed return
n = read(fd, buf, count);
//log_printf(15, "fd_read: fd=%d n=%ld errno=%d\n", fd, n, errno);
if ((n==-1) && ((errno == EAGAIN) || (errno == EINTR))) n = 0;
if (n == 0) { //** Nothing written to let's try and wait
FD_ZERO(&rfd);
FD_SET(fd, &rfd);
err = select(fd+1, &rfd, NULL, NULL, &tm);
if (err > 0) {
n = read(fd, buf, count);
//log_printf(15, "fd_read2: fd=%d n=%ld select=%d errno=%d\n", fd, n, err,errno);
if (n == 0) {
n = -1; //** Dead connection
} else if ((n==-1) && ((errno == EAGAIN) || (errno == EINTR))) {
n = 0; //** Try again later
}
}
}
return(n);
}
//*********************************************************************
// fd_connect - Creates a connection to a remote host
//*********************************************************************
int fd_connect(int *fd, const char *hostname, int port, int tcpsize, Net_timeout_t timeout)
{
struct sockaddr_in addr;
char in_addr[6];
int sfd, err;
fd_set wfd;
time_t endtime;
Net_timeout_t to;
log_printf(15, "fd_connect: Trying to make connection to Hostname: %s Port: %d\n", hostname, port);
*fd = -1;
if (hostname == NULL) {
log_printf(15, "fd_connect: lookup_host failed. Missing hostname! Port: %d\n",port);
return(1);
}
// Get ip address
if (lookup_host(hostname, in_addr) != 0) {
log_printf(15, "fd_connect: lookup_host failed. Hostname: %s Port: %d\n", hostname, port);
return(1);
}
// get the socket
sfd = socket(PF_INET, SOCK_STREAM, 0);
*fd = sfd;
if (sfd == -1) {
log_printf(10, "fd_connect: socket open failed. Hostname: %s Port: %d\n", hostname, port);
return(1);
}
// Configure it correctly
int flag=1;
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) != 0) {
log_printf(0, "fd_connect: Can't configure SO_REUSEADDR!\n");
}
if (tcpsize > 0) {
log_printf(10, "fd_connect: Setting tcpbufsize=%d\n", tcpsize);
if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUF, (char*)&tcpsize, sizeof(tcpsize)) != 0) {
log_printf(0, "fd_connect: Can't configure SO_SNDBUF to %d!\n", tcpsize);
}
if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUF, (char*)&tcpsize, sizeof(tcpsize)) != 0) {
log_printf(0, "fd_connect: Can't configure SO_RCVBUF to %d!\n", tcpsize);
}
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&(addr.sin_addr), &in_addr, 4);
// memset(&(addr.sin_zero), 0, 8);
// Connect it
err = connect(sfd, (struct sockaddr *)&addr, sizeof(struct sockaddr));
if (err != 0) {
if (errno != EINPROGRESS) {
log_printf(0, "fd_connect: connect failed. Hostname: %s Port: %d err=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));
return(1);
}
// err = 0;
}
//Configure the socket for non-blocking I/O
if ((err = fcntl(sfd, F_SETFL, O_NONBLOCK)) == -1) {
log_printf(0, "fd_connect: Can't configure connection for non-blocking I/O!");
}
log_printf(20, "fd_connect: Before select timeout=%lu time=" TT "\n", timeout.tv_sec, time(NULL));
endtime = time(NULL) + 5;
do {
FD_ZERO(&wfd);
FD_SET(sfd, &wfd);
set_net_timeout(&to, 1, 0);
err = select(sfd+1, NULL, &wfd, NULL, &to);
log_printf(20, "fd_connect: After select err=%d\n", err);
if (err != 1) {
if (errno != EINPROGRESS) {
log_printf(0, "fd_connect: select failed. Hostname: %s Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));
return(1);
} else {
log_printf(10, "fd_connect: In progress..... time=" TT " Hostname: %s Port: %d select=%d errno: %d error: %s\n", time(NULL), hostname, port, err, errno, strerror(errno));
}
}
} while ((err != 1) && (time(NULL) < endtime));
if (err != 1) { //** There were problems so return with an error
log_printf(0, "fd_connect: Couldn\'t make connection. Hostname: %s Port: %d select=%d errno: %d error: %s\n", hostname, port, err, errno, strerror(errno));
close(sfd);
return(1);
}
*fd = sfd;
return(0);
}