bio_sock.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright 1995-2025 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. # elif defined _WIN32
  28. # include <winsock.h> /* for type fd_set */
  29. # else
  30. # include <unistd.h>
  31. # if defined __VMS
  32. # include <sys/socket.h>
  33. # elif defined _HPUX_SOURCE
  34. # include <sys/time.h>
  35. # else
  36. # include <sys/select.h>
  37. # endif
  38. # endif
  39. # include "internal/sockets.h" /* for openssl_fdset() */
  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. # ifdef BIO_HAVE_WSAMSG
  116. LPFN_WSARECVMSG bio_WSARecvMsg;
  117. LPFN_WSASENDMSG bio_WSASendMsg;
  118. # endif
  119. int BIO_sock_init(void)
  120. {
  121. # ifdef OPENSSL_SYS_WINDOWS
  122. static struct WSAData wsa_state;
  123. if (!wsa_init_done) {
  124. wsa_init_done = 1;
  125. memset(&wsa_state, 0, sizeof(wsa_state));
  126. /*
  127. * Not making wsa_state available to the rest of the code is formally
  128. * wrong. But the structures we use are [believed to be] invariable
  129. * among Winsock DLLs, while API availability is [expected to be]
  130. * probed at run-time with DSO_global_lookup.
  131. */
  132. if (WSAStartup(0x0202, &wsa_state) != 0) {
  133. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  134. "calling wsastartup()");
  135. ERR_raise(ERR_LIB_BIO, BIO_R_WSASTARTUP);
  136. return -1;
  137. }
  138. /*
  139. * On Windows, some socket functions are not exposed as a prototype.
  140. * Instead, their function pointers must be loaded via this elaborate
  141. * process...
  142. */
  143. # ifdef BIO_HAVE_WSAMSG
  144. {
  145. GUID id_WSARecvMsg = WSAID_WSARECVMSG;
  146. GUID id_WSASendMsg = WSAID_WSASENDMSG;
  147. DWORD len_out = 0;
  148. SOCKET s;
  149. s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  150. if (s != INVALID_SOCKET) {
  151. if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
  152. &id_WSARecvMsg, sizeof(id_WSARecvMsg),
  153. &bio_WSARecvMsg, sizeof(bio_WSARecvMsg),
  154. &len_out, NULL, NULL) != 0
  155. || len_out != sizeof(bio_WSARecvMsg))
  156. bio_WSARecvMsg = NULL;
  157. if (WSAIoctl(s, SIO_GET_EXTENSION_FUNCTION_POINTER,
  158. &id_WSASendMsg, sizeof(id_WSASendMsg),
  159. &bio_WSASendMsg, sizeof(bio_WSASendMsg),
  160. &len_out, NULL, NULL) != 0
  161. || len_out != sizeof(bio_WSASendMsg))
  162. bio_WSASendMsg = NULL;
  163. closesocket(s);
  164. }
  165. }
  166. # endif
  167. }
  168. # endif /* OPENSSL_SYS_WINDOWS */
  169. # ifdef WATT32
  170. extern int _watt_do_exit;
  171. _watt_do_exit = 0; /* don't make sock_init() call exit() */
  172. if (sock_init())
  173. return -1;
  174. # endif
  175. return 1;
  176. }
  177. void bio_sock_cleanup_int(void)
  178. {
  179. # ifdef OPENSSL_SYS_WINDOWS
  180. if (wsa_init_done) {
  181. wsa_init_done = 0;
  182. WSACleanup();
  183. }
  184. # endif
  185. }
  186. int BIO_socket_ioctl(int fd, long type, void *arg)
  187. {
  188. int i;
  189. # ifdef __DJGPP__
  190. i = ioctlsocket(fd, type, (char *)arg);
  191. # else
  192. # if defined(OPENSSL_SYS_VMS)
  193. /*-
  194. * 2011-02-18 SMS.
  195. * VMS ioctl() can't tolerate a 64-bit "void *arg", but we
  196. * observe that all the consumers pass in an "unsigned long *",
  197. * so we arrange a local copy with a short pointer, and use
  198. * that, instead.
  199. */
  200. # if __INITIAL_POINTER_SIZE == 64
  201. # define ARG arg_32p
  202. # pragma pointer_size save
  203. # pragma pointer_size 32
  204. unsigned long arg_32;
  205. unsigned long *arg_32p;
  206. # pragma pointer_size restore
  207. arg_32p = &arg_32;
  208. arg_32 = *((unsigned long *)arg);
  209. # else /* __INITIAL_POINTER_SIZE == 64 */
  210. # define ARG arg
  211. # endif /* __INITIAL_POINTER_SIZE == 64 [else] */
  212. # else /* defined(OPENSSL_SYS_VMS) */
  213. # define ARG arg
  214. # endif /* defined(OPENSSL_SYS_VMS) [else] */
  215. i = ioctlsocket(fd, type, ARG);
  216. # endif /* __DJGPP__ */
  217. if (i < 0)
  218. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  219. "calling ioctlsocket()");
  220. return i;
  221. }
  222. # ifndef OPENSSL_NO_DEPRECATED_1_1_0
  223. int BIO_get_accept_socket(char *host, int bind_mode)
  224. {
  225. int s = INVALID_SOCKET;
  226. char *h = NULL, *p = NULL;
  227. BIO_ADDRINFO *res = NULL;
  228. if (!BIO_parse_hostserv(host, &h, &p, BIO_PARSE_PRIO_SERV))
  229. return INVALID_SOCKET;
  230. if (BIO_sock_init() != 1)
  231. goto err;
  232. if (BIO_lookup(h, p, BIO_LOOKUP_SERVER, AF_UNSPEC, SOCK_STREAM, &res) != 0)
  233. goto err;
  234. if ((s = BIO_socket(BIO_ADDRINFO_family(res), BIO_ADDRINFO_socktype(res),
  235. BIO_ADDRINFO_protocol(res), 0)) == INVALID_SOCKET) {
  236. s = INVALID_SOCKET;
  237. goto err;
  238. }
  239. if (!BIO_listen(s, BIO_ADDRINFO_address(res),
  240. bind_mode ? BIO_SOCK_REUSEADDR : 0)) {
  241. BIO_closesocket(s);
  242. s = INVALID_SOCKET;
  243. }
  244. err:
  245. BIO_ADDRINFO_free(res);
  246. OPENSSL_free(h);
  247. OPENSSL_free(p);
  248. return s;
  249. }
  250. int BIO_accept(int sock, char **ip_port)
  251. {
  252. BIO_ADDR res;
  253. int ret = -1;
  254. ret = BIO_accept_ex(sock, &res, 0);
  255. if (ret == (int)INVALID_SOCKET) {
  256. if (BIO_sock_should_retry(ret)) {
  257. ret = -2;
  258. goto end;
  259. }
  260. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  261. "calling accept()");
  262. ERR_raise(ERR_LIB_BIO, BIO_R_ACCEPT_ERROR);
  263. goto end;
  264. }
  265. if (ip_port != NULL) {
  266. char *host = BIO_ADDR_hostname_string(&res, 1);
  267. char *port = BIO_ADDR_service_string(&res, 1);
  268. if (host != NULL && port != NULL) {
  269. *ip_port = OPENSSL_zalloc(strlen(host) + strlen(port) + 2);
  270. } else {
  271. *ip_port = NULL;
  272. ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB);
  273. }
  274. if (*ip_port == NULL) {
  275. BIO_closesocket(ret);
  276. ret = (int)INVALID_SOCKET;
  277. } else {
  278. strcpy(*ip_port, host);
  279. strcat(*ip_port, ":");
  280. strcat(*ip_port, port);
  281. }
  282. OPENSSL_free(host);
  283. OPENSSL_free(port);
  284. }
  285. end:
  286. return ret;
  287. }
  288. # endif
  289. int BIO_set_tcp_ndelay(int s, int on)
  290. {
  291. int ret = 0;
  292. # if defined(TCP_NODELAY) && (defined(IPPROTO_TCP) || defined(SOL_TCP))
  293. int opt;
  294. # ifdef SOL_TCP
  295. opt = SOL_TCP;
  296. # else
  297. # ifdef IPPROTO_TCP
  298. opt = IPPROTO_TCP;
  299. # endif
  300. # endif
  301. ret = setsockopt(s, opt, TCP_NODELAY, (char *)&on, sizeof(on));
  302. # endif
  303. return (ret == 0);
  304. }
  305. int BIO_socket_nbio(int s, int mode)
  306. {
  307. int ret = -1;
  308. int l;
  309. l = mode;
  310. # ifdef FIONBIO
  311. l = mode;
  312. ret = BIO_socket_ioctl(s, FIONBIO, &l);
  313. # elif defined(F_GETFL) && defined(F_SETFL) && (defined(O_NONBLOCK) || defined(FNDELAY))
  314. /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
  315. l = fcntl(s, F_GETFL, 0);
  316. if (l == -1) {
  317. ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
  318. "calling fcntl()");
  319. ret = -1;
  320. } else {
  321. # if defined(O_NONBLOCK)
  322. l &= ~O_NONBLOCK;
  323. # else
  324. l &= ~FNDELAY; /* BSD4.x */
  325. # endif
  326. if (mode) {
  327. # if defined(O_NONBLOCK)
  328. l |= O_NONBLOCK;
  329. # else
  330. l |= FNDELAY; /* BSD4.x */
  331. # endif
  332. }
  333. ret = fcntl(s, F_SETFL, l);
  334. if (ret < 0) {
  335. ERR_raise_data(ERR_LIB_SYS, get_last_sys_error(),
  336. "calling fcntl()");
  337. }
  338. }
  339. # else
  340. /* make sure this call always pushes an error level; BIO_socket_ioctl() does so, so we do too. */
  341. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_INVALID_ARGUMENT);
  342. # endif
  343. return (ret == 0);
  344. }
  345. int BIO_sock_info(int sock,
  346. enum BIO_sock_info_type type, union BIO_sock_info_u *info)
  347. {
  348. switch (type) {
  349. case BIO_SOCK_INFO_ADDRESS:
  350. {
  351. socklen_t addr_len;
  352. int ret = 0;
  353. addr_len = sizeof(*info->addr);
  354. ret = getsockname(sock, BIO_ADDR_sockaddr_noconst(info->addr),
  355. &addr_len);
  356. if (ret == -1) {
  357. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  358. "calling getsockname()");
  359. ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_ERROR);
  360. return 0;
  361. }
  362. if ((size_t)addr_len > sizeof(*info->addr)) {
  363. ERR_raise(ERR_LIB_BIO, BIO_R_GETSOCKNAME_TRUNCATED_ADDRESS);
  364. return 0;
  365. }
  366. }
  367. break;
  368. default:
  369. ERR_raise(ERR_LIB_BIO, BIO_R_UNKNOWN_INFO_TYPE);
  370. return 0;
  371. }
  372. return 1;
  373. }
  374. /*
  375. * Wait on fd at most until max_time; succeed immediately if max_time == 0.
  376. * If for_read == 0 then assume to wait for writing, else wait for reading.
  377. * Returns -1 on error, 0 on timeout, and 1 on success.
  378. */
  379. int BIO_socket_wait(int fd, int for_read, time_t max_time)
  380. {
  381. # if defined(OPENSSL_SYS_WINDOWS) || !defined(POLLIN)
  382. fd_set confds;
  383. struct timeval tv;
  384. time_t now;
  385. # ifdef _WIN32
  386. if ((SOCKET)fd == INVALID_SOCKET)
  387. # else
  388. if (fd < 0 || fd >= FD_SETSIZE)
  389. # endif
  390. return -1;
  391. if (max_time == 0)
  392. return 1;
  393. now = time(NULL);
  394. if (max_time < now)
  395. return 0;
  396. FD_ZERO(&confds);
  397. openssl_fdset(fd, &confds);
  398. tv.tv_usec = 0;
  399. tv.tv_sec = (long)(max_time - now); /* might overflow */
  400. return select(fd + 1, for_read ? &confds : NULL,
  401. for_read ? NULL : &confds, NULL, &tv);
  402. # else
  403. struct pollfd confds;
  404. time_t now;
  405. if (fd < 0)
  406. return -1;
  407. if (max_time == 0)
  408. return 1;
  409. now = time(NULL);
  410. if (max_time < now)
  411. return 0;
  412. confds.fd = fd;
  413. confds.events = for_read ? POLLIN : POLLOUT;
  414. return poll(&confds, 1, (int)(max_time - now) * 1000);
  415. # endif
  416. }
  417. #endif /* !defined(OPENSSL_NO_SOCK) */