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).
60 #ifndef OPENSSL_NO_DGRAM
67 #include <openssl/bio.h>
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
112 struct sockaddr_storage peer;
114 struct sockaddr_in peer;
116 unsigned int connected;
119 struct timeval next_timeout;
120 struct timeval socket_timeout;
123 BIO_METHOD *BIO_s_datagram(void)
125 return(&methods_dgramp);
128 BIO *BIO_new_dgram(int fd, int close_flag)
132 ret=BIO_new(BIO_s_datagram());
133 if (ret == NULL) return(NULL);
134 BIO_set_fd(ret,fd,close_flag);
138 static int dgram_new(BIO *bi)
140 bio_dgram_data *data = NULL;
144 data = OPENSSL_malloc(sizeof(bio_dgram_data));
147 memset(data, 0x00, sizeof(bio_dgram_data));
154 static int dgram_free(BIO *a)
156 bio_dgram_data *data;
158 if (a == NULL) return(0);
159 if ( ! dgram_clear(a))
162 data = (bio_dgram_data *)a->ptr;
163 if(data != NULL) OPENSSL_free(data);
168 static int dgram_clear(BIO *a)
170 if (a == NULL) return(0);
183 static void dgram_adjust_rcv_timeout(BIO *b)
185 #if defined(SO_RCVTIMEO)
186 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
187 int sz = sizeof(int);
189 /* Is a timer active? */
190 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
192 struct timeval timenow, timeleft;
194 /* Read current socket timeout */
195 #ifdef OPENSSL_SYS_WINDOWS
197 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
198 (void*)&timeout, &sz) < 0)
199 { perror("getsockopt"); }
202 data->socket_timeout.tv_sec = timeout / 1000;
203 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
206 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
207 &(data->socket_timeout), (void *)&sz) < 0)
208 { perror("getsockopt"); }
211 /* Get current time */
212 get_current_time(&timenow);
214 /* Calculate time left until timer expires */
215 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
216 timeleft.tv_sec -= timenow.tv_sec;
217 timeleft.tv_usec -= timenow.tv_usec;
218 if (timeleft.tv_usec < 0)
221 timeleft.tv_usec += 1000000;
224 if (timeleft.tv_sec < 0)
227 timeleft.tv_usec = 1;
230 /* Adjust socket timeout if next handhake message timer
231 * will expire earlier.
233 if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
234 (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
235 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
236 data->socket_timeout.tv_usec >= timeleft.tv_usec))
238 #ifdef OPENSSL_SYS_WINDOWS
239 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
240 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
241 (void*)&timeout, sizeof(timeout)) < 0)
242 { perror("setsockopt"); }
244 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
245 sizeof(struct timeval)) < 0)
246 { perror("setsockopt"); }
253 static void dgram_reset_rcv_timeout(BIO *b)
255 #if defined(SO_RCVTIMEO)
256 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
258 /* Is a timer active? */
259 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
261 #ifdef OPENSSL_SYS_WINDOWS
262 int timeout = data->socket_timeout.tv_sec * 1000 +
263 data->socket_timeout.tv_usec / 1000;
264 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
265 (void*)&timeout, sizeof(timeout)) < 0)
266 { perror("setsockopt"); }
268 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
269 sizeof(struct timeval)) < 0)
270 { perror("setsockopt"); }
276 static int dgram_read(BIO *b, char *out, int outl)
279 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
282 struct sockaddr_storage peer;
284 struct sockaddr_in peer;
286 int peerlen = sizeof(peer);
290 clear_socket_error();
291 memset(&peer, 0x00, peerlen);
292 /* Last arg in recvfrom is signed on some platforms and
293 * unsigned on others. It is of type socklen_t on some
294 * but this is not universal. Cast to (void *) to avoid
297 dgram_adjust_rcv_timeout(b);
298 ret=recvfrom(b->num,out,outl,0,(struct sockaddr *)&peer,(void *)&peerlen);
299 dgram_reset_rcv_timeout(b);
301 if ( ! data->connected && ret >= 0)
302 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
304 BIO_clear_retry_flags(b);
307 if (BIO_dgram_should_retry(ret))
309 BIO_set_retry_read(b);
310 data->_errno = get_last_socket_error();
317 static int dgram_write(BIO *b, const char *in, int inl)
320 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
321 clear_socket_error();
323 if ( data->connected )
324 ret=writesocket(b->num,in,inl);
327 if (data->peer.ss_family == AF_INET)
328 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
329 ret=sendto(b->num, (char *)in, inl, 0, (const struct sockaddr *)&data->peer, sizeof(struct sockaddr_in));
331 ret=sendto(b->num, in, inl, 0, (const struct sockaddr *)&data->peer, sizeof(struct sockaddr_in));
334 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
335 ret=sendto(b->num, (char *)in, inl, 0, (const struct sockaddr *)&data->peer, sizeof(struct sockaddr_in6));
337 ret=sendto(b->num, in, inl, 0, (const struct sockaddr *)&data->peer, sizeof(struct sockaddr_in6));
340 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
341 ret=sendto(b->num, (char *)in, inl, 0, (const struct sockaddr *)&data->peer, sizeof(struct sockaddr_in));
343 ret=sendto(b->num, in, inl, 0, (const struct sockaddr *)&data->peer, sizeof(struct sockaddr_in));
347 BIO_clear_retry_flags(b);
350 if (BIO_dgram_should_retry(ret))
352 BIO_set_retry_write(b);
353 data->_errno = get_last_socket_error();
355 #if 0 /* higher layers are responsible for querying MTU, if necessary */
356 if ( data->_errno == EMSGSIZE)
357 /* retrieve the new MTU */
358 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
365 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
369 struct sockaddr *to = NULL;
370 bio_dgram_data *data = NULL;
371 #if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
372 long sockopt_val = 0;
373 unsigned int sockopt_len = 0;
375 #ifdef OPENSSL_SYS_LINUX
377 struct sockaddr_storage addr;
380 data = (bio_dgram_data *)b->ptr;
386 case BIO_C_FILE_SEEK:
389 case BIO_C_FILE_TELL:
395 b->num= *((int *)ptr);
396 b->shutdown=(int)num;
403 if (ip != NULL) *ip=b->num;
409 case BIO_CTRL_GET_CLOSE:
412 case BIO_CTRL_SET_CLOSE:
413 b->shutdown=(int)num;
415 case BIO_CTRL_PENDING:
416 case BIO_CTRL_WPENDING:
423 case BIO_CTRL_DGRAM_CONNECT:
424 to = (struct sockaddr *)ptr;
426 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
427 { perror("connect"); ret = 0; }
432 memcpy(&(data->peer),to, sizeof(struct sockaddr_storage));
434 memcpy(&(data->peer),to, sizeof(struct sockaddr_in));
440 /* (Linux)kernel sets DF bit on outgoing IP packets */
441 case BIO_CTRL_DGRAM_MTU_DISCOVER:
442 #ifdef OPENSSL_SYS_LINUX
443 addr_len = (socklen_t)sizeof(struct sockaddr_storage);
444 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
445 if (getsockname(b->num, (void *)&addr, &addr_len) < 0)
450 sockopt_len = sizeof(sockopt_val);
451 switch (addr.ss_family)
454 sockopt_val = IP_PMTUDISC_DO;
455 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
456 &sockopt_val, sizeof(sockopt_val))) < 0)
457 perror("setsockopt");
461 sockopt_val = IPV6_PMTUDISC_DO;
462 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
463 &sockopt_val, sizeof(sockopt_val))) < 0)
464 perror("setsockopt");
475 case BIO_CTRL_DGRAM_QUERY_MTU:
476 #ifdef OPENSSL_SYS_LINUX
477 addr_len = (socklen_t)sizeof(struct sockaddr_storage);
478 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
479 if (getsockname(b->num, (void *)&addr, &addr_len) < 0)
484 sockopt_len = sizeof(sockopt_val);
485 switch (addr.ss_family)
488 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
489 &sockopt_len)) < 0 || sockopt_val < 0)
495 /* we assume that the transport protocol is UDP and no
496 * IP options are used.
498 data->mtu = sockopt_val - 8 - 20;
504 if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
505 &sockopt_len)) < 0 || sockopt_val < 0)
511 /* we assume that the transport protocol is UDP and no
512 * IPV6 options are used.
514 data->mtu = sockopt_val - 8 - 40;
527 case BIO_CTRL_DGRAM_GET_MTU:
530 case BIO_CTRL_DGRAM_SET_MTU:
534 case BIO_CTRL_DGRAM_SET_CONNECTED:
535 to = (struct sockaddr *)ptr;
541 memcpy(&(data->peer),to, sizeof(struct sockaddr_storage));
543 memcpy(&(data->peer),to, sizeof(struct sockaddr_in));
550 memset(&(data->peer), 0x00, sizeof(struct sockaddr_storage));
552 memset(&(data->peer), 0x00, sizeof(struct sockaddr_in));
556 case BIO_CTRL_DGRAM_GET_PEER:
557 to = (struct sockaddr *) ptr;
560 memcpy(to, &(data->peer), sizeof(struct sockaddr_storage));
561 ret = sizeof(struct sockaddr_storage);
563 memcpy(to, &(data->peer), sizeof(struct sockaddr_in));
564 ret = sizeof(struct sockaddr_in);
567 case BIO_CTRL_DGRAM_SET_PEER:
568 to = (struct sockaddr *) ptr;
571 memcpy(&(data->peer), to, sizeof(struct sockaddr_storage));
573 memcpy(&(data->peer), to, sizeof(struct sockaddr_in));
576 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
577 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
579 #if defined(SO_RCVTIMEO)
580 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
581 #ifdef OPENSSL_SYS_WINDOWS
583 struct timeval *tv = (struct timeval *)ptr;
584 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
585 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
586 (void*)&timeout, sizeof(timeout)) < 0)
587 { perror("setsockopt"); ret = -1; }
590 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
591 sizeof(struct timeval)) < 0)
592 { perror("setsockopt"); ret = -1; }
595 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
596 #ifdef OPENSSL_SYS_WINDOWS
598 int timeout, sz = sizeof(timeout);
599 struct timeval *tv = (struct timeval *)ptr;
600 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
601 (void*)&timeout, &sz) < 0)
602 { perror("getsockopt"); ret = -1; }
605 tv->tv_sec = timeout / 1000;
606 tv->tv_usec = (timeout % 1000) * 1000;
611 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
612 ptr, (void *)&ret) < 0)
613 { perror("getsockopt"); ret = -1; }
617 #if defined(SO_SNDTIMEO)
618 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
619 #ifdef OPENSSL_SYS_WINDOWS
621 struct timeval *tv = (struct timeval *)ptr;
622 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
623 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
624 (void*)&timeout, sizeof(timeout)) < 0)
625 { perror("setsockopt"); ret = -1; }
628 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
629 sizeof(struct timeval)) < 0)
630 { perror("setsockopt"); ret = -1; }
633 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
634 #ifdef OPENSSL_SYS_WINDOWS
636 int timeout, sz = sizeof(timeout);
637 struct timeval *tv = (struct timeval *)ptr;
638 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
639 (void*)&timeout, &sz) < 0)
640 { perror("getsockopt"); ret = -1; }
643 tv->tv_sec = timeout / 1000;
644 tv->tv_usec = (timeout % 1000) * 1000;
649 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
650 ptr, (void *)&ret) < 0)
651 { perror("getsockopt"); ret = -1; }
655 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
657 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
658 #ifdef OPENSSL_SYS_WINDOWS
659 if ( data->_errno == WSAETIMEDOUT)
661 if ( data->_errno == EAGAIN)
671 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
672 if ( data->_errno == EMSGSIZE)
688 static int dgram_puts(BIO *bp, const char *str)
693 ret=dgram_write(bp,str,n);
697 static int BIO_dgram_should_retry(int i)
701 if ((i == 0) || (i == -1))
703 err=get_last_socket_error();
705 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
706 if ((i == -1) && (err == 0))
710 return(BIO_dgram_non_fatal_error(err));
715 int BIO_dgram_non_fatal_error(int err)
719 #if defined(OPENSSL_SYS_WINDOWS)
720 # if defined(WSAEWOULDBLOCK)
724 # if 0 /* This appears to always be an error */
725 # if defined(WSAENOTCONN)
732 # ifdef WSAEWOULDBLOCK
733 # if WSAEWOULDBLOCK != EWOULDBLOCK
746 #if EWOULDBLOCK != EAGAIN
772 static void get_current_time(struct timeval *t)
774 #ifdef OPENSSL_SYS_WIN32
777 t->tv_sec = (long)tb.time;
778 t->tv_usec = (long)tb.millitm * 1000;
779 #elif defined(OPENSSL_SYS_VMS)
782 t->tv_sec = (long)tb.time;
783 t->tv_usec = (long)tb.millitm * 1000;
785 gettimeofday(t, NULL);