1
0

bio_sock.c 12 KB

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