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
111 struct sockaddr peer;
112 unsigned int connected;
115 struct timeval next_timeout;
116 struct timeval socket_timeout;
119 BIO_METHOD *BIO_s_datagram(void)
121 return(&methods_dgramp);
124 BIO *BIO_new_dgram(int fd, int close_flag)
128 ret=BIO_new(BIO_s_datagram());
129 if (ret == NULL) return(NULL);
130 BIO_set_fd(ret,fd,close_flag);
134 static int dgram_new(BIO *bi)
136 bio_dgram_data *data = NULL;
140 data = OPENSSL_malloc(sizeof(bio_dgram_data));
143 memset(data, 0x00, sizeof(bio_dgram_data));
150 static int dgram_free(BIO *a)
152 bio_dgram_data *data;
154 if (a == NULL) return(0);
155 if ( ! dgram_clear(a))
158 data = (bio_dgram_data *)a->ptr;
159 if(data != NULL) OPENSSL_free(data);
164 static int dgram_clear(BIO *a)
166 if (a == NULL) return(0);
179 static void dgram_adjust_rcv_timeout(BIO *b)
181 #if defined(SO_RCVTIMEO)
182 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
183 int sz = sizeof(int);
185 /* Is a timer active? */
186 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
188 struct timeval timenow, timeleft;
190 /* Read current socket timeout */
191 #ifdef OPENSSL_SYS_WINDOWS
193 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
194 (void*)&timeout, &sz) < 0)
195 { perror("getsockopt"); }
198 data->socket_timeout.tv_sec = timeout / 1000;
199 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
202 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
203 &(data->socket_timeout), (void *)&sz) < 0)
204 { perror("getsockopt"); }
207 /* Get current time */
208 get_current_time(&timenow);
210 /* Calculate time left until timer expires */
211 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
212 timeleft.tv_sec -= timenow.tv_sec;
213 timeleft.tv_usec -= timenow.tv_usec;
214 if (timeleft.tv_usec < 0)
217 timeleft.tv_usec += 1000000;
220 /* Adjust socket timeout if next handhake message timer
221 * will expire earlier.
223 if (data->socket_timeout.tv_sec < timeleft.tv_sec ||
224 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
225 data->socket_timeout.tv_usec <= timeleft.tv_usec))
227 #ifdef OPENSSL_SYS_WINDOWS
228 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
229 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
230 (void*)&timeout, sizeof(timeout)) < 0)
231 { perror("setsockopt"); }
233 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
234 sizeof(struct timeval)) < 0)
235 { perror("setsockopt"); }
242 static void dgram_reset_rcv_timeout(BIO *b)
244 #if defined(SO_RCVTIMEO)
245 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
246 #ifdef OPENSSL_SYS_WINDOWS
247 int timeout = data->socket_timeout.tv_sec * 1000 +
248 data->socket_timeout.tv_usec / 1000;
249 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
250 (void*)&timeout, sizeof(timeout)) < 0)
251 { perror("setsockopt"); }
253 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
254 sizeof(struct timeval)) < 0)
255 { perror("setsockopt"); }
260 static int dgram_read(BIO *b, char *out, int outl)
263 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
265 struct sockaddr peer;
266 int peerlen = sizeof(peer);
270 clear_socket_error();
271 memset(&peer, 0x00, peerlen);
272 /* Last arg in recvfrom is signed on some platforms and
273 * unsigned on others. It is of type socklen_t on some
274 * but this is not universal. Cast to (void *) to avoid
277 dgram_adjust_rcv_timeout(b);
278 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
279 dgram_reset_rcv_timeout(b);
281 if ( ! data->connected && ret > 0)
282 BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
284 BIO_clear_retry_flags(b);
287 if (BIO_dgram_should_retry(ret))
289 BIO_set_retry_read(b);
290 data->_errno = get_last_socket_error();
297 static int dgram_write(BIO *b, const char *in, int inl)
300 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
301 clear_socket_error();
303 if ( data->connected )
304 ret=writesocket(b->num,in,inl);
306 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
307 ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
309 ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
312 BIO_clear_retry_flags(b);
315 if (BIO_sock_should_retry(ret))
317 BIO_set_retry_write(b);
318 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);
330 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
334 struct sockaddr *to = NULL;
335 bio_dgram_data *data = NULL;
336 long sockopt_val = 0;
337 unsigned int sockopt_len = 0;
338 #ifdef OPENSSL_SYS_LINUX
340 struct sockaddr_storage addr;
343 data = (bio_dgram_data *)b->ptr;
349 case BIO_C_FILE_SEEK:
352 case BIO_C_FILE_TELL:
358 b->num= *((int *)ptr);
359 b->shutdown=(int)num;
366 if (ip != NULL) *ip=b->num;
372 case BIO_CTRL_GET_CLOSE:
375 case BIO_CTRL_SET_CLOSE:
376 b->shutdown=(int)num;
378 case BIO_CTRL_PENDING:
379 case BIO_CTRL_WPENDING:
386 case BIO_CTRL_DGRAM_CONNECT:
387 to = (struct sockaddr *)ptr;
389 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
390 { perror("connect"); ret = 0; }
394 memcpy(&(data->peer),to, sizeof(struct sockaddr));
399 /* (Linux)kernel sets DF bit on outgoing IP packets */
400 case BIO_CTRL_DGRAM_MTU_DISCOVER:
401 #ifdef OPENSSL_SYS_LINUX
402 addr_len = (socklen_t)sizeof(struct sockaddr_storage);
403 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
404 if (getsockname(b->num, (void *)&addr, &addr_len) < 0)
409 sockopt_len = sizeof(sockopt_val);
410 switch (addr.ss_family)
413 sockopt_val = IP_PMTUDISC_DO;
414 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
415 &sockopt_val, sizeof(sockopt_val))) < 0)
416 perror("setsockopt");
419 sockopt_val = IPV6_PMTUDISC_DO;
420 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
421 &sockopt_val, sizeof(sockopt_val))) < 0)
422 perror("setsockopt");
432 case BIO_CTRL_DGRAM_QUERY_MTU:
433 #ifdef OPENSSL_SYS_LINUX
434 addr_len = (socklen_t)sizeof(struct sockaddr_storage);
435 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
436 if (getsockname(b->num, (void *)&addr, &addr_len) < 0)
441 sockopt_len = sizeof(sockopt_val);
442 switch (addr.ss_family)
445 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
446 &sockopt_len)) < 0 || sockopt_val < 0)
452 /* we assume that the transport protocol is UDP and no
453 * IP options are used.
455 data->mtu = sockopt_val - 8 - 20;
460 if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
461 &sockopt_len)) < 0 || sockopt_val < 0)
467 /* we assume that the transport protocol is UDP and no
468 * IPV6 options are used.
470 data->mtu = sockopt_val - 8 - 40;
482 case BIO_CTRL_DGRAM_GET_MTU:
485 case BIO_CTRL_DGRAM_SET_MTU:
489 case BIO_CTRL_DGRAM_SET_CONNECTED:
490 to = (struct sockaddr *)ptr;
495 memcpy(&(data->peer),to, sizeof(struct sockaddr));
500 memset(&(data->peer), 0x00, sizeof(struct sockaddr));
503 case BIO_CTRL_DGRAM_SET_PEER:
504 to = (struct sockaddr *) ptr;
506 memcpy(&(data->peer), to, sizeof(struct sockaddr));
508 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
509 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
511 #if defined(SO_RCVTIMEO)
512 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
513 #ifdef OPENSSL_SYS_WINDOWS
515 struct timeval *tv = (struct timeval *)ptr;
516 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
517 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
518 (void*)&timeout, sizeof(timeout)) < 0)
519 { perror("setsockopt"); ret = -1; }
522 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
523 sizeof(struct timeval)) < 0)
524 { perror("setsockopt"); ret = -1; }
527 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
528 #ifdef OPENSSL_SYS_WINDOWS
530 int timeout, sz = sizeof(timeout);
531 struct timeval *tv = (struct timeval *)ptr;
532 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
533 (void*)&timeout, &sz) < 0)
534 { perror("getsockopt"); ret = -1; }
537 tv->tv_sec = timeout / 1000;
538 tv->tv_usec = (timeout % 1000) * 1000;
543 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
544 ptr, (void *)&ret) < 0)
545 { perror("getsockopt"); ret = -1; }
549 #if defined(SO_SNDTIMEO)
550 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
551 #ifdef OPENSSL_SYS_WINDOWS
553 struct timeval *tv = (struct timeval *)ptr;
554 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
555 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
556 (void*)&timeout, sizeof(timeout)) < 0)
557 { perror("setsockopt"); ret = -1; }
560 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
561 sizeof(struct timeval)) < 0)
562 { perror("setsockopt"); ret = -1; }
565 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
566 #ifdef OPENSSL_SYS_WINDOWS
568 int timeout, sz = sizeof(timeout);
569 struct timeval *tv = (struct timeval *)ptr;
570 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
571 (void*)&timeout, &sz) < 0)
572 { perror("getsockopt"); ret = -1; }
575 tv->tv_sec = timeout / 1000;
576 tv->tv_usec = (timeout % 1000) * 1000;
581 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
582 ptr, (void *)&ret) < 0)
583 { perror("getsockopt"); ret = -1; }
587 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
589 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
590 #ifdef OPENSSL_SYS_WINDOWS
591 if ( data->_errno == WSAETIMEDOUT)
593 if ( data->_errno == EAGAIN)
603 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
604 if ( data->_errno == EMSGSIZE)
620 static int dgram_puts(BIO *bp, const char *str)
625 ret=dgram_write(bp,str,n);
629 static int BIO_dgram_should_retry(int i)
633 if ((i == 0) || (i == -1))
635 err=get_last_socket_error();
637 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
638 if ((i == -1) && (err == 0))
642 return(BIO_dgram_non_fatal_error(err));
647 int BIO_dgram_non_fatal_error(int err)
651 #if defined(OPENSSL_SYS_WINDOWS)
652 # if defined(WSAEWOULDBLOCK)
656 # if 0 /* This appears to always be an error */
657 # if defined(WSAENOTCONN)
664 # ifdef WSAEWOULDBLOCK
665 # if WSAEWOULDBLOCK != EWOULDBLOCK
673 #if defined(ENOTCONN)
682 #if EWOULDBLOCK != EAGAIN
699 /* DF bit set, and packet larger than MTU */
713 static void get_current_time(struct timeval *t)
715 #ifdef OPENSSL_SYS_WIN32
718 t->tv_sec = (long)tb.time;
719 t->tv_usec = (long)tb.millitm * 1000;
720 #elif defined(OPENSSL_SYS_VMS)
723 t->tv_sec = (long)tb.time;
724 t->tv_usec = (long)tb.millitm * 1000;
726 gettimeofday(t, NULL);