bss_dgram.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /* crypto/bio/bio_dgram.c */
  2. /*
  3. * DTLS implementation written by Nagendra Modadugu
  4. * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * openssl-core@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <errno.h>
  61. #define USE_SOCKETS
  62. #include "cryptlib.h"
  63. #include <openssl/bio.h>
  64. #ifndef OPENSSL_NO_DGRAM
  65. # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
  66. # include <sys/timeb.h>
  67. # endif
  68. # ifdef OPENSSL_SYS_LINUX
  69. # define IP_MTU 14 /* linux is lame */
  70. # endif
  71. # ifdef WATT32
  72. # define sock_write SockWrite /* Watt-32 uses same names */
  73. # define sock_read SockRead
  74. # define sock_puts SockPuts
  75. # endif
  76. static int dgram_write(BIO *h, const char *buf, int num);
  77. static int dgram_read(BIO *h, char *buf, int size);
  78. static int dgram_puts(BIO *h, const char *str);
  79. static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  80. static int dgram_new(BIO *h);
  81. static int dgram_free(BIO *data);
  82. static int dgram_clear(BIO *bio);
  83. static int BIO_dgram_should_retry(int s);
  84. static void get_current_time(struct timeval *t);
  85. static BIO_METHOD methods_dgramp = {
  86. BIO_TYPE_DGRAM,
  87. "datagram socket",
  88. dgram_write,
  89. dgram_read,
  90. dgram_puts,
  91. NULL, /* dgram_gets, */
  92. dgram_ctrl,
  93. dgram_new,
  94. dgram_free,
  95. NULL,
  96. };
  97. typedef struct bio_dgram_data_st {
  98. union {
  99. struct sockaddr sa;
  100. struct sockaddr_in sa_in;
  101. # if OPENSSL_USE_IPV6
  102. struct sockaddr_in6 sa_in6;
  103. # endif
  104. } peer;
  105. unsigned int connected;
  106. unsigned int _errno;
  107. unsigned int mtu;
  108. struct timeval next_timeout;
  109. struct timeval socket_timeout;
  110. } bio_dgram_data;
  111. BIO_METHOD *BIO_s_datagram(void)
  112. {
  113. return (&methods_dgramp);
  114. }
  115. BIO *BIO_new_dgram(int fd, int close_flag)
  116. {
  117. BIO *ret;
  118. ret = BIO_new(BIO_s_datagram());
  119. if (ret == NULL)
  120. return (NULL);
  121. BIO_set_fd(ret, fd, close_flag);
  122. return (ret);
  123. }
  124. static int dgram_new(BIO *bi)
  125. {
  126. bio_dgram_data *data = NULL;
  127. bi->init = 0;
  128. bi->num = 0;
  129. data = OPENSSL_malloc(sizeof(bio_dgram_data));
  130. if (data == NULL)
  131. return 0;
  132. memset(data, 0x00, sizeof(bio_dgram_data));
  133. bi->ptr = data;
  134. bi->flags = 0;
  135. return (1);
  136. }
  137. static int dgram_free(BIO *a)
  138. {
  139. bio_dgram_data *data;
  140. if (a == NULL)
  141. return (0);
  142. if (!dgram_clear(a))
  143. return 0;
  144. data = (bio_dgram_data *)a->ptr;
  145. if (data != NULL)
  146. OPENSSL_free(data);
  147. return (1);
  148. }
  149. static int dgram_clear(BIO *a)
  150. {
  151. if (a == NULL)
  152. return (0);
  153. if (a->shutdown) {
  154. if (a->init) {
  155. SHUTDOWN2(a->num);
  156. }
  157. a->init = 0;
  158. a->flags = 0;
  159. }
  160. return (1);
  161. }
  162. static void dgram_adjust_rcv_timeout(BIO *b)
  163. {
  164. # if defined(SO_RCVTIMEO)
  165. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  166. union {
  167. size_t s;
  168. int i;
  169. } sz = {
  170. 0
  171. };
  172. /* Is a timer active? */
  173. if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
  174. struct timeval timenow, timeleft;
  175. /* Read current socket timeout */
  176. # ifdef OPENSSL_SYS_WINDOWS
  177. int timeout;
  178. sz.i = sizeof(timeout);
  179. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  180. (void *)&timeout, &sz.i) < 0) {
  181. perror("getsockopt");
  182. } else {
  183. data->socket_timeout.tv_sec = timeout / 1000;
  184. data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
  185. }
  186. # else
  187. sz.i = sizeof(data->socket_timeout);
  188. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  189. &(data->socket_timeout), (void *)&sz) < 0) {
  190. perror("getsockopt");
  191. } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
  192. OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
  193. # endif
  194. /* Get current time */
  195. get_current_time(&timenow);
  196. /* Calculate time left until timer expires */
  197. memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
  198. timeleft.tv_sec -= timenow.tv_sec;
  199. timeleft.tv_usec -= timenow.tv_usec;
  200. if (timeleft.tv_usec < 0) {
  201. timeleft.tv_sec--;
  202. timeleft.tv_usec += 1000000;
  203. }
  204. if (timeleft.tv_sec < 0) {
  205. timeleft.tv_sec = 0;
  206. timeleft.tv_usec = 1;
  207. }
  208. /*
  209. * Adjust socket timeout if next handhake message timer will expire
  210. * earlier.
  211. */
  212. if ((data->socket_timeout.tv_sec == 0
  213. && data->socket_timeout.tv_usec == 0)
  214. || (data->socket_timeout.tv_sec > timeleft.tv_sec)
  215. || (data->socket_timeout.tv_sec == timeleft.tv_sec
  216. && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
  217. # ifdef OPENSSL_SYS_WINDOWS
  218. timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
  219. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  220. (void *)&timeout, sizeof(timeout)) < 0) {
  221. perror("setsockopt");
  222. }
  223. # else
  224. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
  225. sizeof(struct timeval)) < 0) {
  226. perror("setsockopt");
  227. }
  228. # endif
  229. }
  230. }
  231. # endif
  232. }
  233. static void dgram_reset_rcv_timeout(BIO *b)
  234. {
  235. # if defined(SO_RCVTIMEO)
  236. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  237. /* Is a timer active? */
  238. if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
  239. # ifdef OPENSSL_SYS_WINDOWS
  240. int timeout = data->socket_timeout.tv_sec * 1000 +
  241. data->socket_timeout.tv_usec / 1000;
  242. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  243. (void *)&timeout, sizeof(timeout)) < 0) {
  244. perror("setsockopt");
  245. }
  246. # else
  247. if (setsockopt
  248. (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
  249. sizeof(struct timeval)) < 0) {
  250. perror("setsockopt");
  251. }
  252. # endif
  253. }
  254. # endif
  255. }
  256. static int dgram_read(BIO *b, char *out, int outl)
  257. {
  258. int ret = 0;
  259. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  260. struct {
  261. /*
  262. * See commentary in b_sock.c. <appro>
  263. */
  264. union {
  265. size_t s;
  266. int i;
  267. } len;
  268. union {
  269. struct sockaddr sa;
  270. struct sockaddr_in sa_in;
  271. # if OPENSSL_USE_IPV6
  272. struct sockaddr_in6 sa_in6;
  273. # endif
  274. } peer;
  275. } sa;
  276. sa.len.s = 0;
  277. sa.len.i = sizeof(sa.peer);
  278. if (out != NULL) {
  279. clear_socket_error();
  280. memset(&sa.peer, 0x00, sizeof(sa.peer));
  281. dgram_adjust_rcv_timeout(b);
  282. ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, (void *)&sa.len);
  283. if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
  284. OPENSSL_assert(sa.len.s <= sizeof(sa.peer));
  285. sa.len.i = (int)sa.len.s;
  286. }
  287. if (!data->connected && ret >= 0)
  288. BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
  289. BIO_clear_retry_flags(b);
  290. if (ret < 0) {
  291. if (BIO_dgram_should_retry(ret)) {
  292. BIO_set_retry_read(b);
  293. data->_errno = get_last_socket_error();
  294. }
  295. }
  296. dgram_reset_rcv_timeout(b);
  297. }
  298. return (ret);
  299. }
  300. static int dgram_write(BIO *b, const char *in, int inl)
  301. {
  302. int ret;
  303. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  304. clear_socket_error();
  305. if (data->connected)
  306. ret = writesocket(b->num, in, inl);
  307. else {
  308. int peerlen = sizeof(data->peer);
  309. if (data->peer.sa.sa_family == AF_INET)
  310. peerlen = sizeof(data->peer.sa_in);
  311. # if OPENSSL_USE_IPV6
  312. else if (data->peer.sa.sa_family == AF_INET6)
  313. peerlen = sizeof(data->peer.sa_in6);
  314. # endif
  315. # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
  316. ret = sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
  317. # else
  318. ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
  319. # endif
  320. }
  321. BIO_clear_retry_flags(b);
  322. if (ret <= 0) {
  323. if (BIO_dgram_should_retry(ret)) {
  324. BIO_set_retry_write(b);
  325. data->_errno = get_last_socket_error();
  326. # if 0 /* higher layers are responsible for querying
  327. * MTU, if necessary */
  328. if (data->_errno == EMSGSIZE)
  329. /* retrieve the new MTU */
  330. BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  331. # endif
  332. }
  333. }
  334. return (ret);
  335. }
  336. static long dgram_get_mtu_overhead(bio_dgram_data *data)
  337. {
  338. long ret;
  339. switch (data->peer.sa.sa_family) {
  340. case AF_INET:
  341. /*
  342. * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
  343. */
  344. ret = 28;
  345. break;
  346. # if OPENSSL_USE_IPV6
  347. case AF_INET6:
  348. # ifdef IN6_IS_ADDR_V4MAPPED
  349. if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
  350. /*
  351. * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
  352. */
  353. ret = 28;
  354. else
  355. # endif
  356. /*
  357. * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
  358. */
  359. ret = 48;
  360. break;
  361. # endif
  362. default:
  363. /* We don't know. Go with the historical default */
  364. ret = 28;
  365. break;
  366. }
  367. return ret;
  368. }
  369. static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
  370. {
  371. long ret = 1;
  372. int *ip;
  373. struct sockaddr *to = NULL;
  374. bio_dgram_data *data = NULL;
  375. # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
  376. int sockopt_val = 0;
  377. socklen_t sockopt_len; /* assume that system supporting IP_MTU is
  378. * modern enough to define socklen_t */
  379. socklen_t addr_len;
  380. union {
  381. struct sockaddr sa;
  382. struct sockaddr_in s4;
  383. # if OPENSSL_USE_IPV6
  384. struct sockaddr_in6 s6;
  385. # endif
  386. } addr;
  387. # endif
  388. data = (bio_dgram_data *)b->ptr;
  389. switch (cmd) {
  390. case BIO_CTRL_RESET:
  391. num = 0;
  392. case BIO_C_FILE_SEEK:
  393. ret = 0;
  394. break;
  395. case BIO_C_FILE_TELL:
  396. case BIO_CTRL_INFO:
  397. ret = 0;
  398. break;
  399. case BIO_C_SET_FD:
  400. dgram_clear(b);
  401. b->num = *((int *)ptr);
  402. b->shutdown = (int)num;
  403. b->init = 1;
  404. break;
  405. case BIO_C_GET_FD:
  406. if (b->init) {
  407. ip = (int *)ptr;
  408. if (ip != NULL)
  409. *ip = b->num;
  410. ret = b->num;
  411. } else
  412. ret = -1;
  413. break;
  414. case BIO_CTRL_GET_CLOSE:
  415. ret = b->shutdown;
  416. break;
  417. case BIO_CTRL_SET_CLOSE:
  418. b->shutdown = (int)num;
  419. break;
  420. case BIO_CTRL_PENDING:
  421. case BIO_CTRL_WPENDING:
  422. ret = 0;
  423. break;
  424. case BIO_CTRL_DUP:
  425. case BIO_CTRL_FLUSH:
  426. ret = 1;
  427. break;
  428. case BIO_CTRL_DGRAM_CONNECT:
  429. to = (struct sockaddr *)ptr;
  430. # if 0
  431. if (connect(b->num, to, sizeof(struct sockaddr)) < 0) {
  432. perror("connect");
  433. ret = 0;
  434. } else {
  435. # endif
  436. switch (to->sa_family) {
  437. case AF_INET:
  438. memcpy(&data->peer, to, sizeof(data->peer.sa_in));
  439. break;
  440. # if OPENSSL_USE_IPV6
  441. case AF_INET6:
  442. memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
  443. break;
  444. # endif
  445. default:
  446. memcpy(&data->peer, to, sizeof(data->peer.sa));
  447. break;
  448. }
  449. # if 0
  450. }
  451. # endif
  452. break;
  453. /* (Linux)kernel sets DF bit on outgoing IP packets */
  454. case BIO_CTRL_DGRAM_MTU_DISCOVER:
  455. # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
  456. addr_len = (socklen_t) sizeof(addr);
  457. memset((void *)&addr, 0, sizeof(addr));
  458. if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
  459. ret = 0;
  460. break;
  461. }
  462. switch (addr.sa.sa_family) {
  463. case AF_INET:
  464. sockopt_val = IP_PMTUDISC_DO;
  465. if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
  466. &sockopt_val, sizeof(sockopt_val))) < 0)
  467. perror("setsockopt");
  468. break;
  469. # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
  470. case AF_INET6:
  471. sockopt_val = IPV6_PMTUDISC_DO;
  472. if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
  473. &sockopt_val, sizeof(sockopt_val))) < 0)
  474. perror("setsockopt");
  475. break;
  476. # endif
  477. default:
  478. ret = -1;
  479. break;
  480. }
  481. ret = -1;
  482. # else
  483. break;
  484. # endif
  485. case BIO_CTRL_DGRAM_QUERY_MTU:
  486. # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
  487. addr_len = (socklen_t) sizeof(addr);
  488. memset((void *)&addr, 0, sizeof(addr));
  489. if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
  490. ret = 0;
  491. break;
  492. }
  493. sockopt_len = sizeof(sockopt_val);
  494. switch (addr.sa.sa_family) {
  495. case AF_INET:
  496. if ((ret =
  497. getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
  498. &sockopt_len)) < 0 || sockopt_val < 0) {
  499. ret = 0;
  500. } else {
  501. /*
  502. * we assume that the transport protocol is UDP and no IP
  503. * options are used.
  504. */
  505. data->mtu = sockopt_val - 8 - 20;
  506. ret = data->mtu;
  507. }
  508. break;
  509. # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
  510. case AF_INET6:
  511. if ((ret =
  512. getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
  513. (void *)&sockopt_val, &sockopt_len)) < 0
  514. || sockopt_val < 0) {
  515. ret = 0;
  516. } else {
  517. /*
  518. * we assume that the transport protocol is UDP and no IPV6
  519. * options are used.
  520. */
  521. data->mtu = sockopt_val - 8 - 40;
  522. ret = data->mtu;
  523. }
  524. break;
  525. # endif
  526. default:
  527. ret = 0;
  528. break;
  529. }
  530. # else
  531. ret = 0;
  532. # endif
  533. break;
  534. case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
  535. ret = -dgram_get_mtu_overhead(data);
  536. switch (data->peer.sa.sa_family) {
  537. case AF_INET:
  538. ret += 576;
  539. break;
  540. # if OPENSSL_USE_IPV6
  541. case AF_INET6:
  542. # ifdef IN6_IS_ADDR_V4MAPPED
  543. if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
  544. ret += 576;
  545. else
  546. # endif
  547. ret += 1280;
  548. break;
  549. # endif
  550. default:
  551. ret += 576;
  552. break;
  553. }
  554. break;
  555. case BIO_CTRL_DGRAM_GET_MTU:
  556. return data->mtu;
  557. break;
  558. case BIO_CTRL_DGRAM_SET_MTU:
  559. data->mtu = num;
  560. ret = num;
  561. break;
  562. case BIO_CTRL_DGRAM_SET_CONNECTED:
  563. to = (struct sockaddr *)ptr;
  564. if (to != NULL) {
  565. data->connected = 1;
  566. switch (to->sa_family) {
  567. case AF_INET:
  568. memcpy(&data->peer, to, sizeof(data->peer.sa_in));
  569. break;
  570. # if OPENSSL_USE_IPV6
  571. case AF_INET6:
  572. memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
  573. break;
  574. # endif
  575. default:
  576. memcpy(&data->peer, to, sizeof(data->peer.sa));
  577. break;
  578. }
  579. } else {
  580. data->connected = 0;
  581. memset(&(data->peer), 0x00, sizeof(data->peer));
  582. }
  583. break;
  584. case BIO_CTRL_DGRAM_GET_PEER:
  585. switch (data->peer.sa.sa_family) {
  586. case AF_INET:
  587. ret = sizeof(data->peer.sa_in);
  588. break;
  589. # if OPENSSL_USE_IPV6
  590. case AF_INET6:
  591. ret = sizeof(data->peer.sa_in6);
  592. break;
  593. # endif
  594. default:
  595. ret = sizeof(data->peer.sa);
  596. break;
  597. }
  598. if (num == 0 || num > ret)
  599. num = ret;
  600. memcpy(ptr, &data->peer, (ret = num));
  601. break;
  602. case BIO_CTRL_DGRAM_SET_PEER:
  603. to = (struct sockaddr *)ptr;
  604. switch (to->sa_family) {
  605. case AF_INET:
  606. memcpy(&data->peer, to, sizeof(data->peer.sa_in));
  607. break;
  608. # if OPENSSL_USE_IPV6
  609. case AF_INET6:
  610. memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
  611. break;
  612. # endif
  613. default:
  614. memcpy(&data->peer, to, sizeof(data->peer.sa));
  615. break;
  616. }
  617. break;
  618. case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
  619. memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
  620. break;
  621. # if defined(SO_RCVTIMEO)
  622. case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
  623. # ifdef OPENSSL_SYS_WINDOWS
  624. {
  625. struct timeval *tv = (struct timeval *)ptr;
  626. int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  627. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  628. (void *)&timeout, sizeof(timeout)) < 0) {
  629. perror("setsockopt");
  630. ret = -1;
  631. }
  632. }
  633. # else
  634. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
  635. sizeof(struct timeval)) < 0) {
  636. perror("setsockopt");
  637. ret = -1;
  638. }
  639. # endif
  640. break;
  641. case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
  642. {
  643. union {
  644. size_t s;
  645. int i;
  646. } sz = {
  647. 0
  648. };
  649. # ifdef OPENSSL_SYS_WINDOWS
  650. int timeout;
  651. struct timeval *tv = (struct timeval *)ptr;
  652. sz.i = sizeof(timeout);
  653. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  654. (void *)&timeout, &sz.i) < 0) {
  655. perror("getsockopt");
  656. ret = -1;
  657. } else {
  658. tv->tv_sec = timeout / 1000;
  659. tv->tv_usec = (timeout % 1000) * 1000;
  660. ret = sizeof(*tv);
  661. }
  662. # else
  663. sz.i = sizeof(struct timeval);
  664. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  665. ptr, (void *)&sz) < 0) {
  666. perror("getsockopt");
  667. ret = -1;
  668. } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
  669. OPENSSL_assert(sz.s <= sizeof(struct timeval));
  670. ret = (int)sz.s;
  671. } else
  672. ret = sz.i;
  673. # endif
  674. }
  675. break;
  676. # endif
  677. # if defined(SO_SNDTIMEO)
  678. case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
  679. # ifdef OPENSSL_SYS_WINDOWS
  680. {
  681. struct timeval *tv = (struct timeval *)ptr;
  682. int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
  683. if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  684. (void *)&timeout, sizeof(timeout)) < 0) {
  685. perror("setsockopt");
  686. ret = -1;
  687. }
  688. }
  689. # else
  690. if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
  691. sizeof(struct timeval)) < 0) {
  692. perror("setsockopt");
  693. ret = -1;
  694. }
  695. # endif
  696. break;
  697. case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
  698. {
  699. union {
  700. size_t s;
  701. int i;
  702. } sz = {
  703. 0
  704. };
  705. # ifdef OPENSSL_SYS_WINDOWS
  706. int timeout;
  707. struct timeval *tv = (struct timeval *)ptr;
  708. sz.i = sizeof(timeout);
  709. if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  710. (void *)&timeout, &sz.i) < 0) {
  711. perror("getsockopt");
  712. ret = -1;
  713. } else {
  714. tv->tv_sec = timeout / 1000;
  715. tv->tv_usec = (timeout % 1000) * 1000;
  716. ret = sizeof(*tv);
  717. }
  718. # else
  719. sz.i = sizeof(struct timeval);
  720. if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  721. ptr, (void *)&sz) < 0) {
  722. perror("getsockopt");
  723. ret = -1;
  724. } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
  725. OPENSSL_assert(sz.s <= sizeof(struct timeval));
  726. ret = (int)sz.s;
  727. } else
  728. ret = sz.i;
  729. # endif
  730. }
  731. break;
  732. # endif
  733. case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
  734. /* fall-through */
  735. case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
  736. # ifdef OPENSSL_SYS_WINDOWS
  737. if (data->_errno == WSAETIMEDOUT)
  738. # else
  739. if (data->_errno == EAGAIN)
  740. # endif
  741. {
  742. ret = 1;
  743. data->_errno = 0;
  744. } else
  745. ret = 0;
  746. break;
  747. # ifdef EMSGSIZE
  748. case BIO_CTRL_DGRAM_MTU_EXCEEDED:
  749. if (data->_errno == EMSGSIZE) {
  750. ret = 1;
  751. data->_errno = 0;
  752. } else
  753. ret = 0;
  754. break;
  755. # endif
  756. case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
  757. ret = dgram_get_mtu_overhead(data);
  758. break;
  759. default:
  760. ret = 0;
  761. break;
  762. }
  763. return (ret);
  764. }
  765. static int dgram_puts(BIO *bp, const char *str)
  766. {
  767. int n, ret;
  768. n = strlen(str);
  769. ret = dgram_write(bp, str, n);
  770. return (ret);
  771. }
  772. static int BIO_dgram_should_retry(int i)
  773. {
  774. int err;
  775. if ((i == 0) || (i == -1)) {
  776. err = get_last_socket_error();
  777. # if defined(OPENSSL_SYS_WINDOWS)
  778. /*
  779. * If the socket return value (i) is -1 and err is unexpectedly 0 at
  780. * this point, the error code was overwritten by another system call
  781. * before this error handling is called.
  782. */
  783. # endif
  784. return (BIO_dgram_non_fatal_error(err));
  785. }
  786. return (0);
  787. }
  788. int BIO_dgram_non_fatal_error(int err)
  789. {
  790. switch (err) {
  791. # if defined(OPENSSL_SYS_WINDOWS)
  792. # if defined(WSAEWOULDBLOCK)
  793. case WSAEWOULDBLOCK:
  794. # endif
  795. # if 0 /* This appears to always be an error */
  796. # if defined(WSAENOTCONN)
  797. case WSAENOTCONN:
  798. # endif
  799. # endif
  800. # endif
  801. # ifdef EWOULDBLOCK
  802. # ifdef WSAEWOULDBLOCK
  803. # if WSAEWOULDBLOCK != EWOULDBLOCK
  804. case EWOULDBLOCK:
  805. # endif
  806. # else
  807. case EWOULDBLOCK:
  808. # endif
  809. # endif
  810. # ifdef EINTR
  811. case EINTR:
  812. # endif
  813. # ifdef EAGAIN
  814. # if EWOULDBLOCK != EAGAIN
  815. case EAGAIN:
  816. # endif
  817. # endif
  818. # ifdef EPROTO
  819. case EPROTO:
  820. # endif
  821. # ifdef EINPROGRESS
  822. case EINPROGRESS:
  823. # endif
  824. # ifdef EALREADY
  825. case EALREADY:
  826. # endif
  827. return (1);
  828. /* break; */
  829. default:
  830. break;
  831. }
  832. return (0);
  833. }
  834. static void get_current_time(struct timeval *t)
  835. {
  836. # ifdef OPENSSL_SYS_WIN32
  837. struct _timeb tb;
  838. _ftime(&tb);
  839. t->tv_sec = (long)tb.time;
  840. t->tv_usec = (long)tb.millitm * 1000;
  841. # elif defined(OPENSSL_SYS_VMS)
  842. struct timeb tb;
  843. ftime(&tb);
  844. t->tv_sec = (long)tb.time;
  845. t->tv_usec = (long)tb.millitm * 1000;
  846. # else
  847. gettimeofday(t, NULL);
  848. # endif
  849. }
  850. #endif