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 #define IP_MTU 14 /* linux is lame */
72 #define sock_write SockWrite /* Watt-32 uses same names */
73 #define sock_read SockRead
74 #define sock_puts SockPuts
77 static int dgram_write(BIO *h, const char *buf, int num);
78 static int dgram_read(BIO *h, char *buf, int size);
79 static int dgram_puts(BIO *h, const char *str);
80 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
81 static int dgram_new(BIO *h);
82 static int dgram_free(BIO *data);
83 static int dgram_clear(BIO *bio);
85 static int BIO_dgram_should_retry(int s);
87 static BIO_METHOD methods_dgramp=
94 NULL, /* dgram_gets, */
101 typedef struct bio_dgram_data_st
103 struct sockaddr peer;
104 unsigned int connected;
107 struct timeval hstimeoutdiff;
108 struct timeval hstimeout;
111 BIO_METHOD *BIO_s_datagram(void)
113 return(&methods_dgramp);
116 BIO *BIO_new_dgram(int fd, int close_flag)
120 ret=BIO_new(BIO_s_datagram());
121 if (ret == NULL) return(NULL);
122 BIO_set_fd(ret,fd,close_flag);
126 static int dgram_new(BIO *bi)
128 bio_dgram_data *data = NULL;
132 data = OPENSSL_malloc(sizeof(bio_dgram_data));
135 memset(data, 0x00, sizeof(bio_dgram_data));
142 static int dgram_free(BIO *a)
144 bio_dgram_data *data;
146 if (a == NULL) return(0);
147 if ( ! dgram_clear(a))
150 data = (bio_dgram_data *)a->ptr;
151 if(data != NULL) OPENSSL_free(data);
156 static int dgram_clear(BIO *a)
158 if (a == NULL) return(0);
171 static int dgram_read(BIO *b, char *out, int outl)
174 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
176 struct sockaddr peer;
177 int peerlen = sizeof(peer);
181 clear_socket_error();
182 memset(&peer, 0x00, peerlen);
183 /* Last arg in recvfrom is signed on some platforms and
184 * unsigned on others. It is of type socklen_t on some
185 * but this is not universal. Cast to (void *) to avoid
188 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
190 if ( ! data->connected && ret > 0)
191 BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
193 BIO_clear_retry_flags(b);
196 if (BIO_dgram_should_retry(ret))
198 BIO_set_retry_read(b);
199 data->_errno = get_last_socket_error();
201 memset(&(data->hstimeout), 0, sizeof(struct timeval));
205 if (data->hstimeout.tv_sec > 0 || data->hstimeout.tv_usec > 0)
207 struct timeval curtime;
208 gettimeofday(&curtime, NULL);
209 if (curtime.tv_sec >= data->hstimeout.tv_sec &&
210 curtime.tv_usec >= data->hstimeout.tv_usec)
212 data->_errno = EAGAIN;
214 memset(&(data->hstimeout), 0, sizeof(struct timeval));
222 static int dgram_write(BIO *b, const char *in, int inl)
225 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
226 clear_socket_error();
228 if ( data->connected )
229 ret=writesocket(b->num,in,inl);
231 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
232 ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
234 ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
237 BIO_clear_retry_flags(b);
240 if (BIO_sock_should_retry(ret))
242 BIO_set_retry_write(b);
243 data->_errno = get_last_socket_error();
245 #if 0 /* higher layers are responsible for querying MTU, if necessary */
246 if ( data->_errno == EMSGSIZE)
247 /* retrieve the new MTU */
248 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
255 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
259 struct sockaddr *to = NULL;
260 bio_dgram_data *data = NULL;
261 long sockopt_val = 0;
262 unsigned int sockopt_len = 0;
264 data = (bio_dgram_data *)b->ptr;
270 case BIO_C_FILE_SEEK:
273 case BIO_C_FILE_TELL:
279 b->num= *((int *)ptr);
280 b->shutdown=(int)num;
287 if (ip != NULL) *ip=b->num;
293 case BIO_CTRL_GET_CLOSE:
296 case BIO_CTRL_SET_CLOSE:
297 b->shutdown=(int)num;
299 case BIO_CTRL_PENDING:
300 case BIO_CTRL_WPENDING:
307 case BIO_CTRL_DGRAM_CONNECT:
308 to = (struct sockaddr *)ptr;
310 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
311 { perror("connect"); ret = 0; }
315 memcpy(&(data->peer),to, sizeof(struct sockaddr));
320 /* (Linux)kernel sets DF bit on outgoing IP packets */
321 #ifdef IP_MTU_DISCOVER
322 case BIO_CTRL_DGRAM_MTU_DISCOVER:
323 sockopt_val = IP_PMTUDISC_DO;
324 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
325 &sockopt_val, sizeof(sockopt_val))) < 0)
326 perror("setsockopt");
329 case BIO_CTRL_DGRAM_QUERY_MTU:
330 sockopt_len = sizeof(sockopt_val);
331 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
332 &sockopt_len)) < 0 || sockopt_val < 0)
336 data->mtu = sockopt_val;
340 case BIO_CTRL_DGRAM_GET_MTU:
343 case BIO_CTRL_DGRAM_SET_MTU:
347 case BIO_CTRL_DGRAM_SET_CONNECTED:
348 to = (struct sockaddr *)ptr;
353 memcpy(&(data->peer),to, sizeof(struct sockaddr));
358 memset(&(data->peer), 0x00, sizeof(struct sockaddr));
361 case BIO_CTRL_DGRAM_SET_PEER:
362 to = (struct sockaddr *) ptr;
364 memcpy(&(data->peer), to, sizeof(struct sockaddr));
366 case BIO_CTRL_DGRAM_SET_TIMEOUT:
369 gettimeofday(&(data->hstimeout), NULL);
370 data->hstimeout.tv_sec += data->hstimeoutdiff.tv_sec;
371 data->hstimeout.tv_usec += data->hstimeoutdiff.tv_usec;
372 if (data->hstimeout.tv_usec >= 1000000)
374 data->hstimeout.tv_sec++;
375 data->hstimeout.tv_usec -= 1000000;
380 memset(&(data->hstimeout), 0, sizeof(struct timeval));
383 #if defined(SO_RCVTIMEO)
384 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
385 #ifdef OPENSSL_SYS_WINDOWS
387 struct timeval *tv = (struct timeval *)ptr;
388 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
389 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
390 (void*)&timeout, sizeof(timeout)) < 0)
391 { perror("setsockopt"); ret = -1; }
394 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
395 sizeof(struct timeval)) < 0)
396 { perror("setsockopt"); ret = -1; }
398 memcpy(&(data->hstimeoutdiff), ptr, sizeof(struct timeval));
400 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
401 #ifdef OPENSSL_SYS_WINDOWS
403 int timeout, sz = sizeof(timeout);
404 struct timeval *tv = (struct timeval *)ptr;
405 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
406 (void*)&timeout, &sz) < 0)
407 { perror("getsockopt"); ret = -1; }
410 tv->tv_sec = timeout / 1000;
411 tv->tv_usec = (timeout % 1000) * 1000;
416 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
417 ptr, (void *)&ret) < 0)
418 { perror("getsockopt"); ret = -1; }
422 #if defined(SO_SNDTIMEO)
423 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
424 #ifdef OPENSSL_SYS_WINDOWS
426 struct timeval *tv = (struct timeval *)ptr;
427 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
428 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
429 (void*)&timeout, sizeof(timeout)) < 0)
430 { perror("setsockopt"); ret = -1; }
433 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
434 sizeof(struct timeval)) < 0)
435 { perror("setsockopt"); ret = -1; }
438 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
439 #ifdef OPENSSL_SYS_WINDOWS
441 int timeout, sz = sizeof(timeout);
442 struct timeval *tv = (struct timeval *)ptr;
443 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
444 (void*)&timeout, &sz) < 0)
445 { perror("getsockopt"); ret = -1; }
448 tv->tv_sec = timeout / 1000;
449 tv->tv_usec = (timeout % 1000) * 1000;
454 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
455 ptr, (void *)&ret) < 0)
456 { perror("getsockopt"); ret = -1; }
460 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
462 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
463 #ifdef OPENSSL_SYS_WINDOWS
464 if ( data->_errno == WSAETIMEDOUT)
466 if ( data->_errno == EAGAIN)
476 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
477 if ( data->_errno == EMSGSIZE)
493 static int dgram_puts(BIO *bp, const char *str)
498 ret=dgram_write(bp,str,n);
502 static int BIO_dgram_should_retry(int i)
506 if ((i == 0) || (i == -1))
508 err=get_last_socket_error();
510 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
511 if ((i == -1) && (err == 0))
515 return(BIO_dgram_non_fatal_error(err));
520 int BIO_dgram_non_fatal_error(int err)
524 #if defined(OPENSSL_SYS_WINDOWS)
525 # if defined(WSAEWOULDBLOCK)
529 # if 0 /* This appears to always be an error */
530 # if defined(WSAENOTCONN)
537 # ifdef WSAEWOULDBLOCK
538 # if WSAEWOULDBLOCK != EWOULDBLOCK
546 #if defined(ENOTCONN)
555 #if EWOULDBLOCK != EAGAIN
572 /* DF bit set, and packet larger than MTU */