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