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(OPENSSL_SYS_VMS)
69 # include <sys/timeb.h>
72 # ifndef OPENSSL_NO_SCTP
73 # include <netinet/sctp.h>
75 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
76 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
79 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
80 # define IP_MTU 14 /* linux is lame */
83 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
84 # define IPPROTO_IPV6 41 /* windows is lame */
87 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
88 /* Standard definition causes type-punning problems. */
89 # undef IN6_IS_ADDR_V4MAPPED
90 # define s6_addr32 __u6_addr.__u6_addr32
91 # define IN6_IS_ADDR_V4MAPPED(a) \
92 (((a)->s6_addr32[0] == 0) && \
93 ((a)->s6_addr32[1] == 0) && \
94 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
98 # define sock_write SockWrite /* Watt-32 uses same names */
99 # define sock_read SockRead
100 # define sock_puts SockPuts
103 static int dgram_write(BIO *h, const char *buf, int num);
104 static int dgram_read(BIO *h, char *buf, int size);
105 static int dgram_puts(BIO *h, const char *str);
106 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
107 static int dgram_new(BIO *h);
108 static int dgram_free(BIO *data);
109 static int dgram_clear(BIO *bio);
111 # ifndef OPENSSL_NO_SCTP
112 static int dgram_sctp_write(BIO *h, const char *buf, int num);
113 static int dgram_sctp_read(BIO *h, char *buf, int size);
114 static int dgram_sctp_puts(BIO *h, const char *str);
115 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
116 static int dgram_sctp_new(BIO *h);
117 static int dgram_sctp_free(BIO *data);
118 # ifdef SCTP_AUTHENTICATION_EVENT
119 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
124 static int BIO_dgram_should_retry(int s);
126 static void get_current_time(struct timeval *t);
128 static BIO_METHOD methods_dgramp = {
134 NULL, /* dgram_gets, */
141 # ifndef OPENSSL_NO_SCTP
142 static BIO_METHOD methods_dgramp_sctp = {
144 "datagram sctp socket",
148 NULL, /* dgram_gets, */
156 typedef struct bio_dgram_data_st {
159 struct sockaddr_in sa_in;
160 # if OPENSSL_USE_IPV6
161 struct sockaddr_in6 sa_in6;
164 unsigned int connected;
167 struct timeval next_timeout;
168 struct timeval socket_timeout;
171 # ifndef OPENSSL_NO_SCTP
172 typedef struct bio_dgram_sctp_save_message_st {
176 } bio_dgram_sctp_save_message;
178 typedef struct bio_dgram_sctp_data_st {
181 struct sockaddr_in sa_in;
182 # if OPENSSL_USE_IPV6
183 struct sockaddr_in6 sa_in6;
186 unsigned int connected;
189 struct bio_dgram_sctp_sndinfo sndinfo;
190 struct bio_dgram_sctp_rcvinfo rcvinfo;
191 struct bio_dgram_sctp_prinfo prinfo;
192 void (*handle_notifications) (BIO *bio, void *context, void *buf);
193 void *notification_context;
198 int peer_auth_tested;
199 bio_dgram_sctp_save_message saved_message;
200 } bio_dgram_sctp_data;
203 BIO_METHOD *BIO_s_datagram(void)
205 return (&methods_dgramp);
208 BIO *BIO_new_dgram(int fd, int close_flag)
212 ret = BIO_new(BIO_s_datagram());
215 BIO_set_fd(ret, fd, close_flag);
219 static int dgram_new(BIO *bi)
221 bio_dgram_data *data = NULL;
225 data = OPENSSL_malloc(sizeof(bio_dgram_data));
228 memset(data, 0x00, sizeof(bio_dgram_data));
235 static int dgram_free(BIO *a)
237 bio_dgram_data *data;
244 data = (bio_dgram_data *)a->ptr;
251 static int dgram_clear(BIO *a)
265 static void dgram_adjust_rcv_timeout(BIO *b)
267 # if defined(SO_RCVTIMEO)
268 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
276 /* Is a timer active? */
277 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
278 struct timeval timenow, timeleft;
280 /* Read current socket timeout */
281 # ifdef OPENSSL_SYS_WINDOWS
284 sz.i = sizeof(timeout);
285 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
286 (void *)&timeout, &sz.i) < 0) {
287 perror("getsockopt");
289 data->socket_timeout.tv_sec = timeout / 1000;
290 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
293 sz.i = sizeof(data->socket_timeout);
294 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
295 &(data->socket_timeout), (void *)&sz) < 0) {
296 perror("getsockopt");
297 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
298 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
301 /* Get current time */
302 get_current_time(&timenow);
304 /* Calculate time left until timer expires */
305 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
306 if (timeleft.tv_usec < timenow.tv_usec) {
307 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
310 timeleft.tv_usec -= timenow.tv_usec;
312 if (timeleft.tv_sec < timenow.tv_sec) {
314 timeleft.tv_usec = 1;
316 timeleft.tv_sec -= timenow.tv_sec;
320 * Adjust socket timeout if next handhake message timer will expire
323 if ((data->socket_timeout.tv_sec == 0
324 && data->socket_timeout.tv_usec == 0)
325 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
326 || (data->socket_timeout.tv_sec == timeleft.tv_sec
327 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
328 # ifdef OPENSSL_SYS_WINDOWS
329 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
330 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
331 (void *)&timeout, sizeof(timeout)) < 0) {
332 perror("setsockopt");
335 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
336 sizeof(struct timeval)) < 0) {
337 perror("setsockopt");
345 static void dgram_reset_rcv_timeout(BIO *b)
347 # if defined(SO_RCVTIMEO)
348 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
350 /* Is a timer active? */
351 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
352 # ifdef OPENSSL_SYS_WINDOWS
353 int timeout = data->socket_timeout.tv_sec * 1000 +
354 data->socket_timeout.tv_usec / 1000;
355 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
356 (void *)&timeout, sizeof(timeout)) < 0) {
357 perror("setsockopt");
361 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
362 sizeof(struct timeval)) < 0) {
363 perror("setsockopt");
370 static int dgram_read(BIO *b, char *out, int outl)
373 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
377 * See commentary in b_sock.c. <appro>
385 struct sockaddr_in sa_in;
386 # if OPENSSL_USE_IPV6
387 struct sockaddr_in6 sa_in6;
393 sa.len.i = sizeof(sa.peer);
396 clear_socket_error();
397 memset(&sa.peer, 0x00, sizeof(sa.peer));
398 dgram_adjust_rcv_timeout(b);
399 ret = recvfrom(b->num, out, outl, 0, &sa.peer.sa, (void *)&sa.len);
400 if (sizeof(sa.len.i) != sizeof(sa.len.s) && sa.len.i == 0) {
401 OPENSSL_assert(sa.len.s <= sizeof(sa.peer));
402 sa.len.i = (int)sa.len.s;
405 if (!data->connected && ret >= 0)
406 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &sa.peer);
408 BIO_clear_retry_flags(b);
410 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();
428 ret = writesocket(b->num, in, inl);
430 int peerlen = sizeof(data->peer);
432 if (data->peer.sa.sa_family == AF_INET)
433 peerlen = sizeof(data->peer.sa_in);
434 # if OPENSSL_USE_IPV6
435 else if (data->peer.sa.sa_family == AF_INET6)
436 peerlen = sizeof(data->peer.sa_in6);
438 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
439 ret = sendto(b->num, (char *)in, inl, 0, &data->peer.sa, peerlen);
441 ret = sendto(b->num, in, inl, 0, &data->peer.sa, peerlen);
445 BIO_clear_retry_flags(b);
447 if (BIO_dgram_should_retry(ret)) {
448 BIO_set_retry_write(b);
449 data->_errno = get_last_socket_error();
451 # if 0 /* higher layers are responsible for querying
452 * MTU, if necessary */
453 if (data->_errno == EMSGSIZE)
454 /* retrieve the new MTU */
455 BIO_ctrl(b, BIO_CTRL_DGRAM_QUERY_MTU, 0, NULL);
462 static long dgram_get_mtu_overhead(bio_dgram_data *data)
466 switch (data->peer.sa.sa_family) {
469 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
473 # if OPENSSL_USE_IPV6
475 # ifdef IN6_IS_ADDR_V4MAPPED
476 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
478 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
484 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
490 /* We don't know. Go with the historical default */
497 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
501 struct sockaddr *to = NULL;
502 bio_dgram_data *data = NULL;
504 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
505 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
506 * modern enough to define socklen_t */
510 struct sockaddr_in s4;
511 # if OPENSSL_USE_IPV6
512 struct sockaddr_in6 s6;
517 data = (bio_dgram_data *)b->ptr;
529 b->num = *((int *)ptr);
530 b->shutdown = (int)num;
542 case BIO_CTRL_GET_CLOSE:
545 case BIO_CTRL_SET_CLOSE:
546 b->shutdown = (int)num;
548 case BIO_CTRL_PENDING:
549 case BIO_CTRL_WPENDING:
556 case BIO_CTRL_DGRAM_CONNECT:
557 to = (struct sockaddr *)ptr;
559 if (connect(b->num, to, sizeof(struct sockaddr)) < 0) {
564 switch (to->sa_family) {
566 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
568 # if OPENSSL_USE_IPV6
570 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
574 memcpy(&data->peer, to, sizeof(data->peer.sa));
581 /* (Linux)kernel sets DF bit on outgoing IP packets */
582 case BIO_CTRL_DGRAM_MTU_DISCOVER:
583 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
584 addr_len = (socklen_t) sizeof(addr);
585 memset((void *)&addr, 0, sizeof(addr));
586 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
590 switch (addr.sa.sa_family) {
592 sockopt_val = IP_PMTUDISC_DO;
593 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
594 &sockopt_val, sizeof(sockopt_val))) < 0)
595 perror("setsockopt");
597 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
599 sockopt_val = IPV6_PMTUDISC_DO;
600 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
601 &sockopt_val, sizeof(sockopt_val))) < 0)
602 perror("setsockopt");
613 case BIO_CTRL_DGRAM_QUERY_MTU:
614 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
615 addr_len = (socklen_t) sizeof(addr);
616 memset((void *)&addr, 0, sizeof(addr));
617 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
621 sockopt_len = sizeof(sockopt_val);
622 switch (addr.sa.sa_family) {
625 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
626 &sockopt_len)) < 0 || sockopt_val < 0) {
630 * we assume that the transport protocol is UDP and no IP
633 data->mtu = sockopt_val - 8 - 20;
637 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
640 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
641 (void *)&sockopt_val, &sockopt_len)) < 0
642 || sockopt_val < 0) {
646 * we assume that the transport protocol is UDP and no IPV6
649 data->mtu = sockopt_val - 8 - 40;
662 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
663 ret = -dgram_get_mtu_overhead(data);
664 switch (data->peer.sa.sa_family) {
668 # if OPENSSL_USE_IPV6
670 # ifdef IN6_IS_ADDR_V4MAPPED
671 if (IN6_IS_ADDR_V4MAPPED(&data->peer.sa_in6.sin6_addr))
683 case BIO_CTRL_DGRAM_GET_MTU:
686 case BIO_CTRL_DGRAM_SET_MTU:
690 case BIO_CTRL_DGRAM_SET_CONNECTED:
691 to = (struct sockaddr *)ptr;
695 switch (to->sa_family) {
697 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
699 # if OPENSSL_USE_IPV6
701 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
705 memcpy(&data->peer, to, sizeof(data->peer.sa));
710 memset(&(data->peer), 0x00, sizeof(data->peer));
713 case BIO_CTRL_DGRAM_GET_PEER:
714 switch (data->peer.sa.sa_family) {
716 ret = sizeof(data->peer.sa_in);
718 # if OPENSSL_USE_IPV6
720 ret = sizeof(data->peer.sa_in6);
724 ret = sizeof(data->peer.sa);
727 if (num == 0 || num > ret)
729 memcpy(ptr, &data->peer, (ret = num));
731 case BIO_CTRL_DGRAM_SET_PEER:
732 to = (struct sockaddr *)ptr;
733 switch (to->sa_family) {
735 memcpy(&data->peer, to, sizeof(data->peer.sa_in));
737 # if OPENSSL_USE_IPV6
739 memcpy(&data->peer, to, sizeof(data->peer.sa_in6));
743 memcpy(&data->peer, to, sizeof(data->peer.sa));
747 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
748 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
750 # if defined(SO_RCVTIMEO)
751 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
752 # ifdef OPENSSL_SYS_WINDOWS
754 struct timeval *tv = (struct timeval *)ptr;
755 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
756 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
757 (void *)&timeout, sizeof(timeout)) < 0) {
758 perror("setsockopt");
763 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
764 sizeof(struct timeval)) < 0) {
765 perror("setsockopt");
770 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
778 # ifdef OPENSSL_SYS_WINDOWS
780 struct timeval *tv = (struct timeval *)ptr;
782 sz.i = sizeof(timeout);
783 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
784 (void *)&timeout, &sz.i) < 0) {
785 perror("getsockopt");
788 tv->tv_sec = timeout / 1000;
789 tv->tv_usec = (timeout % 1000) * 1000;
793 sz.i = sizeof(struct timeval);
794 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
795 ptr, (void *)&sz) < 0) {
796 perror("getsockopt");
798 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
799 OPENSSL_assert(sz.s <= sizeof(struct timeval));
807 # if defined(SO_SNDTIMEO)
808 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
809 # ifdef OPENSSL_SYS_WINDOWS
811 struct timeval *tv = (struct timeval *)ptr;
812 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
813 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
814 (void *)&timeout, sizeof(timeout)) < 0) {
815 perror("setsockopt");
820 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
821 sizeof(struct timeval)) < 0) {
822 perror("setsockopt");
827 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
835 # ifdef OPENSSL_SYS_WINDOWS
837 struct timeval *tv = (struct timeval *)ptr;
839 sz.i = sizeof(timeout);
840 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
841 (void *)&timeout, &sz.i) < 0) {
842 perror("getsockopt");
845 tv->tv_sec = timeout / 1000;
846 tv->tv_usec = (timeout % 1000) * 1000;
850 sz.i = sizeof(struct timeval);
851 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
852 ptr, (void *)&sz) < 0) {
853 perror("getsockopt");
855 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
856 OPENSSL_assert(sz.s <= sizeof(struct timeval));
864 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
866 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
867 # ifdef OPENSSL_SYS_WINDOWS
868 if (data->_errno == WSAETIMEDOUT)
870 if (data->_errno == EAGAIN)
879 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
880 if (data->_errno == EMSGSIZE) {
887 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
888 sockopt_val = num ? 1 : 0;
890 switch (data->peer.sa.sa_family) {
892 # if defined(IP_DONTFRAG)
893 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
894 &sockopt_val, sizeof(sockopt_val))) < 0) {
895 perror("setsockopt");
898 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
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");
905 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
906 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
907 (const char *)&sockopt_val,
908 sizeof(sockopt_val))) < 0) {
909 perror("setsockopt");
916 # if OPENSSL_USE_IPV6
918 # if defined(IPV6_DONTFRAG)
919 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
920 (const void *)&sockopt_val,
921 sizeof(sockopt_val))) < 0) {
922 perror("setsockopt");
925 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
926 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
927 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
928 &sockopt_val, sizeof(sockopt_val))) < 0) {
929 perror("setsockopt");
942 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
943 ret = dgram_get_mtu_overhead(data);
952 static int dgram_puts(BIO *bp, const char *str)
957 ret = dgram_write(bp, str, n);
961 # ifndef OPENSSL_NO_SCTP
962 BIO_METHOD *BIO_s_datagram_sctp(void)
964 return (&methods_dgramp_sctp);
967 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
970 int ret, optval = 20000;
971 int auth_data = 0, auth_forward = 0;
973 struct sctp_authchunk auth;
974 struct sctp_authchunks *authchunks;
975 socklen_t sockopt_len;
976 # ifdef SCTP_AUTHENTICATION_EVENT
978 struct sctp_event event;
980 struct sctp_event_subscribe event;
984 bio = BIO_new(BIO_s_datagram_sctp());
987 BIO_set_fd(bio, fd, close_flag);
989 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
990 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
992 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
993 sizeof(struct sctp_authchunk));
998 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
1000 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
1001 sizeof(struct sctp_authchunk));
1008 * Test if activation was successful. When using accept(), SCTP-AUTH has
1009 * to be activated for the listening socket already, otherwise the
1010 * connected socket won't use it.
1012 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1013 authchunks = OPENSSL_malloc(sockopt_len);
1018 memset(authchunks, 0, sizeof(sockopt_len));
1020 getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
1024 OPENSSL_free(authchunks);
1029 for (p = (unsigned char *)authchunks->gauth_chunks;
1030 p < (unsigned char *)authchunks + sockopt_len;
1031 p += sizeof(uint8_t)) {
1032 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1034 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1038 OPENSSL_free(authchunks);
1040 OPENSSL_assert(auth_data);
1041 OPENSSL_assert(auth_forward);
1043 # ifdef SCTP_AUTHENTICATION_EVENT
1045 memset(&event, 0, sizeof(struct sctp_event));
1046 event.se_assoc_id = 0;
1047 event.se_type = SCTP_AUTHENTICATION_EVENT;
1050 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
1051 sizeof(struct sctp_event));
1057 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
1058 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
1064 event.sctp_authentication_event = 1;
1067 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
1068 sizeof(struct sctp_event_subscribe));
1077 * Disable partial delivery by setting the min size larger than the max
1078 * record size of 2^14 + 2048 + 13
1081 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
1091 int BIO_dgram_is_sctp(BIO *bio)
1093 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
1096 static int dgram_sctp_new(BIO *bi)
1098 bio_dgram_sctp_data *data = NULL;
1102 data = OPENSSL_malloc(sizeof(bio_dgram_sctp_data));
1105 memset(data, 0x00, sizeof(bio_dgram_sctp_data));
1106 # ifdef SCTP_PR_SCTP_NONE
1107 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
1115 static int dgram_sctp_free(BIO *a)
1117 bio_dgram_sctp_data *data;
1121 if (!dgram_clear(a))
1124 data = (bio_dgram_sctp_data *) a->ptr;
1126 if (data->saved_message.data != NULL)
1127 OPENSSL_free(data->saved_message.data);
1134 # ifdef SCTP_AUTHENTICATION_EVENT
1135 void dgram_sctp_handle_auth_free_key_event(BIO *b,
1136 union sctp_notification *snp)
1139 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
1141 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
1142 struct sctp_authkeyid authkeyid;
1145 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
1146 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1147 &authkeyid, sizeof(struct sctp_authkeyid));
1152 static int dgram_sctp_read(BIO *b, char *out, int outl)
1154 int ret = 0, n = 0, i, optval;
1156 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1157 union sctp_notification *snp;
1160 struct cmsghdr *cmsg;
1164 clear_socket_error();
1167 memset(&data->rcvinfo, 0x00,
1168 sizeof(struct bio_dgram_sctp_rcvinfo));
1171 msg.msg_name = NULL;
1172 msg.msg_namelen = 0;
1175 msg.msg_control = cmsgbuf;
1176 msg.msg_controllen = 512;
1178 n = recvmsg(b->num, &msg, 0);
1186 if (msg.msg_controllen > 0) {
1187 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1188 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1189 if (cmsg->cmsg_level != IPPROTO_SCTP)
1191 # ifdef SCTP_RCVINFO
1192 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1193 struct sctp_rcvinfo *rcvinfo;
1195 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1196 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1197 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1198 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1199 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1200 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1201 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1202 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1206 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1207 struct sctp_sndrcvinfo *sndrcvinfo;
1210 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1211 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1212 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1213 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1214 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1215 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1216 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1217 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1223 if (msg.msg_flags & MSG_NOTIFICATION) {
1224 snp = (union sctp_notification *)out;
1225 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1227 struct sctp_event event;
1229 struct sctp_event_subscribe event;
1230 socklen_t eventsize;
1233 * If a message has been delayed until the socket is dry,
1234 * it can be sent now.
1236 if (data->saved_message.length > 0) {
1237 dgram_sctp_write(data->saved_message.bio,
1238 data->saved_message.data,
1239 data->saved_message.length);
1240 OPENSSL_free(data->saved_message.data);
1241 data->saved_message.data = NULL;
1242 data->saved_message.length = 0;
1245 /* disable sender dry event */
1247 memset(&event, 0, sizeof(struct sctp_event));
1248 event.se_assoc_id = 0;
1249 event.se_type = SCTP_SENDER_DRY_EVENT;
1251 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1252 sizeof(struct sctp_event));
1258 eventsize = sizeof(struct sctp_event_subscribe);
1259 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1266 event.sctp_sender_dry_event = 0;
1268 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1269 sizeof(struct sctp_event_subscribe));
1276 # ifdef SCTP_AUTHENTICATION_EVENT
1277 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1278 dgram_sctp_handle_auth_free_key_event(b, snp);
1281 if (data->handle_notifications != NULL)
1282 data->handle_notifications(b, data->notification_context,
1285 memset(out, 0, outl);
1289 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1292 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1293 /* Partial message read, this should never happen! */
1296 * The buffer was too small, this means the peer sent a message
1297 * that was larger than allowed.
1303 * Test if socket buffer can handle max record size (2^14 + 2048
1306 optlen = (socklen_t) sizeof(int);
1307 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1309 OPENSSL_assert(optval >= 18445);
1312 * Test if SCTP doesn't partially deliver below max record size
1313 * (2^14 + 2048 + 13)
1315 optlen = (socklen_t) sizeof(int);
1317 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1320 OPENSSL_assert(optval >= 18445);
1323 * Partially delivered notification??? Probably a bug....
1325 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1328 * Everything seems ok till now, so it's most likely a message
1329 * dropped by PR-SCTP.
1331 memset(out, 0, outl);
1332 BIO_set_retry_read(b);
1336 BIO_clear_retry_flags(b);
1338 if (BIO_dgram_should_retry(ret)) {
1339 BIO_set_retry_read(b);
1340 data->_errno = get_last_socket_error();
1344 /* Test if peer uses SCTP-AUTH before continuing */
1345 if (!data->peer_auth_tested) {
1346 int ii, auth_data = 0, auth_forward = 0;
1348 struct sctp_authchunks *authchunks;
1351 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1352 authchunks = OPENSSL_malloc(optlen);
1354 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1357 memset(authchunks, 0, sizeof(optlen));
1358 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1359 authchunks, &optlen);
1362 for (p = (unsigned char *)authchunks->gauth_chunks;
1363 p < (unsigned char *)authchunks + optlen;
1364 p += sizeof(uint8_t)) {
1365 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1367 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1371 OPENSSL_free(authchunks);
1373 if (!auth_data || !auth_forward) {
1374 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1378 data->peer_auth_tested = 1;
1384 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1387 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1388 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1389 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1390 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1391 struct iovec iov[1];
1393 struct cmsghdr *cmsg;
1394 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1395 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1396 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1397 struct sctp_sndinfo *sndinfo;
1398 struct sctp_prinfo *prinfo;
1400 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1401 struct sctp_sndrcvinfo *sndrcvinfo;
1404 clear_socket_error();
1407 * If we're send anything else than application data, disable all user
1408 * parameters and flags.
1411 memset(&handshake_sinfo, 0x00, sizeof(struct bio_dgram_sctp_sndinfo));
1412 # ifdef SCTP_SACK_IMMEDIATELY
1413 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1415 sinfo = &handshake_sinfo;
1419 * If we have to send a shutdown alert message and the socket is not dry
1420 * yet, we have to save it and send it as soon as the socket gets dry.
1422 if (data->save_shutdown && !BIO_dgram_sctp_wait_for_dry(b)) {
1424 data->saved_message.bio = b;
1425 if (!(tmp = OPENSSL_malloc(inl))) {
1426 BIOerr(BIO_F_DGRAM_SCTP_WRITE, ERR_R_MALLOC_FAILURE);
1429 if (data->saved_message.data)
1430 OPENSSL_free(data->saved_message.data);
1431 data->saved_message.data = tmp;
1432 memcpy(data->saved_message.data, in, inl);
1433 data->saved_message.length = inl;
1437 iov[0].iov_base = (char *)in;
1438 iov[0].iov_len = inl;
1439 msg.msg_name = NULL;
1440 msg.msg_namelen = 0;
1443 msg.msg_control = (caddr_t) cmsgbuf;
1444 msg.msg_controllen = 0;
1446 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1447 cmsg = (struct cmsghdr *)cmsgbuf;
1448 cmsg->cmsg_level = IPPROTO_SCTP;
1449 cmsg->cmsg_type = SCTP_SNDINFO;
1450 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1451 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1452 memset(sndinfo, 0, sizeof(struct sctp_sndinfo));
1453 sndinfo->snd_sid = sinfo->snd_sid;
1454 sndinfo->snd_flags = sinfo->snd_flags;
1455 sndinfo->snd_ppid = sinfo->snd_ppid;
1456 sndinfo->snd_context = sinfo->snd_context;
1457 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1460 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1461 cmsg->cmsg_level = IPPROTO_SCTP;
1462 cmsg->cmsg_type = SCTP_PRINFO;
1463 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1464 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1465 memset(prinfo, 0, sizeof(struct sctp_prinfo));
1466 prinfo->pr_policy = pinfo->pr_policy;
1467 prinfo->pr_value = pinfo->pr_value;
1468 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1470 cmsg = (struct cmsghdr *)cmsgbuf;
1471 cmsg->cmsg_level = IPPROTO_SCTP;
1472 cmsg->cmsg_type = SCTP_SNDRCV;
1473 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1474 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1475 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
1476 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1477 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1479 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1481 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1482 sndrcvinfo->sinfo_context = sinfo->snd_context;
1483 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1484 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1487 ret = sendmsg(b->num, &msg, 0);
1489 BIO_clear_retry_flags(b);
1491 if (BIO_dgram_should_retry(ret)) {
1492 BIO_set_retry_write(b);
1493 data->_errno = get_last_socket_error();
1499 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1502 bio_dgram_sctp_data *data = NULL;
1503 socklen_t sockopt_len = 0;
1504 struct sctp_authkeyid authkeyid;
1505 struct sctp_authkey *authkey = NULL;
1507 data = (bio_dgram_sctp_data *) b->ptr;
1510 case BIO_CTRL_DGRAM_QUERY_MTU:
1512 * Set to maximum (2^14) and ignore user input to enable transport
1513 * protocol fragmentation. Returns always 2^14.
1518 case BIO_CTRL_DGRAM_SET_MTU:
1520 * Set to maximum (2^14) and ignore input to enable transport
1521 * protocol fragmentation. Returns always 2^14.
1526 case BIO_CTRL_DGRAM_SET_CONNECTED:
1527 case BIO_CTRL_DGRAM_CONNECT:
1528 /* Returns always -1. */
1531 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1533 * SCTP doesn't need the DTLS timer Returns always 1.
1536 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1538 * We allow transport protocol fragmentation so this is irrelevant
1542 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1544 data->in_handshake = 1;
1546 data->in_handshake = 0;
1549 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1550 &data->in_handshake, sizeof(int));
1552 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1554 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1557 /* Get active key */
1558 sockopt_len = sizeof(struct sctp_authkeyid);
1560 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1566 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1567 authkey = OPENSSL_malloc(sockopt_len);
1568 if (authkey == NULL) {
1572 memset(authkey, 0x00, sockopt_len);
1573 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1574 # ifndef __FreeBSD__
1576 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1577 * and higher work without it.
1579 authkey->sca_keylength = 64;
1581 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1584 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1586 OPENSSL_free(authkey);
1591 /* Reset active key */
1592 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1593 &authkeyid, sizeof(struct sctp_authkeyid));
1598 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1599 /* Returns 0 on success, -1 otherwise. */
1601 /* Get active key */
1602 sockopt_len = sizeof(struct sctp_authkeyid);
1604 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1609 /* Set active key */
1610 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1611 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1612 &authkeyid, sizeof(struct sctp_authkeyid));
1617 * CCS has been sent, so remember that and fall through to check if
1618 * we need to deactivate an old key
1622 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1623 /* Returns 0 on success, -1 otherwise. */
1626 * Has this command really been called or is this just a
1629 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1633 * CSS has been both, received and sent, so deactivate an old key
1635 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1636 /* Get active key */
1637 sockopt_len = sizeof(struct sctp_authkeyid);
1639 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1640 &authkeyid, &sockopt_len);
1645 * Deactivate key or delete second last key if
1646 * SCTP_AUTHENTICATION_EVENT is not available.
1648 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1649 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1650 sockopt_len = sizeof(struct sctp_authkeyid);
1651 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1652 &authkeyid, sockopt_len);
1656 # ifndef SCTP_AUTHENTICATION_EVENT
1657 if (authkeyid.scact_keynumber > 0) {
1658 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1659 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1660 &authkeyid, sizeof(struct sctp_authkeyid));
1670 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1671 /* Returns the size of the copied struct. */
1672 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1673 num = sizeof(struct bio_dgram_sctp_sndinfo);
1675 memcpy(ptr, &(data->sndinfo), num);
1678 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1679 /* Returns the size of the copied struct. */
1680 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1681 num = sizeof(struct bio_dgram_sctp_sndinfo);
1683 memcpy(&(data->sndinfo), ptr, num);
1685 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1686 /* Returns the size of the copied struct. */
1687 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1688 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1690 memcpy(ptr, &data->rcvinfo, num);
1694 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1695 /* Returns the size of the copied struct. */
1696 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1697 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1699 memcpy(&(data->rcvinfo), ptr, num);
1701 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1702 /* Returns the size of the copied struct. */
1703 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1704 num = sizeof(struct bio_dgram_sctp_prinfo);
1706 memcpy(ptr, &(data->prinfo), num);
1709 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1710 /* Returns the size of the copied struct. */
1711 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1712 num = sizeof(struct bio_dgram_sctp_prinfo);
1714 memcpy(&(data->prinfo), ptr, num);
1716 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1717 /* Returns always 1. */
1719 data->save_shutdown = 1;
1721 data->save_shutdown = 0;
1726 * Pass to default ctrl function to process SCTP unspecific commands
1728 ret = dgram_ctrl(b, cmd, num, ptr);
1734 int BIO_dgram_sctp_notification_cb(BIO *b,
1735 void (*handle_notifications) (BIO *bio,
1741 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1743 if (handle_notifications != NULL) {
1744 data->handle_notifications = handle_notifications;
1745 data->notification_context = context;
1752 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1755 int n, sockflags, ret;
1756 union sctp_notification snp;
1760 struct sctp_event event;
1762 struct sctp_event_subscribe event;
1763 socklen_t eventsize;
1765 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1767 /* set sender dry event */
1769 memset(&event, 0, sizeof(struct sctp_event));
1770 event.se_assoc_id = 0;
1771 event.se_type = SCTP_SENDER_DRY_EVENT;
1774 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1775 sizeof(struct sctp_event));
1777 eventsize = sizeof(struct sctp_event_subscribe);
1778 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1782 event.sctp_sender_dry_event = 1;
1785 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1786 sizeof(struct sctp_event_subscribe));
1791 /* peek for notification */
1792 memset(&snp, 0x00, sizeof(union sctp_notification));
1793 iov.iov_base = (char *)&snp;
1794 iov.iov_len = sizeof(union sctp_notification);
1795 msg.msg_name = NULL;
1796 msg.msg_namelen = 0;
1799 msg.msg_control = NULL;
1800 msg.msg_controllen = 0;
1803 n = recvmsg(b->num, &msg, MSG_PEEK);
1805 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1806 && (get_last_socket_error() != EWOULDBLOCK))
1812 /* if we find a notification, process it and try again if necessary */
1813 while (msg.msg_flags & MSG_NOTIFICATION) {
1814 memset(&snp, 0x00, sizeof(union sctp_notification));
1815 iov.iov_base = (char *)&snp;
1816 iov.iov_len = sizeof(union sctp_notification);
1817 msg.msg_name = NULL;
1818 msg.msg_namelen = 0;
1821 msg.msg_control = NULL;
1822 msg.msg_controllen = 0;
1825 n = recvmsg(b->num, &msg, 0);
1827 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1828 && (get_last_socket_error() != EWOULDBLOCK))
1834 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1837 /* disable sender dry event */
1839 memset(&event, 0, sizeof(struct sctp_event));
1840 event.se_assoc_id = 0;
1841 event.se_type = SCTP_SENDER_DRY_EVENT;
1844 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1845 sizeof(struct sctp_event));
1847 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1849 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1854 event.sctp_sender_dry_event = 0;
1857 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1858 sizeof(struct sctp_event_subscribe));
1863 # ifdef SCTP_AUTHENTICATION_EVENT
1864 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1865 dgram_sctp_handle_auth_free_key_event(b, &snp);
1868 if (data->handle_notifications != NULL)
1869 data->handle_notifications(b, data->notification_context,
1872 /* found notification, peek again */
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;
1884 /* if we have seen the dry already, don't wait */
1886 sockflags = fcntl(b->num, F_GETFL, 0);
1887 fcntl(b->num, F_SETFL, O_NONBLOCK);
1890 n = recvmsg(b->num, &msg, MSG_PEEK);
1893 fcntl(b->num, F_SETFL, sockflags);
1897 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1898 && (get_last_socket_error() != EWOULDBLOCK))
1905 /* read anything else */
1909 int BIO_dgram_sctp_msg_waiting(BIO *b)
1912 union sctp_notification snp;
1915 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1917 /* Check if there are any messages waiting to be read */
1919 memset(&snp, 0x00, sizeof(union sctp_notification));
1920 iov.iov_base = (char *)&snp;
1921 iov.iov_len = sizeof(union sctp_notification);
1922 msg.msg_name = NULL;
1923 msg.msg_namelen = 0;
1926 msg.msg_control = NULL;
1927 msg.msg_controllen = 0;
1930 sockflags = fcntl(b->num, F_GETFL, 0);
1931 fcntl(b->num, F_SETFL, O_NONBLOCK);
1932 n = recvmsg(b->num, &msg, MSG_PEEK);
1933 fcntl(b->num, F_SETFL, sockflags);
1935 /* if notification, process and try again */
1936 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1937 # ifdef SCTP_AUTHENTICATION_EVENT
1938 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1939 dgram_sctp_handle_auth_free_key_event(b, &snp);
1942 memset(&snp, 0x00, sizeof(union sctp_notification));
1943 iov.iov_base = (char *)&snp;
1944 iov.iov_len = sizeof(union sctp_notification);
1945 msg.msg_name = NULL;
1946 msg.msg_namelen = 0;
1949 msg.msg_control = NULL;
1950 msg.msg_controllen = 0;
1952 n = recvmsg(b->num, &msg, 0);
1954 if (data->handle_notifications != NULL)
1955 data->handle_notifications(b, data->notification_context,
1959 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1961 /* Return 1 if there is a message to be read, return 0 otherwise. */
1968 static int dgram_sctp_puts(BIO *bp, const char *str)
1973 ret = dgram_sctp_write(bp, str, n);
1978 static int BIO_dgram_should_retry(int i)
1982 if ((i == 0) || (i == -1)) {
1983 err = get_last_socket_error();
1985 # if defined(OPENSSL_SYS_WINDOWS)
1987 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1988 * this point, the error code was overwritten by another system call
1989 * before this error handling is called.
1993 return (BIO_dgram_non_fatal_error(err));
1998 int BIO_dgram_non_fatal_error(int err)
2001 # if defined(OPENSSL_SYS_WINDOWS)
2002 # if defined(WSAEWOULDBLOCK)
2003 case WSAEWOULDBLOCK:
2006 # if 0 /* This appears to always be an error */
2007 # if defined(WSAENOTCONN)
2014 # ifdef WSAEWOULDBLOCK
2015 # if WSAEWOULDBLOCK != EWOULDBLOCK
2028 # if EWOULDBLOCK != EAGAIN
2053 static void get_current_time(struct timeval *t)
2055 # if defined(_WIN32)
2058 unsigned __int64 ul;
2063 SystemTimeToFileTime(&st, &now.ft);
2065 now.ul -= 116444736000000000ULL;
2067 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
2069 t->tv_sec = (long)(now.ul / 10000000);
2070 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
2071 # elif defined(OPENSSL_SYS_VMS)
2074 t->tv_sec = (long)tb.time;
2075 t->tv_usec = (long)tb.millitm * 1000;
2077 gettimeofday(t, NULL);