connecthostport.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* $Id: connecthostport.c,v 1.17 2017/04/21 09:58:30 nanard Exp $ */
  2. /* Project : miniupnp
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2010-2017 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution. */
  7. /* use getaddrinfo() or gethostbyname()
  8. * uncomment the following line in order to use gethostbyname() */
  9. #ifdef NO_GETADDRINFO
  10. #define USE_GETHOSTBYNAME
  11. #endif
  12. #include <string.h>
  13. #include <stdio.h>
  14. #ifdef _WIN32
  15. #include <winsock2.h>
  16. #include <ws2tcpip.h>
  17. #include <io.h>
  18. #define MAXHOSTNAMELEN 64
  19. #define snprintf _snprintf
  20. #define herror
  21. #define socklen_t int
  22. #else /* #ifdef _WIN32 */
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
  26. #include <sys/time.h>
  27. #endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
  28. #include <sys/param.h>
  29. #include <sys/select.h>
  30. #include <errno.h>
  31. #define closesocket close
  32. #include <netdb.h>
  33. #include <netinet/in.h>
  34. /* defining MINIUPNPC_IGNORE_EINTR enable the ignore of interruptions
  35. * during the connect() call */
  36. #define MINIUPNPC_IGNORE_EINTR
  37. #include <sys/socket.h>
  38. #include <sys/select.h>
  39. #endif /* #else _WIN32 */
  40. /* definition of PRINT_SOCKET_ERROR */
  41. #ifdef _WIN32
  42. #define PRINT_SOCKET_ERROR(x) fprintf(stderr, "Socket error: %s, %d\n", x, WSAGetLastError());
  43. #else
  44. #define PRINT_SOCKET_ERROR(x) perror(x)
  45. #endif
  46. #if defined(__amigaos__) || defined(__amigaos4__)
  47. #define herror(A) printf("%s\n", A)
  48. #endif
  49. #include "connecthostport.h"
  50. #ifndef MAXHOSTNAMELEN
  51. #define MAXHOSTNAMELEN 64
  52. #endif
  53. /* connecthostport()
  54. * return a socket connected (TCP) to the host and port
  55. * or -1 in case of error */
  56. int connecthostport(const char * host, unsigned short port,
  57. unsigned int scope_id)
  58. {
  59. int s, n;
  60. #ifdef USE_GETHOSTBYNAME
  61. struct sockaddr_in dest;
  62. struct hostent *hp;
  63. #else /* #ifdef USE_GETHOSTBYNAME */
  64. char tmp_host[MAXHOSTNAMELEN+1];
  65. char port_str[8];
  66. struct addrinfo *ai, *p;
  67. struct addrinfo hints;
  68. #endif /* #ifdef USE_GETHOSTBYNAME */
  69. #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
  70. struct timeval timeout;
  71. #endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
  72. #ifdef USE_GETHOSTBYNAME
  73. hp = gethostbyname(host);
  74. if(hp == NULL)
  75. {
  76. herror(host);
  77. return -1;
  78. }
  79. memcpy(&dest.sin_addr, hp->h_addr, sizeof(dest.sin_addr));
  80. memset(dest.sin_zero, 0, sizeof(dest.sin_zero));
  81. s = socket(PF_INET, SOCK_STREAM, 0);
  82. if(s < 0)
  83. {
  84. PRINT_SOCKET_ERROR("socket");
  85. return -1;
  86. }
  87. #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
  88. /* setting a 3 seconds timeout for the connect() call */
  89. timeout.tv_sec = 3;
  90. timeout.tv_usec = 0;
  91. if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
  92. {
  93. PRINT_SOCKET_ERROR("setsockopt SO_RCVTIMEO");
  94. }
  95. timeout.tv_sec = 3;
  96. timeout.tv_usec = 0;
  97. if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
  98. {
  99. PRINT_SOCKET_ERROR("setsockopt SO_SNDTIMEO");
  100. }
  101. #endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
  102. dest.sin_family = AF_INET;
  103. dest.sin_port = htons(port);
  104. n = connect(s, (struct sockaddr *)&dest, sizeof(struct sockaddr_in));
  105. #ifdef MINIUPNPC_IGNORE_EINTR
  106. /* EINTR The system call was interrupted by a signal that was caught
  107. * EINPROGRESS The socket is nonblocking and the connection cannot
  108. * be completed immediately. */
  109. while(n < 0 && (errno == EINTR || errno == EINPROGRESS))
  110. {
  111. socklen_t len;
  112. fd_set wset;
  113. int err;
  114. FD_ZERO(&wset);
  115. FD_SET(s, &wset);
  116. if((n = select(s + 1, NULL, &wset, NULL, NULL)) == -1 && errno == EINTR)
  117. continue;
  118. /*len = 0;*/
  119. /*n = getpeername(s, NULL, &len);*/
  120. len = sizeof(err);
  121. if(getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
  122. PRINT_SOCKET_ERROR("getsockopt");
  123. closesocket(s);
  124. return -1;
  125. }
  126. if(err != 0) {
  127. errno = err;
  128. n = -1;
  129. }
  130. }
  131. #endif /* #ifdef MINIUPNPC_IGNORE_EINTR */
  132. if(n<0)
  133. {
  134. PRINT_SOCKET_ERROR("connect");
  135. closesocket(s);
  136. return -1;
  137. }
  138. #else /* #ifdef USE_GETHOSTBYNAME */
  139. /* use getaddrinfo() instead of gethostbyname() */
  140. memset(&hints, 0, sizeof(hints));
  141. /* hints.ai_flags = AI_ADDRCONFIG; */
  142. #ifdef AI_NUMERICSERV
  143. hints.ai_flags = AI_NUMERICSERV;
  144. #endif
  145. hints.ai_socktype = SOCK_STREAM;
  146. hints.ai_family = AF_UNSPEC; /* AF_INET, AF_INET6 or AF_UNSPEC */
  147. /* hints.ai_protocol = IPPROTO_TCP; */
  148. snprintf(port_str, sizeof(port_str), "%hu", port);
  149. if(host[0] == '[')
  150. {
  151. /* literal ip v6 address */
  152. int i, j;
  153. for(i = 0, j = 1; host[j] && (host[j] != ']') && i < MAXHOSTNAMELEN; i++, j++)
  154. {
  155. tmp_host[i] = host[j];
  156. if(0 == memcmp(host+j, "%25", 3)) /* %25 is just url encoding for '%' */
  157. j+=2; /* skip "25" */
  158. }
  159. tmp_host[i] = '\0';
  160. }
  161. else
  162. {
  163. strncpy(tmp_host, host, MAXHOSTNAMELEN);
  164. }
  165. tmp_host[MAXHOSTNAMELEN] = '\0';
  166. n = getaddrinfo(tmp_host, port_str, &hints, &ai);
  167. if(n != 0)
  168. {
  169. #ifdef _WIN32
  170. fprintf(stderr, "getaddrinfo() error : %d\n", n);
  171. #else
  172. fprintf(stderr, "getaddrinfo() error : %s\n", gai_strerror(n));
  173. #endif
  174. return -1;
  175. }
  176. s = -1;
  177. for(p = ai; p; p = p->ai_next)
  178. {
  179. s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
  180. if(s < 0)
  181. continue;
  182. if(p->ai_addr->sa_family == AF_INET6 && scope_id > 0) {
  183. struct sockaddr_in6 * addr6 = (struct sockaddr_in6 *)p->ai_addr;
  184. addr6->sin6_scope_id = scope_id;
  185. }
  186. #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
  187. /* setting a 3 seconds timeout for the connect() call */
  188. timeout.tv_sec = 3;
  189. timeout.tv_usec = 0;
  190. if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
  191. {
  192. PRINT_SOCKET_ERROR("setsockopt");
  193. }
  194. timeout.tv_sec = 3;
  195. timeout.tv_usec = 0;
  196. if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
  197. {
  198. PRINT_SOCKET_ERROR("setsockopt");
  199. }
  200. #endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
  201. n = connect(s, p->ai_addr, p->ai_addrlen);
  202. #ifdef MINIUPNPC_IGNORE_EINTR
  203. /* EINTR The system call was interrupted by a signal that was caught
  204. * EINPROGRESS The socket is nonblocking and the connection cannot
  205. * be completed immediately. */
  206. while(n < 0 && (errno == EINTR || errno == EINPROGRESS))
  207. {
  208. socklen_t len;
  209. fd_set wset;
  210. int err;
  211. FD_ZERO(&wset);
  212. FD_SET(s, &wset);
  213. if((n = select(s + 1, NULL, &wset, NULL, NULL)) == -1 && errno == EINTR)
  214. continue;
  215. /*len = 0;*/
  216. /*n = getpeername(s, NULL, &len);*/
  217. len = sizeof(err);
  218. if(getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
  219. PRINT_SOCKET_ERROR("getsockopt");
  220. closesocket(s);
  221. freeaddrinfo(ai);
  222. return -1;
  223. }
  224. if(err != 0) {
  225. errno = err;
  226. n = -1;
  227. }
  228. }
  229. #endif /* #ifdef MINIUPNPC_IGNORE_EINTR */
  230. if(n < 0)
  231. {
  232. closesocket(s);
  233. continue;
  234. }
  235. else
  236. {
  237. break;
  238. }
  239. }
  240. freeaddrinfo(ai);
  241. if(s < 0)
  242. {
  243. PRINT_SOCKET_ERROR("socket");
  244. return -1;
  245. }
  246. if(n < 0)
  247. {
  248. PRINT_SOCKET_ERROR("connect");
  249. return -1;
  250. }
  251. #endif /* #ifdef USE_GETHOSTBYNAME */
  252. return s;
  253. }