2 * Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
14 #ifndef OPENSSL_NO_DGRAM
16 # ifndef OPENSSL_NO_SCTP
17 # include <netinet/sctp.h>
19 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
20 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
23 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
24 # define IP_MTU 14 /* linux is lame */
27 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
28 # define IPPROTO_IPV6 41 /* windows is lame */
31 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
32 /* Standard definition causes type-punning problems. */
33 # undef IN6_IS_ADDR_V4MAPPED
34 # define s6_addr32 __u6_addr.__u6_addr32
35 # define IN6_IS_ADDR_V4MAPPED(a) \
36 (((a)->s6_addr32[0] == 0) && \
37 ((a)->s6_addr32[1] == 0) && \
38 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
41 static int dgram_write(BIO *h, const char *buf, int num);
42 static int dgram_read(BIO *h, char *buf, int size);
43 static int dgram_puts(BIO *h, const char *str);
44 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
45 static int dgram_new(BIO *h);
46 static int dgram_free(BIO *data);
47 static int dgram_clear(BIO *bio);
49 # ifndef OPENSSL_NO_SCTP
50 static int dgram_sctp_write(BIO *h, const char *buf, int num);
51 static int dgram_sctp_read(BIO *h, char *buf, int size);
52 static int dgram_sctp_puts(BIO *h, const char *str);
53 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
54 static int dgram_sctp_new(BIO *h);
55 static int dgram_sctp_free(BIO *data);
56 # ifdef SCTP_AUTHENTICATION_EVENT
57 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
62 static int BIO_dgram_should_retry(int s);
64 static void get_current_time(struct timeval *t);
66 static const BIO_METHOD methods_dgramp = {
69 /* TODO: Convert to new style write function */
72 /* TODO: Convert to new style read function */
76 NULL, /* dgram_gets, */
80 NULL, /* dgram_callback_ctrl */
83 # ifndef OPENSSL_NO_SCTP
84 static const BIO_METHOD methods_dgramp_sctp = {
86 "datagram sctp socket",
87 /* TODO: Convert to new style write function */
90 /* TODO: Convert to new style write function */
94 NULL, /* dgram_gets, */
98 NULL, /* dgram_callback_ctrl */
102 typedef struct bio_dgram_data_st {
104 unsigned int connected;
107 struct timeval next_timeout;
108 struct timeval socket_timeout;
109 unsigned int peekmode;
112 # ifndef OPENSSL_NO_SCTP
113 typedef struct bio_dgram_sctp_save_message_st {
117 } bio_dgram_sctp_save_message;
119 typedef struct bio_dgram_sctp_data_st {
121 unsigned int connected;
124 struct bio_dgram_sctp_sndinfo sndinfo;
125 struct bio_dgram_sctp_rcvinfo rcvinfo;
126 struct bio_dgram_sctp_prinfo prinfo;
127 void (*handle_notifications) (BIO *bio, void *context, void *buf);
128 void *notification_context;
133 int peer_auth_tested;
134 } bio_dgram_sctp_data;
137 const BIO_METHOD *BIO_s_datagram(void)
139 return &methods_dgramp;
142 BIO *BIO_new_dgram(int fd, int close_flag)
146 ret = BIO_new(BIO_s_datagram());
149 BIO_set_fd(ret, fd, close_flag);
153 static int dgram_new(BIO *bi)
155 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
163 static int dgram_free(BIO *a)
165 bio_dgram_data *data;
172 data = (bio_dgram_data *)a->ptr;
178 static int dgram_clear(BIO *a)
184 BIO_closesocket(a->num);
192 static void dgram_adjust_rcv_timeout(BIO *b)
194 # if defined(SO_RCVTIMEO)
195 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
203 /* Is a timer active? */
204 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
205 struct timeval timenow, timeleft;
207 /* Read current socket timeout */
208 # ifdef OPENSSL_SYS_WINDOWS
211 sz.i = sizeof(timeout);
212 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
213 (void *)&timeout, &sz.i) < 0) {
214 perror("getsockopt");
216 data->socket_timeout.tv_sec = timeout / 1000;
217 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
220 sz.i = sizeof(data->socket_timeout);
221 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
222 &(data->socket_timeout), (void *)&sz) < 0) {
223 perror("getsockopt");
224 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
225 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
228 /* Get current time */
229 get_current_time(&timenow);
231 /* Calculate time left until timer expires */
232 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
233 if (timeleft.tv_usec < timenow.tv_usec) {
234 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
237 timeleft.tv_usec -= timenow.tv_usec;
239 if (timeleft.tv_sec < timenow.tv_sec) {
241 timeleft.tv_usec = 1;
243 timeleft.tv_sec -= timenow.tv_sec;
247 * Adjust socket timeout if next handshake message timer will expire
250 if ((data->socket_timeout.tv_sec == 0
251 && data->socket_timeout.tv_usec == 0)
252 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
253 || (data->socket_timeout.tv_sec == timeleft.tv_sec
254 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
255 # ifdef OPENSSL_SYS_WINDOWS
256 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
257 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
258 (void *)&timeout, sizeof(timeout)) < 0) {
259 perror("setsockopt");
262 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
263 sizeof(struct timeval)) < 0) {
264 perror("setsockopt");
272 static void dgram_reset_rcv_timeout(BIO *b)
274 # if defined(SO_RCVTIMEO)
275 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
277 /* Is a timer active? */
278 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
279 # ifdef OPENSSL_SYS_WINDOWS
280 int timeout = data->socket_timeout.tv_sec * 1000 +
281 data->socket_timeout.tv_usec / 1000;
282 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
283 (void *)&timeout, sizeof(timeout)) < 0) {
284 perror("setsockopt");
288 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
289 sizeof(struct timeval)) < 0) {
290 perror("setsockopt");
297 static int dgram_read(BIO *b, char *out, int outl)
300 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
304 socklen_t len = sizeof(peer);
307 clear_socket_error();
308 memset(&peer, 0, sizeof(peer));
309 dgram_adjust_rcv_timeout(b);
312 ret = recvfrom(b->num, out, outl, flags,
313 BIO_ADDR_sockaddr_noconst(&peer), &len);
315 if (!data->connected && ret >= 0)
316 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
318 BIO_clear_retry_flags(b);
320 if (BIO_dgram_should_retry(ret)) {
321 BIO_set_retry_read(b);
322 data->_errno = get_last_socket_error();
326 dgram_reset_rcv_timeout(b);
331 static int dgram_write(BIO *b, const char *in, int inl)
334 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
335 clear_socket_error();
338 ret = writesocket(b->num, in, inl);
340 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
342 ret = sendto(b->num, in, inl, 0,
343 BIO_ADDR_sockaddr(&data->peer), peerlen);
346 BIO_clear_retry_flags(b);
348 if (BIO_dgram_should_retry(ret)) {
349 BIO_set_retry_write(b);
350 data->_errno = get_last_socket_error();
356 static long dgram_get_mtu_overhead(bio_dgram_data *data)
360 switch (BIO_ADDR_family(&data->peer)) {
363 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
370 # ifdef IN6_IS_ADDR_V4MAPPED
371 struct in6_addr tmp_addr;
372 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
373 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
375 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
381 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
388 /* We don't know. Go with the historical default */
395 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
399 bio_dgram_data *data = NULL;
402 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
403 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
404 * modern enough to define socklen_t */
409 data = (bio_dgram_data *)b->ptr;
421 b->num = *((int *)ptr);
422 b->shutdown = (int)num;
434 case BIO_CTRL_GET_CLOSE:
437 case BIO_CTRL_SET_CLOSE:
438 b->shutdown = (int)num;
440 case BIO_CTRL_PENDING:
441 case BIO_CTRL_WPENDING:
448 case BIO_CTRL_DGRAM_CONNECT:
449 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
451 /* (Linux)kernel sets DF bit on outgoing IP packets */
452 case BIO_CTRL_DGRAM_MTU_DISCOVER:
453 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
454 addr_len = (socklen_t) sizeof(addr);
455 memset(&addr, 0, sizeof(addr));
456 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
460 switch (addr.sa.sa_family) {
462 sockopt_val = IP_PMTUDISC_DO;
463 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
464 &sockopt_val, sizeof(sockopt_val))) < 0)
465 perror("setsockopt");
467 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
469 sockopt_val = IPV6_PMTUDISC_DO;
470 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
471 &sockopt_val, sizeof(sockopt_val))) < 0)
472 perror("setsockopt");
483 case BIO_CTRL_DGRAM_QUERY_MTU:
484 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
485 addr_len = (socklen_t) sizeof(addr);
486 memset(&addr, 0, sizeof(addr));
487 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
491 sockopt_len = sizeof(sockopt_val);
492 switch (addr.sa.sa_family) {
495 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
496 &sockopt_len)) < 0 || sockopt_val < 0) {
500 * we assume that the transport protocol is UDP and no IP
503 data->mtu = sockopt_val - 8 - 20;
507 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
510 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
511 (void *)&sockopt_val, &sockopt_len)) < 0
512 || sockopt_val < 0) {
516 * we assume that the transport protocol is UDP and no IPV6
519 data->mtu = sockopt_val - 8 - 40;
532 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
533 ret = -dgram_get_mtu_overhead(data);
534 switch (BIO_ADDR_family(&data->peer)) {
538 # if OPENSSL_USE_IPV6
541 # ifdef IN6_IS_ADDR_V4MAPPED
542 struct in6_addr tmp_addr;
543 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
544 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
557 case BIO_CTRL_DGRAM_GET_MTU:
559 case BIO_CTRL_DGRAM_SET_MTU:
563 case BIO_CTRL_DGRAM_SET_CONNECTED:
566 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
569 memset(&data->peer, 0, sizeof(data->peer));
572 case BIO_CTRL_DGRAM_GET_PEER:
573 ret = BIO_ADDR_sockaddr_size(&data->peer);
574 /* FIXME: if num < ret, we will only return part of an address.
575 That should bee an error, no? */
576 if (num == 0 || num > ret)
578 memcpy(ptr, &data->peer, (ret = num));
580 case BIO_CTRL_DGRAM_SET_PEER:
581 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
583 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
584 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
586 # if defined(SO_RCVTIMEO)
587 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
588 # ifdef OPENSSL_SYS_WINDOWS
590 struct timeval *tv = (struct timeval *)ptr;
591 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
592 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
593 (void *)&timeout, sizeof(timeout)) < 0) {
594 perror("setsockopt");
599 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
600 sizeof(struct timeval)) < 0) {
601 perror("setsockopt");
606 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
614 # ifdef OPENSSL_SYS_WINDOWS
616 struct timeval *tv = (struct timeval *)ptr;
618 sz.i = sizeof(timeout);
619 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
620 (void *)&timeout, &sz.i) < 0) {
621 perror("getsockopt");
624 tv->tv_sec = timeout / 1000;
625 tv->tv_usec = (timeout % 1000) * 1000;
629 sz.i = sizeof(struct timeval);
630 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
631 ptr, (void *)&sz) < 0) {
632 perror("getsockopt");
634 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
635 OPENSSL_assert(sz.s <= sizeof(struct timeval));
643 # if defined(SO_SNDTIMEO)
644 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
645 # ifdef OPENSSL_SYS_WINDOWS
647 struct timeval *tv = (struct timeval *)ptr;
648 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
649 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
650 (void *)&timeout, sizeof(timeout)) < 0) {
651 perror("setsockopt");
656 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
657 sizeof(struct timeval)) < 0) {
658 perror("setsockopt");
663 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
671 # ifdef OPENSSL_SYS_WINDOWS
673 struct timeval *tv = (struct timeval *)ptr;
675 sz.i = sizeof(timeout);
676 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
677 (void *)&timeout, &sz.i) < 0) {
678 perror("getsockopt");
681 tv->tv_sec = timeout / 1000;
682 tv->tv_usec = (timeout % 1000) * 1000;
686 sz.i = sizeof(struct timeval);
687 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
688 ptr, (void *)&sz) < 0) {
689 perror("getsockopt");
691 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
692 OPENSSL_assert(sz.s <= sizeof(struct timeval));
700 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
702 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
703 # ifdef OPENSSL_SYS_WINDOWS
704 d_errno = (data->_errno == WSAETIMEDOUT);
706 d_errno = (data->_errno == EAGAIN);
715 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
716 if (data->_errno == EMSGSIZE) {
723 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
724 sockopt_val = num ? 1 : 0;
726 switch (data->peer.sa.sa_family) {
728 # if defined(IP_DONTFRAG)
729 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
730 &sockopt_val, sizeof(sockopt_val))) < 0) {
731 perror("setsockopt");
734 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
735 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
736 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
737 &sockopt_val, sizeof(sockopt_val))) < 0) {
738 perror("setsockopt");
741 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
742 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
743 (const char *)&sockopt_val,
744 sizeof(sockopt_val))) < 0) {
745 perror("setsockopt");
752 # if OPENSSL_USE_IPV6
754 # if defined(IPV6_DONTFRAG)
755 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
756 (const void *)&sockopt_val,
757 sizeof(sockopt_val))) < 0) {
758 perror("setsockopt");
761 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
762 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
763 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
764 &sockopt_val, sizeof(sockopt_val))) < 0) {
765 perror("setsockopt");
778 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
779 ret = dgram_get_mtu_overhead(data);
783 * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
784 * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
785 * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
786 * value has been updated to a non-clashing value. However to preserve
787 * binary compatiblity we now respond to both the old value and the new one
789 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
790 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
791 data->peekmode = (unsigned int)num;
800 static int dgram_puts(BIO *bp, const char *str)
805 ret = dgram_write(bp, str, n);
809 # ifndef OPENSSL_NO_SCTP
810 const BIO_METHOD *BIO_s_datagram_sctp(void)
812 return &methods_dgramp_sctp;
815 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
818 int ret, optval = 20000;
819 int auth_data = 0, auth_forward = 0;
821 struct sctp_authchunk auth;
822 struct sctp_authchunks *authchunks;
823 socklen_t sockopt_len;
824 # ifdef SCTP_AUTHENTICATION_EVENT
826 struct sctp_event event;
828 struct sctp_event_subscribe event;
832 bio = BIO_new(BIO_s_datagram_sctp());
835 BIO_set_fd(bio, fd, close_flag);
837 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
838 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
840 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
841 sizeof(struct sctp_authchunk));
844 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
845 ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
848 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
850 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
851 sizeof(struct sctp_authchunk));
854 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
855 ERR_add_error_data(1, "Ensure SCTP AUTH chunks are enabled in kernel");
860 * Test if activation was successful. When using accept(), SCTP-AUTH has
861 * to be activated for the listening socket already, otherwise the
862 * connected socket won't use it. Similarly with connect(): the socket
863 * prior to connection must be activated for SCTP-AUTH
865 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
866 authchunks = OPENSSL_zalloc(sockopt_len);
867 if (authchunks == NULL) {
871 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
874 OPENSSL_free(authchunks);
879 for (p = (unsigned char *)authchunks->gauth_chunks;
880 p < (unsigned char *)authchunks + sockopt_len;
881 p += sizeof(uint8_t)) {
882 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
884 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
888 OPENSSL_free(authchunks);
890 if (!auth_data || !auth_forward) {
892 BIOerr(BIO_F_BIO_NEW_DGRAM_SCTP, ERR_R_SYS_LIB);
893 ERR_add_error_data(1,
894 "Ensure SCTP AUTH chunks are enabled on the "
895 "underlying socket");
899 # ifdef SCTP_AUTHENTICATION_EVENT
901 memset(&event, 0, sizeof(event));
902 event.se_assoc_id = 0;
903 event.se_type = SCTP_AUTHENTICATION_EVENT;
906 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
907 sizeof(struct sctp_event));
913 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
914 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
920 event.sctp_authentication_event = 1;
923 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
924 sizeof(struct sctp_event_subscribe));
933 * Disable partial delivery by setting the min size larger than the max
934 * record size of 2^14 + 2048 + 13
937 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
947 int BIO_dgram_is_sctp(BIO *bio)
949 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
952 static int dgram_sctp_new(BIO *bi)
954 bio_dgram_sctp_data *data = NULL;
958 data = OPENSSL_zalloc(sizeof(*data));
961 # ifdef SCTP_PR_SCTP_NONE
962 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
970 static int dgram_sctp_free(BIO *a)
972 bio_dgram_sctp_data *data;
979 data = (bio_dgram_sctp_data *) a->ptr;
986 # ifdef SCTP_AUTHENTICATION_EVENT
987 void dgram_sctp_handle_auth_free_key_event(BIO *b,
988 union sctp_notification *snp)
991 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
993 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
994 struct sctp_authkeyid authkeyid;
997 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
998 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
999 &authkeyid, sizeof(struct sctp_authkeyid));
1004 static int dgram_sctp_read(BIO *b, char *out, int outl)
1006 int ret = 0, n = 0, i, optval;
1008 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1009 union sctp_notification *snp;
1012 struct cmsghdr *cmsg;
1016 clear_socket_error();
1019 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1022 msg.msg_name = NULL;
1023 msg.msg_namelen = 0;
1026 msg.msg_control = cmsgbuf;
1027 msg.msg_controllen = 512;
1029 n = recvmsg(b->num, &msg, 0);
1037 if (msg.msg_controllen > 0) {
1038 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1039 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1040 if (cmsg->cmsg_level != IPPROTO_SCTP)
1042 # ifdef SCTP_RCVINFO
1043 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1044 struct sctp_rcvinfo *rcvinfo;
1046 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1047 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1048 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1049 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1050 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1051 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1052 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1053 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1057 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1058 struct sctp_sndrcvinfo *sndrcvinfo;
1061 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1062 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1063 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1064 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1065 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1066 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1067 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1068 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1074 if (msg.msg_flags & MSG_NOTIFICATION) {
1075 snp = (union sctp_notification *)out;
1076 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1078 struct sctp_event event;
1080 struct sctp_event_subscribe event;
1081 socklen_t eventsize;
1084 /* disable sender dry event */
1086 memset(&event, 0, sizeof(event));
1087 event.se_assoc_id = 0;
1088 event.se_type = SCTP_SENDER_DRY_EVENT;
1090 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1091 sizeof(struct sctp_event));
1097 eventsize = sizeof(struct sctp_event_subscribe);
1098 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1105 event.sctp_sender_dry_event = 0;
1107 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1108 sizeof(struct sctp_event_subscribe));
1115 # ifdef SCTP_AUTHENTICATION_EVENT
1116 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1117 dgram_sctp_handle_auth_free_key_event(b, snp);
1120 if (data->handle_notifications != NULL)
1121 data->handle_notifications(b, data->notification_context,
1124 memset(out, 0, outl);
1128 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1131 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1132 /* Partial message read, this should never happen! */
1135 * The buffer was too small, this means the peer sent a message
1136 * that was larger than allowed.
1142 * Test if socket buffer can handle max record size (2^14 + 2048
1145 optlen = (socklen_t) sizeof(int);
1146 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1148 OPENSSL_assert(optval >= 18445);
1151 * Test if SCTP doesn't partially deliver below max record size
1152 * (2^14 + 2048 + 13)
1154 optlen = (socklen_t) sizeof(int);
1156 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1159 OPENSSL_assert(optval >= 18445);
1162 * Partially delivered notification??? Probably a bug....
1164 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1167 * Everything seems ok till now, so it's most likely a message
1168 * dropped by PR-SCTP.
1170 memset(out, 0, outl);
1171 BIO_set_retry_read(b);
1175 BIO_clear_retry_flags(b);
1177 if (BIO_dgram_should_retry(ret)) {
1178 BIO_set_retry_read(b);
1179 data->_errno = get_last_socket_error();
1183 /* Test if peer uses SCTP-AUTH before continuing */
1184 if (!data->peer_auth_tested) {
1185 int ii, auth_data = 0, auth_forward = 0;
1187 struct sctp_authchunks *authchunks;
1190 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1191 authchunks = OPENSSL_malloc(optlen);
1192 if (authchunks == NULL) {
1193 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1196 memset(authchunks, 0, optlen);
1197 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1198 authchunks, &optlen);
1201 for (p = (unsigned char *)authchunks->gauth_chunks;
1202 p < (unsigned char *)authchunks + optlen;
1203 p += sizeof(uint8_t)) {
1204 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1206 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1210 OPENSSL_free(authchunks);
1212 if (!auth_data || !auth_forward) {
1213 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1217 data->peer_auth_tested = 1;
1224 * dgram_sctp_write - send message on SCTP socket
1225 * @b: BIO to write to
1227 * @inl: amount of bytes in @in to send
1229 * Returns -1 on error or the sent amount of bytes on success
1231 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1234 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1235 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1236 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1237 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1238 struct iovec iov[1];
1240 struct cmsghdr *cmsg;
1241 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1242 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1243 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1244 struct sctp_sndinfo *sndinfo;
1245 struct sctp_prinfo *prinfo;
1247 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1248 struct sctp_sndrcvinfo *sndrcvinfo;
1251 clear_socket_error();
1254 * If we're send anything else than application data, disable all user
1255 * parameters and flags.
1258 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1259 # ifdef SCTP_SACK_IMMEDIATELY
1260 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1262 sinfo = &handshake_sinfo;
1265 /* We can only send a shutdown alert if the socket is dry */
1266 if (data->save_shutdown) {
1267 ret = BIO_dgram_sctp_wait_for_dry(b);
1271 BIO_clear_retry_flags(b);
1272 BIO_set_retry_write(b);
1277 iov[0].iov_base = (char *)in;
1278 iov[0].iov_len = inl;
1279 msg.msg_name = NULL;
1280 msg.msg_namelen = 0;
1283 msg.msg_control = (caddr_t) cmsgbuf;
1284 msg.msg_controllen = 0;
1286 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1287 cmsg = (struct cmsghdr *)cmsgbuf;
1288 cmsg->cmsg_level = IPPROTO_SCTP;
1289 cmsg->cmsg_type = SCTP_SNDINFO;
1290 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1291 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1292 memset(sndinfo, 0, sizeof(*sndinfo));
1293 sndinfo->snd_sid = sinfo->snd_sid;
1294 sndinfo->snd_flags = sinfo->snd_flags;
1295 sndinfo->snd_ppid = sinfo->snd_ppid;
1296 sndinfo->snd_context = sinfo->snd_context;
1297 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1300 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1301 cmsg->cmsg_level = IPPROTO_SCTP;
1302 cmsg->cmsg_type = SCTP_PRINFO;
1303 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1304 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1305 memset(prinfo, 0, sizeof(*prinfo));
1306 prinfo->pr_policy = pinfo->pr_policy;
1307 prinfo->pr_value = pinfo->pr_value;
1308 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1310 cmsg = (struct cmsghdr *)cmsgbuf;
1311 cmsg->cmsg_level = IPPROTO_SCTP;
1312 cmsg->cmsg_type = SCTP_SNDRCV;
1313 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1314 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1315 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1316 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1317 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1319 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1321 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1322 sndrcvinfo->sinfo_context = sinfo->snd_context;
1323 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1324 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1327 ret = sendmsg(b->num, &msg, 0);
1329 BIO_clear_retry_flags(b);
1331 if (BIO_dgram_should_retry(ret)) {
1332 BIO_set_retry_write(b);
1333 data->_errno = get_last_socket_error();
1339 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1342 bio_dgram_sctp_data *data = NULL;
1343 socklen_t sockopt_len = 0;
1344 struct sctp_authkeyid authkeyid;
1345 struct sctp_authkey *authkey = NULL;
1347 data = (bio_dgram_sctp_data *) b->ptr;
1350 case BIO_CTRL_DGRAM_QUERY_MTU:
1352 * Set to maximum (2^14) and ignore user input to enable transport
1353 * protocol fragmentation. Returns always 2^14.
1358 case BIO_CTRL_DGRAM_SET_MTU:
1360 * Set to maximum (2^14) and ignore input to enable transport
1361 * protocol fragmentation. Returns always 2^14.
1366 case BIO_CTRL_DGRAM_SET_CONNECTED:
1367 case BIO_CTRL_DGRAM_CONNECT:
1368 /* Returns always -1. */
1371 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1373 * SCTP doesn't need the DTLS timer Returns always 1.
1376 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1378 * We allow transport protocol fragmentation so this is irrelevant
1382 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1384 data->in_handshake = 1;
1386 data->in_handshake = 0;
1389 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1390 &data->in_handshake, sizeof(int));
1392 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1394 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1397 /* Get active key */
1398 sockopt_len = sizeof(struct sctp_authkeyid);
1400 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1406 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1407 authkey = OPENSSL_malloc(sockopt_len);
1408 if (authkey == NULL) {
1412 memset(authkey, 0, sockopt_len);
1413 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1414 # ifndef __FreeBSD__
1416 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1417 * and higher work without it.
1419 authkey->sca_keylength = 64;
1421 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1424 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1426 OPENSSL_free(authkey);
1431 /* Reset active key */
1432 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1433 &authkeyid, sizeof(struct sctp_authkeyid));
1438 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1439 /* Returns 0 on success, -1 otherwise. */
1441 /* Get active key */
1442 sockopt_len = sizeof(struct sctp_authkeyid);
1444 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1449 /* Set active key */
1450 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1451 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1452 &authkeyid, sizeof(struct sctp_authkeyid));
1457 * CCS has been sent, so remember that and fall through to check if
1458 * we need to deactivate an old key
1463 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1464 /* Returns 0 on success, -1 otherwise. */
1467 * Has this command really been called or is this just a
1470 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1474 * CSS has been both, received and sent, so deactivate an old key
1476 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1477 /* Get active key */
1478 sockopt_len = sizeof(struct sctp_authkeyid);
1480 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1481 &authkeyid, &sockopt_len);
1486 * Deactivate key or delete second last key if
1487 * SCTP_AUTHENTICATION_EVENT is not available.
1489 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1490 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1491 sockopt_len = sizeof(struct sctp_authkeyid);
1492 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1493 &authkeyid, sockopt_len);
1497 # ifndef SCTP_AUTHENTICATION_EVENT
1498 if (authkeyid.scact_keynumber > 0) {
1499 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1500 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1501 &authkeyid, sizeof(struct sctp_authkeyid));
1511 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1512 /* Returns the size of the copied struct. */
1513 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1514 num = sizeof(struct bio_dgram_sctp_sndinfo);
1516 memcpy(ptr, &(data->sndinfo), num);
1519 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1520 /* Returns the size of the copied struct. */
1521 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1522 num = sizeof(struct bio_dgram_sctp_sndinfo);
1524 memcpy(&(data->sndinfo), ptr, num);
1526 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1527 /* Returns the size of the copied struct. */
1528 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1529 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1531 memcpy(ptr, &data->rcvinfo, num);
1535 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1536 /* Returns the size of the copied struct. */
1537 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1538 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1540 memcpy(&(data->rcvinfo), ptr, num);
1542 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1543 /* Returns the size of the copied struct. */
1544 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1545 num = sizeof(struct bio_dgram_sctp_prinfo);
1547 memcpy(ptr, &(data->prinfo), num);
1550 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1551 /* Returns the size of the copied struct. */
1552 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1553 num = sizeof(struct bio_dgram_sctp_prinfo);
1555 memcpy(&(data->prinfo), ptr, num);
1557 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1558 /* Returns always 1. */
1560 data->save_shutdown = 1;
1562 data->save_shutdown = 0;
1567 * Pass to default ctrl function to process SCTP unspecific commands
1569 ret = dgram_ctrl(b, cmd, num, ptr);
1575 int BIO_dgram_sctp_notification_cb(BIO *b,
1576 void (*handle_notifications) (BIO *bio,
1582 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1584 if (handle_notifications != NULL) {
1585 data->handle_notifications = handle_notifications;
1586 data->notification_context = context;
1594 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1595 * @b: The BIO to check for the dry event
1597 * Wait until the peer confirms all packets have been received, and so that
1598 * our kernel doesn't have anything to send anymore. This is only received by
1599 * the peer's kernel, not the application.
1603 * 0 when not dry yet
1606 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1611 union sctp_notification snp;
1615 struct sctp_event event;
1617 struct sctp_event_subscribe event;
1618 socklen_t eventsize;
1620 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1622 /* set sender dry event */
1624 memset(&event, 0, sizeof(event));
1625 event.se_assoc_id = 0;
1626 event.se_type = SCTP_SENDER_DRY_EVENT;
1629 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1630 sizeof(struct sctp_event));
1632 eventsize = sizeof(struct sctp_event_subscribe);
1633 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1637 event.sctp_sender_dry_event = 1;
1640 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1641 sizeof(struct sctp_event_subscribe));
1646 /* peek for notification */
1647 memset(&snp, 0, sizeof(snp));
1648 iov.iov_base = (char *)&snp;
1649 iov.iov_len = sizeof(union sctp_notification);
1650 msg.msg_name = NULL;
1651 msg.msg_namelen = 0;
1654 msg.msg_control = NULL;
1655 msg.msg_controllen = 0;
1658 n = recvmsg(b->num, &msg, MSG_PEEK);
1660 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1661 && (get_last_socket_error() != EWOULDBLOCK))
1667 /* if we find a notification, process it and try again if necessary */
1668 while (msg.msg_flags & MSG_NOTIFICATION) {
1669 memset(&snp, 0, sizeof(snp));
1670 iov.iov_base = (char *)&snp;
1671 iov.iov_len = sizeof(union sctp_notification);
1672 msg.msg_name = NULL;
1673 msg.msg_namelen = 0;
1676 msg.msg_control = NULL;
1677 msg.msg_controllen = 0;
1680 n = recvmsg(b->num, &msg, 0);
1682 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1683 && (get_last_socket_error() != EWOULDBLOCK))
1689 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1692 /* disable sender dry event */
1694 memset(&event, 0, sizeof(event));
1695 event.se_assoc_id = 0;
1696 event.se_type = SCTP_SENDER_DRY_EVENT;
1699 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1700 sizeof(struct sctp_event));
1702 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1704 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1709 event.sctp_sender_dry_event = 0;
1712 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1713 sizeof(struct sctp_event_subscribe));
1718 # ifdef SCTP_AUTHENTICATION_EVENT
1719 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1720 dgram_sctp_handle_auth_free_key_event(b, &snp);
1723 if (data->handle_notifications != NULL)
1724 data->handle_notifications(b, data->notification_context,
1727 /* found notification, peek again */
1728 memset(&snp, 0, sizeof(snp));
1729 iov.iov_base = (char *)&snp;
1730 iov.iov_len = sizeof(union sctp_notification);
1731 msg.msg_name = NULL;
1732 msg.msg_namelen = 0;
1735 msg.msg_control = NULL;
1736 msg.msg_controllen = 0;
1739 /* if we have seen the dry already, don't wait */
1741 sockflags = fcntl(b->num, F_GETFL, 0);
1742 fcntl(b->num, F_SETFL, O_NONBLOCK);
1745 n = recvmsg(b->num, &msg, MSG_PEEK);
1748 fcntl(b->num, F_SETFL, sockflags);
1752 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1753 && (get_last_socket_error() != EWOULDBLOCK))
1760 /* read anything else */
1764 int BIO_dgram_sctp_msg_waiting(BIO *b)
1767 union sctp_notification snp;
1770 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1772 /* Check if there are any messages waiting to be read */
1774 memset(&snp, 0, sizeof(snp));
1775 iov.iov_base = (char *)&snp;
1776 iov.iov_len = sizeof(union sctp_notification);
1777 msg.msg_name = NULL;
1778 msg.msg_namelen = 0;
1781 msg.msg_control = NULL;
1782 msg.msg_controllen = 0;
1785 sockflags = fcntl(b->num, F_GETFL, 0);
1786 fcntl(b->num, F_SETFL, O_NONBLOCK);
1787 n = recvmsg(b->num, &msg, MSG_PEEK);
1788 fcntl(b->num, F_SETFL, sockflags);
1790 /* if notification, process and try again */
1791 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1792 # ifdef SCTP_AUTHENTICATION_EVENT
1793 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1794 dgram_sctp_handle_auth_free_key_event(b, &snp);
1797 memset(&snp, 0, sizeof(snp));
1798 iov.iov_base = (char *)&snp;
1799 iov.iov_len = sizeof(union sctp_notification);
1800 msg.msg_name = NULL;
1801 msg.msg_namelen = 0;
1804 msg.msg_control = NULL;
1805 msg.msg_controllen = 0;
1807 n = recvmsg(b->num, &msg, 0);
1809 if (data->handle_notifications != NULL)
1810 data->handle_notifications(b, data->notification_context,
1814 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1816 /* Return 1 if there is a message to be read, return 0 otherwise. */
1823 static int dgram_sctp_puts(BIO *bp, const char *str)
1828 ret = dgram_sctp_write(bp, str, n);
1833 static int BIO_dgram_should_retry(int i)
1837 if ((i == 0) || (i == -1)) {
1838 err = get_last_socket_error();
1840 # if defined(OPENSSL_SYS_WINDOWS)
1842 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1843 * this point, the error code was overwritten by another system call
1844 * before this error handling is called.
1848 return BIO_dgram_non_fatal_error(err);
1853 int BIO_dgram_non_fatal_error(int err)
1856 # if defined(OPENSSL_SYS_WINDOWS)
1857 # if defined(WSAEWOULDBLOCK)
1858 case WSAEWOULDBLOCK:
1863 # ifdef WSAEWOULDBLOCK
1864 # if WSAEWOULDBLOCK != EWOULDBLOCK
1877 # if EWOULDBLOCK != EAGAIN
1901 static void get_current_time(struct timeval *t)
1903 # if defined(_WIN32)
1906 unsigned __int64 ul;
1911 SystemTimeToFileTime(&st, &now.ft);
1913 now.ul -= 116444736000000000ULL;
1915 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1917 t->tv_sec = (long)(now.ul / 10000000);
1918 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1920 gettimeofday(t, NULL);