summaryrefslogtreecommitdiff
path: root/exploits/7350php/network.c
diff options
context:
space:
mode:
authorRoot THC2026-02-24 12:42:47 +0000
committerRoot THC2026-02-24 12:42:47 +0000
commitc9cbeced5b3f2bdd7407e29c0811e65954132540 (patch)
treeaefc355416b561111819de159ccbd86c3004cf88 /exploits/7350php/network.c
parent073fe4bf9fca6bf40cef2886d75df832ef4b6fca (diff)
initial
Diffstat (limited to 'exploits/7350php/network.c')
-rw-r--r--exploits/7350php/network.c918
1 files changed, 918 insertions, 0 deletions
diff --git a/exploits/7350php/network.c b/exploits/7350php/network.c
new file mode 100644
index 0000000..71d4a21
--- /dev/null
+++ b/exploits/7350php/network.c
@@ -0,0 +1,918 @@
1
2/* scut's leet network library ;)
3 * 1999 (c) scut
4 *
5 * networking routines
6 * based on my hbot networking sources,
7 * revised, extended and adapted 990405
8 * extended, improved and fixed 990430
9 *
10 * nearly all of this code wouldn't have been possible without w. richard stevens
11 * excellent network coding book. if you are interested in network coding,
12 * there is no way around it.
13 */
14
15#include <sys/types.h>
16#include <sys/ioctl.h>
17#include <sys/socket.h>
18#include <sys/time.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21#include <netdb.h>
22#include <net/if.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdarg.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30#include "network.h"
31
32int net_readtimeout = NET_READTIMEOUT;
33int net_conntimeout = NET_CONNTIMEOUT;
34int net_identtimeout = NET_IDENTTIMEOUT;
35
36
37int
38net_socks_connect (char *socks, unsigned short int sport, char *server, unsigned short int port, int sec)
39{
40 int s5s;
41 struct sockaddr_in cs;
42
43 s5s = net_connect (&cs, socks, sport, NULL, 0, sec);
44 if (s5s == -1)
45 return (-1);
46
47 if (net_socks_put_s5info (s5s, server, port, sec) == -1) {
48 close (s5s);
49 return (-1);
50 }
51 return (s5s);
52}
53
54
55int
56net_socks_put_s5info (int s5s, char *server, unsigned short int port, int sec)
57{
58 int n;
59 char buff[1024];
60
61 /* v5 + noauth */
62 net_write (s5s, "\x05\x01%c", 0);
63 if (net_rtimeout (s5s, sec) == -1)
64 return (-1);
65 recv (s5s, buff, sizeof (buff), 0);
66
67 /* chain us =) */
68 net_write (s5s, "\x05\x01%c\x03%c%s%c%c", 0, strlen (server), server, (port >> 8) & 0xff, port & 0xff);
69 if (net_rtimeout (s5s, sec) == -1)
70 return (-1);
71 n = recv (s5s, buff, sizeof (buff), 0);
72 if (buff[1] != 0x00) {
73 return (-1);
74 }
75 return (1);
76}
77
78
79int
80net_parseip (char *inp, char **ip, unsigned short int *port)
81{
82 int n;
83
84 if (inp == NULL)
85 return (0);
86 if (strchr (inp, ':') == NULL)
87 return (0);
88
89 *ip = calloc (1, 256);
90 if (*ip == NULL)
91 return (0);
92
93 n = sscanf (inp, "%[^:]:%hu", *ip, port);
94 if (n != 2)
95 return (0);
96
97 *ip = realloc (*ip, strlen (*ip) + 1);
98 if (*ip == NULL || (*port < 1 || *port > 65535))
99 return (0);
100
101 return (1);
102}
103
104
105char *
106net_getlocalip (void)
107{
108 struct sockaddr_in pf;
109 char name[255];
110
111 memset (name, '\0', sizeof (name));
112
113 if (gethostname (name, sizeof (name) - 1) == -1) {
114 return (NULL);
115 }
116
117 pf.sin_addr.s_addr = net_resolve (name);
118
119 return (strdup (inet_ntoa (pf.sin_addr)));;
120}
121
122
123char *
124net_peeraddress (int socket)
125{
126 char * hip;
127 struct sockaddr_in peeraddr;
128 size_t size = sizeof (struct sockaddr_in);
129
130 if (getpeername (socket, (struct sockaddr *) &peeraddr, &size) == -1)
131 return (NULL);
132
133 net_printipa (&peeraddr.sin_addr, &hip);
134
135 return (hip);
136}
137
138
139int
140net_addrpair (int socket, struct in_addr *src_ip, unsigned short int *src_prt,
141 struct in_addr *dst_ip, unsigned short int *dst_prt)
142{
143 size_t size = sizeof (struct sockaddr_in);
144 struct sockaddr_in addr;
145
146
147 if (getsockname (socket, (struct sockaddr *) &addr, &size) == -1)
148 return (1);
149
150 src_ip->s_addr = addr.sin_addr.s_addr;
151 *src_prt = ntohs (addr.sin_port);
152
153 if (getpeername (socket, (struct sockaddr *) &addr, &size) == -1)
154 return (1);
155
156 dst_ip->s_addr = addr.sin_addr.s_addr;
157 *dst_prt = ntohs (addr.sin_port);
158
159 return (0);
160}
161
162
163char *
164net_peername (int socket)
165{
166 struct sockaddr_in peeraddr;
167 struct hostent he, *hep;
168 size_t size = sizeof (struct sockaddr_in);
169 int n, h_errno;
170 unsigned char h_buf[8192];
171
172 if (getpeername (socket, (struct sockaddr *) &peeraddr, &size) == -1)
173 return (NULL);
174
175 /* digital unix / hp-ux freaks mod here =)
176 */
177 n = gethostbyaddr_r ((char *) &peeraddr.sin_addr, sizeof (struct in_addr),
178 AF_INET, &he, h_buf, sizeof (h_buf), &hep, &h_errno);
179
180 if (hep == NULL) {
181 char *ip_str = NULL;
182
183 net_printipa (&peeraddr.sin_addr, &ip_str);
184 return (ip_str);
185 }
186
187 return (strdup (he.h_name));
188}
189
190
191FILE *
192net_descriptify (int socket)
193{
194 FILE *fp;
195
196 fp = fdopen (socket, "r+");
197 return ((fp == NULL) ? (NULL) : (fp));
198}
199
200
201/* loosely based on rfc931.c */
202
203int
204net_ident (char **ident, struct sockaddr_in *locals, unsigned short int localport,
205 struct sockaddr_in *remotes, unsigned short int remoteport)
206{
207 int is; /* ident socket */
208 struct sockaddr_in isa;
209 int n;
210 char identreply[512], *cp;
211 unsigned int rmt_port, our_port;
212
213
214 *ident = NULL;
215
216 is = net_connect (&isa, inet_ntoa (remotes->sin_addr), 113, NULL, 0, net_identtimeout);
217 if (is == -1)
218 return (-1);
219
220 /* ident request */
221 net_write (is, "%u,%u\r\n", remoteport, localport);
222 memset (identreply, '\0', sizeof (identreply));
223
224 n = net_rlinet (is, identreply, sizeof(identreply) -1, net_identtimeout);
225 if (n == -1) {
226 close (is);
227 return (-1);
228 }
229 close (is);
230
231 *ident = calloc (1, 256);
232#ifdef DEBUG
233 printf("%s\n", identreply);
234#endif
235 n = sscanf (identreply, "%u , %u : USERID :%*[^:]:%255s", &rmt_port, &our_port, *ident);
236 if (n != 3) {
237 free (*ident);
238 *ident = NULL;
239 return (-1);
240 }
241
242 /* check the ports 'man */
243 if ((rmt_port != remoteport) || (our_port != localport)) {
244 free (*ident);
245 *ident = NULL;
246 return (-1);
247 }
248
249 /* strip character and save some memory */
250 if ((cp = strchr (*ident, '\r')))
251 *cp = '\0';
252 n = strlen (*ident);
253 *ident = realloc (*ident, n + 1);
254 (*ident)[n] = '\0';
255
256#ifdef DEBUG
257 printf("ident-return: %s\n", *ident);
258#endif
259 return (1);
260}
261
262
263int
264net_accept (int s, struct sockaddr_in *cs, int maxsec)
265{
266 int flags, n;
267 fd_set ac_s;
268 int len;
269 struct timeval tval;
270 struct sockaddr_in csa;
271
272 if (cs == NULL)
273 cs = &csa;
274
275 flags = fcntl(s, F_GETFL, 0);
276 if (flags == -1)
277 return (-1);
278 n = fcntl(s, F_SETFL, flags | O_NONBLOCK);
279 if (n == -1)
280 return (-1);
281
282 FD_ZERO(&ac_s);
283 FD_SET(s, &ac_s);
284 tval.tv_sec = maxsec;
285 tval.tv_usec = 0;
286
287 n = select(s + 1, &ac_s, NULL, NULL, maxsec ? &tval : NULL);
288 if (n == 0)
289 return (0);
290
291 if (FD_ISSET(s, &ac_s)) {
292 len = sizeof(struct sockaddr_in);
293 n = accept(s, (struct sockaddr *) cs, &len);
294 if (n == -1) {
295 switch (errno) {
296 case EWOULDBLOCK:
297 case ECONNABORTED:
298 case EPROTO:
299 case EINTR: if (fcntl(s, F_SETFL, flags) == -1)
300 return (-1);
301 return (0);
302 default: return (-1);
303 }
304 }
305 if (fcntl(s, F_SETFL, flags) == -1)
306 return (-1);
307 return (n);
308 }
309 if (fcntl(s, F_SETFL, flags) == -1)
310 return (-1);
311 return (0);
312}
313
314
315int
316net_testvip (char *ip)
317{
318 struct ifi_info *ifi, *ifc;
319 struct in_addr ip_n;
320
321 if (ip == NULL)
322 return (1);
323 if (strcmp(ip, "*") == 0)
324 return (1);
325
326 ip_n.s_addr = net_resolve(ip);
327 if (!(ip_n.s_addr))
328 return (0);
329
330 ifi = net_ifi_get (AF_INET, 1);
331 if (ifi == NULL)
332 return (0);
333 for (ifc = ifi; ifc != NULL; ifc = ifc->ifi_next) {
334 if (memcmp (&ip_n.s_addr, &ifc->ifi_saddr.s_addr, sizeof (struct in_addr)) == 0) {
335 net_ifi_free(ifi);
336 return (1);
337 }
338 }
339 net_ifi_free(ifi);
340 return (0);
341}
342
343
344void
345net_ifi_free (struct ifi_info *tf)
346{
347 struct ifi_info *ifi, *ifil;
348
349 ifil = NULL;
350 for (ifi = tf; ifi != NULL; ifi = ifi->ifi_next) {
351 if (ifil)
352 free (ifil);
353 if (ifi->ifi_addr)
354 free (ifi->ifi_addr);
355 ifil = ifi;
356 }
357 if (ifil)
358 free (ifil);
359 return;
360}
361
362
363struct ifi_info *
364net_ifi_get (int family, int doaliases)
365{
366 struct ifi_info *ifi, *ifihead, **ifipnext;
367 int sockfd, len, lastlen, flags, myflags;
368 char *ptr, *buf, lastname[IFNAMSIZ], *cptr;
369 struct ifconf ifc;
370 struct ifreq *ifr, ifrcopy;
371 struct sockaddr_in *sinptr;
372
373 sockfd = socket(AF_INET, SOCK_DGRAM, 0);
374 if (sockfd == -1)
375 return (NULL);
376
377 lastlen = 0;
378 len = 100 * sizeof(struct ifreq);
379 for (;;) {
380 buf = malloc(len);
381 if (buf == NULL)
382 return (NULL);
383 ifc.ifc_len = len;
384 ifc.ifc_buf = buf;
385 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
386 if (errno != EINVAL || lastlen != 0)
387 return (NULL);
388 } else {
389 if (ifc.ifc_len == lastlen)
390 break;
391 lastlen = ifc.ifc_len;
392 }
393 len += 10 * sizeof(struct ifreq);
394 free (buf);
395 }
396 ifihead = NULL;
397 ifipnext = &ifihead;
398 lastname[0] = 0;
399
400 for (ptr = buf; ptr < buf + ifc.ifc_len;) {
401 ifr = (struct ifreq *) ptr;
402 if (ifr->ifr_addr.sa_family == AF_INET)
403 len = sizeof(struct sockaddr);
404 ptr += sizeof(ifr->ifr_name) + len;
405
406 if (ifr->ifr_addr.sa_family != family)
407 continue;
408 myflags = 0;
409 if ((cptr = strchr(ifr->ifr_name, ':')) != NULL)
410 *cptr = 0;
411 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
412 if (doaliases == 0)
413 continue;
414 myflags = IFI_ALIAS;
415 }
416 memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
417
418 ifrcopy = *ifr;
419 if (ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy) < 0)
420 return (NULL);
421 flags = ifrcopy.ifr_flags;
422 if ((flags & IFF_UP) == 0)
423 continue;
424
425 ifi = calloc(1, sizeof(struct ifi_info));
426 if (ifi == NULL)
427 return (NULL);
428 *ifipnext = ifi;
429 ifipnext = &ifi->ifi_next;
430 ifi->ifi_flags = flags;
431 ifi->ifi_myflags = myflags;
432 memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
433 ifi->ifi_name[IFI_NAME - 1] = '\0';
434
435#ifdef DEBUG
436 printf("got: %s\n", ifi->ifi_name);
437#endif
438
439 switch (ifr->ifr_addr.sa_family) {
440 case AF_INET:
441 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
442 memcpy(&ifi->ifi_saddr, &sinptr->sin_addr, sizeof(struct in_addr));
443 if (ifi->ifi_addr == NULL) {
444 ifi->ifi_addr = calloc(1, sizeof(struct sockaddr_in));
445 if (ifi->ifi_addr == NULL)
446 return (NULL);
447 memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
448 }
449 break;
450 default:
451 break;
452 }
453 }
454 free (buf);
455 return (ifihead);
456}
457
458
459void
460net_boundfree (bound *bf)
461{
462 close (bf->bs);
463 free (bf);
464 return;
465}
466
467
468bound *
469net_bind (char *ip, unsigned short int port)
470{
471 bound *b;
472 int br, gsnr, lr;
473 int len, reusetmp;
474 struct sockaddr_in *sap;
475
476 if (port >= 65536)
477 return (NULL);
478
479 b = calloc(1, sizeof (bound));
480 if (b == NULL)
481 return (NULL);
482 b->bs = socket (AF_INET, SOCK_STREAM, 0);
483 if (b->bs == -1)
484 goto berror;
485
486 reusetmp = 1;
487#ifdef SO_REUSEPORT
488 if (setsockopt (b->bs, SOL_SOCKET, SO_REUSEPORT, &reusetmp, sizeof (reusetmp)) == -1)
489 goto berror;
490#else
491 if (setsockopt (b->bs, SOL_SOCKET, SO_REUSEADDR, &reusetmp, sizeof (reusetmp)) == -1)
492 goto berror;
493#endif
494
495 sap = (struct sockaddr_in *) &b->bsa;
496 sap->sin_family = AF_INET;
497 sap->sin_port = htons (port); /* 0 = ephemeral */
498
499 if (ip != NULL) {
500 if (strcmp (ip, "*") == 0) {
501 sap->sin_addr.s_addr = htonl (INADDR_ANY);
502 } else {
503 if (!(sap->sin_addr.s_addr = net_resolve (ip))) {
504 goto berror;
505 }
506 }
507 } else {
508 sap->sin_addr.s_addr = htonl (INADDR_ANY);
509 }
510
511 br = bind (b->bs, (struct sockaddr *) &b->bsa, sizeof (struct sockaddr));
512 if (br == -1)
513 goto berror;
514
515 len = sizeof (struct sockaddr);
516 gsnr = getsockname (b->bs, (struct sockaddr *) &b->bsa, &len);
517 b->port = ntohs (sap->sin_port);
518 if (gsnr == -1)
519 goto berror;
520
521 lr = listen (b->bs, 16);
522 if (lr == -1) {
523 goto berror;
524 }
525 return (b);
526
527berror:
528 free (b);
529
530 return(NULL);
531}
532
533
534unsigned long int
535net_resolve (char *host)
536{
537 long i;
538 struct hostent *he;
539
540 i = inet_addr(host);
541 if (i == -1) {
542 he = gethostbyname(host);
543 if (he == NULL) {
544 return (0);
545 } else {
546 return (*(unsigned long *) he->h_addr);
547 }
548 }
549 return (i);
550}
551
552
553int
554net_assignaddr (int sd, char *sourceip, unsigned short int sourceport)
555{
556 struct sockaddr_in sourcedef;
557
558 if (sourceip && strcmp (sourceip, "*") == 0)
559 sourceip = NULL;
560
561 if (sourceip == NULL && sourceport == 0)
562 return (1);
563
564 /* is the IP available on the local host ? (not really necessary) */
565 if (sourceip && !net_testvip (sourceip)) {
566 return (0);
567 }
568
569 memset (&sourcedef, '\0', sizeof (struct sockaddr_in));
570
571 /* if sourceip is specified, set it */
572 if (sourceip) {
573 sourcedef.sin_addr.s_addr = net_resolve (sourceip);
574 } else {
575 sourcedef.sin_addr.s_addr = htonl (INADDR_ANY);
576 }
577 if (sourceport)
578 sourcedef.sin_port = htons (sourceport);
579
580 /* now set the source on the socket by binding it */
581 if (bind (sd, (struct sockaddr *) &sourcedef, sizeof (struct sockaddr_in)) == -1) {
582 return (0);
583 }
584
585 return (1);
586}
587
588
589int
590net_connect (struct sockaddr_in *cs, char *server, unsigned short int port, char *sourceip,
591 unsigned short int sourceport, int sec)
592{
593 int n,
594 len,
595 error,
596 flags;
597 int fd;
598 struct timeval tv;
599 fd_set rset, wset;
600 struct sockaddr_in csa;
601
602 if (cs == NULL)
603 cs = &csa;
604
605 /* first allocate a socket */
606 cs->sin_family = AF_INET;
607 cs->sin_port = htons (port);
608 fd = socket (cs->sin_family, SOCK_STREAM, 0);
609 if (fd == -1)
610 return (-1);
611
612 /* check wether we should change the defaults */
613 if (net_assignaddr (fd, sourceip, sourceport) == 0) {
614 close (fd);
615 return (-1);
616 }
617
618 if (!(cs->sin_addr.s_addr = net_resolve (server))) {
619 close (fd);
620 return (-1);
621 }
622
623 flags = fcntl (fd, F_GETFL, 0);
624 if (flags == -1) {
625 close (fd);
626 return (-1);
627 }
628 n = fcntl (fd, F_SETFL, flags | O_NONBLOCK);
629 if (n == -1) {
630 close (fd);
631 return (-1);
632 }
633
634 error = 0;
635
636 n = connect (fd, (struct sockaddr *) cs, sizeof (struct sockaddr_in));
637 if (n < 0) {
638 if (errno != EINPROGRESS) {
639 close (fd);
640 return (-1);
641 }
642 }
643 if (n == 0)
644 goto done;
645
646 FD_ZERO(&rset);
647 FD_ZERO(&wset);
648 FD_SET(fd, &rset);
649 FD_SET(fd, &wset);
650 tv.tv_sec = sec;
651 tv.tv_usec = 0;
652
653 n = select(fd + 1, &rset, &wset, NULL, &tv);
654 if (n == 0) {
655 close(fd);
656 errno = ETIMEDOUT;
657 return (-1);
658 }
659 if (n == -1)
660 return (-1);
661
662 if (FD_ISSET(fd, &rset) || FD_ISSET(fd, &wset)) {
663 if (FD_ISSET(fd, &rset) && FD_ISSET(fd, &wset)) {
664 len = sizeof(error);
665 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
666 errno = ETIMEDOUT;
667 return (-1);
668 }
669 if (error == 0) {
670 goto done;
671 } else {
672 errno = error;
673 return (-1);
674 }
675 }
676 } else
677 return (-1);
678
679done:
680 n = fcntl(fd, F_SETFL, flags);
681 if (n == -1)
682 return (-1);
683 return (fd);
684}
685
686
687int
688net_tline (char *buf, int bufsize)
689{
690 int p;
691
692 for (p = 0; p < bufsize; p++) {
693 if (buf[p] == '\n')
694 return (p + 1);
695 }
696 return (-1);
697}
698
699#define LINET_A 1024
700
701
702int
703net_rlineta (int fd, char **buf, int sec)
704{
705 int n; /* return value */
706 int bufsize = 0;
707
708 *buf = NULL;
709
710 do {
711 bufsize += LINET_A;
712 *buf = realloc (*buf, bufsize);
713 if (*buf == NULL)
714 return (-1);
715
716 n = net_rlinet (fd, *buf + bufsize - LINET_A, LINET_A, sec);
717
718 if (n == -1)
719 goto rlinetaerr;
720 if (n >= 0)
721 goto rlinetastrip;
722 } while (n == -2);
723
724rlinetastrip:
725 *buf = realloc (*buf, strlen (*buf) + 1);
726 return (strlen (*buf));
727
728rlinetaerr:
729 free (*buf);
730 return (-1);
731}
732
733
734int
735net_rlinet (int fd, char *buf, int bufsize, int sec)
736{
737 int n;
738 unsigned long int rb = 0;
739 struct timeval tv_start, tv_cur;
740
741 memset(buf, '\0', bufsize);
742 (void) gettimeofday(&tv_start, NULL);
743
744 do {
745 (void) gettimeofday(&tv_cur, NULL);
746 if (sec > 0) {
747 if ((((tv_cur.tv_sec * 1000000) + (tv_cur.tv_usec)) -
748 ((tv_start.tv_sec * 1000000) + (tv_start.tv_usec))) > (sec * 1000000)) {
749 return (-1);
750 }
751 }
752 n = net_rtimeout(fd, net_readtimeout);
753 if (n <= 0) {
754 return (-1);
755 }
756 n = read(fd, buf, 1);
757 if (n <= 0) {
758 return (n);
759 }
760 rb++;
761 if (*buf == '\n')
762 return (rb);
763 buf++;
764 if (rb >= bufsize)
765 return (-2); /* buffer full */
766 } while (1);
767}
768
769
770long int
771net_rbuf (int fd, char **dst)
772{
773 long int ml = 0;
774 long int read_bytes;
775 int p;
776
777 *dst = NULL;
778
779 while ((p = net_rtimeout(fd, net_readtimeout)) == 1) {
780 *dst = (char *) realloc(*dst, ml + NET_BSIZE);
781 if (*dst == NULL)
782 return (-1);
783 ml += read_bytes = read(fd, *dst + ml, NET_BSIZE);
784 if (read_bytes == 0) {
785 *dst = (char *) realloc(*dst, ml);
786 if ((*dst == NULL) && (ml == 0)) {
787 return (1);
788 } else if (*dst == NULL) {
789 return (-1);
790 } else {
791 return (ml);
792 }
793 }
794 }
795 return (-1);
796}
797
798
799int
800net_rbuft (int fd, char *dst, unsigned long int dsize)
801{
802 unsigned long int bl = 0, m;
803 int p;
804
805 while (bl < dsize) {
806 p = net_rtimeout(fd, net_readtimeout);
807 if ((p == 0) || (p == -1)) {
808 return (-1);
809 }
810
811 m = read(fd, dst + bl, (dsize - bl));
812 if ((m == 0) || (m == -1)) {
813 return (-1);
814 }
815 bl += m;
816 }
817 return (1);
818}
819
820
821int
822net_rtimeout (int fd, int sec)
823{
824 fd_set rset;
825 struct timeval tv;
826 int n, error, flags;
827
828 error = 0;
829 flags = fcntl(fd, F_GETFL, 0);
830 n = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
831 if (n == -1)
832 return (-1);
833
834 FD_ZERO(&rset);
835 FD_SET(fd, &rset);
836 tv.tv_sec = sec;
837 tv.tv_usec = 0;
838
839 /* now we wait until more data is received then the tcp low level watermark,
840 * which should be setted to 1 in this case (1 is default)
841 */
842
843 n = select(fd + 1, &rset, NULL, NULL, &tv);
844 if (n == 0) {
845 n = fcntl(fd, F_SETFL, flags);
846 if (n == -1)
847 return (-1);
848 errno = ETIMEDOUT;
849 return (-1);
850 }
851 if (n == -1) {
852 return (-1);
853 }
854 /* socket readable ? */
855 if (FD_ISSET(fd, &rset)) {
856 n = fcntl(fd, F_SETFL, flags);
857 if (n == -1)
858 return (-1);
859 return (1);
860 } else {
861 n = fcntl(fd, F_SETFL, flags);
862 if (n == -1)
863 return (-1);
864 errno = ETIMEDOUT;
865 return (-1);
866 }
867}
868
869
870void
871net_write (int fd, const char *str, ...)
872{
873 char tmp[1025];
874 va_list vl;
875 int i;
876
877 va_start(vl, str);
878 memset(tmp, 0, sizeof(tmp));
879 i = vsnprintf(tmp, sizeof(tmp), str, vl);
880 va_end(vl);
881
882#ifdef DEBUG
883 printf("[snd] %s\n", tmp);
884#endif
885
886 send(fd, tmp, i, 0);
887 return;
888}
889
890
891int
892net_printip (struct in_addr *ia, char *str, size_t len)
893{
894 unsigned char *ipp;
895
896 ipp = (unsigned char *) &ia->s_addr;
897 snprintf (str, len - 1, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]);
898
899 return (0);
900}
901
902
903int
904net_printipa (struct in_addr *ia, char **str)
905{
906 unsigned char *ipp;
907
908 ipp = (unsigned char *) &ia->s_addr;
909 *str = calloc (1, 256);
910 if (*str == NULL)
911 return (1);
912
913 snprintf (*str, 255, "%d.%d.%d.%d", ipp[0], ipp[1], ipp[2], ipp[3]);
914 *str = realloc (*str, strlen (*str) + 1);
915
916 return ((*str == NULL) ? 1 : 0);
917}
918