unp.h 15 KB

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