1 /* crypto/bio/bio_dgram.c */
3 * DTLS implementation written by Nagendra Modadugu
4 * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
6 /* ====================================================================
7 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
66 #include <openssl/bio.h>
67 #ifndef OPENSSL_NO_DGRAM
69 #if defined(OPENSSL_SYS_VMS)
70 #include <sys/timeb.h>
73 #ifndef OPENSSL_NO_SCTP
74 #include <netinet/sctp.h>
76 #define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
77 #define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
80 #if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
81 #define IP_MTU 14 /* linux is lame */
84 #if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
85 #define IPPROTO_IPV6 41 /* windows is lame */
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)))
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);
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);
118 static int BIO_dgram_should_retry(int s);
120 static void get_current_time(struct timeval *t);
122 static BIO_METHOD methods_dgramp=
129 NULL, /* dgram_gets, */
136 #ifndef OPENSSL_NO_SCTP
137 static BIO_METHOD methods_dgramp_sctp=
140 "datagram sctp socket",
144 NULL, /* dgram_gets, */
152 typedef struct bio_dgram_data_st
156 struct sockaddr_in sa_in;
158 struct sockaddr_in6 sa_in6;
161 unsigned int connected;
164 struct timeval next_timeout;
165 struct timeval socket_timeout;
168 #ifndef OPENSSL_NO_SCTP
169 typedef struct bio_dgram_sctp_save_message_st
174 } bio_dgram_sctp_save_message;
176 typedef struct bio_dgram_sctp_data_st
180 struct sockaddr_in sa_in;
182 struct sockaddr_in6 sa_in6;
185 unsigned int connected;
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;
197 int peer_auth_tested;
198 bio_dgram_sctp_save_message saved_message;
199 } bio_dgram_sctp_data;
202 BIO_METHOD *BIO_s_datagram(void)
204 return(&methods_dgramp);
207 BIO *BIO_new_dgram(int fd, int close_flag)
211 ret=BIO_new(BIO_s_datagram());
212 if (ret == NULL) return(NULL);
213 BIO_set_fd(ret,fd,close_flag);
217 static int dgram_new(BIO *bi)
219 bio_dgram_data *data = NULL;
223 data = OPENSSL_malloc(sizeof(bio_dgram_data));
226 memset(data, 0x00, sizeof(bio_dgram_data));
233 static int dgram_free(BIO *a)
235 bio_dgram_data *data;
237 if (a == NULL) return(0);
238 if ( ! dgram_clear(a))
241 data = (bio_dgram_data *)a->ptr;
242 if(data != NULL) OPENSSL_free(data);
247 static int dgram_clear(BIO *a)
249 if (a == NULL) return(0);
262 static void dgram_adjust_rcv_timeout(BIO *b)
264 #if defined(SO_RCVTIMEO)
265 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
266 union { size_t s; int i; } sz = {0};
268 /* Is a timer active? */
269 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
271 struct timeval timenow, timeleft;
273 /* Read current socket timeout */
274 #ifdef OPENSSL_SYS_WINDOWS
277 sz.i = sizeof(timeout);
278 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
279 (void*)&timeout, &sz.i) < 0)
280 { perror("getsockopt"); }
283 data->socket_timeout.tv_sec = timeout / 1000;
284 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
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));
295 /* Get current time */
296 get_current_time(&timenow);
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)
305 timeleft.tv_usec += 1000000;
308 if (timeleft.tv_sec < 0)
311 timeleft.tv_usec = 1;
314 /* Adjust socket timeout if next handhake message timer
315 * will expire earlier.
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))
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"); }
328 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
329 sizeof(struct timeval)) < 0)
330 { perror("setsockopt"); }
337 static void dgram_reset_rcv_timeout(BIO *b)
339 #if defined(SO_RCVTIMEO)
340 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
342 /* Is a timer active? */
343 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
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"); }
352 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
353 sizeof(struct timeval)) < 0)
354 { perror("setsockopt"); }
360 static int dgram_read(BIO *b, char *out, int outl)
363 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
367 * See commentary in b_sock.c. <appro>
369 union { size_t s; int i; } len;
372 struct sockaddr_in sa_in;
374 struct sockaddr_in6 sa_in6;
380 sa.len.i=sizeof(sa.peer);
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)
390 OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
391 sa.len.i = (int)sa.len.s;
394 if ( ! data->connected && ret >= 0)
395 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
397 BIO_clear_retry_flags(b);
400 if (BIO_dgram_should_retry(ret))
402 BIO_set_retry_read(b);
403 data->_errno = get_last_socket_error();
407 dgram_reset_rcv_timeout(b);
412 static int dgram_write(BIO *b, const char *in, int inl)
415 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
416 clear_socket_error();
418 if ( data->connected )
419 ret=writesocket(b->num,in,inl);
422 int peerlen = sizeof(data->peer);
424 if (data->peer.sa.sa_family == AF_INET)
425 peerlen = sizeof(data->peer.sa_in);
427 else if (data->peer.sa.sa_family == AF_INET6)
428 peerlen = sizeof(data->peer.sa_in6);
430 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
431 ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
433 ret=sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
437 BIO_clear_retry_flags(b);
440 if (BIO_dgram_should_retry(ret))
442 BIO_set_retry_write(b);
443 data->_errno = get_last_socket_error();
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);
455 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
459 struct sockaddr *to = NULL;
460 bio_dgram_data *data = NULL;
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 */
468 struct sockaddr_in s4;
470 struct sockaddr_in6 s6;
475 data = (bio_dgram_data *)b->ptr;
481 case BIO_C_FILE_SEEK:
484 case BIO_C_FILE_TELL:
490 b->num= *((int *)ptr);
491 b->shutdown=(int)num;
498 if (ip != NULL) *ip=b->num;
504 case BIO_CTRL_GET_CLOSE:
507 case BIO_CTRL_SET_CLOSE:
508 b->shutdown=(int)num;
510 case BIO_CTRL_PENDING:
511 case BIO_CTRL_WPENDING:
518 case BIO_CTRL_DGRAM_CONNECT:
519 to = (struct sockaddr *)ptr;
521 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
522 { perror("connect"); ret = 0; }
526 switch (to->sa_family)
529 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
533 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
537 memcpy(&data->peer,to,sizeof(data->peer.sa));
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)
554 switch (addr.sa.sa_family)
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");
562 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
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");
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)
587 sockopt_len = sizeof(sockopt_val);
588 switch (addr.sa.sa_family)
591 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
592 &sockopt_len)) < 0 || sockopt_val < 0)
598 /* we assume that the transport protocol is UDP and no
599 * IP options are used.
601 data->mtu = sockopt_val - 8 - 20;
605 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
607 if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
608 &sockopt_len)) < 0 || sockopt_val < 0)
614 /* we assume that the transport protocol is UDP and no
615 * IPV6 options are used.
617 data->mtu = sockopt_val - 8 - 40;
630 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
631 switch (data->peer.sa.sa_family)
638 #ifdef IN6_IS_ADDR_V4MAPPED
639 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
651 case BIO_CTRL_DGRAM_GET_MTU:
654 case BIO_CTRL_DGRAM_SET_MTU:
658 case BIO_CTRL_DGRAM_SET_CONNECTED:
659 to = (struct sockaddr *)ptr;
664 switch (to->sa_family)
667 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
671 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
675 memcpy(&data->peer,to,sizeof(data->peer.sa));
682 memset(&(data->peer), 0x00, sizeof(data->peer));
685 case BIO_CTRL_DGRAM_GET_PEER:
686 switch (data->peer.sa.sa_family)
689 ret=sizeof(data->peer.sa_in);
693 ret=sizeof(data->peer.sa_in6);
697 ret=sizeof(data->peer.sa);
700 if (num==0 || num>ret)
702 memcpy(ptr,&data->peer,(ret=num));
704 case BIO_CTRL_DGRAM_SET_PEER:
705 to = (struct sockaddr *) ptr;
706 switch (to->sa_family)
709 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
713 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
717 memcpy(&data->peer,to,sizeof(data->peer.sa));
721 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
722 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
724 #if defined(SO_RCVTIMEO)
725 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
726 #ifdef OPENSSL_SYS_WINDOWS
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; }
735 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
736 sizeof(struct timeval)) < 0)
737 { perror("setsockopt"); ret = -1; }
740 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
742 union { size_t s; int i; } sz = {0};
743 #ifdef OPENSSL_SYS_WINDOWS
745 struct timeval *tv = (struct timeval *)ptr;
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; }
753 tv->tv_sec = timeout / 1000;
754 tv->tv_usec = (timeout % 1000) * 1000;
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)
764 OPENSSL_assert(sz.s<=sizeof(struct timeval));
773 #if defined(SO_SNDTIMEO)
774 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
775 #ifdef OPENSSL_SYS_WINDOWS
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; }
784 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
785 sizeof(struct timeval)) < 0)
786 { perror("setsockopt"); ret = -1; }
789 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
791 union { size_t s; int i; } sz = {0};
792 #ifdef OPENSSL_SYS_WINDOWS
794 struct timeval *tv = (struct timeval *)ptr;
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; }
802 tv->tv_sec = timeout / 1000;
803 tv->tv_usec = (timeout % 1000) * 1000;
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)
813 OPENSSL_assert(sz.s<=sizeof(struct timeval));
822 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
824 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
825 #ifdef OPENSSL_SYS_WINDOWS
826 if ( data->_errno == WSAETIMEDOUT)
828 if ( data->_errno == EAGAIN)
838 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
839 if ( data->_errno == EMSGSIZE)
848 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
849 sockopt_val = num ? 1 : 0;
851 switch (data->peer.sa.sa_family)
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; }
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; }
899 static int dgram_puts(BIO *bp, const char *str)
904 ret=dgram_write(bp,str,n);
908 #ifndef OPENSSL_NO_SCTP
909 BIO_METHOD *BIO_s_datagram_sctp(void)
911 return(&methods_dgramp_sctp);
914 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
917 int ret, optval = 20000;
918 int auth_data = 0, auth_forward = 0;
920 struct sctp_authchunk auth;
921 struct sctp_authchunks *authchunks;
922 socklen_t sockopt_len;
923 #ifdef SCTP_AUTHENTICATION_EVENT
925 struct sctp_event event;
927 struct sctp_event_subscribe event;
931 bio=BIO_new(BIO_s_datagram_sctp());
932 if (bio == NULL) return(NULL);
933 BIO_set_fd(bio,fd,close_flag);
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);
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);
952 for (p = (unsigned char*) authchunks->gauth_chunks;
953 p < (unsigned char*) authchunks + sockopt_len;
954 p += sizeof(uint8_t))
956 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE) auth_data = 1;
957 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE) auth_forward = 1;
960 OPENSSL_free(authchunks);
962 OPENSSL_assert(auth_data);
963 OPENSSL_assert(auth_forward);
965 #ifdef SCTP_AUTHENTICATION_EVENT
967 memset(&event, 0, sizeof(struct sctp_event));
968 event.se_assoc_id = 0;
969 event.se_type = SCTP_AUTHENTICATION_EVENT;
971 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
972 OPENSSL_assert(ret >= 0);
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);
978 event.sctp_authentication_event = 1;
980 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
981 OPENSSL_assert(ret >= 0);
985 /* Disable partial delivery by setting the min size
986 * larger than the max record size of 2^14 + 2048 + 13
988 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval, sizeof(optval));
989 OPENSSL_assert(ret >= 0);
994 int BIO_dgram_is_sctp(BIO *bio)
996 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
999 static int dgram_sctp_new(BIO *bi)
1001 bio_dgram_sctp_data *data = NULL;
1005 data = OPENSSL_malloc(sizeof(bio_dgram_sctp_data));
1008 memset(data, 0x00, sizeof(bio_dgram_sctp_data));
1009 #ifdef SCTP_PR_SCTP_NONE
1010 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1018 static int dgram_sctp_free(BIO *a)
1020 bio_dgram_sctp_data *data;
1022 if (a == NULL) return(0);
1023 if ( ! dgram_clear(a))
1026 data = (bio_dgram_sctp_data *)a->ptr;
1027 if(data != NULL) OPENSSL_free(data);
1032 #ifdef SCTP_AUTHENTICATION_EVENT
1033 void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification *snp)
1036 struct sctp_authkey_event* authkeyevent = &snp->sn_auth_event;
1038 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY)
1040 struct sctp_authkeyid authkeyid;
1043 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1044 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1045 &authkeyid, sizeof(struct sctp_authkeyid));
1050 static int dgram_sctp_read(BIO *b, char *out, int outl)
1052 int ret = 0, n = 0, i, optval;
1054 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1055 union sctp_notification *snp;
1058 struct cmsghdr *cmsg;
1063 clear_socket_error();
1067 memset(&data->rcvinfo, 0x00, sizeof(struct bio_dgram_sctp_rcvinfo));
1070 msg.msg_name = NULL;
1071 msg.msg_namelen = 0;
1074 msg.msg_control = cmsgbuf;
1075 msg.msg_controllen = 512;
1077 n = recvmsg(b->num, &msg, 0);
1079 if (msg.msg_controllen > 0)
1081 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1083 if (cmsg->cmsg_level != IPPROTO_SCTP)
1086 if (cmsg->cmsg_type == SCTP_RCVINFO)
1088 struct sctp_rcvinfo *rcvinfo;
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;
1101 if (cmsg->cmsg_type == SCTP_SNDRCV)
1103 struct sctp_sndrcvinfo *sndrcvinfo;
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;
1125 if (msg.msg_flags & MSG_NOTIFICATION)
1127 snp = (union sctp_notification*) out;
1128 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
1131 struct sctp_event event;
1133 struct sctp_event_subscribe event;
1134 socklen_t eventsize;
1136 /* If a message has been delayed until the socket
1137 * is dry, it can be sent now.
1139 if (data->saved_message.length > 0)
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;
1147 /* disable sender dry event */
1149 memset(&event, 0, sizeof(struct sctp_event));
1150 event.se_assoc_id = 0;
1151 event.se_type = SCTP_SENDER_DRY_EVENT;
1153 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1154 OPENSSL_assert(i >= 0);
1156 eventsize = sizeof(struct sctp_event_subscribe);
1157 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1158 OPENSSL_assert(i >= 0);
1160 event.sctp_sender_dry_event = 0;
1162 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1163 OPENSSL_assert(i >= 0);
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);
1172 if (data->handle_notifications != NULL)
1173 data->handle_notifications(b, data->notification_context, (void*) out);
1175 memset(out, 0, outl);
1180 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR) && (ret < outl));
1182 if (ret > 0 && !(msg.msg_flags & MSG_EOR))
1184 /* Partial message read, this should never happen! */
1186 /* The buffer was too small, this means the peer sent
1187 * a message that was larger than allowed. */
1191 /* Test if socket buffer can handle max record
1192 * size (2^14 + 2048 + 13)
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);
1199 /* Test if SCTP doesn't partially deliver below
1200 * max record size (2^14 + 2048 + 13)
1202 optlen = (socklen_t) sizeof(int);
1203 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1205 OPENSSL_assert(ret >= 0);
1206 OPENSSL_assert(optval >= 18445);
1208 /* Partially delivered notification??? Probably a bug.... */
1209 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1211 /* Everything seems ok till now, so it's most likely
1212 * a message dropped by PR-SCTP.
1214 memset(out, 0, outl);
1215 BIO_set_retry_read(b);
1219 BIO_clear_retry_flags(b);
1222 if (BIO_dgram_should_retry(ret))
1224 BIO_set_retry_read(b);
1225 data->_errno = get_last_socket_error();
1229 /* Test if peer uses SCTP-AUTH before continuing */
1230 if (!data->peer_auth_tested)
1232 int ii, auth_data = 0, auth_forward = 0;
1234 struct sctp_authchunks *authchunks;
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);
1242 for (p = (unsigned char*) authchunks->gauth_chunks;
1243 p < (unsigned char*) authchunks + optlen;
1244 p += sizeof(uint8_t))
1246 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE) auth_data = 1;
1247 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE) auth_forward = 1;
1250 OPENSSL_free(authchunks);
1252 if (!auth_data || !auth_forward)
1254 BIOerr(BIO_F_DGRAM_SCTP_READ,BIO_R_CONNECT_ERROR);
1258 data->peer_auth_tested = 1;
1264 static int dgram_sctp_write(BIO *b, const char *in, int inl)
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];
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;
1279 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1280 struct sctp_sndrcvinfo *sndrcvinfo;
1283 clear_socket_error();
1285 /* If we're send anything else than application data,
1286 * disable all user parameters and flags.
1289 memset(&handshake_sinfo, 0x00, sizeof(struct bio_dgram_sctp_sndinfo));
1290 #ifdef SCTP_SACK_IMMEDIATELY
1291 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1293 sinfo = &handshake_sinfo;
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.
1300 if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b))
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);
1309 iov[0].iov_base = (char *)in;
1310 iov[0].iov_len = inl;
1311 msg.msg_name = NULL;
1312 msg.msg_namelen = 0;
1315 msg.msg_control = (caddr_t)cmsgbuf;
1316 msg.msg_controllen = 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));
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));
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;
1350 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
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));
1358 ret = sendmsg(b->num, &msg, 0);
1360 BIO_clear_retry_flags(b);
1363 if (BIO_dgram_should_retry(ret))
1365 BIO_set_retry_write(b);
1366 data->_errno = get_last_socket_error();
1372 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1375 bio_dgram_sctp_data *data = NULL;
1376 socklen_t sockopt_len = 0;
1377 struct sctp_authkeyid authkeyid;
1378 struct sctp_authkey *authkey = NULL;
1380 data = (bio_dgram_sctp_data *)b->ptr;
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.
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.
1402 case BIO_CTRL_DGRAM_SET_CONNECTED:
1403 case BIO_CTRL_DGRAM_CONNECT:
1404 /* Returns always -1. */
1407 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1408 /* SCTP doesn't need the DTLS timer
1412 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1414 data->in_handshake = 1;
1416 data->in_handshake = 0;
1418 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY, &data->in_handshake, sizeof(int));
1420 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1421 /* New shared key for SCTP AUTH.
1422 * Returns 0 on success, -1 otherwise.
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);
1431 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1432 authkey = OPENSSL_malloc(sockopt_len);
1433 if (authkey == NULL)
1438 memset(authkey, 0x00, sockopt_len);
1439 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1441 /* This field is missing in FreeBSD 8.2 and earlier,
1442 * and FreeBSD 8.3 and higher work without it.
1444 authkey->sca_keylength = 64;
1446 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1448 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey, sockopt_len);
1449 OPENSSL_free(authkey);
1453 /* Reset active key */
1454 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1455 &authkeyid, sizeof(struct sctp_authkeyid));
1459 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1460 /* Returns 0 on success, -1 otherwise. */
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);
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));
1473 /* CCS has been sent, so remember that and fall through
1474 * to check if we need to deactivate an old key
1478 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1479 /* Returns 0 on success, -1 otherwise. */
1481 /* Has this command really been called or is this just a fall-through? */
1482 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1485 /* CSS has been both, received and sent, so deactivate an old key */
1486 if (data->ccs_rcvd == 1 && data->ccs_sent == 1)
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);
1493 /* Deactivate key or delete second last key if
1494 * SCTP_AUTHENTICATION_EVENT is not available.
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);
1503 #ifndef SCTP_AUTHENTICATION_EVENT
1504 if (authkeyid.scact_keynumber > 0)
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));
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);
1522 memcpy(ptr, &(data->sndinfo), num);
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);
1530 memcpy(&(data->sndinfo), ptr, num);
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);
1537 memcpy(ptr, &data->rcvinfo, num);
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);
1546 memcpy(&(data->rcvinfo), ptr, num);
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);
1553 memcpy(ptr, &(data->prinfo), num);
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);
1561 memcpy(&(data->prinfo), ptr, num);
1563 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1564 /* Returns always 1. */
1566 data->save_shutdown = 1;
1568 data->save_shutdown = 0;
1572 /* Pass to default ctrl function to
1573 * process SCTP unspecific commands
1575 ret=dgram_ctrl(b, cmd, num, ptr);
1581 int BIO_dgram_sctp_notification_cb(BIO *b,
1582 void (*handle_notifications)(BIO *bio, void *context, void *buf),
1585 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1587 if (handle_notifications != NULL)
1589 data->handle_notifications = handle_notifications;
1590 data->notification_context = context;
1598 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1601 int n, sockflags, ret;
1602 union sctp_notification snp;
1606 struct sctp_event event;
1608 struct sctp_event_subscribe event;
1609 socklen_t eventsize;
1611 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1613 /* set sender dry event */
1615 memset(&event, 0, sizeof(struct sctp_event));
1616 event.se_assoc_id = 0;
1617 event.se_type = SCTP_SENDER_DRY_EVENT;
1619 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1621 eventsize = sizeof(struct sctp_event_subscribe);
1622 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1626 event.sctp_sender_dry_event = 1;
1628 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
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;
1641 msg.msg_control = NULL;
1642 msg.msg_controllen = 0;
1645 n = recvmsg(b->num, &msg, MSG_PEEK);
1648 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1654 /* if we find a notification, process it and try again if necessary */
1655 while (msg.msg_flags & MSG_NOTIFICATION)
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;
1664 msg.msg_control = NULL;
1665 msg.msg_controllen = 0;
1668 n = recvmsg(b->num, &msg, 0);
1671 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1677 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
1681 /* disable sender dry event */
1683 memset(&event, 0, sizeof(struct sctp_event));
1684 event.se_assoc_id = 0;
1685 event.se_type = SCTP_SENDER_DRY_EVENT;
1687 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1689 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1690 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1694 event.sctp_sender_dry_event = 0;
1696 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
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);
1707 if (data->handle_notifications != NULL)
1708 data->handle_notifications(b, data->notification_context, (void*) &snp);
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;
1718 msg.msg_control = NULL;
1719 msg.msg_controllen = 0;
1722 /* if we have seen the dry already, don't wait */
1725 sockflags = fcntl(b->num, F_GETFL, 0);
1726 fcntl(b->num, F_SETFL, O_NONBLOCK);
1729 n = recvmsg(b->num, &msg, MSG_PEEK);
1733 fcntl(b->num, F_SETFL, sockflags);
1738 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1745 /* read anything else */
1749 int BIO_dgram_sctp_msg_waiting(BIO *b)
1752 union sctp_notification snp;
1755 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1757 /* Check if there are any messages waiting to be read */
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;
1767 msg.msg_control = NULL;
1768 msg.msg_controllen = 0;
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);
1776 /* if notification, process and try again */
1777 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION))
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);
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;
1791 msg.msg_control = NULL;
1792 msg.msg_controllen = 0;
1794 n = recvmsg(b->num, &msg, 0);
1796 if (data->handle_notifications != NULL)
1797 data->handle_notifications(b, data->notification_context, (void*) &snp);
1800 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1802 /* Return 1 if there is a message to be read, return 0 otherwise. */
1809 static int dgram_sctp_puts(BIO *bp, const char *str)
1814 ret=dgram_sctp_write(bp,str,n);
1819 static int BIO_dgram_should_retry(int i)
1823 if ((i == 0) || (i == -1))
1825 err=get_last_socket_error();
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.
1836 return(BIO_dgram_non_fatal_error(err));
1841 int BIO_dgram_non_fatal_error(int err)
1845 #if defined(OPENSSL_SYS_WINDOWS)
1846 # if defined(WSAEWOULDBLOCK)
1847 case WSAEWOULDBLOCK:
1850 # if 0 /* This appears to always be an error */
1851 # if defined(WSAENOTCONN)
1858 # ifdef WSAEWOULDBLOCK
1859 # if WSAEWOULDBLOCK != EWOULDBLOCK
1872 #if EWOULDBLOCK != EAGAIN
1897 static void get_current_time(struct timeval *t)
1901 union { unsigned __int64 ul; FILETIME ft; } now;
1904 SystemTimeToFileTime(&st,&now.ft);
1906 now.ul -= 116444736000000000ULL;
1908 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1910 t->tv_sec = (long)(now.ul/10000000);
1911 t->tv_usec = ((int)(now.ul%10000000))/10;
1912 #elif defined(OPENSSL_SYS_VMS)
1915 t->tv_sec = (long)tb.time;
1916 t->tv_usec = (long)tb.millitm * 1000;
1918 gettimeofday(t, NULL);