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 #ifdef OPENSSL_SYS_WIN32
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 BIO_METHOD methods_dgramp=
98 NULL, /* dgram_gets, */
105 typedef struct bio_dgram_data_st
107 struct sockaddr peer;
108 unsigned int connected;
111 struct timeval hstimeoutdiff;
112 struct timeval hstimeout;
115 BIO_METHOD *BIO_s_datagram(void)
117 return(&methods_dgramp);
120 BIO *BIO_new_dgram(int fd, int close_flag)
124 ret=BIO_new(BIO_s_datagram());
125 if (ret == NULL) return(NULL);
126 BIO_set_fd(ret,fd,close_flag);
130 static int dgram_new(BIO *bi)
132 bio_dgram_data *data = NULL;
136 data = OPENSSL_malloc(sizeof(bio_dgram_data));
139 memset(data, 0x00, sizeof(bio_dgram_data));
146 static int dgram_free(BIO *a)
148 bio_dgram_data *data;
150 if (a == NULL) return(0);
151 if ( ! dgram_clear(a))
154 data = (bio_dgram_data *)a->ptr;
155 if(data != NULL) OPENSSL_free(data);
160 static int dgram_clear(BIO *a)
162 if (a == NULL) return(0);
175 static int dgram_read(BIO *b, char *out, int outl)
178 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
180 struct sockaddr peer;
181 int peerlen = sizeof(peer);
185 clear_socket_error();
186 memset(&peer, 0x00, peerlen);
187 /* Last arg in recvfrom is signed on some platforms and
188 * unsigned on others. It is of type socklen_t on some
189 * but this is not universal. Cast to (void *) to avoid
192 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
194 if ( ! data->connected && ret > 0)
195 BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
197 BIO_clear_retry_flags(b);
200 if (BIO_dgram_should_retry(ret))
202 BIO_set_retry_read(b);
203 data->_errno = get_last_socket_error();
205 memset(&(data->hstimeout), 0, sizeof(struct timeval));
209 if (data->hstimeout.tv_sec > 0 || data->hstimeout.tv_usec > 0)
211 struct timeval curtime;
212 #ifdef OPENSSL_SYS_WIN32
215 curtime.tv_sec = (long)tb.time;
216 curtime.tv_usec = (long)tb.millitm * 1000;
218 gettimeofday(&curtime, NULL);
221 if (curtime.tv_sec >= data->hstimeout.tv_sec &&
222 curtime.tv_usec >= data->hstimeout.tv_usec)
224 data->_errno = EAGAIN;
226 memset(&(data->hstimeout), 0, sizeof(struct timeval));
234 static int dgram_write(BIO *b, const char *in, int inl)
237 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
238 clear_socket_error();
240 if ( data->connected )
241 ret=writesocket(b->num,in,inl);
243 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
244 ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
246 ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
249 BIO_clear_retry_flags(b);
252 if (BIO_sock_should_retry(ret))
254 BIO_set_retry_write(b);
255 data->_errno = get_last_socket_error();
257 #if 0 /* higher layers are responsible for querying MTU, if necessary */
258 if ( data->_errno == EMSGSIZE)
259 /* retrieve the new MTU */
260 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
267 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
271 struct sockaddr *to = NULL;
272 bio_dgram_data *data = NULL;
273 long sockopt_val = 0;
274 unsigned int sockopt_len = 0;
276 data = (bio_dgram_data *)b->ptr;
282 case BIO_C_FILE_SEEK:
285 case BIO_C_FILE_TELL:
291 b->num= *((int *)ptr);
292 b->shutdown=(int)num;
299 if (ip != NULL) *ip=b->num;
305 case BIO_CTRL_GET_CLOSE:
308 case BIO_CTRL_SET_CLOSE:
309 b->shutdown=(int)num;
311 case BIO_CTRL_PENDING:
312 case BIO_CTRL_WPENDING:
319 case BIO_CTRL_DGRAM_CONNECT:
320 to = (struct sockaddr *)ptr;
322 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
323 { perror("connect"); ret = 0; }
327 memcpy(&(data->peer),to, sizeof(struct sockaddr));
332 /* (Linux)kernel sets DF bit on outgoing IP packets */
333 #ifdef IP_MTU_DISCOVER
334 case BIO_CTRL_DGRAM_MTU_DISCOVER:
335 sockopt_val = IP_PMTUDISC_DO;
336 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
337 &sockopt_val, sizeof(sockopt_val))) < 0)
338 perror("setsockopt");
341 case BIO_CTRL_DGRAM_QUERY_MTU:
342 sockopt_len = sizeof(sockopt_val);
343 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
344 &sockopt_len)) < 0 || sockopt_val < 0)
348 data->mtu = sockopt_val;
352 case BIO_CTRL_DGRAM_GET_MTU:
355 case BIO_CTRL_DGRAM_SET_MTU:
359 case BIO_CTRL_DGRAM_SET_CONNECTED:
360 to = (struct sockaddr *)ptr;
365 memcpy(&(data->peer),to, sizeof(struct sockaddr));
370 memset(&(data->peer), 0x00, sizeof(struct sockaddr));
373 case BIO_CTRL_DGRAM_SET_PEER:
374 to = (struct sockaddr *) ptr;
376 memcpy(&(data->peer), to, sizeof(struct sockaddr));
378 case BIO_CTRL_DGRAM_SET_TIMEOUT:
381 #ifdef OPENSSL_SYS_WIN32
384 data->hstimeout.tv_sec = (long)tb.time;
385 data->hstimeout.tv_usec = (long)tb.millitm * 1000;
387 gettimeofday(&(data->hstimeout), NULL);
389 data->hstimeout.tv_sec += data->hstimeoutdiff.tv_sec;
390 data->hstimeout.tv_usec += data->hstimeoutdiff.tv_usec;
391 if (data->hstimeout.tv_usec >= 1000000)
393 data->hstimeout.tv_sec++;
394 data->hstimeout.tv_usec -= 1000000;
399 memset(&(data->hstimeout), 0, sizeof(struct timeval));
402 #if defined(SO_RCVTIMEO)
403 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
404 #ifdef OPENSSL_SYS_WINDOWS
406 struct timeval *tv = (struct timeval *)ptr;
407 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
408 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
409 (void*)&timeout, sizeof(timeout)) < 0)
410 { perror("setsockopt"); ret = -1; }
413 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
414 sizeof(struct timeval)) < 0)
415 { perror("setsockopt"); ret = -1; }
417 memcpy(&(data->hstimeoutdiff), ptr, sizeof(struct timeval));
419 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
420 #ifdef OPENSSL_SYS_WINDOWS
422 int timeout, sz = sizeof(timeout);
423 struct timeval *tv = (struct timeval *)ptr;
424 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
425 (void*)&timeout, &sz) < 0)
426 { perror("getsockopt"); ret = -1; }
429 tv->tv_sec = timeout / 1000;
430 tv->tv_usec = (timeout % 1000) * 1000;
435 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
436 ptr, (void *)&ret) < 0)
437 { perror("getsockopt"); ret = -1; }
441 #if defined(SO_SNDTIMEO)
442 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
443 #ifdef OPENSSL_SYS_WINDOWS
445 struct timeval *tv = (struct timeval *)ptr;
446 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
447 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
448 (void*)&timeout, sizeof(timeout)) < 0)
449 { perror("setsockopt"); ret = -1; }
452 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
453 sizeof(struct timeval)) < 0)
454 { perror("setsockopt"); ret = -1; }
457 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
458 #ifdef OPENSSL_SYS_WINDOWS
460 int timeout, sz = sizeof(timeout);
461 struct timeval *tv = (struct timeval *)ptr;
462 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
463 (void*)&timeout, &sz) < 0)
464 { perror("getsockopt"); ret = -1; }
467 tv->tv_sec = timeout / 1000;
468 tv->tv_usec = (timeout % 1000) * 1000;
473 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
474 ptr, (void *)&ret) < 0)
475 { perror("getsockopt"); ret = -1; }
479 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
481 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
482 #ifdef OPENSSL_SYS_WINDOWS
483 if ( data->_errno == WSAETIMEDOUT)
485 if ( data->_errno == EAGAIN)
495 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
496 if ( data->_errno == EMSGSIZE)
512 static int dgram_puts(BIO *bp, const char *str)
517 ret=dgram_write(bp,str,n);
521 static int BIO_dgram_should_retry(int i)
525 if ((i == 0) || (i == -1))
527 err=get_last_socket_error();
529 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
530 if ((i == -1) && (err == 0))
534 return(BIO_dgram_non_fatal_error(err));
539 int BIO_dgram_non_fatal_error(int err)
543 #if defined(OPENSSL_SYS_WINDOWS)
544 # if defined(WSAEWOULDBLOCK)
548 # if 0 /* This appears to always be an error */
549 # if defined(WSAENOTCONN)
556 # ifdef WSAEWOULDBLOCK
557 # if WSAEWOULDBLOCK != EWOULDBLOCK
565 #if defined(ENOTCONN)
574 #if EWOULDBLOCK != EAGAIN
591 /* DF bit set, and packet larger than MTU */