b_sock.c 10.0 KB

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