wrapsock.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Socket wrapper functions.
  3. * These could all go into separate files, so only the ones needed cause
  4. * the corresponding function to be added to the executable. If sockets
  5. * are a library (SVR4) this might make a difference (?), but if sockets
  6. * are in the kernel (BSD) it doesn't matter.
  7. *
  8. * These wrapper functions also use the same prototypes as POSIX.1g,
  9. * which might differ from many implementations (i.e., POSIX.1g specifies
  10. * the fourth argument to getsockopt() as "void *", not "char *").
  11. *
  12. * If your system's headers are not correct [i.e., the Solaris 2.5
  13. * <sys/socket.h> omits the "const" from the second argument to both
  14. * bind() and connect()], you'll get warnings of the form:
  15. *warning: passing arg 2 of `bind' discards `const' from pointer target type
  16. *warning: passing arg 2 of `connect' discards `const' from pointer target type
  17. */
  18. #include "unp.h"
  19. int
  20. Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)
  21. {
  22. int n;
  23. again:
  24. if ( (n = accept(fd, sa, salenptr)) < 0) {
  25. #ifdef EPROTO
  26. if (errno == EPROTO || errno == ECONNABORTED)
  27. #else
  28. if (errno == ECONNABORTED)
  29. #endif
  30. goto again;
  31. else
  32. err_sys("accept error");
  33. }
  34. return(n);
  35. }
  36. void
  37. Bind(int fd, const struct sockaddr *sa, socklen_t salen)
  38. {
  39. if (bind(fd, sa, salen) < 0)
  40. err_sys("bind error");
  41. }
  42. void
  43. Connect(int fd, const struct sockaddr *sa, socklen_t salen)
  44. {
  45. if (connect(fd, sa, salen) < 0)
  46. err_sys("connect error");
  47. }
  48. void
  49. Getpeername(int fd, struct sockaddr *sa, socklen_t *salenptr)
  50. {
  51. if (getpeername(fd, sa, salenptr) < 0)
  52. err_sys("getpeername error");
  53. }
  54. void
  55. Getsockname(int fd, struct sockaddr *sa, socklen_t *salenptr)
  56. {
  57. if (getsockname(fd, sa, salenptr) < 0)
  58. err_sys("getsockname error");
  59. }
  60. void
  61. Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr)
  62. {
  63. if (getsockopt(fd, level, optname, optval, optlenptr) < 0)
  64. err_sys("getsockopt error");
  65. }
  66. #ifdef HAVE_INET6_RTH_INIT
  67. int
  68. Inet6_rth_space(int type, int segments)
  69. {
  70. int ret;
  71. ret = inet6_rth_space(type, segments);
  72. if (ret < 0)
  73. err_quit("inet6_rth_space error");
  74. return ret;
  75. }
  76. void *
  77. Inet6_rth_init(void *rthbuf, socklen_t rthlen, int type, int segments)
  78. {
  79. void *ret;
  80. ret = inet6_rth_init(rthbuf, rthlen, type, segments);
  81. if (ret == NULL)
  82. err_quit("inet6_rth_init error");
  83. return ret;
  84. }
  85. void
  86. Inet6_rth_add(void *rthbuf, const struct in6_addr *addr)
  87. {
  88. if (inet6_rth_add(rthbuf, addr) < 0)
  89. err_quit("inet6_rth_add error");
  90. }
  91. void
  92. Inet6_rth_reverse(const void *in, void *out)
  93. {
  94. if (inet6_rth_reverse(in, out) < 0)
  95. err_quit("inet6_rth_reverse error");
  96. }
  97. int
  98. Inet6_rth_segments(const void *rthbuf)
  99. {
  100. int ret;
  101. ret = inet6_rth_segments(rthbuf);
  102. if (ret < 0)
  103. err_quit("inet6_rth_segments error");
  104. return ret;
  105. }
  106. struct in6_addr *
  107. Inet6_rth_getaddr(const void *rthbuf, int idx)
  108. {
  109. struct in6_addr *ret;
  110. ret = inet6_rth_getaddr(rthbuf, idx);
  111. if (ret == NULL)
  112. err_quit("inet6_rth_getaddr error");
  113. return ret;
  114. }
  115. #endif
  116. #ifdef HAVE_KQUEUE
  117. int
  118. Kqueue(void)
  119. {
  120. int ret;
  121. if ((ret = kqueue()) < 0)
  122. err_sys("kqueue error");
  123. return ret;
  124. }
  125. int
  126. Kevent(int kq, const struct kevent *changelist, int nchanges,
  127. struct kevent *eventlist, int nevents, const struct timespec *timeout)
  128. {
  129. int ret;
  130. if ((ret = kevent(kq, changelist, nchanges,
  131. eventlist, nevents, timeout)) < 0)
  132. err_sys("kevent error");
  133. return ret;
  134. }
  135. #endif
  136. /* include Listen */
  137. void
  138. Listen(int fd, int backlog)
  139. {
  140. char *ptr;
  141. /*4can override 2nd argument with environment variable */
  142. if ( (ptr = getenv("LISTENQ")) != NULL)
  143. backlog = atoi(ptr);
  144. if (listen(fd, backlog) < 0)
  145. err_sys("listen error");
  146. }
  147. /* end Listen */
  148. #ifdef HAVE_POLL
  149. int
  150. Poll(struct pollfd *fdarray, unsigned long nfds, int timeout)
  151. {
  152. int n;
  153. if ( (n = poll(fdarray, nfds, timeout)) < 0)
  154. err_sys("poll error");
  155. return(n);
  156. }
  157. #endif
  158. ssize_t
  159. Recv(int fd, void *ptr, size_t nbytes, int flags)
  160. {
  161. ssize_t n;
  162. if ( (n = recv(fd, ptr, nbytes, flags)) < 0)
  163. err_sys("recv error");
  164. return(n);
  165. }
  166. ssize_t
  167. Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
  168. struct sockaddr *sa, socklen_t *salenptr)
  169. {
  170. ssize_t n;
  171. if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
  172. err_sys("recvfrom error");
  173. return(n);
  174. }
  175. ssize_t
  176. Recvmsg(int fd, struct msghdr *msg, int flags)
  177. {
  178. ssize_t n;
  179. if ( (n = recvmsg(fd, msg, flags)) < 0)
  180. err_sys("recvmsg error");
  181. return(n);
  182. }
  183. int
  184. Select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
  185. struct timeval *timeout)
  186. {
  187. int n;
  188. if ( (n = select(nfds, readfds, writefds, exceptfds, timeout)) < 0)
  189. err_sys("select error");
  190. return(n); /* can return 0 on timeout */
  191. }
  192. void
  193. Send(int fd, const void *ptr, size_t nbytes, int flags)
  194. {
  195. if (send(fd, ptr, nbytes, flags) != (ssize_t)nbytes)
  196. err_sys("send error");
  197. }
  198. void
  199. Sendto(int fd, const void *ptr, size_t nbytes, int flags,
  200. const struct sockaddr *sa, socklen_t salen)
  201. {
  202. if (sendto(fd, ptr, nbytes, flags, sa, salen) != (ssize_t)nbytes)
  203. err_sys("sendto error");
  204. }
  205. void
  206. Sendmsg(int fd, const struct msghdr *msg, int flags)
  207. {
  208. unsigned int i;
  209. ssize_t nbytes;
  210. nbytes = 0; /* must first figure out what return value should be */
  211. for (i = 0; i < msg->msg_iovlen; i++)
  212. nbytes += msg->msg_iov[i].iov_len;
  213. if (sendmsg(fd, msg, flags) != nbytes)
  214. err_sys("sendmsg error");
  215. }
  216. void
  217. Setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen)
  218. {
  219. if (setsockopt(fd, level, optname, optval, optlen) < 0)
  220. err_sys("setsockopt error");
  221. }
  222. void
  223. Shutdown(int fd, int how)
  224. {
  225. if (shutdown(fd, how) < 0)
  226. err_sys("shutdown error");
  227. }
  228. int
  229. Sockatmark(int fd)
  230. {
  231. int n;
  232. if ( (n = sockatmark(fd)) < 0)
  233. err_sys("sockatmark error");
  234. return(n);
  235. }
  236. /* include Socket */
  237. int
  238. Socket(int family, int type, int protocol)
  239. {
  240. int n;
  241. if ( (n = socket(family, type, protocol)) < 0)
  242. err_sys("socket error");
  243. return(n);
  244. }
  245. /* end Socket */
  246. void
  247. Socketpair(int family, int type, int protocol, int *fd)
  248. {
  249. int n;
  250. if ( (n = socketpair(family, type, protocol, fd)) < 0)
  251. err_sys("socketpair error");
  252. }