1 /* include unph */ 2 /* Our own header. Tabs are set for 4 spaces, not 8 */ 3 4 #ifndef __unp_h 5 #define __unp_h 6 7 #include "../config.h" /* configuration options for current OS */ 8 /* "../config.h" is generated by configure */ 9 10 /* If anything changes in the following list of #includes, must change 11 acsite.m4 also, for configure's tests. */ 12 13 #include <sys/types.h> /* basic system data types */ 14 #include <sys/socket.h> /* basic socket definitions */ 15 #include <sys/time.h> /* timeval{} for select() */ 16 #include <time.h> /* timespec{} for pselect() */ 17 #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */ 18 #include <arpa/inet.h> /* inet(3) functions */ 19 #include <errno.h> 20 #include <fcntl.h> /* for nonblocking */ 21 #include <netdb.h> 22 #include <signal.h> 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 26 #include <sys/stat.h> /* for S_xxx file mode constants */ 27 #include <sys/uio.h> /* for iovec{} and readv/writev */ 28 #include <unistd.h> 29 #include <sys/wait.h> 30 #include <sys/un.h> /* for Unix domain sockets */ 31 32 #ifdef HAVE_SYS_SELECT_H 33 # include <sys/select.h> /* for convenience */ 34 #endif 35 36 #ifdef HAVE_POLL_H 37 # include <poll.h> /* for convenience */ 38 #endif 39 40 #ifdef HAVE_STRINGS_H 41 # include <strings.h> /* for convenience */ 42 #endif 43 44 /* Three headers are normally needed for socket/file ioctl's: 45 * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>. 46 */ 47 #ifdef HAVE_SYS_IOCTL_H 48 # include <sys/ioctl.h> 49 #endif 50 #ifdef HAVE_SYS_FILIO_H 51 # include <sys/filio.h> 52 #endif 53 #ifdef HAVE_SYS_SOCKIO_H 54 # include <sys/sockio.h> 55 #endif 56 57 #ifdef HAVE_PTHREAD_H 58 # include <pthread.h> 59 #endif 60 61 /* OSF/1 actually disables recv() and send() in <sys/socket.h> */ 62 #ifdef __osf__ 63 #undef recv 64 #undef send 65 #define recv(a,b,c,d) recvfrom(a,b,c,d,0,0) 66 #define send(a,b,c,d) sendto(a,b,c,d,0,0) 67 #endif 68 69 #ifndef INADDR_NONE 70 /* $$.Ic INADDR_NONE$$ */ 71 #define INADDR_NONE 0xffffffff /* should have been in <netinet/in.h> */ 72 #endif 73 74 #ifndef SHUT_RD /* these three Posix.1g names are quite new */ 75 #define SHUT_RD 0 /* shutdown for reading */ 76 #define SHUT_WR 1 /* shutdown for writing */ 77 #define SHUT_RDWR 2 /* shutdown for reading and writing */ 78 /* $$.Ic SHUT_RD$$ */ 79 /* $$.Ic SHUT_WR$$ */ 80 /* $$.Ic SHUT_RDWR$$ */ 81 #endif 82 83 /* *INDENT-OFF* */ 84 #ifndef INET_ADDRSTRLEN 85 /* $$.Ic INET_ADDRSTRLEN$$ */ 86 #define INET_ADDRSTRLEN 16 /* "ddd.ddd.ddd.ddd\0" 87 1234567890123456 */ 88 #endif 89 90 /* Define following even if IPv6 not supported, so we can always allocate 91 an adequately-sized buffer, without #ifdefs in the code. */ 92 #ifndef INET6_ADDRSTRLEN 93 /* $$.Ic INET6_ADDRSTRLEN$$ */ 94 #define INET6_ADDRSTRLEN 46 /* max size of IPv6 address string: 95 "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or 96 "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0" 97 1234567890123456789012345678901234567890123456 */ 98 #endif 99 /* *INDENT-ON* */ 100 101 /* Define bzero() as a macro if it's not in standard C library. */ 102 #ifndef HAVE_BZERO 103 #define bzero(ptr,n) memset(ptr, 0, n) 104 /* $$.If bzero$$ */ 105 /* $$.If memset$$ */ 106 #endif 107 108 /* Older resolvers do not have gethostbyname2() */ 109 #ifndef HAVE_GETHOSTBYNAME2 110 #define gethostbyname2(host,family) gethostbyname((host)) 111 #endif 112 113 /* The structure returned by recvfrom_flags() */ 114 struct in_pktinfo { 115 struct in_addr ipi_addr; /* dst IPv4 address */ 116 int ipi_ifindex;/* received interface index */ 117 }; 118 /* $$.It in_pktinfo$$ */ 119 /* $$.Ib ipi_addr$$ */ 120 /* $$.Ib ipi_ifindex$$ */ 121 122 /* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few 123 implementations support them today. These two macros really need 124 an ALIGN() macro, but each implementation does this differently. */ 125 #ifndef CMSG_LEN 126 /* $$.Ic CMSG_LEN$$ */ 127 #define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size)) 128 #endif 129 #ifndef CMSG_SPACE 130 /* $$.Ic CMSG_SPACE$$ */ 131 #define CMSG_SPACE(size) (sizeof(struct cmsghdr) + (size)) 132 #endif 133 134 /* Posix.1g 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 /* $$.Im SUN_LEN$$ */ 139 # define SUN_LEN(su) \ 140 (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) 141 #endif 142 143 /* Posix.1g renames "Unix domain" as "local IPC". 144 But not all systems DefinE AF_LOCAL and AF_LOCAL (yet). */ 145 #ifndef AF_LOCAL 146 #define AF_LOCAL AF_UNIX 147 #endif 148 #ifndef PF_LOCAL 149 #define PF_LOCAL PF_UNIX 150 #endif 151 152 /* Posix.1g requires that an #include of <poll.h> DefinE INFTIM, but many 153 systems still DefinE it in <sys/stropts.h>. We don't want to include 154 all the streams stuff if it's not needed, so we just DefinE INFTIM here. 155 This is the standard value, but there's no guarantee it is -1. */ 156 #ifndef INFTIM 157 #define INFTIM (-1) /* infinite poll timeout */ 158 /* $$.Ic INFTIM$$ */ 159 #ifdef HAVE_POLL_H 160 #define INFTIM_UNPH /* tell unpxti.h we defined it */ 161 #endif 162 #endif 163 164 /* Following could be derived from SOMAXCONN in <sys/socket.h>, but many 165 kernels still #define it as 5, while actually supporting many more */ 166 #define LISTENQ 1024 /* 2nd argument to listen() */ 167 168 /* Miscellaneous constants */ 169 #define MAXLINE 4096 /* max text line length */ 170 #define MAXSOCKADDR 128 /* max socket address structure size */ 171 #define BUFFSIZE 8192 /* buffer size for reads and writes */ 172 173 /* Define some port number that can be used for client-servers */ 174 #define SERV_PORT 9877 /* TCP and UDP client-servers */ 175 #define SERV_PORT_STR "9877" /* TCP and UDP client-servers */ 176 #define UNIXSTR_PATH "/tmp/unix.str" /* Unix domain stream cli-serv */ 177 #define UNIXDG_PATH "/tmp/unix.dg" /* Unix domain datagram cli-serv */ 178 /* $$.ix [LISTENQ]~constant,~definition~of$$ */ 179 /* $$.ix [MAXLINE]~constant,~definition~of$$ */ 180 /* $$.ix [MAXSOCKADDR]~constant,~definition~of$$ */ 181 /* $$.ix [BUFFSIZE]~constant,~definition~of$$ */ 182 /* $$.ix [SERV_PORT]~constant,~definition~of$$ */ 183 /* $$.ix [UNIXSTR_PATH]~constant,~definition~of$$ */ 184 /* $$.ix [UNIXDG_PATH]~constant,~definition~of$$ */ 185 186 /* Following shortens all the type casts of pointer arguments */ 187 #define SA struct sockaddr 188 189 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 190 /* default file access permissions for new files */ 191 #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH) 192 /* default permissions for new directories */ 193 194 typedef void Sigfunc(int); /* for signal handlers */ 195 196 #define min(a,b) ((a) < (b) ? (a) : (b)) 197 #define max(a,b) ((a) > (b) ? (a) : (b)) 198 199 #ifndef HAVE_ADDRINFO_STRUCT 200 # include "../lib/addrinfo.h" 201 #endif 202 203 #ifndef HAVE_IF_NAMEINDEX_STRUCT 204 struct if_nameindex { 205 unsigned int if_index; /* 1, 2, ... */ 206 char *if_name; /* null terminated name: "le0", ... */ 207 }; 208 /* $$.It if_nameindex$$ */ 209 /* $$.Ib if_index$$ */ 210 /* $$.Ib if_name$$ */ 211 #endif 212 213 #ifndef HAVE_TIMESPEC_STRUCT 214 struct timespec { 215 time_t tv_sec; /* seconds */ 216 long tv_nsec; /* and nanoseconds */ 217 }; 218 /* $$.It timespec$$ */ 219 /* $$.Ib tv_sec$$ */ 220 /* $$.Ib tv_nsec$$ */ 221 #endif 222 /* end unph */ 223 224 /* prototypes for our own library functions */ 225 int connect_nonb(int, const SA *, socklen_t, int); 226 int connect_timeo(int, const SA *, socklen_t, int); 227 void daemon_init(const char *, int); 228 void daemon_inetd(const char *, int); 229 void dg_cli(FILE *, int, const SA *, socklen_t); 230 void dg_echo(int, SA *, socklen_t); 231 char *gf_time(void); 232 void heartbeat_cli(int, int, int); 233 void heartbeat_serv(int, int, int); 234 struct addrinfo *host_serv(const char *, const char *, int, int); 235 int inet_srcrt_add(char *, int); 236 u_char *inet_srcrt_init(void); 237 void inet_srcrt_print(u_char *, int); 238 char **my_addrs(int *); 239 int readable_timeo(int, int); 240 ssize_t readline(int, void *, size_t); 241 ssize_t readn(int, void *, size_t); 242 ssize_t read_fd(int, void *, size_t, int *); 243 ssize_t recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *, 244 struct in_pktinfo *); 245 Sigfunc *signal_intr(int, Sigfunc *); 246 int sock_bind_wild(int, int); 247 int sock_cmp_addr(const SA *, const SA *, socklen_t); 248 int sock_cmp_port(const SA *, const SA *, socklen_t); 249 int sock_get_port(const SA *, socklen_t); 250 void sock_set_addr(SA *, socklen_t, const void *); 251 void sock_set_port(SA *, socklen_t, int); 252 void sock_set_wild(SA *, socklen_t); 253 char *sock_ntop(const SA *, socklen_t); 254 char *sock_ntop_host(const SA *, socklen_t); 255 int sockfd_to_family(int); 256 void str_echo(int); 257 void str_cli(FILE *, int); 258 int tcp_connect(const char *, const char *); 259 int tcp_listen(const char *, const char *, socklen_t *); 260 void tv_sub(struct timeval *, struct timeval *); 261 int udp_client(const char *, const char *, void **, socklen_t *); 262 int udp_connect(const char *, const char *); 263 int udp_server(const char *, const char *, socklen_t *); 264 int writable_timeo(int, int); 265 ssize_t writen(int, const void *, size_t); 266 ssize_t write_fd(int, void *, size_t, int); 267 268 #ifdef MCAST 269 int mcast_leave(int, const SA *, socklen_t); 270 int mcast_join(int, const SA *, socklen_t, const char *, u_int); 271 int mcast_get_if(int); 272 int mcast_get_loop(int); 273 int mcast_get_ttl(int); 274 int mcast_set_if(int, const char *, u_int); 275 int mcast_set_loop(int, int); 276 int mcast_set_ttl(int, int); 277 278 void Mcast_leave(int, const SA *, socklen_t); 279 void Mcast_join(int, const SA *, socklen_t, const char *, u_int); 280 int Mcast_get_if(int); 281 int Mcast_get_loop(int); 282 int Mcast_get_ttl(int); 283 void Mcast_set_if(int, const char *, u_int); 284 void Mcast_set_loop(int, int); 285 void Mcast_set_ttl(int, int); 286 #endif 287 288 unsigned short in_cksum(unsigned short *, int); 289 290 #ifndef HAVE_GETADDRINFO_PROTO 291 int getaddrinfo(const char *, const char *, const struct addrinfo *, 292 struct addrinfo **); 293 void freeaddrinfo(struct addrinfo *); 294 char *gai_strerror(int); 295 #endif 296 297 #ifndef HAVE_GETNAMEINFO_PROTO 298 int getnameinfo(const SA *, socklen_t, char *, size_t, char *, size_t, int); 299 #endif 300 301 #ifndef HAVE_GETHOSTNAME_PROTO 302 int gethostname(char *, int); 303 #endif 304 305 #ifndef HAVE_HSTRERROR_PROTO 306 const char *hstrerror(int); 307 #endif 308 309 #ifndef HAVE_IF_NAMETOINDEX_PROTO 310 unsigned int if_nametoindex(const char *); 311 char *if_indextoname(unsigned int, char *); 312 void if_freenameindex(struct if_nameindex *); 313 struct if_nameindex *if_nameindex(void); 314 #endif 315 316 #ifndef HAVE_INET_PTON_PROTO 317 int inet_pton(int, const char *, void *); 318 const char *inet_ntop(int, const void *, char *, size_t); 319 #endif 320 321 #ifndef HAVE_INET_ATON_PROTO 322 int inet_aton(const char *, struct in_addr *); 323 #endif 324 325 #ifndef HAVE_ISFDTYPE_PROTO 326 int isfdtype(int, int); 327 #endif 328 329 #ifndef HAVE_PSELECT_PROTO 330 int pselect(int, fd_set *, fd_set *, fd_set *, 331 const struct timespec *, const sigset_t *); 332 #endif 333 334 #ifndef HAVE_SOCKATMARK_PROTO 335 int sockatmark(int); 336 #endif 337 338 #ifndef HAVE_SNPRINTF_PROTO 339 int snprintf(char *, size_t, const char *, ...); 340 #endif 341 342 /* prototypes for our own library wrapper functions */ 343 void Connect_timeo(int, const SA *, socklen_t, int); 344 struct addrinfo *Host_serv(const char *, const char *, int, int); 345 const char *Inet_ntop(int, const void *, char *, size_t); 346 void Inet_pton(int, const char *, void *); 347 char *If_indextoname(unsigned int, char *); 348 unsigned int If_nametoindex(const char *); 349 struct if_nameindex *If_nameindex(void); 350 char **My_addrs(int *); 351 ssize_t Read_fd(int, void *, size_t, int *); 352 int Readable_timeo(int, int); 353 ssize_t Recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *, 354 struct in_pktinfo *); 355 Sigfunc *Signal(int, Sigfunc *); 356 Sigfunc *Signal_intr(int, Sigfunc *); 357 int Sock_bind_wild(int, int); 358 char *Sock_ntop(const SA *, socklen_t); 359 char *Sock_ntop_host(const SA *, socklen_t); 360 int Sockfd_to_family(int); 361 int Tcp_connect(const char *, const char *); 362 int Tcp_listen(const char *, const char *, socklen_t *); 363 int Udp_client(const char *, const char *, void **, socklen_t *); 364 int Udp_connect(const char *, const char *); 365 int Udp_server(const char *, const char *, socklen_t *); 366 ssize_t Write_fd(int, void *, size_t, int); 367 int Writable_timeo(int, int); 368 369 /* prototypes for our Unix wrapper functions: see {Sec errors} */ 370 void *Calloc(size_t, size_t); 371 void Close(int); 372 void Dup2(int, int); 373 int Fcntl(int, int, int); 374 void Gettimeofday(struct timeval *, void *); 375 int Ioctl(int, int, void *); 376 pid_t Fork(void); 377 void *Malloc(size_t); 378 void Mktemp(char *); 379 void *Mmap(void *, size_t, int, int, int, off_t); 380 int Open(const char *, int, mode_t); 381 void Pipe(int *fds); 382 ssize_t Read(int, void *, size_t); 383 void Sigaddset(sigset_t *, int); 384 void Sigdelset(sigset_t *, int); 385 void Sigemptyset(sigset_t *); 386 void Sigfillset(sigset_t *); 387 int Sigismember(const sigset_t *, int); 388 void Sigpending(sigset_t *); 389 void Sigprocmask(int, const sigset_t *, sigset_t *); 390 char *Strdup(const char *); 391 long Sysconf(int); 392 void Sysctl(int *, u_int, void *, size_t *, void *, size_t); 393 void Unlink(const char *); 394 pid_t Wait(int *); 395 pid_t Waitpid(pid_t, int *, int); 396 void Write(int, void *, size_t); 397 398 /* prototypes for our stdio wrapper functions: see {Sec errors} */ 399 void Fclose(FILE *); 400 FILE *Fdopen(int, const char *); 401 char *Fgets(char *, int, FILE *); 402 FILE *Fopen(const char *, const char *); 403 void Fputs(const char *, FILE *); 404 405 /* prototypes for our socket wrapper functions: see {Sec errors} */ 406 int Accept(int, SA *, socklen_t *); 407 void Bind(int, const SA *, socklen_t); 408 void Connect(int, const SA *, socklen_t); 409 void Getpeername(int, SA *, socklen_t *); 410 void Getsockname(int, SA *, socklen_t *); 411 void Getsockopt(int, int, int, void *, socklen_t *); 412 int Isfdtype(int, int); 413 void Listen(int, int); 414 #ifdef HAVE_POLL 415 int Poll(struct pollfd *, unsigned long, int); 416 #endif 417 ssize_t Readline(int, void *, size_t); 418 ssize_t Readn(int, void *, size_t); 419 ssize_t Recv(int, void *, size_t, int); 420 ssize_t Recvfrom(int, void *, size_t, int, SA *, socklen_t *); 421 ssize_t Recvmsg(int, struct msghdr *, int); 422 int Select(int, fd_set *, fd_set *, fd_set *, struct timeval *); 423 void Send(int, const void *, size_t, int); 424 void Sendto(int, const void *, size_t, int, const SA *, socklen_t); 425 void Sendmsg(int, const struct msghdr *, int); 426 void Setsockopt(int, int, int, const void *, socklen_t); 427 void Shutdown(int, int); 428 int Sockatmark(int); 429 int Socket(int, int, int); 430 void Socketpair(int, int, int, int *); 431 void Writen(int, void *, size_t); 432 433 void err_dump(const char *, ...); 434 void err_msg(const char *, ...); 435 void err_quit(const char *, ...); 436 void err_ret(const char *, ...); 437 void err_sys(const char *, ...); 438 439 #endif /* __unp_h */ 440