unixNetworkProgramming: unp.h

File unp.h, 7.4 KB (added by wade, 15 years ago)
Line 
1/* Our own header. Tabs are set for 4 spaces, not 8 */
2
3#ifndef __unp_h
4#define __unp_h
5
6#include    "../config.h"      /* configuration options for current OS */
7                           /* "../config.h" is generated by configure */
8
9/* If anything changes in the following list of #includes, must change
10   acsite.m4 also, for configure's tests. */
11
12#include    <sys/types.h>       /* basic system data types */
13#include    <sys/socket.h>      /* basic socket definitions */
14#include    <sys/time.h>        /* timeval{} for select() */
15#include    <time.h>            /* timespec{} for pselect() */
16#include    <netinet/in.h>      /* sockaddr_in{} and other Internet defns */
17#include    <arpa/inet.h>       /* inet(3) functions */
18#include    <errno.h>
19#include    <fcntl.h>           /* for nonblocking */
20#include    <netdb.h>
21#include    <signal.h>
22#include    <stdio.h>
23#include    <stdlib.h>
24#include    <string.h>
25#include    <sys/stat.h>        /* for S_xxx file mode constants */
26#include    <sys/uio.h>         /* for iovec{} and readv/writev */
27#include    <unistd.h>
28#include    <sys/wait.h>
29#include    <sys/un.h>          /* for Unix domain sockets */
30
31#ifdef  HAVE_SYS_SELECT_H
32# include   <sys/select.h>      /* for convenience */
33#endif
34
35#ifdef  HAVE_SYS_SYSCTL_H
36# include   <sys/sysctl.h>
37#endif
38
39#ifdef  HAVE_POLL_H
40# include  <poll.h>             /* for convenience */
41#endif
42
43#ifdef  HAVE_SYS_EVENT_H
44# include   <sys/event.h>       /* for kqueue */
45#endif
46
47#ifdef  HAVE_STRINGS_H
48# include   <strings.h>         /* for convenience */
49#endif
50
51/* Three headers are normally needed for socket/file ioctl's:
52* <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
53*/
54#ifdef  HAVE_SYS_IOCTL_H
55# include   <sys/ioctl.h>
56#endif
57#ifdef  HAVE_SYS_FILIO_H
58# include   <sys/filio.h>
59#endif
60#ifdef  HAVE_SYS_SOCKIO_H
61# include   <sys/sockio.h>
62#endif
63
64#ifdef  HAVE_PTHREAD_H
65# include   <pthread.h>
66#endif
67
68#ifdef  HAVE_NET_IF_DL_H
69# include    <net/if_dl.h>
70#endif
71
72#ifdef  HAVE_NETINET_SCTP_H
73#include     <netinet/sctp.h>
74#endif
75
76/* OSF/1 actually disables recv() and send() in <sys/socket.h> */
77#ifdef  __osf__
78#undef  recv
79#undef  send
80#define recv(a,b,c,d)   recvfrom(a,b,c,d,0,0)
81#define send(a,b,c,d)   sendto(a,b,c,d,0,0)
82#endif
83
84#ifndef INADDR_NONE
85#define INADDR_NONE 0xffffffff  /* should have been in <netinet/in.h> */
86#endif
87
88#ifndef SHUT_RD                 /* these three POSIX names are new */
89#define SHUT_RD     0           /* shutdown for reading */
90#define SHUT_WR     1           /* shutdown for writing */
91#define SHUT_RDWR   2           /* shutdown for reading and writing */
92#endif
93
94#ifndef INET_ADDRSTRLEN
95#define INET_ADDRSTRLEN     16  /* "ddd.ddd.ddd.ddd\0"
96                                   1234567890123456 */
97#endif
98
99/* Define following even if IPv6 not supported, so we can always allocate
100   an adequately sized buffer without #ifdefs in the code. */
101#ifndef INET6_ADDRSTRLEN
102#define INET6_ADDRSTRLEN    46  /* max size of IPv6 address string:
103                   "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or
104                   "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0"
105                    1234567890123456789012345678901234567890123456 */
106#endif
107
108/* Define bzero() as a macro if it's not in standard C library. */
109#ifndef HAVE_BZERO
110#define bzero(ptr,n)        memset (ptr, 0, n)
111#endif
112
113/* Older resolvers do not have gethostbyname2() */
114#ifndef HAVE_GETHOSTBYNAME2
115#define gethostbyname2(host,family)     gethostbyname((host))
116#endif
117
118/* The structure returned by recvfrom_flags() */
119struct unp_in_pktinfo {
120    struct in_addr ipi_addr;    /* dst IPv4 address */
121    int     ipi_ifindex;        /* received interface index */
122};
123
124/* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few
125   implementations support them today. These two macros really need
126    an ALIGN() macro, but each implementation does this differently. */
127#ifndef CMSG_LEN
128#define CMSG_LEN(size)      (sizeof(struct cmsghdr) + (size))
129#endif
130#ifndef CMSG_SPACE
131#define CMSG_SPACE(size)    (sizeof(struct cmsghdr) + (size))
132#endif
133
134/* POSIX requires the SUN_LEN() macro, but not all implementations define
135   it (yet). Note that this 4.4BSD macro works regardless whether there is
136   a length field or not. */
137#ifndef SUN_LEN
138# define    SUN_LEN (su) \
139    (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path))
140#endif
141
142/* POSIX renames "Unix domain" as "local IPC."
143   Not all systems define AF_LOCAL and PF_LOCAL (yet). */
144#ifndef AF_LOCAL
145#define AF_LOCAL    AF_UNIX
146#endif
147#ifndef PF_LOCAL
148#define PF_LOCAL    PF_UNIX
149#endif
150
151/* POSIX requires that an #include of <poll.h> define INFTIM, but many
152   systems still define it in <sys/stropts.h>. We don't want to include
153   all the STREAMS stuff if it's not needed, so we just define INFTIM here.
154   This is the standard value, but there's no guarantee it is -1. */
155#ifndef INFTIM
156#define INFTIM          (-1)     /* infinite poll timeout */
157#ifdef HAVE_POLL_H
158#define INFTIM_UNPH              /* tell unpxti.h we defined it */
159#endif
160#endif
161
162/* Following could be derived from SOMAXCONN in <sys/socket.h>, but many
163   kernels still #define it as 5, while actually supporting many more */
164#define LISTENQ     1024         /* 2nd argument to listen () */
165
166/* Miscellaneous constants */
167#define MAXLINE     4096         /* max text line length */
168#define BUFFSIZE    8192         /* buffer size for reads and writes */
169
170/* Define some port number that can be used for our examples */
171#define SERV_PORT        9877    /* TCP and UDP */
172#define SERV_PORT_STR   "9877"   /* TCP and UDP */
173#define UNIXSTR_PATH    "/tmp/unix.str" /* Unix domain stream */
174#define UNIXDG_PATH     "/tmp/unix.dg"  /* Unix domain datagram */
175
176/* Following shortens all the typecasts of pointer arguments: */
177#define SA struct sockaddr
178
179#define HAVE_STRUCT_SOCKADDR_STORAGE
180#ifndef HAVE_STRUCT_SOCKADDR_STORAGE
181/*
182* RFC 3493: protocol-independent placeholder for socket addresses
183*/
184#define __SS_MAXSIZE    128
185#define __SS_ALIGNSIZE  (sizeof(int64_t))
186#ifdef HAVE_SOCKADDR_SA_LEN
187#define __SS_PAD1SIZE   (__SS_ALIGNSIZE - sizeof(u_char) - sizeof(sa_family_t))
188#else
189#define __SS_PAD1SIZE   (__SS_ALIGNSIZE - sizeof(sa_family_t))
190#endif
191#define __SS_PAD2SIZE   (__SS_MAXSIZE - 2*__SS_ALIGNSIZE)
192
193struct sockaddr_storage {
194#ifdef HAVE_SOCKADDR_SA_LEN
195    u_char  ss_len;
196#endif
197    sa_family_t ss_family;
198    char    __ss_pad1[__SS_PAD1SIZE];
199    int64_t __ss_align;
200    char    __ss_pad2[__SS_PAD2SIZE];
201};
202#endif
203
204#define FILE_MODE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
205                    /* default file access permissions for new files */
206#define DIR_MODE    (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH)
207                    /* default permissions for new directories */
208
209typedef void Sigfunc (int);     /* for signal handlers */
210
211#define min(a,b)    ((a) < (b) ? (a) : (b))
212#define max(a,b)    ((a) > (b) ? (a) : (b))
213
214#ifndef HAVE_ADDRINFO_STRUCT
215# include   "../lib/addrinfo.h"
216#endif
217
218#ifndef HAVE_IF_NAMEINDEX_STRUCT
219struct if_nameindex {
220    unsigned int if_index;      /* 1, 2, ... */
221    char *if_name;              /* null-terminated name: "le0", ... */
222};
223#endif
224
225#ifndef HAVE_TIMESPEC_STRUCT
226struct timespec {
227    time_t tv_sec;              /* seconds */
228    long     tv_nsec;           /* and nanoseconds */
229};
230#endif
231