b_sock.c 11 KB

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