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 #define IP_MTU 14 /* linux is lame */
76 #define sock_write SockWrite /* Watt-32 uses same names */
77 #define sock_read SockRead
78 #define sock_puts SockPuts
81 static int dgram_write(BIO *h, const char *buf, int num);
82 static int dgram_read(BIO *h, char *buf, int size);
83 static int dgram_puts(BIO *h, const char *str);
84 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
85 static int dgram_new(BIO *h);
86 static int dgram_free(BIO *data);
87 static int dgram_clear(BIO *bio);
89 static int BIO_dgram_should_retry(int s);
91 static void get_current_time(struct timeval *t);
93 static BIO_METHOD methods_dgramp=
100 NULL, /* dgram_gets, */
107 typedef struct bio_dgram_data_st
109 struct sockaddr peer;
110 unsigned int connected;
113 struct timeval next_timeout;
114 struct timeval socket_timeout;
117 BIO_METHOD *BIO_s_datagram(void)
119 return(&methods_dgramp);
122 BIO *BIO_new_dgram(int fd, int close_flag)
126 ret=BIO_new(BIO_s_datagram());
127 if (ret == NULL) return(NULL);
128 BIO_set_fd(ret,fd,close_flag);
132 static int dgram_new(BIO *bi)
134 bio_dgram_data *data = NULL;
138 data = OPENSSL_malloc(sizeof(bio_dgram_data));
141 memset(data, 0x00, sizeof(bio_dgram_data));
148 static int dgram_free(BIO *a)
150 bio_dgram_data *data;
152 if (a == NULL) return(0);
153 if ( ! dgram_clear(a))
156 data = (bio_dgram_data *)a->ptr;
157 if(data != NULL) OPENSSL_free(data);
162 static int dgram_clear(BIO *a)
164 if (a == NULL) return(0);
177 static void dgram_adjust_rcv_timeout(BIO *b)
179 #if defined(SO_RCVTIMEO)
180 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
181 int sz = sizeof(int);
183 /* Is a timer active? */
184 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
186 struct timeval timenow, timeleft;
188 /* Read current socket timeout */
189 #ifdef OPENSSL_SYS_WINDOWS
191 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
192 (void*)&timeout, &sz) < 0)
193 { perror("getsockopt"); }
196 data->socket_timeout.tv_sec = timeout / 1000;
197 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
200 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
201 &(data->socket_timeout), (void *)&sz) < 0)
202 { perror("getsockopt"); }
205 /* Get current time */
206 get_current_time(&timenow);
208 /* Calculate time left until timer expires */
209 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
210 timeleft.tv_sec -= timenow.tv_sec;
211 timeleft.tv_usec -= timenow.tv_usec;
212 if (timeleft.tv_usec < 0)
215 timeleft.tv_usec += 1000000;
218 /* Adjust socket timeout if next handhake message timer
219 * will expire earlier.
221 if (data->socket_timeout.tv_sec < timeleft.tv_sec ||
222 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
223 data->socket_timeout.tv_usec <= timeleft.tv_usec))
225 #ifdef OPENSSL_SYS_WINDOWS
226 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
227 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
228 (void*)&timeout, sizeof(timeout)) < 0)
229 { perror("setsockopt"); }
231 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
232 sizeof(struct timeval)) < 0)
233 { perror("setsockopt"); }
240 static void dgram_reset_rcv_timeout(BIO *b)
242 #if defined(SO_RCVTIMEO)
243 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
244 #ifdef OPENSSL_SYS_WINDOWS
245 int timeout = data->socket_timeout.tv_sec * 1000 +
246 data->socket_timeout.tv_usec / 1000;
247 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
248 (void*)&timeout, sizeof(timeout)) < 0)
249 { perror("setsockopt"); }
251 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
252 sizeof(struct timeval)) < 0)
253 { perror("setsockopt"); }
258 static int dgram_read(BIO *b, char *out, int outl)
261 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
263 struct sockaddr peer;
264 int peerlen = sizeof(peer);
268 clear_socket_error();
269 memset(&peer, 0x00, peerlen);
270 /* Last arg in recvfrom is signed on some platforms and
271 * unsigned on others. It is of type socklen_t on some
272 * but this is not universal. Cast to (void *) to avoid
275 dgram_adjust_rcv_timeout(b);
276 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
277 dgram_reset_rcv_timeout(b);
279 if ( ! data->connected && ret > 0)
280 BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
282 BIO_clear_retry_flags(b);
285 if (BIO_dgram_should_retry(ret))
287 BIO_set_retry_read(b);
288 data->_errno = get_last_socket_error();
291 memset(&(data->hstimeout), 0, sizeof(struct timeval));
298 static int dgram_write(BIO *b, const char *in, int inl)
301 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
302 clear_socket_error();
304 if ( data->connected )
305 ret=writesocket(b->num,in,inl);
307 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
308 ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
310 ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
313 BIO_clear_retry_flags(b);
316 if (BIO_sock_should_retry(ret))
318 BIO_set_retry_write(b);
319 data->_errno = get_last_socket_error();
321 #if 0 /* higher layers are responsible for querying MTU, if necessary */
322 if ( data->_errno == EMSGSIZE)
323 /* retrieve the new MTU */
324 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
331 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
335 struct sockaddr *to = NULL;
336 bio_dgram_data *data = NULL;
337 long sockopt_val = 0;
338 unsigned int sockopt_len = 0;
340 data = (bio_dgram_data *)b->ptr;
346 case BIO_C_FILE_SEEK:
349 case BIO_C_FILE_TELL:
355 b->num= *((int *)ptr);
356 b->shutdown=(int)num;
363 if (ip != NULL) *ip=b->num;
369 case BIO_CTRL_GET_CLOSE:
372 case BIO_CTRL_SET_CLOSE:
373 b->shutdown=(int)num;
375 case BIO_CTRL_PENDING:
376 case BIO_CTRL_WPENDING:
383 case BIO_CTRL_DGRAM_CONNECT:
384 to = (struct sockaddr *)ptr;
386 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
387 { perror("connect"); ret = 0; }
391 memcpy(&(data->peer),to, sizeof(struct sockaddr));
396 /* (Linux)kernel sets DF bit on outgoing IP packets */
397 #ifdef IP_MTU_DISCOVER
398 case BIO_CTRL_DGRAM_MTU_DISCOVER:
399 sockopt_val = IP_PMTUDISC_DO;
400 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
401 &sockopt_val, sizeof(sockopt_val))) < 0)
402 perror("setsockopt");
405 case BIO_CTRL_DGRAM_QUERY_MTU:
406 sockopt_len = sizeof(sockopt_val);
407 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
408 &sockopt_len)) < 0 || sockopt_val < 0)
412 data->mtu = sockopt_val;
416 case BIO_CTRL_DGRAM_GET_MTU:
419 case BIO_CTRL_DGRAM_SET_MTU:
423 case BIO_CTRL_DGRAM_SET_CONNECTED:
424 to = (struct sockaddr *)ptr;
429 memcpy(&(data->peer),to, sizeof(struct sockaddr));
434 memset(&(data->peer), 0x00, sizeof(struct sockaddr));
437 case BIO_CTRL_DGRAM_SET_PEER:
438 to = (struct sockaddr *) ptr;
440 memcpy(&(data->peer), to, sizeof(struct sockaddr));
442 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
443 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
445 #if defined(SO_RCVTIMEO)
446 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
447 #ifdef OPENSSL_SYS_WINDOWS
449 struct timeval *tv = (struct timeval *)ptr;
450 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
451 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
452 (void*)&timeout, sizeof(timeout)) < 0)
453 { perror("setsockopt"); ret = -1; }
456 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
457 sizeof(struct timeval)) < 0)
458 { perror("setsockopt"); ret = -1; }
461 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
462 #ifdef OPENSSL_SYS_WINDOWS
464 int timeout, sz = sizeof(timeout);
465 struct timeval *tv = (struct timeval *)ptr;
466 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
467 (void*)&timeout, &sz) < 0)
468 { perror("getsockopt"); ret = -1; }
471 tv->tv_sec = timeout / 1000;
472 tv->tv_usec = (timeout % 1000) * 1000;
477 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
478 ptr, (void *)&ret) < 0)
479 { perror("getsockopt"); ret = -1; }
483 #if defined(SO_SNDTIMEO)
484 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
485 #ifdef OPENSSL_SYS_WINDOWS
487 struct timeval *tv = (struct timeval *)ptr;
488 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
489 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
490 (void*)&timeout, sizeof(timeout)) < 0)
491 { perror("setsockopt"); ret = -1; }
494 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
495 sizeof(struct timeval)) < 0)
496 { perror("setsockopt"); ret = -1; }
499 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
500 #ifdef OPENSSL_SYS_WINDOWS
502 int timeout, sz = sizeof(timeout);
503 struct timeval *tv = (struct timeval *)ptr;
504 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
505 (void*)&timeout, &sz) < 0)
506 { perror("getsockopt"); ret = -1; }
509 tv->tv_sec = timeout / 1000;
510 tv->tv_usec = (timeout % 1000) * 1000;
515 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
516 ptr, (void *)&ret) < 0)
517 { perror("getsockopt"); ret = -1; }
521 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
523 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
524 #ifdef OPENSSL_SYS_WINDOWS
525 if ( data->_errno == WSAETIMEDOUT)
527 if ( data->_errno == EAGAIN)
537 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
538 if ( data->_errno == EMSGSIZE)
554 static int dgram_puts(BIO *bp, const char *str)
559 ret=dgram_write(bp,str,n);
563 static int BIO_dgram_should_retry(int i)
567 if ((i == 0) || (i == -1))
569 err=get_last_socket_error();
571 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
572 if ((i == -1) && (err == 0))
576 return(BIO_dgram_non_fatal_error(err));
581 int BIO_dgram_non_fatal_error(int err)
585 #if defined(OPENSSL_SYS_WINDOWS)
586 # if defined(WSAEWOULDBLOCK)
590 # if 0 /* This appears to always be an error */
591 # if defined(WSAENOTCONN)
598 # ifdef WSAEWOULDBLOCK
599 # if WSAEWOULDBLOCK != EWOULDBLOCK
607 #if defined(ENOTCONN)
616 #if EWOULDBLOCK != EAGAIN
633 /* DF bit set, and packet larger than MTU */
647 static void get_current_time(struct timeval *t)
649 #ifdef OPENSSL_SYS_WIN32
652 t->tv_sec = (long)tb.time;
653 t->tv_usec = (long)tb.millitm * 1000;
654 #elif defined(OPENSSL_SYS_VMS)
657 t->tv_sec = (long)tb.time;
658 t->tv_usec = (long)tb.millitm * 1000;
660 gettimeofday(t, NULL);