OPENSSL_Applink update.
[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 #define IP_MTU      14 /* linux is lame */
70
71 #ifdef WATT32
72 #define sock_write SockWrite  /* Watt-32 uses same names */
73 #define sock_read  SockRead
74 #define sock_puts  SockPuts
75 #endif
76
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);
84
85 int BIO_dgram_should_retry(int s);
86
87 static BIO_METHOD methods_dgramp=
88         {
89         BIO_TYPE_DGRAM,
90         "datagram socket",
91         dgram_write,
92         dgram_read,
93         dgram_puts,
94         NULL, /* dgram_gets, */
95         dgram_ctrl,
96         dgram_new,
97         dgram_free,
98         NULL,
99         };
100
101 typedef struct bio_dgram_data_st
102         {
103         struct sockaddr peer;
104         unsigned int connected;
105         unsigned int _errno;
106         unsigned int mtu;
107         } bio_dgram_data;
108
109 BIO_METHOD *BIO_s_datagram(void)
110         {
111         return(&methods_dgramp);
112         }
113
114 BIO *BIO_new_dgram(int fd, int close_flag)
115         {
116         BIO *ret;
117
118         ret=BIO_new(BIO_s_datagram());
119         if (ret == NULL) return(NULL);
120         BIO_set_fd(ret,fd,close_flag);
121         return(ret);
122         }
123
124 static int dgram_new(BIO *bi)
125         {
126         bio_dgram_data *data = NULL;
127
128         bi->init=0;
129         bi->num=0;
130         data = OPENSSL_malloc(sizeof(bio_dgram_data));
131         if (data == NULL)
132                 return 0;
133         memset(data, 0x00, sizeof(bio_dgram_data));
134     bi->ptr = data;
135
136         bi->flags=0;
137         return(1);
138         }
139
140 static int dgram_free(BIO *a)
141         {
142         bio_dgram_data *data;
143
144         if (a == NULL) return(0);
145         if ( ! dgram_clear(a))
146                 return 0;
147
148         data = (bio_dgram_data *)a->ptr;
149         if(data != NULL) OPENSSL_free(data);
150
151         return(1);
152         }
153
154 static int dgram_clear(BIO *a)
155         {
156         if (a == NULL) return(0);
157         if (a->shutdown)
158                 {
159                 if (a->init)
160                         {
161                         SHUTDOWN2(a->num);
162                         }
163                 a->init=0;
164                 a->flags=0;
165                 }
166         return(1);
167         }
168         
169 static int dgram_read(BIO *b, char *out, int outl)
170         {
171         int ret=0;
172         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
173
174         struct sockaddr peer;
175         int peerlen = sizeof(peer);
176
177         if (out != NULL)
178                 {
179                 clear_socket_error();
180                 memset(&peer, 0x00, peerlen);
181                 /* Last arg in recvfrom is signed on some platforms and
182                  * unsigned on others. It is of type socklen_t on some
183                  * but this is not universal. Cast to (void *) to avoid
184                  * compiler warnings.
185                  */
186                 ret=recvfrom(b->num,out,outl,0,&peer,(void *)&peerlen);
187
188                 if ( ! data->connected  && ret > 0)
189                         BIO_ctrl(b, BIO_CTRL_DGRAM_CONNECT, 0, &peer);
190
191                 BIO_clear_retry_flags(b);
192                 if (ret <= 0)
193                         {
194                         if (BIO_dgram_should_retry(ret))
195                                 {
196                                 BIO_set_retry_read(b);
197                                 data->_errno = get_last_socket_error();
198                                 }
199                         }
200                 }
201         return(ret);
202         }
203
204 static int dgram_write(BIO *b, const char *in, int inl)
205         {
206         int ret;
207         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
208         clear_socket_error();
209
210     if ( data->connected )
211         ret=send(b->num,in,inl,0);
212     else
213         ret=sendto(b->num, in, inl, 0, &data->peer, sizeof(data->peer));
214
215         BIO_clear_retry_flags(b);
216         if (ret <= 0)
217                 {
218                 if (BIO_sock_should_retry(ret))
219                         {
220                         BIO_set_retry_write(b);  
221                         data->_errno = get_last_socket_error();
222
223 #if 0 /* higher layers are responsible for querying MTU, if necessary */
224                         if ( data->_errno == EMSGSIZE)
225                                 /* retrieve the new MTU */
226                                 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
227 #endif
228                         }
229                 }
230         return(ret);
231         }
232
233 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
234         {
235         long ret=1;
236         int *ip;
237         struct sockaddr *to = NULL;
238         bio_dgram_data *data = NULL;
239         long sockopt_val = 0;
240         unsigned int sockopt_len = 0;
241
242         data = (bio_dgram_data *)b->ptr;
243
244         switch (cmd)
245                 {
246         case BIO_CTRL_RESET:
247                 num=0;
248         case BIO_C_FILE_SEEK:
249                 ret=0;
250                 break;
251         case BIO_C_FILE_TELL:
252         case BIO_CTRL_INFO:
253                 ret=0;
254                 break;
255         case BIO_C_SET_FD:
256                 dgram_clear(b);
257                 b->num= *((int *)ptr);
258                 b->shutdown=(int)num;
259                 b->init=1;
260                 break;
261         case BIO_C_GET_FD:
262                 if (b->init)
263                         {
264                         ip=(int *)ptr;
265                         if (ip != NULL) *ip=b->num;
266                         ret=b->num;
267                         }
268                 else
269                         ret= -1;
270                 break;
271         case BIO_CTRL_GET_CLOSE:
272                 ret=b->shutdown;
273                 break;
274         case BIO_CTRL_SET_CLOSE:
275                 b->shutdown=(int)num;
276                 break;
277         case BIO_CTRL_PENDING:
278         case BIO_CTRL_WPENDING:
279                 ret=0;
280                 break;
281         case BIO_CTRL_DUP:
282         case BIO_CTRL_FLUSH:
283                 ret=1;
284                 break;
285         case BIO_CTRL_DGRAM_CONNECT:
286                 to = (struct sockaddr *)ptr;
287 #if 0
288                 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
289                         { perror("connect"); ret = 0; }
290                 else
291                         {
292 #endif
293                         memcpy(&(data->peer),to, sizeof(struct sockaddr));
294 #if 0
295                         }
296 #endif
297                 break;
298                 /* (Linux)kernel sets DF bit on outgoing IP packets */
299 #ifdef IP_MTU_DISCOVER
300         case BIO_CTRL_DGRAM_MTU_DISCOVER:
301                 sockopt_val = IP_PMTUDISC_DO;
302                 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
303                         &sockopt_val, sizeof(sockopt_val))) < 0)
304                         perror("setsockopt");
305                 break;
306 #endif
307         case BIO_CTRL_DGRAM_QUERY_MTU:
308          sockopt_len = sizeof(sockopt_val);
309                 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
310                         &sockopt_len)) < 0 || sockopt_val < 0)
311                         { ret = 0; }
312                 else
313                         {
314                         data->mtu = sockopt_val;
315                         ret = data->mtu;
316                         }
317                 break;
318         case BIO_CTRL_DGRAM_GET_MTU:
319                 return data->mtu;
320                 break;
321         case BIO_CTRL_DGRAM_SET_MTU:
322                 data->mtu = num;
323                 ret = num;
324                 break;
325         case BIO_CTRL_DGRAM_SET_CONNECTED:
326                 to = (struct sockaddr *)ptr;
327
328                 if ( to != NULL)
329                         {
330                         data->connected = 1;
331                         memcpy(&(data->peer),to, sizeof(struct sockaddr));
332                         }
333                 else
334                         {
335                         data->connected = 0;
336                         memset(&(data->peer), 0x00, sizeof(struct sockaddr));
337                         }
338                 break;
339     case BIO_CTRL_DGRAM_SET_PEER:
340         to = (struct sockaddr *) ptr;
341
342         memcpy(&(data->peer), to, sizeof(struct sockaddr));
343         break;
344         case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
345                 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
346                         sizeof(struct timeval)) < 0)
347                         { perror("setsockopt"); ret = -1; }
348                 break;
349         case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
350                 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
351                         ptr, (void *)&ret) < 0)
352                         { perror("getsockopt"); ret = -1; }
353                 break;
354         case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
355                 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
356                         sizeof(struct timeval)) < 0)
357                         { perror("setsockopt"); ret = -1; }
358                 break;
359         case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
360                 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 
361                         ptr, (void *)&ret) < 0)
362                         { perror("getsockopt"); ret = -1; }
363                 break;
364         case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
365                 /* fall-through */
366         case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
367                 if ( data->_errno == EAGAIN)
368                         {
369                         ret = 1;
370                         data->_errno = 0;
371                         }
372                 else
373                         ret = 0;
374                 break;
375 #ifdef EMSGSIZE
376         case BIO_CTRL_DGRAM_MTU_EXCEEDED:
377                 if ( data->_errno == EMSGSIZE)
378                         {
379                         ret = 1;
380                         data->_errno = 0;
381                         }
382                 else
383                         ret = 0;
384                 break;
385 #endif
386         default:
387                 ret=0;
388                 break;
389                 }
390         return(ret);
391         }
392
393 static int dgram_puts(BIO *bp, const char *str)
394         {
395         int n,ret;
396
397         n=strlen(str);
398         ret=dgram_write(bp,str,n);
399         return(ret);
400         }
401
402 int BIO_dgram_should_retry(int i)
403         {
404         int err;
405
406         if ((i == 0) || (i == -1))
407                 {
408                 err=get_last_socket_error();
409
410 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
411                 if ((i == -1) && (err == 0))
412                         return(1);
413 #endif
414
415                 return(BIO_dgram_non_fatal_error(err));
416                 }
417         return(0);
418         }
419
420 int BIO_dgram_non_fatal_error(int err)
421         {
422         switch (err)
423                 {
424 #if defined(OPENSSL_SYS_WINDOWS)
425 # if defined(WSAEWOULDBLOCK)
426         case WSAEWOULDBLOCK:
427 # endif
428
429 # if 0 /* This appears to always be an error */
430 #  if defined(WSAENOTCONN)
431         case WSAENOTCONN:
432 #  endif
433 # endif
434 #endif
435
436 #ifdef EWOULDBLOCK
437 # ifdef WSAEWOULDBLOCK
438 #  if WSAEWOULDBLOCK != EWOULDBLOCK
439         case EWOULDBLOCK:
440 #  endif
441 # else
442         case EWOULDBLOCK:
443 # endif
444 #endif
445
446 #if defined(ENOTCONN)
447         case ENOTCONN:
448 #endif
449
450 #ifdef EINTR
451         case EINTR:
452 #endif
453
454 #ifdef EAGAIN
455 #if EWOULDBLOCK != EAGAIN
456         case EAGAIN:
457 # endif
458 #endif
459
460 #ifdef EPROTO
461         case EPROTO:
462 #endif
463
464 #ifdef EINPROGRESS
465         case EINPROGRESS:
466 #endif
467
468 #ifdef EALREADY
469         case EALREADY:
470 #endif
471
472 /* DF bit set, and packet larger than MTU */
473 #ifdef EMSGSIZE
474         case EMSGSIZE:
475 #endif
476
477                 return(1);
478                 /* break; */
479         default:
480                 break;
481                 }
482         return(0);
483         }
484 #endif