PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/postfix-2.9.3/src/util/inet_listen.c

#
C | 175 lines | 87 code | 16 blank | 72 comment | 22 complexity | a92f73546aedf72507237cdc4d0f62e4 MD5 | raw file
Possible License(s): IPL-1.0, AGPL-3.0
  1. /*++
  2. /* NAME
  3. /* inet_listen 3
  4. /* SUMMARY
  5. /* start TCP listener
  6. /* SYNOPSIS
  7. /* #include <listen.h>
  8. /*
  9. /* int inet_windowsize;
  10. /*
  11. /* int inet_listen(addr, backlog, block_mode)
  12. /* const char *addr;
  13. /* int backlog;
  14. /* int block_mode;
  15. /*
  16. /* int inet_accept(fd)
  17. /* int fd;
  18. /* DESCRIPTION
  19. /* The \fBinet_listen\fR routine starts a TCP listener
  20. /* on the specified address, with the specified backlog, and returns
  21. /* the resulting file descriptor.
  22. /*
  23. /* inet_accept() accepts a connection and sanitizes error results.
  24. /*
  25. /* Specify an inet_windowsize value > 0 to override the TCP
  26. /* window size that the server advertises to the client.
  27. /*
  28. /* Arguments:
  29. /* .IP addr
  30. /* The communication endpoint to listen on. The syntax is "host:port".
  31. /* Host and port may be specified in symbolic form or numerically.
  32. /* A null host field means listen on all network interfaces.
  33. /* .IP backlog
  34. /* This argument is passed on to the \fIlisten(2)\fR routine.
  35. /* .IP block_mode
  36. /* Either NON_BLOCKING for a non-blocking socket, or BLOCKING for
  37. /* blocking mode.
  38. /* .IP fd
  39. /* File descriptor returned by inet_listen().
  40. /* DIAGNOSTICS
  41. /* Fatal errors: inet_listen() aborts upon any system call failure.
  42. /* inet_accept() leaves all error handling up to the caller.
  43. /* LICENSE
  44. /* .ad
  45. /* .fi
  46. /* The Secure Mailer license must be distributed with this software.
  47. /* AUTHOR(S)
  48. /* Wietse Venema
  49. /* IBM T.J. Watson Research
  50. /* P.O. Box 704
  51. /* Yorktown Heights, NY 10598, USA
  52. /*--*/
  53. /* System libraries. */
  54. #include <sys_defs.h>
  55. #include <sys/socket.h>
  56. #include <netinet/in.h>
  57. #include <arpa/inet.h>
  58. #include <netdb.h>
  59. #ifndef MAXHOSTNAMELEN
  60. #include <sys/param.h>
  61. #endif
  62. #include <errno.h>
  63. #include <string.h>
  64. #include <unistd.h>
  65. /* Utility library. */
  66. #include "mymalloc.h"
  67. #include "msg.h"
  68. #include "host_port.h"
  69. #include "iostuff.h"
  70. #include "listen.h"
  71. #include "sane_accept.h"
  72. #include "myaddrinfo.h"
  73. #include "sock_addr.h"
  74. #include "inet_proto.h"
  75. /* inet_listen - create TCP listener */
  76. int inet_listen(const char *addr, int backlog, int block_mode)
  77. {
  78. struct addrinfo *res;
  79. struct addrinfo *res0;
  80. int aierr;
  81. int sock;
  82. int on = 1;
  83. char *buf;
  84. char *host;
  85. char *port;
  86. const char *parse_err;
  87. MAI_HOSTADDR_STR hostaddr;
  88. MAI_SERVPORT_STR portnum;
  89. INET_PROTO_INFO *proto_info;
  90. /*
  91. * Translate address information to internal form.
  92. */
  93. buf = mystrdup(addr);
  94. if ((parse_err = host_port(buf, &host, "", &port, (char *) 0)) != 0)
  95. msg_fatal("%s: %s", addr, parse_err);
  96. if (*host == 0)
  97. host = 0;
  98. if ((aierr = hostname_to_sockaddr(host, port, SOCK_STREAM, &res0)) != 0)
  99. msg_fatal("%s: %s", addr, MAI_STRERROR(aierr));
  100. myfree(buf);
  101. /* No early returns or res0 leaks. */
  102. proto_info = inet_proto_info();
  103. for (res = res0; /* see below */ ; res = res->ai_next) {
  104. /*
  105. * No usable address found.
  106. */
  107. if (res == 0)
  108. msg_fatal("%s: host found but no usable address", addr);
  109. /*
  110. * Safety net.
  111. */
  112. if (strchr((char *) proto_info->sa_family_list, res->ai_family) != 0)
  113. break;
  114. msg_info("skipping address family %d for %s", res->ai_family, addr);
  115. }
  116. /*
  117. * Show what address we're trying.
  118. */
  119. if (msg_verbose) {
  120. SOCKADDR_TO_HOSTADDR(res->ai_addr, res->ai_addrlen,
  121. &hostaddr, &portnum, 0);
  122. msg_info("trying... [%s]:%s", hostaddr.buf, portnum.buf);
  123. }
  124. /*
  125. * Create a listener socket.
  126. */
  127. if ((sock = socket(res->ai_family, res->ai_socktype, 0)) < 0)
  128. msg_fatal("socket: %m");
  129. #ifdef HAS_IPV6
  130. # if defined(IPV6_V6ONLY) && !defined(BROKEN_AI_PASSIVE_NULL_HOST)
  131. if (res->ai_family == AF_INET6
  132. && setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY,
  133. (char *) &on, sizeof(on)) < 0)
  134. msg_fatal("setsockopt(IPV6_V6ONLY): %m");
  135. # endif
  136. #endif
  137. if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
  138. (char *) &on, sizeof(on)) < 0)
  139. msg_fatal("setsockopt(SO_REUSEADDR): %m");
  140. if (bind(sock, res->ai_addr, res->ai_addrlen) < 0) {
  141. SOCKADDR_TO_HOSTADDR(res->ai_addr, res->ai_addrlen,
  142. &hostaddr, &portnum, 0);
  143. msg_fatal("bind %s port %s: %m", hostaddr.buf, portnum.buf);
  144. }
  145. freeaddrinfo(res0);
  146. non_blocking(sock, block_mode);
  147. if (inet_windowsize > 0)
  148. set_inet_windowsize(sock, inet_windowsize);
  149. if (listen(sock, backlog) < 0)
  150. msg_fatal("listen: %m");
  151. return (sock);
  152. }
  153. /* inet_accept - accept connection */
  154. int inet_accept(int fd)
  155. {
  156. struct sockaddr_storage ss;
  157. SOCKADDR_SIZE ss_len = sizeof(ss);
  158. return (sane_accept(fd, (struct sockaddr *) & ss, &ss_len));
  159. }