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(_WIN32) || defined(OPENSSL_SYS_VMS))
70 # include <sys/time.h>
72 #if defined(OPENSSL_SYS_VMS)
73 #include <sys/timeb.h>
76 #ifndef OPENSSL_NO_SCTP
77 #include <netinet/sctp.h>
79 #define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
80 #define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
83 #if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
84 #define IP_MTU 14 /* linux is lame */
87 #if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
88 #define IPPROTO_IPV6 41 /* windows is lame */
91 #if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
92 /* Standard definition causes type-punning problems. */
93 #undef IN6_IS_ADDR_V4MAPPED
94 #define s6_addr32 __u6_addr.__u6_addr32
95 #define IN6_IS_ADDR_V4MAPPED(a) \
96 (((a)->s6_addr32[0] == 0) && \
97 ((a)->s6_addr32[1] == 0) && \
98 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
102 #define sock_write SockWrite /* Watt-32 uses same names */
103 #define sock_read SockRead
104 #define sock_puts SockPuts
107 static int dgram_write(BIO *h, const char *buf, int num);
108 static int dgram_read(BIO *h, char *buf, int size);
109 static int dgram_puts(BIO *h, const char *str);
110 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
111 static int dgram_new(BIO *h);
112 static int dgram_free(BIO *data);
113 static int dgram_clear(BIO *bio);
115 #ifndef OPENSSL_NO_SCTP
116 static int dgram_sctp_write(BIO *h, const char *buf, int num);
117 static int dgram_sctp_read(BIO *h, char *buf, int size);
118 static int dgram_sctp_puts(BIO *h, const char *str);
119 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
120 static int dgram_sctp_new(BIO *h);
121 static int dgram_sctp_free(BIO *data);
122 #ifdef SCTP_AUTHENTICATION_EVENT
123 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification *snp);
127 static int BIO_dgram_should_retry(int s);
129 static void get_current_time(struct timeval *t);
131 static BIO_METHOD methods_dgramp=
138 NULL, /* dgram_gets, */
145 #ifndef OPENSSL_NO_SCTP
146 static BIO_METHOD methods_dgramp_sctp=
149 "datagram sctp socket",
153 NULL, /* dgram_gets, */
161 typedef struct bio_dgram_data_st
165 struct sockaddr_in sa_in;
167 struct sockaddr_in6 sa_in6;
170 unsigned int connected;
173 struct timeval next_timeout;
174 struct timeval socket_timeout;
177 #ifndef OPENSSL_NO_SCTP
178 typedef struct bio_dgram_sctp_save_message_st
183 } bio_dgram_sctp_save_message;
185 typedef struct bio_dgram_sctp_data_st
189 struct sockaddr_in sa_in;
191 struct sockaddr_in6 sa_in6;
194 unsigned int connected;
197 struct bio_dgram_sctp_sndinfo sndinfo;
198 struct bio_dgram_sctp_rcvinfo rcvinfo;
199 struct bio_dgram_sctp_prinfo prinfo;
200 void (*handle_notifications)(BIO *bio, void *context, void *buf);
201 void* notification_context;
206 int peer_auth_tested;
207 bio_dgram_sctp_save_message saved_message;
208 } bio_dgram_sctp_data;
211 BIO_METHOD *BIO_s_datagram(void)
213 return(&methods_dgramp);
216 BIO *BIO_new_dgram(int fd, int close_flag)
220 ret=BIO_new(BIO_s_datagram());
221 if (ret == NULL) return(NULL);
222 BIO_set_fd(ret,fd,close_flag);
226 static int dgram_new(BIO *bi)
228 bio_dgram_data *data = NULL;
232 data = OPENSSL_malloc(sizeof(bio_dgram_data));
235 memset(data, 0x00, sizeof(bio_dgram_data));
242 static int dgram_free(BIO *a)
244 bio_dgram_data *data;
246 if (a == NULL) return(0);
247 if ( ! dgram_clear(a))
250 data = (bio_dgram_data *)a->ptr;
251 if(data != NULL) OPENSSL_free(data);
256 static int dgram_clear(BIO *a)
258 if (a == NULL) return(0);
271 static void dgram_adjust_rcv_timeout(BIO *b)
273 #if defined(SO_RCVTIMEO)
274 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
275 union { size_t s; int i; } sz = {0};
277 /* Is a timer active? */
278 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
280 struct timeval timenow, timeleft;
282 /* Read current socket timeout */
283 #ifdef OPENSSL_SYS_WINDOWS
286 sz.i = sizeof(timeout);
287 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
288 (void*)&timeout, &sz.i) < 0)
289 { perror("getsockopt"); }
292 data->socket_timeout.tv_sec = timeout / 1000;
293 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
296 sz.i = sizeof(data->socket_timeout);
297 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
298 &(data->socket_timeout), (void *)&sz) < 0)
299 { perror("getsockopt"); }
300 else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
301 OPENSSL_assert(sz.s<=sizeof(data->socket_timeout));
304 /* Get current time */
305 get_current_time(&timenow);
307 /* Calculate time left until timer expires */
308 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
309 timeleft.tv_sec -= timenow.tv_sec;
310 timeleft.tv_usec -= timenow.tv_usec;
311 if (timeleft.tv_usec < 0)
314 timeleft.tv_usec += 1000000;
317 if (timeleft.tv_sec < 0)
320 timeleft.tv_usec = 1;
323 /* Adjust socket timeout if next handhake message timer
324 * will expire earlier.
326 if ((data->socket_timeout.tv_sec == 0 && data->socket_timeout.tv_usec == 0) ||
327 (data->socket_timeout.tv_sec > timeleft.tv_sec) ||
328 (data->socket_timeout.tv_sec == timeleft.tv_sec &&
329 data->socket_timeout.tv_usec >= timeleft.tv_usec))
331 #ifdef OPENSSL_SYS_WINDOWS
332 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
333 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
334 (void*)&timeout, sizeof(timeout)) < 0)
335 { perror("setsockopt"); }
337 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
338 sizeof(struct timeval)) < 0)
339 { perror("setsockopt"); }
346 static void dgram_reset_rcv_timeout(BIO *b)
348 #if defined(SO_RCVTIMEO)
349 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
351 /* Is a timer active? */
352 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0)
354 #ifdef OPENSSL_SYS_WINDOWS
355 int timeout = data->socket_timeout.tv_sec * 1000 +
356 data->socket_timeout.tv_usec / 1000;
357 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
358 (void*)&timeout, sizeof(timeout)) < 0)
359 { perror("setsockopt"); }
361 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
362 sizeof(struct timeval)) < 0)
363 { perror("setsockopt"); }
369 static int dgram_read(BIO *b, char *out, int outl)
372 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
376 * See commentary in b_sock.c. <appro>
378 union { size_t s; int i; } len;
381 struct sockaddr_in sa_in;
383 struct sockaddr_in6 sa_in6;
389 sa.len.i=sizeof(sa.peer);
393 clear_socket_error();
394 memset(&sa.peer, 0x00, sizeof(sa.peer));
395 dgram_adjust_rcv_timeout(b);
396 ret=recvfrom(b->num,out,outl,0,&sa.peer.sa,(void *)&sa.len);
397 if (sizeof(sa.len.i)!=sizeof(sa.len.s) && sa.len.i==0)
399 OPENSSL_assert(sa.len.s<=sizeof(sa.peer));
400 sa.len.i = (int)sa.len.s;
403 if ( ! data->connected && ret >= 0)
404 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
406 BIO_clear_retry_flags(b);
409 if (BIO_dgram_should_retry(ret))
411 BIO_set_retry_read(b);
412 data->_errno = get_last_socket_error();
416 dgram_reset_rcv_timeout(b);
421 static int dgram_write(BIO *b, const char *in, int inl)
424 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
425 clear_socket_error();
427 if ( data->connected )
428 ret=writesocket(b->num,in,inl);
431 int peerlen = sizeof(data->peer);
433 if (data->peer.sa.sa_family == AF_INET)
434 peerlen = sizeof(data->peer.sa_in);
436 else if (data->peer.sa.sa_family == AF_INET6)
437 peerlen = sizeof(data->peer.sa_in6);
439 #if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
440 ret=sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
442 ret=sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
446 BIO_clear_retry_flags(b);
449 if (BIO_dgram_should_retry(ret))
451 BIO_set_retry_write(b);
452 data->_errno = get_last_socket_error();
454 #if 0 /* higher layers are responsible for querying MTU, if necessary */
455 if ( data->_errno == EMSGSIZE)
456 /* retrieve the new MTU */
457 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
464 static long dgram_get_mtu_overhead(bio_dgram_data *data)
468 switch (data->peer.sa.sa_family)
471 /* Assume this is UDP - 20 bytes for IP, 8 bytes for UDP */
476 #ifdef IN6_IS_ADDR_V4MAPPED
477 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
478 /* Assume this is UDP - 20 bytes for IP, 8 bytes for UDP */
482 /* Assume this is UDP - 40 bytes for IP, 8 bytes for UDP */
487 /* We don't know. Go with the historical default */
494 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
498 struct sockaddr *to = NULL;
499 bio_dgram_data *data = NULL;
501 #if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
502 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
503 * modern enough to define socklen_t */
507 struct sockaddr_in s4;
509 struct sockaddr_in6 s6;
514 data = (bio_dgram_data *)b->ptr;
520 case BIO_C_FILE_SEEK:
523 case BIO_C_FILE_TELL:
529 b->num= *((int *)ptr);
530 b->shutdown=(int)num;
537 if (ip != NULL) *ip=b->num;
543 case BIO_CTRL_GET_CLOSE:
546 case BIO_CTRL_SET_CLOSE:
547 b->shutdown=(int)num;
549 case BIO_CTRL_PENDING:
550 case BIO_CTRL_WPENDING:
557 case BIO_CTRL_DGRAM_CONNECT:
558 to = (struct sockaddr *)ptr;
560 if (connect(b->num, to, sizeof(struct sockaddr)) < 0)
561 { perror("connect"); ret = 0; }
565 switch (to->sa_family)
568 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
572 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
576 memcpy(&data->peer,to,sizeof(data->peer.sa));
583 /* (Linux)kernel sets DF bit on outgoing IP packets */
584 case BIO_CTRL_DGRAM_MTU_DISCOVER:
585 #if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
586 addr_len = (socklen_t)sizeof(addr);
587 memset((void *)&addr, 0, sizeof(addr));
588 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
593 switch (addr.sa.sa_family)
596 sockopt_val = IP_PMTUDISC_DO;
597 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
598 &sockopt_val, sizeof(sockopt_val))) < 0)
599 perror("setsockopt");
601 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
603 sockopt_val = IPV6_PMTUDISC_DO;
604 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
605 &sockopt_val, sizeof(sockopt_val))) < 0)
606 perror("setsockopt");
617 case BIO_CTRL_DGRAM_QUERY_MTU:
618 #if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
619 addr_len = (socklen_t)sizeof(addr);
620 memset((void *)&addr, 0, sizeof(addr));
621 if (getsockname(b->num, &addr.sa, &addr_len) < 0)
626 sockopt_len = sizeof(sockopt_val);
627 switch (addr.sa.sa_family)
630 if ((ret = getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
631 &sockopt_len)) < 0 || sockopt_val < 0)
637 /* we assume that the transport protocol is UDP and no
638 * IP options are used.
640 data->mtu = sockopt_val - 8 - 20;
644 #if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
646 if ((ret = getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU, (void *)&sockopt_val,
647 &sockopt_len)) < 0 || sockopt_val < 0)
653 /* we assume that the transport protocol is UDP and no
654 * IPV6 options are used.
656 data->mtu = sockopt_val - 8 - 40;
669 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
670 ret = -dgram_get_mtu_overhead(data);
671 switch (data->peer.sa.sa_family)
678 #ifdef IN6_IS_ADDR_V4MAPPED
679 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
691 case BIO_CTRL_DGRAM_GET_MTU:
694 case BIO_CTRL_DGRAM_SET_MTU:
698 case BIO_CTRL_DGRAM_SET_CONNECTED:
699 to = (struct sockaddr *)ptr;
704 switch (to->sa_family)
707 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
711 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
715 memcpy(&data->peer,to,sizeof(data->peer.sa));
722 memset(&(data->peer), 0x00, sizeof(data->peer));
725 case BIO_CTRL_DGRAM_GET_PEER:
726 switch (data->peer.sa.sa_family)
729 ret=sizeof(data->peer.sa_in);
733 ret=sizeof(data->peer.sa_in6);
737 ret=sizeof(data->peer.sa);
740 if (num==0 || num>ret)
742 memcpy(ptr,&data->peer,(ret=num));
744 case BIO_CTRL_DGRAM_SET_PEER:
745 to = (struct sockaddr *) ptr;
746 switch (to->sa_family)
749 memcpy(&data->peer,to,sizeof(data->peer.sa_in));
753 memcpy(&data->peer,to,sizeof(data->peer.sa_in6));
757 memcpy(&data->peer,to,sizeof(data->peer.sa));
761 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
762 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
764 #if defined(SO_RCVTIMEO)
765 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
766 #ifdef OPENSSL_SYS_WINDOWS
768 struct timeval *tv = (struct timeval *)ptr;
769 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
770 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
771 (void*)&timeout, sizeof(timeout)) < 0)
772 { perror("setsockopt"); ret = -1; }
775 if ( setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
776 sizeof(struct timeval)) < 0)
777 { perror("setsockopt"); ret = -1; }
780 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
782 union { size_t s; int i; } sz = {0};
783 #ifdef OPENSSL_SYS_WINDOWS
785 struct timeval *tv = (struct timeval *)ptr;
787 sz.i = sizeof(timeout);
788 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
789 (void*)&timeout, &sz.i) < 0)
790 { perror("getsockopt"); ret = -1; }
793 tv->tv_sec = timeout / 1000;
794 tv->tv_usec = (timeout % 1000) * 1000;
798 sz.i = sizeof(struct timeval);
799 if ( getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
800 ptr, (void *)&sz) < 0)
801 { perror("getsockopt"); ret = -1; }
802 else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
804 OPENSSL_assert(sz.s<=sizeof(struct timeval));
813 #if defined(SO_SNDTIMEO)
814 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
815 #ifdef OPENSSL_SYS_WINDOWS
817 struct timeval *tv = (struct timeval *)ptr;
818 int timeout = tv->tv_sec * 1000 + tv->tv_usec/1000;
819 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
820 (void*)&timeout, sizeof(timeout)) < 0)
821 { perror("setsockopt"); ret = -1; }
824 if ( setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
825 sizeof(struct timeval)) < 0)
826 { perror("setsockopt"); ret = -1; }
829 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
831 union { size_t s; int i; } sz = {0};
832 #ifdef OPENSSL_SYS_WINDOWS
834 struct timeval *tv = (struct timeval *)ptr;
836 sz.i = sizeof(timeout);
837 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
838 (void*)&timeout, &sz.i) < 0)
839 { perror("getsockopt"); ret = -1; }
842 tv->tv_sec = timeout / 1000;
843 tv->tv_usec = (timeout % 1000) * 1000;
847 sz.i = sizeof(struct timeval);
848 if ( getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
849 ptr, (void *)&sz) < 0)
850 { perror("getsockopt"); ret = -1; }
851 else if (sizeof(sz.s)!=sizeof(sz.i) && sz.i==0)
853 OPENSSL_assert(sz.s<=sizeof(struct timeval));
862 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
864 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
865 #ifdef OPENSSL_SYS_WINDOWS
866 if ( data->_errno == WSAETIMEDOUT)
868 if ( data->_errno == EAGAIN)
878 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
879 if ( data->_errno == EMSGSIZE)
888 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
889 sockopt_val = num ? 1 : 0;
891 switch (data->peer.sa.sa_family)
894 #if defined(IP_DONTFRAG)
895 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
896 &sockopt_val, sizeof(sockopt_val))) < 0)
897 { perror("setsockopt"); ret = -1; }
898 #elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTUDISCOVER)
899 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
900 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
901 &sockopt_val, sizeof(sockopt_val))) < 0)
902 { perror("setsockopt"); ret = -1; }
903 #elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
904 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
905 (const char *)&sockopt_val, sizeof(sockopt_val))) < 0)
906 { perror("setsockopt"); ret = -1; }
913 #if defined(IPV6_DONTFRAG)
914 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
915 (const void *)&sockopt_val, sizeof(sockopt_val))) < 0)
916 { perror("setsockopt"); ret = -1; }
917 #elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
918 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
919 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
920 &sockopt_val, sizeof(sockopt_val))) < 0)
921 { perror("setsockopt"); ret = -1; }
932 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
933 ret = dgram_get_mtu_overhead(data);
942 static int dgram_puts(BIO *bp, const char *str)
947 ret=dgram_write(bp,str,n);
951 #ifndef OPENSSL_NO_SCTP
952 BIO_METHOD *BIO_s_datagram_sctp(void)
954 return(&methods_dgramp_sctp);
957 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
960 int ret, optval = 20000;
961 int auth_data = 0, auth_forward = 0;
963 struct sctp_authchunk auth;
964 struct sctp_authchunks *authchunks;
965 socklen_t sockopt_len;
966 #ifdef SCTP_AUTHENTICATION_EVENT
968 struct sctp_event event;
970 struct sctp_event_subscribe event;
974 bio=BIO_new(BIO_s_datagram_sctp());
975 if (bio == NULL) return(NULL);
976 BIO_set_fd(bio,fd,close_flag);
978 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
979 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
980 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth, sizeof(struct sctp_authchunk));
986 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
987 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth, sizeof(struct sctp_authchunk));
994 /* Test if activation was successful. When using accept(),
995 * SCTP-AUTH has to be activated for the listening socket
996 * already, otherwise the connected socket won't use it. */
997 sockopt_len = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
998 authchunks = OPENSSL_malloc(sockopt_len);
999 memset(authchunks, 0, sockopt_len);
1000 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks, &sockopt_len);
1004 OPENSSL_free(authchunks);
1009 for (p = (unsigned char*) authchunks->gauth_chunks;
1010 p < (unsigned char*) authchunks + sockopt_len;
1011 p += sizeof(uint8_t))
1013 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE) auth_data = 1;
1014 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE) auth_forward = 1;
1017 OPENSSL_free(authchunks);
1019 OPENSSL_assert(auth_data);
1020 OPENSSL_assert(auth_forward);
1022 #ifdef SCTP_AUTHENTICATION_EVENT
1024 memset(&event, 0, sizeof(struct sctp_event));
1025 event.se_assoc_id = 0;
1026 event.se_type = SCTP_AUTHENTICATION_EVENT;
1028 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1035 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1036 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1043 event.sctp_authentication_event = 1;
1045 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1054 /* Disable partial delivery by setting the min size
1055 * larger than the max record size of 2^14 + 2048 + 13
1057 ret = setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval, sizeof(optval));
1067 int BIO_dgram_is_sctp(BIO *bio)
1069 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1072 static int dgram_sctp_new(BIO *bi)
1074 bio_dgram_sctp_data *data = NULL;
1078 data = OPENSSL_malloc(sizeof(bio_dgram_sctp_data));
1081 memset(data, 0x00, sizeof(bio_dgram_sctp_data));
1082 #ifdef SCTP_PR_SCTP_NONE
1083 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1091 static int dgram_sctp_free(BIO *a)
1093 bio_dgram_sctp_data *data;
1095 if (a == NULL) return(0);
1096 if ( ! dgram_clear(a))
1099 data = (bio_dgram_sctp_data *)a->ptr;
1100 if(data != NULL) OPENSSL_free(data);
1105 #ifdef SCTP_AUTHENTICATION_EVENT
1106 void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification *snp)
1109 struct sctp_authkey_event* authkeyevent = &snp->sn_auth_event;
1111 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY)
1113 struct sctp_authkeyid authkeyid;
1116 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1117 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1118 &authkeyid, sizeof(struct sctp_authkeyid));
1123 static int dgram_sctp_read(BIO *b, char *out, int outl)
1125 int ret = 0, n = 0, i, optval;
1127 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1128 union sctp_notification *snp;
1131 struct cmsghdr *cmsg;
1136 clear_socket_error();
1140 memset(&data->rcvinfo, 0x00, sizeof(struct bio_dgram_sctp_rcvinfo));
1143 msg.msg_name = NULL;
1144 msg.msg_namelen = 0;
1147 msg.msg_control = cmsgbuf;
1148 msg.msg_controllen = 512;
1150 n = recvmsg(b->num, &msg, 0);
1159 if (msg.msg_controllen > 0)
1161 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg))
1163 if (cmsg->cmsg_level != IPPROTO_SCTP)
1166 if (cmsg->cmsg_type == SCTP_RCVINFO)
1168 struct sctp_rcvinfo *rcvinfo;
1170 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1171 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1172 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1173 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1174 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1175 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1176 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1177 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1181 if (cmsg->cmsg_type == SCTP_SNDRCV)
1183 struct sctp_sndrcvinfo *sndrcvinfo;
1185 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1186 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1187 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1188 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1189 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1190 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1191 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1192 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1198 if (msg.msg_flags & MSG_NOTIFICATION)
1200 snp = (union sctp_notification*) out;
1201 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
1204 struct sctp_event event;
1206 struct sctp_event_subscribe event;
1207 socklen_t eventsize;
1209 /* If a message has been delayed until the socket
1210 * is dry, it can be sent now.
1212 if (data->saved_message.length > 0)
1214 dgram_sctp_write(data->saved_message.bio, data->saved_message.data,
1215 data->saved_message.length);
1216 OPENSSL_free(data->saved_message.data);
1217 data->saved_message.length = 0;
1220 /* disable sender dry event */
1222 memset(&event, 0, sizeof(struct sctp_event));
1223 event.se_assoc_id = 0;
1224 event.se_type = SCTP_SENDER_DRY_EVENT;
1226 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1233 eventsize = sizeof(struct sctp_event_subscribe);
1234 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1241 event.sctp_sender_dry_event = 0;
1243 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1252 #ifdef SCTP_AUTHENTICATION_EVENT
1253 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1254 dgram_sctp_handle_auth_free_key_event(b, snp);
1257 if (data->handle_notifications != NULL)
1258 data->handle_notifications(b, data->notification_context, (void*) out);
1260 memset(out, 0, outl);
1265 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR) && (ret < outl));
1267 if (ret > 0 && !(msg.msg_flags & MSG_EOR))
1269 /* Partial message read, this should never happen! */
1271 /* The buffer was too small, this means the peer sent
1272 * a message that was larger than allowed. */
1276 /* Test if socket buffer can handle max record
1277 * size (2^14 + 2048 + 13)
1279 optlen = (socklen_t) sizeof(int);
1280 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1282 OPENSSL_assert(optval >= 18445);
1284 /* Test if SCTP doesn't partially deliver below
1285 * max record size (2^14 + 2048 + 13)
1287 optlen = (socklen_t) sizeof(int);
1288 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1291 OPENSSL_assert(optval >= 18445);
1293 /* Partially delivered notification??? Probably a bug.... */
1294 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1296 /* Everything seems ok till now, so it's most likely
1297 * a message dropped by PR-SCTP.
1299 memset(out, 0, outl);
1300 BIO_set_retry_read(b);
1304 BIO_clear_retry_flags(b);
1307 if (BIO_dgram_should_retry(ret))
1309 BIO_set_retry_read(b);
1310 data->_errno = get_last_socket_error();
1314 /* Test if peer uses SCTP-AUTH before continuing */
1315 if (!data->peer_auth_tested)
1317 int ii, auth_data = 0, auth_forward = 0;
1319 struct sctp_authchunks *authchunks;
1321 optlen = (socklen_t)(sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1322 authchunks = OPENSSL_malloc(optlen);
1323 memset(authchunks, 0, optlen);
1324 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS, authchunks, &optlen);
1327 for (p = (unsigned char*) authchunks->gauth_chunks;
1328 p < (unsigned char*) authchunks + optlen;
1329 p += sizeof(uint8_t))
1331 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE) auth_data = 1;
1332 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE) auth_forward = 1;
1335 OPENSSL_free(authchunks);
1337 if (!auth_data || !auth_forward)
1339 BIOerr(BIO_F_DGRAM_SCTP_READ,BIO_R_CONNECT_ERROR);
1343 data->peer_auth_tested = 1;
1349 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1352 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1353 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1354 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1355 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1356 struct iovec iov[1];
1358 struct cmsghdr *cmsg;
1359 #if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1360 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) + CMSG_SPACE(sizeof(struct sctp_prinfo))];
1361 struct sctp_sndinfo *sndinfo;
1362 struct sctp_prinfo *prinfo;
1364 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1365 struct sctp_sndrcvinfo *sndrcvinfo;
1368 clear_socket_error();
1370 /* If we're send anything else than application data,
1371 * disable all user parameters and flags.
1374 memset(&handshake_sinfo, 0x00, sizeof(struct bio_dgram_sctp_sndinfo));
1375 #ifdef SCTP_SACK_IMMEDIATELY
1376 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1378 sinfo = &handshake_sinfo;
1381 /* If we have to send a shutdown alert message and the
1382 * socket is not dry yet, we have to save it and send it
1383 * as soon as the socket gets dry.
1385 if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b))
1387 data->saved_message.bio = b;
1388 data->saved_message.length = inl;
1389 data->saved_message.data = OPENSSL_malloc(inl);
1390 memcpy(data->saved_message.data, in, inl);
1394 iov[0].iov_base = (char *)in;
1395 iov[0].iov_len = inl;
1396 msg.msg_name = NULL;
1397 msg.msg_namelen = 0;
1400 msg.msg_control = (caddr_t)cmsgbuf;
1401 msg.msg_controllen = 0;
1403 #if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1404 cmsg = (struct cmsghdr *)cmsgbuf;
1405 cmsg->cmsg_level = IPPROTO_SCTP;
1406 cmsg->cmsg_type = SCTP_SNDINFO;
1407 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1408 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1409 memset(sndinfo, 0, sizeof(struct sctp_sndinfo));
1410 sndinfo->snd_sid = sinfo->snd_sid;
1411 sndinfo->snd_flags = sinfo->snd_flags;
1412 sndinfo->snd_ppid = sinfo->snd_ppid;
1413 sndinfo->snd_context = sinfo->snd_context;
1414 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1416 cmsg = (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1417 cmsg->cmsg_level = IPPROTO_SCTP;
1418 cmsg->cmsg_type = SCTP_PRINFO;
1419 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1420 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1421 memset(prinfo, 0, sizeof(struct sctp_prinfo));
1422 prinfo->pr_policy = pinfo->pr_policy;
1423 prinfo->pr_value = pinfo->pr_value;
1424 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1426 cmsg = (struct cmsghdr *)cmsgbuf;
1427 cmsg->cmsg_level = IPPROTO_SCTP;
1428 cmsg->cmsg_type = SCTP_SNDRCV;
1429 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1430 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1431 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
1432 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1433 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1435 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1437 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1438 sndrcvinfo->sinfo_context = sinfo->snd_context;
1439 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1440 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1443 ret = sendmsg(b->num, &msg, 0);
1445 BIO_clear_retry_flags(b);
1448 if (BIO_dgram_should_retry(ret))
1450 BIO_set_retry_write(b);
1451 data->_errno = get_last_socket_error();
1457 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1460 bio_dgram_sctp_data *data = NULL;
1461 socklen_t sockopt_len = 0;
1462 struct sctp_authkeyid authkeyid;
1463 struct sctp_authkey *authkey = NULL;
1465 data = (bio_dgram_sctp_data *)b->ptr;
1469 case BIO_CTRL_DGRAM_QUERY_MTU:
1470 /* Set to maximum (2^14)
1471 * and ignore user input to enable transport
1472 * protocol fragmentation.
1473 * Returns always 2^14.
1478 case BIO_CTRL_DGRAM_SET_MTU:
1479 /* Set to maximum (2^14)
1480 * and ignore input to enable transport
1481 * protocol fragmentation.
1482 * Returns always 2^14.
1487 case BIO_CTRL_DGRAM_SET_CONNECTED:
1488 case BIO_CTRL_DGRAM_CONNECT:
1489 /* Returns always -1. */
1492 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1493 /* SCTP doesn't need the DTLS timer
1497 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1498 /* We allow transport protocol fragmentation so this is irrelevant */
1501 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1503 data->in_handshake = 1;
1505 data->in_handshake = 0;
1507 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY, &data->in_handshake, sizeof(int));
1509 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1510 /* New shared key for SCTP AUTH.
1511 * Returns 0 on success, -1 otherwise.
1514 /* Get active key */
1515 sockopt_len = sizeof(struct sctp_authkeyid);
1516 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, &sockopt_len);
1520 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1521 authkey = OPENSSL_malloc(sockopt_len);
1522 if (authkey == NULL)
1527 memset(authkey, 0x00, sockopt_len);
1528 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1530 /* This field is missing in FreeBSD 8.2 and earlier,
1531 * and FreeBSD 8.3 and higher work without it.
1533 authkey->sca_keylength = 64;
1535 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1537 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey, sockopt_len);
1538 OPENSSL_free(authkey);
1542 /* Reset active key */
1543 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1544 &authkeyid, sizeof(struct sctp_authkeyid));
1548 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1549 /* Returns 0 on success, -1 otherwise. */
1551 /* Get active key */
1552 sockopt_len = sizeof(struct sctp_authkeyid);
1553 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, &sockopt_len);
1556 /* Set active key */
1557 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1558 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1559 &authkeyid, sizeof(struct sctp_authkeyid));
1562 /* CCS has been sent, so remember that and fall through
1563 * to check if we need to deactivate an old key
1567 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1568 /* Returns 0 on success, -1 otherwise. */
1570 /* Has this command really been called or is this just a fall-through? */
1571 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1574 /* CSS has been both, received and sent, so deactivate an old key */
1575 if (data->ccs_rcvd == 1 && data->ccs_sent == 1)
1577 /* Get active key */
1578 sockopt_len = sizeof(struct sctp_authkeyid);
1579 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid, &sockopt_len);
1582 /* Deactivate key or delete second last key if
1583 * SCTP_AUTHENTICATION_EVENT is not available.
1585 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1586 #ifdef SCTP_AUTH_DEACTIVATE_KEY
1587 sockopt_len = sizeof(struct sctp_authkeyid);
1588 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1589 &authkeyid, sockopt_len);
1592 #ifndef SCTP_AUTHENTICATION_EVENT
1593 if (authkeyid.scact_keynumber > 0)
1595 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1596 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1597 &authkeyid, sizeof(struct sctp_authkeyid));
1606 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1607 /* Returns the size of the copied struct. */
1608 if (num > (long) sizeof(struct bio_dgram_sctp_sndinfo))
1609 num = sizeof(struct bio_dgram_sctp_sndinfo);
1611 memcpy(ptr, &(data->sndinfo), num);
1614 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1615 /* Returns the size of the copied struct. */
1616 if (num > (long) sizeof(struct bio_dgram_sctp_sndinfo))
1617 num = sizeof(struct bio_dgram_sctp_sndinfo);
1619 memcpy(&(data->sndinfo), ptr, num);
1621 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1622 /* Returns the size of the copied struct. */
1623 if (num > (long) sizeof(struct bio_dgram_sctp_rcvinfo))
1624 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1626 memcpy(ptr, &data->rcvinfo, num);
1630 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1631 /* Returns the size of the copied struct. */
1632 if (num > (long) sizeof(struct bio_dgram_sctp_rcvinfo))
1633 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1635 memcpy(&(data->rcvinfo), ptr, num);
1637 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1638 /* Returns the size of the copied struct. */
1639 if (num > (long) sizeof(struct bio_dgram_sctp_prinfo))
1640 num = sizeof(struct bio_dgram_sctp_prinfo);
1642 memcpy(ptr, &(data->prinfo), num);
1645 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1646 /* Returns the size of the copied struct. */
1647 if (num > (long) sizeof(struct bio_dgram_sctp_prinfo))
1648 num = sizeof(struct bio_dgram_sctp_prinfo);
1650 memcpy(&(data->prinfo), ptr, num);
1652 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1653 /* Returns always 1. */
1655 data->save_shutdown = 1;
1657 data->save_shutdown = 0;
1661 /* Pass to default ctrl function to
1662 * process SCTP unspecific commands
1664 ret=dgram_ctrl(b, cmd, num, ptr);
1670 int BIO_dgram_sctp_notification_cb(BIO *b,
1671 void (*handle_notifications)(BIO *bio, void *context, void *buf),
1674 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1676 if (handle_notifications != NULL)
1678 data->handle_notifications = handle_notifications;
1679 data->notification_context = context;
1687 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1690 int n, sockflags, ret;
1691 union sctp_notification snp;
1695 struct sctp_event event;
1697 struct sctp_event_subscribe event;
1698 socklen_t eventsize;
1700 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1702 /* set sender dry event */
1704 memset(&event, 0, sizeof(struct sctp_event));
1705 event.se_assoc_id = 0;
1706 event.se_type = SCTP_SENDER_DRY_EVENT;
1708 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1710 eventsize = sizeof(struct sctp_event_subscribe);
1711 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1715 event.sctp_sender_dry_event = 1;
1717 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1722 /* peek for notification */
1723 memset(&snp, 0x00, sizeof(union sctp_notification));
1724 iov.iov_base = (char *)&snp;
1725 iov.iov_len = sizeof(union sctp_notification);
1726 msg.msg_name = NULL;
1727 msg.msg_namelen = 0;
1730 msg.msg_control = NULL;
1731 msg.msg_controllen = 0;
1734 n = recvmsg(b->num, &msg, MSG_PEEK);
1737 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1743 /* if we find a notification, process it and try again if necessary */
1744 while (msg.msg_flags & MSG_NOTIFICATION)
1746 memset(&snp, 0x00, sizeof(union sctp_notification));
1747 iov.iov_base = (char *)&snp;
1748 iov.iov_len = sizeof(union sctp_notification);
1749 msg.msg_name = NULL;
1750 msg.msg_namelen = 0;
1753 msg.msg_control = NULL;
1754 msg.msg_controllen = 0;
1757 n = recvmsg(b->num, &msg, 0);
1760 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1766 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT)
1770 /* disable sender dry event */
1772 memset(&event, 0, sizeof(struct sctp_event));
1773 event.se_assoc_id = 0;
1774 event.se_type = SCTP_SENDER_DRY_EVENT;
1776 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event, sizeof(struct sctp_event));
1778 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1779 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1783 event.sctp_sender_dry_event = 0;
1785 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(struct sctp_event_subscribe));
1791 #ifdef SCTP_AUTHENTICATION_EVENT
1792 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1793 dgram_sctp_handle_auth_free_key_event(b, &snp);
1796 if (data->handle_notifications != NULL)
1797 data->handle_notifications(b, data->notification_context, (void*) &snp);
1799 /* found notification, peek again */
1800 memset(&snp, 0x00, sizeof(union sctp_notification));
1801 iov.iov_base = (char *)&snp;
1802 iov.iov_len = sizeof(union sctp_notification);
1803 msg.msg_name = NULL;
1804 msg.msg_namelen = 0;
1807 msg.msg_control = NULL;
1808 msg.msg_controllen = 0;
1811 /* if we have seen the dry already, don't wait */
1814 sockflags = fcntl(b->num, F_GETFL, 0);
1815 fcntl(b->num, F_SETFL, O_NONBLOCK);
1818 n = recvmsg(b->num, &msg, MSG_PEEK);
1822 fcntl(b->num, F_SETFL, sockflags);
1827 if ((n < 0) && (get_last_socket_error() != EAGAIN) && (get_last_socket_error() != EWOULDBLOCK))
1834 /* read anything else */
1838 int BIO_dgram_sctp_msg_waiting(BIO *b)
1841 union sctp_notification snp;
1844 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *)b->ptr;
1846 /* Check if there are any messages waiting to be read */
1849 memset(&snp, 0x00, sizeof(union sctp_notification));
1850 iov.iov_base = (char *)&snp;
1851 iov.iov_len = sizeof(union sctp_notification);
1852 msg.msg_name = NULL;
1853 msg.msg_namelen = 0;
1856 msg.msg_control = NULL;
1857 msg.msg_controllen = 0;
1860 sockflags = fcntl(b->num, F_GETFL, 0);
1861 fcntl(b->num, F_SETFL, O_NONBLOCK);
1862 n = recvmsg(b->num, &msg, MSG_PEEK);
1863 fcntl(b->num, F_SETFL, sockflags);
1865 /* if notification, process and try again */
1866 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION))
1868 #ifdef SCTP_AUTHENTICATION_EVENT
1869 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1870 dgram_sctp_handle_auth_free_key_event(b, &snp);
1873 memset(&snp, 0x00, sizeof(union sctp_notification));
1874 iov.iov_base = (char *)&snp;
1875 iov.iov_len = sizeof(union sctp_notification);
1876 msg.msg_name = NULL;
1877 msg.msg_namelen = 0;
1880 msg.msg_control = NULL;
1881 msg.msg_controllen = 0;
1883 n = recvmsg(b->num, &msg, 0);
1885 if (data->handle_notifications != NULL)
1886 data->handle_notifications(b, data->notification_context, (void*) &snp);
1889 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1891 /* Return 1 if there is a message to be read, return 0 otherwise. */
1898 static int dgram_sctp_puts(BIO *bp, const char *str)
1903 ret=dgram_sctp_write(bp,str,n);
1908 static int BIO_dgram_should_retry(int i)
1912 if ((i == 0) || (i == -1))
1914 err=get_last_socket_error();
1916 #if defined(OPENSSL_SYS_WINDOWS)
1917 /* If the socket return value (i) is -1
1918 * and err is unexpectedly 0 at this point,
1919 * the error code was overwritten by
1920 * another system call before this error
1921 * handling is called.
1925 return(BIO_dgram_non_fatal_error(err));
1930 int BIO_dgram_non_fatal_error(int err)
1934 #if defined(OPENSSL_SYS_WINDOWS)
1935 # if defined(WSAEWOULDBLOCK)
1936 case WSAEWOULDBLOCK:
1939 # if 0 /* This appears to always be an error */
1940 # if defined(WSAENOTCONN)
1947 # ifdef WSAEWOULDBLOCK
1948 # if WSAEWOULDBLOCK != EWOULDBLOCK
1961 #if EWOULDBLOCK != EAGAIN
1986 static void get_current_time(struct timeval *t)
1990 union { unsigned __int64 ul; FILETIME ft; } now;
1993 SystemTimeToFileTime(&st,&now.ft);
1995 now.ul -= 116444736000000000ULL;
1997 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1999 t->tv_sec = (long)(now.ul/10000000);
2000 t->tv_usec = ((int)(now.ul%10000000))/10;
2001 #elif defined(OPENSSL_SYS_VMS)
2004 t->tv_sec = (long)tb.time;
2005 t->tv_usec = (long)tb.millitm * 1000;
2007 gettimeofday(t, NULL);