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).
65 #include <openssl/bio.h>
66 #ifndef OPENSSL_NO_DGRAM
68 # if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
69 # include <sys/timeb.h>
72 # ifdef OPENSSL_SYS_LINUX
73 # 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 void get_current_time(struct timeval *t);
94 static BIO_METHOD methods_dgramp = {
100 NULL, /* dgram_gets, */
107 typedef struct bio_dgram_data_st {
108 struct sockaddr peer;
109 unsigned int connected;
112 struct timeval next_timeout;
113 struct timeval socket_timeout;
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());
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;
157 data = (bio_dgram_data *)a->ptr;
164 static int dgram_clear(BIO *a)
178 static void dgram_adjust_rcv_timeout(BIO *b)
180 # if defined(SO_RCVTIMEO)
181 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
182 int sz = sizeof(int);
184 /* Is a timer active? */
185 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");
195 data->socket_timeout.tv_sec = timeout / 1000;
196 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
199 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
200 &(data->socket_timeout), (void *)&sz) < 0) {
201 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) {
214 timeleft.tv_usec += 1000000;
217 if (timeleft.tv_sec < 0) {
219 timeleft.tv_usec = 1;
223 * Adjust socket timeout if next handhake message timer will expire
226 if ((data->socket_timeout.tv_sec == 0
227 && data->socket_timeout.tv_usec == 0)
228 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
229 || (data->socket_timeout.tv_sec == timeleft.tv_sec
230 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
231 # ifdef OPENSSL_SYS_WINDOWS
232 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
233 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
234 (void *)&timeout, sizeof(timeout)) < 0) {
235 perror("setsockopt");
238 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
239 sizeof(struct timeval)) < 0) {
240 perror("setsockopt");
248 static void dgram_reset_rcv_timeout(BIO *b)
250 # if defined(SO_RCVTIMEO)
251 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
253 /* Is a timer active? */
254 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
255 # ifdef OPENSSL_SYS_WINDOWS
256 int timeout = data->socket_timeout.tv_sec * 1000 +
257 data->socket_timeout.tv_usec / 1000;
258 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
259 (void *)&timeout, sizeof(timeout)) < 0) {
260 perror("setsockopt");
264 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
265 sizeof(struct timeval)) < 0) {
266 perror("setsockopt");
273 static int dgram_read(BIO *b, char *out, int outl)
276 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
278 struct sockaddr peer;
279 int peerlen = sizeof(peer);
282 clear_socket_error();
283 memset(&peer, 0x00, peerlen);
285 * Last arg in recvfrom is signed on some platforms and unsigned on
286 * others. It is of type socklen_t on some but this is not universal.
287 * Cast to (void *) to avoid compiler warnings.
289 dgram_adjust_rcv_timeout(b);
290 ret = recvfrom(b->num, out, outl, 0, &peer, (void *)&peerlen);
292 if (!data->connected && ret >= 0)
293 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
295 BIO_clear_retry_flags(b);
297 if (BIO_dgram_should_retry(ret)) {
298 BIO_set_retry_read(b);
299 data->_errno = get_last_socket_error();
303 dgram_reset_rcv_timeout(b);
308 static int dgram_write(BIO *b, const char *in, int inl)
311 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
312 clear_socket_error();
315 ret = writesocket(b->num, in, inl);
317 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
319 sendto(b->num, (char *)in, inl, 0, &data->peer,
322 ret = sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
325 BIO_clear_retry_flags(b);
327 if (BIO_dgram_should_retry(ret)) {
328 BIO_set_retry_write(b);
329 data->_errno = get_last_socket_error();
331 # if 0 /* higher layers are responsible for querying
332 * MTU, if necessary */
333 if (data->_errno == EMSGSIZE)
334 /* retrieve the new MTU */
335 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
342 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
346 struct sockaddr *to = NULL;
347 bio_dgram_data *data = NULL;
348 # if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
349 long sockopt_val = 0;
350 unsigned int sockopt_len = 0;
352 # ifdef OPENSSL_SYS_LINUX
354 struct sockaddr_storage addr;
357 data = (bio_dgram_data *)b->ptr;
362 case BIO_C_FILE_SEEK:
365 case BIO_C_FILE_TELL:
371 b->num = *((int *)ptr);
372 b->shutdown = (int)num;
384 case BIO_CTRL_GET_CLOSE:
387 case BIO_CTRL_SET_CLOSE:
388 b->shutdown = (int)num;
390 case BIO_CTRL_PENDING:
391 case BIO_CTRL_WPENDING:
398 case BIO_CTRL_DGRAM_CONNECT:
399 to = (struct sockaddr *)ptr;
401 if (connect(b->num, to, sizeof(struct sockaddr)) < 0) {
406 memcpy(&(data->peer), to, sizeof(struct sockaddr));
411 /* (Linux)kernel sets DF bit on outgoing IP packets */
412 case BIO_CTRL_DGRAM_MTU_DISCOVER:
413 # ifdef OPENSSL_SYS_LINUX
414 addr_len = (socklen_t) sizeof(struct sockaddr_storage);
415 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
416 if (getsockname(b->num, (void *)&addr, &addr_len) < 0) {
420 sockopt_len = sizeof(sockopt_val);
421 switch (addr.ss_family) {
423 sockopt_val = IP_PMTUDISC_DO;
424 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
425 &sockopt_val, sizeof(sockopt_val))) < 0)
426 perror("setsockopt");
429 sockopt_val = IPV6_PMTUDISC_DO;
430 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
431 &sockopt_val, sizeof(sockopt_val))) < 0)
432 perror("setsockopt");
442 case BIO_CTRL_DGRAM_QUERY_MTU:
443 # ifdef OPENSSL_SYS_LINUX
444 addr_len = (socklen_t) sizeof(struct sockaddr_storage);
445 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
446 if (getsockname(b->num, (void *)&addr, &addr_len) < 0) {
450 sockopt_len = sizeof(sockopt_val);
451 switch (addr.ss_family) {
454 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
455 &sockopt_len)) < 0 || sockopt_val < 0) {
459 * we assume that the transport protocol is UDP and no IP
462 data->mtu = sockopt_val - 8 - 20;
468 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
469 (void *)&sockopt_val, &sockopt_len)) < 0
470 || sockopt_val < 0) {
474 * we assume that the transport protocol is UDP and no IPV6
477 data->mtu = sockopt_val - 8 - 40;
489 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
492 case BIO_CTRL_DGRAM_GET_MTU:
495 case BIO_CTRL_DGRAM_SET_MTU:
499 case BIO_CTRL_DGRAM_SET_CONNECTED:
500 to = (struct sockaddr *)ptr;
504 memcpy(&(data->peer), to, sizeof(struct sockaddr));
507 memset(&(data->peer), 0x00, sizeof(struct sockaddr));
510 case BIO_CTRL_DGRAM_GET_PEER:
511 to = (struct sockaddr *)ptr;
513 memcpy(to, &(data->peer), sizeof(struct sockaddr));
514 ret = sizeof(struct sockaddr);
516 case BIO_CTRL_DGRAM_SET_PEER:
517 to = (struct sockaddr *)ptr;
519 memcpy(&(data->peer), to, sizeof(struct sockaddr));
521 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
522 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
524 # if defined(SO_RCVTIMEO)
525 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
526 # ifdef OPENSSL_SYS_WINDOWS
528 struct timeval *tv = (struct timeval *)ptr;
529 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
530 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
531 (void *)&timeout, sizeof(timeout)) < 0) {
532 perror("setsockopt");
537 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
538 sizeof(struct timeval)) < 0) {
539 perror("setsockopt");
544 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
545 # ifdef OPENSSL_SYS_WINDOWS
547 int timeout, sz = sizeof(timeout);
548 struct timeval *tv = (struct timeval *)ptr;
549 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
550 (void *)&timeout, &sz) < 0) {
551 perror("getsockopt");
554 tv->tv_sec = timeout / 1000;
555 tv->tv_usec = (timeout % 1000) * 1000;
560 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
561 ptr, (void *)&ret) < 0) {
562 perror("getsockopt");
568 # if defined(SO_SNDTIMEO)
569 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
570 # ifdef OPENSSL_SYS_WINDOWS
572 struct timeval *tv = (struct timeval *)ptr;
573 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
574 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
575 (void *)&timeout, sizeof(timeout)) < 0) {
576 perror("setsockopt");
581 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
582 sizeof(struct timeval)) < 0) {
583 perror("setsockopt");
588 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
589 # ifdef OPENSSL_SYS_WINDOWS
591 int timeout, sz = sizeof(timeout);
592 struct timeval *tv = (struct timeval *)ptr;
593 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
594 (void *)&timeout, &sz) < 0) {
595 perror("getsockopt");
598 tv->tv_sec = timeout / 1000;
599 tv->tv_usec = (timeout % 1000) * 1000;
604 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
605 ptr, (void *)&ret) < 0) {
606 perror("getsockopt");
612 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
614 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
615 # ifdef OPENSSL_SYS_WINDOWS
616 if (data->_errno == WSAETIMEDOUT)
618 if (data->_errno == EAGAIN)
627 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
628 if (data->_errno == EMSGSIZE) {
642 static int dgram_puts(BIO *bp, const char *str)
647 ret = dgram_write(bp, str, n);
651 static int BIO_dgram_should_retry(int i)
655 if ((i == 0) || (i == -1)) {
656 err = get_last_socket_error();
658 # if defined(OPENSSL_SYS_WINDOWS)
660 * If the socket return value (i) is -1 and err is unexpectedly 0 at
661 * this point, the error code was overwritten by another system call
662 * before this error handling is called.
666 return (BIO_dgram_non_fatal_error(err));
671 int BIO_dgram_non_fatal_error(int err)
674 # if defined(OPENSSL_SYS_WINDOWS)
675 # if defined(WSAEWOULDBLOCK)
679 # if 0 /* This appears to always be an error */
680 # if defined(WSAENOTCONN)
687 # ifdef WSAEWOULDBLOCK
688 # if WSAEWOULDBLOCK != EWOULDBLOCK
701 # if EWOULDBLOCK != EAGAIN
726 static void get_current_time(struct timeval *t)
728 # ifdef OPENSSL_SYS_WIN32
731 t->tv_sec = (long)tb.time;
732 t->tv_usec = (long)tb.millitm * 1000;
733 # elif defined(OPENSSL_SYS_VMS)
736 t->tv_sec = (long)tb.time;
737 t->tv_usec = (long)tb.millitm * 1000;
739 gettimeofday(t, NULL);