Remove DJGPP (and therefore WATT32) #ifdef's.
[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
61 #include <stdio.h>
62 #include <errno.h>
63 #define USE_SOCKETS
64 #include "cryptlib.h"
65
66 #include <openssl/bio.h>
67 #ifndef OPENSSL_NO_DGRAM
68
69 #if defined(OPENSSL_SYS_VMS)
70 #include <sys/timeb.h>
71 #endif
72
73 #ifndef OPENSSL_NO_SCTP
74 #include <netinet/sctp.h>
75 #include <fcntl.h>
76 #define OPENSSL_SCTP_DATA_CHUNK_TYPE            0x00
77 #define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
78 #endif
79
80 #if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
81 #define IP_MTU      14 /* linux is lame */
82 #endif
83
84 #if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
85 #define IPPROTO_IPV6 41 /* windows is lame */
86 #endif
87
88 #if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
89 /* Standard definition causes type-punning problems. */
90 #undef IN6_IS_ADDR_V4MAPPED
91 #define s6_addr32 __u6_addr.__u6_addr32
92 #define IN6_IS_ADDR_V4MAPPED(a)               \
93         (((a)->s6_addr32[0] == 0) &&          \
94          ((a)->s6_addr32[1] == 0) &&          \
95          ((a)->s6_addr32[2] == htonl(0x0000ffff)))
96 #endif
97
98 static int dgram_write(BIO *h, const char *buf, int num);
99 static int dgram_read(BIO *h, char *buf, int size);
100 static int dgram_puts(BIO *h, const char *str);
101 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
102 static int dgram_new(BIO *h);
103 static int dgram_free(BIO *data);
104 static int dgram_clear(BIO *bio);
105
106 #ifndef OPENSSL_NO_SCTP
107 static int dgram_sctp_write(BIO *h, const char *buf, int num);
108 static int dgram_sctp_read(BIO *h, char *buf, int size);
109 static int dgram_sctp_puts(BIO *h, const char *str);
110 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
111 static int dgram_sctp_new(BIO *h);
112 static int dgram_sctp_free(BIO *data);
113 #ifdef SCTP_AUTHENTICATION_EVENT
114 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification *snp);
115 #endif
116 #endif
117
118 static int BIO_dgram_should_retry(int s);
119
120 static void get_current_time(struct timeval *t);
121
122 static BIO_METHOD methods_dgramp=
123         {
124         BIO_TYPE_DGRAM,
125         "datagram socket",
126         dgram_write,
127         dgram_read,
128         dgram_puts,
129         NULL, /* dgram_gets, */
130         dgram_ctrl,
131         dgram_new,
132         dgram_free,
133         NULL,
134         };
135
136 #ifndef OPENSSL_NO_SCTP
137 static BIO_METHOD methods_dgramp_sctp=
138         {
139         BIO_TYPE_DGRAM_SCTP,
140         "datagram sctp socket",
141         dgram_sctp_write,
142         dgram_sctp_read,
143         dgram_sctp_puts,
144         NULL, /* dgram_gets, */
145         dgram_sctp_ctrl,
146         dgram_sctp_new,
147         dgram_sctp_free,
148         NULL,
149         };
150 #endif
151
152 typedef struct bio_dgram_data_st
153         {
154         union {
155                 struct sockaddr sa;
156                 struct sockaddr_in sa_in;
157 #if OPENSSL_USE_IPV6
158                 struct sockaddr_in6 sa_in6;
159 #endif
160         } peer;
161         unsigned int connected;
162         unsigned int _errno;
163         unsigned int mtu;
164         struct timeval next_timeout;
165         struct timeval socket_timeout;
166         } bio_dgram_data;
167
168 #ifndef OPENSSL_NO_SCTP
169 typedef struct bio_dgram_sctp_save_message_st
170         {
171         BIO *bio;
172         char *data;
173         int length;
174         } bio_dgram_sctp_save_message;
175
176 typedef struct bio_dgram_sctp_data_st
177         {
178         union {
179                 struct sockaddr sa;
180                 struct sockaddr_in sa_in;
181 #if OPENSSL_USE_IPV6
182                 struct sockaddr_in6 sa_in6;
183 #endif
184         } peer;
185         unsigned int connected;
186         unsigned int _errno;
187         unsigned int mtu;
188         struct bio_dgram_sctp_sndinfo sndinfo;
189         struct bio_dgram_sctp_rcvinfo rcvinfo;
190         struct bio_dgram_sctp_prinfo prinfo;
191         void (*handle_notifications)(BIO *bio, void *context, void *buf);
192         void* notification_context;
193         int in_handshake;
194         int ccs_rcvd;
195         int ccs_sent;
196         int save_shutdown;
197         int peer_auth_tested;
198         bio_dgram_sctp_save_message saved_message;
199         } bio_dgram_sctp_data;
200 #endif
201
202 BIO_METHOD *BIO_s_datagram(void)
203         {
204         return(&methods_dgramp);
205         }
206
207 BIO *BIO_new_dgram(int fd, int close_flag)
208         {
209         BIO *ret;
210
211         ret=BIO_new(BIO_s_datagram());
212         if (ret == NULL) return(NULL);
213         BIO_set_fd(ret,fd,close_flag);
214         return(ret);
215         }
216
217 static int dgram_new(BIO *bi)
218         {
219         bio_dgram_data *data = NULL;
220
221         bi->init=0;
222         bi->num=0;
223         data = OPENSSL_malloc(sizeof(bio_dgram_data));
224         if (data == NULL)
225                 return 0;
226         memset(data, 0x00, sizeof(bio_dgram_data));
227     bi->ptr = data;
228
229         bi->flags=0;
230         return(1);
231         }
232
233 static int dgram_free(BIO *a)
234         {
235         bio_dgram_data *data;
236
237         if (a == NULL) return(0);
238         if ( ! dgram_clear(a))
239                 return 0;
240
241         data = (bio_dgram_data *)a->ptr;
242         if(data != NULL) OPENSSL_free(data);
243
244         return(1);
245         }
246
247 static int dgram_clear(BIO *a)
248         {
249         if (a == NULL) return(0);
250         if (a->shutdown)
251                 {
252                 if (a->init)
253                         {
254                         SHUTDOWN2(a->num);
255                         }
256                 a->init=0;
257                 a->flags=0;
258                 }
259         return(1);
260         }
261
262 static void dgram_adjust_rcv_timeout(BIO *b)
263         {
264 #if defined(SO_RCVTIMEO)
265         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
266         union { size_t s; int i; } sz = {0};
267
268         /* Is a timer active? */
269         if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
270                 {
271                 struct timeval timenow, timeleft;
272
273                 /* Read current socket timeout */
274 #ifdef OPENSSL_SYS_WINDOWS
275                 int timeout;
276
277                 sz.i = sizeof(timeout);
278                 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
279                                            (void*)&timeout, &sz.i) < 0)
280                         { perror("getsockopt"); }
281                 else
282                         {
283                         data->socket_timeout.tv_sec = timeout / 1000;
284                         data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
285                         }
286 #else
287                 sz.i = sizeof(data->socket_timeout);
288                 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
289                                                 &(data->socket_timeout), (void *)&sz) < 0)
290                         { perror("getsockopt"); }
291                 else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
292                         OPENSSL_assert(sz.s<=sizeof(data->socket_timeout));
293 #endif
294
295                 /* Get current time */
296                 get_current_time(&timenow);
297
298                 /* Calculate time left until timer expires */
299                 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
300                 timeleft.tv_sec -= timenow.tv_sec;
301                 timeleft.tv_usec -= timenow.tv_usec;
302                 if (timeleft.tv_usec < 0)
303                         {
304                         timeleft.tv_sec--;
305                         timeleft.tv_usec += 1000000;
306                         }
307
308                 if (timeleft.tv_sec < 0)
309                         {
310                         timeleft.tv_sec = 0;
311                         timeleft.tv_usec = 1;
312                         }
313
314                 /* Adjust socket timeout if next handhake message timer
315                  * will expire earlier.
316                  */
317                 if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
318                         (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
319                         (data->socket_timeout.tv_sec == timeleft.tv_sec &&
320                          data->socket_timeout.tv_usec >= timeleft.tv_usec))
321                         {
322 #ifdef OPENSSL_SYS_WINDOWS
323                         timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
324                         if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
325                                                    (void*)&timeout, sizeof(timeout)) < 0)
326                                 { perror("setsockopt"); }
327 #else
328                         if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
329                                                         sizeof(struct timeval)) < 0)
330                                 { perror("setsockopt"); }
331 #endif
332                         }
333                 }
334 #endif
335         }
336
337 static void dgram_reset_rcv_timeout(BIO *b)
338         {
339 #if defined(SO_RCVTIMEO)
340         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
341
342         /* Is a timer active? */
343         if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
344                 {
345 #ifdef OPENSSL_SYS_WINDOWS
346                 int timeout = data->socket_timeout.tv_sec * 1000 +
347                                           data->socket_timeout.tv_usec / 1000;
348                 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
349                                            (void*)&timeout, sizeof(timeout)) < 0)
350                         { perror("setsockopt"); }
351 #else
352                 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
353                                                 sizeof(struct timeval)) < 0)
354                         { perror("setsockopt"); }
355 #endif
356                 }
357 #endif
358         }
359
360 static int dgram_read(BIO *b, char *out, int outl)
361         {
362         int ret=0;
363         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
364
365         struct  {
366         /*
367          * See commentary in b_sock.c. <appro>
368          */
369         union   { size_t s; int i; } len;
370         union   {
371                 struct sockaddr sa;
372                 struct sockaddr_in sa_in;
373 #if OPENSSL_USE_IPV6
374                 struct sockaddr_in6 sa_in6;
375 #endif
376                 } peer;
377         } sa;
378
379         sa.len.s=0;
380         sa.len.i=sizeof(sa.peer);
381
382         if (out != NULL)
383                 {
384                 clear_socket_error();
385                 memset(&sa.peer, 0x00, sizeof(sa.peer));
386                 dgram_adjust_rcv_timeout(b);
387                 ret=recvfrom(b->num,out,outl,0,&sa.peer.sa,(void *)&sa.len);
388                 if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
389                         {
390                         OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
391                         sa.len.i = (int)sa.len.s;
392                         }
393
394                 if ( ! data->connected  && ret >= 0)
395                         BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
396
397                 BIO_clear_retry_flags(b);
398                 if (ret < 0)
399                         {
400                         if (BIO_dgram_should_retry(ret))
401                                 {
402                                 BIO_set_retry_read(b);
403                                 data->_errno = get_last_socket_error();
404                                 }
405                         }
406
407                 dgram_reset_rcv_timeout(b);
408                 }
409         return(ret);
410         }
411
412 static int dgram_write(BIO *b, const char *in, int inl)
413         {
414         int ret;
415         bio_dgram_data *data = (bio_dgram_data *)b->ptr;
416         clear_socket_error();
417
418         if ( data->connected )
419                 ret=writesocket(b->num,in,inl);
420         else
421                 {
422                 int peerlen = sizeof(data->peer);
423
424                 if (data->peer.sa.sa_family == AF_INET)
425                         peerlen = sizeof(data->peer.sa_in);
426 #if OPENSSL_USE_IPV6
427                 else if (data->peer.sa.sa_family == AF_INET6)
428                         peerlen = sizeof(data->peer.sa_in6);
429 #endif
430 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
431                 ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
432 #else
433                 ret=sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
434 #endif
435                 }
436
437         BIO_clear_retry_flags(b);
438         if (ret <= 0)
439                 {
440                 if (BIO_dgram_should_retry(ret))
441                         {
442                         BIO_set_retry_write(b);  
443                         data->_errno = get_last_socket_error();
444
445 #if 0 /* higher layers are responsible for querying MTU, if necessary */
446                         if ( data->_errno == EMSGSIZE)
447                                 /* retrieve the new MTU */
448                                 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
449 #endif
450                         }
451                 }
452         return(ret);
453         }
454
455 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
456         {
457         long ret=1;
458         int *ip;
459         struct sockaddr *to = NULL;
460         bio_dgram_data *data = NULL;
461         int sockopt_val = 0;
462 #if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
463         socklen_t sockopt_len;  /* assume that system supporting IP_MTU is
464                                  * modern enough to define socklen_t */
465         socklen_t addr_len;
466         union   {
467                 struct sockaddr sa;
468                 struct sockaddr_in s4;
469 #if OPENSSL_USE_IPV6
470                 struct sockaddr_in6 s6;
471 #endif
472                 } addr;
473 #endif
474
475         data = (bio_dgram_data *)b->ptr;
476
477         switch (cmd)
478                 {
479         case BIO_CTRL_RESET:
480                 num=0;
481         case BIO_C_FILE_SEEK:
482                 ret=0;
483                 break;
484         case BIO_C_FILE_TELL:
485         case BIO_CTRL_INFO:
486                 ret=0;
487                 break;
488         case BIO_C_SET_FD:
489                 dgram_clear(b);
490                 b->num= *((int *)ptr);
491                 b->shutdown=(int)num;
492                 b->init=1;
493                 break;
494         case BIO_C_GET_FD:
495                 if (b->init)
496                         {
497                         ip=(int *)ptr;
498                         if (ip != NULL) *ip=b->num;
499                         ret=b->num;
500                         }
501                 else
502                         ret= -1;
503                 break;
504         case BIO_CTRL_GET_CLOSE:
505                 ret=b->shutdown;
506                 break;
507         case BIO_CTRL_SET_CLOSE:
508                 b->shutdown=(int)num;
509                 break;
510         case BIO_CTRL_PENDING:
511         case BIO_CTRL_WPENDING:
512                 ret=0;
513                 break;
514         case BIO_CTRL_DUP:
515         case BIO_CTRL_FLUSH:
516                 ret=1;
517                 break;
518         case BIO_CTRL_DGRAM_CONNECT:
519                 to = (struct sockaddr *)ptr;
520 #if 0
521                 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
522                         { perror("connect"); ret = 0; }
523                 else
524                         {
525 #endif
526                         switch (to->sa_family)
527                                 {
528                                 case AF_INET:
529                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in));
530                                         break;
531 #if OPENSSL_USE_IPV6
532                                 case AF_INET6:
533                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
534                                         break;
535 #endif
536                                 default:
537                                         memcpy(&data->peer,to,sizeof(data->peer.sa));
538                                         break;
539                                 }
540 #if 0
541                         }
542 #endif
543                 break;
544                 /* (Linux)kernel sets DF bit on outgoing IP packets */
545         case BIO_CTRL_DGRAM_MTU_DISCOVER:
546 #if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
547                 addr_len = (socklen_t)sizeof(addr);
548                 memset((void *)&addr, 0, sizeof(addr));
549                 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
550                         {
551                         ret = 0;
552                         break;
553                         }
554                 switch (addr.sa.sa_family)
555                         {
556                 case AF_INET:
557                         sockopt_val = IP_PMTUDISC_DO;
558                         if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
559                                 &sockopt_val, sizeof(sockopt_val))) < 0)
560                                 perror("setsockopt");
561                         break;
562 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
563                 case AF_INET6:
564                         sockopt_val = IPV6_PMTUDISC_DO;
565                         if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
566                                 &sockopt_val, sizeof(sockopt_val))) < 0)
567                                 perror("setsockopt");
568                         break;
569 #endif
570                 default:
571                         ret = -1;
572                         break;
573                         }
574                 ret = -1;
575 #else
576                 break;
577 #endif
578         case BIO_CTRL_DGRAM_QUERY_MTU:
579 #if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
580                 addr_len = (socklen_t)sizeof(addr);
581                 memset((void *)&addr, 0, sizeof(addr));
582                 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
583                         {
584                         ret = 0;
585                         break;
586                         }
587                 sockopt_len = sizeof(sockopt_val);
588                 switch (addr.sa.sa_family)
589                         {
590                 case AF_INET:
591                         if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
592                                 &sockopt_len)) < 0 || sockopt_val < 0)
593                                 {
594                                 ret = 0;
595                                 }
596                         else
597                                 {
598                                 /* we assume that the transport protocol is UDP and no
599                                  * IP options are used.
600                                  */
601                                 data->mtu = sockopt_val - 8 - 20;
602                                 ret = data->mtu;
603                                 }
604                         break;
605 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
606                 case AF_INET6:
607                         if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
608                                 &sockopt_len)) < 0 || sockopt_val < 0)
609                                 {
610                                 ret = 0;
611                                 }
612                         else
613                                 {
614                                 /* we assume that the transport protocol is UDP and no
615                                  * IPV6 options are used.
616                                  */
617                                 data->mtu = sockopt_val - 8 - 40;
618                                 ret = data->mtu;
619                                 }
620                         break;
621 #endif
622                 default:
623                         ret = 0;
624                         break;
625                         }
626 #else
627                 ret = 0;
628 #endif
629                 break;
630         case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
631                 switch (data->peer.sa.sa_family)
632                         {
633                         case AF_INET:
634                                 ret = 576 - 20 - 8;
635                                 break;
636 #if OPENSSL_USE_IPV6
637                         case AF_INET6:
638 #ifdef IN6_IS_ADDR_V4MAPPED
639                                 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
640                                         ret = 576 - 20 - 8;
641                                 else
642 #endif
643                                         ret = 1280 - 40 - 8;
644                                 break;
645 #endif
646                         default:
647                                 ret = 576 - 20 - 8;
648                                 break;
649                         }
650                 break;
651         case BIO_CTRL_DGRAM_GET_MTU:
652                 return data->mtu;
653                 break;
654         case BIO_CTRL_DGRAM_SET_MTU:
655                 data->mtu = num;
656                 ret = num;
657                 break;
658         case BIO_CTRL_DGRAM_SET_CONNECTED:
659                 to = (struct sockaddr *)ptr;
660
661                 if ( to != NULL)
662                         {
663                         data->connected = 1;
664                         switch (to->sa_family)
665                                 {
666                                 case AF_INET:
667                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in));
668                                         break;
669 #if OPENSSL_USE_IPV6
670                                 case AF_INET6:
671                                         memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
672                                         break;
673 #endif
674                                 default:
675                                         memcpy(&data->peer,to,sizeof(data->peer.sa));
676                                         break;
677                                 }
678                         }
679                 else
680                         {
681                         data->connected = 0;
682                         memset(&(data->peer), 0x00, sizeof(data->peer));
683                         }
684                 break;
685         case BIO_CTRL_DGRAM_GET_PEER:
686                 switch (data->peer.sa.sa_family)
687                         {
688                         case AF_INET:
689                                 ret=sizeof(data->peer.sa_in);
690                                 break;
691 #if OPENSSL_USE_IPV6
692                         case AF_INET6:
693                                 ret=sizeof(data->peer.sa_in6);
694                                 break;
695 #endif
696                         default:
697                                 ret=sizeof(data->peer.sa);
698                                 break;
699                         }
700                 if (num==0 || num>ret)
701                         num=ret;
702                 memcpy(ptr,&data->peer,(ret=num));
703                 break;
704         case BIO_CTRL_DGRAM_SET_PEER:
705                 to = (struct sockaddr *) ptr;
706                 switch (to->sa_family)
707                         {
708                         case AF_INET:
709                                 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
710                                 break;
711 #if OPENSSL_USE_IPV6
712                         case AF_INET6:
713                                 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
714                                 break;
715 #endif
716                         default:
717                                 memcpy(&data->peer,to,sizeof(data->peer.sa));
718                                 break;
719                         }
720                 break;
721         case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
722                 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
723                 break;
724 #if defined(SO_RCVTIMEO)
725         case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
726 #ifdef OPENSSL_SYS_WINDOWS
727                 {
728                 struct timeval *tv = (struct timeval *)ptr;
729                 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
730                 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
731                         (void*)&timeout, sizeof(timeout)) < 0)
732                         { perror("setsockopt"); ret = -1; }
733                 }
734 #else
735                 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
736                         sizeof(struct timeval)) < 0)
737                         { perror("setsockopt"); ret = -1; }
738 #endif
739                 break;
740         case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
741                 {
742                 union { size_t s; int i; } sz = {0};
743 #ifdef OPENSSL_SYS_WINDOWS
744                 int timeout;
745                 struct timeval *tv = (struct timeval *)ptr;
746
747                 sz.i = sizeof(timeout);
748                 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
749                         (void*)&timeout, &sz.i) < 0)
750                         { perror("getsockopt"); ret = -1; }
751                 else
752                         {
753                         tv->tv_sec = timeout / 1000;
754                         tv->tv_usec = (timeout % 1000) * 1000;
755                         ret = sizeof(*tv);
756                         }
757 #else
758                 sz.i = sizeof(struct timeval);
759                 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, 
760                         ptr, (void *)&sz) < 0)
761                         { perror("getsockopt"); ret = -1; }
762                 else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
763                         {
764                         OPENSSL_assert(sz.s<=sizeof(struct timeval));
765                         ret = (int)sz.s;
766                         }
767                 else
768                         ret = sz.i;
769 #endif
770                 }
771                 break;
772 #endif
773 #if defined(SO_SNDTIMEO)
774         case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
775 #ifdef OPENSSL_SYS_WINDOWS
776                 {
777                 struct timeval *tv = (struct timeval *)ptr;
778                 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
779                 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
780                         (void*)&timeout, sizeof(timeout)) < 0)
781                         { perror("setsockopt"); ret = -1; }
782                 }
783 #else
784                 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
785                         sizeof(struct timeval)) < 0)
786                         { perror("setsockopt"); ret = -1; }
787 #endif
788                 break;
789         case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
790                 {
791                 union { size_t s; int i; } sz = {0};
792 #ifdef OPENSSL_SYS_WINDOWS
793                 int timeout;
794                 struct timeval *tv = (struct timeval *)ptr;
795
796                 sz.i = sizeof(timeout);
797                 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
798                         (void*)&timeout, &sz.i) < 0)
799                         { perror("getsockopt"); ret = -1; }
800                 else
801                         {
802                         tv->tv_sec = timeout / 1000;
803                         tv->tv_usec = (timeout % 1000) * 1000;
804                         ret = sizeof(*tv);
805                         }
806 #else
807                 sz.i = sizeof(struct timeval);
808                 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, 
809                         ptr, (void *)&sz) < 0)
810                         { perror("getsockopt"); ret = -1; }
811                 else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
812                         {
813                         OPENSSL_assert(sz.s<=sizeof(struct timeval));
814                         ret = (int)sz.s;
815                         }
816                 else
817                         ret = sz.i;
818 #endif
819                 }
820                 break;
821 #endif
822         case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
823                 /* fall-through */
824         case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
825 #ifdef OPENSSL_SYS_WINDOWS
826                 if ( data->_errno == WSAETIMEDOUT)
827 #else
828                 if ( data->_errno == EAGAIN)
829 #endif
830                         {
831                         ret = 1;
832                         data->_errno = 0;
833                         }
834                 else
835                         ret = 0;
836                 break;
837 #ifdef EMSGSIZE
838         case BIO_CTRL_DGRAM_MTU_EXCEEDED:
839                 if ( data->_errno == EMSGSIZE)
840                         {
841                         ret = 1;
842                         data->_errno = 0;
843                         }
844                 else
845                         ret = 0;
846                 break;
847 #endif
848         case BIO_CTRL_DGRAM_SET_DONT_FRAG:
849                 sockopt_val = num ? 1 : 0;
850
851                 switch (data->peer.sa.sa_family)
852                         {
853                         case AF_INET:
854 #if defined(IP_DONTFRAG)
855                                 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
856                                         &sockopt_val, sizeof(sockopt_val))) < 0)
857                                         { perror("setsockopt"); ret = -1; }
858 #elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTUDISCOVER)
859                                 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
860                                     (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
861                                         &sockopt_val, sizeof(sockopt_val))) < 0)
862                                         { perror("setsockopt"); ret = -1; }
863 #elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
864                                 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
865                                         (const char *)&sockopt_val, sizeof(sockopt_val))) < 0)
866                                         { perror("setsockopt"); ret = -1; }
867 #else
868                                 ret = -1;
869 #endif
870                                 break;
871 #if OPENSSL_USE_IPV6 
872                         case AF_INET6:
873 #if defined(IPV6_DONTFRAG)
874                                 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
875                                         (const void *)&sockopt_val, sizeof(sockopt_val))) < 0)
876                                         { perror("setsockopt"); ret = -1; }
877 #elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
878                                 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
879                                     (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
880                                         &sockopt_val, sizeof(sockopt_val))) < 0)
881                                         { perror("setsockopt"); ret = -1; }
882 #else
883                                 ret = -1;
884 #endif
885                                 break;
886 #endif
887                         default:
888                                 ret = -1;
889                                 break;
890                         }
891                 break;
892         default:
893                 ret=0;
894                 break;
895                 }
896         return(ret);
897         }
898
899 static int dgram_puts(BIO *bp, const char *str)
900         {
901         int n,ret;
902
903         n=strlen(str);
904         ret=dgram_write(bp,str,n);
905         return(ret);
906         }
907
908 #ifndef OPENSSL_NO_SCTP
909 BIO_METHOD *BIO_s_datagram_sctp(void)
910         {
911         return(&methods_dgramp_sctp);
912         }
913
914 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
915         {
916         BIO *bio;
917         int ret, optval = 20000;
918         int auth_data = 0, auth_forward = 0;
919         unsigned char *p;
920         struct sctp_authchunk auth;
921         struct sctp_authchunks *authchunks;
922         socklen_t sockopt_len;
923 #ifdef SCTP_AUTHENTICATION_EVENT
924 #ifdef SCTP_EVENT
925         struct sctp_event event;
926 #else
927         struct sctp_event_subscribe event;
928 #endif
929 #endif
930
931         bio=BIO_new(BIO_s_datagram_sctp());
932         if (bio == NULL) return(NULL);
933         BIO_set_fd(bio,fd,close_flag);
934
935         /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
936         auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
937         ret = setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth, sizeof(struct sctp_authchunk));
938         OPENSSL_assert(ret >= 0);
939         auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
940         ret = setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth, sizeof(struct sctp_authchunk));
941         OPENSSL_assert(ret >= 0);
942
943         /* Test if activation was successful. When using accept(),
944          * SCTP-AUTH has to be activated for the listening socket
945          * already, otherwise the connected socket won't use it. */
946         sockopt_len = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
947         authchunks = OPENSSL_malloc(sockopt_len);
948         memset(authchunks, 0, sizeof(sockopt_len));
949         ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks, &sockopt_len);
950         OPENSSL_assert(ret >= 0);
951
952         for (p = (unsigned char*) authchunks->gauth_chunks;
953              p < (unsigned char*) authchunks + sockopt_len;
954              p += sizeof(uint8_t))
955                 {
956                 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE) auth_data = 1;
957                 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE) auth_forward = 1;
958                 }
959                 
960         OPENSSL_free(authchunks);
961
962         OPENSSL_assert(auth_data);
963         OPENSSL_assert(auth_forward);
964
965 #ifdef SCTP_AUTHENTICATION_EVENT
966 #ifdef SCTP_EVENT
967         memset(&event, 0, sizeof(struct sctp_event));
968         event.se_assoc_id = 0;
969         event.se_type = SCTP_AUTHENTICATION_EVENT;
970         event.se_on = 1;
971         ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
972         OPENSSL_assert(ret >= 0);
973 #else
974         sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
975         ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
976         OPENSSL_assert(ret >= 0);
977
978         event.sctp_authentication_event = 1;
979
980         ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
981         OPENSSL_assert(ret >= 0);
982 #endif
983 #endif
984
985         /* Disable partial delivery by setting the min size
986          * larger than the max record size of 2^14 + 2048 + 13
987          */
988         ret = setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval, sizeof(optval));
989         OPENSSL_assert(ret >= 0);
990
991         return(bio);
992         }
993
994 int BIO_dgram_is_sctp(BIO *bio)
995         {
996         return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
997         }
998
999 static int dgram_sctp_new(BIO *bi)
1000         {
1001         bio_dgram_sctp_data *data = NULL;
1002
1003         bi->init=0;
1004         bi->num=0;
1005         data = OPENSSL_malloc(sizeof(bio_dgram_sctp_data));
1006         if (data == NULL)
1007                 return 0;
1008         memset(data, 0x00, sizeof(bio_dgram_sctp_data));
1009 #ifdef SCTP_PR_SCTP_NONE
1010         data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1011 #endif
1012     bi->ptr = data;
1013
1014         bi->flags=0;
1015         return(1);
1016         }
1017
1018 static int dgram_sctp_free(BIO *a)
1019         {
1020         bio_dgram_sctp_data *data;
1021
1022         if (a == NULL) return(0);
1023         if ( ! dgram_clear(a))
1024                 return 0;
1025
1026         data = (bio_dgram_sctp_data *)a->ptr;
1027         if(data != NULL) OPENSSL_free(data);
1028
1029         return(1);
1030         }
1031
1032 #ifdef SCTP_AUTHENTICATION_EVENT
1033 void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification *snp)
1034         {
1035         int ret;
1036         struct sctp_authkey_event* authkeyevent = &snp->sn_auth_event;
1037
1038         if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY)
1039                 {
1040                 struct sctp_authkeyid authkeyid;
1041
1042                 /* delete key */
1043                 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1044                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1045                       &authkeyid, sizeof(struct sctp_authkeyid));
1046                 }
1047         }
1048 #endif
1049
1050 static int dgram_sctp_read(BIO *b, char *out, int outl)
1051         {
1052         int ret = 0, n = 0, i, optval;
1053         socklen_t optlen;
1054         bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1055         union sctp_notification *snp;
1056         struct msghdr msg;
1057         struct iovec iov;
1058         struct cmsghdr *cmsg;
1059         char cmsgbuf[512];
1060
1061         if (out != NULL)
1062                 {
1063                 clear_socket_error();
1064
1065                 do
1066                         {
1067                         memset(&data->rcvinfo, 0x00, sizeof(struct bio_dgram_sctp_rcvinfo));
1068                         iov.iov_base = out;
1069                         iov.iov_len = outl;
1070                         msg.msg_name = NULL;
1071                         msg.msg_namelen = 0;
1072                         msg.msg_iov = &iov;
1073                         msg.msg_iovlen = 1;
1074                         msg.msg_control = cmsgbuf;
1075                         msg.msg_controllen = 512;
1076                         msg.msg_flags = 0;
1077                         n = recvmsg(b->num, &msg, 0);
1078
1079                         if (msg.msg_controllen > 0)
1080                                 {
1081                                 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1082                                         {
1083                                         if (cmsg->cmsg_level != IPPROTO_SCTP)
1084                                                 continue;
1085 #ifdef SCTP_RCVINFO
1086                                         if (cmsg->cmsg_type == SCTP_RCVINFO)
1087                                                 {
1088                                                 struct sctp_rcvinfo *rcvinfo;
1089
1090                                                 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1091                                                 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1092                                                 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1093                                                 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1094                                                 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1095                                                 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1096                                                 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1097                                                 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1098                                                 }
1099 #endif
1100 #ifdef SCTP_SNDRCV
1101                                         if (cmsg->cmsg_type == SCTP_SNDRCV)
1102                                                 {
1103                                                 struct sctp_sndrcvinfo *sndrcvinfo;
1104
1105                                                 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1106                                                 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1107                                                 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1108                                                 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1109                                                 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1110                                                 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1111                                                 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1112                                                 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1113                                                 }
1114 #endif
1115                                         }
1116                                 }
1117
1118                         if (n <= 0)
1119                                 {
1120                                 if (n < 0)
1121                                         ret = n;
1122                                 break;
1123                                 }
1124
1125                         if (msg.msg_flags & MSG_NOTIFICATION)
1126                                 {
1127                                 snp = (union sctp_notification*) out;
1128                                 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
1129                                         {
1130 #ifdef SCTP_EVENT
1131                                         struct sctp_event event;
1132 #else
1133                                         struct sctp_event_subscribe event;
1134                                         socklen_t eventsize;
1135 #endif
1136                                         /* If a message has been delayed until the socket
1137                                          * is dry, it can be sent now.
1138                                          */
1139                                         if (data->saved_message.length > 0)
1140                                                 {
1141                                                 dgram_sctp_write(data->saved_message.bio, data->saved_message.data,
1142                                                                  data->saved_message.length);
1143                                                 OPENSSL_free(data->saved_message.data);
1144                                                 data->saved_message.length = 0;
1145                                                 }
1146
1147                                         /* disable sender dry event */
1148 #ifdef SCTP_EVENT
1149                                         memset(&event, 0, sizeof(struct sctp_event));
1150                                         event.se_assoc_id = 0;
1151                                         event.se_type = SCTP_SENDER_DRY_EVENT;
1152                                         event.se_on = 0;
1153                                         i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1154                                         OPENSSL_assert(i >= 0);
1155 #else
1156                                         eventsize = sizeof(struct sctp_event_subscribe);
1157                                         i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1158                                         OPENSSL_assert(i >= 0);
1159
1160                                         event.sctp_sender_dry_event = 0;
1161
1162                                         i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1163                                         OPENSSL_assert(i >= 0);
1164 #endif
1165                                         }
1166
1167 #ifdef SCTP_AUTHENTICATION_EVENT
1168                                 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1169                                         dgram_sctp_handle_auth_free_key_event(b, snp);
1170 #endif
1171
1172                                 if (data->handle_notifications != NULL)
1173                                         data->handle_notifications(b, data->notification_context, (void*) out);
1174
1175                                 memset(out, 0, outl);
1176                                 }
1177                         else
1178                                 ret += n;
1179                         }
1180                 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR) && (ret < outl));
1181
1182                 if (ret > 0 && !(msg.msg_flags & MSG_EOR))
1183                         {
1184                         /* Partial message read, this should never happen! */
1185
1186                         /* The buffer was too small, this means the peer sent
1187                          * a message that was larger than allowed. */
1188                         if (ret == outl)
1189                                 return -1;
1190
1191                         /* Test if socket buffer can handle max record
1192                          * size (2^14 + 2048 + 13)
1193                          */
1194                         optlen = (socklen_t) sizeof(int);
1195                         ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1196                         OPENSSL_assert(ret >= 0);
1197                         OPENSSL_assert(optval >= 18445);
1198
1199                         /* Test if SCTP doesn't partially deliver below
1200                          * max record size (2^14 + 2048 + 13)
1201                          */
1202                         optlen = (socklen_t) sizeof(int);
1203                         ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1204                                          &optval, &optlen);
1205                         OPENSSL_assert(ret >= 0);
1206                         OPENSSL_assert(optval >= 18445);
1207
1208                         /* Partially delivered notification??? Probably a bug.... */
1209                         OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1210
1211                         /* Everything seems ok till now, so it's most likely
1212                          * a message dropped by PR-SCTP.
1213                          */
1214                         memset(out, 0, outl);
1215                         BIO_set_retry_read(b);
1216                         return -1;
1217                         }
1218
1219                 BIO_clear_retry_flags(b);
1220                 if (ret < 0)
1221                         {
1222                         if (BIO_dgram_should_retry(ret))
1223                                 {
1224                                 BIO_set_retry_read(b);
1225                                 data->_errno = get_last_socket_error();
1226                                 }
1227                         }
1228
1229                 /* Test if peer uses SCTP-AUTH before continuing */
1230                 if (!data->peer_auth_tested)
1231                         {
1232                         int ii, auth_data = 0, auth_forward = 0;
1233                         unsigned char *p;
1234                         struct sctp_authchunks *authchunks;
1235
1236                         optlen = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1237                         authchunks = OPENSSL_malloc(optlen);
1238                         memset(authchunks, 0, sizeof(optlen));
1239                         ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS, authchunks, &optlen);
1240                         OPENSSL_assert(ii >= 0);
1241
1242                         for (p = (unsigned char*) authchunks->gauth_chunks;
1243                                  p < (unsigned char*) authchunks + optlen;
1244                                  p += sizeof(uint8_t))
1245                                 {
1246                                 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE) auth_data = 1;
1247                                 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE) auth_forward = 1;
1248                                 }
1249
1250                         OPENSSL_free(authchunks);
1251
1252                         if (!auth_data || !auth_forward)
1253                                 {
1254                                 BIOerr(BIO_F_DGRAM_SCTP_READ,BIO_R_CONNECT_ERROR);
1255                                 return -1;
1256                                 }
1257
1258                         data->peer_auth_tested = 1;
1259                         }
1260                 }
1261         return(ret);
1262         }
1263
1264 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1265         {
1266         int ret;
1267         bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1268         struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1269         struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1270         struct bio_dgram_sctp_sndinfo handshake_sinfo;
1271         struct iovec iov[1];
1272         struct msghdr msg;
1273         struct cmsghdr *cmsg;
1274 #if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1275         char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) + CMSG_SPACE(sizeof(struct sctp_prinfo))];
1276         struct sctp_sndinfo *sndinfo;
1277         struct sctp_prinfo *prinfo;
1278 #else
1279         char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1280         struct sctp_sndrcvinfo *sndrcvinfo;
1281 #endif
1282
1283         clear_socket_error();
1284
1285         /* If we're send anything else than application data,
1286          * disable all user parameters and flags.
1287          */
1288         if (in[0] != 23) {
1289                 memset(&handshake_sinfo, 0x00, sizeof(struct bio_dgram_sctp_sndinfo));
1290 #ifdef SCTP_SACK_IMMEDIATELY
1291                 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1292 #endif
1293                 sinfo = &handshake_sinfo;
1294         }
1295
1296         /* If we have to send a shutdown alert message and the
1297          * socket is not dry yet, we have to save it and send it
1298          * as soon as the socket gets dry.
1299          */
1300         if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b))
1301         {
1302                 data->saved_message.bio = b;
1303                 data->saved_message.length = inl;
1304                 data->saved_message.data = OPENSSL_malloc(inl);
1305                 memcpy(data->saved_message.data, in, inl);
1306                 return inl;
1307         }
1308
1309         iov[0].iov_base = (char *)in;
1310         iov[0].iov_len = inl;
1311         msg.msg_name = NULL;
1312         msg.msg_namelen = 0;
1313         msg.msg_iov = iov;
1314         msg.msg_iovlen = 1;
1315         msg.msg_control = (caddr_t)cmsgbuf;
1316         msg.msg_controllen = 0;
1317         msg.msg_flags = 0;
1318 #if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1319         cmsg = (struct cmsghdr *)cmsgbuf;
1320         cmsg->cmsg_level = IPPROTO_SCTP;
1321         cmsg->cmsg_type = SCTP_SNDINFO;
1322         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1323         sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1324         memset(sndinfo, 0, sizeof(struct sctp_sndinfo));
1325         sndinfo->snd_sid = sinfo->snd_sid;
1326         sndinfo->snd_flags = sinfo->snd_flags;
1327         sndinfo->snd_ppid = sinfo->snd_ppid;
1328         sndinfo->snd_context = sinfo->snd_context;
1329         msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1330
1331         cmsg = (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1332         cmsg->cmsg_level = IPPROTO_SCTP;
1333         cmsg->cmsg_type = SCTP_PRINFO;
1334         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1335         prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1336         memset(prinfo, 0, sizeof(struct sctp_prinfo));
1337         prinfo->pr_policy = pinfo->pr_policy;
1338         prinfo->pr_value = pinfo->pr_value;
1339         msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1340 #else
1341         cmsg = (struct cmsghdr *)cmsgbuf;
1342         cmsg->cmsg_level = IPPROTO_SCTP;
1343         cmsg->cmsg_type = SCTP_SNDRCV;
1344         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1345         sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1346         memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
1347         sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1348         sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1349 #ifdef __FreeBSD__
1350         sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1351 #endif
1352         sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1353         sndrcvinfo->sinfo_context = sinfo->snd_context;
1354         sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1355         msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1356 #endif
1357
1358         ret = sendmsg(b->num, &msg, 0);
1359
1360         BIO_clear_retry_flags(b);
1361         if (ret <= 0)
1362                 {
1363                 if (BIO_dgram_should_retry(ret))
1364                         {
1365                         BIO_set_retry_write(b);  
1366                         data->_errno = get_last_socket_error();
1367                         }
1368                 }
1369         return(ret);
1370         }
1371
1372 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1373         {
1374         long ret=1;
1375         bio_dgram_sctp_data *data = NULL;
1376         socklen_t sockopt_len = 0;
1377         struct sctp_authkeyid authkeyid;
1378         struct sctp_authkey *authkey = NULL;
1379
1380         data = (bio_dgram_sctp_data *)b->ptr;
1381
1382         switch (cmd)
1383                 {
1384         case BIO_CTRL_DGRAM_QUERY_MTU:
1385                 /* Set to maximum (2^14)
1386                  * and ignore user input to enable transport
1387                  * protocol fragmentation.
1388                  * Returns always 2^14.
1389                  */
1390                 data->mtu = 16384;
1391                 ret = data->mtu;
1392                 break;
1393         case BIO_CTRL_DGRAM_SET_MTU:
1394                 /* Set to maximum (2^14)
1395                  * and ignore input to enable transport
1396                  * protocol fragmentation.
1397                  * Returns always 2^14.
1398                  */
1399                 data->mtu = 16384;
1400                 ret = data->mtu;
1401                 break;
1402         case BIO_CTRL_DGRAM_SET_CONNECTED:
1403         case BIO_CTRL_DGRAM_CONNECT:
1404                 /* Returns always -1. */
1405                 ret = -1;
1406                 break;
1407         case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1408                 /* SCTP doesn't need the DTLS timer
1409                  * Returns always 1.
1410                  */
1411                 break;
1412         case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1413                 if (num > 0)
1414                         data->in_handshake = 1;
1415                 else
1416                         data->in_handshake = 0;
1417
1418                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY, &data->in_handshake, sizeof(int));
1419                 break;
1420         case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1421                 /* New shared key for SCTP AUTH.
1422                  * Returns 0 on success, -1 otherwise.
1423                  */
1424
1425                 /* Get active key */
1426                 sockopt_len = sizeof(struct sctp_authkeyid);
1427                 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, &sockopt_len);
1428                 if (ret < 0) break;
1429
1430                 /* Add new key */
1431                 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1432                 authkey = OPENSSL_malloc(sockopt_len);
1433                 if (authkey == NULL)
1434                         {
1435                         ret = -1;
1436                         break;
1437                         }
1438                 memset(authkey, 0x00, sockopt_len);
1439                 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1440 #ifndef __FreeBSD__
1441                 /* This field is missing in FreeBSD 8.2 and earlier,
1442                  * and FreeBSD 8.3 and higher work without it.
1443                  */
1444                 authkey->sca_keylength = 64;
1445 #endif
1446                 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1447
1448                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey, sockopt_len);
1449                 OPENSSL_free(authkey);
1450                 authkey = NULL;
1451                 if (ret < 0) break;
1452
1453                 /* Reset active key */
1454                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1455                       &authkeyid, sizeof(struct sctp_authkeyid));
1456                 if (ret < 0) break;
1457
1458                 break;
1459         case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1460                 /* Returns 0 on success, -1 otherwise. */
1461
1462                 /* Get active key */
1463                 sockopt_len = sizeof(struct sctp_authkeyid);
1464                 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, &sockopt_len);
1465                 if (ret < 0) break;
1466
1467                 /* Set active key */
1468                 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1469                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1470                       &authkeyid, sizeof(struct sctp_authkeyid));
1471                 if (ret < 0) break;
1472
1473                 /* CCS has been sent, so remember that and fall through
1474                  * to check if we need to deactivate an old key
1475                  */
1476                 data->ccs_sent = 1;
1477
1478         case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1479                 /* Returns 0 on success, -1 otherwise. */
1480
1481                 /* Has this command really been called or is this just a fall-through? */
1482                 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1483                         data->ccs_rcvd = 1;
1484
1485                 /* CSS has been both, received and sent, so deactivate an old key */
1486                 if (data->ccs_rcvd == 1 && data->ccs_sent == 1)
1487                         {
1488                         /* Get active key */
1489                         sockopt_len = sizeof(struct sctp_authkeyid);
1490                         ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, &sockopt_len);
1491                         if (ret < 0) break;
1492
1493                         /* Deactivate key or delete second last key if
1494                          * SCTP_AUTHENTICATION_EVENT is not available.
1495                          */
1496                         authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1497 #ifdef SCTP_AUTH_DEACTIVATE_KEY
1498                         sockopt_len = sizeof(struct sctp_authkeyid);
1499                         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1500                               &authkeyid, sockopt_len);
1501                         if (ret < 0) break;
1502 #endif
1503 #ifndef SCTP_AUTHENTICATION_EVENT
1504                         if (authkeyid.scact_keynumber > 0)
1505                                 {
1506                                 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1507                                 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1508                                           &authkeyid, sizeof(struct sctp_authkeyid));
1509                                 if (ret < 0) break;
1510                                 }
1511 #endif
1512
1513                         data->ccs_rcvd = 0;
1514                         data->ccs_sent = 0;
1515                         }
1516                 break;
1517         case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1518                 /* Returns the size of the copied struct. */
1519                 if (num > (long) sizeof(struct bio_dgram_sctp_sndinfo))
1520                         num = sizeof(struct bio_dgram_sctp_sndinfo);
1521
1522                 memcpy(ptr, &(data->sndinfo), num);
1523                 ret = num;
1524                 break;
1525         case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1526                 /* Returns the size of the copied struct. */
1527                 if (num > (long) sizeof(struct bio_dgram_sctp_sndinfo))
1528                         num = sizeof(struct bio_dgram_sctp_sndinfo);
1529
1530                 memcpy(&(data->sndinfo), ptr, num);
1531                 break;
1532         case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1533                 /* Returns the size of the copied struct. */
1534                 if (num > (long) sizeof(struct bio_dgram_sctp_rcvinfo))
1535                         num = sizeof(struct bio_dgram_sctp_rcvinfo);
1536
1537                 memcpy(ptr, &data->rcvinfo, num);
1538
1539                 ret = num;
1540                 break;
1541         case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1542                 /* Returns the size of the copied struct. */
1543                 if (num > (long) sizeof(struct bio_dgram_sctp_rcvinfo))
1544                         num = sizeof(struct bio_dgram_sctp_rcvinfo);
1545
1546                 memcpy(&(data->rcvinfo), ptr, num);
1547                 break;
1548         case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1549                 /* Returns the size of the copied struct. */
1550                 if (num > (long) sizeof(struct bio_dgram_sctp_prinfo))
1551                         num = sizeof(struct bio_dgram_sctp_prinfo);
1552
1553                 memcpy(ptr, &(data->prinfo), num);
1554                 ret = num;
1555                 break;
1556         case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1557                 /* Returns the size of the copied struct. */
1558                 if (num > (long) sizeof(struct bio_dgram_sctp_prinfo))
1559                         num = sizeof(struct bio_dgram_sctp_prinfo);
1560
1561                 memcpy(&(data->prinfo), ptr, num);
1562                 break;
1563         case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1564                 /* Returns always 1. */
1565                 if (num > 0)
1566                         data->save_shutdown = 1;
1567                 else
1568                         data->save_shutdown = 0;
1569                 break;
1570
1571         default:
1572                 /* Pass to default ctrl function to
1573                  * process SCTP unspecific commands
1574                  */
1575                 ret=dgram_ctrl(b, cmd, num, ptr);
1576                 break;
1577                 }
1578         return(ret);
1579         }
1580
1581 int BIO_dgram_sctp_notification_cb(BIO *b,
1582                                    void (*handle_notifications)(BIO *bio, void *context, void *buf),
1583                                    void *context)
1584         {
1585         bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1586
1587         if (handle_notifications != NULL)
1588                 {
1589                 data->handle_notifications = handle_notifications;
1590                 data->notification_context = context;
1591                 }
1592         else
1593                 return -1;
1594
1595         return 0;
1596         }
1597
1598 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1599 {
1600         int is_dry = 0;
1601         int n, sockflags, ret;
1602         union sctp_notification snp;
1603         struct msghdr msg;
1604         struct iovec iov;
1605 #ifdef SCTP_EVENT
1606         struct sctp_event event;
1607 #else
1608         struct sctp_event_subscribe event;
1609         socklen_t eventsize;
1610 #endif
1611         bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1612
1613         /* set sender dry event */
1614 #ifdef SCTP_EVENT
1615         memset(&event, 0, sizeof(struct sctp_event));
1616         event.se_assoc_id = 0;
1617         event.se_type = SCTP_SENDER_DRY_EVENT;
1618         event.se_on = 1;
1619         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1620 #else
1621         eventsize = sizeof(struct sctp_event_subscribe);
1622         ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1623         if (ret < 0)
1624                 return -1;
1625         
1626         event.sctp_sender_dry_event = 1;
1627         
1628         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1629 #endif
1630         if (ret < 0)
1631                 return -1;
1632
1633         /* peek for notification */
1634         memset(&snp, 0x00, sizeof(union sctp_notification));
1635         iov.iov_base = (char *)&snp;
1636         iov.iov_len = sizeof(union sctp_notification);
1637         msg.msg_name = NULL;
1638         msg.msg_namelen = 0;
1639         msg.msg_iov = &iov;
1640         msg.msg_iovlen = 1;
1641         msg.msg_control = NULL;
1642         msg.msg_controllen = 0;
1643         msg.msg_flags = 0;
1644
1645         n = recvmsg(b->num, &msg, MSG_PEEK);
1646         if (n <= 0)
1647                 {
1648                 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1649                         return -1;
1650                 else
1651                         return 0;
1652                 }
1653
1654         /* if we find a notification, process it and try again if necessary */
1655         while (msg.msg_flags & MSG_NOTIFICATION)
1656                 {
1657                 memset(&snp, 0x00, sizeof(union sctp_notification));
1658                 iov.iov_base = (char *)&snp;
1659                 iov.iov_len = sizeof(union sctp_notification);
1660                 msg.msg_name = NULL;
1661                 msg.msg_namelen = 0;
1662                 msg.msg_iov = &iov;
1663                 msg.msg_iovlen = 1;
1664                 msg.msg_control = NULL;
1665                 msg.msg_controllen = 0;
1666                 msg.msg_flags = 0;
1667
1668                 n = recvmsg(b->num, &msg, 0);
1669                 if (n <= 0)
1670                         {
1671                         if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1672                                 return -1;
1673                         else
1674                                 return is_dry;
1675                         }
1676                 
1677                 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
1678                         {
1679                         is_dry = 1;
1680
1681                         /* disable sender dry event */
1682 #ifdef SCTP_EVENT
1683                         memset(&event, 0, sizeof(struct sctp_event));
1684                         event.se_assoc_id = 0;
1685                         event.se_type = SCTP_SENDER_DRY_EVENT;
1686                         event.se_on = 0;
1687                         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1688 #else
1689                         eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1690                         ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1691                         if (ret < 0)
1692                                 return -1;
1693
1694                         event.sctp_sender_dry_event = 0;
1695
1696                         ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1697 #endif
1698                         if (ret < 0)
1699                                 return -1;
1700                         }
1701
1702 #ifdef SCTP_AUTHENTICATION_EVENT
1703                 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1704                         dgram_sctp_handle_auth_free_key_event(b, &snp);
1705 #endif
1706
1707                 if (data->handle_notifications != NULL)
1708                         data->handle_notifications(b, data->notification_context, (void*) &snp);
1709
1710                 /* found notification, peek again */
1711                 memset(&snp, 0x00, sizeof(union sctp_notification));
1712                 iov.iov_base = (char *)&snp;
1713                 iov.iov_len = sizeof(union sctp_notification);
1714                 msg.msg_name = NULL;
1715                 msg.msg_namelen = 0;
1716                 msg.msg_iov = &iov;
1717                 msg.msg_iovlen = 1;
1718                 msg.msg_control = NULL;
1719                 msg.msg_controllen = 0;
1720                 msg.msg_flags = 0;
1721
1722                 /* if we have seen the dry already, don't wait */
1723                 if (is_dry)
1724                         {
1725                         sockflags = fcntl(b->num, F_GETFL, 0);
1726                         fcntl(b->num, F_SETFL, O_NONBLOCK);
1727                         }
1728
1729                 n = recvmsg(b->num, &msg, MSG_PEEK);
1730
1731                 if (is_dry)
1732                         {
1733                         fcntl(b->num, F_SETFL, sockflags);
1734                         }
1735
1736                 if (n <= 0)
1737                         {
1738                         if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1739                                 return -1;
1740                         else
1741                                 return is_dry;
1742                         }
1743                 }
1744
1745         /* read anything else */
1746         return is_dry;
1747 }
1748
1749 int BIO_dgram_sctp_msg_waiting(BIO *b)
1750         {
1751         int n, sockflags;
1752         union sctp_notification snp;
1753         struct msghdr msg;
1754         struct iovec iov;
1755         bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1756
1757         /* Check if there are any messages waiting to be read */
1758         do
1759                 {
1760                 memset(&snp, 0x00, sizeof(union sctp_notification));
1761                 iov.iov_base = (char *)&snp;
1762                 iov.iov_len = sizeof(union sctp_notification);
1763                 msg.msg_name = NULL;
1764                 msg.msg_namelen = 0;
1765                 msg.msg_iov = &iov;
1766                 msg.msg_iovlen = 1;
1767                 msg.msg_control = NULL;
1768                 msg.msg_controllen = 0;
1769                 msg.msg_flags = 0;
1770
1771                 sockflags = fcntl(b->num, F_GETFL, 0);
1772                 fcntl(b->num, F_SETFL, O_NONBLOCK);
1773                 n = recvmsg(b->num, &msg, MSG_PEEK);
1774                 fcntl(b->num, F_SETFL, sockflags);
1775
1776                 /* if notification, process and try again */
1777                 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION))
1778                         {
1779 #ifdef SCTP_AUTHENTICATION_EVENT
1780                         if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1781                                 dgram_sctp_handle_auth_free_key_event(b, &snp);
1782 #endif
1783
1784                         memset(&snp, 0x00, sizeof(union sctp_notification));
1785                         iov.iov_base = (char *)&snp;
1786                         iov.iov_len = sizeof(union sctp_notification);
1787                         msg.msg_name = NULL;
1788                         msg.msg_namelen = 0;
1789                         msg.msg_iov = &iov;
1790                         msg.msg_iovlen = 1;
1791                         msg.msg_control = NULL;
1792                         msg.msg_controllen = 0;
1793                         msg.msg_flags = 0;
1794                         n = recvmsg(b->num, &msg, 0);
1795
1796                         if (data->handle_notifications != NULL)
1797                                 data->handle_notifications(b, data->notification_context, (void*) &snp);
1798                         }
1799
1800                 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1801
1802         /* Return 1 if there is a message to be read, return 0 otherwise. */
1803         if (n > 0)
1804                 return 1;
1805         else
1806                 return 0;
1807         }
1808
1809 static int dgram_sctp_puts(BIO *bp, const char *str)
1810         {
1811         int n,ret;
1812
1813         n=strlen(str);
1814         ret=dgram_sctp_write(bp,str,n);
1815         return(ret);
1816         }
1817 #endif
1818
1819 static int BIO_dgram_should_retry(int i)
1820         {
1821         int err;
1822
1823         if ((i == 0) || (i == -1))
1824                 {
1825                 err=get_last_socket_error();
1826
1827 #if defined(OPENSSL_SYS_WINDOWS)
1828         /* If the socket return value (i) is -1
1829          * and err is unexpectedly 0 at this point,
1830          * the error code was overwritten by
1831          * another system call before this error
1832          * handling is called.
1833          */
1834 #endif
1835
1836                 return(BIO_dgram_non_fatal_error(err));
1837                 }
1838         return(0);
1839         }
1840
1841 int BIO_dgram_non_fatal_error(int err)
1842         {
1843         switch (err)
1844                 {
1845 #if defined(OPENSSL_SYS_WINDOWS)
1846 # if defined(WSAEWOULDBLOCK)
1847         case WSAEWOULDBLOCK:
1848 # endif
1849
1850 # if 0 /* This appears to always be an error */
1851 #  if defined(WSAENOTCONN)
1852         case WSAENOTCONN:
1853 #  endif
1854 # endif
1855 #endif
1856
1857 #ifdef EWOULDBLOCK
1858 # ifdef WSAEWOULDBLOCK
1859 #  if WSAEWOULDBLOCK != EWOULDBLOCK
1860         case EWOULDBLOCK:
1861 #  endif
1862 # else
1863         case EWOULDBLOCK:
1864 # endif
1865 #endif
1866
1867 #ifdef EINTR
1868         case EINTR:
1869 #endif
1870
1871 #ifdef EAGAIN
1872 #if EWOULDBLOCK != EAGAIN
1873         case EAGAIN:
1874 # endif
1875 #endif
1876
1877 #ifdef EPROTO
1878         case EPROTO:
1879 #endif
1880
1881 #ifdef EINPROGRESS
1882         case EINPROGRESS:
1883 #endif
1884
1885 #ifdef EALREADY
1886         case EALREADY:
1887 #endif
1888
1889                 return(1);
1890                 /* break; */
1891         default:
1892                 break;
1893                 }
1894         return(0);
1895         }
1896
1897 static void get_current_time(struct timeval *t)
1898         {
1899 #if defined(_WIN32)
1900         SYSTEMTIME st;
1901         union { unsigned __int64 ul; FILETIME ft; } now;
1902
1903         GetSystemTime(&st);
1904         SystemTimeToFileTime(&st,&now.ft);
1905 #ifdef  __MINGW32__
1906         now.ul -= 116444736000000000ULL;
1907 #else
1908         now.ul -= 116444736000000000UI64;       /* re-bias to 1/1/1970 */
1909 #endif
1910         t->tv_sec  = (long)(now.ul/10000000);
1911         t->tv_usec = ((int)(now.ul%10000000))/10;
1912 #elif defined(OPENSSL_SYS_VMS)
1913         struct timeb tb;
1914         ftime(&tb);
1915         t->tv_sec = (long)tb.time;
1916         t->tv_usec = (long)tb.millitm * 1000;
1917 #else
1918         gettimeofday(t, NULL);
1919 #endif
1920         }
1921
1922 #endif