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