Updates from 1.0.0-stable.
[openssl.git] / crypto / bio / bss_dgram.c
1 /* crypto/bio/bio_dgram.c */
2 /* 
3  * DTLS implementation written by Nagendra Modadugu
4  * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.  
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2005 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer. 
15  *
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
19  *    distribution.
20  *
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/)"
25  *
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.
30  *
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.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
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  * ====================================================================
53  *
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).
57  *
58  */
59
60 #ifndef OPENSSL_NO_DGRAM
61
62 #include <stdio.h>
63 #include <errno.h>
64 #define USE_SOCKETS
65 #include "cryptlib.h"
66
67 #include <openssl/bio.h>
68
69 #ifdef OPENSSL_SYS_WIN32
70 #include <sys/timeb.h>
71 #endif
72
73 #define IP_MTU      14 /* linux is lame */
74
75 #ifdef WATT32
76 #define sock_write SockWrite  /* Watt-32 uses same names */
77 #define sock_read  SockRead
78 #define sock_puts  SockPuts
79 #endif
80
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);
88
89 static int BIO_dgram_should_retry(int s);
90
91 static BIO_METHOD methods_dgramp=
92         {
93         BIO_TYPE_DGRAM,
94         "datagram socket",
95         dgram_write,
96         dgram_read,
97         dgram_puts,
98         NULL, /* dgram_gets, */
99         dgram_ctrl,
100         dgram_new,
101         dgram_free,
102         NULL,
103         };
104
105 typedef struct bio_dgram_data_st
106         {
107         struct sockaddr peer;
108         unsigned int connected;
109         unsigned int _errno;
110         unsigned int mtu;
111         struct timeval hstimeoutdiff;
112         struct timeval hstimeout;
113         } bio_dgram_data;
114
115 BIO_METHOD *BIO_s_datagram(void)
116         {
117         return(&methods_dgramp);
118         }
119
120 BIO *BIO_new_dgram(int fd, int close_flag)
121         {
122         BIO *ret;
123
124         ret=BIO_new(BIO_s_datagram());
125         if (ret == NULL) return(NULL);
126         BIO_set_fd(ret,fd,close_flag);
127         return(ret);
128         }
129
130 static int dgram_new(BIO *bi)
131         {
132         bio_dgram_data *data = NULL;
133
134         bi->init=0;
135         bi->num=0;
136         data = OPENSSL_malloc(sizeof(bio_dgram_data));
137         if (data == NULL)
138                 return 0;
139         memset(data, 0x00, sizeof(bio_dgram_data));
140     bi->ptr = data;
141
142         bi->flags=0;
143         return(1);
144         }
145
146 static int dgram_free(BIO *a)
147         {
148         bio_dgram_data *data;
149
150         if (a == NULL) return(0);
151         if ( ! dgram_clear(a))
152                 return 0;
153
154         data = (bio_dgram_data *)a->ptr;
155         if(data != NULL) OPENSSL_free(data);
156
157         return(1);
158         }
159
160 static int dgram_clear(BIO *a)
161         {
162         if (a == NULL) return(0);
163         if (a->shutdown)
164                 {
165                 if (a->init)
166                         {
167                         SHUTDOWN2(a->num);
168                         }
169                 a->init=0;
170                 a->flags=0;
171                 }
172         return(1);
173         }
174         
175 static int dgram_read(BIO *b, char *out, int outl)
176         {
177         int ret=0;
178         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
179
180         struct sockaddr peer;
181         int peerlen = sizeof(peer);
182
183         if (out != NULL)
184                 {
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
190                  * compiler warnings.
191                  */
192                 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
193
194                 if ( ! data->connected  && ret > 0)
195                         BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
196
197                 BIO_clear_retry_flags(b);
198                 if (ret <= 0)
199                         {
200                         if (BIO_dgram_should_retry(ret))
201                                 {
202                                 BIO_set_retry_read(b);
203                                 data->_errno = get_last_socket_error();
204                                 }
205                         memset(&(data->hstimeout), 0, sizeof(struct timeval));
206                         }
207                 else
208                         {
209                         if (data->hstimeout.tv_sec > 0 || data->hstimeout.tv_usec > 0)
210                                 {
211                                 struct timeval curtime;
212 #ifdef OPENSSL_SYS_WIN32
213                                 struct _timeb tb;
214                                 _ftime(&tb);
215                                 curtime.tv_sec = (long)tb.time;
216                                 curtime.tv_usec = (long)tb.millitm * 1000;
217 #else
218                                 gettimeofday(&curtime, NULL);
219 #endif
220
221                                 if (curtime.tv_sec >= data->hstimeout.tv_sec &&
222                                         curtime.tv_usec >= data->hstimeout.tv_usec)
223                                         {
224                                         data->_errno = EAGAIN;
225                                         ret = -1;
226                                         memset(&(data->hstimeout), 0, sizeof(struct timeval));
227                                         }
228                                 }
229                         }
230                 }
231         return(ret);
232         }
233
234 static int dgram_write(BIO *b, const char *in, int inl)
235         {
236         int ret;
237         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
238         clear_socket_error();
239
240     if ( data->connected )
241         ret=writesocket(b->num,in,inl);
242     else
243 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
244         ret=sendto(b->num, (char *)in, inl, 0, &data->peer, sizeof(data->peer));
245 #else
246         ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
247 #endif
248
249         BIO_clear_retry_flags(b);
250         if (ret <= 0)
251                 {
252                 if (BIO_sock_should_retry(ret))
253                         {
254                         BIO_set_retry_write(b);  
255                         data->_errno = get_last_socket_error();
256
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);
261 #endif
262                         }
263                 }
264         return(ret);
265         }
266
267 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
268         {
269         long ret=1;
270         int *ip;
271         struct sockaddr *to = NULL;
272         bio_dgram_data *data = NULL;
273         long sockopt_val = 0;
274         unsigned int sockopt_len = 0;
275
276         data = (bio_dgram_data *)b->ptr;
277
278         switch (cmd)
279                 {
280         case BIO_CTRL_RESET:
281                 num=0;
282         case BIO_C_FILE_SEEK:
283                 ret=0;
284                 break;
285         case BIO_C_FILE_TELL:
286         case BIO_CTRL_INFO:
287                 ret=0;
288                 break;
289         case BIO_C_SET_FD:
290                 dgram_clear(b);
291                 b->num= *((int *)ptr);
292                 b->shutdown=(int)num;
293                 b->init=1;
294                 break;
295         case BIO_C_GET_FD:
296                 if (b->init)
297                         {
298                         ip=(int *)ptr;
299                         if (ip != NULL) *ip=b->num;
300                         ret=b->num;
301                         }
302                 else
303                         ret= -1;
304                 break;
305         case BIO_CTRL_GET_CLOSE:
306                 ret=b->shutdown;
307                 break;
308         case BIO_CTRL_SET_CLOSE:
309                 b->shutdown=(int)num;
310                 break;
311         case BIO_CTRL_PENDING:
312         case BIO_CTRL_WPENDING:
313                 ret=0;
314                 break;
315         case BIO_CTRL_DUP:
316         case BIO_CTRL_FLUSH:
317                 ret=1;
318                 break;
319         case BIO_CTRL_DGRAM_CONNECT:
320                 to = (struct sockaddr *)ptr;
321 #if 0
322                 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
323                         { perror("connect"); ret = 0; }
324                 else
325                         {
326 #endif
327                         memcpy(&(data->peer),to, sizeof(struct sockaddr));
328 #if 0
329                         }
330 #endif
331                 break;
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");
339                 break;
340 #endif
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)
345                         { ret = 0; }
346                 else
347                         {
348                         data->mtu = sockopt_val;
349                         ret = data->mtu;
350                         }
351                 break;
352         case BIO_CTRL_DGRAM_GET_MTU:
353                 return data->mtu;
354                 break;
355         case BIO_CTRL_DGRAM_SET_MTU:
356                 data->mtu = num;
357                 ret = num;
358                 break;
359         case BIO_CTRL_DGRAM_SET_CONNECTED:
360                 to = (struct sockaddr *)ptr;
361
362                 if ( to != NULL)
363                         {
364                         data->connected = 1;
365                         memcpy(&(data->peer),to, sizeof(struct sockaddr));
366                         }
367                 else
368                         {
369                         data->connected = 0;
370                         memset(&(data->peer), 0x00, sizeof(struct sockaddr));
371                         }
372                 break;
373     case BIO_CTRL_DGRAM_SET_PEER:
374         to = (struct sockaddr *) ptr;
375
376         memcpy(&(data->peer), to, sizeof(struct sockaddr));
377         break;
378         case BIO_CTRL_DGRAM_SET_TIMEOUT:
379                 if (num > 0)
380                         {
381 #ifdef OPENSSL_SYS_WIN32
382                         struct _timeb tb;
383                         _ftime(&tb);
384                         data->hstimeout.tv_sec = (long)tb.time;
385                         data->hstimeout.tv_usec = (long)tb.millitm * 1000;
386 #else
387                         gettimeofday(&(data->hstimeout), NULL);
388 #endif
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)
392                                 {
393                                 data->hstimeout.tv_sec++;
394                                 data->hstimeout.tv_usec -= 1000000;
395                                 }
396                         }
397                 else
398                         {
399                         memset(&(data->hstimeout), 0, sizeof(struct timeval));
400                         }
401                 break;
402 #if defined(SO_RCVTIMEO)
403         case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
404 #ifdef OPENSSL_SYS_WINDOWS
405                 {
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; }
411                 }
412 #else
413                 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
414                         sizeof(struct timeval)) < 0)
415                         { perror("setsockopt"); ret = -1; }
416 #endif
417                 memcpy(&(data->hstimeoutdiff), ptr, sizeof(struct timeval));
418                 break;
419         case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
420 #ifdef OPENSSL_SYS_WINDOWS
421                 {
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; }
427                 else
428                         {
429                         tv->tv_sec = timeout / 1000;
430                         tv->tv_usec = (timeout % 1000) * 1000;
431                         ret = sizeof(*tv);
432                         }
433                 }
434 #else
435                 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
436                         ptr, (void *)&ret) < 0)
437                         { perror("getsockopt"); ret = -1; }
438 #endif
439                 break;
440 #endif
441 #if defined(SO_SNDTIMEO)
442         case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
443 #ifdef OPENSSL_SYS_WINDOWS
444                 {
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; }
450                 }
451 #else
452                 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
453                         sizeof(struct timeval)) < 0)
454                         { perror("setsockopt"); ret = -1; }
455 #endif
456                 break;
457         case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
458 #ifdef OPENSSL_SYS_WINDOWS
459                 {
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; }
465                 else
466                         {
467                         tv->tv_sec = timeout / 1000;
468                         tv->tv_usec = (timeout % 1000) * 1000;
469                         ret = sizeof(*tv);
470                         }
471                 }
472 #else
473                 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 
474                         ptr, (void *)&ret) < 0)
475                         { perror("getsockopt"); ret = -1; }
476 #endif
477                 break;
478 #endif
479         case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
480                 /* fall-through */
481         case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
482 #ifdef OPENSSL_SYS_WINDOWS
483                 if ( data->_errno == WSAETIMEDOUT)
484 #else
485                 if ( data->_errno == EAGAIN)
486 #endif
487                         {
488                         ret = 1;
489                         data->_errno = 0;
490                         }
491                 else
492                         ret = 0;
493                 break;
494 #ifdef EMSGSIZE
495         case BIO_CTRL_DGRAM_MTU_EXCEEDED:
496                 if ( data->_errno == EMSGSIZE)
497                         {
498                         ret = 1;
499                         data->_errno = 0;
500                         }
501                 else
502                         ret = 0;
503                 break;
504 #endif
505         default:
506                 ret=0;
507                 break;
508                 }
509         return(ret);
510         }
511
512 static int dgram_puts(BIO *bp, const char *str)
513         {
514         int n,ret;
515
516         n=strlen(str);
517         ret=dgram_write(bp,str,n);
518         return(ret);
519         }
520
521 static int BIO_dgram_should_retry(int i)
522         {
523         int err;
524
525         if ((i == 0) || (i == -1))
526                 {
527                 err=get_last_socket_error();
528
529 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
530                 if ((i == -1) && (err == 0))
531                         return(1);
532 #endif
533
534                 return(BIO_dgram_non_fatal_error(err));
535                 }
536         return(0);
537         }
538
539 int BIO_dgram_non_fatal_error(int err)
540         {
541         switch (err)
542                 {
543 #if defined(OPENSSL_SYS_WINDOWS)
544 # if defined(WSAEWOULDBLOCK)
545         case WSAEWOULDBLOCK:
546 # endif
547
548 # if 0 /* This appears to always be an error */
549 #  if defined(WSAENOTCONN)
550         case WSAENOTCONN:
551 #  endif
552 # endif
553 #endif
554
555 #ifdef EWOULDBLOCK
556 # ifdef WSAEWOULDBLOCK
557 #  if WSAEWOULDBLOCK != EWOULDBLOCK
558         case EWOULDBLOCK:
559 #  endif
560 # else
561         case EWOULDBLOCK:
562 # endif
563 #endif
564
565 #if defined(ENOTCONN)
566         case ENOTCONN:
567 #endif
568
569 #ifdef EINTR
570         case EINTR:
571 #endif
572
573 #ifdef EAGAIN
574 #if EWOULDBLOCK != EAGAIN
575         case EAGAIN:
576 # endif
577 #endif
578
579 #ifdef EPROTO
580         case EPROTO:
581 #endif
582
583 #ifdef EINPROGRESS
584         case EINPROGRESS:
585 #endif
586
587 #ifdef EALREADY
588         case EALREADY:
589 #endif
590
591 /* DF bit set, and packet larger than MTU */
592 #ifdef EMSGSIZE
593         case EMSGSIZE:
594 #endif
595
596                 return(1);
597                 /* break; */
598         default:
599                 break;
600                 }
601         return(0);
602         }
603 #endif