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).
65 #include <openssl/bio.h>
66 #ifndef OPENSSL_NO_DGRAM
68 # if !(defined(_WIN32) || defined(OPENSSL_SYS_VMS))
69 # include <sys/time.h>
71 # if defined(OPENSSL_SYS_VMS)
72 # include <sys/timeb.h>
75 # ifndef OPENSSL_NO_SCTP
76 # include <netinet/sctp.h>
78 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
79 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
82 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
83 # define IP_MTU 14 /* linux is lame */
86 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
87 # define IPPROTO_IPV6 41 /* windows is lame */
90 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
91 /* Standard definition causes type-punning problems. */
92 # undef IN6_IS_ADDR_V4MAPPED
93 # define s6_addr32 __u6_addr.__u6_addr32
94 # define IN6_IS_ADDR_V4MAPPED(a) \
95 (((a)->s6_addr32[0] == 0) && \
96 ((a)->s6_addr32[1] == 0) && \
97 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
101 # define sock_write SockWrite /* Watt-32 uses same names */
102 # define sock_read SockRead
103 # define sock_puts SockPuts
106 static int dgram_write(BIO *h, const char *buf, int num);
107 static int dgram_read(BIO *h, char *buf, int size);
108 static int dgram_puts(BIO *h, const char *str);
109 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
110 static int dgram_new(BIO *h);
111 static int dgram_free(BIO *data);
112 static int dgram_clear(BIO *bio);
114 # ifndef OPENSSL_NO_SCTP
115 static int dgram_sctp_write(BIO *h, const char *buf, int num);
116 static int dgram_sctp_read(BIO *h, char *buf, int size);
117 static int dgram_sctp_puts(BIO *h, const char *str);
118 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
119 static int dgram_sctp_new(BIO *h);
120 static int dgram_sctp_free(BIO *data);
121 # ifdef SCTP_AUTHENTICATION_EVENT
122 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
127 static int BIO_dgram_should_retry(int s);
129 static void get_current_time(struct timeval *t);
131 static BIO_METHOD methods_dgramp = {
137 NULL, /* dgram_gets, */
144 # ifndef OPENSSL_NO_SCTP
145 static BIO_METHOD methods_dgramp_sctp = {
147 "datagram sctp socket",
151 NULL, /* dgram_gets, */
159 typedef struct bio_dgram_data_st {
162 struct sockaddr_in sa_in;
163 # if OPENSSL_USE_IPV6
164 struct sockaddr_in6 sa_in6;
167 unsigned int connected;
170 struct timeval next_timeout;
171 struct timeval socket_timeout;
174 # ifndef OPENSSL_NO_SCTP
175 typedef struct bio_dgram_sctp_save_message_st {
179 } bio_dgram_sctp_save_message;
181 typedef struct bio_dgram_sctp_data_st {
184 struct sockaddr_in sa_in;
185 # if OPENSSL_USE_IPV6
186 struct sockaddr_in6 sa_in6;
189 unsigned int connected;
192 struct bio_dgram_sctp_sndinfo sndinfo;
193 struct bio_dgram_sctp_rcvinfo rcvinfo;
194 struct bio_dgram_sctp_prinfo prinfo;
195 void (*handle_notifications) (BIO *bio, void *context, void *buf);
196 void *notification_context;
201 int peer_auth_tested;
202 bio_dgram_sctp_save_message saved_message;
203 } bio_dgram_sctp_data;
206 BIO_METHOD *BIO_s_datagram(void)
208 return (&methods_dgramp);
211 BIO *BIO_new_dgram(int fd, int close_flag)
215 ret = BIO_new(BIO_s_datagram());
218 BIO_set_fd(ret, fd, close_flag);
222 static int dgram_new(BIO *bi)
224 bio_dgram_data *data = NULL;
228 data = OPENSSL_malloc(sizeof(bio_dgram_data));
231 memset(data, 0x00, sizeof(bio_dgram_data));
238 static int dgram_free(BIO *a)
240 bio_dgram_data *data;
247 data = (bio_dgram_data *)a->ptr;
254 static int dgram_clear(BIO *a)
268 static void dgram_adjust_rcv_timeout(BIO *b)
270 # if defined(SO_RCVTIMEO)
271 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
279 /* Is a timer active? */
280 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
281 struct timeval timenow, timeleft;
283 /* Read current socket timeout */
284 # ifdef OPENSSL_SYS_WINDOWS
287 sz.i = sizeof(timeout);
288 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
289 (void *)&timeout, &sz.i) < 0) {
290 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) {
313 timeleft.tv_usec += 1000000;
316 if (timeleft.tv_sec < 0) {
318 timeleft.tv_usec = 1;
322 * Adjust socket timeout if next handhake message timer will expire
325 if ((data->socket_timeout.tv_sec == 0
326 && 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)) {
330 # ifdef OPENSSL_SYS_WINDOWS
331 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
332 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
333 (void *)&timeout, sizeof(timeout)) < 0) {
334 perror("setsockopt");
337 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
338 sizeof(struct timeval)) < 0) {
339 perror("setsockopt");
347 static void dgram_reset_rcv_timeout(BIO *b)
349 # if defined(SO_RCVTIMEO)
350 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
352 /* Is a timer active? */
353 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");
363 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
364 sizeof(struct timeval)) < 0) {
365 perror("setsockopt");
372 static int dgram_read(BIO *b, char *out, int outl)
375 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
379 * See commentary in b_sock.c. <appro>
387 struct sockaddr_in sa_in;
388 # if OPENSSL_USE_IPV6
389 struct sockaddr_in6 sa_in6;
395 sa.len.i = sizeof(sa.peer);
398 clear_socket_error();
399 memset(&sa.peer, 0x00, sizeof(sa.peer));
400 dgram_adjust_rcv_timeout(b);
401 ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, (void *)&sa.len);
402 if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
403 OPENSSL_assert(sa.len.s <= sizeof(sa.peer));
404 sa.len.i = (int)sa.len.s;
407 if (!data->connected && ret >= 0)
408 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
410 BIO_clear_retry_flags(b);
412 if (BIO_dgram_should_retry(ret)) {
413 BIO_set_retry_read(b);
414 data->_errno = get_last_socket_error();
418 dgram_reset_rcv_timeout(b);
423 static int dgram_write(BIO *b, const char *in, int inl)
426 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
427 clear_socket_error();
430 ret = writesocket(b->num, in, inl);
432 int peerlen = sizeof(data->peer);
434 if (data->peer.sa.sa_family == AF_INET)
435 peerlen = sizeof(data->peer.sa_in);
436 # if OPENSSL_USE_IPV6
437 else if (data->peer.sa.sa_family == AF_INET6)
438 peerlen = sizeof(data->peer.sa_in6);
440 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
441 ret = sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
443 ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
447 BIO_clear_retry_flags(b);
449 if (BIO_dgram_should_retry(ret)) {
450 BIO_set_retry_write(b);
451 data->_errno = get_last_socket_error();
453 # if 0 /* higher layers are responsible for querying
454 * 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
475 # if OPENSSL_USE_IPV6
477 # ifdef IN6_IS_ADDR_V4MAPPED
478 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
480 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
486 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
492 /* We don't know. Go with the historical default */
499 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
503 struct sockaddr *to = NULL;
504 bio_dgram_data *data = NULL;
506 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
507 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
508 * modern enough to define socklen_t */
512 struct sockaddr_in s4;
513 # if OPENSSL_USE_IPV6
514 struct sockaddr_in6 s6;
519 data = (bio_dgram_data *)b->ptr;
524 case BIO_C_FILE_SEEK:
527 case BIO_C_FILE_TELL:
533 b->num = *((int *)ptr);
534 b->shutdown = (int)num;
546 case BIO_CTRL_GET_CLOSE:
549 case BIO_CTRL_SET_CLOSE:
550 b->shutdown = (int)num;
552 case BIO_CTRL_PENDING:
553 case BIO_CTRL_WPENDING:
560 case BIO_CTRL_DGRAM_CONNECT:
561 to = (struct sockaddr *)ptr;
563 if (connect(b->num, to, sizeof(struct sockaddr)) < 0) {
568 switch (to->sa_family) {
570 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
572 # if OPENSSL_USE_IPV6
574 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
578 memcpy(&data->peer, to, sizeof(data->peer.sa));
585 /* (Linux)kernel sets DF bit on outgoing IP packets */
586 case BIO_CTRL_DGRAM_MTU_DISCOVER:
587 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
588 addr_len = (socklen_t) sizeof(addr);
589 memset((void *)&addr, 0, sizeof(addr));
590 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
594 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) {
625 sockopt_len = sizeof(sockopt_val);
626 switch (addr.sa.sa_family) {
629 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
630 &sockopt_len)) < 0 || sockopt_val < 0) {
634 * we assume that the transport protocol is UDP and no IP
637 data->mtu = sockopt_val - 8 - 20;
641 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
644 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
645 (void *)&sockopt_val, &sockopt_len)) < 0
646 || sockopt_val < 0) {
650 * we assume that the transport protocol is UDP and no IPV6
653 data->mtu = sockopt_val - 8 - 40;
666 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
667 ret = -dgram_get_mtu_overhead(data);
668 switch (data->peer.sa.sa_family) {
672 # if OPENSSL_USE_IPV6
674 # ifdef IN6_IS_ADDR_V4MAPPED
675 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
687 case BIO_CTRL_DGRAM_GET_MTU:
689 case BIO_CTRL_DGRAM_SET_MTU:
693 case BIO_CTRL_DGRAM_SET_CONNECTED:
694 to = (struct sockaddr *)ptr;
698 switch (to->sa_family) {
700 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
702 # if OPENSSL_USE_IPV6
704 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
708 memcpy(&data->peer, to, sizeof(data->peer.sa));
713 memset(&(data->peer), 0x00, sizeof(data->peer));
716 case BIO_CTRL_DGRAM_GET_PEER:
717 switch (data->peer.sa.sa_family) {
719 ret = sizeof(data->peer.sa_in);
721 # if OPENSSL_USE_IPV6
723 ret = sizeof(data->peer.sa_in6);
727 ret = sizeof(data->peer.sa);
730 if (num == 0 || num > ret)
732 memcpy(ptr, &data->peer, (ret = num));
734 case BIO_CTRL_DGRAM_SET_PEER:
735 to = (struct sockaddr *)ptr;
736 switch (to->sa_family) {
738 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
740 # if OPENSSL_USE_IPV6
742 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
746 memcpy(&data->peer, to, sizeof(data->peer.sa));
750 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
751 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
753 # if defined(SO_RCVTIMEO)
754 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
755 # ifdef OPENSSL_SYS_WINDOWS
757 struct timeval *tv = (struct timeval *)ptr;
758 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
759 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
760 (void *)&timeout, sizeof(timeout)) < 0) {
761 perror("setsockopt");
766 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
767 sizeof(struct timeval)) < 0) {
768 perror("setsockopt");
773 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
781 # ifdef OPENSSL_SYS_WINDOWS
783 struct timeval *tv = (struct timeval *)ptr;
785 sz.i = sizeof(timeout);
786 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
787 (void *)&timeout, &sz.i) < 0) {
788 perror("getsockopt");
791 tv->tv_sec = timeout / 1000;
792 tv->tv_usec = (timeout % 1000) * 1000;
796 sz.i = sizeof(struct timeval);
797 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
798 ptr, (void *)&sz) < 0) {
799 perror("getsockopt");
801 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
802 OPENSSL_assert(sz.s <= sizeof(struct timeval));
810 # if defined(SO_SNDTIMEO)
811 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
812 # ifdef OPENSSL_SYS_WINDOWS
814 struct timeval *tv = (struct timeval *)ptr;
815 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
816 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
817 (void *)&timeout, sizeof(timeout)) < 0) {
818 perror("setsockopt");
823 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
824 sizeof(struct timeval)) < 0) {
825 perror("setsockopt");
830 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
838 # ifdef OPENSSL_SYS_WINDOWS
840 struct timeval *tv = (struct timeval *)ptr;
842 sz.i = sizeof(timeout);
843 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
844 (void *)&timeout, &sz.i) < 0) {
845 perror("getsockopt");
848 tv->tv_sec = timeout / 1000;
849 tv->tv_usec = (timeout % 1000) * 1000;
853 sz.i = sizeof(struct timeval);
854 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
855 ptr, (void *)&sz) < 0) {
856 perror("getsockopt");
858 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
859 OPENSSL_assert(sz.s <= sizeof(struct timeval));
867 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
869 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
870 # ifdef OPENSSL_SYS_WINDOWS
871 if (data->_errno == WSAETIMEDOUT)
873 if (data->_errno == EAGAIN)
882 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
883 if (data->_errno == EMSGSIZE) {
890 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
891 sockopt_val = num ? 1 : 0;
893 switch (data->peer.sa.sa_family) {
895 # if defined(IP_DONTFRAG)
896 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
897 &sockopt_val, sizeof(sockopt_val))) < 0) {
898 perror("setsockopt");
901 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTUDISCOVER)
902 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
903 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
904 &sockopt_val, sizeof(sockopt_val))) < 0) {
905 perror("setsockopt");
908 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
909 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
910 (const char *)&sockopt_val,
911 sizeof(sockopt_val))) < 0) {
912 perror("setsockopt");
919 # if OPENSSL_USE_IPV6
921 # if defined(IPV6_DONTFRAG)
922 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
923 (const void *)&sockopt_val,
924 sizeof(sockopt_val))) < 0) {
925 perror("setsockopt");
928 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
929 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
930 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
931 &sockopt_val, sizeof(sockopt_val))) < 0) {
932 perror("setsockopt");
945 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
946 ret = dgram_get_mtu_overhead(data);
955 static int dgram_puts(BIO *bp, const char *str)
960 ret = dgram_write(bp, str, n);
964 # ifndef OPENSSL_NO_SCTP
965 BIO_METHOD *BIO_s_datagram_sctp(void)
967 return (&methods_dgramp_sctp);
970 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
973 int ret, optval = 20000;
974 int auth_data = 0, auth_forward = 0;
976 struct sctp_authchunk auth;
977 struct sctp_authchunks *authchunks;
978 socklen_t sockopt_len;
979 # ifdef SCTP_AUTHENTICATION_EVENT
981 struct sctp_event event;
983 struct sctp_event_subscribe event;
987 bio = BIO_new(BIO_s_datagram_sctp());
990 BIO_set_fd(bio, fd, close_flag);
992 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
993 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
995 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
996 sizeof(struct sctp_authchunk));
1001 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
1003 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1004 sizeof(struct sctp_authchunk));
1011 * Test if activation was successful. When using accept(), SCTP-AUTH has
1012 * to be activated for the listening socket already, otherwise the
1013 * connected socket won't use it.
1015 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1016 authchunks = OPENSSL_malloc(sockopt_len);
1017 memset(authchunks, 0, sockopt_len);
1019 getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1023 OPENSSL_free(authchunks);
1028 for (p = (unsigned char *)authchunks->gauth_chunks;
1029 p < (unsigned char *)authchunks + sockopt_len;
1030 p += sizeof(uint8_t)) {
1031 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1033 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1037 OPENSSL_free(authchunks);
1039 OPENSSL_assert(auth_data);
1040 OPENSSL_assert(auth_forward);
1042 # ifdef SCTP_AUTHENTICATION_EVENT
1044 memset(&event, 0, sizeof(struct sctp_event));
1045 event.se_assoc_id = 0;
1046 event.se_type = SCTP_AUTHENTICATION_EVENT;
1049 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1050 sizeof(struct sctp_event));
1056 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1057 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1063 event.sctp_authentication_event = 1;
1066 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1067 sizeof(struct sctp_event_subscribe));
1076 * Disable partial delivery by setting the min size larger than the max
1077 * record size of 2^14 + 2048 + 13
1080 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1090 int BIO_dgram_is_sctp(BIO *bio)
1092 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1095 static int dgram_sctp_new(BIO *bi)
1097 bio_dgram_sctp_data *data = NULL;
1101 data = OPENSSL_malloc(sizeof(bio_dgram_sctp_data));
1104 memset(data, 0x00, sizeof(bio_dgram_sctp_data));
1105 # ifdef SCTP_PR_SCTP_NONE
1106 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1114 static int dgram_sctp_free(BIO *a)
1116 bio_dgram_sctp_data *data;
1120 if (!dgram_clear(a))
1123 data = (bio_dgram_sctp_data *) a->ptr;
1125 if (data->saved_message.data != NULL)
1126 OPENSSL_free(data->saved_message.data);
1133 # ifdef SCTP_AUTHENTICATION_EVENT
1134 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1135 union sctp_notification *snp)
1138 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1140 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1141 struct sctp_authkeyid authkeyid;
1144 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1145 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1146 &authkeyid, sizeof(struct sctp_authkeyid));
1151 static int dgram_sctp_read(BIO *b, char *out, int outl)
1153 int ret = 0, n = 0, i, optval;
1155 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1156 union sctp_notification *snp;
1159 struct cmsghdr *cmsg;
1163 clear_socket_error();
1166 memset(&data->rcvinfo, 0x00,
1167 sizeof(struct bio_dgram_sctp_rcvinfo));
1170 msg.msg_name = NULL;
1171 msg.msg_namelen = 0;
1174 msg.msg_control = cmsgbuf;
1175 msg.msg_controllen = 512;
1177 n = recvmsg(b->num, &msg, 0);
1185 if (msg.msg_controllen > 0) {
1186 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1187 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1188 if (cmsg->cmsg_level != IPPROTO_SCTP)
1190 # ifdef SCTP_RCVINFO
1191 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1192 struct sctp_rcvinfo *rcvinfo;
1194 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1195 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1196 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1197 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1198 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1199 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1200 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1201 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1205 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1206 struct sctp_sndrcvinfo *sndrcvinfo;
1209 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1210 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1211 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1212 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1213 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1214 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1215 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1216 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1222 if (msg.msg_flags & MSG_NOTIFICATION) {
1223 snp = (union sctp_notification *)out;
1224 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1226 struct sctp_event event;
1228 struct sctp_event_subscribe event;
1229 socklen_t eventsize;
1232 * If a message has been delayed until the socket is dry,
1233 * it can be sent now.
1235 if (data->saved_message.length > 0) {
1236 dgram_sctp_write(data->saved_message.bio,
1237 data->saved_message.data,
1238 data->saved_message.length);
1239 OPENSSL_free(data->saved_message.data);
1240 data->saved_message.data = NULL;
1241 data->saved_message.length = 0;
1244 /* disable sender dry event */
1246 memset(&event, 0, sizeof(struct sctp_event));
1247 event.se_assoc_id = 0;
1248 event.se_type = SCTP_SENDER_DRY_EVENT;
1250 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1251 sizeof(struct sctp_event));
1257 eventsize = sizeof(struct sctp_event_subscribe);
1258 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1265 event.sctp_sender_dry_event = 0;
1267 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1268 sizeof(struct sctp_event_subscribe));
1275 # ifdef SCTP_AUTHENTICATION_EVENT
1276 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1277 dgram_sctp_handle_auth_free_key_event(b, snp);
1280 if (data->handle_notifications != NULL)
1281 data->handle_notifications(b, data->notification_context,
1284 memset(out, 0, outl);
1288 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1291 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1292 /* Partial message read, this should never happen! */
1295 * The buffer was too small, this means the peer sent a message
1296 * that was larger than allowed.
1302 * Test if socket buffer can handle max record size (2^14 + 2048
1305 optlen = (socklen_t) sizeof(int);
1306 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1308 OPENSSL_assert(optval >= 18445);
1311 * Test if SCTP doesn't partially deliver below max record size
1312 * (2^14 + 2048 + 13)
1314 optlen = (socklen_t) sizeof(int);
1316 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1319 OPENSSL_assert(optval >= 18445);
1322 * Partially delivered notification??? Probably a bug....
1324 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1327 * Everything seems ok till now, so it's most likely a message
1328 * dropped by PR-SCTP.
1330 memset(out, 0, outl);
1331 BIO_set_retry_read(b);
1335 BIO_clear_retry_flags(b);
1337 if (BIO_dgram_should_retry(ret)) {
1338 BIO_set_retry_read(b);
1339 data->_errno = get_last_socket_error();
1343 /* Test if peer uses SCTP-AUTH before continuing */
1344 if (!data->peer_auth_tested) {
1345 int ii, auth_data = 0, auth_forward = 0;
1347 struct sctp_authchunks *authchunks;
1350 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1351 authchunks = OPENSSL_malloc(optlen);
1352 memset(authchunks, 0, optlen);
1353 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1354 authchunks, &optlen);
1357 for (p = (unsigned char *)authchunks->gauth_chunks;
1358 p < (unsigned char *)authchunks + optlen;
1359 p += sizeof(uint8_t)) {
1360 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1362 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1366 OPENSSL_free(authchunks);
1368 if (!auth_data || !auth_forward) {
1369 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1373 data->peer_auth_tested = 1;
1379 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1382 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1383 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1384 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1385 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1386 struct iovec iov[1];
1388 struct cmsghdr *cmsg;
1389 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1390 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1391 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1392 struct sctp_sndinfo *sndinfo;
1393 struct sctp_prinfo *prinfo;
1395 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1396 struct sctp_sndrcvinfo *sndrcvinfo;
1399 clear_socket_error();
1402 * If we're send anything else than application data, disable all user
1403 * parameters and flags.
1406 memset(&handshake_sinfo, 0x00, sizeof(struct bio_dgram_sctp_sndinfo));
1407 # ifdef SCTP_SACK_IMMEDIATELY
1408 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1410 sinfo = &handshake_sinfo;
1414 * If we have to send a shutdown alert message and the socket is not dry
1415 * yet, we have to save it and send it as soon as the socket gets dry.
1417 if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b)) {
1418 data->saved_message.bio = b;
1419 if (data->saved_message.data)
1420 OPENSSL_free(data->saved_message.data);
1421 data->saved_message.data = OPENSSL_malloc(inl);
1422 memcpy(data->saved_message.data, in, inl);
1423 data->saved_message.length = inl;
1427 iov[0].iov_base = (char *)in;
1428 iov[0].iov_len = inl;
1429 msg.msg_name = NULL;
1430 msg.msg_namelen = 0;
1433 msg.msg_control = (caddr_t) cmsgbuf;
1434 msg.msg_controllen = 0;
1436 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1437 cmsg = (struct cmsghdr *)cmsgbuf;
1438 cmsg->cmsg_level = IPPROTO_SCTP;
1439 cmsg->cmsg_type = SCTP_SNDINFO;
1440 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1441 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1442 memset(sndinfo, 0, sizeof(struct sctp_sndinfo));
1443 sndinfo->snd_sid = sinfo->snd_sid;
1444 sndinfo->snd_flags = sinfo->snd_flags;
1445 sndinfo->snd_ppid = sinfo->snd_ppid;
1446 sndinfo->snd_context = sinfo->snd_context;
1447 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1450 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1451 cmsg->cmsg_level = IPPROTO_SCTP;
1452 cmsg->cmsg_type = SCTP_PRINFO;
1453 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1454 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1455 memset(prinfo, 0, sizeof(struct sctp_prinfo));
1456 prinfo->pr_policy = pinfo->pr_policy;
1457 prinfo->pr_value = pinfo->pr_value;
1458 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1460 cmsg = (struct cmsghdr *)cmsgbuf;
1461 cmsg->cmsg_level = IPPROTO_SCTP;
1462 cmsg->cmsg_type = SCTP_SNDRCV;
1463 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1464 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1465 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
1466 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1467 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1469 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1471 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1472 sndrcvinfo->sinfo_context = sinfo->snd_context;
1473 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1474 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1477 ret = sendmsg(b->num, &msg, 0);
1479 BIO_clear_retry_flags(b);
1481 if (BIO_dgram_should_retry(ret)) {
1482 BIO_set_retry_write(b);
1483 data->_errno = get_last_socket_error();
1489 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1492 bio_dgram_sctp_data *data = NULL;
1493 socklen_t sockopt_len = 0;
1494 struct sctp_authkeyid authkeyid;
1495 struct sctp_authkey *authkey = NULL;
1497 data = (bio_dgram_sctp_data *) b->ptr;
1500 case BIO_CTRL_DGRAM_QUERY_MTU:
1502 * Set to maximum (2^14) and ignore user input to enable transport
1503 * protocol fragmentation. Returns always 2^14.
1508 case BIO_CTRL_DGRAM_SET_MTU:
1510 * Set to maximum (2^14) and ignore input to enable transport
1511 * protocol fragmentation. Returns always 2^14.
1516 case BIO_CTRL_DGRAM_SET_CONNECTED:
1517 case BIO_CTRL_DGRAM_CONNECT:
1518 /* Returns always -1. */
1521 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1523 * SCTP doesn't need the DTLS timer Returns always 1.
1526 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1528 * We allow transport protocol fragmentation so this is irrelevant
1532 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1534 data->in_handshake = 1;
1536 data->in_handshake = 0;
1539 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1540 &data->in_handshake, sizeof(int));
1542 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1544 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1547 /* Get active key */
1548 sockopt_len = sizeof(struct sctp_authkeyid);
1550 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1556 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1557 authkey = OPENSSL_malloc(sockopt_len);
1558 if (authkey == NULL) {
1562 memset(authkey, 0x00, sockopt_len);
1563 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1564 # ifndef __FreeBSD__
1566 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1567 * and higher work without it.
1569 authkey->sca_keylength = 64;
1571 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1574 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1576 OPENSSL_free(authkey);
1581 /* Reset active key */
1582 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1583 &authkeyid, sizeof(struct sctp_authkeyid));
1588 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1589 /* Returns 0 on success, -1 otherwise. */
1591 /* Get active key */
1592 sockopt_len = sizeof(struct sctp_authkeyid);
1594 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1599 /* Set active key */
1600 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1601 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1602 &authkeyid, sizeof(struct sctp_authkeyid));
1607 * CCS has been sent, so remember that and fall through to check if
1608 * we need to deactivate an old key
1612 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1613 /* Returns 0 on success, -1 otherwise. */
1616 * Has this command really been called or is this just a
1619 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1623 * CSS has been both, received and sent, so deactivate an old key
1625 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1626 /* Get active key */
1627 sockopt_len = sizeof(struct sctp_authkeyid);
1629 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1630 &authkeyid, &sockopt_len);
1635 * Deactivate key or delete second last key if
1636 * SCTP_AUTHENTICATION_EVENT is not available.
1638 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1639 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1640 sockopt_len = sizeof(struct sctp_authkeyid);
1641 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1642 &authkeyid, sockopt_len);
1646 # ifndef SCTP_AUTHENTICATION_EVENT
1647 if (authkeyid.scact_keynumber > 0) {
1648 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1649 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1650 &authkeyid, sizeof(struct sctp_authkeyid));
1660 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1661 /* Returns the size of the copied struct. */
1662 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1663 num = sizeof(struct bio_dgram_sctp_sndinfo);
1665 memcpy(ptr, &(data->sndinfo), num);
1668 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1669 /* Returns the size of the copied struct. */
1670 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1671 num = sizeof(struct bio_dgram_sctp_sndinfo);
1673 memcpy(&(data->sndinfo), ptr, num);
1675 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1676 /* Returns the size of the copied struct. */
1677 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1678 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1680 memcpy(ptr, &data->rcvinfo, num);
1684 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1685 /* Returns the size of the copied struct. */
1686 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1687 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1689 memcpy(&(data->rcvinfo), ptr, num);
1691 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1692 /* Returns the size of the copied struct. */
1693 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1694 num = sizeof(struct bio_dgram_sctp_prinfo);
1696 memcpy(ptr, &(data->prinfo), num);
1699 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1700 /* Returns the size of the copied struct. */
1701 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1702 num = sizeof(struct bio_dgram_sctp_prinfo);
1704 memcpy(&(data->prinfo), ptr, num);
1706 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1707 /* Returns always 1. */
1709 data->save_shutdown = 1;
1711 data->save_shutdown = 0;
1716 * Pass to default ctrl function to process SCTP unspecific commands
1718 ret = dgram_ctrl(b, cmd, num, ptr);
1724 int BIO_dgram_sctp_notification_cb(BIO *b,
1725 void (*handle_notifications) (BIO *bio,
1731 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1733 if (handle_notifications != NULL) {
1734 data->handle_notifications = handle_notifications;
1735 data->notification_context = context;
1742 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1745 int n, sockflags, ret;
1746 union sctp_notification snp;
1750 struct sctp_event event;
1752 struct sctp_event_subscribe event;
1753 socklen_t eventsize;
1755 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1757 /* set sender dry event */
1759 memset(&event, 0, sizeof(struct sctp_event));
1760 event.se_assoc_id = 0;
1761 event.se_type = SCTP_SENDER_DRY_EVENT;
1764 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1765 sizeof(struct sctp_event));
1767 eventsize = sizeof(struct sctp_event_subscribe);
1768 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1772 event.sctp_sender_dry_event = 1;
1775 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1776 sizeof(struct sctp_event_subscribe));
1781 /* peek for notification */
1782 memset(&snp, 0x00, sizeof(union sctp_notification));
1783 iov.iov_base = (char *)&snp;
1784 iov.iov_len = sizeof(union sctp_notification);
1785 msg.msg_name = NULL;
1786 msg.msg_namelen = 0;
1789 msg.msg_control = NULL;
1790 msg.msg_controllen = 0;
1793 n = recvmsg(b->num, &msg, MSG_PEEK);
1795 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1796 && (get_last_socket_error() != EWOULDBLOCK))
1802 /* if we find a notification, process it and try again if necessary */
1803 while (msg.msg_flags & MSG_NOTIFICATION) {
1804 memset(&snp, 0x00, sizeof(union sctp_notification));
1805 iov.iov_base = (char *)&snp;
1806 iov.iov_len = sizeof(union sctp_notification);
1807 msg.msg_name = NULL;
1808 msg.msg_namelen = 0;
1811 msg.msg_control = NULL;
1812 msg.msg_controllen = 0;
1815 n = recvmsg(b->num, &msg, 0);
1817 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1818 && (get_last_socket_error() != EWOULDBLOCK))
1824 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1827 /* disable sender dry event */
1829 memset(&event, 0, sizeof(struct sctp_event));
1830 event.se_assoc_id = 0;
1831 event.se_type = SCTP_SENDER_DRY_EVENT;
1834 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1835 sizeof(struct sctp_event));
1837 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1839 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1844 event.sctp_sender_dry_event = 0;
1847 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1848 sizeof(struct sctp_event_subscribe));
1853 # ifdef SCTP_AUTHENTICATION_EVENT
1854 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1855 dgram_sctp_handle_auth_free_key_event(b, &snp);
1858 if (data->handle_notifications != NULL)
1859 data->handle_notifications(b, data->notification_context,
1862 /* found notification, peek again */
1863 memset(&snp, 0x00, sizeof(union sctp_notification));
1864 iov.iov_base = (char *)&snp;
1865 iov.iov_len = sizeof(union sctp_notification);
1866 msg.msg_name = NULL;
1867 msg.msg_namelen = 0;
1870 msg.msg_control = NULL;
1871 msg.msg_controllen = 0;
1874 /* if we have seen the dry already, don't wait */
1876 sockflags = fcntl(b->num, F_GETFL, 0);
1877 fcntl(b->num, F_SETFL, O_NONBLOCK);
1880 n = recvmsg(b->num, &msg, MSG_PEEK);
1883 fcntl(b->num, F_SETFL, sockflags);
1887 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1888 && (get_last_socket_error() != EWOULDBLOCK))
1895 /* read anything else */
1899 int BIO_dgram_sctp_msg_waiting(BIO *b)
1902 union sctp_notification snp;
1905 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1907 /* Check if there are any messages waiting to be read */
1909 memset(&snp, 0x00, sizeof(union sctp_notification));
1910 iov.iov_base = (char *)&snp;
1911 iov.iov_len = sizeof(union sctp_notification);
1912 msg.msg_name = NULL;
1913 msg.msg_namelen = 0;
1916 msg.msg_control = NULL;
1917 msg.msg_controllen = 0;
1920 sockflags = fcntl(b->num, F_GETFL, 0);
1921 fcntl(b->num, F_SETFL, O_NONBLOCK);
1922 n = recvmsg(b->num, &msg, MSG_PEEK);
1923 fcntl(b->num, F_SETFL, sockflags);
1925 /* if notification, process and try again */
1926 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1927 # ifdef SCTP_AUTHENTICATION_EVENT
1928 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1929 dgram_sctp_handle_auth_free_key_event(b, &snp);
1932 memset(&snp, 0x00, sizeof(union sctp_notification));
1933 iov.iov_base = (char *)&snp;
1934 iov.iov_len = sizeof(union sctp_notification);
1935 msg.msg_name = NULL;
1936 msg.msg_namelen = 0;
1939 msg.msg_control = NULL;
1940 msg.msg_controllen = 0;
1942 n = recvmsg(b->num, &msg, 0);
1944 if (data->handle_notifications != NULL)
1945 data->handle_notifications(b, data->notification_context,
1949 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1951 /* Return 1 if there is a message to be read, return 0 otherwise. */
1958 static int dgram_sctp_puts(BIO *bp, const char *str)
1963 ret = dgram_sctp_write(bp, str, n);
1968 static int BIO_dgram_should_retry(int i)
1972 if ((i == 0) || (i == -1)) {
1973 err = get_last_socket_error();
1975 # if defined(OPENSSL_SYS_WINDOWS)
1977 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1978 * this point, the error code was overwritten by another system call
1979 * before this error handling is called.
1983 return (BIO_dgram_non_fatal_error(err));
1988 int BIO_dgram_non_fatal_error(int err)
1991 # if defined(OPENSSL_SYS_WINDOWS)
1992 # if defined(WSAEWOULDBLOCK)
1993 case WSAEWOULDBLOCK:
1996 # if 0 /* This appears to always be an error */
1997 # if defined(WSAENOTCONN)
2004 # ifdef WSAEWOULDBLOCK
2005 # if WSAEWOULDBLOCK != EWOULDBLOCK
2018 # if EWOULDBLOCK != EAGAIN
2043 static void get_current_time(struct timeval *t)
2045 # if defined(_WIN32)
2048 unsigned __int64 ul;
2053 SystemTimeToFileTime(&st, &now.ft);
2055 now.ul -= 116444736000000000ULL;
2057 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
2059 t->tv_sec = (long)(now.ul / 10000000);
2060 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
2061 # elif defined(OPENSSL_SYS_VMS)
2064 t->tv_sec = (long)tb.time;
2065 t->tv_usec = (long)tb.millitm * 1000;
2067 gettimeofday(t, NULL);