1 /* crypto/bio/bio_dgram.c */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
66 #include <openssl/bio.h>
67 #ifndef OPENSSL_NO_DGRAM
69 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
70 #include <sys/timeb.h>
73 #ifdef OPENSSL_SYS_LINUX
74 #define IP_MTU 14 /* linux is lame */
78 #define sock_write SockWrite /* Watt-32 uses same names */
79 #define sock_read SockRead
80 #define sock_puts SockPuts
83 static int dgram_write(BIO *h, const char *buf, int num);
84 static int dgram_read(BIO *h, char *buf, int size);
85 static int dgram_puts(BIO *h, const char *str);
86 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
87 static int dgram_new(BIO *h);
88 static int dgram_free(BIO *data);
89 static int dgram_clear(BIO *bio);
91 static int BIO_dgram_should_retry(int s);
93 static void get_current_time(struct timeval *t);
95 static BIO_METHOD methods_dgramp=
102 NULL, /* dgram_gets, */
109 typedef struct bio_dgram_data_st
113 struct sockaddr_in sa_in;
115 struct sockaddr_in6 sa_in6;
118 unsigned int connected;
121 struct timeval next_timeout;
122 struct timeval socket_timeout;
125 BIO_METHOD *BIO_s_datagram(void)
127 return(&methods_dgramp);
130 BIO *BIO_new_dgram(int fd, int close_flag)
134 ret=BIO_new(BIO_s_datagram());
135 if (ret == NULL) return(NULL);
136 BIO_set_fd(ret,fd,close_flag);
140 static int dgram_new(BIO *bi)
142 bio_dgram_data *data = NULL;
146 data = OPENSSL_malloc(sizeof(bio_dgram_data));
149 memset(data, 0x00, sizeof(bio_dgram_data));
156 static int dgram_free(BIO *a)
158 bio_dgram_data *data;
160 if (a == NULL) return(0);
161 if ( ! dgram_clear(a))
164 data = (bio_dgram_data *)a->ptr;
165 if(data != NULL) OPENSSL_free(data);
170 static int dgram_clear(BIO *a)
172 if (a == NULL) return(0);
185 static void dgram_adjust_rcv_timeout(BIO *b)
187 #if defined(SO_RCVTIMEO)
188 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
189 int sz = sizeof(int);
191 /* Is a timer active? */
192 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
194 struct timeval timenow, timeleft;
196 /* Read current socket timeout */
197 #ifdef OPENSSL_SYS_WINDOWS
199 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
200 (void*)&timeout, &sz) < 0)
201 { perror("getsockopt"); }
204 data->socket_timeout.tv_sec = timeout / 1000;
205 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
208 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
209 &(data->socket_timeout), (void *)&sz) < 0)
210 { perror("getsockopt"); }
213 /* Get current time */
214 get_current_time(&timenow);
216 /* Calculate time left until timer expires */
217 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
218 timeleft.tv_sec -= timenow.tv_sec;
219 timeleft.tv_usec -= timenow.tv_usec;
220 if (timeleft.tv_usec < 0)
223 timeleft.tv_usec += 1000000;
226 if (timeleft.tv_sec < 0)
229 timeleft.tv_usec = 1;
232 /* Adjust socket timeout if next handhake message timer
233 * will expire earlier.
235 if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
236 (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
237 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
238 data->socket_timeout.tv_usec >= timeleft.tv_usec))
240 #ifdef OPENSSL_SYS_WINDOWS
241 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
242 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
243 (void*)&timeout, sizeof(timeout)) < 0)
244 { perror("setsockopt"); }
246 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
247 sizeof(struct timeval)) < 0)
248 { perror("setsockopt"); }
255 static void dgram_reset_rcv_timeout(BIO *b)
257 #if defined(SO_RCVTIMEO)
258 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
260 /* Is a timer active? */
261 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
263 #ifdef OPENSSL_SYS_WINDOWS
264 int timeout = data->socket_timeout.tv_sec * 1000 +
265 data->socket_timeout.tv_usec / 1000;
266 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
267 (void*)&timeout, sizeof(timeout)) < 0)
268 { perror("setsockopt"); }
270 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
271 sizeof(struct timeval)) < 0)
272 { perror("setsockopt"); }
278 static int dgram_read(BIO *b, char *out, int outl)
281 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
285 * See commentary in b_sock.c. <appro>
287 union { size_t s; int i; } len;
290 struct sockaddr_in sa_in;
292 struct sockaddr_in6 sa_in6;
298 sa.len.i=sizeof(sa.peer);
302 clear_socket_error();
303 memset(&sa.peer, 0x00, sizeof(sa.peer));
304 dgram_adjust_rcv_timeout(b);
305 ret=recvfrom(b->num,out,outl,0,&sa.peer.sa,(void *)&sa.len);
306 if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
308 OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
309 sa.len.i = (int)sa.len.s;
312 if ( ! data->connected && ret >= 0)
313 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
315 BIO_clear_retry_flags(b);
318 if (BIO_dgram_should_retry(ret))
320 BIO_set_retry_read(b);
321 data->_errno = get_last_socket_error();
325 dgram_reset_rcv_timeout(b);
330 static int dgram_write(BIO *b, const char *in, int inl)
333 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
334 clear_socket_error();
336 if ( data->connected )
337 ret=writesocket(b->num,in,inl);
340 int peerlen = sizeof(data->peer);
342 if (data->peer.sa.sa_family == AF_INET)
343 peerlen = sizeof(data->peer.sa_in);
345 else if (data->peer.sa.sa_family == AF_INET6)
346 peerlen = sizeof(data->peer.sa_in6);
348 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
349 ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
351 ret=sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
355 BIO_clear_retry_flags(b);
358 if (BIO_dgram_should_retry(ret))
360 BIO_set_retry_write(b);
361 data->_errno = get_last_socket_error();
363 #if 0 /* higher layers are responsible for querying MTU, if necessary */
364 if ( data->_errno == EMSGSIZE)
365 /* retrieve the new MTU */
366 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
373 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
377 struct sockaddr *to = NULL;
378 bio_dgram_data *data = NULL;
379 #if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
380 long sockopt_val = 0;
381 unsigned int sockopt_len = 0;
383 #ifdef OPENSSL_SYS_LINUX
387 struct sockaddr_in s4;
389 struct sockaddr_in6 s6;
394 data = (bio_dgram_data *)b->ptr;
400 case BIO_C_FILE_SEEK:
403 case BIO_C_FILE_TELL:
409 b->num= *((int *)ptr);
410 b->shutdown=(int)num;
417 if (ip != NULL) *ip=b->num;
423 case BIO_CTRL_GET_CLOSE:
426 case BIO_CTRL_SET_CLOSE:
427 b->shutdown=(int)num;
429 case BIO_CTRL_PENDING:
430 case BIO_CTRL_WPENDING:
437 case BIO_CTRL_DGRAM_CONNECT:
438 to = (struct sockaddr *)ptr;
440 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
441 { perror("connect"); ret = 0; }
445 switch (to->sa_family)
448 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
452 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
456 memcpy(&data->peer,to,sizeof(data->peer.sa));
463 /* (Linux)kernel sets DF bit on outgoing IP packets */
464 case BIO_CTRL_DGRAM_MTU_DISCOVER:
465 #ifdef OPENSSL_SYS_LINUX
466 addr_len = (socklen_t)sizeof(addr);
467 memset((void *)&addr, 0, sizeof(addr));
468 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
473 sockopt_len = sizeof(sockopt_val);
474 switch (addr.sa.sa_family)
477 sockopt_val = IP_PMTUDISC_DO;
478 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
479 &sockopt_val, sizeof(sockopt_val))) < 0)
480 perror("setsockopt");
482 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER)
484 sockopt_val = IPV6_PMTUDISC_DO;
485 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
486 &sockopt_val, sizeof(sockopt_val))) < 0)
487 perror("setsockopt");
498 case BIO_CTRL_DGRAM_QUERY_MTU:
499 #ifdef OPENSSL_SYS_LINUX
500 addr_len = (socklen_t)sizeof(addr);
501 memset((void *)&addr, 0, sizeof(addr));
502 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
507 sockopt_len = sizeof(sockopt_val);
508 switch (addr.sa.sa_family)
511 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
512 &sockopt_len)) < 0 || sockopt_val < 0)
518 /* we assume that the transport protocol is UDP and no
519 * IP options are used.
521 data->mtu = sockopt_val - 8 - 20;
525 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
527 if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
528 &sockopt_len)) < 0 || sockopt_val < 0)
534 /* we assume that the transport protocol is UDP and no
535 * IPV6 options are used.
537 data->mtu = sockopt_val - 8 - 40;
550 case BIO_CTRL_DGRAM_GET_MTU:
553 case BIO_CTRL_DGRAM_SET_MTU:
557 case BIO_CTRL_DGRAM_SET_CONNECTED:
558 to = (struct sockaddr *)ptr;
563 switch (to->sa_family)
566 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
570 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
574 memcpy(&data->peer,to,sizeof(data->peer.sa));
581 memset(&(data->peer), 0x00, sizeof(data->peer));
584 case BIO_CTRL_DGRAM_GET_PEER:
585 switch (data->peer.sa.sa_family)
588 ret=sizeof(data->peer.sa_in);
592 ret=sizeof(data->peer.sa_in6);
596 ret=sizeof(data->peer.sa);
599 if (num==0 || num>ret)
601 memcpy(ptr,&data->peer,(ret=num));
603 case BIO_CTRL_DGRAM_SET_PEER:
604 to = (struct sockaddr *) ptr;
605 switch (to->sa_family)
608 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
612 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
616 memcpy(&data->peer,to,sizeof(data->peer.sa));
620 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
621 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
623 #if defined(SO_RCVTIMEO)
624 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
625 #ifdef OPENSSL_SYS_WINDOWS
627 struct timeval *tv = (struct timeval *)ptr;
628 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
629 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
630 (void*)&timeout, sizeof(timeout)) < 0)
631 { perror("setsockopt"); ret = -1; }
634 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
635 sizeof(struct timeval)) < 0)
636 { perror("setsockopt"); ret = -1; }
639 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
640 #ifdef OPENSSL_SYS_WINDOWS
642 int timeout, sz = sizeof(timeout);
643 struct timeval *tv = (struct timeval *)ptr;
644 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
645 (void*)&timeout, &sz) < 0)
646 { perror("getsockopt"); ret = -1; }
649 tv->tv_sec = timeout / 1000;
650 tv->tv_usec = (timeout % 1000) * 1000;
655 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
656 ptr, (void *)&ret) < 0)
657 { perror("getsockopt"); ret = -1; }
661 #if defined(SO_SNDTIMEO)
662 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
663 #ifdef OPENSSL_SYS_WINDOWS
665 struct timeval *tv = (struct timeval *)ptr;
666 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
667 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
668 (void*)&timeout, sizeof(timeout)) < 0)
669 { perror("setsockopt"); ret = -1; }
672 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
673 sizeof(struct timeval)) < 0)
674 { perror("setsockopt"); ret = -1; }
677 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
678 #ifdef OPENSSL_SYS_WINDOWS
680 int timeout, sz = sizeof(timeout);
681 struct timeval *tv = (struct timeval *)ptr;
682 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
683 (void*)&timeout, &sz) < 0)
684 { perror("getsockopt"); ret = -1; }
687 tv->tv_sec = timeout / 1000;
688 tv->tv_usec = (timeout % 1000) * 1000;
693 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
694 ptr, (void *)&ret) < 0)
695 { perror("getsockopt"); ret = -1; }
699 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
701 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
702 #ifdef OPENSSL_SYS_WINDOWS
703 if ( data->_errno == WSAETIMEDOUT)
705 if ( data->_errno == EAGAIN)
715 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
716 if ( data->_errno == EMSGSIZE)
732 static int dgram_puts(BIO *bp, const char *str)
737 ret=dgram_write(bp,str,n);
741 static int BIO_dgram_should_retry(int i)
745 if ((i == 0) || (i == -1))
747 err=get_last_socket_error();
749 #if defined(OPENSSL_SYS_WINDOWS)
750 /* If the socket return value (i) is -1
751 * and err is unexpectedly 0 at this point,
752 * the error code was overwritten by
753 * another system call before this error
754 * handling is called.
758 return(BIO_dgram_non_fatal_error(err));
763 int BIO_dgram_non_fatal_error(int err)
767 #if defined(OPENSSL_SYS_WINDOWS)
768 # if defined(WSAEWOULDBLOCK)
772 # if 0 /* This appears to always be an error */
773 # if defined(WSAENOTCONN)
780 # ifdef WSAEWOULDBLOCK
781 # if WSAEWOULDBLOCK != EWOULDBLOCK
794 #if EWOULDBLOCK != EAGAIN
819 static void get_current_time(struct timeval *t)
821 #ifdef OPENSSL_SYS_WIN32
824 t->tv_sec = (long)tb.time;
825 t->tv_usec = (long)tb.millitm * 1000;
826 #elif defined(OPENSSL_SYS_VMS)
829 t->tv_sec = (long)tb.time;
830 t->tv_usec = (long)tb.millitm * 1000;
832 gettimeofday(t, NULL);