You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2129 lines
69 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /* source: xio-socket.c */
  2. /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
  3. /* Published under the GNU General Public License V.2, see file COPYING */
  4. /* this file contains the source for socket related functions, and the
  5. implementation of generic socket addresses */
  6. #include "xiosysincludes.h"
  7. #if _WITH_SOCKET
  8. #include "xioopen.h"
  9. #include "xio-ascii.h"
  10. #include "xio-socket.h"
  11. #include "xio-named.h"
  12. #include "xio-unix.h"
  13. #if WITH_IP4
  14. #include "xio-ip4.h"
  15. #endif /* WITH_IP4 */
  16. #if WITH_IP6
  17. #include "xio-ip6.h"
  18. #endif /* WITH_IP6 */
  19. #include "xio-ip.h"
  20. #include "xio-listen.h"
  21. #include "xio-ipapp.h" /*! not clean */
  22. #include "xio-tcpwrap.h"
  23. static
  24. int xioopen_socket_connect(int argc, const char *argv[], struct opt *opts,
  25. int xioflags, xiofile_t *xfd, unsigned groups,
  26. int dummy1, int dummy2, int dummy3);
  27. static
  28. int xioopen_socket_listen(int argc, const char *argv[], struct opt *opts,
  29. int xioflags, xiofile_t *xfd, unsigned groups,
  30. int dummy1, int dummy2, int dummy3);
  31. static
  32. int xioopen_socket_sendto(int argc, const char *argv[], struct opt *opts,
  33. int xioflags, xiofile_t *xfd, unsigned groups,
  34. int dummy1, int dummy2, int dummy3);
  35. static
  36. int xioopen_socket_datagram(int argc, const char *argv[], struct opt *opts,
  37. int xioflags, xiofile_t *xfd, unsigned groups,
  38. int dummy1, int dummy2, int dummy3);
  39. static
  40. int xioopen_socket_recvfrom(int argc, const char *argv[], struct opt *opts,
  41. int xioflags, xiofile_t *xfd, unsigned groups,
  42. int dummy1, int socktype, int dummy3);
  43. static
  44. int xioopen_socket_recv(int argc, const char *argv[], struct opt *opts,
  45. int xioflags, xiofile_t *xfd, unsigned groups,
  46. int dumy1, int dummy2, int dummy3);
  47. static
  48. int _xioopen_socket_sendto(const char *pfname, const char *type,
  49. const char *proto, const char *address,
  50. struct opt *opts, int xioflags, xiofile_t *xxfd,
  51. unsigned groups);
  52. static int
  53. xiolog_ancillary_socket(struct cmsghdr *cmsg, int *num,
  54. char *typbuff, int typlen,
  55. char *nambuff, int namlen,
  56. char *envbuff, int envlen,
  57. char *valbuff, int vallen);
  58. #if WITH_GENERICSOCKET
  59. /* generic socket addresses */
  60. const struct addrdesc xioaddr_socket_connect = { "socket-connect", 1, xioopen_socket_connect, GROUP_FD|GROUP_SOCKET|GROUP_CHILD|GROUP_RETRY, 0, 0, 0 HELP(":<domain>:<protocol>:<remote-address>") };
  61. #if WITH_LISTEN
  62. const struct addrdesc xioaddr_socket_listen = { "socket-listen", 1, xioopen_socket_listen, GROUP_FD|GROUP_SOCKET|GROUP_LISTEN|GROUP_RANGE|GROUP_CHILD|GROUP_RETRY, 0, 0, 0 HELP(":<domain>:<protocol>:<local-address>") };
  63. #endif /* WITH_LISTEN */
  64. const struct addrdesc xioaddr_socket_sendto = { "socket-sendto", 3, xioopen_socket_sendto, GROUP_FD|GROUP_SOCKET, 0, 0, 0 HELP(":<domain>:<type>:<protocol>:<remote-address>") };
  65. const struct addrdesc xioaddr_socket_datagram= { "socket-datagram", 3, xioopen_socket_datagram, GROUP_FD|GROUP_SOCKET|GROUP_RANGE, 0, 0, 0 HELP(":<domain>:<type>:<protocol>:<remote-address>") };
  66. const struct addrdesc xioaddr_socket_recvfrom= { "socket-recvfrom", 3, xioopen_socket_recvfrom, GROUP_FD|GROUP_SOCKET|GROUP_RANGE|GROUP_CHILD, 0, 0, 0 HELP(":<domain>:<type>:<protocol>:<local-address>") };
  67. const struct addrdesc xioaddr_socket_recv = { "socket-recv", 1, xioopen_socket_recv, GROUP_FD|GROUP_SOCKET|GROUP_RANGE, 0, 0, 0 HELP(":<domain>:<type>:<protocol>:<local-address>") };
  68. #endif /* WITH_GENERICSOCKET */
  69. /* the following options apply not only to generic socket addresses but to all
  70. addresses that have anything to do with sockets */
  71. const struct optdesc opt_so_debug = { "so-debug", "debug", OPT_SO_DEBUG, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_DEBUG };
  72. #ifdef SO_ACCEPTCONN /* AIX433 */
  73. const struct optdesc opt_so_acceptconn={ "so-acceptconn","acceptconn",OPT_SO_ACCEPTCONN,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_ACCEPTCONN};
  74. #endif /* SO_ACCEPTCONN */
  75. const struct optdesc opt_so_broadcast= { "so-broadcast", "broadcast", OPT_SO_BROADCAST,GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_BROADCAST};
  76. const struct optdesc opt_so_reuseaddr= { "so-reuseaddr", "reuseaddr", OPT_SO_REUSEADDR,GROUP_SOCKET, PH_PREBIND, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_REUSEADDR};
  77. const struct optdesc opt_so_keepalive= { "so-keepalive", "keepalive", OPT_SO_KEEPALIVE,GROUP_SOCKET, PH_FD, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_KEEPALIVE};
  78. #if HAVE_STRUCT_LINGER
  79. const struct optdesc opt_so_linger = { "so-linger", "linger", OPT_SO_LINGER, GROUP_SOCKET, PH_PASTSOCKET, TYPE_LINGER,OFUNC_SOCKOPT,SOL_SOCKET, SO_LINGER };
  80. #else /* !HAVE_STRUCT_LINGER */
  81. const struct optdesc opt_so_linger = { "so-linger", "linger", OPT_SO_LINGER, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_LINGER };
  82. #endif /* !HAVE_STRUCT_LINGER */
  83. const struct optdesc opt_so_oobinline= { "so-oobinline", "oobinline", OPT_SO_OOBINLINE,GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_OOBINLINE};
  84. const struct optdesc opt_so_sndbuf = { "so-sndbuf", "sndbuf", OPT_SO_SNDBUF, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_SNDBUF};
  85. const struct optdesc opt_so_sndbuf_late={ "so-sndbuf-late","sndbuf-late",OPT_SO_SNDBUF_LATE,GROUP_SOCKET,PH_LATE,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_SNDBUF };
  86. const struct optdesc opt_so_rcvbuf = { "so-rcvbuf", "rcvbuf", OPT_SO_RCVBUF, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_RCVBUF};
  87. const struct optdesc opt_so_rcvbuf_late={"so-rcvbuf-late","rcvbuf-late",OPT_SO_RCVBUF_LATE,GROUP_SOCKET,PH_LATE,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_RCVBUF };
  88. const struct optdesc opt_so_error = { "so-error", "error", OPT_SO_ERROR, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_ERROR};
  89. const struct optdesc opt_so_type = { "so-type", "type", OPT_SO_TYPE, GROUP_SOCKET, PH_SOCKET, TYPE_INT, OFUNC_SPEC, SOL_SOCKET, SO_TYPE };
  90. const struct optdesc opt_so_dontroute= { "so-dontroute", "dontroute", OPT_SO_DONTROUTE,GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_DONTROUTE };
  91. #ifdef SO_RCVLOWAT
  92. const struct optdesc opt_so_rcvlowat = { "so-rcvlowat", "rcvlowat", OPT_SO_RCVLOWAT, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_RCVLOWAT };
  93. #endif
  94. #ifdef SO_SNDLOWAT
  95. const struct optdesc opt_so_sndlowat = { "so-sndlowat", "sndlowat", OPT_SO_SNDLOWAT, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_SNDLOWAT };
  96. #endif
  97. /* end of setsockopt options of UNIX98 standard */
  98. #ifdef SO_AUDIT /* AIX 4.3.3 */
  99. const struct optdesc opt_so_audit = { "so-audit", "audit", OPT_SO_AUDIT, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_AUDIT };
  100. #endif /* SO_AUDIT */
  101. #ifdef SO_ATTACH_FILTER
  102. const struct optdesc opt_so_attach_filter={"so-attach-filter","attachfilter",OPT_SO_ATTACH_FILTER,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_ATTACH_FILTER};
  103. #endif
  104. #ifdef SO_DETACH_FILTER
  105. const struct optdesc opt_so_detach_filter={"so-detach-filter","detachfilter",OPT_SO_DETACH_FILTER,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_DETACH_FILTER};
  106. #endif
  107. #ifdef SO_BINDTODEVICE /* Linux: man 7 socket */
  108. const struct optdesc opt_so_bindtodevice={"so-bindtodevice","if",OPT_SO_BINDTODEVICE,GROUP_SOCKET,PH_PASTSOCKET,TYPE_NAME,OFUNC_SOCKOPT,SOL_SOCKET,SO_BINDTODEVICE};
  109. #endif
  110. #ifdef SO_BSDCOMPAT
  111. const struct optdesc opt_so_bsdcompat= { "so-bsdcompat","bsdcompat",OPT_SO_BSDCOMPAT,GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_BSDCOMPAT };
  112. #endif
  113. #ifdef SO_CKSUMRECV
  114. const struct optdesc opt_so_cksumrecv= { "so-cksumrecv","cksumrecv",OPT_SO_CKSUMRECV,GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_CKSUMRECV };
  115. #endif /* SO_CKSUMRECV */
  116. #ifdef SO_TIMESTAMP
  117. const struct optdesc opt_so_timestamp= { "so-timestamp","timestamp",OPT_SO_TIMESTAMP,GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_TIMESTAMP };
  118. #endif
  119. #ifdef SO_KERNACCEPT /* AIX 4.3.3 */
  120. const struct optdesc opt_so_kernaccept={ "so-kernaccept","kernaccept",OPT_SO_KERNACCEPT,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_KERNACCEPT};
  121. #endif /* SO_KERNACCEPT */
  122. #ifdef SO_NO_CHECK
  123. const struct optdesc opt_so_no_check = { "so-no-check", "nocheck",OPT_SO_NO_CHECK, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_NO_CHECK };
  124. #endif
  125. #ifdef SO_NOREUSEADDR /* AIX 4.3.3 */
  126. const struct optdesc opt_so_noreuseaddr={"so-noreuseaddr","noreuseaddr",OPT_SO_NOREUSEADDR,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET, SO_NOREUSEADDR};
  127. #endif /* SO_NOREUSEADDR */
  128. #ifdef SO_PASSCRED
  129. const struct optdesc opt_so_passcred = { "so-passcred", "passcred", OPT_SO_PASSCRED, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_PASSCRED};
  130. #endif
  131. #ifdef SO_PEERCRED
  132. const struct optdesc opt_so_peercred = { "so-peercred", "peercred", OPT_SO_PEERCRED, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT3,OFUNC_SOCKOPT, SOL_SOCKET, SO_PEERCRED};
  133. #endif
  134. #ifdef SO_PRIORITY
  135. const struct optdesc opt_so_priority = { "so-priority", "priority", OPT_SO_PRIORITY, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_PRIORITY};
  136. #endif
  137. #ifdef SO_REUSEPORT /* AIX 4.3.3, BSD, HP-UX, Linux >=3.9 */
  138. const struct optdesc opt_so_reuseport= { "so-reuseport","reuseport",OPT_SO_REUSEPORT,GROUP_SOCKET, PH_PREBIND, TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_REUSEPORT };
  139. #endif /* defined(SO_REUSEPORT) */
  140. #ifdef SO_SECURITY_AUTHENTICATION
  141. const struct optdesc opt_so_security_authentication={"so-security-authentication","securityauthentication",OPT_SO_SECURITY_AUTHENTICATION,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_SECURITY_AUTHENTICATION};
  142. #endif
  143. #ifdef SO_SECURITY_ENCRYPTION_NETWORK
  144. const struct optdesc opt_so_security_encryption_network={"so-security-encryption-network","securityencryptionnetwork",OPT_SO_SECURITY_ENCRYPTION_NETWORK,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_SECURITY_ENCRYPTION_NETWORK};
  145. #endif
  146. #ifdef SO_SECURITY_ENCRYPTION_TRANSPORT
  147. const struct optdesc opt_so_security_encryption_transport={"so-security-encryption-transport","securityencryptiontransport",OPT_SO_SECURITY_ENCRYPTION_TRANSPORT,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_SECURITY_ENCRYPTION_TRANSPORT};
  148. #endif
  149. #ifdef SO_USE_IFBUFS
  150. const struct optdesc opt_so_use_ifbufs={ "so-use-ifbufs","useifbufs",OPT_SO_USE_IFBUFS,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT, OFUNC_SOCKOPT, SOL_SOCKET, SO_USE_IFBUFS};
  151. #endif /* SO_USE_IFBUFS */
  152. #ifdef SO_USELOOPBACK /* AIX433, Solaris, HP-UX */
  153. const struct optdesc opt_so_useloopback={"so-useloopback","useloopback",OPT_SO_USELOOPBACK,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT, SOL_SOCKET, SO_USELOOPBACK};
  154. #endif /* SO_USELOOPBACK */
  155. #ifdef SO_DGRAM_ERRIND /* Solaris */
  156. const struct optdesc opt_so_dgram_errind={"so-dgram-errind","dgramerrind",OPT_SO_DGRAM_ERRIND,GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_DGRAM_ERRIND};
  157. #endif /* SO_DGRAM_ERRIND */
  158. #ifdef SO_DONTLINGER /* Solaris */
  159. const struct optdesc opt_so_dontlinger = {"so-dontlinger", "dontlinger", OPT_SO_DONTLINGER, GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT,OFUNC_SOCKOPT,SOL_SOCKET,SO_DONTLINGER };
  160. #endif
  161. /* the SO_PROTOTYPE is OS defined on Solaris, HP-UX; we lend this for a more
  162. general purpose */
  163. const struct optdesc opt_so_prototype = {"so-prototype", "prototype", OPT_SO_PROTOTYPE, GROUP_SOCKET,PH_SOCKET, TYPE_INT,OFUNC_SPEC, SOL_SOCKET,SO_PROTOTYPE };
  164. #ifdef FIOSETOWN
  165. const struct optdesc opt_fiosetown = { "fiosetown", NULL, OPT_FIOSETOWN, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_IOCTL, FIOSETOWN };
  166. #endif
  167. #ifdef SIOCSPGRP
  168. const struct optdesc opt_siocspgrp = { "siocspgrp", NULL, OPT_SIOCSPGRP, GROUP_SOCKET, PH_PASTSOCKET, TYPE_INT, OFUNC_IOCTL, SIOCSPGRP };
  169. #endif
  170. const struct optdesc opt_bind = { "bind", NULL, OPT_BIND, GROUP_SOCKET, PH_BIND, TYPE_STRING,OFUNC_SPEC };
  171. const struct optdesc opt_connect_timeout = { "connect-timeout", NULL, OPT_CONNECT_TIMEOUT, GROUP_SOCKET, PH_PASTSOCKET, TYPE_TIMEVAL, OFUNC_OFFSET, XIO_OFFSETOF(para.socket.connect_timeout) };
  172. const struct optdesc opt_protocol_family = { "protocol-family", "pf", OPT_PROTOCOL_FAMILY, GROUP_SOCKET, PH_PRESOCKET, TYPE_STRING, OFUNC_SPEC };
  173. const struct optdesc opt_protocol = { "protocol", NULL, OPT_PROTOCOL, GROUP_SOCKET, PH_PRESOCKET, TYPE_STRING, OFUNC_SPEC };
  174. /* generic setsockopt() options */
  175. const struct optdesc opt_setsockopt_int = { "setsockopt-int", "sockopt-int", OPT_SETSOCKOPT_INT, GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT_INT_INT, OFUNC_SOCKOPT_GENERIC, 0, 0 };
  176. const struct optdesc opt_setsockopt_bin = { "setsockopt-bin", "sockopt-bin", OPT_SETSOCKOPT_BIN, GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT_INT_BIN, OFUNC_SOCKOPT_GENERIC, 0, 0 };
  177. const struct optdesc opt_setsockopt_string = { "setsockopt-string", "sockopt-string", OPT_SETSOCKOPT_STRING, GROUP_SOCKET,PH_PASTSOCKET,TYPE_INT_INT_STRING, OFUNC_SOCKOPT_GENERIC, 0, 0 };
  178. const struct optdesc opt_null_eof = { "null-eof", NULL, OPT_NULL_EOF, GROUP_SOCKET, PH_INIT, TYPE_BOOL, OFUNC_OFFSET, XIO_OFFSETOF(para.socket.null_eof) };
  179. #if WITH_GENERICSOCKET
  180. static
  181. int xioopen_socket_connect(int argc, const char *argv[], struct opt *opts,
  182. int xioflags, xiofile_t *xxfd, unsigned groups,
  183. int dummy1, int dummy2, int dummy3) {
  184. struct single *xfd = &xxfd->stream;
  185. const char *pfname = argv[1];
  186. const char *protname = argv[2];
  187. const char *address = argv[3];
  188. char *garbage;
  189. int pf;
  190. int proto;
  191. int socktype = SOCK_STREAM;
  192. int needbind = 0;
  193. union sockaddr_union them; socklen_t themlen; size_t themsize;
  194. union sockaddr_union us; socklen_t uslen = sizeof(us);
  195. int result;
  196. if (argc != 4) {
  197. Error2("%s: wrong number of parameters (%d instead of 3)",
  198. argv[0], argc-1);
  199. return STAT_NORETRY;
  200. }
  201. pf = strtoul(pfname, &garbage, 0);
  202. if (*garbage != '\0') {
  203. Warn1("garbage in parameter: \"%s\"", garbage);
  204. }
  205. proto = strtoul(protname, &garbage, 0);
  206. if (*garbage != '\0') {
  207. Warn1("garbage in parameter: \"%s\"", garbage);
  208. }
  209. retropt_socket_pf(opts, &pf);
  210. retropt_int(opts, OPT_SO_TYPE, &socktype);
  211. /*retropt_int(opts, OPT_IP_PROTOCOL, &proto);*/
  212. xfd->howtoend = END_SHUTDOWN;
  213. if (applyopts_single(xfd, opts, PH_INIT) < 0) return -1;
  214. applyopts(-1, opts, PH_INIT);
  215. applyopts(-1, opts, PH_EARLY);
  216. themsize = 0;
  217. if ((result =
  218. dalan(address, (char *)&them.soa.sa_data, &themsize, sizeof(them)))
  219. < 0) {
  220. Error1("data too long: \"%s\"", address);
  221. } else if (result > 0) {
  222. Error1("syntax error in \"%s\"", address);
  223. }
  224. them.soa.sa_family = pf;
  225. themlen = themsize +
  226. #if HAVE_STRUCT_SOCKADDR_SALEN
  227. sizeof(them.soa.sa_len) +
  228. #endif
  229. sizeof(them.soa.sa_family);
  230. xfd->dtype = XIOREAD_STREAM|XIOWRITE_STREAM;
  231. socket_init(0, &us);
  232. if (retropt_bind(opts, 0 /*pf*/, socktype, proto, (struct sockaddr *)&us, &uslen, 3,
  233. 0, 0)
  234. != STAT_NOACTION) {
  235. needbind = true;
  236. us.soa.sa_family = pf;
  237. }
  238. if ((result =
  239. xioopen_connect(xfd,
  240. needbind?(struct sockaddr *)&us:NULL, uslen,
  241. (struct sockaddr *)&them, themlen,
  242. opts, pf, socktype, proto, false)) != 0) {
  243. return result;
  244. }
  245. if ((result = _xio_openlate(xfd, opts)) < 0) {
  246. return result;
  247. }
  248. return STAT_OK;
  249. }
  250. #if WITH_LISTEN
  251. static
  252. int xioopen_socket_listen(int argc, const char *argv[], struct opt *opts,
  253. int xioflags, xiofile_t *xxfd, unsigned groups,
  254. int dummy1, int dummy2, int dummy3) {
  255. struct single *xfd = &xxfd->stream;
  256. const char *pfname = argv[1];
  257. const char *protname = argv[2];
  258. const char *usname = argv[3];
  259. char *garbage;
  260. int pf;
  261. int proto;
  262. int socktype = SOCK_STREAM;
  263. union sockaddr_union us; socklen_t uslen; size_t ussize;
  264. struct opt *opts0;
  265. int result;
  266. if (argc != 4) {
  267. Error2("%s: wrong number of parameters (%d instead of 3)",
  268. argv[0], argc-1);
  269. return STAT_NORETRY;
  270. }
  271. pf = strtoul(pfname, &garbage, 0);
  272. if (*garbage != '\0') {
  273. Warn1("garbage in parameter: \"%s\"", garbage);
  274. }
  275. proto = strtoul(protname, &garbage, 0);
  276. if (*garbage != '\0') {
  277. Warn1("garbage in parameter: \"%s\"", garbage);
  278. }
  279. retropt_socket_pf(opts, &pf);
  280. retropt_int(opts, OPT_SO_TYPE, &socktype);
  281. /*retropt_int(opts, OPT_IP_PROTOCOL, &proto);*/
  282. xfd->howtoend = END_SHUTDOWN;
  283. socket_init(0, &us);
  284. ussize = 0;
  285. if ((result =
  286. dalan(usname, (char *)&us.soa.sa_data, &ussize, sizeof(us)))
  287. < 0) {
  288. Error1("data too long: \"%s\"", usname);
  289. } else if (result > 0) {
  290. Error1("syntax error in \"%s\"", usname);
  291. }
  292. uslen = ussize + sizeof(us.soa.sa_family)
  293. #if HAVE_STRUCT_SOCKADDR_SALEN
  294. + sizeof(us.soa.sa_len)
  295. #endif
  296. ;
  297. us.soa.sa_family = pf;
  298. if (applyopts_single(xfd, opts, PH_INIT) < 0) return -1;
  299. applyopts(-1, opts, PH_INIT);
  300. applyopts(-1, opts, PH_EARLY);
  301. opts0 = copyopts(opts, GROUP_ALL);
  302. if ((result =
  303. xioopen_listen(xfd, xioflags,
  304. (struct sockaddr *)&us, uslen,
  305. opts, opts0, 0/*instead of pf*/, socktype, proto))
  306. != STAT_OK)
  307. return result;
  308. return STAT_OK;
  309. }
  310. #endif /* WITH_LISTEN */
  311. /* we expect the form: ...:domain:type:protocol:remote-address */
  312. static
  313. int xioopen_socket_sendto(int argc, const char *argv[], struct opt *opts,
  314. int xioflags, xiofile_t *xxfd, unsigned groups,
  315. int dummy1, int dummy2, int dummy3) {
  316. int result;
  317. if (argc != 5) {
  318. Error2("%s: wrong number of parameters (%d instead of 4)",
  319. argv[0], argc-1);
  320. return STAT_NORETRY;
  321. }
  322. if ((result =
  323. _xioopen_socket_sendto(argv[1], argv[2], argv[3], argv[4],
  324. opts, xioflags, xxfd, groups))
  325. != STAT_OK) {
  326. return result;
  327. }
  328. _xio_openlate(&xxfd->stream, opts);
  329. return STAT_OK;
  330. }
  331. static
  332. int _xioopen_socket_sendto(const char *pfname, const char *type,
  333. const char *protname, const char *address,
  334. struct opt *opts, int xioflags, xiofile_t *xxfd,
  335. unsigned groups) {
  336. xiosingle_t *xfd = &xxfd->stream;
  337. char *garbage;
  338. union sockaddr_union us = {{0}};
  339. socklen_t uslen = 0; size_t ussize;
  340. size_t themsize;
  341. int pf;
  342. int socktype = SOCK_RAW;
  343. int proto;
  344. bool needbind = false;
  345. char *bindstring = NULL;
  346. int result;
  347. pf = strtoul(pfname, &garbage, 0);
  348. if (*garbage != '\0') {
  349. Warn1("garbage in parameter: \"%s\"", garbage);
  350. }
  351. socktype = strtoul(type, &garbage, 0);
  352. if (*garbage != '\0') {
  353. Warn1("garbage in parameter: \"%s\"", garbage);
  354. }
  355. proto = strtoul(protname, &garbage, 0);
  356. if (*garbage != '\0') {
  357. Warn1("garbage in parameter: \"%s\"", garbage);
  358. }
  359. retropt_socket_pf(opts, &pf);
  360. retropt_int(opts, OPT_SO_TYPE, &socktype);
  361. /*retropt_int(opts, OPT_IP_PROTOCOL, &proto);*/
  362. xfd->howtoend = END_SHUTDOWN;
  363. xfd->peersa.soa.sa_family = pf;
  364. themsize = 0;
  365. if ((result =
  366. dalan(address, (char *)&xfd->peersa.soa.sa_data, &themsize,
  367. sizeof(xfd->peersa)))
  368. < 0) {
  369. Error1("data too long: \"%s\"", address);
  370. } else if (result > 0) {
  371. Error1("syntax error in \"%s\"", address);
  372. }
  373. xfd->salen = themsize + sizeof(sa_family_t)
  374. #if HAVE_STRUCT_SOCKADDR_SALEN
  375. + sizeof(xfd->peersa.soa.sa_len)
  376. #endif
  377. ;
  378. #if HAVE_STRUCT_SOCKADDR_SALEN
  379. xfd->peersa.soa.sa_len =
  380. sizeof(xfd->peersa.soa.sa_len) + sizeof(xfd->peersa.soa.sa_family) +
  381. themsize;
  382. #endif
  383. /* ...res_opts[] */
  384. if (applyopts_single(xfd, opts, PH_INIT) < 0) return -1;
  385. applyopts(-1, opts, PH_INIT);
  386. if (pf == PF_UNSPEC) {
  387. pf = xfd->peersa.soa.sa_family;
  388. }
  389. xfd->dtype = XIODATA_RECVFROM;
  390. if (retropt_string(opts, OPT_BIND, &bindstring) == 0) {
  391. ussize = 0;
  392. if ((result =
  393. dalan(bindstring, (char *)&us.soa.sa_data, &ussize, sizeof(us)))
  394. < 0) {
  395. Error1("data too long: \"%s\"", bindstring);
  396. } else if (result > 0) {
  397. Error1("syntax error in \"%s\"", bindstring);
  398. }
  399. us.soa.sa_family = pf;
  400. uslen = ussize + sizeof(sa_family_t)
  401. #if HAVE_STRUCT_SOCKADDR_SALEN
  402. + sizeof(us.soa.sa_len)
  403. #endif
  404. ;
  405. needbind = true;
  406. }
  407. return
  408. _xioopen_dgram_sendto(needbind?&us:NULL, uslen,
  409. opts, xioflags, xfd, groups, pf, socktype, proto);
  410. }
  411. /* we expect the form: ...:domain:socktype:protocol:local-address */
  412. static
  413. int xioopen_socket_recvfrom(int argc, const char *argv[], struct opt *opts,
  414. int xioflags, xiofile_t *xxfd, unsigned groups,
  415. int dummy, int summy2, int dummy3) {
  416. struct single *xfd = &xxfd->stream;
  417. const char *pfname = argv[1];
  418. const char *typename = argv[2];
  419. const char *protname = argv[3];
  420. const char *address = argv[4];
  421. char *garbage;
  422. union sockaddr_union *us = &xfd->para.socket.la;
  423. socklen_t uslen; size_t ussize;
  424. int pf, socktype, proto;
  425. char *rangename;
  426. int result;
  427. if (argc != 5) {
  428. Error2("%s: wrong number of parameters (%d instead of 4)",
  429. argv[0], argc-1);
  430. return STAT_NORETRY;
  431. }
  432. pf = strtoul(pfname, &garbage, 0);
  433. if (*garbage != '\0') {
  434. Warn1("garbage in parameter: \"%s\"", garbage);
  435. }
  436. socktype = strtoul(typename, &garbage, 0);
  437. if (*garbage != '\0') {
  438. Warn1("garbage in parameter: \"%s\"", garbage);
  439. }
  440. proto = strtoul(protname, &garbage, 0);
  441. if (*garbage != '\0') {
  442. Warn1("garbage in parameter: \"%s\"", garbage);
  443. }
  444. retropt_socket_pf(opts, &pf);
  445. retropt_int(opts, OPT_SO_TYPE, &socktype);
  446. /*retropt_int(opts, OPT_IP_PROTOCOL, &proto);*/
  447. xfd->howtoend = END_NONE;
  448. ussize = 0;
  449. if ((result =
  450. dalan(address, (char *)&us->soa.sa_data, &ussize, sizeof(*us)))
  451. < 0) {
  452. Error1("data too long: \"%s\"", address);
  453. } else if (result > 0) {
  454. Error1("syntax error in \"%s\"", address);
  455. }
  456. us->soa.sa_family = pf;
  457. uslen = ussize + sizeof(us->soa.sa_family)
  458. #if HAVE_STRUCT_SOCKADDR_SALEN
  459. + sizeof(us->soa.sa_len);
  460. #endif
  461. ;
  462. xfd->dtype = XIOREAD_RECV|XIOWRITE_SENDTO;
  463. if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
  464. if (xioparserange(rangename, 0, &xfd->para.socket.range) < 0) {
  465. return STAT_NORETRY;
  466. }
  467. xfd->para.socket.dorange = true;
  468. free(rangename);
  469. }
  470. if ((result =
  471. _xioopen_dgram_recvfrom(xfd, xioflags, &us->soa, uslen,
  472. opts, pf, socktype, proto, E_ERROR))
  473. != STAT_OK) {
  474. return result;
  475. }
  476. _xio_openlate(xfd, opts);
  477. return STAT_OK;
  478. }
  479. /* we expect the form: ...:domain:type:protocol:local-address */
  480. static
  481. int xioopen_socket_recv(int argc, const char *argv[], struct opt *opts,
  482. int xioflags, xiofile_t *xxfd, unsigned groups,
  483. int dummy1, int dummy2, int dummy3) {
  484. struct single *xfd = &xxfd->stream;
  485. const char *pfname = argv[1];
  486. const char *typename = argv[2];
  487. const char *protname = argv[3];
  488. const char *address = argv[4];
  489. char *garbage;
  490. union sockaddr_union us;
  491. socklen_t uslen; size_t ussize;
  492. int pf, socktype, proto;
  493. char *rangename;
  494. int result;
  495. if (argc != 5) {
  496. Error2("%s: wrong number of parameters (%d instead of 4)",
  497. argv[0], argc-1);
  498. return STAT_NORETRY;
  499. }
  500. pf = strtoul(pfname, &garbage, 0);
  501. if (*garbage != '\0') {
  502. Warn1("garbage in parameter: \"%s\"", garbage);
  503. }
  504. socktype = strtoul(typename, &garbage, 0);
  505. if (*garbage != '\0') {
  506. Warn1("garbage in parameter: \"%s\"", garbage);
  507. }
  508. proto = strtoul(protname, &garbage, 0);
  509. if (*garbage != '\0') {
  510. Warn1("garbage in parameter: \"%s\"", garbage);
  511. }
  512. retropt_socket_pf(opts, &pf);
  513. retropt_int(opts, OPT_SO_TYPE, &socktype);
  514. /*retropt_int(opts, OPT_IP_PROTOCOL, &proto);*/
  515. xfd->howtoend = END_NONE;
  516. ussize = 0;
  517. if ((result =
  518. dalan(address, (char *)&us.soa.sa_data, &ussize, sizeof(us)))
  519. < 0) {
  520. Error1("data too long: \"%s\"", address);
  521. } else if (result > 0) {
  522. Error1("syntax error in \"%s\"", address);
  523. }
  524. us.soa.sa_family = pf;
  525. uslen = ussize + sizeof(sa_family_t)
  526. #if HAVE_STRUCT_SOCKADDR_SALEN
  527. +sizeof(us.soa.sa_len)
  528. #endif
  529. ;
  530. xfd->dtype = XIOREAD_RECV;
  531. xfd->para.socket.la.soa.sa_family = pf;
  532. if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
  533. if (xioparserange(rangename, 0, &xfd->para.socket.range) < 0) {
  534. return STAT_NORETRY;
  535. }
  536. xfd->para.socket.dorange = true;
  537. free(rangename);
  538. }
  539. if ((result =
  540. _xioopen_dgram_recv(xfd, xioflags, &us.soa,
  541. uslen, opts, pf, socktype, proto, E_ERROR))
  542. != STAT_OK) {
  543. return result;
  544. }
  545. _xio_openlate(xfd, opts);
  546. return STAT_OK;
  547. }
  548. /* we expect the form: ...:domain:type:protocol:remote-address */
  549. static
  550. int xioopen_socket_datagram(int argc, const char *argv[], struct opt *opts,
  551. int xioflags, xiofile_t *xxfd, unsigned groups,
  552. int dummy1, int dummy2, int dummy3) {
  553. xiosingle_t *xfd = &xxfd->stream;
  554. const char *pfname = argv[1];
  555. const char *typename = argv[2];
  556. const char *protname = argv[3];
  557. const char *address = argv[4];
  558. char *garbage;
  559. char *rangename;
  560. size_t themsize;
  561. int pf;
  562. int result;
  563. if (argc != 5) {
  564. Error2("%s: wrong number of parameters (%d instead of 4)",
  565. argv[0], argc-1);
  566. return STAT_NORETRY;
  567. }
  568. pf = strtoul(pfname, &garbage, 0);
  569. if (*garbage != '\0') {
  570. Warn1("garbage in parameter: \"%s\"", garbage);
  571. }
  572. retropt_socket_pf(opts, &pf);
  573. /*retropt_int(opts, OPT_IP_PROTOCOL, &proto);*/
  574. xfd->howtoend = END_SHUTDOWN;
  575. xfd->peersa.soa.sa_family = pf;
  576. themsize = 0;
  577. if ((result =
  578. dalan(address, (char *)&xfd->peersa.soa.sa_data, &themsize,
  579. sizeof(xfd->peersa)))
  580. < 0) {
  581. Error1("data too long: \"%s\"", address);
  582. } else if (result > 0) {
  583. Error1("syntax error in \"%s\"", address);
  584. }
  585. xfd->salen = themsize + sizeof(sa_family_t);
  586. #if HAVE_STRUCT_SOCKADDR_SALEN
  587. xfd->peersa.soa.sa_len =
  588. sizeof(xfd->peersa.soa.sa_len) + sizeof(xfd->peersa.soa.sa_family) +
  589. themsize;
  590. #endif
  591. if ((result =
  592. _xioopen_socket_sendto(pfname, typename, protname, address,
  593. opts, xioflags, xxfd, groups))
  594. != STAT_OK) {
  595. return result;
  596. }
  597. xfd->dtype = XIOREAD_RECV|XIOWRITE_SENDTO;
  598. xfd->para.socket.la.soa.sa_family = xfd->peersa.soa.sa_family;
  599. /* which reply sockets will accept - determine by range option */
  600. if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
  601. if (xioparserange(rangename, 0, &xfd->para.socket.range) < 0) {
  602. free(rangename);
  603. return STAT_NORETRY;
  604. }
  605. xfd->para.socket.dorange = true;
  606. xfd->dtype |= XIOREAD_RECV_CHECKRANGE;
  607. free(rangename);
  608. }
  609. _xio_openlate(xfd, opts);
  610. return STAT_OK;
  611. }
  612. #endif /* WITH_GENERICSOCKET */
  613. /* a subroutine that is common to all socket addresses that want to connect
  614. to a peer address.
  615. might fork.
  616. applies and consumes the following options:
  617. PH_PASTSOCKET, PH_FD, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_CONNECT,
  618. PH_CONNECTED, PH_LATE,
  619. OFUNC_OFFSET,
  620. OPT_SO_TYPE, OPT_SO_PROTOTYPE, OPT_USER, OPT_GROUP, OPT_CLOEXEC
  621. returns 0 on success.
  622. */
  623. int _xioopen_connect(struct single *xfd, struct sockaddr *us, size_t uslen,
  624. struct sockaddr *them, size_t themlen,
  625. struct opt *opts, int pf, int socktype, int protocol,
  626. bool alt, int level) {
  627. int fcntl_flags = 0;
  628. char infobuff[256];
  629. union sockaddr_union la;
  630. socklen_t lalen = themlen;
  631. int _errno;
  632. int result;
  633. if ((xfd->fd = xiosocket(opts, pf, socktype, protocol, level)) < 0) {
  634. return STAT_RETRYLATER;
  635. }
  636. applyopts_offset(xfd, opts);
  637. applyopts(xfd->fd, opts, PH_PASTSOCKET);
  638. applyopts(xfd->fd, opts, PH_FD);
  639. applyopts_cloexec(xfd->fd, opts);
  640. applyopts(xfd->fd, opts, PH_PREBIND);
  641. applyopts(xfd->fd, opts, PH_BIND);
  642. #if WITH_TCP || WITH_UDP
  643. if (alt) {
  644. union sockaddr_union sin, *sinp;
  645. unsigned short *port, i, N;
  646. div_t dv;
  647. /* prepare sockaddr for bind probing */
  648. if (us) {
  649. sinp = (union sockaddr_union *)us;
  650. } else {
  651. if (them->sa_family == AF_INET) {
  652. socket_in_init(&sin.ip4);
  653. #if WITH_IP6
  654. } else {
  655. socket_in6_init(&sin.ip6);
  656. #endif
  657. }
  658. sinp = &sin;
  659. }
  660. if (them->sa_family == AF_INET) {
  661. port = &sin.ip4.sin_port;
  662. #if WITH_IP6
  663. } else if (them->sa_family == AF_INET6) {
  664. port = &sin.ip6.sin6_port;
  665. #endif
  666. } else {
  667. port = 0; /* just to make compiler happy */
  668. }
  669. /* combine random+step variant to quickly find a free port when only
  670. few are in use, and certainly find a free port in defined time even
  671. if there are almost all in use */
  672. /* dirt 1: having tcp/udp code in socket function */
  673. /* dirt 2: using a time related system call for init of random */
  674. {
  675. /* generate a random port, with millisecond random init */
  676. #if 0
  677. struct timeb tb;
  678. ftime(&tb);
  679. srandom(tb.time*1000+tb.millitm);
  680. #else
  681. struct timeval tv;
  682. struct timezone tz;
  683. tz.tz_minuteswest = 0;
  684. tz.tz_dsttime = 0;
  685. if ((result = Gettimeofday(&tv, &tz)) < 0) {
  686. Warn2("gettimeofday(%p, {0,0}): %s", &tv, strerror(errno));
  687. }
  688. srandom(tv.tv_sec*1000000+tv.tv_usec);
  689. #endif
  690. }
  691. dv = div(random(), IPPORT_RESERVED-XIO_IPPORT_LOWER);
  692. i = N = XIO_IPPORT_LOWER + dv.rem;
  693. do { /* loop over lowport bind() attempts */
  694. *port = htons(i);
  695. if (Bind(xfd->fd, (struct sockaddr *)sinp, sizeof(*sinp)) < 0) {
  696. Msg4(errno==EADDRINUSE?E_INFO:level,
  697. "bind(%d, {%s}, "F_Zd"): %s", xfd->fd,
  698. sockaddr_info(&sinp->soa, sizeof(*sinp), infobuff, sizeof(infobuff)),
  699. sizeof(*sinp), strerror(errno));
  700. if (errno != EADDRINUSE) {
  701. Close(xfd->fd);
  702. return STAT_RETRYLATER;
  703. }
  704. } else {
  705. break; /* could bind to port, good, continue past loop */
  706. }
  707. --i; if (i < XIO_IPPORT_LOWER) i = IPPORT_RESERVED-1;
  708. if (i == N) {
  709. Msg(level, "no low port available");
  710. /*errno = EADDRINUSE; still assigned */
  711. Close(xfd->fd);
  712. return STAT_RETRYLATER;
  713. }
  714. } while (i != N);
  715. } else
  716. #endif /* WITH_TCP || WITH_UDP */
  717. if (us) {
  718. if (Bind(xfd->fd, us, uslen) < 0) {
  719. Msg4(level, "bind(%d, {%s}, "F_Zd"): %s",
  720. xfd->fd, sockaddr_info(us, uslen, infobuff, sizeof(infobuff)),
  721. uslen, strerror(errno));
  722. Close(xfd->fd);
  723. return STAT_RETRYLATER;
  724. }
  725. }
  726. applyopts(xfd->fd, opts, PH_PASTBIND);
  727. applyopts(xfd->fd, opts, PH_CONNECT);
  728. if (xfd->para.socket.connect_timeout.tv_sec != 0 ||
  729. xfd->para.socket.connect_timeout.tv_usec != 0) {
  730. fcntl_flags = Fcntl(xfd->fd, F_GETFL);
  731. Fcntl_l(xfd->fd, F_SETFL, fcntl_flags|O_NONBLOCK);
  732. }
  733. result = Connect(xfd->fd, (struct sockaddr *)them, themlen);
  734. _errno = errno;
  735. la.soa.sa_family = them->sa_family; lalen = sizeof(la);
  736. if (Getsockname(xfd->fd, &la.soa, &lalen) < 0) {
  737. Msg4(level-1, "getsockname(%d, %p, {%d}): %s",
  738. xfd->fd, &la.soa, lalen, strerror(errno));
  739. }
  740. errno = _errno;
  741. if (result < 0) {
  742. if (errno == EINPROGRESS) {
  743. if (xfd->para.socket.connect_timeout.tv_sec != 0 ||
  744. xfd->para.socket.connect_timeout.tv_usec != 0) {
  745. struct timeval timeout;
  746. struct pollfd writefd;
  747. int result;
  748. Info4("connect(%d, %s, "F_Zd"): %s",
  749. xfd->fd, sockaddr_info(them, themlen, infobuff, sizeof(infobuff)),
  750. themlen, strerror(errno));
  751. timeout = xfd->para.socket.connect_timeout;
  752. writefd.fd = xfd->fd;
  753. writefd.events = (POLLOUT|POLLERR);
  754. result = xiopoll(&writefd, 1, &timeout);
  755. if (result < 0) {
  756. Msg4(level, "xiopoll({%d,POLLOUT|POLLERR},,{"F_tv_sec"."F_tv_usec"): %s",
  757. xfd->fd, timeout.tv_sec, timeout.tv_usec, strerror(errno));
  758. return STAT_RETRYLATER;
  759. }
  760. if (result == 0) {
  761. Msg2(level, "connecting to %s: %s",
  762. sockaddr_info(them, themlen, infobuff, sizeof(infobuff)),
  763. strerror(ETIMEDOUT));
  764. return STAT_RETRYLATER;
  765. }
  766. if (writefd.revents & POLLERR) {
  767. #if 0
  768. unsigned char dummy[1];
  769. Read(xfd->fd, &dummy, 1); /* get error message */
  770. Msg2(level, "connecting to %s: %s",
  771. sockaddr_info(them, infobuff, sizeof(infobuff)),
  772. strerror(errno));
  773. #else
  774. Connect(xfd->fd, them, themlen); /* get error message */
  775. Msg4(level, "connect(%d, %s, "F_Zd"): %s",
  776. xfd->fd, sockaddr_info(them, themlen, infobuff, sizeof(infobuff)),
  777. themlen, strerror(errno));
  778. #endif
  779. return STAT_RETRYLATER;
  780. }
  781. /* otherwise OK */
  782. Fcntl_l(xfd->fd, F_SETFL, fcntl_flags);
  783. } else {
  784. Warn4("connect(%d, %s, "F_Zd"): %s",
  785. xfd->fd, sockaddr_info(them, themlen, infobuff, sizeof(infobuff)),
  786. themlen, strerror(errno));
  787. }
  788. } else if (pf == PF_UNIX && errno == EPROTOTYPE) {
  789. /* this is for UNIX domain sockets: a connect attempt seems to be
  790. the only way to distinguish stream and datagram sockets */
  791. int _errno = errno;
  792. Info4("connect(%d, %s, "F_Zd"): %s",
  793. xfd->fd, sockaddr_info(them, themlen, infobuff, sizeof(infobuff)),
  794. themlen, strerror(errno));
  795. #if 0
  796. Info("assuming datagram socket");
  797. xfd->dtype = DATA_RECVFROM;
  798. xfd->salen = themlen;
  799. memcpy(&xfd->peersa.soa, them, xfd->salen);
  800. #endif
  801. /*!!! and remove bind socket */
  802. Close(xfd->fd); xfd->fd = -1;
  803. errno = _errno;
  804. return -1;
  805. } else {
  806. Msg4(level, "connect(%d, %s, "F_Zd"): %s",
  807. xfd->fd, sockaddr_info(them, themlen, infobuff, sizeof(infobuff)),
  808. themlen, strerror(errno));
  809. Close(xfd->fd);
  810. return STAT_RETRYLATER;
  811. }
  812. } else { /* result >= 0 */
  813. Notice1("successfully connected from local address %s",
  814. sockaddr_info(&la.soa, themlen, infobuff, sizeof(infobuff)));
  815. }
  816. applyopts_fchown(xfd->fd, opts); /* OPT_USER, OPT_GROUP */
  817. applyopts(xfd->fd, opts, PH_CONNECTED);
  818. applyopts(xfd->fd, opts, PH_LATE);
  819. return STAT_OK;
  820. }
  821. /* a subroutine that is common to all socket addresses that want to connect
  822. to a peer address.
  823. might fork.
  824. applies and consumes the following option:
  825. PH_PASTSOCKET, PH_FD, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_CONNECT,
  826. PH_CONNECTED, PH_LATE,
  827. OFUNC_OFFSET,
  828. OPT_FORK, OPT_SO_TYPE, OPT_SO_PROTOTYPE, OPT_USER, OPT_GROUP, OPT_CLOEXEC
  829. returns 0 on success.
  830. */
  831. int xioopen_connect(struct single *xfd, struct sockaddr *us, size_t uslen,
  832. struct sockaddr *them, size_t themlen,
  833. struct opt *opts, int pf, int socktype, int protocol,
  834. bool alt) {
  835. bool dofork = false;
  836. struct opt *opts0;
  837. char infobuff[256];
  838. int level;
  839. int result;
  840. retropt_bool(opts, OPT_FORK, &dofork);
  841. opts0 = copyopts(opts, GROUP_ALL);
  842. Notice1("opening connection to %s",
  843. sockaddr_info(them, themlen, infobuff, sizeof(infobuff)));
  844. do { /* loop over retries and forks */
  845. #if WITH_RETRY
  846. if (xfd->forever || xfd->retry) {
  847. level = E_INFO;
  848. } else
  849. #endif /* WITH_RETRY */
  850. level = E_ERROR;
  851. result =
  852. _xioopen_connect(xfd, us, uslen, them, themlen, opts,
  853. pf, socktype, protocol, alt, level);
  854. switch (result) {
  855. case STAT_OK: break;
  856. #if WITH_RETRY
  857. case STAT_RETRYLATER:
  858. if (xfd->forever || xfd->retry) {
  859. --xfd->retry;
  860. if (result == STAT_RETRYLATER) {
  861. Nanosleep(&xfd->intervall, NULL);
  862. }
  863. dropopts(opts, PH_ALL); opts = copyopts(opts0, GROUP_ALL);
  864. continue;
  865. }
  866. return STAT_NORETRY;
  867. #endif /* WITH_RETRY */
  868. default:
  869. return result;
  870. }
  871. if (dofork) {
  872. xiosetchilddied(); /* set SIGCHLD handler */
  873. }
  874. #if WITH_RETRY
  875. if (dofork) {
  876. pid_t pid;
  877. int level = E_ERROR;
  878. if (xfd->forever || xfd->retry) {
  879. level = E_WARN; /* most users won't expect a problem here,
  880. so Notice is too weak */
  881. }
  882. while ((pid = xio_fork(false, level)) < 0) {
  883. --xfd->retry;
  884. if (xfd->forever || xfd->retry) {
  885. dropopts(opts, PH_ALL); opts = copyopts(opts0, GROUP_ALL);
  886. Nanosleep(&xfd->intervall, NULL); continue;
  887. }
  888. return STAT_RETRYLATER;
  889. }
  890. if (pid == 0) { /* child process */
  891. break;
  892. }
  893. /* parent process */
  894. Close(xfd->fd);
  895. /* with and without retry */
  896. Nanosleep(&xfd->intervall, NULL);
  897. dropopts(opts, PH_ALL); opts = copyopts(opts0, GROUP_ALL);
  898. continue; /* with next socket() bind() connect() */
  899. } else
  900. #endif /* WITH_RETRY */
  901. {
  902. break;
  903. }
  904. #if 0
  905. if ((result = _xio_openlate(fd, opts)) < 0)
  906. return result;
  907. #endif
  908. } while (true);
  909. return 0;
  910. }
  911. /* common to xioopen_udp_sendto, ..unix_sendto, ..rawip
  912. applies and consumes the following option:
  913. PH_PASTSOCKET, PH_FD, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_CONNECTED, PH_LATE
  914. OFUNC_OFFSET
  915. OPT_SO_TYPE, OPT_SO_PROTOTYPE, OPT_USER, OPT_GROUP, OPT_CLOEXEC
  916. */
  917. int _xioopen_dgram_sendto(/* them is already in xfd->peersa */
  918. union sockaddr_union *us, socklen_t uslen,
  919. struct opt *opts,
  920. int xioflags, xiosingle_t *xfd, unsigned groups,
  921. int pf, int socktype, int ipproto) {
  922. int level = E_ERROR;
  923. union sockaddr_union la; socklen_t lalen = sizeof(la);
  924. char infobuff[256];
  925. if ((xfd->fd = xiosocket(opts, pf, socktype, ipproto, level)) < 0) {
  926. return STAT_RETRYLATER;
  927. }
  928. applyopts_offset(xfd, opts);
  929. applyopts_single(xfd, opts, PH_PASTSOCKET);
  930. applyopts(xfd->fd, opts, PH_PASTSOCKET);
  931. applyopts(xfd->fd, opts, PH_FD);
  932. applyopts_cloexec(xfd->fd, opts);
  933. applyopts(xfd->fd, opts, PH_PREBIND);
  934. applyopts(xfd->fd, opts, PH_BIND);
  935. if (us) {
  936. if (Bind(xfd->fd, (struct sockaddr *)us, uslen) < 0) {
  937. Msg4(level, "bind(%d, {%s}, "F_socklen"): %s",
  938. xfd->fd, sockaddr_info((struct sockaddr *)us, uslen, infobuff, sizeof(infobuff)),
  939. uslen, strerror(errno));
  940. Close(xfd->fd);
  941. return STAT_RETRYLATER;
  942. }
  943. }
  944. applyopts(xfd->fd, opts, PH_PASTBIND);
  945. /*applyopts(xfd->fd, opts, PH_CONNECT);*/
  946. if (Getsockname(xfd->fd, &la.soa, &lalen) < 0) {
  947. Warn4("getsockname(%d, %p, {%d}): %s",
  948. xfd->fd, &la.soa, lalen, strerror(errno));
  949. }
  950. applyopts_fchown(xfd->fd, opts);
  951. applyopts(xfd->fd, opts, PH_CONNECTED);
  952. applyopts(xfd->fd, opts, PH_LATE);
  953. /* xfd->dtype = DATA_RECVFROM; *//* no, the caller must set this (ev _SKIPIP) */
  954. Notice1("successfully prepared local socket %s",
  955. sockaddr_info(&la.soa, lalen, infobuff, sizeof(infobuff)));
  956. return STAT_OK;
  957. }
  958. /* when the recvfrom address (with option fork) receives a packet it keeps this
  959. packet in the IP stacks input queue and forks a sub process. The sub process
  960. then reads this packet for processing its data.
  961. There is a problem because the parent process would find the same packet
  962. again if it calls select()/poll() before the child process reads the
  963. packet.
  964. To solve this problem we implement the following mechanism:
  965. The sub process sends a SIGUSR1 when it has read the packet (or a SIGCHLD if
  966. it dies before). The parent process waits until it receives that signal and
  967. only then continues to listen.
  968. To prevent a signal from another process to trigger our loop, we pass the
  969. pid of the sub process to the signal handler in xio_waitingfor. The signal
  970. handler sets xio_hashappened if the pid matched.
  971. */
  972. static pid_t xio_waitingfor; /* info from recv loop to signal handler:
  973. indicates the pid of the child process
  974. that should send us the USR1 signal */
  975. static bool xio_hashappened; /* info from signal handler to loop: child
  976. process has read ("consumed") the packet */
  977. /* this is the signal handler for USR1 and CHLD */
  978. void xiosigaction_hasread(int signum
  979. #if HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined(SA_SIGINFO)
  980. , siginfo_t *siginfo, void *ucontext
  981. #endif
  982. ) {
  983. pid_t pid;
  984. int _errno;
  985. int status = 0;
  986. bool wassig = false;
  987. _errno = errno;
  988. diag_in_handler = 1;
  989. #if HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined(SA_SIGINFO)
  990. Debug5("xiosigaction_hasread(%d, {%d,%d,%d,"F_pid"}, )",
  991. signum, siginfo->si_signo, siginfo->si_errno, siginfo->si_code,
  992. siginfo->si_pid);
  993. #else
  994. Debug1("xiosigaction_hasread(%d)", signum);
  995. #endif
  996. if (signum == SIGCHLD) {
  997. do {
  998. pid = Waitpid(-1, &status, WNOHANG);
  999. if (pid == 0) {
  1000. Msg(wassig?E_INFO:E_WARN,
  1001. "waitpid(-1, {}, WNOHANG): no child has exited");
  1002. Info("xiosigaction_hasread() finished");
  1003. Debug("xiosigaction_hasread() ->");
  1004. diag_in_handler = 0;
  1005. errno = _errno;
  1006. return;
  1007. } else if (pid < 0 && errno == ECHILD) {
  1008. Msg(wassig?E_INFO:E_WARN,
  1009. "waitpid(-1, {}, WNOHANG): "F_strerror);
  1010. Info("xiosigaction_hasread() finished");
  1011. Debug("xiosigaction_hasread() ->");
  1012. diag_in_handler = 0;
  1013. errno = _errno;
  1014. return;
  1015. }
  1016. wassig = true;
  1017. if (pid < 0) {
  1018. Warn1("waitpid(-1, {%d}, WNOHANG): "F_strerror, status);
  1019. Info("xiosigaction_hasread() finished");
  1020. Debug("xiosigaction_hasread() ->");
  1021. diag_in_handler = 0;
  1022. errno = _errno;
  1023. return;
  1024. }
  1025. if (pid == xio_waitingfor) {
  1026. xio_hashappened = true;
  1027. Debug("xiosigaction_hasread() ->");
  1028. diag_in_handler = 0;
  1029. errno = _errno;
  1030. return;
  1031. }
  1032. } while (1);
  1033. }
  1034. #if HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined(SA_SIGINFO)
  1035. if (xio_waitingfor == siginfo->si_pid) {
  1036. xio_hashappened = true;
  1037. }
  1038. #else
  1039. xio_hashappened = true;
  1040. #endif
  1041. #if !HAVE_SIGACTION
  1042. Signal(sig, xiosigaction_hasread);
  1043. #endif /* !HAVE_SIGACTION */
  1044. Debug("xiosigaction_hasread() ->");
  1045. diag_in_handler = 0;
  1046. errno = _errno;
  1047. return;
  1048. }
  1049. /* waits for incoming packet, checks its source address and port. Depending
  1050. on fork option, it may fork a subprocess.
  1051. Returns STAT_OK if a the packet was accepted; with fork option, this is already in
  1052. a new subprocess!
  1053. Other return values indicate a problem; this can happen in the master
  1054. process or in a subprocess.
  1055. This function does not retry. If you need retries, handle this is a
  1056. loop in the calling function.
  1057. after fork, we set the forever/retry of the child process to 0
  1058. applies and consumes the following options:
  1059. PH_INIT, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_EARLY, PH_PREOPEN, PH_FD,
  1060. PH_CONNECTED, PH_LATE, PH_LATE2
  1061. OPT_FORK, OPT_SO_TYPE, OPT_SO_PROTOTYPE, cloexec, OPT_RANGE, tcpwrap
  1062. */
  1063. int _xioopen_dgram_recvfrom(struct single *xfd, int xioflags,
  1064. struct sockaddr *us, socklen_t uslen,
  1065. struct opt *opts,
  1066. int pf, int socktype, int proto, int level) {
  1067. char *rangename;
  1068. bool dofork = false;
  1069. pid_t pid; /* mostly int; only used with fork */
  1070. char infobuff[256];
  1071. char lisname[256];
  1072. bool drop = false; /* true if current packet must be dropped */
  1073. int result;
  1074. retropt_bool(opts, OPT_FORK, &dofork);
  1075. if (dofork) {
  1076. if (!(xioflags & XIO_MAYFORK)) {
  1077. Error("option fork not allowed here");
  1078. return STAT_NORETRY;
  1079. }
  1080. xfd->flags |= XIO_DOESFORK;
  1081. }
  1082. if (applyopts_single(xfd, opts, PH_INIT) < 0) return STAT_NORETRY;
  1083. if ((xfd->fd = xiosocket(opts, pf, socktype, proto, level)) < 0) {
  1084. return STAT_RETRYLATER;
  1085. }
  1086. applyopts_single(xfd, opts, PH_PASTSOCKET);
  1087. applyopts(xfd->fd, opts, PH_PASTSOCKET);
  1088. applyopts_cloexec(xfd->fd, opts);
  1089. applyopts(xfd->fd, opts, PH_PREBIND);
  1090. applyopts(xfd->fd, opts, PH_BIND);
  1091. if ((us != NULL) && Bind(xfd->fd, (struct sockaddr *)us, uslen) < 0) {
  1092. Msg4(level, "bind(%d, {%s}, "F_socklen"): %s", xfd->fd,
  1093. sockaddr_info(us, uslen, infobuff, sizeof(infobuff)), uslen,
  1094. strerror(errno));
  1095. Close(xfd->fd);
  1096. return STAT_RETRYLATER;
  1097. }
  1098. #if WITH_UNIX
  1099. if (pf == AF_UNIX && us != NULL) {
  1100. applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_FD);
  1101. }
  1102. #endif
  1103. applyopts(xfd->fd, opts, PH_PASTBIND);
  1104. #if WITH_UNIX
  1105. if (pf == AF_UNIX && us != NULL) {
  1106. /*applyopts_early(((struct sockaddr_un *)us)->sun_path, opts);*/
  1107. applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_EARLY);
  1108. applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_PREOPEN);
  1109. }
  1110. #endif /* WITH_UNIX */
  1111. /* for generic sockets, this has already been retrieved */
  1112. if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
  1113. if (xioparserange(rangename, pf, &xfd->para.socket.range)
  1114. < 0) {
  1115. free(rangename);
  1116. return STAT_NORETRY;
  1117. }
  1118. free(rangename);
  1119. xfd->para.socket.dorange = true;
  1120. }
  1121. #if (WITH_TCP || WITH_UDP) && WITH_LIBWRAP
  1122. xio_retropt_tcpwrap(xfd, opts);
  1123. #endif /* && (WITH_TCP || WITH_UDP) && WITH_LIBWRAP */
  1124. if (xioopts.logopt == 'm') {
  1125. Info("starting recvfrom loop, switching to syslog");
  1126. diag_set('y', xioopts.syslogfac); xioopts.logopt = 'y';
  1127. } else {
  1128. Info("starting recvfrom loop");
  1129. }
  1130. if (dofork) {
  1131. #if HAVE_SIGACTION
  1132. {
  1133. struct sigaction act;
  1134. memset(&act, 0, sizeof(struct sigaction));
  1135. act.sa_flags = SA_NOCLDSTOP/*|SA_RESTART*/
  1136. #ifdef SA_SIGINFO /* not on Linux 2.0(.33) */
  1137. |SA_SIGINFO
  1138. #endif
  1139. #ifdef SA_NOMASK
  1140. |SA_NOMASK
  1141. #endif
  1142. ;
  1143. #if HAVE_STRUCT_SIGACTION_SA_SIGACTION && defined(SA_SIGINFO)
  1144. act.sa_sigaction = xiosigaction_hasread;
  1145. #else /* Linux 2.0(.33) does not have sigaction.sa_sigaction */
  1146. act.sa_handler = xiosigaction_hasread;
  1147. #endif
  1148. sigfillset(&act.sa_mask);
  1149. if (Sigaction(SIGUSR1, &act, NULL) < 0) {
  1150. /*! Linux man does not explicitely say that errno is defined */
  1151. Warn1("sigaction(SIGUSR1, {&xiosigaction_subaddr_ok}, NULL): %s", strerror(errno));
  1152. }
  1153. if (Sigaction(SIGCHLD, &act, NULL) < 0) {
  1154. /*! Linux man does not explicitely say that errno is defined */
  1155. Warn1("sigaction(SIGCHLD, {&xiosigaction_subaddr_ok}, NULL): %s", strerror(errno));
  1156. }
  1157. }
  1158. #else /* !HAVE_SIGACTION */
  1159. /*!!!*/
  1160. if (Signal(SIGUSR1, xiosigaction_hasread) == SIG_ERR) {
  1161. Warn1("signal(SIGUSR1, xiosigaction_hasread): %s", strerror(errno));
  1162. }
  1163. if (Signal(SIGCHLD, xiosigaction_hasread) == SIG_ERR) {
  1164. Warn1("signal(SIGCHLD, xiosigaction_hasread): %s", strerror(errno));
  1165. }
  1166. #endif /* !HAVE_SIGACTION */
  1167. }
  1168. while (true) { /* but we only loop if fork option is set */
  1169. char peername[256];
  1170. union sockaddr_union _peername;
  1171. union sockaddr_union _sockname;
  1172. union sockaddr_union *pa = &_peername; /* peer address */
  1173. union sockaddr_union *la = &_sockname; /* local address */
  1174. socklen_t palen = sizeof(_peername); /* peer address size */
  1175. char ctrlbuff[1024]; /* ancillary messages */
  1176. struct msghdr msgh = {0};
  1177. socket_init(pf, pa);
  1178. if (drop) {
  1179. char *dummy[2];
  1180. Recv(xfd->fd, dummy, sizeof(dummy), 0);
  1181. drop = true;
  1182. }
  1183. /* loop until select()/poll() returns valid */
  1184. do {
  1185. struct pollfd readfd;
  1186. /*? int level = E_ERROR;*/
  1187. if (us != NULL) {
  1188. Notice1("receiving on %s", sockaddr_info(us, uslen, lisname, sizeof(lisname)));
  1189. } else {
  1190. Notice1("receiving IP protocol %u", proto);
  1191. }
  1192. readfd.fd = xfd->fd;
  1193. readfd.events = POLLIN;
  1194. if (xiopoll(&readfd, 1, NULL) > 0) {
  1195. break;
  1196. }
  1197. if (errno == EINTR) {
  1198. continue;
  1199. }
  1200. Msg2(level, "poll({%d,,},,-1): %s", xfd->fd, strerror(errno));
  1201. Close(xfd->fd);
  1202. return STAT_RETRYLATER;
  1203. } while (true);
  1204. msgh.msg_name = pa;
  1205. msgh.msg_namelen = palen;
  1206. #if HAVE_STRUCT_MSGHDR_MSGCONTROL
  1207. msgh.msg_control = ctrlbuff;
  1208. #endif
  1209. #if HAVE_STRUCT_MSGHDR_MSGCONTROLLEN
  1210. msgh.msg_controllen = sizeof(ctrlbuff);
  1211. #endif
  1212. if (xiogetpacketsrc(xfd->fd, &msgh) < 0) {
  1213. return STAT_RETRYLATER;
  1214. }
  1215. palen = msgh.msg_namelen;
  1216. Notice1("receiving packet from %s"/*"src"*/,
  1217. sockaddr_info((struct sockaddr *)pa, palen, peername, sizeof(peername))/*,
  1218. sockaddr_info(&la->soa, sockname, sizeof(sockname))*/);
  1219. xiodopacketinfo(&msgh, true, true);
  1220. if (xiocheckpeer(xfd, pa, la) < 0) {
  1221. /* drop packet */
  1222. char buff[512];
  1223. Recv(xfd->fd, buff, sizeof(buff), 0);
  1224. continue;
  1225. }
  1226. Info1("permitting packet from %s",
  1227. sockaddr_info((struct sockaddr *)pa, palen,
  1228. infobuff, sizeof(infobuff)));
  1229. /* set the env vars describing the local and remote sockets */
  1230. /*xiosetsockaddrenv("SOCK", la, lalen, proto);*/
  1231. xiosetsockaddrenv("PEER", pa, palen, proto);
  1232. applyopts(xfd->fd, opts, PH_FD);
  1233. applyopts(xfd->fd, opts, PH_CONNECTED);
  1234. xfd->peersa = *(union sockaddr_union *)pa;
  1235. xfd->salen = palen;
  1236. if (dofork) {
  1237. sigset_t mask_sigchldusr1;
  1238. /* we must prevent that the current packet triggers another fork;
  1239. therefore we wait for a signal from the recent child: USR1
  1240. indicates that is has consumed the last packet; CHLD means it has
  1241. terminated */
  1242. /* block SIGCHLD and SIGUSR1 until parent is ready to react */
  1243. sigemptyset(&mask_sigchldusr1);
  1244. sigaddset(&mask_sigchldusr1, SIGCHLD);
  1245. sigaddset(&mask_sigchldusr1, SIGUSR1);
  1246. Sigprocmask(SIG_BLOCK, &mask_sigchldusr1, NULL);
  1247. if ((pid = xio_fork(false, level)) < 0) {
  1248. Close(xfd->fd);
  1249. Sigprocmask(SIG_UNBLOCK, &mask_sigchldusr1, NULL);
  1250. return STAT_RETRYLATER;
  1251. }
  1252. if (pid == 0) { /* child */
  1253. /* no reason to block SIGCHLD in child process */
  1254. Sigprocmask(SIG_UNBLOCK, &mask_sigchldusr1, NULL);
  1255. xfd->ppid = Getppid(); /* send parent a signal when packet has
  1256. been consumed */
  1257. #if WITH_RETRY
  1258. /* !? */
  1259. xfd->retry = 0;
  1260. xfd->forever = 0;
  1261. level = E_ERROR;
  1262. #endif /* WITH_RETRY */
  1263. #if WITH_UNIX
  1264. /* with UNIX sockets: only listening parent is allowed to remove
  1265. the socket file */
  1266. xfd->opt_unlink_close = false;
  1267. #endif /* WITH_UNIX */
  1268. break;
  1269. }
  1270. /* server: continue loop with listen */
  1271. xio_waitingfor = pid;
  1272. /* now we are ready to handle signals */
  1273. Sigprocmask(SIG_UNBLOCK, &mask_sigchldusr1, NULL);
  1274. while (!xio_hashappened) {
  1275. Sleep(UINT_MAX); /* any signal lets us continue */
  1276. }
  1277. xio_waitingfor = 0; /* so this child will not set hashappened again */
  1278. xio_hashappened = false;
  1279. Info("continue listening");
  1280. } else {
  1281. break;
  1282. }
  1283. }
  1284. if ((result = _xio_openlate(xfd, opts)) != 0)
  1285. return STAT_NORETRY;
  1286. return STAT_OK;
  1287. }
  1288. /* returns STAT_* */
  1289. int _xioopen_dgram_recv(struct single *xfd, int xioflags,
  1290. struct sockaddr *us, socklen_t uslen,
  1291. struct opt *opts, int pf, int socktype, int proto,
  1292. int level) {
  1293. char *rangename;
  1294. char infobuff[256];
  1295. if (applyopts_single(xfd, opts, PH_INIT) < 0) return STAT_NORETRY;
  1296. if ((xfd->fd = xiosocket(opts, pf, socktype, proto, level)) < 0) {
  1297. return STAT_RETRYLATER;
  1298. }
  1299. applyopts_single(xfd, opts, PH_PASTSOCKET);
  1300. applyopts(xfd->fd, opts, PH_PASTSOCKET);
  1301. applyopts_cloexec(xfd->fd, opts);
  1302. applyopts(xfd->fd, opts, PH_PREBIND);
  1303. applyopts(xfd->fd, opts, PH_BIND);
  1304. if ((us != NULL) && Bind(xfd->fd, (struct sockaddr *)us, uslen) < 0) {
  1305. Msg4(level, "bind(%d, {%s}, "F_socklen"): %s", xfd->fd,
  1306. sockaddr_info(us, uslen, infobuff, sizeof(infobuff)), uslen,
  1307. strerror(errno));
  1308. Close(xfd->fd);
  1309. return STAT_RETRYLATER;
  1310. }
  1311. #if WITH_UNIX
  1312. if (pf == AF_UNIX && us != NULL) {
  1313. applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_FD);
  1314. }
  1315. #endif
  1316. applyopts_single(xfd, opts, PH_PASTBIND);
  1317. applyopts(xfd->fd, opts, PH_PASTBIND);
  1318. #if WITH_UNIX
  1319. if (pf == AF_UNIX && us != NULL) {
  1320. /*applyopts_early(((struct sockaddr_un *)us)->sun_path, opts);*/
  1321. applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_EARLY);
  1322. applyopts_named(((struct sockaddr_un *)us)->sun_path, opts, PH_PREOPEN);
  1323. }
  1324. #endif /* WITH_UNIX */
  1325. #if WITH_IP4 /*|| WITH_IP6*/
  1326. if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
  1327. if (xioparserange(rangename, pf, &xfd->para.socket.range)
  1328. < 0) {
  1329. free(rangename);
  1330. return STAT_NORETRY;
  1331. }
  1332. free(rangename);
  1333. xfd->para.socket.dorange = true;
  1334. }
  1335. #endif
  1336. #if (WITH_TCP || WITH_UDP) && WITH_LIBWRAP
  1337. xio_retropt_tcpwrap(xfd, opts);
  1338. #endif /* && (WITH_TCP || WITH_UDP) && WITH_LIBWRAP */
  1339. if (xioopts.logopt == 'm') {
  1340. Info("starting recvfrom loop, switching to syslog");
  1341. diag_set('y', xioopts.syslogfac); xioopts.logopt = 'y';
  1342. } else {
  1343. Info("starting recvfrom loop");
  1344. }
  1345. return STAT_OK;
  1346. }
  1347. int retropt_socket_pf(struct opt *opts, int *pf) {
  1348. char *pfname;
  1349. if (retropt_string(opts, OPT_PROTOCOL_FAMILY, &pfname) >= 0) {
  1350. if (isdigit(pfname[0])) {
  1351. *pf = strtoul(pfname, NULL /*!*/, 0);
  1352. #if WITH_IP4
  1353. } else if (!strcasecmp("inet", pfname) ||
  1354. !strcasecmp("inet4", pfname) ||
  1355. !strcasecmp("ip4", pfname) ||
  1356. !strcasecmp("ipv4", pfname) ||
  1357. !strcasecmp("2", pfname)) {
  1358. *pf = PF_INET;
  1359. #endif /* WITH_IP4 */
  1360. #if WITH_IP6
  1361. } else if (!strcasecmp("inet6", pfname) ||
  1362. !strcasecmp("ip6", pfname) ||
  1363. !strcasecmp("ipv6", pfname) ||
  1364. !strcasecmp("10", pfname)) {
  1365. *pf = PF_INET6;
  1366. #endif /* WITH_IP6 */
  1367. } else {
  1368. Error1("unknown protocol family \"%s\"", pfname);
  1369. /*! Warn("falling back to INET");*/
  1370. }
  1371. free(pfname);
  1372. return 0;
  1373. }
  1374. return -1;
  1375. }
  1376. /* this function calls recvmsg(..., MSG_PEEK, ...) to obtain information about
  1377. the arriving packet. in msgh the msg_name pointer must refer to an (empty)
  1378. sockaddr storage. */
  1379. int xiogetpacketsrc(int fd, struct msghdr *msgh) {
  1380. char peekbuff[1];
  1381. #if HAVE_STRUCT_IOVEC
  1382. struct iovec iovec;
  1383. #endif
  1384. #if HAVE_STRUCT_IOVEC
  1385. iovec.iov_base = peekbuff;
  1386. iovec.iov_len = sizeof(peekbuff);
  1387. msgh->msg_iov = &iovec;
  1388. msgh->msg_iovlen = 1;
  1389. #endif
  1390. #if HAVE_STRUCT_MSGHDR_MSGFLAGS
  1391. msgh->msg_flags = 0;
  1392. #endif
  1393. if (Recvmsg(fd, msgh, MSG_PEEK
  1394. #ifdef MSG_TRUNC
  1395. |MSG_TRUNC
  1396. #endif
  1397. ) < 0) {
  1398. Warn1("recvmsg(): %s", strerror(errno));
  1399. return STAT_RETRYLATER;
  1400. }
  1401. return STAT_OK;
  1402. }
  1403. /* works through the ancillary messages found in the given socket header record
  1404. and logs the relevant information (E_DEBUG, E_INFO).
  1405. calls protocol/layer specific functions for handling the messages
  1406. creates appropriate environment vars if withenv is set */
  1407. int xiodopacketinfo(struct msghdr *msgh, bool withlog, bool withenv) {
  1408. #if defined(HAVE_STRUCT_CMSGHDR) && defined(CMSG_DATA)
  1409. struct cmsghdr *cmsg;
  1410. /* parse ancillary messages */
  1411. cmsg = CMSG_FIRSTHDR(msgh);
  1412. while (cmsg != NULL) {
  1413. int num = 0; /* number of data components of a ancill.msg */
  1414. int i;
  1415. char typbuff[16], *typp;
  1416. char nambuff[128], *namp;
  1417. char valbuff[256], *valp;
  1418. char envbuff[256], *envp;
  1419. if (withlog) {
  1420. xiodump(CMSG_DATA(cmsg),
  1421. cmsg->cmsg_len-((char *)CMSG_DATA(cmsg)-(char *)cmsg),
  1422. valbuff, sizeof(valbuff)-1, 0);
  1423. Debug4("ancillary message: len="F_cmsg_len", level=%d, type=%d, data=%s",
  1424. cmsg->cmsg_len, cmsg->cmsg_level, cmsg->cmsg_type,
  1425. valbuff);
  1426. }
  1427. /* try to get the anc.msg. contents in handy components, protocol/level
  1428. dependent */
  1429. switch (cmsg->cmsg_level) {
  1430. case SOL_SOCKET:
  1431. xiolog_ancillary_socket(cmsg, &num, typbuff, sizeof(typbuff)-1,
  1432. nambuff, sizeof(nambuff)-1,
  1433. envbuff, sizeof(envbuff)-1,
  1434. valbuff, sizeof(valbuff)-1);
  1435. break;
  1436. #if WITH_IP4 || WITH_IP6
  1437. case SOL_IP:
  1438. xiolog_ancillary_ip(cmsg, &num, typbuff, sizeof(typbuff)-1,
  1439. nambuff, sizeof(nambuff)-1,
  1440. envbuff, sizeof(envbuff)-1,
  1441. valbuff, sizeof(valbuff)-1);
  1442. break;
  1443. #endif /* WITH_IP4 || WITH_IP6 */
  1444. #if WITH_IP6
  1445. case SOL_IPV6:
  1446. xiolog_ancillary_ip6(cmsg, &num, typbuff, sizeof(typbuff)-1,
  1447. nambuff, sizeof(nambuff)-1,
  1448. envbuff, sizeof(envbuff)-1,
  1449. valbuff, sizeof(valbuff)-1);
  1450. break;
  1451. #endif /* WITH_IP6 */
  1452. default:
  1453. num = 1;
  1454. snprintf(typbuff, sizeof(typbuff)-1, "LEVEL%u", cmsg->cmsg_level);
  1455. snprintf(nambuff, sizeof(nambuff)-1, "type%u", cmsg->cmsg_type);
  1456. xiodump(CMSG_DATA(cmsg),
  1457. cmsg->cmsg_len-((char *)CMSG_DATA(cmsg)-(char *)cmsg),
  1458. valbuff, sizeof(valbuff)-1, 0);
  1459. }
  1460. /* here the info is in typbuff (one string), nambuff (num consecutive
  1461. strings), and valbuff (num consecutive strings) */
  1462. i = 0;
  1463. typp = typbuff; namp = nambuff; envp = envbuff; valp = valbuff;
  1464. while (i < num) {
  1465. if (withlog) {
  1466. Info3("ancillary message: %s: %s=%s", typp, namp, valp);
  1467. }
  1468. if (withenv) {
  1469. if (*envp) {
  1470. xiosetenv(envp, valp, 1, NULL);
  1471. } else if (!strcasecmp(typp+strlen(typp)-strlen(namp), namp)) {
  1472. xiosetenv(typp, valp, 1, NULL);
  1473. } else {
  1474. xiosetenv2(typp, namp, valp, 1, NULL);
  1475. }
  1476. }
  1477. if (++i == num) break;
  1478. namp = strchr(namp, '\0')+1;
  1479. envp = strchr(envp, '\0')+1;
  1480. valp = strchr(valp, '\0')+1;
  1481. }
  1482. cmsg = CMSG_NXTHDR(msgh, cmsg);
  1483. }
  1484. return 0;
  1485. #else /* !(defined(HAVE_STRUCT_CMSGHDR) && defined(CMSG_DATA)) */
  1486. return -1;
  1487. #endif /* !(defined(HAVE_STRUCT_CMSGHDR) && defined(CMSG_DATA)) */
  1488. }
  1489. /* check if peer address is within permitted range.
  1490. return >= 0 if so. */
  1491. int xiocheckrange(union sockaddr_union *sa, struct xiorange *range) {
  1492. switch (sa->soa.sa_family) {
  1493. #if WITH_IP4
  1494. case PF_INET:
  1495. return
  1496. xiocheckrange_ip4(&sa->ip4, range);
  1497. #endif /* WITH_IP4 */
  1498. #if WITH_IP6
  1499. case PF_INET6:
  1500. return
  1501. xiocheckrange_ip6(&sa->ip6, range);
  1502. #endif /* WITH_IP6 */
  1503. #if 0
  1504. case PF_UNSPEC:
  1505. {
  1506. socklen_t i;
  1507. for (i = 0; i < sizeof(sa->soa.sa_data); ++i) {
  1508. if ((range->netmask.soa.sa_data[i] & sa->soa.sa_data[i]) != range->netaddr.soa.sa_data[i]) {
  1509. return -1;
  1510. }
  1511. }
  1512. return 0;
  1513. }
  1514. #endif
  1515. }
  1516. return -1;
  1517. }
  1518. int xiocheckpeer(xiosingle_t *xfd,
  1519. union sockaddr_union *pa, union sockaddr_union *la) {
  1520. char infobuff[256];
  1521. int result;
  1522. #if WITH_IP4
  1523. if (xfd->para.socket.dorange) {
  1524. if (pa == NULL) { return -1; }
  1525. if (xiocheckrange(pa, &xfd->para.socket.range) < 0) {
  1526. char infobuff[256];
  1527. Warn1("refusing connection from %s due to range option",
  1528. sockaddr_info((struct sockaddr *)pa, 0,
  1529. infobuff, sizeof(infobuff)));
  1530. return -1;
  1531. }
  1532. Info1("permitting connection from %s due to range option",
  1533. sockaddr_info((struct sockaddr *)pa, 0,
  1534. infobuff, sizeof(infobuff)));
  1535. }
  1536. #endif /* WITH_IP4 */
  1537. #if WITH_TCP || WITH_UDP
  1538. if (xfd->para.socket.ip.dosourceport) {
  1539. if (pa == NULL) { return -1; }
  1540. #if WITH_IP4
  1541. if (pa->soa.sa_family == AF_INET &&
  1542. ntohs(((struct sockaddr_in *)pa)->sin_port) != xfd->para.socket.ip.sourceport) {
  1543. Warn1("refusing connection from %s due to wrong sourceport",
  1544. sockaddr_info((struct sockaddr *)pa, 0,
  1545. infobuff, sizeof(infobuff)));
  1546. return -1;
  1547. }
  1548. #endif /* WITH_IP4 */
  1549. #if WITH_IP6
  1550. if (pa->soa.sa_family == AF_INET6 &&
  1551. ntohs(((struct sockaddr_in6 *)pa)->sin6_port) != xfd->para.socket.ip.sourceport) {
  1552. Warn1("refusing connection from %s due to wrong sourceport",
  1553. sockaddr_info((struct sockaddr *)pa, 0,
  1554. infobuff, sizeof(infobuff)));
  1555. return -1;
  1556. }
  1557. #endif /* WITH_IP6 */
  1558. Info1("permitting connection from %s due to sourceport option",
  1559. sockaddr_info((struct sockaddr *)pa, 0,
  1560. infobuff, sizeof(infobuff)));
  1561. } else if (xfd->para.socket.ip.lowport) {
  1562. if (pa == NULL) { return -1; }
  1563. if (pa->soa.sa_family == AF_INET &&
  1564. ntohs(((struct sockaddr_in *)pa)->sin_port) >= IPPORT_RESERVED) {
  1565. Warn1("refusing connection from %s due to lowport option",
  1566. sockaddr_info((struct sockaddr *)pa, 0,
  1567. infobuff, sizeof(infobuff)));
  1568. return -1;
  1569. }
  1570. #if WITH_IP6
  1571. else if (pa->soa.sa_family == AF_INET6 &&
  1572. ntohs(((struct sockaddr_in6 *)pa)->sin6_port) >=
  1573. IPPORT_RESERVED) {
  1574. Warn1("refusing connection from %s due to lowport option",
  1575. sockaddr_info((struct sockaddr *)pa, 0,
  1576. infobuff, sizeof(infobuff)));
  1577. return -1;
  1578. }
  1579. #endif /* WITH_IP6 */
  1580. Info1("permitting connection from %s due to lowport option",
  1581. sockaddr_info((struct sockaddr *)pa, 0,
  1582. infobuff, sizeof(infobuff)));
  1583. }
  1584. #endif /* WITH_TCP || WITH_UDP */
  1585. #if (WITH_TCP || WITH_UDP) && WITH_LIBWRAP
  1586. result = xio_tcpwrap_check(xfd, la, pa);
  1587. if (result < 0) {
  1588. char infobuff[256];
  1589. Warn1("refusing connection from %s due to tcpwrapper option",
  1590. sockaddr_info((struct sockaddr *)pa, 0,
  1591. infobuff, sizeof(infobuff)));
  1592. return -1;
  1593. } else if (result > 0) {
  1594. Info1("permitting connection from %s due to tcpwrapper option",
  1595. sockaddr_info((struct sockaddr *)pa, 0,
  1596. infobuff, sizeof(infobuff)));
  1597. }
  1598. #endif /* (WITH_TCP || WITH_UDP) && WITH_LIBWRAP */
  1599. return 0; /* permitted */
  1600. }
  1601. #if HAVE_STRUCT_CMSGHDR
  1602. /* converts the ancillary message in *cmsg into a form useable for further
  1603. processing. knows the specifics of common message types.
  1604. returns the number of resulting syntax elements in *num
  1605. returns a sequence of \0 terminated type strings in *typbuff
  1606. returns a sequence of \0 terminated name strings in *nambuff
  1607. returns a sequence of \0 terminated value strings in *valbuff
  1608. the respective len parameters specify the available space in the buffers
  1609. returns STAT_OK or other STAT_*
  1610. */
  1611. static int
  1612. xiolog_ancillary_socket(struct cmsghdr *cmsg, int *num,
  1613. char *typbuff, int typlen,
  1614. char *nambuff, int namlen,
  1615. char *envbuff, int envlen,
  1616. char *valbuff, int vallen) {
  1617. const char *cmsgtype, *cmsgname, *cmsgenvn;
  1618. size_t msglen;
  1619. struct timeval *tv;
  1620. int rc = STAT_OK;
  1621. #if defined(CMSG_DATA)
  1622. msglen = cmsg->cmsg_len-((char *)CMSG_DATA(cmsg)-(char *)cmsg);
  1623. switch (cmsg->cmsg_type) {
  1624. #ifdef SO_PASSCRED
  1625. case SO_PASSCRED: /* this is really a UNIX/LOCAL message */
  1626. /*! needs implementation */
  1627. #endif /* SO_PASSCRED */
  1628. #ifdef SO_RIGHTS
  1629. case SO_RIGHTS: /* this is really a UNIX/LOCAL message */
  1630. /*! needs implementation */
  1631. #endif
  1632. default: /* binary data */
  1633. snprintf(typbuff, typlen, "SOCKET.%u", cmsg->cmsg_type);
  1634. nambuff[0] = '\0'; strncat(nambuff, "data", namlen-1);
  1635. xiodump(CMSG_DATA(cmsg), msglen, valbuff, vallen, 0);
  1636. return STAT_OK;
  1637. #ifdef SO_TIMESTAMP
  1638. # ifdef SCM_TIMESTAMP
  1639. case SCM_TIMESTAMP:
  1640. # else
  1641. case SO_TIMESTAMP:
  1642. # endif
  1643. tv = (struct timeval *)CMSG_DATA(cmsg);
  1644. cmsgtype =
  1645. #ifdef SCM_TIMESTAMP
  1646. "SCM_TIMESTAMP" /* FreeBSD */
  1647. #else
  1648. "SO_TIMESTAMP" /* Linux */
  1649. #endif
  1650. ;
  1651. cmsgname = "timestamp";
  1652. cmsgenvn = "TIMESTAMP";
  1653. { time_t t = tv->tv_sec; ctime_r(&t, valbuff); }
  1654. snprintf(strchr(valbuff, '\0')-1/*del \n*/, vallen-strlen(valbuff)+1, ", %06ld usecs", (long)tv->tv_usec);
  1655. break;
  1656. #endif /* defined(SO_TIMESTAMP) */
  1657. ;
  1658. }
  1659. /* when we come here we provide a single parameter
  1660. with type in cmsgtype, name in cmsgname,
  1661. and value already in valbuff */
  1662. *num = 1;
  1663. if (strlen(cmsgtype) >= typlen) rc = STAT_WARNING;
  1664. typbuff[0] = '\0'; strncat(typbuff, cmsgtype, typlen-1);
  1665. if (strlen(cmsgname) >= namlen) rc = STAT_WARNING;
  1666. nambuff[0] = '\0'; strncat(nambuff, cmsgname, namlen-1);
  1667. if (strlen(cmsgenvn) >= envlen) rc = STAT_WARNING;
  1668. envbuff[0] = '\0'; strncat(envbuff, cmsgenvn, envlen-1);
  1669. return rc;
  1670. #else /* !defined(CMSG_DATA) */
  1671. return STAT_NORETRY;
  1672. #endif /* !defined(CMSG_DATA) */
  1673. }
  1674. #endif /* HAVE_STRUCT_CMSGHDR */
  1675. /* return the name of the interface with given index
  1676. or NULL if is fails
  1677. The system call requires an arbitrary socket; the calling program may
  1678. provide one in parameter ins to avoid creation of a dummy socket. ins must
  1679. be <0 if it does not specify a socket fd. */
  1680. char *xiogetifname(int ind, char *val, int ins) {
  1681. #if !HAVE_PROTOTYPE_LIB_if_indextoname
  1682. int s;
  1683. struct ifreq ifr;
  1684. if (ins >= 0) {
  1685. s = ins;
  1686. } else {
  1687. if ((s = Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
  1688. Error1("socket(PF_INET, SOCK_DGRAM, IPPROTO_IP): %s", strerror(errno));
  1689. return NULL;
  1690. }
  1691. }
  1692. #if HAVE_STRUCT_IFREQ_IFR_INDEX
  1693. ifr.ifr_index = ind;
  1694. #elif HAVE_STRUCT_IFREQ_IFR_IFINDEX
  1695. ifr.ifr_ifindex = ind;
  1696. #endif
  1697. #ifdef SIOCGIFNAME
  1698. if(Ioctl(s, SIOCGIFNAME, &ifr) < 0) {
  1699. #if HAVE_STRUCT_IFREQ_IFR_INDEX
  1700. Info3("ioctl(%d, SIOCGIFNAME, {..., ifr_index=%d, ...}: %s",
  1701. s, ifr.ifr_index, strerror(errno));
  1702. #elif HAVE_STRUCT_IFREQ_IFR_IFINDEX
  1703. Info3("ioctl(%d, SIOCGIFNAME, {..., ifr_ifindex=%d, ...}: %s",
  1704. s, ifr.ifr_ifindex, strerror(errno));
  1705. #endif
  1706. if (ins < 0) Close(s);
  1707. return NULL;
  1708. }
  1709. #endif /* SIOCGIFNAME */
  1710. if (ins < 0) Close(s);
  1711. strcpy(val, ifr.ifr_name);
  1712. return val;
  1713. #else /* HAVE_PROTOTYPE_LIB_if_indextoname */
  1714. return if_indextoname(ind, val);
  1715. #endif /* HAVE_PROTOTYPE_LIB_if_indextoname */
  1716. }
  1717. /* parses a network specification consisting of an address and a mask. */
  1718. int xioparsenetwork(const char *rangename, int pf, struct xiorange *range) {
  1719. size_t addrlen = 0, masklen = 0;
  1720. int result;
  1721. switch (pf) {
  1722. #if WITH_IP4
  1723. case PF_INET:
  1724. return xioparsenetwork_ip4(rangename, range);
  1725. break;
  1726. #endif /* WITH_IP4 */
  1727. #if WITH_IP6
  1728. case PF_INET6:
  1729. return xioparsenetwork_ip6(rangename, range);
  1730. break;
  1731. #endif /* WITH_IP6 */
  1732. case PF_UNSPEC:
  1733. {
  1734. char *addrname;
  1735. const char *maskname;
  1736. if ((maskname = strchr(rangename, ':')) == NULL) {
  1737. Error1("syntax error in range \"%s\": use <addr>:<mask>", rangename);
  1738. return STAT_NORETRY;
  1739. }
  1740. ++maskname; /* skip ':' */
  1741. if ((addrname = Malloc(maskname-rangename)) == NULL) {
  1742. return STAT_NORETRY;
  1743. }
  1744. strncpy(addrname, rangename, maskname-rangename-1); /* ok */
  1745. addrname[maskname-rangename-1] = '\0';
  1746. result =
  1747. dalan(addrname, (char *)&range->netaddr.soa.sa_data, &addrlen,
  1748. sizeof(range->netaddr)-(size_t)(&((struct sockaddr *)0)->sa_data)
  1749. /* data length */);
  1750. if (result < 0) {
  1751. Error1("data too long: \"%s\"", addrname);
  1752. free(addrname); return STAT_NORETRY;
  1753. } else if (result > 0) {
  1754. Error1("syntax error in \"%s\"", addrname);
  1755. free(addrname); return STAT_NORETRY;
  1756. }
  1757. free(addrname);
  1758. result =
  1759. dalan(maskname, (char *)&range->netmask.soa.sa_data, &masklen,
  1760. sizeof(range->netaddr)-(size_t)(&((struct sockaddr *)0)->sa_data)
  1761. /* data length */);
  1762. if (result < 0) {
  1763. Error1("data too long: \"%s\"", maskname);
  1764. return STAT_NORETRY;
  1765. } else if (result > 0) {
  1766. Error1("syntax error in \"%s\"", maskname);
  1767. return STAT_NORETRY;
  1768. }
  1769. if (addrlen != masklen) {
  1770. Error2("network address is "F_Zu" bytes long, mask is "F_Zu" bytes long",
  1771. addrlen, masklen);
  1772. /* recover by padding the shorter component with 0 */
  1773. memset((char *)&range->netaddr.soa.sa_data+addrlen, 0,
  1774. MAX(0, addrlen-masklen));
  1775. memset((char *)&range->netmask.soa.sa_data+masklen, 0,
  1776. MAX(0, masklen-addrlen));
  1777. }
  1778. }
  1779. break;
  1780. default:
  1781. Error1("range option not supported with address family %d", pf);
  1782. return STAT_NORETRY;
  1783. }
  1784. return STAT_OK;
  1785. }
  1786. /* parses a string of form address/bits or address:mask, and fills the fields
  1787. of the range union. The addr component is masked with mask. */
  1788. int xioparserange(const char *rangename, int pf, struct xiorange *range) {
  1789. int i;
  1790. if (xioparsenetwork(rangename, pf, range) < 0) {
  1791. return -1;
  1792. }
  1793. /* we have parsed the address and mask; now we make sure that the stored
  1794. address has 0 where mask is 0, to simplify comparisions */
  1795. switch (pf) {
  1796. #if WITH_IP4
  1797. case PF_INET:
  1798. range->netaddr.ip4.sin_addr.s_addr &= range->netmask.ip4.sin_addr.s_addr;
  1799. break;
  1800. #endif /* WITH_IP4 */
  1801. #if WITH_IP6
  1802. case PF_INET6:
  1803. return xiorange_ip6andmask(range);
  1804. break;
  1805. #endif /* WITH_IP6 */
  1806. case PF_UNSPEC:
  1807. for (i = 0; i < sizeof(range->netaddr); ++i) {
  1808. ((char *)&range->netaddr)[i] &= ((char *)&range->netmask)[i];
  1809. }
  1810. break;
  1811. default:
  1812. Error1("range option not supported with address family %d", pf);
  1813. return STAT_NORETRY;
  1814. }
  1815. return 0;
  1816. }
  1817. /* set environment variables describing (part of) a socket address, e.g.
  1818. SOCAT_SOCKADDR. lr (local/remote) specifies a string like "SOCK" or "PEER".
  1819. proto should correspond to the third parameter of socket(2) and is used to
  1820. determine the presence of port information. */
  1821. int xiosetsockaddrenv(const char *lr,
  1822. union sockaddr_union *sau, socklen_t salen,
  1823. int proto) {
  1824. # define XIOSOCKADDRENVLEN 256
  1825. char namebuff[XIOSOCKADDRENVLEN];
  1826. char valuebuff[XIOSOCKADDRENVLEN];
  1827. int idx = 0, result;
  1828. strcpy(namebuff, lr);
  1829. switch (sau->soa.sa_family) {
  1830. #if WITH_UNIX
  1831. case PF_UNIX:
  1832. result =
  1833. xiosetsockaddrenv_unix(idx, strchr(namebuff, '\0'), XIOSOCKADDRENVLEN-strlen(lr),
  1834. valuebuff, XIOSOCKADDRENVLEN,
  1835. &sau->un, salen, proto);
  1836. xiosetenv(namebuff, valuebuff, 1, NULL);
  1837. break;
  1838. #endif /* WITH_UNIX */
  1839. #if WITH_IP4
  1840. case PF_INET:
  1841. do {
  1842. result =
  1843. xiosetsockaddrenv_ip4(idx, strchr(namebuff, '\0'), XIOSOCKADDRENVLEN-strlen(lr),
  1844. valuebuff, XIOSOCKADDRENVLEN,
  1845. &sau->ip4, proto);
  1846. xiosetenv(namebuff, valuebuff, 1, NULL);
  1847. namebuff[strlen(lr)] = '\0'; ++idx;
  1848. } while (result > 0);
  1849. break;
  1850. #endif /* WITH_IP4 */
  1851. #if WITH_IP6
  1852. case PF_INET6:
  1853. strcpy(namebuff, lr);
  1854. do {
  1855. result =
  1856. xiosetsockaddrenv_ip6(idx, strchr(namebuff, '\0'), XIOSOCKADDRENVLEN-strlen(lr),
  1857. valuebuff, XIOSOCKADDRENVLEN,
  1858. &sau->ip6, proto);
  1859. xiosetenv(namebuff, valuebuff, 1, NULL);
  1860. namebuff[strlen(lr)] = '\0'; ++idx;
  1861. } while (result > 0);
  1862. break;
  1863. #endif /* WITH_IP6 */
  1864. #if LATER
  1865. case PF_PACKET:
  1866. result = xiosetsockaddrenv_packet(lr, (void *)sau, proto); break;
  1867. #endif
  1868. default:
  1869. result = -1;
  1870. break;
  1871. }
  1872. return result;
  1873. # undef XIOSOCKADDRENVLEN
  1874. }
  1875. #endif /* _WITH_SOCKET */
  1876. /* these do sockets internally */
  1877. /* retrieves options so-type and so-prototype from opts, calls socket, and
  1878. ev. generates an appropriate error message.
  1879. returns 0 on success or -1 if an error occurred. */
  1880. int
  1881. xiosocket(struct opt *opts, int pf, int socktype, int proto, int msglevel) {
  1882. int result;
  1883. retropt_int(opts, OPT_SO_TYPE, &socktype);
  1884. retropt_int(opts, OPT_SO_PROTOTYPE, &proto);
  1885. result = Socket(pf, socktype, proto);
  1886. if (result < 0) {
  1887. Msg4(msglevel, "socket(%d, %d, %d): %s",
  1888. pf, socktype, proto, strerror(errno));
  1889. return -1;
  1890. }
  1891. return result;
  1892. }
  1893. /* retrieves options so-type and so-prototype from opts, calls socketpair, and
  1894. ev. generates an appropriate error message.
  1895. returns 0 on success or -1 if an error occurred. */
  1896. int
  1897. xiosocketpair(struct opt *opts, int pf, int socktype, int proto, int sv[2]) {
  1898. int result;
  1899. retropt_int(opts, OPT_SO_TYPE, &socktype);
  1900. retropt_int(opts, OPT_SO_PROTOTYPE, &proto);
  1901. result = Socketpair(pf, socktype, proto, sv);
  1902. if (result < 0) {
  1903. Error5("socketpair(%d, %d, %d, %p): %s",
  1904. pf, socktype, proto, sv, strerror(errno));
  1905. return -1;
  1906. }
  1907. return result;
  1908. }