xconnect.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Connect to host at port using address resolution from getaddrinfo
  6. *
  7. */
  8. #include "libbb.h"
  9. /* Return network byte ordered port number for a service.
  10. * If "port" is a number use it as the port.
  11. * If "port" is a name it is looked up in /etc/services, if it isnt found return
  12. * default_port
  13. */
  14. unsigned short bb_lookup_port(const char *port, const char *protocol, unsigned short default_port)
  15. {
  16. unsigned short port_nr = htons(default_port);
  17. if (port) {
  18. char *endptr;
  19. int old_errno;
  20. long port_long;
  21. /* Since this is a lib function, we're not allowed to reset errno to 0.
  22. * Doing so could break an app that is deferring checking of errno. */
  23. old_errno = errno;
  24. errno = 0;
  25. port_long = strtol(port, &endptr, 10);
  26. if (errno != 0 || *endptr!='\0' || endptr==port || port_long < 0 || port_long > 65535) {
  27. struct servent *tserv = getservbyname(port, protocol);
  28. if (tserv) {
  29. port_nr = tserv->s_port;
  30. }
  31. } else {
  32. port_nr = htons(port_long);
  33. }
  34. errno = old_errno;
  35. }
  36. return port_nr;
  37. }
  38. void bb_lookup_host(struct sockaddr_in *s_in, const char *host)
  39. {
  40. struct hostent *he;
  41. memset(s_in, 0, sizeof(struct sockaddr_in));
  42. s_in->sin_family = AF_INET;
  43. he = xgethostbyname(host);
  44. memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
  45. }
  46. void xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
  47. {
  48. if (connect(s, s_addr, addrlen) < 0) {
  49. if (ENABLE_FEATURE_CLEAN_UP) close(s);
  50. if (s_addr->sa_family == AF_INET)
  51. bb_perror_msg_and_die("cannot connect to remote host (%s)",
  52. inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
  53. bb_perror_msg_and_die("cannot connect to remote host");
  54. }
  55. }
  56. int xconnect_tcp_v4(struct sockaddr_in *s_addr)
  57. {
  58. int s = xsocket(AF_INET, SOCK_STREAM, 0);
  59. xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
  60. return s;
  61. }
  62. static const int one = 1;
  63. int setsockopt_reuseaddr(int fd)
  64. {
  65. return setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
  66. }
  67. int setsockopt_broadcast(int fd)
  68. {
  69. return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
  70. }
  71. int dotted2sockaddr(const char *dotted, struct sockaddr* sp, int socklen)
  72. {
  73. union {
  74. struct in_addr a4;
  75. #if ENABLE_FEATURE_IPV6
  76. struct in6_addr a6;
  77. #endif
  78. } a;
  79. /* TODO maybe: port spec? like n.n.n.n:nn */
  80. #if ENABLE_FEATURE_IPV6
  81. if (socklen >= sizeof(struct sockaddr_in6)
  82. && inet_pton(AF_INET6, dotted, &a.a6) > 0
  83. ) {
  84. ((struct sockaddr_in6*)sp)->sin6_family = AF_INET6;
  85. ((struct sockaddr_in6*)sp)->sin6_addr = a.a6;
  86. /* ((struct sockaddr_in6*)sp)->sin6_port = */
  87. return 0; /* success */
  88. }
  89. #endif
  90. if (socklen >= sizeof(struct sockaddr_in)
  91. && inet_pton(AF_INET, dotted, &a.a4) > 0
  92. ) {
  93. ((struct sockaddr_in*)sp)->sin_family = AF_INET;
  94. ((struct sockaddr_in*)sp)->sin_addr = a.a4;
  95. /* ((struct sockaddr_in*)sp)->sin_port = */
  96. return 0; /* success */
  97. }
  98. return 1;
  99. }
  100. int xsocket_stream_ip4or6(sa_family_t *fp)
  101. {
  102. int fd;
  103. #if ENABLE_FEATURE_IPV6
  104. fd = socket(AF_INET6, SOCK_STREAM, 0);
  105. if (fp) *fp = AF_INET6;
  106. if (fd < 0)
  107. #endif
  108. {
  109. fd = xsocket(AF_INET, SOCK_STREAM, 0);
  110. if (fp) *fp = AF_INET;
  111. }
  112. return fd;
  113. }
  114. int create_and_bind_socket_ip4or6(const char *hostaddr, int port)
  115. {
  116. int fd;
  117. sockaddr_inet sa;
  118. memset(&sa, 0, sizeof(sa));
  119. if (hostaddr) {
  120. if (dotted2sockaddr(hostaddr, &sa.sa, sizeof(sa)))
  121. bb_error_msg_and_die("bad address '%s'", hostaddr);
  122. /* user specified bind addr dictates family */
  123. fd = xsocket(sa.sa.sa_family, SOCK_STREAM, 0);
  124. } else
  125. fd = xsocket_stream_ip4or6(&sa.sa.sa_family);
  126. setsockopt_reuseaddr(fd);
  127. /* if (port >= 0) { */
  128. #if ENABLE_FEATURE_IPV6
  129. if (sa.sa.sa_family == AF_INET6 /* && !sa.sin6.sin6_port */)
  130. sa.sin6.sin6_port = htons(port);
  131. #endif
  132. if (sa.sa.sa_family == AF_INET /* && !sa.sin.sin_port */)
  133. sa.sin.sin_port = htons(port);
  134. /* } */
  135. xbind(fd, &sa.sa, sizeof(sa));
  136. return fd;
  137. }