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