bio_sock.c 12 KB

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