s_socket.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 199-2015 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * licensing@OpenSSL.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. */
  105. /* socket-related functions used by s_client and s_server */
  106. #include <stdio.h>
  107. #include <stdlib.h>
  108. #include <string.h>
  109. #include <errno.h>
  110. #include <signal.h>
  111. /*
  112. * With IPv6, it looks like Digital has mixed up the proper order of
  113. * recursive header file inclusion, resulting in the compiler complaining
  114. * that u_int isn't defined, but only if _POSIX_C_SOURCE is defined, which is
  115. * needed to have fileno() declared correctly... So let's define u_int
  116. */
  117. #if defined(OPENSSL_SYS_VMS_DECC) && !defined(__U_INT)
  118. # define __U_INT
  119. typedef unsigned int u_int;
  120. #endif
  121. #define USE_SOCKETS
  122. #include "apps.h"
  123. #undef USE_SOCKETS
  124. #include "s_apps.h"
  125. #include <openssl/ssl.h>
  126. #ifdef FLAT_INC
  127. # include "e_os.h"
  128. #else
  129. # include "../e_os.h"
  130. #endif
  131. #ifndef OPENSSL_NO_SOCK
  132. # if defined(OPENSSL_SYS_NETWARE) && defined(NETWARE_BSDSOCK)
  133. # include "netdb.h"
  134. # endif
  135. # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
  136. static void ssl_sock_cleanup(void);
  137. # endif
  138. static int ssl_sock_init(void);
  139. static int init_client_ip(int *sock, const unsigned char ip[4], int port,
  140. int type);
  141. static int init_server(int *sock, int port, int type);
  142. static int init_server_long(int *sock, int port, char *ip, int type);
  143. static int do_accept(int acc_sock, int *sock, char **host);
  144. static int host_ip(const char *str, unsigned char ip[4]);
  145. # ifndef NO_SYS_UN_H
  146. static int init_server_unix(int *sock, const char *path);
  147. static int do_accept_unix(int acc_sock, int *sock);
  148. # endif
  149. # if defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
  150. static int wsa_init_done = 0;
  151. # endif
  152. # ifdef OPENSSL_SYS_WINDOWS
  153. static struct WSAData wsa_state;
  154. static int wsa_init_done = 0;
  155. # endif /* OPENSSL_SYS_WINDOWS */
  156. # ifdef OPENSSL_SYS_WINDOWS
  157. static void ssl_sock_cleanup(void)
  158. {
  159. if (wsa_init_done) {
  160. wsa_init_done = 0;
  161. # ifndef OPENSSL_SYS_WINCE
  162. WSACancelBlockingCall();
  163. # endif
  164. WSACleanup();
  165. }
  166. }
  167. # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
  168. static void sock_cleanup(void)
  169. {
  170. if (wsa_init_done) {
  171. wsa_init_done = 0;
  172. WSACleanup();
  173. }
  174. }
  175. # endif
  176. static int ssl_sock_init(void)
  177. {
  178. # ifdef WATT32
  179. extern int _watt_do_exit;
  180. _watt_do_exit = 0;
  181. if (sock_init())
  182. return (0);
  183. # elif defined(OPENSSL_SYS_WINDOWS)
  184. if (!wsa_init_done) {
  185. int err;
  186. # ifdef SIGINT
  187. signal(SIGINT, (void (*)(int))ssl_sock_cleanup);
  188. # endif
  189. wsa_init_done = 1;
  190. memset(&wsa_state, 0, sizeof(wsa_state));
  191. if (WSAStartup(0x0101, &wsa_state) != 0) {
  192. err = WSAGetLastError();
  193. BIO_printf(bio_err, "unable to start WINSOCK, error code=%d\n",
  194. err);
  195. return (0);
  196. }
  197. }
  198. # elif defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK)
  199. WORD wVerReq;
  200. WSADATA wsaData;
  201. int err;
  202. if (!wsa_init_done) {
  203. # ifdef SIGINT
  204. signal(SIGINT, (void (*)(int))sock_cleanup);
  205. # endif
  206. wsa_init_done = 1;
  207. wVerReq = MAKEWORD(2, 0);
  208. err = WSAStartup(wVerReq, &wsaData);
  209. if (err != 0) {
  210. BIO_printf(bio_err, "unable to start WINSOCK2, error code=%d\n",
  211. err);
  212. return (0);
  213. }
  214. }
  215. # endif
  216. return (1);
  217. }
  218. int init_client(int *sock, const char *host, int port, int type)
  219. {
  220. unsigned char ip[4];
  221. ip[0] = ip[1] = ip[2] = ip[3] = 0;
  222. if (!host_ip(host, &(ip[0])))
  223. return 0;
  224. return init_client_ip(sock, ip, port, type);
  225. }
  226. static int init_client_ip(int *sock, const unsigned char ip[4], int port,
  227. int type)
  228. {
  229. unsigned long addr;
  230. struct sockaddr_in them;
  231. int s, i;
  232. if (!ssl_sock_init())
  233. return (0);
  234. memset(&them, 0, sizeof(them));
  235. them.sin_family = AF_INET;
  236. them.sin_port = htons((unsigned short)port);
  237. addr = (unsigned long)
  238. ((unsigned long)ip[0] << 24L) |
  239. ((unsigned long)ip[1] << 16L) |
  240. ((unsigned long)ip[2] << 8L) | ((unsigned long)ip[3]);
  241. them.sin_addr.s_addr = htonl(addr);
  242. if (type == SOCK_STREAM)
  243. s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  244. else /* ( type == SOCK_DGRAM) */
  245. s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  246. if (s == (int)INVALID_SOCKET) {
  247. perror("socket");
  248. return (0);
  249. }
  250. # if defined(SO_KEEPALIVE)
  251. if (type == SOCK_STREAM) {
  252. i = 0;
  253. i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i));
  254. if (i < 0) {
  255. closesocket(s);
  256. perror("keepalive");
  257. return (0);
  258. }
  259. }
  260. # endif
  261. if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
  262. closesocket(s);
  263. perror("connect");
  264. return (0);
  265. }
  266. *sock = s;
  267. return (1);
  268. }
  269. # ifndef NO_SYS_UN_H
  270. int init_client_unix(int *sock, const char *server)
  271. {
  272. struct sockaddr_un them;
  273. int s;
  274. if (strlen(server) > (UNIX_PATH_MAX + 1))
  275. return (0);
  276. if (!ssl_sock_init())
  277. return (0);
  278. s = socket(AF_UNIX, SOCK_STREAM, 0);
  279. if (s == (int)INVALID_SOCKET) {
  280. perror("socket");
  281. return (0);
  282. }
  283. memset(&them, 0, sizeof(them));
  284. them.sun_family = AF_UNIX;
  285. strcpy(them.sun_path, server);
  286. if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
  287. closesocket(s);
  288. perror("connect");
  289. return (0);
  290. }
  291. *sock = s;
  292. return (1);
  293. }
  294. # endif
  295. int do_server(int port, int type, int *ret,
  296. int (*cb) (char *hostname, int s, int stype,
  297. unsigned char *context), unsigned char *context,
  298. int naccept)
  299. {
  300. int sock;
  301. char *name = NULL;
  302. int accept_socket = 0;
  303. int i;
  304. if (!init_server(&accept_socket, port, type))
  305. return (0);
  306. if (ret != NULL) {
  307. *ret = accept_socket;
  308. /* return(1); */
  309. }
  310. for (;;) {
  311. if (type == SOCK_STREAM) {
  312. # ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
  313. if (do_accept(accept_socket, &sock, NULL) == 0)
  314. # else
  315. if (do_accept(accept_socket, &sock, &name) == 0)
  316. # endif
  317. {
  318. SHUTDOWN(accept_socket);
  319. return (0);
  320. }
  321. } else
  322. sock = accept_socket;
  323. i = (*cb) (name, sock, type, context);
  324. OPENSSL_free(name);
  325. if (type == SOCK_STREAM)
  326. SHUTDOWN2(sock);
  327. if (naccept != -1)
  328. naccept--;
  329. if (i < 0 || naccept == 0) {
  330. SHUTDOWN2(accept_socket);
  331. return (i);
  332. }
  333. }
  334. }
  335. # ifndef NO_SYS_UN_H
  336. int do_server_unix(const char *path, int *ret,
  337. int (*cb) (char *hostname, int s, int stype,
  338. unsigned char *context), unsigned char *context,
  339. int naccept)
  340. {
  341. int sock;
  342. int accept_socket = 0;
  343. int i;
  344. if (!init_server_unix(&accept_socket, path))
  345. return (0);
  346. if (ret != NULL)
  347. *ret = accept_socket;
  348. for (;;) {
  349. if (do_accept_unix(accept_socket, &sock) == 0) {
  350. SHUTDOWN(accept_socket);
  351. i = 0;
  352. goto out;
  353. }
  354. i = (*cb) (NULL, sock, 0, context);
  355. SHUTDOWN2(sock);
  356. if (naccept != -1)
  357. naccept--;
  358. if (i < 0 || naccept == 0) {
  359. SHUTDOWN2(accept_socket);
  360. goto out;
  361. }
  362. }
  363. out:
  364. unlink(path);
  365. return (i);
  366. }
  367. # endif
  368. static int init_server_long(int *sock, int port, char *ip, int type)
  369. {
  370. int ret = 0;
  371. struct sockaddr_in server;
  372. int s = -1;
  373. if (!ssl_sock_init())
  374. return (0);
  375. memset(&server, 0, sizeof(server));
  376. server.sin_family = AF_INET;
  377. server.sin_port = htons((unsigned short)port);
  378. if (ip == NULL)
  379. server.sin_addr.s_addr = INADDR_ANY;
  380. else
  381. /* Added for T3E, address-of fails on bit field (beckman@acl.lanl.gov) */
  382. # ifndef BIT_FIELD_LIMITS
  383. memcpy(&server.sin_addr.s_addr, ip, 4);
  384. # else
  385. memcpy(&server.sin_addr, ip, 4);
  386. # endif
  387. if (type == SOCK_STREAM)
  388. s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  389. else /* type == SOCK_DGRAM */
  390. s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  391. if (s == (int)INVALID_SOCKET)
  392. goto err;
  393. # if defined SOL_SOCKET && defined SO_REUSEADDR
  394. {
  395. int j = 1;
  396. setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&j, sizeof j);
  397. }
  398. # endif
  399. if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
  400. # ifndef OPENSSL_SYS_WINDOWS
  401. perror("bind");
  402. # endif
  403. goto err;
  404. }
  405. /* Make it 128 for linux */
  406. if (type == SOCK_STREAM && listen(s, 128) == -1)
  407. goto err;
  408. *sock = s;
  409. ret = 1;
  410. err:
  411. if ((ret == 0) && (s != -1)) {
  412. SHUTDOWN(s);
  413. }
  414. return (ret);
  415. }
  416. static int init_server(int *sock, int port, int type)
  417. {
  418. return (init_server_long(sock, port, NULL, type));
  419. }
  420. # ifndef NO_SYS_UN_H
  421. static int init_server_unix(int *sock, const char *path)
  422. {
  423. int ret = 0;
  424. struct sockaddr_un server;
  425. int s = -1;
  426. if (strlen(path) > (UNIX_PATH_MAX + 1))
  427. return (0);
  428. if (!ssl_sock_init())
  429. return (0);
  430. s = socket(AF_UNIX, SOCK_STREAM, 0);
  431. if (s == (int)INVALID_SOCKET)
  432. goto err;
  433. memset(&server, 0, sizeof(server));
  434. server.sun_family = AF_UNIX;
  435. strcpy(server.sun_path, path);
  436. if (bind(s, (struct sockaddr *)&server, sizeof(server)) == -1) {
  437. # ifndef OPENSSL_SYS_WINDOWS
  438. perror("bind");
  439. # endif
  440. goto err;
  441. }
  442. /* Make it 128 for linux */
  443. if (listen(s, 128) == -1) {
  444. # ifndef OPENSSL_SYS_WINDOWS
  445. perror("listen");
  446. # endif
  447. unlink(path);
  448. goto err;
  449. }
  450. *sock = s;
  451. ret = 1;
  452. err:
  453. if ((ret == 0) && (s != -1)) {
  454. SHUTDOWN(s);
  455. }
  456. return (ret);
  457. }
  458. # endif
  459. static int do_accept(int acc_sock, int *sock, char **host)
  460. {
  461. int ret;
  462. struct hostent *h1, *h2;
  463. static struct sockaddr_in from;
  464. int len;
  465. /* struct linger ling; */
  466. if (!ssl_sock_init())
  467. return (0);
  468. # ifndef OPENSSL_SYS_WINDOWS
  469. redoit:
  470. # endif
  471. memset(&from, 0, sizeof(from));
  472. len = sizeof(from);
  473. /*
  474. * Note: under VMS with SOCKETSHR the fourth parameter is currently of
  475. * type (int *) whereas under other systems it is (void *) if you don't
  476. * have a cast it will choke the compiler: if you do have a cast then you
  477. * can either go for (int *) or (void *).
  478. */
  479. ret = accept(acc_sock, (struct sockaddr *)&from, (void *)&len);
  480. if (ret == (int)INVALID_SOCKET) {
  481. # if defined(OPENSSL_SYS_WINDOWS) || (defined(OPENSSL_SYS_NETWARE) && !defined(NETWARE_BSDSOCK))
  482. int i;
  483. i = WSAGetLastError();
  484. BIO_printf(bio_err, "accept error %d\n", i);
  485. # else
  486. if (errno == EINTR) {
  487. /*
  488. * check_timeout();
  489. */
  490. goto redoit;
  491. }
  492. BIO_printf(bio_err, "accept errno=%d, %s\n", errno, strerror(errno));
  493. # endif
  494. return (0);
  495. }
  496. if (host == NULL)
  497. goto end;
  498. # ifndef BIT_FIELD_LIMITS
  499. /* I should use WSAAsyncGetHostByName() under windows */
  500. h1 = gethostbyaddr((char *)&from.sin_addr.s_addr,
  501. sizeof(from.sin_addr.s_addr), AF_INET);
  502. # else
  503. h1 = gethostbyaddr((char *)&from.sin_addr,
  504. sizeof(struct in_addr), AF_INET);
  505. # endif
  506. if (h1 == NULL) {
  507. BIO_printf(bio_err, "bad gethostbyaddr\n");
  508. *host = NULL;
  509. /* return(0); */
  510. } else {
  511. *host = app_malloc(strlen(h1->h_name) + 1, "copy hostname");
  512. OPENSSL_strlcpy(*host, h1->h_name, strlen(h1->h_name) + 1);
  513. h2 = gethostbyname(*host);
  514. if (h2 == NULL) {
  515. BIO_printf(bio_err, "gethostbyname failure\n");
  516. closesocket(ret);
  517. return (0);
  518. }
  519. if (h2->h_addrtype != AF_INET) {
  520. BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
  521. closesocket(ret);
  522. return (0);
  523. }
  524. }
  525. end:
  526. *sock = ret;
  527. return (1);
  528. }
  529. # ifndef NO_SYS_UN_H
  530. static int do_accept_unix(int acc_sock, int *sock)
  531. {
  532. int ret;
  533. if (!ssl_sock_init())
  534. return (0);
  535. redoit:
  536. ret = accept(acc_sock, NULL, NULL);
  537. if (ret == (int)INVALID_SOCKET) {
  538. if (errno == EINTR) {
  539. /*
  540. * check_timeout();
  541. */
  542. goto redoit;
  543. }
  544. BIO_printf(bio_err, "accept errno=%d, %s\n", errno, strerror(errno));
  545. return (0);
  546. }
  547. *sock = ret;
  548. return (1);
  549. }
  550. # endif
  551. int extract_host_port(char *str, char **host_ptr, unsigned char *ip,
  552. unsigned short *port_ptr)
  553. {
  554. char *h, *p;
  555. h = str;
  556. p = strchr(str, ':');
  557. if (p == NULL) {
  558. BIO_printf(bio_err, "no port defined\n");
  559. return (0);
  560. }
  561. *(p++) = '\0';
  562. if ((ip != NULL) && !host_ip(str, ip))
  563. goto err;
  564. if (host_ptr != NULL)
  565. *host_ptr = h;
  566. if (!extract_port(p, port_ptr))
  567. goto err;
  568. return (1);
  569. err:
  570. return (0);
  571. }
  572. static int host_ip(const char *str, unsigned char ip[4])
  573. {
  574. unsigned int in[4];
  575. int i;
  576. if (sscanf(str, "%u.%u.%u.%u", &(in[0]), &(in[1]), &(in[2]), &(in[3])) ==
  577. 4) {
  578. for (i = 0; i < 4; i++)
  579. if (in[i] > 255) {
  580. BIO_printf(bio_err, "invalid IP address\n");
  581. goto err;
  582. }
  583. ip[0] = in[0];
  584. ip[1] = in[1];
  585. ip[2] = in[2];
  586. ip[3] = in[3];
  587. } else { /* do a gethostbyname */
  588. struct hostent *he;
  589. if (!ssl_sock_init())
  590. return (0);
  591. he = gethostbyname(str);
  592. if (he == NULL) {
  593. BIO_printf(bio_err, "gethostbyname failure\n");
  594. goto err;
  595. }
  596. if (he->h_addrtype != AF_INET) {
  597. BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
  598. return (0);
  599. }
  600. ip[0] = he->h_addr_list[0][0];
  601. ip[1] = he->h_addr_list[0][1];
  602. ip[2] = he->h_addr_list[0][2];
  603. ip[3] = he->h_addr_list[0][3];
  604. }
  605. return (1);
  606. err:
  607. return (0);
  608. }
  609. int extract_port(const char *str, unsigned short *port_ptr)
  610. {
  611. int i;
  612. struct servent *s;
  613. i = atoi(str);
  614. if (i != 0)
  615. *port_ptr = (unsigned short)i;
  616. else {
  617. s = getservbyname(str, "tcp");
  618. if (s == NULL) {
  619. BIO_printf(bio_err, "getservbyname failure for %s\n", str);
  620. return (0);
  621. }
  622. *port_ptr = ntohs((unsigned short)s->s_port);
  623. }
  624. return (1);
  625. }
  626. #endif