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
68 #include <openssl/bio.h>
70 #ifdef OPENSSL_SYS_WIN32
71 #include <sys/timeb.h>
74 #define IP_MTU 14 /* linux is lame */
77 #define sock_write SockWrite /* Watt-32 uses same names */
78 #define sock_read SockRead
79 #define sock_puts SockPuts
82 static int dgram_write(BIO *h, const char *buf, int num);
83 static int dgram_read(BIO *h, char *buf, int size);
84 static int dgram_puts(BIO *h, const char *str);
85 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
86 static int dgram_new(BIO *h);
87 static int dgram_free(BIO *data);
88 static int dgram_clear(BIO *bio);
90 static int BIO_dgram_should_retry(int s);
92 static BIO_METHOD methods_dgramp=
99 NULL, /* dgram_gets, */
106 typedef struct bio_dgram_data_st
108 struct sockaddr peer;
109 unsigned int connected;
112 struct timeval hstimeoutdiff;
113 struct timeval hstimeout;
116 BIO_METHOD *BIO_s_datagram(void)
118 return(&methods_dgramp);
121 BIO *BIO_new_dgram(int fd, int close_flag)
125 ret=BIO_new(BIO_s_datagram());
126 if (ret == NULL) return(NULL);
127 BIO_set_fd(ret,fd,close_flag);
131 static int dgram_new(BIO *bi)
133 bio_dgram_data *data = NULL;
137 data = OPENSSL_malloc(sizeof(bio_dgram_data));
140 memset(data, 0x00, sizeof(bio_dgram_data));
147 static int dgram_free(BIO *a)
149 bio_dgram_data *data;
151 if (a == NULL) return(0);
152 if ( ! dgram_clear(a))
155 data = (bio_dgram_data *)a->ptr;
156 if(data != NULL) OPENSSL_free(data);
161 static int dgram_clear(BIO *a)
163 if (a == NULL) return(0);
176 static int dgram_read(BIO *b, char *out, int outl)
179 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
181 struct sockaddr peer;
182 int peerlen = sizeof(peer);
186 clear_socket_error();
187 memset(&peer, 0x00, peerlen);
188 /* Last arg in recvfrom is signed on some platforms and
189 * unsigned on others. It is of type socklen_t on some
190 * but this is not universal. Cast to (void *) to avoid
193 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
195 if ( ! data->connected && ret > 0)
196 BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
198 BIO_clear_retry_flags(b);
201 if (BIO_dgram_should_retry(ret))
203 BIO_set_retry_read(b);
204 data->_errno = get_last_socket_error();
206 memset(&(data->hstimeout), 0, sizeof(struct timeval));
210 if (data->hstimeout.tv_sec > 0 || data->hstimeout.tv_usec > 0)
212 struct timeval curtime;
213 #ifdef OPENSSL_SYS_WIN32
216 curtime.tv_sec = (long)tb.time;
217 curtime.tv_usec = (long)tb.millitm * 1000;
219 gettimeofday(&curtime, NULL);
222 if (curtime.tv_sec >= data->hstimeout.tv_sec &&
223 curtime.tv_usec >= data->hstimeout.tv_usec)
225 data->_errno = EAGAIN;
227 memset(&(data->hstimeout), 0, sizeof(struct timeval));
235 static int dgram_write(BIO *b, const char *in, int inl)
238 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
239 clear_socket_error();
241 if ( data->connected )
242 ret=writesocket(b->num,in,inl);
244 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
245 ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
247 ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
250 BIO_clear_retry_flags(b);
253 if (BIO_sock_should_retry(ret))
255 BIO_set_retry_write(b);
256 data->_errno = get_last_socket_error();
258 #if 0 /* higher layers are responsible for querying MTU, if necessary */
259 if ( data->_errno == EMSGSIZE)
260 /* retrieve the new MTU */
261 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
268 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
272 struct sockaddr *to = NULL;
273 bio_dgram_data *data = NULL;
274 long sockopt_val = 0;
275 unsigned int sockopt_len = 0;
277 data = (bio_dgram_data *)b->ptr;
283 case BIO_C_FILE_SEEK:
286 case BIO_C_FILE_TELL:
292 b->num= *((int *)ptr);
293 b->shutdown=(int)num;
300 if (ip != NULL) *ip=b->num;
306 case BIO_CTRL_GET_CLOSE:
309 case BIO_CTRL_SET_CLOSE:
310 b->shutdown=(int)num;
312 case BIO_CTRL_PENDING:
313 case BIO_CTRL_WPENDING:
320 case BIO_CTRL_DGRAM_CONNECT:
321 to = (struct sockaddr *)ptr;
323 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
324 { perror("connect"); ret = 0; }
328 memcpy(&(data->peer),to, sizeof(struct sockaddr));
333 /* (Linux)kernel sets DF bit on outgoing IP packets */
334 #ifdef IP_MTU_DISCOVER
335 case BIO_CTRL_DGRAM_MTU_DISCOVER:
336 sockopt_val = IP_PMTUDISC_DO;
337 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
338 &sockopt_val, sizeof(sockopt_val))) < 0)
339 perror("setsockopt");
342 case BIO_CTRL_DGRAM_QUERY_MTU:
343 sockopt_len = sizeof(sockopt_val);
344 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
345 &sockopt_len)) < 0 || sockopt_val < 0)
349 data->mtu = sockopt_val;
353 case BIO_CTRL_DGRAM_GET_MTU:
356 case BIO_CTRL_DGRAM_SET_MTU:
360 case BIO_CTRL_DGRAM_SET_CONNECTED:
361 to = (struct sockaddr *)ptr;
366 memcpy(&(data->peer),to, sizeof(struct sockaddr));
371 memset(&(data->peer), 0x00, sizeof(struct sockaddr));
374 case BIO_CTRL_DGRAM_SET_PEER:
375 to = (struct sockaddr *) ptr;
377 memcpy(&(data->peer), to, sizeof(struct sockaddr));
379 case BIO_CTRL_DGRAM_SET_TIMEOUT:
382 #ifdef OPENSSL_SYS_WIN32
385 data->hstimeout.tv_sec = (long)tb.time;
386 data->hstimeout.tv_usec = (long)tb.millitm * 1000;
388 gettimeofday(&(data->hstimeout), NULL);
390 data->hstimeout.tv_sec += data->hstimeoutdiff.tv_sec;
391 data->hstimeout.tv_usec += data->hstimeoutdiff.tv_usec;
392 if (data->hstimeout.tv_usec >= 1000000)
394 data->hstimeout.tv_sec++;
395 data->hstimeout.tv_usec -= 1000000;
400 memset(&(data->hstimeout), 0, sizeof(struct timeval));
403 #if defined(SO_RCVTIMEO)
404 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
405 #ifdef OPENSSL_SYS_WINDOWS
407 struct timeval *tv = (struct timeval *)ptr;
408 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
409 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
410 (void*)&timeout, sizeof(timeout)) < 0)
411 { perror("setsockopt"); ret = -1; }
414 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
415 sizeof(struct timeval)) < 0)
416 { perror("setsockopt"); ret = -1; }
418 memcpy(&(data->hstimeoutdiff), ptr, sizeof(struct timeval));
420 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
421 #ifdef OPENSSL_SYS_WINDOWS
423 int timeout, sz = sizeof(timeout);
424 struct timeval *tv = (struct timeval *)ptr;
425 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
426 (void*)&timeout, &sz) < 0)
427 { perror("getsockopt"); ret = -1; }
430 tv->tv_sec = timeout / 1000;
431 tv->tv_usec = (timeout % 1000) * 1000;
436 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
437 ptr, (void *)&ret) < 0)
438 { perror("getsockopt"); ret = -1; }
442 #if defined(SO_SNDTIMEO)
443 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
444 #ifdef OPENSSL_SYS_WINDOWS
446 struct timeval *tv = (struct timeval *)ptr;
447 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
448 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
449 (void*)&timeout, sizeof(timeout)) < 0)
450 { perror("setsockopt"); ret = -1; }
453 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
454 sizeof(struct timeval)) < 0)
455 { perror("setsockopt"); ret = -1; }
458 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
459 #ifdef OPENSSL_SYS_WINDOWS
461 int timeout, sz = sizeof(timeout);
462 struct timeval *tv = (struct timeval *)ptr;
463 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
464 (void*)&timeout, &sz) < 0)
465 { perror("getsockopt"); ret = -1; }
468 tv->tv_sec = timeout / 1000;
469 tv->tv_usec = (timeout % 1000) * 1000;
474 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
475 ptr, (void *)&ret) < 0)
476 { perror("getsockopt"); ret = -1; }
480 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
482 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
483 #ifdef OPENSSL_SYS_WINDOWS
484 if ( data->_errno == WSAETIMEDOUT)
486 if ( data->_errno == EAGAIN)
496 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
497 if ( data->_errno == EMSGSIZE)
513 static int dgram_puts(BIO *bp, const char *str)
518 ret=dgram_write(bp,str,n);
522 static int BIO_dgram_should_retry(int i)
526 if ((i == 0) || (i == -1))
528 err=get_last_socket_error();
530 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
531 if ((i == -1) && (err == 0))
535 return(BIO_dgram_non_fatal_error(err));
540 int BIO_dgram_non_fatal_error(int err)
544 #if defined(OPENSSL_SYS_WINDOWS)
545 # if defined(WSAEWOULDBLOCK)
549 # if 0 /* This appears to always be an error */
550 # if defined(WSAENOTCONN)
557 # ifdef WSAEWOULDBLOCK
558 # if WSAEWOULDBLOCK != EWOULDBLOCK
566 #if defined(ENOTCONN)
575 #if EWOULDBLOCK != EAGAIN
592 /* DF bit set, and packet larger than MTU */