connecthostport.c 6.8 KB

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