-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsock.h
1471 lines (1185 loc) · 31.4 KB
/
sock.h
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _SOCK_H
/* include unph */
/* Our own header. Tabs are set for 4 spaces, not 8 */
//#ifndef __unp_h
//#define __unp_h
/* If anything changes in the following list of #includes, must change
acsite.m4 also, for configure's tests. */
#include <sys/types.h> /* basic system data types */
#include <sys/socket.h> /* basic socket definitions */
#include <sys/time.h> /* timeval{} for select() */
#include <time.h> /* timespec{} for pselect() */
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h> /* inet(3) functions */
#include <errno.h>
#include <fcntl.h> /* for nonblocking */
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> /* for S_xxx file mode constants */
#include <sys/uio.h> /* for iovec{} and readv/writev */
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h> /* for Unix domain sockets */
#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h> /* for convenience */
#endif
#ifdef HAVE_POLL_H
# include <poll.h> /* for convenience */
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h> /* for convenience */
#endif
/* Three headers are normally needed for socket/file ioctl's:
* <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
*/
#include <sys/ioctl.h>
#ifdef HAVE_SYS_FILIO_H
# include <sys/filio.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>
#endif
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
/* OSF/1 actually disables recv() and send() in <sys/socket.h> */
#ifdef __osf__
#undef recv
#undef send
#define recv(a,b,c,d) recvfrom(a,b,c,d,0,0)
#define send(a,b,c,d) sendto(a,b,c,d,0,0)
#endif
#ifndef INADDR_NONE
/* $$.Ic INADDR_NONE$$ */
#define INADDR_NONE 0xffffffff /* should have been in <netinet/in.h> */
#endif
#ifndef SHUT_RD /* these three Posix.1g names are quite new */
#define SHUT_RD 0 /* shutdown for reading */
#define SHUT_WR 1 /* shutdown for writing */
#define SHUT_RDWR 2 /* shutdown for reading and writing */
/* $$.Ic SHUT_RD$$ */
/* $$.Ic SHUT_WR$$ */
/* $$.Ic SHUT_RDWR$$ */
#endif
/* *INDENT-OFF* */
#ifndef INET_ADDRSTRLEN
/* $$.Ic INET_ADDRSTRLEN$$ */
#define INET_ADDRSTRLEN 16 /* "ddd.ddd.ddd.ddd\0"
1234567890123456 */
#endif
/* Define following even if IPv6 not supported, so we can always allocate
an adequately-sized buffer, without #ifdefs in the code. */
#ifndef INET6_ADDRSTRLEN
/* $$.Ic INET6_ADDRSTRLEN$$ */
#define INET6_ADDRSTRLEN 46 /* max size of IPv6 address string:
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or
"xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0"
1234567890123456789012345678901234567890123456 */
#endif
/* *INDENT-ON* */
/* Define bzero() as a macro if it's not in standard C library. */
#ifndef HAVE_BZERO
#define bzero(ptr,n) memset(ptr, 0, n)
/* $$.If bzero$$ */
/* $$.If memset$$ */
#endif
/* Older resolvers do not have gethostbyname2() */
#ifndef HAVE_GETHOSTBYNAME2
#define gethostbyname2(host,family) gethostbyname((host))
#endif
/* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few
implementations support them today. These two macros really need
an ALIGN() macro, but each implementation does this differently. */
#ifndef CMSG_LEN
/* $$.Ic CMSG_LEN$$ */
#define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size))
#endif
#ifndef CMSG_SPACE
/* $$.Ic CMSG_SPACE$$ */
#define CMSG_SPACE(size) (sizeof(struct cmsghdr) + (size))
#endif
/* Posix.1g renames "Unix domain" as "local IPC".
But not all systems DefinE AF_LOCAL and AF_LOCAL (yet). */
#ifndef AF_LOCAL
#define AF_LOCAL AF_UNIX
#endif
#ifndef PF_LOCAL
#define PF_LOCAL PF_UNIX
#endif
/* Posix.1g requires that an #include of <poll.h> DefinE INFTIM, but many
systems still DefinE it in <sys/stropts.h>. We don't want to include
all the streams stuff if it's not needed, so we just DefinE INFTIM here.
This is the standard value, but there's no guarantee it is -1. */
#ifndef INFTIM
#define INFTIM (-1) /* infinite poll timeout */
/* $$.Ic INFTIM$$ */
#ifdef HAVE_POLL_H
#define INFTIM_UNPH /* tell unpxti.h we defined it */
#endif
#endif
/* Following could be derived from SOMAXCONN in <sys/socket.h>, but many
kernels still #define it as 5, while actually supporting many more */
#define LISTENQ 1024 /* 2nd argument to listen() */
/* Miscellaneous constants */
#define MAXLINE 4096 /* max text line length */
#define MAXSOCKADDR 128 /* max socket address structure size */
#define BUFFSIZE 8192 /* buffer size for reads and writes */
/* Following shortens all the type casts of pointer arguments */
#define SA struct sockaddr
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
/* default file access permissions for new files */
#define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
/* default permissions for new directories */
typedef void Sigfunc(int); /* for signal handlers */
#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
/* prototypes for our own library functions */
void daemon_init(const char *, int);
Sigfunc *signal_intr(int, Sigfunc *);
int tcp_connect(const char *, const char *);
int tcp_listen(const char *, const char *, socklen_t *);
int udp_client(const char *, const char *, void **, socklen_t *);
int udp_connect(const char *, const char *);
int udp_server(const char *, const char *, socklen_t *);
#ifndef HAVE_INET_PTON_PROTO
int inet_pton(int, const char *, void *);
//const char *inet_ntop(int, const void *, char *, size_t);
#endif
#ifndef HAVE_INET_ATON_PROTO
// int inet_aton(const char *, struct in_addr *);
#endif
#ifndef HAVE_ISFDTYPE_PROTO
int isfdtype(int, int);
#endif
#ifndef HAVE_SNPRINTF_PROTO
int snprintf(char *, size_t, const char *, ...);
#endif
/* prototypes for our own library wrapper functions */
const char *Inet_ntop(int, const void *, char *, size_t);
void Inet_pton(int, const char *, void *);
char *If_indextoname(unsigned int, char *);
Sigfunc *Signal(int, Sigfunc *);
Sigfunc *Signal_intr(int, Sigfunc *);
int Sock_bind_wild(int, int);
int Tcp_connect(const char *, const char *);
int Tcp_listen(const char *, const char *, socklen_t *);
int Udp_client(const char *, const char *, void **, socklen_t *);
int Udp_connect(const char *, const char *);
int Udp_server(const char *, const char *, socklen_t *);
/* prototypes for our Unix wrapper functions: see {Sec errors} */
void *Calloc(size_t, size_t);
void Close(int);
void Dup2(int, int);
int Fcntl(int, int, int);
void Gettimeofday(struct timeval *, void *);
int Ioctl(int, int, void *);
pid_t Fork(void);
void *Malloc(size_t);
void Mktemp(char *);
void *Mmap(void *, size_t, int, int, int, off_t);
int Open(const char *, int, mode_t);
void Pipe(int *fds);
ssize_t Read(int, void *, size_t);
void Sigaddset(sigset_t *, int);
void Sigdelset(sigset_t *, int);
void Sigemptyset(sigset_t *);
void Sigfillset(sigset_t *);
int Sigismember(const sigset_t *, int);
void Sigpending(sigset_t *);
void Sigprocmask(int, const sigset_t *, sigset_t *);
char *Strdup(const char *);
long Sysconf(int);
void Sysctl(int *, u_int, void *, size_t *, void *, size_t);
void Unlink(const char *);
pid_t Wait(int *);
pid_t Waitpid(pid_t, int *, int);
void Write(int, void *, size_t);
/* prototypes for our stdio wrapper functions: see {Sec errors} */
void Fclose(FILE *);
FILE *Fdopen(int, const char *);
char *Fgets(char *, int, FILE *);
FILE *Fopen(const char *, const char *);
void Fputs(const char *, FILE *);
/* prototypes for our socket wrapper functions: see {Sec errors} */
int Accept(int, SA *, socklen_t *);
void Bind(int, const SA *, socklen_t);
void Connect(int, const SA *, socklen_t);
void Getpeername(int, SA *, socklen_t *);
void Getsockname(int, SA *, socklen_t *);
void Getsockopt(int, int, int, void *, socklen_t *);
int Isfdtype(int, int);
void Listen(int, int);
#ifdef HAVE_POLL
int Poll(struct pollfd *, unsigned long, int);
#endif
ssize_t Readline(int, void *, size_t);
ssize_t Readn(int, void *, size_t);
ssize_t Recv(int, void *, size_t, int);
ssize_t Recvfrom(int, void *, size_t, int, SA *, socklen_t *);
ssize_t Recvmsg(int, struct msghdr *, int);
int Select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
void Send(int, const void *, size_t, int);
void Sendto(int, const void *, size_t, int, const SA *, socklen_t);
void Sendmsg(int, const struct msghdr *, int);
void Setsockopt(int, int, int, const void *, socklen_t);
void Shutdown(int, int);
int Socket(int, int, int);
void Socketpair(int, int, int, int *);
void Writen(int, void *, size_t);
unsigned long Getaddr(char *);
void err_dump(const char *, ...);
void err_msg(const char *, ...);
void err_quit(const char *, ...);
void err_ret(const char *, ...);
void err_sys(const char *, ...);
/*
* Socket wrapper functions.
* These could all go into separate files, so only the ones needed cause
* the corresponding function to be added to the executable. If sockets
* are a library (SVR4) this might make a difference (?), but if sockets
* are in the kernel (BSD) it doesn't matter.
*
* These wrapper functions also use the same prototypes as POSIX.1g,
* which might differ from many implementations (i.e., POSIX.1g specifies
* the fourth argument to getsockopt() as "void *", not "char *").
*
* If your system's headers are not correct [i.e., the Solaris 2.5
* <sys/socket.h> omits the "const" from the second argument to both
* bind() and connect()], you'll get warnings of the form:
*warning: passing arg 2 of `bind' discards `const' from pointer target type
*warning: passing arg 2 of `connect' discards `const' from pointer target type
*/
int
Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
int n;
again:
if ( (n = accept(fd, sa, salenptr)) < 0) {
#ifdef EPROTO
if (errno == EPROTO || errno == ECONNABORTED)
#else
if (errno == ECONNABORTED)
#endif
goto again;
else
err_sys("accept error");
}
return(n);
}
void
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if (bind(fd, sa, salen) < 0)
err_sys("bind error");
}
void
Connect(int fd, const struct sockaddr *sa, socklen_t salen)
{
if (connect(fd, sa, salen) < 0)
err_sys("connect error");
}
void
Getpeername(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
if (getpeername(fd, sa, salenptr) < 0)
err_sys("getpeername error");
}
void
Getsockname(int fd, struct sockaddr *sa, socklen_t *salenptr)
{
if (getsockname(fd, sa, salenptr) < 0)
err_sys("getsockname error");
}
void
Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr)
{
if (getsockopt(fd, level, optname, optval, optlenptr) < 0)
err_sys("getsockopt error");
}
int
Isfdtype(int fd, int fdtype)
{
int n;
if ( (n = isfdtype(fd, fdtype)) < 0)
err_sys("isfdtype error");
return(n);
}
/* include Listen */
void
Listen(int fd, int backlog)
{
char *ptr;
/*4can override 2nd argument with environment variable */
if ( (ptr = getenv("LISTENQ")) != NULL)
backlog = atoi(ptr);
if (listen(fd, backlog) < 0)
err_sys("listen error");
}
/* end Listen */
#ifdef HAVE_POLL
int
Poll(struct pollfd *fdarray, unsigned long nfds, int timeout)
{
int n;
if ( (n = poll(fdarray, nfds, timeout)) < 0)
err_sys("poll error");
return(n);
}
#endif
ssize_t
Recv(int fd, void *ptr, size_t nbytes, int flags)
{
ssize_t n;
if ( (n = recv(fd, ptr, nbytes, flags)) < 0)
err_sys("recv error");
return(n);
}
ssize_t
Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
struct sockaddr *sa, socklen_t *salenptr)
{
ssize_t n;
if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
err_sys("recvfrom error");
return(n);
}
ssize_t
Recvmsg(int fd, struct msghdr *msg, int flags)
{
ssize_t n;
if ( (n = recvmsg(fd, msg, flags)) < 0)
err_sys("recvmsg error");
return(n);
}
int
Select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
struct timeval *timeout)
{
int n;
if ( (n = select(nfds, readfds, writefds, exceptfds, timeout)) < 0)
err_sys("select error");
return(n); /* can return 0 on timeout */
}
void
Send(int fd, const void *ptr, size_t nbytes, int flags)
{
if (send(fd, ptr, nbytes, flags) != nbytes)
err_sys("send error");
}
void
Sendto(int fd, const void *ptr, size_t nbytes, int flags,
const struct sockaddr *sa, socklen_t salen)
{
if (sendto(fd, ptr, nbytes, flags, sa, salen) != nbytes)
err_sys("sendto error");
}
void
Sendmsg(int fd, const struct msghdr *msg, int flags)
{
int i;
ssize_t nbytes;
nbytes = 0; /* must first figure out what return value should be */
for (i = 0; i < msg->msg_iovlen; i++)
nbytes += msg->msg_iov[i].iov_len;
if (sendmsg(fd, msg, flags) != nbytes)
err_sys("sendmsg error");
}
void
Setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
{
if (setsockopt(fd, level, optname, optval, optlen) < 0)
err_sys("setsockopt error");
}
void
Shutdown(int fd, int how)
{
if (shutdown(fd, how) < 0)
err_sys("shutdown error");
}
/* include Socket */
int
Socket(int family, int type, int protocol)
{
int n;
if ( (n = socket(family, type, protocol)) < 0)
err_sys("socket error");
return(n);
}
/* end Socket */
void
Socketpair(int family, int type, int protocol, int *fd)
{
int n;
if ( (n = socketpair(family, type, protocol, fd)) < 0)
err_sys("socketpair error");
}
/*
* Wrapper functions for our own library functions.
* Most are included in the source file for the function itself.
*/
const char *
Inet_ntop(int family, const void *addrptr, char *strptr, size_t len)
{
const char *ptr;
if (strptr == NULL) /* check for old code */
err_quit("NULL 3rd argument to inet_ntop");
if ( (ptr = inet_ntop(family, addrptr, strptr, len)) == NULL)
err_sys("inet_ntop error"); /* sets errno */
return(ptr);
}
void
Inet_pton(int family, const char *strptr, void *addrptr)
{
int n;
if ( (n = inet_pton(family, strptr, addrptr)) < 0)
err_sys("inet_pton error for %s", strptr); /* errno set */
else if (n == 0)
err_quit("inet_pton error for %s", strptr); /* errno not set */
/* nothing to return */
}
/*
* Socket wrapper functions.
* These could all go into separate files, so only the ones needed cause
* the corresponding function to be added to the executable. If sockets
* are a library (SVR4) this might make a difference (?), but if sockets
* are in the kernel (BSD) it doesn't matter.
*
* These wrapper functions also use the same prototypes as POSIX.1g,
* which might differ from many implementations (i.e., POSIX.1g specifies
* the fourth argument to getsockopt() as "void *", not "char *").
*
* If your system's headers are not correct [i.e., the Solaris 2.5
* <sys/socket.h> omits the "const" from the second argument to both
* bind() and connect()], you'll get warnings of the form:
*warning: passing arg 2 of `bind' discards `const' from pointer target type
*warning: passing arg 2 of `connect' discards `const' from pointer target type
*/
void *
Calloc(size_t n, size_t size)
{
void *ptr;
if ( (ptr = calloc(n, size)) == NULL)
err_sys("calloc error");
return(ptr);
}
void
Close(int fd)
{
if (close(fd) == -1)
err_sys("close error");
}
void
Dup2(int fd1, int fd2)
{
if (dup2(fd1, fd2) == -1)
err_sys("dup2 error");
}
int
Fcntl(int fd, int cmd, int arg)
{
int n;
if ( (n = fcntl(fd, cmd, arg)) == -1)
err_sys("fcntl error");
return(n);
}
void
Gettimeofday(struct timeval *tv, void *foo)
{
if (gettimeofday(tv, foo) == -1)
err_sys("gettimeofday error");
return;
}
int
Ioctl(int fd, int request, void *arg)
{
int n;
if ( (n = ioctl(fd, request, arg)) == -1)
err_sys("ioctl error");
return(n); /* streamio of I_LIST returns value */
}
pid_t
Fork(void)
{
pid_t pid;
if ( (pid = fork()) == -1)
err_sys("fork error");
return(pid);
}
void *
Malloc(size_t size)
{
void *ptr;
if ( (ptr = malloc(size)) == NULL)
err_sys("malloc error");
return(ptr);
}
void
Mktemp(char *template)
{
if (mkstemp(template) == -1 || template[0] == 0)
err_quit("mkstemp error");
}
#include <sys/mman.h>
void *
Mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
{
void *ptr;
if ( (ptr = mmap(addr, len, prot, flags, fd, offset)) == ((void *) -1))
err_sys("mmap error");
return(ptr);
}
int
Open(const char *pathname, int oflag, mode_t mode)
{
int fd;
if ( (fd = open(pathname, oflag, mode)) == -1)
err_sys("open error for %s", pathname);
return(fd);
}
void
Pipe(int *fds)
{
if (pipe(fds) < 0)
err_sys("pipe error");
}
ssize_t
Read(int fd, void *ptr, size_t nbytes)
{
ssize_t n;
if ( (n = read(fd, ptr, nbytes)) == -1)
err_sys("read error");
return(n);
}
void
Sigaddset(sigset_t *set, int signo)
{
if (sigaddset(set, signo) == -1)
err_sys("sigaddset error");
}
void
Sigdelset(sigset_t *set, int signo)
{
if (sigdelset(set, signo) == -1)
err_sys("sigdelset error");
}
void
Sigemptyset(sigset_t *set)
{
if (sigemptyset(set) == -1)
err_sys("sigemptyset error");
}
void
Sigfillset(sigset_t *set)
{
if (sigfillset(set) == -1)
err_sys("sigfillset error");
}
int
Sigismember(const sigset_t *set, int signo)
{
int n;
if ( (n = sigismember(set, signo)) == -1)
err_sys("sigismember error");
return(n);
}
void
Sigpending(sigset_t *set)
{
if (sigpending(set) == -1)
err_sys("sigpending error");
}
void
Sigprocmask(int how, const sigset_t *set, sigset_t *oset)
{
if (sigprocmask(how, set, oset) == -1)
err_sys("sigprocmask error");
}
char *
Strdup(const char *str)
{
char *ptr;
if ( (ptr = strdup(str)) == NULL)
err_sys("strdup error");
return(ptr);
}
long
Sysconf(int name)
{
long val;
errno = 0; /* in case sysconf() does not change this */
if ( (val = sysconf(name)) == -1)
err_sys("sysconf error");
return(val);
}
#ifdef HAVE_SYS_SYSCTL_H
void
Sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
void *newp, size_t newlen)
{
if (sysctl(name, namelen, oldp, oldlenp, newp, newlen) == -1)
err_sys("sysctl error");
}
#endif
void
Unlink(const char *pathname)
{
if (unlink(pathname) == -1)
err_sys("unlink error for %s", pathname);
}
pid_t
Wait(int *iptr)
{
pid_t pid;
if ( (pid = wait(iptr)) == -1)
err_sys("wait error");
return(pid);
}
pid_t
Waitpid(pid_t pid, int *iptr, int options)
{
pid_t retpid;
if ( (retpid = waitpid(pid, iptr, options)) == -1)
err_sys("waitpid error");
return(retpid);
}
void
Write(int fd, void *ptr, size_t nbytes)
{
if (write(fd, ptr, nbytes) != nbytes)
err_sys("write error");
}
/*
* Standard I/O wrapper functions.
*/
void
Fclose(FILE *fp)
{
if (fclose(fp) != 0)
err_sys("fclose error");
}
FILE *
Fdopen(int fd, const char *type)
{
FILE *fp;
if ( (fp = fdopen(fd, type)) == NULL)
err_sys("fdopen error");
return(fp);
}
char *
Fgets(char *ptr, int n, FILE *stream)
{
char *rptr;
if ( (rptr = fgets(ptr, n, stream)) == NULL && ferror(stream))
err_sys("fgets error");
return (rptr);
}
FILE *
Fopen(const char *filename, const char *mode)
{
FILE *fp;
if ( (fp = fopen(filename, mode)) == NULL)
err_sys("fopen error");
return(fp);
}
void
Fputs(const char *ptr, FILE *stream)
{
if (fputs(ptr, stream) == EOF)
err_sys("fputs error");
}
/* include tcp_connect */
int
tcp_connect(const char *host, const char *serv)
{
int sockfd, n;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
err_quit("tcp_connect error for %s, %s",
host, serv);
ressave = res;
do {
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0)
continue; /* ignore this one */
if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
Close(sockfd); /* ignore this one */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) /* errno set from final connect() */
err_sys("tcp_connect error for %s, %s", host, serv);
freeaddrinfo(ressave);
return(sockfd);
}
/* end tcp_connect */
/*
* We place the wrapper function here, not in wraplib.c, because some
* XTI programs need to include wraplib.c, and it also defines
* a Tcp_connect() function.
*/
int
Tcp_connect(const char *host, const char *serv)
{
return(tcp_connect(host, serv));
}
/* include tcp_listen */
int
tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
int listenfd, n;
const int on = 1;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
err_quit("tcp_listen error for %s, %s",
host, serv);
ressave = res;
do {
listenfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (listenfd < 0)
continue; /* error, try next one */
Setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
if (bind(listenfd, res->ai_addr, res->ai_addrlen) == 0)
break; /* success */
Close(listenfd); /* bind error, close and try next one */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) /* errno from final socket() or bind() */
err_sys("tcp_listen error for %s, %s", host, serv);
Listen(listenfd, LISTENQ);
if (addrlenp)
*addrlenp = res->ai_addrlen; /* return size of protocol address */
freeaddrinfo(ressave);
return(listenfd);
}
/* end tcp_listen */
/*
* We place the wrapper function here, not in wraplib.c, because some
* XTI programs need to include wraplib.c, and it also defines
* a Tcp_listen() function.
*/
int
Tcp_listen(const char *host, const char *serv, socklen_t *addrlenp)
{
return(tcp_listen(host, serv, addrlenp));
}
/* include udp_client */
int
udp_client(const char *host, const char *serv, void **saptr, socklen_t *lenp)
{
int sockfd, n;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
err_quit("udp_client error for %s, %s",
host, serv);
ressave = res;
do {
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd >= 0)
break; /* success */
} while ( (res = res->ai_next) != NULL);
if (res == NULL) /* errno set from final socket() */
err_sys("udp_client error for %s, %s", host, serv);
*saptr = Malloc(res->ai_addrlen);
memcpy(*saptr, res->ai_addr, res->ai_addrlen);
*lenp = res->ai_addrlen;
freeaddrinfo(ressave);
return(sockfd);
}
/* end udp_client */
int
Udp_client(const char *host, const char *serv, void **saptr, socklen_t *lenptr)
{
return(udp_client(host, serv, saptr, lenptr));
}
/* include udp_connect */
int
udp_connect(const char *host, const char *serv)
{
int sockfd, n;
struct addrinfo hints, *res, *ressave;
bzero(&hints, sizeof(struct addrinfo));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0)
err_quit("udp_connect error for %s, %s",
host, serv);
ressave = res;