unp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <netinet/sctp.h> /* note if sctp does not exist we blow up :> */
  16. #include <errno.h>
  17. #include <fcntl.h> /* for nonblocking */
  18. #include <netdb.h>
  19. #include <signal.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h> /* for S_xxx file mode constants */
  24. #include <sys/uio.h> /* for iovec{} and readv/writev */
  25. #include <unistd.h>
  26. #include <sys/wait.h>
  27. #include <sys/un.h> /* for Unix domain sockets */
  28. #ifdef HAVE_SYS_SELECT_H
  29. # include <sys/select.h> /* for convenience */
  30. #endif
  31. #ifdef HAVE_POLL_H
  32. # include <poll.h> /* for convenience */
  33. #endif
  34. #ifdef HAVE_STRINGS_H
  35. # include <strings.h> /* for convenience */
  36. #endif
  37. /* Three headers are normally needed for socket/file ioctl's:
  38. * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
  39. */
  40. #ifdef HAVE_SYS_IOCTL_H
  41. # include <sys/ioctl.h>
  42. #endif
  43. #ifdef HAVE_SYS_FILIO_H
  44. # include <sys/filio.h>
  45. #endif
  46. #ifdef HAVE_SYS_SOCKIO_H
  47. # include <sys/sockio.h>
  48. #endif
  49. #ifdef HAVE_PTHREAD_H
  50. # include <pthread.h>
  51. #endif
  52. /* OSF/1 actually disables recv() and send() in <sys/socket.h> */
  53. #ifdef __osf__
  54. #undef recv
  55. #undef send
  56. #define recv(a,b,c,d) recvfrom(a,b,c,d,0,0)
  57. #define send(a,b,c,d) sendto(a,b,c,d,0,0)
  58. #endif
  59. #ifndef INADDR_NONE
  60. /* $$.Ic INADDR_NONE$$ */
  61. #define INADDR_NONE 0xffffffff /* should have been in <netinet/in.h> */
  62. #endif
  63. #ifndef SHUT_RD /* these three Posix.1g names are quite new */
  64. #define SHUT_RD 0 /* shutdown for reading */
  65. #define SHUT_WR 1 /* shutdown for writing */
  66. #define SHUT_RDWR 2 /* shutdown for reading and writing */
  67. /* $$.Ic SHUT_RD$$ */
  68. /* $$.Ic SHUT_WR$$ */
  69. /* $$.Ic SHUT_RDWR$$ */
  70. #endif
  71. /* *INDENT-OFF* */
  72. #ifndef INET_ADDRSTRLEN
  73. /* $$.Ic INET_ADDRSTRLEN$$ */
  74. #define INET_ADDRSTRLEN 16 /* "ddd.ddd.ddd.ddd\0"
  75. 1234567890123456 */
  76. #endif
  77. /* Define following even if IPv6 not supported, so we can always allocate
  78. an adequately-sized buffer, without #ifdefs in the code. */
  79. #ifndef INET6_ADDRSTRLEN
  80. /* $$.Ic INET6_ADDRSTRLEN$$ */
  81. #define INET6_ADDRSTRLEN 46 /* max size of IPv6 address string:
  82. "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or
  83. "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0"
  84. 1234567890123456789012345678901234567890123456 */
  85. #endif
  86. /* *INDENT-ON* */
  87. /* Define bzero() as a macro if it's not in standard C library. */
  88. #ifndef HAVE_BZERO
  89. #define bzero(ptr,n) memset(ptr, 0, n)
  90. /* $$.If bzero$$ */
  91. /* $$.If memset$$ */
  92. #endif
  93. /* Older resolvers do not have gethostbyname2() */
  94. #ifndef HAVE_GETHOSTBYNAME2
  95. #define gethostbyname2(host,family) gethostbyname((host))
  96. #endif
  97. /* The structure returned by recvfrom_flags() */
  98. struct in_pktinfo {
  99. struct in_addr ipi_addr; /* dst IPv4 address */
  100. int ipi_ifindex;/* received interface index */
  101. };
  102. /* $$.It in_pktinfo$$ */
  103. /* $$.Ib ipi_addr$$ */
  104. /* $$.Ib ipi_ifindex$$ */
  105. /* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few
  106. implementations support them today. These two macros really need
  107. an ALIGN() macro, but each implementation does this differently. */
  108. #ifndef CMSG_LEN
  109. /* $$.Ic CMSG_LEN$$ */
  110. #define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size))
  111. #endif
  112. #ifndef CMSG_SPACE
  113. /* $$.Ic CMSG_SPACE$$ */
  114. #define CMSG_SPACE(size) (sizeof(struct cmsghdr) + (size))
  115. #endif
  116. /* Posix.1g requires the SUN_LEN() macro but not all implementations DefinE
  117. it (yet). Note that this 4.4BSD macro works regardless whether there is
  118. a length field or not. */
  119. #ifndef SUN_LEN
  120. /* $$.Im SUN_LEN$$ */
  121. # define SUN_LEN(su) \
  122. (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
  123. #endif
  124. /* Posix.1g renames "Unix domain" as "local IPC".
  125. But not all systems DefinE AF_LOCAL and PF_LOCAL (yet). */
  126. #ifndef AF_LOCAL
  127. #define AF_LOCAL AF_UNIX
  128. #endif
  129. #ifndef PF_LOCAL
  130. #define PF_LOCAL PF_UNIX
  131. #endif
  132. /* Posix.1g requires that an #include of <poll.h> DefinE INFTIM, but many
  133. systems still DefinE it in <sys/stropts.h>. We don't want to include
  134. all the streams stuff if it's not needed, so we just DefinE INFTIM here.
  135. This is the standard value, but there's no guarantee it is -1. */
  136. #ifndef INFTIM
  137. #define INFTIM (-1) /* infinite poll timeout */
  138. /* $$.Ic INFTIM$$ */
  139. #ifdef HAVE_POLL_H
  140. #define INFTIM_UNPH /* tell unpxti.h we defined it */
  141. #endif
  142. #endif
  143. /* Following could be derived from SOMAXCONN in <sys/socket.h>, but many
  144. kernels still #define it as 5, while actually supporting many more */
  145. #define LISTENQ 1024 /* 2nd argument to listen() */
  146. /* Miscellaneous constants */
  147. #define MAXLINE 4096 /* max text line length */
  148. #define MAXSOCKADDR 128 /* max socket address structure size */
  149. #define BUFFSIZE 8192 /* buffer size for reads and writes */
  150. /* Define some port number that can be used for client-servers */
  151. #define SERV_PORT 9877 /* TCP and UDP client-servers */
  152. #define SERV_PORT_STR "9877" /* TCP and UDP client-servers */
  153. /* Some things for SCTP */
  154. #define SCTP_PDAPI_INCR_SZ 65535 /* increment size for pdapi when adding buf space */
  155. #define SCTP_PDAPI_NEED_MORE_THRESHOLD 1024 /* need more space threshold */
  156. #define SERV_MAX_SCTP_STRM 10 /* normal maximum streams */
  157. #define SERV_MORE_STRMS_SCTP 20 /* larger number of streams */
  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. char *gf_time(void);
  206. void heartbeat_cli(int, int, int);
  207. void heartbeat_serv(int, int, int);
  208. struct addrinfo *host_serv(const char *, const char *, int, int);
  209. int inet_srcrt_add(char *, int);
  210. u_char *inet_srcrt_init(void);
  211. void inet_srcrt_print(u_char *, int);
  212. char **my_addrs(int *);
  213. int readable_timeo(int, int);
  214. ssize_t readline(int, void *, size_t);
  215. ssize_t readn(int, void *, size_t);
  216. ssize_t read_fd(int, void *, size_t, int *);
  217. ssize_t recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *,
  218. struct in_pktinfo *);
  219. Sigfunc *signal_intr(int, Sigfunc *);
  220. int sock_bind_wild(int, int);
  221. int sock_cmp_addr(const SA *, const SA *, socklen_t);
  222. int sock_cmp_port(const SA *, const SA *, socklen_t);
  223. int sock_get_port(const SA *, socklen_t);
  224. void sock_set_addr(SA *, socklen_t, const void *);
  225. void sock_set_port(SA *, socklen_t, int);
  226. void sock_set_wild(SA *, socklen_t);
  227. char *sock_ntop(const SA *, socklen_t);
  228. char *sock_ntop_host(const SA *, socklen_t);
  229. int sockfd_to_family(int);
  230. void str_echo(int);
  231. void str_cli(FILE *, int);
  232. void sctpstr_cli(FILE *fp, int sock_fd, struct sockaddr *to, socklen_t tolen);
  233. void sctpstr_cli_echoall(FILE *fp, int sock_fd, struct sockaddr *to,
  234. socklen_t tolen);
  235. uint8_t *sctp_pdapi_recvmsg(int sock_fd, int *rdlen, SA *from, int *from_len,
  236. struct sctp_sndrcvinfo *sri, int *msg_flags);
  237. int sctp_bind_arg_list(int sock_fd, char **argv, int argc);
  238. void sctp_print_addresses(struct sockaddr_storage *addrs, int num);
  239. void sctp_check_notification(int sock_fd,char *recvlin);
  240. #define SCTP_ON_DEMAND_HB 1
  241. #define SCTP_SET_HB_INTERVAL 2
  242. #define SCTP_DISABLE_HB 3
  243. int sctp_heartbeat_action(int sock_fd, struct sockaddr *sa,
  244. int action, u_int value);
  245. sctp_assoc_t
  246. sctp_address_to_associd(int sock_fd, struct sockaddr *sa, socklen_t);
  247. void
  248. sctp_print_notification(char *notify_buf);
  249. int tcp_connect(const char *, const char *);
  250. int tcp_listen(const char *, const char *, socklen_t *);
  251. void tv_sub(struct timeval *, struct timeval *);
  252. int udp_client(const char *, const char *, void **, socklen_t *);
  253. int udp_connect(const char *, const char *);
  254. int udp_server(const char *, const char *, socklen_t *);
  255. int writable_timeo(int, int);
  256. ssize_t writen(int, const void *, size_t);
  257. ssize_t write_fd(int, void *, size_t, int);
  258. #ifdef MCAST
  259. int mcast_leave(int, const SA *, socklen_t);
  260. int mcast_join(int, const SA *, socklen_t, const char *, u_int);
  261. int mcast_get_if(int);
  262. int mcast_get_loop(int);
  263. int mcast_get_ttl(int);
  264. int mcast_set_if(int, const char *, u_int);
  265. int mcast_set_loop(int, int);
  266. int mcast_set_ttl(int, int);
  267. void Mcast_leave(int, const SA *, socklen_t);
  268. void Mcast_join(int, const SA *, socklen_t, const char *, u_int);
  269. int Mcast_get_if(int);
  270. int Mcast_get_loop(int);
  271. int Mcast_get_ttl(int);
  272. void Mcast_set_if(int, const char *, u_int);
  273. void Mcast_set_loop(int, int);
  274. void Mcast_set_ttl(int, int);
  275. #endif
  276. unsigned short in_cksum(unsigned short *, int);
  277. #ifndef HAVE_GETADDRINFO_PROTO
  278. int getaddrinfo(const char *, const char *, const struct addrinfo *,
  279. struct addrinfo **);
  280. void freeaddrinfo(struct addrinfo *);
  281. char *gai_strerror(int);
  282. #endif
  283. #ifndef HAVE_GETNAMEINFO_PROTO
  284. int getnameinfo(const SA *, socklen_t, char *, size_t, char *, size_t, int);
  285. #endif
  286. #ifndef HAVE_GETHOSTNAME_PROTO
  287. int gethostname(char *, int);
  288. #endif
  289. #ifndef HAVE_HSTRERROR_PROTO
  290. const char *hstrerror(int);
  291. #endif
  292. #ifndef HAVE_IF_NAMETOINDEX_PROTO
  293. unsigned int if_nametoindex(const char *);
  294. char *if_indextoname(unsigned int, char *);
  295. void if_freenameindex(struct if_nameindex *);
  296. struct if_nameindex *if_nameindex(void);
  297. #endif
  298. #ifndef HAVE_INET_PTON_PROTO
  299. int inet_pton(int, const char *, void *);
  300. const char *inet_ntop(int, const void *, char *, size_t);
  301. #endif
  302. #ifndef HAVE_INET_ATON_PROTO
  303. int inet_aton(const char *, struct in_addr *);
  304. #endif
  305. #ifndef HAVE_ISFDTYPE_PROTO
  306. int isfdtype(int, int);
  307. #endif
  308. #ifndef HAVE_PSELECT_PROTO
  309. int pselect(int, fd_set *, fd_set *, fd_set *,
  310. const struct timespec *, const sigset_t *);
  311. #endif
  312. #ifndef HAVE_SOCKATMARK_PROTO
  313. int sockatmark(int);
  314. #endif
  315. #ifndef HAVE_SNPRINTF_PROTO
  316. int snprintf(char *, size_t, const char *, ...);
  317. #endif
  318. /* prototypes for our own library wrapper functions */
  319. void Connect_timeo(int, const SA *, socklen_t, int);
  320. struct addrinfo *Host_serv(const char *, const char *, int, int);
  321. const char *Inet_ntop(int, const void *, char *, size_t);
  322. void Inet_pton(int, const char *, void *);
  323. char *If_indextoname(unsigned int, char *);
  324. unsigned int If_nametoindex(const char *);
  325. struct if_nameindex *If_nameindex(void);
  326. char **My_addrs(int *);
  327. ssize_t Read_fd(int, void *, size_t, int *);
  328. int Readable_timeo(int, int);
  329. ssize_t Recvfrom_flags(int, void *, size_t, int *, SA *, socklen_t *,
  330. struct in_pktinfo *);
  331. Sigfunc *Signal(int, Sigfunc *);
  332. Sigfunc *Signal_intr(int, Sigfunc *);
  333. int Sock_bind_wild(int, int);
  334. char *Sock_ntop(const SA *, socklen_t);
  335. char *Sock_ntop_host(const SA *, socklen_t);
  336. int Sockfd_to_family(int);
  337. int Tcp_connect(const char *, const char *);
  338. int Tcp_listen(const char *, const char *, socklen_t *);
  339. int Udp_client(const char *, const char *, void **, socklen_t *);
  340. int Udp_connect(const char *, const char *);
  341. int Udp_server(const char *, const char *, socklen_t *);
  342. ssize_t Write_fd(int, void *, size_t, int);
  343. int Writable_timeo(int, int);
  344. /* prototypes for our Unix wrapper functions: see {Sec errors} */
  345. void *Calloc(size_t, size_t);
  346. void Close(int);
  347. void Dup2(int, int);
  348. int Fcntl(int, int, int);
  349. void Gettimeofday(struct timeval *, void *);
  350. int Ioctl(int, int, void *);
  351. pid_t Fork(void);
  352. void *Malloc(size_t);
  353. void Mktemp(char *);
  354. void *Mmap(void *, size_t, int, int, int, off_t);
  355. int Open(const char *, int, mode_t);
  356. void Pipe(int *fds);
  357. ssize_t Read(int, void *, size_t);
  358. void Sigaddset(sigset_t *, int);
  359. void Sigdelset(sigset_t *, int);
  360. void Sigemptyset(sigset_t *);
  361. void Sigfillset(sigset_t *);
  362. int Sigismember(const sigset_t *, int);
  363. void Sigpending(sigset_t *);
  364. void Sigprocmask(int, const sigset_t *, sigset_t *);
  365. char *Strdup(const char *);
  366. long Sysconf(int);
  367. void Sysctl(int *, u_int, void *, size_t *, void *, size_t);
  368. void Unlink(const char *);
  369. pid_t Wait(int *);
  370. pid_t Waitpid(pid_t, int *, int);
  371. void Write(int, void *, size_t);
  372. /* prototypes for our stdio wrapper functions: see {Sec errors} */
  373. void Fclose(FILE *);
  374. FILE *Fdopen(int, const char *);
  375. char *Fgets(char *, int, FILE *);
  376. FILE *Fopen(const char *, const char *);
  377. void Fputs(const char *, FILE *);
  378. /* prototypes for our socket wrapper functions: see {Sec errors} */
  379. int Accept(int, SA *, socklen_t *);
  380. void Bind(int, const SA *, socklen_t);
  381. void Connect(int, const SA *, socklen_t);
  382. void Getpeername(int, SA *, socklen_t *);
  383. void Getsockname(int, SA *, socklen_t *);
  384. void Getsockopt(int, int, int, void *, socklen_t *);
  385. int Isfdtype(int, int);
  386. void Listen(int, int);
  387. #ifdef HAVE_POLL
  388. int Poll(struct pollfd *, unsigned long, int);
  389. #endif
  390. ssize_t Readline(int, void *, size_t);
  391. ssize_t Readn(int, void *, size_t);
  392. ssize_t Recv(int, void *, size_t, int);
  393. ssize_t Recvfrom(int, void *, size_t, int, SA *, socklen_t *);
  394. ssize_t Recvmsg(int, struct msghdr *, int);
  395. int Select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
  396. void Send(int, const void *, size_t, int);
  397. void Sendto(int, const void *, size_t, int, const SA *, socklen_t);
  398. void Sendmsg(int, const struct msghdr *, int);
  399. void Setsockopt(int, int, int, const void *, socklen_t);
  400. void Shutdown(int, int);
  401. int Sockatmark(int);
  402. int Socket(int, int, int);
  403. void Socketpair(int, int, int, int *);
  404. void Writen(int, void *, size_t);
  405. void err_dump(const char *, ...);
  406. void err_msg(const char *, ...);
  407. void err_quit(const char *, ...);
  408. void err_ret(const char *, ...);
  409. void err_sys(const char *, ...);
  410. #endif /* __unp_h */