A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

404 lines
11 KiB

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