2
0

bss_dgram.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. {
  87. BIO_TYPE_DGRAM,
  88. "datagram socket",
  89. dgram_write,
  90. dgram_read,
  91. dgram_puts,
  92. NULL, /* dgram_gets, */
  93. dgram_ctrl,
  94. dgram_new,
  95. dgram_free,
  96. NULL,
  97. };
  98. typedef struct bio_dgram_data_st
  99. {
  100. union {
  101. struct sockaddr sa;
  102. struct sockaddr_in sa_in;
  103. #if OPENSSL_USE_IPV6
  104. struct sockaddr_in6 sa_in6;
  105. #endif
  106. } peer;
  107. unsigned int connected;
  108. unsigned int _errno;
  109. unsigned int mtu;
  110. struct timeval next_timeout;
  111. struct timeval socket_timeout;
  112. } bio_dgram_data;
  113. BIO_METHOD *BIO_s_datagram(void)
  114. {
  115. return(&methods_dgramp);
  116. }
  117. BIO *BIO_new_dgram(int fd, int close_flag)
  118. {
  119. BIO *ret;
  120. ret=BIO_new(BIO_s_datagram());
  121. if (ret == NULL) return(NULL);
  122. BIO_set_fd(ret,fd,close_flag);
  123. return(ret);
  124. }
  125. static int dgram_new(BIO *bi)
  126. {
  127. bio_dgram_data *data = NULL;
  128. bi->init=0;
  129. bi->num=0;
  130. data = OPENSSL_malloc(sizeof(bio_dgram_data));
  131. if (data == NULL)
  132. return 0;
  133. memset(data, 0x00, sizeof(bio_dgram_data));
  134. bi->ptr = data;
  135. bi->flags=0;
  136. return(1);
  137. }
  138. static int dgram_free(BIO *a)
  139. {
  140. bio_dgram_data *data;
  141. if (a == NULL) return(0);
  142. if ( ! dgram_clear(a))
  143. return 0;
  144. data = (bio_dgram_data *)a->ptr;
  145. if(data != NULL) OPENSSL_free(data);
  146. return(1);
  147. }
  148. static int dgram_clear(BIO *a)
  149. {
  150. if (a == NULL) return(0);
  151. if (a->shutdown)
  152. {
  153. if (a->init)
  154. {
  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. int sz = sizeof(int);
  167. /* Is a timer active? */
  168. if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
  169. {
  170. struct timeval timenow, timeleft;
  171. /* Read current socket timeout */
  172. #ifdef OPENSSL_SYS_WINDOWS
  173. int timeout;
  174. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  175. (void*)&timeout, &sz) < 0)
  176. { perror("getsockopt"); }
  177. else
  178. {
  179. data->socket_timeout.tv_sec = timeout / 1000;
  180. data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
  181. }
  182. #else
  183. if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  184. &(data->socket_timeout), (void *)&sz) < 0)
  185. { perror("getsockopt"); }
  186. #endif
  187. /* Get current time */
  188. get_current_time(&timenow);
  189. /* Calculate time left until timer expires */
  190. memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
  191. timeleft.tv_sec -= timenow.tv_sec;
  192. timeleft.tv_usec -= timenow.tv_usec;
  193. if (timeleft.tv_usec < 0)
  194. {
  195. timeleft.tv_sec--;
  196. timeleft.tv_usec += 1000000;
  197. }
  198. if (timeleft.tv_sec < 0)
  199. {
  200. timeleft.tv_sec = 0;
  201. timeleft.tv_usec = 1;
  202. }
  203. /* Adjust socket timeout if next handhake message timer
  204. * will expire earlier.
  205. */
  206. if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
  207. (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
  208. (data->socket_timeout.tv_sec == timeleft.tv_sec &&
  209. data->socket_timeout.tv_usec >= timeleft.tv_usec))
  210. {
  211. #ifdef OPENSSL_SYS_WINDOWS
  212. timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
  213. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  214. (void*)&timeout, sizeof(timeout)) < 0)
  215. { perror("setsockopt"); }
  216. #else
  217. if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
  218. sizeof(struct timeval)) < 0)
  219. { perror("setsockopt"); }
  220. #endif
  221. }
  222. }
  223. #endif
  224. }
  225. static void dgram_reset_rcv_timeout(BIO *b)
  226. {
  227. #if defined(SO_RCVTIMEO)
  228. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  229. /* Is a timer active? */
  230. if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
  231. {
  232. #ifdef OPENSSL_SYS_WINDOWS
  233. int timeout = data->socket_timeout.tv_sec * 1000 +
  234. data->socket_timeout.tv_usec / 1000;
  235. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  236. (void*)&timeout, sizeof(timeout)) < 0)
  237. { perror("setsockopt"); }
  238. #else
  239. if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
  240. sizeof(struct timeval)) < 0)
  241. { perror("setsockopt"); }
  242. #endif
  243. }
  244. #endif
  245. }
  246. static int dgram_read(BIO *b, char *out, int outl)
  247. {
  248. int ret=0;
  249. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  250. struct {
  251. /*
  252. * See commentary in b_sock.c. <appro>
  253. */
  254. union { size_t s; int i; } len;
  255. union {
  256. struct sockaddr sa;
  257. struct sockaddr_in sa_in;
  258. #if OPENSSL_USE_IPV6
  259. struct sockaddr_in6 sa_in6;
  260. #endif
  261. } peer;
  262. } sa;
  263. sa.len.s=0;
  264. sa.len.i=sizeof(sa.peer);
  265. if (out != NULL)
  266. {
  267. clear_socket_error();
  268. memset(&sa.peer, 0x00, sizeof(sa.peer));
  269. dgram_adjust_rcv_timeout(b);
  270. ret=recvfrom(b->num,out,outl,0,&sa.peer.sa,(void *)&sa.len);
  271. if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
  272. {
  273. OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
  274. sa.len.i = (int)sa.len.s;
  275. }
  276. if ( ! data->connected && ret >= 0)
  277. BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
  278. BIO_clear_retry_flags(b);
  279. if (ret < 0)
  280. {
  281. if (BIO_dgram_should_retry(ret))
  282. {
  283. BIO_set_retry_read(b);
  284. data->_errno = get_last_socket_error();
  285. }
  286. }
  287. dgram_reset_rcv_timeout(b);
  288. }
  289. return(ret);
  290. }
  291. static int dgram_write(BIO *b, const char *in, int inl)
  292. {
  293. int ret;
  294. bio_dgram_data *data = (bio_dgram_data *)b->ptr;
  295. clear_socket_error();
  296. if ( data->connected )
  297. ret=writesocket(b->num,in,inl);
  298. else
  299. {
  300. int peerlen = sizeof(data->peer);
  301. if (data->peer.sa.sa_family == AF_INET)
  302. peerlen = sizeof(data->peer.sa_in);
  303. #if OPENSSL_USE_IPV6
  304. else if (data->peer.sa.sa_family == AF_INET6)
  305. peerlen = sizeof(data->peer.sa_in6);
  306. #endif
  307. #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
  308. ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
  309. #else
  310. ret=sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
  311. #endif
  312. }
  313. BIO_clear_retry_flags(b);
  314. if (ret <= 0)
  315. {
  316. if (BIO_dgram_should_retry(ret))
  317. {
  318. BIO_set_retry_write(b);
  319. data->_errno = get_last_socket_error();
  320. #if 0 /* higher layers are responsible for querying MTU, if necessary */
  321. if ( data->_errno == EMSGSIZE)
  322. /* retrieve the new MTU */
  323. BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
  324. #endif
  325. }
  326. }
  327. return(ret);
  328. }
  329. static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
  330. {
  331. long ret=1;
  332. int *ip;
  333. struct sockaddr *to = NULL;
  334. bio_dgram_data *data = NULL;
  335. #if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
  336. long sockopt_val = 0;
  337. unsigned int sockopt_len = 0;
  338. #endif
  339. #ifdef OPENSSL_SYS_LINUX
  340. socklen_t addr_len;
  341. union {
  342. struct sockaddr sa;
  343. struct sockaddr_in s4;
  344. #if OPENSSL_USE_IPV6
  345. struct sockaddr_in6 s6;
  346. #endif
  347. } addr;
  348. #endif
  349. data = (bio_dgram_data *)b->ptr;
  350. switch (cmd)
  351. {
  352. case BIO_CTRL_RESET:
  353. num=0;
  354. case BIO_C_FILE_SEEK:
  355. ret=0;
  356. break;
  357. case BIO_C_FILE_TELL:
  358. case BIO_CTRL_INFO:
  359. ret=0;
  360. break;
  361. case BIO_C_SET_FD:
  362. dgram_clear(b);
  363. b->num= *((int *)ptr);
  364. b->shutdown=(int)num;
  365. b->init=1;
  366. break;
  367. case BIO_C_GET_FD:
  368. if (b->init)
  369. {
  370. ip=(int *)ptr;
  371. if (ip != NULL) *ip=b->num;
  372. ret=b->num;
  373. }
  374. else
  375. ret= -1;
  376. break;
  377. case BIO_CTRL_GET_CLOSE:
  378. ret=b->shutdown;
  379. break;
  380. case BIO_CTRL_SET_CLOSE:
  381. b->shutdown=(int)num;
  382. break;
  383. case BIO_CTRL_PENDING:
  384. case BIO_CTRL_WPENDING:
  385. ret=0;
  386. break;
  387. case BIO_CTRL_DUP:
  388. case BIO_CTRL_FLUSH:
  389. ret=1;
  390. break;
  391. case BIO_CTRL_DGRAM_CONNECT:
  392. to = (struct sockaddr *)ptr;
  393. #if 0
  394. if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
  395. { perror("connect"); ret = 0; }
  396. else
  397. {
  398. #endif
  399. switch (to->sa_family)
  400. {
  401. case AF_INET:
  402. memcpy(&data->peer,to,sizeof(data->peer.sa_in));
  403. break;
  404. #if OPENSSL_USE_IPV6
  405. case AF_INET6:
  406. memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
  407. break;
  408. #endif
  409. default:
  410. memcpy(&data->peer,to,sizeof(data->peer.sa));
  411. break;
  412. }
  413. #if 0
  414. }
  415. #endif
  416. break;
  417. /* (Linux)kernel sets DF bit on outgoing IP packets */
  418. case BIO_CTRL_DGRAM_MTU_DISCOVER:
  419. #ifdef OPENSSL_SYS_LINUX
  420. addr_len = (socklen_t)sizeof(addr);
  421. memset((void *)&addr, 0, sizeof(addr));
  422. if (getsockname(b->num, &addr.sa, &addr_len) < 0)
  423. {
  424. ret = 0;
  425. break;
  426. }
  427. sockopt_len = sizeof(sockopt_val);
  428. switch (addr.sa.sa_family)
  429. {
  430. case AF_INET:
  431. sockopt_val = IP_PMTUDISC_DO;
  432. if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
  433. &sockopt_val, sizeof(sockopt_val))) < 0)
  434. perror("setsockopt");
  435. break;
  436. #if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER)
  437. case AF_INET6:
  438. sockopt_val = IPV6_PMTUDISC_DO;
  439. if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
  440. &sockopt_val, sizeof(sockopt_val))) < 0)
  441. perror("setsockopt");
  442. break;
  443. #endif
  444. default:
  445. ret = -1;
  446. break;
  447. }
  448. ret = -1;
  449. #else
  450. break;
  451. #endif
  452. case BIO_CTRL_DGRAM_QUERY_MTU:
  453. #ifdef OPENSSL_SYS_LINUX
  454. addr_len = (socklen_t)sizeof(addr);
  455. memset((void *)&addr, 0, sizeof(addr));
  456. if (getsockname(b->num, &addr.sa, &addr_len) < 0)
  457. {
  458. ret = 0;
  459. break;
  460. }
  461. sockopt_len = sizeof(sockopt_val);
  462. switch (addr.sa.sa_family)
  463. {
  464. case AF_INET:
  465. if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
  466. &sockopt_len)) < 0 || sockopt_val < 0)
  467. {
  468. ret = 0;
  469. }
  470. else
  471. {
  472. /* we assume that the transport protocol is UDP and no
  473. * IP options are used.
  474. */
  475. data->mtu = sockopt_val - 8 - 20;
  476. ret = data->mtu;
  477. }
  478. break;
  479. #if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
  480. case AF_INET6:
  481. if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
  482. &sockopt_len)) < 0 || sockopt_val < 0)
  483. {
  484. ret = 0;
  485. }
  486. else
  487. {
  488. /* we assume that the transport protocol is UDP and no
  489. * IPV6 options are used.
  490. */
  491. data->mtu = sockopt_val - 8 - 40;
  492. ret = data->mtu;
  493. }
  494. break;
  495. #endif
  496. default:
  497. ret = 0;
  498. break;
  499. }
  500. #else
  501. ret = 0;
  502. #endif
  503. break;
  504. case BIO_CTRL_DGRAM_GET_MTU:
  505. return data->mtu;
  506. break;
  507. case BIO_CTRL_DGRAM_SET_MTU:
  508. data->mtu = num;
  509. ret = num;
  510. break;
  511. case BIO_CTRL_DGRAM_SET_CONNECTED:
  512. to = (struct sockaddr *)ptr;
  513. if ( to != NULL)
  514. {
  515. data->connected = 1;
  516. switch (to->sa_family)
  517. {
  518. case AF_INET:
  519. memcpy(&data->peer,to,sizeof(data->peer.sa_in));
  520. break;
  521. #if OPENSSL_USE_IPV6
  522. case AF_INET6:
  523. memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
  524. break;
  525. #endif
  526. default:
  527. memcpy(&data->peer,to,sizeof(data->peer.sa));
  528. break;
  529. }
  530. }
  531. else
  532. {
  533. data->connected = 0;
  534. memset(&(data->peer), 0x00, sizeof(data->peer));
  535. }
  536. break;
  537. case BIO_CTRL_DGRAM_GET_PEER:
  538. switch (data->peer.sa.sa_family)
  539. {
  540. case AF_INET:
  541. ret=sizeof(data->peer.sa_in);
  542. break;
  543. #if OPENSSL_USE_IPV6
  544. case AF_INET6:
  545. ret=sizeof(data->peer.sa_in6);
  546. break;
  547. #endif
  548. default:
  549. ret=sizeof(data->peer.sa);
  550. break;
  551. }
  552. if (num==0 || num>ret)
  553. num=ret;
  554. memcpy(ptr,&data->peer,(ret=num));
  555. break;
  556. case BIO_CTRL_DGRAM_SET_PEER:
  557. to = (struct sockaddr *) ptr;
  558. switch (to->sa_family)
  559. {
  560. case AF_INET:
  561. memcpy(&data->peer,to,sizeof(data->peer.sa_in));
  562. break;
  563. #if OPENSSL_USE_IPV6
  564. case AF_INET6:
  565. memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
  566. break;
  567. #endif
  568. default:
  569. memcpy(&data->peer,to,sizeof(data->peer.sa));
  570. break;
  571. }
  572. break;
  573. case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
  574. memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
  575. break;
  576. #if defined(SO_RCVTIMEO)
  577. case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
  578. #ifdef OPENSSL_SYS_WINDOWS
  579. {
  580. struct timeval *tv = (struct timeval *)ptr;
  581. int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
  582. if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  583. (void*)&timeout, sizeof(timeout)) < 0)
  584. { perror("setsockopt"); ret = -1; }
  585. }
  586. #else
  587. if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
  588. sizeof(struct timeval)) < 0)
  589. { perror("setsockopt"); ret = -1; }
  590. #endif
  591. break;
  592. case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
  593. #ifdef OPENSSL_SYS_WINDOWS
  594. {
  595. int timeout, sz = sizeof(timeout);
  596. struct timeval *tv = (struct timeval *)ptr;
  597. if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  598. (void*)&timeout, &sz) < 0)
  599. { perror("getsockopt"); ret = -1; }
  600. else
  601. {
  602. tv->tv_sec = timeout / 1000;
  603. tv->tv_usec = (timeout % 1000) * 1000;
  604. ret = sizeof(*tv);
  605. }
  606. }
  607. #else
  608. if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
  609. ptr, (void *)&ret) < 0)
  610. { perror("getsockopt"); ret = -1; }
  611. #endif
  612. break;
  613. #endif
  614. #if defined(SO_SNDTIMEO)
  615. case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
  616. #ifdef OPENSSL_SYS_WINDOWS
  617. {
  618. struct timeval *tv = (struct timeval *)ptr;
  619. int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
  620. if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  621. (void*)&timeout, sizeof(timeout)) < 0)
  622. { perror("setsockopt"); ret = -1; }
  623. }
  624. #else
  625. if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
  626. sizeof(struct timeval)) < 0)
  627. { perror("setsockopt"); ret = -1; }
  628. #endif
  629. break;
  630. case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
  631. #ifdef OPENSSL_SYS_WINDOWS
  632. {
  633. int timeout, sz = sizeof(timeout);
  634. struct timeval *tv = (struct timeval *)ptr;
  635. if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  636. (void*)&timeout, &sz) < 0)
  637. { perror("getsockopt"); ret = -1; }
  638. else
  639. {
  640. tv->tv_sec = timeout / 1000;
  641. tv->tv_usec = (timeout % 1000) * 1000;
  642. ret = sizeof(*tv);
  643. }
  644. }
  645. #else
  646. if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
  647. ptr, (void *)&ret) < 0)
  648. { perror("getsockopt"); ret = -1; }
  649. #endif
  650. break;
  651. #endif
  652. case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
  653. /* fall-through */
  654. case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
  655. #ifdef OPENSSL_SYS_WINDOWS
  656. if ( data->_errno == WSAETIMEDOUT)
  657. #else
  658. if ( data->_errno == EAGAIN)
  659. #endif
  660. {
  661. ret = 1;
  662. data->_errno = 0;
  663. }
  664. else
  665. ret = 0;
  666. break;
  667. #ifdef EMSGSIZE
  668. case BIO_CTRL_DGRAM_MTU_EXCEEDED:
  669. if ( data->_errno == EMSGSIZE)
  670. {
  671. ret = 1;
  672. data->_errno = 0;
  673. }
  674. else
  675. ret = 0;
  676. break;
  677. #endif
  678. default:
  679. ret=0;
  680. break;
  681. }
  682. return(ret);
  683. }
  684. static int dgram_puts(BIO *bp, const char *str)
  685. {
  686. int n,ret;
  687. n=strlen(str);
  688. ret=dgram_write(bp,str,n);
  689. return(ret);
  690. }
  691. static int BIO_dgram_should_retry(int i)
  692. {
  693. int err;
  694. if ((i == 0) || (i == -1))
  695. {
  696. err=get_last_socket_error();
  697. #if defined(OPENSSL_SYS_WINDOWS)
  698. /* If the socket return value (i) is -1
  699. * and err is unexpectedly 0 at this point,
  700. * the error code was overwritten by
  701. * another system call before this error
  702. * handling is called.
  703. */
  704. #endif
  705. return(BIO_dgram_non_fatal_error(err));
  706. }
  707. return(0);
  708. }
  709. int BIO_dgram_non_fatal_error(int err)
  710. {
  711. switch (err)
  712. {
  713. #if defined(OPENSSL_SYS_WINDOWS)
  714. # if defined(WSAEWOULDBLOCK)
  715. case WSAEWOULDBLOCK:
  716. # endif
  717. # if 0 /* This appears to always be an error */
  718. # if defined(WSAENOTCONN)
  719. case WSAENOTCONN:
  720. # endif
  721. # endif
  722. #endif
  723. #ifdef EWOULDBLOCK
  724. # ifdef WSAEWOULDBLOCK
  725. # if WSAEWOULDBLOCK != EWOULDBLOCK
  726. case EWOULDBLOCK:
  727. # endif
  728. # else
  729. case EWOULDBLOCK:
  730. # endif
  731. #endif
  732. #ifdef EINTR
  733. case EINTR:
  734. #endif
  735. #ifdef EAGAIN
  736. #if EWOULDBLOCK != EAGAIN
  737. case EAGAIN:
  738. # endif
  739. #endif
  740. #ifdef EPROTO
  741. case EPROTO:
  742. #endif
  743. #ifdef EINPROGRESS
  744. case EINPROGRESS:
  745. #endif
  746. #ifdef EALREADY
  747. case EALREADY:
  748. #endif
  749. return(1);
  750. /* break; */
  751. default:
  752. break;
  753. }
  754. return(0);
  755. }
  756. static void get_current_time(struct timeval *t)
  757. {
  758. #ifdef OPENSSL_SYS_WIN32
  759. struct _timeb tb;
  760. _ftime(&tb);
  761. t->tv_sec = (long)tb.time;
  762. t->tv_usec = (long)tb.millitm * 1000;
  763. #elif defined(OPENSSL_SYS_VMS)
  764. struct timeb tb;
  765. ftime(&tb);
  766. t->tv_sec = (long)tb.time;
  767. t->tv_usec = (long)tb.millitm * 1000;
  768. #else
  769. gettimeofday(t, NULL);
  770. #endif
  771. }
  772. #endif