2
0

b_sock.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include "bio_lcl.h"
  13. #ifndef OPENSSL_NO_SOCK
  14. # define SOCKET_PROTOCOL IPPROTO_TCP
  15. # ifdef SO_MAXCONN
  16. # define MAX_LISTEN SO_MAXCONN
  17. # elif defined(SOMAXCONN)
  18. # define MAX_LISTEN SOMAXCONN
  19. # else
  20. # define MAX_LISTEN 32
  21. # endif
  22. # if defined(OPENSSL_SYS_WINDOWS)
  23. static int wsa_init_done = 0;
  24. # endif
  25. # if OPENSSL_API_COMPAT < 0x10100000L
  26. int BIO_get_host_ip(const char *str, unsigned char *ip)
  27. {
  28. BIO_ADDRINFO *res = NULL;
  29. int ret = 0;
  30. if (BIO_sock_init() != 1)
  31. return 0; /* don't generate another error code here */
  32. if (BIO_lookup(str, NULL, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
  33. size_t l;
  34. if (BIO_ADDRINFO_family(res) != AF_INET) {
  35. BIOerr(BIO_F_BIO_GET_HOST_IP,
  36. BIO_R_GETHOSTBYNAME_ADDR_IS_NOT_AF_INET);
  37. } else if (BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), NULL, &l)) {
  38. /*
  39. * Because only AF_INET addresses will reach this far, we can assert
  40. * that l should be 4
  41. */
  42. if (ossl_assert(l == 4))
  43. ret = BIO_ADDR_rawaddress(BIO_ADDRINFO_address(res), ip, &l);
  44. }
  45. BIO_ADDRINFO_free(res);
  46. } else {
  47. ERR_add_error_data(2, "host=", str);
  48. }
  49. return ret;
  50. }
  51. int BIO_get_port(const char *str, unsigned short *port_ptr)
  52. {
  53. BIO_ADDRINFO *res = NULL;
  54. int ret = 0;
  55. if (str == NULL) {
  56. BIOerr(BIO_F_BIO_GET_PORT, BIO_R_NO_PORT_DEFINED);
  57. return 0;
  58. }
  59. if (BIO_sock_init() != 1)
  60. return 0; /* don't generate another error code here */
  61. if (BIO_lookup(NULL, str, BIO_LOOKUP_CLIENT, AF_INET, SOCK_STREAM, &res)) {
  62. if (BIO_ADDRINFO_family(res) != AF_INET) {
  63. BIOerr(BIO_F_BIO_GET_PORT,
  64. BIO_R_ADDRINFO_ADDR_IS_NOT_AF_INET);
  65. } else {
  66. *port_ptr = ntohs(BIO_ADDR_rawport(BIO_ADDRINFO_address(res)));
  67. ret = 1;
  68. }
  69. BIO_ADDRINFO_free(res);
  70. } else {
  71. ERR_add_error_data(2, "host=", str);
  72. }
  73. return ret;
  74. }
  75. # endif
  76. int BIO_sock_error(int sock)
  77. {
  78. int j = 0, i;
  79. socklen_t size = sizeof(j);
  80. /*
  81. * Note: under Windows the third parameter is of type (char *) whereas
  82. * under other systems it is (void *) if you don't have a cast it will
  83. * choke the compiler: if you do have a cast then you can either go for
  84. * (char *) or (void *).
  85. */
  86. i = getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&j, &size);
  87. if (i < 0)
  88. return get_last_socket_error();
  89. else
  90. return j;
  91. }
  92. # if OPENSSL_API_COMPAT < 0x10100000L
  93. struct hostent *BIO_gethostbyname(const char *name)
  94. {
  95. /*
  96. * Caching gethostbyname() results forever is wrong, so we have to let
  97. * the true gethostbyname() worry about this
  98. */
  99. return gethostbyname(name);
  100. }
  101. # endif
  102. int BIO_sock_init(void)
  103. {
  104. # ifdef OPENSSL_SYS_WINDOWS
  105. static struct WSAData wsa_state;
  106. if (!wsa_init_done) {
  107. int err;
  108. wsa_init_done = 1;
  109. memset(&wsa_state, 0, sizeof(wsa_state));
  110. /*
  111. * Not making wsa_state available to the rest of the code is formally
  112. * wrong. But the structures we use are [believed to be] invariable
  113. * among Winsock DLLs, while API availability is [expected to be]
  114. * probed at run-time with DSO_global_lookup.
  115. */
  116. if (WSAStartup(0x0202, &wsa_state) != 0) {
  117. err = WSAGetLastError();
  118. SYSerr(SYS_F_WSASTARTUP, err);
  119. BIOerr(BIO_F_BIO_SOCK_INIT, BIO_R_WSASTARTUP);
  120. return -1;
  121. }
  122. }
  123. # endif /* OPENSSL_SYS_WINDOWS */
  124. # ifdef WATT32
  125. extern int _watt_do_exit;
  126. _watt_do_exit = 0; /* don't make sock_init() call exit() */
  127. if (sock_init())
  128. return -1;
  129. # endif
  130. return 1;
  131. }
  132. void bio_sock_cleanup_int(void)
  133. {
  134. # ifdef OPENSSL_SYS_WINDOWS
  135. if (wsa_init_done) {
  136. wsa_init_done = 0;
  137. WSACleanup();
  138. }
  139. # endif
  140. }
  141. int BIO_socket_ioctl(int fd, long type, void *arg)
  142. {
  143. int i;
  144. # ifdef __DJGPP__
  145. i = ioctlsocket(fd, type, (char *)arg);
  146. # else
  147. # if defined(OPENSSL_SYS_VMS)
  148. /*-
  149. * 2011-02-18 SMS.
  150. * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
  151. * observe that all the consumers pass in an "unsigned long *",
  152. * so we arrange a local copy with a short pointer, and use
  153. * that, instead.
  154. */
  155. # if __INITIAL_POINTER_SIZE == 64
  156. # define ARG arg_32p
  157. # pragma pointer_size save
  158. # pragma pointer_size 32
  159. unsigned long arg_32;
  160. unsigned long *arg_32p;
  161. # pragma pointer_size restore
  162. arg_32p = &arg_32;
  163. arg_32 = *((unsigned long *)arg);
  164. # else /* __INITIAL_POINTER_SIZE == 64 */
  165. # define ARG arg
  166. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  167. # else /* defined(OPENSSL_SYS_VMS) */
  168. # define ARG arg
  169. # endif /* defined(OPENSSL_SYS_VMS) [else] */
  170. i = ioctlsocket(fd, type, ARG);
  171. # endif /* __DJGPP__ */
  172. if (i < 0)
  173. SYSerr(SYS_F_IOCTLSOCKET, get_last_socket_error());
  174. return i;
  175. }
  176. # if OPENSSL_API_COMPAT < 0x10100000L
  177. int BIO_get_accept_socket(char *host, int bind_mode)
  178. {
  179. int s = INVALID_SOCKET;
  180. char *h = NULL, *p = NULL;
  181. BIO_ADDRINFO *res = NULL;
  182. if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
  183. return INVALID_SOCKET;
  184. if (BIO_sock_init() != 1)
  185. return INVALID_SOCKET;
  186. if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
  187. goto err;
  188. if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
  189. BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
  190. s = INVALID_SOCKET;
  191. goto err;
  192. }
  193. if (!BIO_listen(s, BIO_ADDRINFO_address(res),
  194. bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
  195. BIO_closesocket(s);
  196. s = INVALID_SOCKET;
  197. }
  198. err:
  199. BIO_ADDRINFO_free(res);
  200. OPENSSL_free(h);
  201. OPENSSL_free(p);
  202. return s;
  203. }
  204. int BIO_accept(int sock, char **ip_port)
  205. {
  206. BIO_ADDR res;
  207. int ret = -1;
  208. ret = BIO_accept_ex(sock, &res, 0);
  209. if (ret == (int)INVALID_SOCKET) {
  210. if (BIO_sock_should_retry(ret)) {
  211. ret = -2;
  212. goto end;
  213. }
  214. SYSerr(SYS_F_ACCEPT, get_last_socket_error());
  215. BIOerr(BIO_F_BIO_ACCEPT, BIO_R_ACCEPT_ERROR);
  216. goto end;
  217. }
  218. if (ip_port != NULL) {
  219. char *host = BIO_ADDR_hostname_string(&res, 1);
  220. char *port = BIO_ADDR_service_string(&res, 1);
  221. if (host != NULL && port != NULL)
  222. *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
  223. else
  224. *ip_port = NULL;
  225. if (*ip_port == NULL) {
  226. BIOerr(BIO_F_BIO_ACCEPT, ERR_R_MALLOC_FAILURE);
  227. BIO_closesocket(ret);
  228. ret = (int)INVALID_SOCKET;
  229. } else {
  230. strcpy(*ip_port, host);
  231. strcat(*ip_port, ":");
  232. strcat(*ip_port, port);
  233. }
  234. OPENSSL_free(host);
  235. OPENSSL_free(port);
  236. }
  237. end:
  238. return ret;
  239. }
  240. # endif
  241. int BIO_set_tcp_ndelay(int s, int on)
  242. {
  243. int ret = 0;
  244. # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
  245. int opt;
  246. # ifdef SOL_TCP
  247. opt = SOL_TCP;
  248. # else
  249. # ifdef IPPROTO_TCP
  250. opt = IPPROTO_TCP;
  251. # endif
  252. # endif
  253. ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
  254. # endif
  255. return (ret == 0);
  256. }
  257. int BIO_socket_nbio(int s, int mode)
  258. {
  259. int ret = -1;
  260. int l;
  261. l = mode;
  262. # ifdef FIONBIO
  263. l = mode;
  264. ret = BIO_socket_ioctl(s, FIONBIO, &l);
  265. # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
  266. /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
  267. l = fcntl(s, F_GETFL, 0);
  268. if (l == -1) {
  269. SYSerr(SYS_F_FCNTL, get_last_sys_error());
  270. ret = -1;
  271. } else {
  272. # if defined(O_NONBLOCK)
  273. l &= ~O_NONBLOCK;
  274. # else
  275. l &= ~FNDELAY; /* BSD4.x */
  276. # endif
  277. if (mode) {
  278. # if defined(O_NONBLOCK)
  279. l |= O_NONBLOCK;
  280. # else
  281. l |= FNDELAY; /* BSD4.x */
  282. # endif
  283. }
  284. ret = fcntl(s, F_SETFL, l);
  285. if (ret < 0) {
  286. SYSerr(SYS_F_FCNTL, get_last_sys_error());
  287. }
  288. }
  289. # else
  290. /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
  291. BIOerr(BIO_F_BIO_SOCKET_NBIO, ERR_R_PASSED_INVALID_ARGUMENT);
  292. # endif
  293. return (ret == 0);
  294. }
  295. int BIO_sock_info(int sock,
  296. enum BIO_sock_info_type type, union BIO_sock_info_u *info)
  297. {
  298. switch (type) {
  299. case BIO_SOCK_INFO_ADDRESS:
  300. {
  301. socklen_t addr_len;
  302. int ret = 0;
  303. addr_len = sizeof(*info->addr);
  304. ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
  305. &addr_len);
  306. if (ret == -1) {
  307. SYSerr(SYS_F_GETSOCKNAME, get_last_socket_error());
  308. BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_ERROR);
  309. return 0;
  310. }
  311. if ((size_t)addr_len > sizeof(*info->addr)) {
  312. BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
  313. return 0;
  314. }
  315. }
  316. break;
  317. default:
  318. BIOerr(BIO_F_BIO_SOCK_INFO, BIO_R_UNKNOWN_INFO_TYPE);
  319. return 0;
  320. }
  321. return 1;
  322. }
  323. #endif