bss_dgram.c: more elegant solution to PR#2069. Use socklen_t heuristic
[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 #if defined(OPENSSL_SYS_WIN32) || defined(OPENSSL_SYS_VMS)
70 #include <sys/timeb.h>
71 #endif
72
73 #ifdef OPENSSL_SYS_LINUX
74 #define IP_MTU      14 /* linux is lame */
75 #endif
76
77 #ifdef WATT32
78 #define sock_write SockWrite  /* Watt-32 uses same names */
79 #define sock_read  SockRead
80 #define sock_puts  SockPuts
81 #endif
82
83 static int dgram_write(BIO *h, const char *buf, int num);
84 static int dgram_read(BIO *h, char *buf, int size);
85 static int dgram_puts(BIO *h, const char *str);
86 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
87 static int dgram_new(BIO *h);
88 static int dgram_free(BIO *data);
89 static int dgram_clear(BIO *bio);
90
91 static int BIO_dgram_should_retry(int s);
92
93 static void get_current_time(struct timeval *t);
94
95 static BIO_METHOD methods_dgramp=
96         {
97         BIO_TYPE_DGRAM,
98         "datagram socket",
99         dgram_write,
100         dgram_read,
101         dgram_puts,
102         NULL, /* dgram_gets, */
103         dgram_ctrl,
104         dgram_new,
105         dgram_free,
106         NULL,
107         };
108
109 typedef struct bio_dgram_data_st
110         {
111         union {
112                 struct sockaddr sa;
113                 struct sockaddr_in sa_in;
114 #if OPENSSL_USE_IPV6
115                 struct sockaddr_in6 sa_in6;
116 #endif
117         } peer;
118         unsigned int connected;
119         unsigned int _errno;
120         unsigned int mtu;
121         struct timeval next_timeout;
122         struct timeval socket_timeout;
123         } bio_dgram_data;
124
125 BIO_METHOD *BIO_s_datagram(void)
126         {
127         return(&methods_dgramp);
128         }
129
130 BIO *BIO_new_dgram(int fd, int close_flag)
131         {
132         BIO *ret;
133
134         ret=BIO_new(BIO_s_datagram());
135         if (ret == NULL) return(NULL);
136         BIO_set_fd(ret,fd,close_flag);
137         return(ret);
138         }
139
140 static int dgram_new(BIO *bi)
141         {
142         bio_dgram_data *data = NULL;
143
144         bi->init=0;
145         bi->num=0;
146         data = OPENSSL_malloc(sizeof(bio_dgram_data));
147         if (data == NULL)
148                 return 0;
149         memset(data, 0x00, sizeof(bio_dgram_data));
150     bi->ptr = data;
151
152         bi->flags=0;
153         return(1);
154         }
155
156 static int dgram_free(BIO *a)
157         {
158         bio_dgram_data *data;
159
160         if (a == NULL) return(0);
161         if ( ! dgram_clear(a))
162                 return 0;
163
164         data = (bio_dgram_data *)a->ptr;
165         if(data != NULL) OPENSSL_free(data);
166
167         return(1);
168         }
169
170 static int dgram_clear(BIO *a)
171         {
172         if (a == NULL) return(0);
173         if (a->shutdown)
174                 {
175                 if (a->init)
176                         {
177                         SHUTDOWN2(a->num);
178                         }
179                 a->init=0;
180                 a->flags=0;
181                 }
182         return(1);
183         }
184
185 static void dgram_adjust_rcv_timeout(BIO *b)
186         {
187 #if defined(SO_RCVTIMEO)
188         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
189         int sz = sizeof(int);
190
191         /* Is a timer active? */
192         if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
193                 {
194                 struct timeval timenow, timeleft;
195
196                 /* Read current socket timeout */
197 #ifdef OPENSSL_SYS_WINDOWS
198                 int timeout;
199                 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
200                                            (void*)&timeout, &sz) < 0)
201                         { perror("getsockopt"); }
202                 else
203                         {
204                         data->socket_timeout.tv_sec = timeout / 1000;
205                         data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
206                         }
207 #else
208                 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
209                                                 &(data->socket_timeout), (void *)&sz) < 0)
210                         { perror("getsockopt"); }
211 #endif
212
213                 /* Get current time */
214                 get_current_time(&timenow);
215
216                 /* Calculate time left until timer expires */
217                 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
218                 timeleft.tv_sec -= timenow.tv_sec;
219                 timeleft.tv_usec -= timenow.tv_usec;
220                 if (timeleft.tv_usec < 0)
221                         {
222                         timeleft.tv_sec--;
223                         timeleft.tv_usec += 1000000;
224                         }
225
226                 if (timeleft.tv_sec < 0)
227                         {
228                         timeleft.tv_sec = 0;
229                         timeleft.tv_usec = 1;
230                         }
231
232                 /* Adjust socket timeout if next handhake message timer
233                  * will expire earlier.
234                  */
235                 if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
236                         (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
237                         (data->socket_timeout.tv_sec == timeleft.tv_sec &&
238                          data->socket_timeout.tv_usec >= timeleft.tv_usec))
239                         {
240 #ifdef OPENSSL_SYS_WINDOWS
241                         timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
242                         if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
243                                                    (void*)&timeout, sizeof(timeout)) < 0)
244                                 { perror("setsockopt"); }
245 #else
246                         if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
247                                                         sizeof(struct timeval)) < 0)
248                                 { perror("setsockopt"); }
249 #endif
250                         }
251                 }
252 #endif
253         }
254
255 static void dgram_reset_rcv_timeout(BIO *b)
256         {
257 #if defined(SO_RCVTIMEO)
258         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
259
260         /* Is a timer active? */
261         if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
262                 {
263 #ifdef OPENSSL_SYS_WINDOWS
264                 int timeout = data->socket_timeout.tv_sec * 1000 +
265                                           data->socket_timeout.tv_usec / 1000;
266                 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
267                                            (void*)&timeout, sizeof(timeout)) < 0)
268                         { perror("setsockopt"); }
269 #else
270                 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
271                                                 sizeof(struct timeval)) < 0)
272                         { perror("setsockopt"); }
273 #endif
274                 }
275 #endif
276         }
277
278 static int dgram_read(BIO *b, char *out, int outl)
279         {
280         int ret=0;
281         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
282
283         struct  {
284         /*
285          * See commentary in b_sock.c. <appro>
286          */
287         union   { size_t s; int i; } len;
288         union   {
289                 struct sockaddr sa;
290                 struct sockaddr_in sa_in;
291 #if OPENSSL_USE_IPV6
292                 struct sockaddr_in6 sa_in6;
293 #endif
294                 } peer;
295         } sa;
296
297         sa.len.s=0;
298         sa.len.i=sizeof(sa.peer);
299
300         if (out != NULL)
301                 {
302                 clear_socket_error();
303                 memset(&sa.peer, 0x00, sizeof(sa.peer));
304                 dgram_adjust_rcv_timeout(b);
305                 ret=recvfrom(b->num,out,outl,0,&sa.peer.sa,(void *)&sa.len);
306                 if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
307                         {
308                         OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
309                         sa.len.i = (int)sa.len.s;
310                         }
311                 dgram_reset_rcv_timeout(b);
312
313                 if ( ! data->connected  && ret >= 0)
314                         BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
315
316                 BIO_clear_retry_flags(b);
317                 if (ret < 0)
318                         {
319                         if (BIO_dgram_should_retry(ret))
320                                 {
321                                 BIO_set_retry_read(b);
322                                 data->_errno = get_last_socket_error();
323                                 }
324                         }
325                 }
326         return(ret);
327         }
328
329 static int dgram_write(BIO *b, const char *in, int inl)
330         {
331         int ret;
332         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
333         clear_socket_error();
334
335         if ( data->connected )
336                 ret=writesocket(b->num,in,inl);
337         else
338 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
339                 ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, sizeof(data->peer));
340 #else
341                 ret=sendto(b->num, in, inl, 0, &data->peer.sa, sizeof(data->peer));
342 #endif
343
344         BIO_clear_retry_flags(b);
345         if (ret <= 0)
346                 {
347                 if (BIO_dgram_should_retry(ret))
348                         {
349                         BIO_set_retry_write(b);  
350                         data->_errno = get_last_socket_error();
351
352 #if 0 /* higher layers are responsible for querying MTU, if necessary */
353                         if ( data->_errno == EMSGSIZE)
354                                 /* retrieve the new MTU */
355                                 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
356 #endif
357                         }
358                 }
359         return(ret);
360         }
361
362 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
363         {
364         long ret=1;
365         int *ip;
366         struct sockaddr *to = NULL;
367         bio_dgram_data *data = NULL;
368 #if defined(IP_MTU_DISCOVER) || defined(IP_MTU)
369         long sockopt_val = 0;
370         unsigned int sockopt_len = 0;
371 #endif
372 #ifdef OPENSSL_SYS_LINUX
373         socklen_t addr_len;
374         struct sockaddr_storage addr;
375 #endif
376
377         data = (bio_dgram_data *)b->ptr;
378
379         switch (cmd)
380                 {
381         case BIO_CTRL_RESET:
382                 num=0;
383         case BIO_C_FILE_SEEK:
384                 ret=0;
385                 break;
386         case BIO_C_FILE_TELL:
387         case BIO_CTRL_INFO:
388                 ret=0;
389                 break;
390         case BIO_C_SET_FD:
391                 dgram_clear(b);
392                 b->num= *((int *)ptr);
393                 b->shutdown=(int)num;
394                 b->init=1;
395                 break;
396         case BIO_C_GET_FD:
397                 if (b->init)
398                         {
399                         ip=(int *)ptr;
400                         if (ip != NULL) *ip=b->num;
401                         ret=b->num;
402                         }
403                 else
404                         ret= -1;
405                 break;
406         case BIO_CTRL_GET_CLOSE:
407                 ret=b->shutdown;
408                 break;
409         case BIO_CTRL_SET_CLOSE:
410                 b->shutdown=(int)num;
411                 break;
412         case BIO_CTRL_PENDING:
413         case BIO_CTRL_WPENDING:
414                 ret=0;
415                 break;
416         case BIO_CTRL_DUP:
417         case BIO_CTRL_FLUSH:
418                 ret=1;
419                 break;
420         case BIO_CTRL_DGRAM_CONNECT:
421                 to = (struct sockaddr *)ptr;
422 #if 0
423                 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
424                         { perror("connect"); ret = 0; }
425                 else
426                         {
427 #endif
428                         switch (to->sa_family)
429                                 {
430                                 case AF_INET:
431                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in));
432                                         break;
433 #if OPENSSL_USE_IPV6
434                                 case AF_INET6:
435                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
436                                         break;
437 #endif
438                                 default:
439                                         memcpy(&data->peer,to,sizeof(data->peer.sa));
440                                         break;
441                                 }
442 #if 0
443                         }
444 #endif
445                 break;
446                 /* (Linux)kernel sets DF bit on outgoing IP packets */
447         case BIO_CTRL_DGRAM_MTU_DISCOVER:
448 #ifdef OPENSSL_SYS_LINUX
449                 addr_len = (socklen_t)sizeof(struct sockaddr_storage);
450                 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
451                 if (getsockname(b->num, (void *)&addr, &addr_len) < 0)
452                         {
453                         ret = 0;
454                         break;
455                         }
456                 sockopt_len = sizeof(sockopt_val);
457                 switch (addr.ss_family)
458                         {
459                 case AF_INET:
460                         sockopt_val = IP_PMTUDISC_DO;
461                         if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
462                                 &sockopt_val, sizeof(sockopt_val))) < 0)
463                                 perror("setsockopt");
464                         break;
465 #if OPENSSL_USE_IPV6
466                 case AF_INET6:
467                         sockopt_val = IPV6_PMTUDISC_DO;
468                         if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
469                                 &sockopt_val, sizeof(sockopt_val))) < 0)
470                                 perror("setsockopt");
471                         break;
472 #endif
473                 default:
474                         ret = -1;
475                         break;
476                         }
477                 ret = -1;
478 #else
479                 break;
480 #endif
481         case BIO_CTRL_DGRAM_QUERY_MTU:
482 #ifdef OPENSSL_SYS_LINUX
483                 addr_len = (socklen_t)sizeof(struct sockaddr_storage);
484                 memset((void *)&addr, 0, sizeof(struct sockaddr_storage));
485                 if (getsockname(b->num, (void *)&addr, &addr_len) < 0)
486                         {
487                         ret = 0;
488                         break;
489                         }
490                 sockopt_len = sizeof(sockopt_val);
491                 switch (addr.ss_family)
492                         {
493                 case AF_INET:
494                         if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
495                                 &sockopt_len)) < 0 || sockopt_val < 0)
496                                 {
497                                 ret = 0;
498                                 }
499                         else
500                                 {
501                                 /* we assume that the transport protocol is UDP and no
502                                  * IP options are used.
503                                  */
504                                 data->mtu = sockopt_val - 8 - 20;
505                                 ret = data->mtu;
506                                 }
507                         break;
508 #if OPENSSL_USE_IPV6
509                 case AF_INET6:
510                         if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
511                                 &sockopt_len)) < 0 || sockopt_val < 0)
512                                 {
513                                 ret = 0;
514                                 }
515                         else
516                                 {
517                                 /* we assume that the transport protocol is UDP and no
518                                  * IPV6 options are used.
519                                  */
520                                 data->mtu = sockopt_val - 8 - 40;
521                                 ret = data->mtu;
522                                 }
523                         break;
524 #endif
525                 default:
526                         ret = 0;
527                         break;
528                         }
529 #else
530                 ret = 0;
531 #endif
532                 break;
533         case BIO_CTRL_DGRAM_GET_MTU:
534                 return data->mtu;
535                 break;
536         case BIO_CTRL_DGRAM_SET_MTU:
537                 data->mtu = num;
538                 ret = num;
539                 break;
540         case BIO_CTRL_DGRAM_SET_CONNECTED:
541                 to = (struct sockaddr *)ptr;
542
543                 if ( to != NULL)
544                         {
545                         data->connected = 1;
546                         switch (to->sa_family)
547                                 {
548                                 case AF_INET:
549                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in));
550                                         break;
551 #if OPENSSL_USE_IPV6
552                                 case AF_INET6:
553                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
554                                         break;
555 #endif
556                                 default:
557                                         memcpy(&data->peer,to,sizeof(data->peer.sa));
558                                         break;
559                                 }
560                         }
561                 else
562                         {
563                         data->connected = 0;
564                         memset(&(data->peer), 0x00, sizeof(data->peer));
565                         }
566                 break;
567         case BIO_CTRL_DGRAM_GET_PEER:
568                 to = (struct sockaddr *) ptr;
569                 switch (to->sa_family)
570                         {
571                         case AF_INET:
572                                 memcpy(to,&data->peer,(ret=sizeof(data->peer.sa_in)));
573                                 break;
574 #if OPENSSL_USE_IPV6
575                         case AF_INET6:
576                                 memcpy(to,&data->peer,(ret=sizeof(data->peer.sa_in6)));
577                                 break;
578 #endif
579                         default:
580                                 memcpy(to,&data->peer,(ret=sizeof(data->peer.sa)));
581                                 break;
582                         }
583                 break;
584         case BIO_CTRL_DGRAM_SET_PEER:
585                 to = (struct sockaddr *) ptr;
586                 switch (to->sa_family)
587                         {
588                         case AF_INET:
589                                 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
590                                 break;
591 #if OPENSSL_USE_IPV6
592                         case AF_INET6:
593                                 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
594                                 break;
595 #endif
596                         default:
597                                 memcpy(&data->peer,to,sizeof(data->peer.sa));
598                                 break;
599                         }
600                 break;
601         case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
602                 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
603                 break;
604 #if defined(SO_RCVTIMEO)
605         case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
606 #ifdef OPENSSL_SYS_WINDOWS
607                 {
608                 struct timeval *tv = (struct timeval *)ptr;
609                 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
610                 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
611                         (void*)&timeout, sizeof(timeout)) < 0)
612                         { perror("setsockopt"); ret = -1; }
613                 }
614 #else
615                 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
616                         sizeof(struct timeval)) < 0)
617                         { perror("setsockopt"); ret = -1; }
618 #endif
619                 break;
620         case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
621 #ifdef OPENSSL_SYS_WINDOWS
622                 {
623                 int timeout, sz = sizeof(timeout);
624                 struct timeval *tv = (struct timeval *)ptr;
625                 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
626                         (void*)&timeout, &sz) < 0)
627                         { perror("getsockopt"); ret = -1; }
628                 else
629                         {
630                         tv->tv_sec = timeout / 1000;
631                         tv->tv_usec = (timeout % 1000) * 1000;
632                         ret = sizeof(*tv);
633                         }
634                 }
635 #else
636                 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
637                         ptr, (void *)&ret) < 0)
638                         { perror("getsockopt"); ret = -1; }
639 #endif
640                 break;
641 #endif
642 #if defined(SO_SNDTIMEO)
643         case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
644 #ifdef OPENSSL_SYS_WINDOWS
645                 {
646                 struct timeval *tv = (struct timeval *)ptr;
647                 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
648                 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
649                         (void*)&timeout, sizeof(timeout)) < 0)
650                         { perror("setsockopt"); ret = -1; }
651                 }
652 #else
653                 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
654                         sizeof(struct timeval)) < 0)
655                         { perror("setsockopt"); ret = -1; }
656 #endif
657                 break;
658         case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
659 #ifdef OPENSSL_SYS_WINDOWS
660                 {
661                 int timeout, sz = sizeof(timeout);
662                 struct timeval *tv = (struct timeval *)ptr;
663                 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
664                         (void*)&timeout, &sz) < 0)
665                         { perror("getsockopt"); ret = -1; }
666                 else
667                         {
668                         tv->tv_sec = timeout / 1000;
669                         tv->tv_usec = (timeout % 1000) * 1000;
670                         ret = sizeof(*tv);
671                         }
672                 }
673 #else
674                 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 
675                         ptr, (void *)&ret) < 0)
676                         { perror("getsockopt"); ret = -1; }
677 #endif
678                 break;
679 #endif
680         case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
681                 /* fall-through */
682         case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
683 #ifdef OPENSSL_SYS_WINDOWS
684                 if ( data->_errno == WSAETIMEDOUT)
685 #else
686                 if ( data->_errno == EAGAIN)
687 #endif
688                         {
689                         ret = 1;
690                         data->_errno = 0;
691                         }
692                 else
693                         ret = 0;
694                 break;
695 #ifdef EMSGSIZE
696         case BIO_CTRL_DGRAM_MTU_EXCEEDED:
697                 if ( data->_errno == EMSGSIZE)
698                         {
699                         ret = 1;
700                         data->_errno = 0;
701                         }
702                 else
703                         ret = 0;
704                 break;
705 #endif
706         default:
707                 ret=0;
708                 break;
709                 }
710         return(ret);
711         }
712
713 static int dgram_puts(BIO *bp, const char *str)
714         {
715         int n,ret;
716
717         n=strlen(str);
718         ret=dgram_write(bp,str,n);
719         return(ret);
720         }
721
722 static int BIO_dgram_should_retry(int i)
723         {
724         int err;
725
726         if ((i == 0) || (i == -1))
727                 {
728                 err=get_last_socket_error();
729
730 #if defined(OPENSSL_SYS_WINDOWS) && 0 /* more microsoft stupidity? perhaps not? Ben 4/1/99 */
731                 if ((i == -1) && (err == 0))
732                         return(1);
733 #endif
734
735                 return(BIO_dgram_non_fatal_error(err));
736                 }
737         return(0);
738         }
739
740 int BIO_dgram_non_fatal_error(int err)
741         {
742         switch (err)
743                 {
744 #if defined(OPENSSL_SYS_WINDOWS)
745 # if defined(WSAEWOULDBLOCK)
746         case WSAEWOULDBLOCK:
747 # endif
748
749 # if 0 /* This appears to always be an error */
750 #  if defined(WSAENOTCONN)
751         case WSAENOTCONN:
752 #  endif
753 # endif
754 #endif
755
756 #ifdef EWOULDBLOCK
757 # ifdef WSAEWOULDBLOCK
758 #  if WSAEWOULDBLOCK != EWOULDBLOCK
759         case EWOULDBLOCK:
760 #  endif
761 # else
762         case EWOULDBLOCK:
763 # endif
764 #endif
765
766 #ifdef EINTR
767         case EINTR:
768 #endif
769
770 #ifdef EAGAIN
771 #if EWOULDBLOCK != EAGAIN
772         case EAGAIN:
773 # endif
774 #endif
775
776 #ifdef EPROTO
777         case EPROTO:
778 #endif
779
780 #ifdef EINPROGRESS
781         case EINPROGRESS:
782 #endif
783
784 #ifdef EALREADY
785         case EALREADY:
786 #endif
787
788                 return(1);
789                 /* break; */
790         default:
791                 break;
792                 }
793         return(0);
794         }
795 #endif
796
797 static void get_current_time(struct timeval *t)
798         {
799 #ifdef OPENSSL_SYS_WIN32
800         struct _timeb tb;
801         _ftime(&tb);
802         t->tv_sec = (long)tb.time;
803         t->tv_usec = (long)tb.millitm * 1000;
804 #elif defined(OPENSSL_SYS_VMS)
805         struct timeb tb;
806         ftime(&tb);
807         t->tv_sec = (long)tb.time;
808         t->tv_usec = (long)tb.millitm * 1000;
809 #else
810         gettimeofday(t, NULL);
811 #endif
812         }