-
Notifications
You must be signed in to change notification settings - Fork 0
/
sockutil.c
527 lines (449 loc) · 13.3 KB
/
sockutil.c
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* Socket utility routines.
*
* Services is copyright (c) 1996-1999 Andy Church.
* E-mail: <achurch@dragonfire.net>
* This program is free but copyrighted software; see the file COPYING for
* details.
*/
#include "services.h"
/*************************************************************************/
/*************************************************************************/
/* Read from a socket with buffering. */
static char read_netbuf[NET_BUFSIZE];
static char *read_curpos = read_netbuf; /* Next byte to return */
static char *read_bufend = read_netbuf; /* Next position for data from socket */
static char * const read_buftop = read_netbuf + NET_BUFSIZE;
int32 total_read = 0;
/* Return amount of data in read buffer. */
int32 read_buffer_len()
{
if (read_bufend >= read_curpos)
return read_bufend - read_curpos;
else
return (read_bufend+NET_BUFSIZE) - read_curpos;
}
/* Read data. */
static int buffered_read(int fd, char *buf, int len)
{
int nread, left = len;
fd_set fds;
struct timeval tv = {0,0};
int errno_save = errno;
if (fd < 0) {
errno = EBADF;
return -1;
}
while (left > 0) {
struct timeval *tvptr = (read_bufend == read_curpos ? NULL : &tv);
FD_ZERO(&fds);
FD_SET(fd, &fds);
while (read_bufend != read_curpos-1 && !(read_curpos == read_netbuf && read_bufend == read_buftop-1)
&& select(fd+1, &fds, 0, 0, tvptr) == 1) {
int maxread;
tvptr = &tv; /* don't wait next time */
if (read_bufend < read_curpos) /* wrapped around? */
maxread = (read_curpos-1) - read_bufend;
else if (read_curpos == read_netbuf)
maxread = read_buftop - read_bufend - 1;
else
maxread = read_buftop - read_bufend;
nread = read(fd, read_bufend, maxread);
errno_save = errno;
if (debug >= 3)
log("debug: buffered_read wanted %d, got %d", maxread, nread);
if (nread <= 0)
break;
read_bufend += nread;
if (read_bufend == read_buftop)
read_bufend = read_netbuf;
}
if (read_curpos == read_bufend) /* No more data on socket */
break;
/* See if we can gobble up the rest of the buffer. */
if (read_curpos+left >= read_buftop && read_bufend < read_curpos) {
nread = read_buftop-read_curpos;
memcpy(buf, read_curpos, nread);
buf += nread;
left -= nread;
read_curpos = read_netbuf;
}
/* Now everything we need is in a single chunk at read_curpos. */
if (read_bufend > read_curpos && read_bufend-read_curpos < left)
nread = read_bufend-read_curpos;
else
nread = left;
if (nread) {
memcpy(buf, read_curpos, nread);
buf += nread;
left -= nread;
read_curpos += nread;
}
}
total_read += len - left;
if (debug >= 4) {
log("debug: buffered_read(%d,%p,%d) returning %d",
fd, buf, len, len-left);
}
errno = errno_save;
return len - left;
}
/* Optimized version of the above for reading a single character; returns
* the character in an int or EOF, like fgetc(). */
static int buffered_read_one(int fd)
{
int nread;
fd_set fds;
struct timeval tv = {0,0};
char c;
struct timeval *tvptr = (read_bufend == read_curpos ? NULL : &tv);
int errno_save = errno;
if (fd < 0) {
errno = EBADF;
return -1;
}
FD_ZERO(&fds);
FD_SET(fd, &fds);
while (read_bufend != read_curpos-1
&& !(read_curpos == read_netbuf && read_bufend == read_buftop-1)
&& select(fd+1, &fds, 0, 0, tvptr) == 1) {
int maxread;
tvptr = &tv; /* don't wait next time */
if (read_bufend < read_curpos) /* wrapped around? */
maxread = (read_curpos-1) - read_bufend;
else if (read_curpos == read_netbuf)
maxread = read_buftop - read_bufend - 1;
else
maxread = read_buftop - read_bufend;
nread = read(fd, read_bufend, maxread);
errno_save = errno;
if (debug >= 3)
log("debug: buffered_read_one wanted %d, got %d", maxread, nread);
if (nread <= 0)
break;
read_bufend += nread;
if (read_bufend == read_buftop)
read_bufend = read_netbuf;
}
if (read_curpos == read_bufend) { /* No more data on socket */
if (debug >= 4)
log("debug: buffered_read_one(%d) returning %d", fd, EOF);
errno = errno_save;
return EOF;
}
c = *read_curpos++;
if (read_curpos == read_buftop)
read_curpos = read_netbuf;
total_read++;
if (debug >= 4)
log("debug: buffered_read_one(%d) returning %d", fd, c);
return (int)c & 0xFF;
}
/*************************************************************************/
/* Write to a socket with buffering. Note that this assumes only one
* socket. */
static char write_netbuf[NET_BUFSIZE];
static char *write_curpos = write_netbuf; /* Next byte to write to socket */
static char *write_bufend = write_netbuf; /* Next position for data to socket */
static char * const write_buftop = write_netbuf + NET_BUFSIZE;
static int write_fd = -1;
int32 total_written;
/* Return amount of data in write buffer. */
int32 write_buffer_len()
{
if (write_bufend >= write_curpos)
return write_bufend - write_curpos;
else
return (write_bufend+NET_BUFSIZE) - write_curpos;
}
/* Helper routine to try and write up to one chunk of data from the buffer
* to the socket. Return how much was written. */
static int flush_write_buffer(int wait)
{
fd_set fds;
struct timeval tv = {0,0};
int errno_save = errno;
if (write_bufend == write_curpos || write_fd == -1)
return 0;
FD_ZERO(&fds);
FD_SET(write_fd, &fds);
if (select(write_fd+1, 0, &fds, 0, wait ? NULL : &tv) == 1) {
int maxwrite, nwritten;
if (write_curpos > write_bufend) /* wrapped around? */
maxwrite = write_buftop - write_curpos;
else if (write_bufend == write_netbuf)
maxwrite = write_buftop - write_curpos - 1;
else
maxwrite = write_bufend - write_curpos;
nwritten = write(write_fd, write_curpos, maxwrite);
errno_save = errno;
if (debug >= 3)
log("debug: flush_write_buffer wanted %d, got %d", maxwrite, nwritten);
if (nwritten > 0) {
write_curpos += nwritten;
if (write_curpos == write_buftop)
write_curpos = write_netbuf;
total_written += nwritten;
return nwritten;
}
}
errno = errno_save;
return 0;
}
/* Write data. */
static int buffered_write(int fd, char *buf, int len)
{
int nwritten, left = len;
int errno_save = errno;
if (fd < 0) {
errno = EBADF;
return -1;
}
write_fd = fd;
while (left > 0) {
/* Don't try putting anything in the buffer if it's full. */
if (write_curpos != write_bufend+1 &&
(write_curpos != write_netbuf || write_bufend != write_buftop-1)) {
/* See if we need to write up to the end of the buffer. */
if (write_bufend+left >= write_buftop && write_curpos <= write_bufend) {
nwritten = write_buftop-write_bufend;
memcpy(write_bufend, buf, nwritten);
buf += nwritten;
left -= nwritten;
write_bufend = write_netbuf;
}
/* Now we can copy a single chunk to write_bufend. */
if (write_curpos > write_bufend && write_curpos-write_bufend-1 < left)
nwritten = write_curpos - write_bufend - 1;
else
nwritten = left;
if (nwritten) {
memcpy(write_bufend, buf, nwritten);
buf += nwritten;
left -= nwritten;
write_bufend += nwritten;
}
}
/* Now write to the socket as much as we can. */
if (write_curpos == write_bufend+1 ||
(write_curpos == write_netbuf && write_bufend == write_buftop-1))
flush_write_buffer(1);
else
flush_write_buffer(0);
errno_save = errno;
if (write_curpos == write_bufend+1 ||
(write_curpos == write_netbuf && write_bufend == write_buftop-1)) {
/* Write failed on full buffer */
break;
}
}
if (debug >= 4) {
log("debug: buffered_write(%d,%p,%d) returning %d",
fd, buf, len, len-left);
}
errno = errno_save;
return len - left;
}
/* Optimized version of the above for writing a single character; returns
* the character in an int or EOF, like fputc(). Commented out because it
* isn't currently used. */
#if 0
static int buffered_write_one(int c, int fd)
{
struct timeval tv = {0,0};
if (fd < 0) {
errno = EBADF;
return -1;
}
write_fd = fd;
/* Try to flush the buffer if it's full. */
if (write_curpos == write_bufend+1 ||
(write_curpos == write_netbuf && write_bufend == write_buftop-1)) {
flush_write_buffer(1);
if (write_curpos == write_bufend+1 ||
(write_curpos == write_netbuf && write_bufend == write_buftop-1)) {
/* Write failed */
if (debug >= 4)
log("debug: buffered_write_one(%d) returning %d", fd, EOF);
return EOF;
}
}
/* Write the character. */
*write_bufend++ = c;
if (write_bufend == write_buftop)
write_bufend = write_netbuf;
/* Move it to the socket if we can. */
flush_write_buffer(0);
if (debug >= 4)
log("debug: buffered_write_one(%d) returning %d", fd, c);
return (int)c & 0xFF;
}
#endif /* 0 */
/*************************************************************************/
/*************************************************************************/
static int lastchar = EOF;
int sgetc(int s)
{
int c;
if (lastchar != EOF) {
c = lastchar;
lastchar = EOF;
return c;
}
return buffered_read_one(s);
}
int sungetc(int c, int s)
{
return lastchar = c;
}
/*************************************************************************/
/* If connection was broken, return NULL. If the read timed out, return
* (char *)-1.
*/
char *sgets(char *buf, int len, int s)
{
int c = 0;
struct timeval tv;
fd_set fds;
char *ptr = buf;
if (len == 0)
return NULL;
FD_SET(s, &fds);
tv.tv_sec = ReadTimeout;
tv.tv_usec = 0;
while (read_buffer_len() == 0 &&
(c = select(s+1, &fds, NULL, NULL, &tv)) < 0) {
if (errno != EINTR)
break;
}
if (read_buffer_len() == 0 && c == 0)
return (char *)-1;
c = sgetc(s);
while (--len && (*ptr++ = c) != '\n' && (c = sgetc(s)) >= 0)
;
if (c < 0)
return NULL;
*ptr = 0;
return buf;
}
/*************************************************************************/
/* sgets2: Read a line of text from a socket, and strip newline and
* carriage return characters from the end of the line.
*/
char *sgets2(char *buf, int len, int s)
{
char *str = sgets(buf, len, s);
if (!str || str == (char *)-1)
return str;
str = buf + strlen(buf)-1;
if (*str == '\n')
*str-- = 0;
if (*str == '\r')
*str = 0;
return buf;
}
/*************************************************************************/
/* Read from a socket. (Use this instead of read() because it has
* buffering.) */
int sread(int s, char *buf, int len)
{
return buffered_read(s, buf, len);
}
/*************************************************************************/
int sputs(char *str, int s)
{
return buffered_write(s, str, strlen(str));
}
/*************************************************************************/
int sockprintf(int s, char *fmt, ...)
{
va_list args;
char buf[16384]; /* Really huge, to try and avoid truncation */
va_start(args, fmt);
return buffered_write(s, buf, vsnprintf(buf, sizeof(buf), fmt, args));
}
/*************************************************************************/
/*************************************************************************/
#if !HAVE_GETHOSTBYNAME
/* Translate an IP dotted-quad address to a 4-byte character string.
* Return NULL if the given string is not in dotted-quad format.
*/
static char *pack_ip(const char *ipaddr)
{
static char ipbuf[4];
int tmp[4], i;
if (sscanf(ipaddr, "%d.%d.%d.%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != 4)
return NULL;
for (i = 0; i < 4; i++) {
if (tmp[i] < 0 || tmp[i] > 255)
return NULL;
ipbuf[i] = tmp[i];
}
return ipbuf;
}
#endif
/*************************************************************************/
/* lhost/lport specify the local side of the connection. If they are not
* given (lhost==NULL, lport==0), then they are left free to vary.
*/
int conn(const char *host, int port, const char *lhost, int lport)
{
#if HAVE_GETHOSTBYNAME
struct hostent *hp;
#else
char *addr;
#endif
struct sockaddr_in sa, lsa;
int sock;
memset(&lsa, 0, sizeof(lsa));
if (lhost) {
#if HAVE_GETHOSTBYNAME
if ((hp = gethostbyname(lhost)) != NULL)
memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length);
#else
if (addr = pack_ip(lhost))
memcpy((char *)&sa.sin_addr, addr, 4);
#endif
else
lhost = NULL;
}
if (lport)
sa.sin_port = htons(lport);
memset(&sa, 0, sizeof(sa));
#if HAVE_GETHOSTBYNAME
if (!(hp = gethostbyname(host)))
return -1;
memcpy((char *)&sa.sin_addr, hp->h_addr, hp->h_length);
sa.sin_family = hp->h_addrtype;
#else
if (!(addr = pack_ip(host))) {
log("conn(): `%s' is not a valid IP address", host);
errno = EINVAL;
return -1;
}
memcpy((char *)&sa.sin_addr, addr, 4);
sa.sin_family = AF_INET;
#endif
sa.sin_port = htons((unsigned short)port);
if ((sock = socket(sa.sin_family, SOCK_STREAM, 0)) < 0)
return -1;
if ((lhost || lport) && bind(sock,(struct sockaddr *)&lsa,sizeof(sa)) < 0) {
int errno_save = errno;
close(sock);
errno = errno_save;
return -1;
}
if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
int errno_save = errno;
close(sock);
errno = errno_save;
return -1;
}
return sock;
}
/*************************************************************************/
void disconn(int s)
{
shutdown(s, 2);
close(s);
}
/*************************************************************************/