2 * Copyright 2005-2016 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 # if !(defined(_WIN32) || defined(OPENSSL_SYS_VMS))
17 # include <sys/time.h>
19 # if defined(OPENSSL_SYS_VMS)
20 # include <sys/timeb.h>
23 # ifndef OPENSSL_NO_SCTP
24 # include <netinet/sctp.h>
26 # define OPENSSL_SCTP_DATA_CHUNK_TYPE 0x00
27 # define OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE 0xc0
30 # if defined(OPENSSL_SYS_LINUX) && !defined(IP_MTU)
31 # define IP_MTU 14 /* linux is lame */
34 # if OPENSSL_USE_IPV6 && !defined(IPPROTO_IPV6)
35 # define IPPROTO_IPV6 41 /* windows is lame */
38 # if defined(__FreeBSD__) && defined(IN6_IS_ADDR_V4MAPPED)
39 /* Standard definition causes type-punning problems. */
40 # undef IN6_IS_ADDR_V4MAPPED
41 # define s6_addr32 __u6_addr.__u6_addr32
42 # define IN6_IS_ADDR_V4MAPPED(a) \
43 (((a)->s6_addr32[0] == 0) && \
44 ((a)->s6_addr32[1] == 0) && \
45 ((a)->s6_addr32[2] == htonl(0x0000ffff)))
48 static int dgram_write(BIO *h, const char *buf, int num);
49 static int dgram_read(BIO *h, char *buf, int size);
50 static int dgram_puts(BIO *h, const char *str);
51 static long dgram_ctrl(BIO *h, int cmd, long arg1, void *arg2);
52 static int dgram_new(BIO *h);
53 static int dgram_free(BIO *data);
54 static int dgram_clear(BIO *bio);
56 # ifndef OPENSSL_NO_SCTP
57 static int dgram_sctp_write(BIO *h, const char *buf, int num);
58 static int dgram_sctp_read(BIO *h, char *buf, int size);
59 static int dgram_sctp_puts(BIO *h, const char *str);
60 static long dgram_sctp_ctrl(BIO *h, int cmd, long arg1, void *arg2);
61 static int dgram_sctp_new(BIO *h);
62 static int dgram_sctp_free(BIO *data);
63 # ifdef SCTP_AUTHENTICATION_EVENT
64 static void dgram_sctp_handle_auth_free_key_event(BIO *b, union sctp_notification
69 static int BIO_dgram_should_retry(int s);
71 static void get_current_time(struct timeval *t);
73 static const BIO_METHOD methods_dgramp = {
79 NULL, /* dgram_gets, */
83 NULL, /* dgram_callback_ctrl */
86 # ifndef OPENSSL_NO_SCTP
87 static const BIO_METHOD methods_dgramp_sctp = {
89 "datagram sctp socket",
93 NULL, /* dgram_gets, */
97 NULL, /* dgram_callback_ctrl */
101 typedef struct bio_dgram_data_st {
103 unsigned int connected;
106 struct timeval next_timeout;
107 struct timeval socket_timeout;
108 unsigned int peekmode;
111 # ifndef OPENSSL_NO_SCTP
112 typedef struct bio_dgram_sctp_save_message_st {
116 } bio_dgram_sctp_save_message;
118 typedef struct bio_dgram_sctp_data_st {
120 unsigned int connected;
123 struct bio_dgram_sctp_sndinfo sndinfo;
124 struct bio_dgram_sctp_rcvinfo rcvinfo;
125 struct bio_dgram_sctp_prinfo prinfo;
126 void (*handle_notifications) (BIO *bio, void *context, void *buf);
127 void *notification_context;
132 int peer_auth_tested;
133 } bio_dgram_sctp_data;
136 const BIO_METHOD *BIO_s_datagram(void)
138 return (&methods_dgramp);
141 BIO *BIO_new_dgram(int fd, int close_flag)
145 ret = BIO_new(BIO_s_datagram());
148 BIO_set_fd(ret, fd, close_flag);
152 static int dgram_new(BIO *bi)
154 bio_dgram_data *data = OPENSSL_zalloc(sizeof(*data));
162 static int dgram_free(BIO *a)
164 bio_dgram_data *data;
171 data = (bio_dgram_data *)a->ptr;
177 static int dgram_clear(BIO *a)
183 BIO_closesocket(a->num);
191 static void dgram_adjust_rcv_timeout(BIO *b)
193 # if defined(SO_RCVTIMEO)
194 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
202 /* Is a timer active? */
203 if (data->next_timeout.tv_sec > 0 || data->next_timeout.tv_usec > 0) {
204 struct timeval timenow, timeleft;
206 /* Read current socket timeout */
207 # ifdef OPENSSL_SYS_WINDOWS
210 sz.i = sizeof(timeout);
211 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
212 (void *)&timeout, &sz.i) < 0) {
213 perror("getsockopt");
215 data->socket_timeout.tv_sec = timeout / 1000;
216 data->socket_timeout.tv_usec = (timeout % 1000) * 1000;
219 sz.i = sizeof(data->socket_timeout);
220 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
221 &(data->socket_timeout), (void *)&sz) < 0) {
222 perror("getsockopt");
223 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0)
224 OPENSSL_assert(sz.s <= sizeof(data->socket_timeout));
227 /* Get current time */
228 get_current_time(&timenow);
230 /* Calculate time left until timer expires */
231 memcpy(&timeleft, &(data->next_timeout), sizeof(struct timeval));
232 if (timeleft.tv_usec < timenow.tv_usec) {
233 timeleft.tv_usec = 1000000 - timenow.tv_usec + timeleft.tv_usec;
236 timeleft.tv_usec -= timenow.tv_usec;
238 if (timeleft.tv_sec < timenow.tv_sec) {
240 timeleft.tv_usec = 1;
242 timeleft.tv_sec -= timenow.tv_sec;
246 * Adjust socket timeout if next handshake message timer will expire
249 if ((data->socket_timeout.tv_sec == 0
250 && data->socket_timeout.tv_usec == 0)
251 || (data->socket_timeout.tv_sec > timeleft.tv_sec)
252 || (data->socket_timeout.tv_sec == timeleft.tv_sec
253 && data->socket_timeout.tv_usec >= timeleft.tv_usec)) {
254 # ifdef OPENSSL_SYS_WINDOWS
255 timeout = timeleft.tv_sec * 1000 + timeleft.tv_usec / 1000;
256 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
257 (void *)&timeout, sizeof(timeout)) < 0) {
258 perror("setsockopt");
261 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, &timeleft,
262 sizeof(struct timeval)) < 0) {
263 perror("setsockopt");
271 static void dgram_reset_rcv_timeout(BIO *b)
273 # if defined(SO_RCVTIMEO)
274 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 # ifdef OPENSSL_SYS_WINDOWS
279 int timeout = data->socket_timeout.tv_sec * 1000 +
280 data->socket_timeout.tv_usec / 1000;
281 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
282 (void *)&timeout, sizeof(timeout)) < 0) {
283 perror("setsockopt");
287 (b->num, SOL_SOCKET, SO_RCVTIMEO, &(data->socket_timeout),
288 sizeof(struct timeval)) < 0) {
289 perror("setsockopt");
296 static int dgram_read(BIO *b, char *out, int outl)
299 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
303 socklen_t len = sizeof(peer);
306 clear_socket_error();
307 memset(&peer, 0, sizeof(peer));
308 dgram_adjust_rcv_timeout(b);
311 ret = recvfrom(b->num, out, outl, flags,
312 BIO_ADDR_sockaddr_noconst(&peer), &len);
314 if (!data->connected && ret >= 0)
315 BIO_ctrl(b, BIO_CTRL_DGRAM_SET_PEER, 0, &peer);
317 BIO_clear_retry_flags(b);
319 if (BIO_dgram_should_retry(ret)) {
320 BIO_set_retry_read(b);
321 data->_errno = get_last_socket_error();
325 dgram_reset_rcv_timeout(b);
330 static int dgram_write(BIO *b, const char *in, int inl)
333 bio_dgram_data *data = (bio_dgram_data *)b->ptr;
334 clear_socket_error();
337 ret = writesocket(b->num, in, inl);
339 int peerlen = BIO_ADDR_sockaddr_size(&data->peer);
341 # if defined(NETWARE_CLIB) && defined(NETWARE_BSDSOCK)
342 ret = sendto(b->num, (char *)in, inl, 0,
343 BIO_ADDR_sockaddr(&data->peer), peerlen);
345 ret = sendto(b->num, in, inl, 0,
346 BIO_ADDR_sockaddr(&data->peer), peerlen);
350 BIO_clear_retry_flags(b);
352 if (BIO_dgram_should_retry(ret)) {
353 BIO_set_retry_write(b);
354 data->_errno = get_last_socket_error();
360 static long dgram_get_mtu_overhead(bio_dgram_data *data)
364 switch (BIO_ADDR_family(&data->peer)) {
367 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
374 # ifdef IN6_IS_ADDR_V4MAPPED
375 struct in6_addr tmp_addr;
376 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
377 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
379 * Assume this is UDP - 20 bytes for IP, 8 bytes for UDP
385 * Assume this is UDP - 40 bytes for IP, 8 bytes for UDP
392 /* We don't know. Go with the historical default */
399 static long dgram_ctrl(BIO *b, int cmd, long num, void *ptr)
403 bio_dgram_data *data = NULL;
406 # if defined(OPENSSL_SYS_LINUX) && (defined(IP_MTU_DISCOVER) || defined(IP_MTU))
407 socklen_t sockopt_len; /* assume that system supporting IP_MTU is
408 * modern enough to define socklen_t */
413 data = (bio_dgram_data *)b->ptr;
425 b->num = *((int *)ptr);
426 b->shutdown = (int)num;
438 case BIO_CTRL_GET_CLOSE:
441 case BIO_CTRL_SET_CLOSE:
442 b->shutdown = (int)num;
444 case BIO_CTRL_PENDING:
445 case BIO_CTRL_WPENDING:
452 case BIO_CTRL_DGRAM_CONNECT:
453 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
455 /* (Linux)kernel sets DF bit on outgoing IP packets */
456 case BIO_CTRL_DGRAM_MTU_DISCOVER:
457 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DO)
458 addr_len = (socklen_t) sizeof(addr);
459 memset(&addr, 0, sizeof(addr));
460 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
464 switch (addr.sa.sa_family) {
466 sockopt_val = IP_PMTUDISC_DO;
467 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
468 &sockopt_val, sizeof(sockopt_val))) < 0)
469 perror("setsockopt");
471 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DO)
473 sockopt_val = IPV6_PMTUDISC_DO;
474 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
475 &sockopt_val, sizeof(sockopt_val))) < 0)
476 perror("setsockopt");
487 case BIO_CTRL_DGRAM_QUERY_MTU:
488 # if defined(OPENSSL_SYS_LINUX) && defined(IP_MTU)
489 addr_len = (socklen_t) sizeof(addr);
490 memset(&addr, 0, sizeof(addr));
491 if (getsockname(b->num, &addr.sa, &addr_len) < 0) {
495 sockopt_len = sizeof(sockopt_val);
496 switch (addr.sa.sa_family) {
499 getsockopt(b->num, IPPROTO_IP, IP_MTU, (void *)&sockopt_val,
500 &sockopt_len)) < 0 || sockopt_val < 0) {
504 * we assume that the transport protocol is UDP and no IP
507 data->mtu = sockopt_val - 8 - 20;
511 # if OPENSSL_USE_IPV6 && defined(IPV6_MTU)
514 getsockopt(b->num, IPPROTO_IPV6, IPV6_MTU,
515 (void *)&sockopt_val, &sockopt_len)) < 0
516 || sockopt_val < 0) {
520 * we assume that the transport protocol is UDP and no IPV6
523 data->mtu = sockopt_val - 8 - 40;
536 case BIO_CTRL_DGRAM_GET_FALLBACK_MTU:
537 ret = -dgram_get_mtu_overhead(data);
538 switch (BIO_ADDR_family(&data->peer)) {
542 # if OPENSSL_USE_IPV6
545 # ifdef IN6_IS_ADDR_V4MAPPED
546 struct in6_addr tmp_addr;
547 if (BIO_ADDR_rawaddress(&data->peer, &tmp_addr, NULL)
548 && IN6_IS_ADDR_V4MAPPED(&tmp_addr))
561 case BIO_CTRL_DGRAM_GET_MTU:
563 case BIO_CTRL_DGRAM_SET_MTU:
567 case BIO_CTRL_DGRAM_SET_CONNECTED:
570 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
573 memset(&data->peer, 0, sizeof(data->peer));
576 case BIO_CTRL_DGRAM_GET_PEER:
577 ret = BIO_ADDR_sockaddr_size(&data->peer);
578 /* FIXME: if num < ret, we will only return part of an address.
579 That should bee an error, no? */
580 if (num == 0 || num > ret)
582 memcpy(ptr, &data->peer, (ret = num));
584 case BIO_CTRL_DGRAM_SET_PEER:
585 BIO_ADDR_make(&data->peer, BIO_ADDR_sockaddr((BIO_ADDR *)ptr));
587 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
588 memcpy(&(data->next_timeout), ptr, sizeof(struct timeval));
590 # if defined(SO_RCVTIMEO)
591 case BIO_CTRL_DGRAM_SET_RECV_TIMEOUT:
592 # ifdef OPENSSL_SYS_WINDOWS
594 struct timeval *tv = (struct timeval *)ptr;
595 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
596 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
597 (void *)&timeout, sizeof(timeout)) < 0) {
598 perror("setsockopt");
603 if (setsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO, ptr,
604 sizeof(struct timeval)) < 0) {
605 perror("setsockopt");
610 case BIO_CTRL_DGRAM_GET_RECV_TIMEOUT:
618 # ifdef OPENSSL_SYS_WINDOWS
620 struct timeval *tv = (struct timeval *)ptr;
622 sz.i = sizeof(timeout);
623 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
624 (void *)&timeout, &sz.i) < 0) {
625 perror("getsockopt");
628 tv->tv_sec = timeout / 1000;
629 tv->tv_usec = (timeout % 1000) * 1000;
633 sz.i = sizeof(struct timeval);
634 if (getsockopt(b->num, SOL_SOCKET, SO_RCVTIMEO,
635 ptr, (void *)&sz) < 0) {
636 perror("getsockopt");
638 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
639 OPENSSL_assert(sz.s <= sizeof(struct timeval));
647 # if defined(SO_SNDTIMEO)
648 case BIO_CTRL_DGRAM_SET_SEND_TIMEOUT:
649 # ifdef OPENSSL_SYS_WINDOWS
651 struct timeval *tv = (struct timeval *)ptr;
652 int timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
653 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
654 (void *)&timeout, sizeof(timeout)) < 0) {
655 perror("setsockopt");
660 if (setsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO, ptr,
661 sizeof(struct timeval)) < 0) {
662 perror("setsockopt");
667 case BIO_CTRL_DGRAM_GET_SEND_TIMEOUT:
675 # ifdef OPENSSL_SYS_WINDOWS
677 struct timeval *tv = (struct timeval *)ptr;
679 sz.i = sizeof(timeout);
680 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
681 (void *)&timeout, &sz.i) < 0) {
682 perror("getsockopt");
685 tv->tv_sec = timeout / 1000;
686 tv->tv_usec = (timeout % 1000) * 1000;
690 sz.i = sizeof(struct timeval);
691 if (getsockopt(b->num, SOL_SOCKET, SO_SNDTIMEO,
692 ptr, (void *)&sz) < 0) {
693 perror("getsockopt");
695 } else if (sizeof(sz.s) != sizeof(sz.i) && sz.i == 0) {
696 OPENSSL_assert(sz.s <= sizeof(struct timeval));
704 case BIO_CTRL_DGRAM_GET_SEND_TIMER_EXP:
706 case BIO_CTRL_DGRAM_GET_RECV_TIMER_EXP:
707 # ifdef OPENSSL_SYS_WINDOWS
708 d_errno = (data->_errno == WSAETIMEDOUT);
710 d_errno = (data->_errno == EAGAIN);
719 case BIO_CTRL_DGRAM_MTU_EXCEEDED:
720 if (data->_errno == EMSGSIZE) {
727 case BIO_CTRL_DGRAM_SET_DONT_FRAG:
728 sockopt_val = num ? 1 : 0;
730 switch (data->peer.sa.sa_family) {
732 # if defined(IP_DONTFRAG)
733 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAG,
734 &sockopt_val, sizeof(sockopt_val))) < 0) {
735 perror("setsockopt");
738 # elif defined(OPENSSL_SYS_LINUX) && defined(IP_MTU_DISCOVER) && defined (IP_PMTUDISC_PROBE)
739 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
740 (ret = setsockopt(b->num, IPPROTO_IP, IP_MTU_DISCOVER,
741 &sockopt_val, sizeof(sockopt_val))) < 0) {
742 perror("setsockopt");
745 # elif defined(OPENSSL_SYS_WINDOWS) && defined(IP_DONTFRAGMENT)
746 if ((ret = setsockopt(b->num, IPPROTO_IP, IP_DONTFRAGMENT,
747 (const char *)&sockopt_val,
748 sizeof(sockopt_val))) < 0) {
749 perror("setsockopt");
756 # if OPENSSL_USE_IPV6
758 # if defined(IPV6_DONTFRAG)
759 if ((ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_DONTFRAG,
760 (const void *)&sockopt_val,
761 sizeof(sockopt_val))) < 0) {
762 perror("setsockopt");
765 # elif defined(OPENSSL_SYS_LINUX) && defined(IPV6_MTUDISCOVER)
766 if ((sockopt_val = num ? IP_PMTUDISC_PROBE : IP_PMTUDISC_DONT),
767 (ret = setsockopt(b->num, IPPROTO_IPV6, IPV6_MTU_DISCOVER,
768 &sockopt_val, sizeof(sockopt_val))) < 0) {
769 perror("setsockopt");
782 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
783 ret = dgram_get_mtu_overhead(data);
787 * BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE is used here for compatibility
788 * reasons. When BIO_CTRL_DGRAM_SET_PEEK_MODE was first defined its value
789 * was incorrectly clashing with BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE. The
790 * value has been updated to a non-clashing value. However to preserve
791 * binary compatiblity we now respond to both the old value and the new one
793 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
794 case BIO_CTRL_DGRAM_SET_PEEK_MODE:
795 data->peekmode = (unsigned int)num;
804 static int dgram_puts(BIO *bp, const char *str)
809 ret = dgram_write(bp, str, n);
813 # ifndef OPENSSL_NO_SCTP
814 const BIO_METHOD *BIO_s_datagram_sctp(void)
816 return (&methods_dgramp_sctp);
819 BIO *BIO_new_dgram_sctp(int fd, int close_flag)
822 int ret, optval = 20000;
823 int auth_data = 0, auth_forward = 0;
825 struct sctp_authchunk auth;
826 struct sctp_authchunks *authchunks;
827 socklen_t sockopt_len;
828 # ifdef SCTP_AUTHENTICATION_EVENT
830 struct sctp_event event;
832 struct sctp_event_subscribe event;
836 bio = BIO_new(BIO_s_datagram_sctp());
839 BIO_set_fd(bio, fd, close_flag);
841 /* Activate SCTP-AUTH for DATA and FORWARD-TSN chunks */
842 auth.sauth_chunk = OPENSSL_SCTP_DATA_CHUNK_TYPE;
844 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
845 sizeof(struct sctp_authchunk));
850 auth.sauth_chunk = OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE;
852 setsockopt(fd, IPPROTO_SCTP, SCTP_AUTH_CHUNK, &auth,
853 sizeof(struct sctp_authchunk));
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.
864 sockopt_len = (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
865 authchunks = OPENSSL_zalloc(sockopt_len);
866 if (authchunks == NULL) {
870 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_LOCAL_AUTH_CHUNKS, authchunks,
873 OPENSSL_free(authchunks);
878 for (p = (unsigned char *)authchunks->gauth_chunks;
879 p < (unsigned char *)authchunks + sockopt_len;
880 p += sizeof(uint8_t)) {
881 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
883 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
887 OPENSSL_free(authchunks);
889 OPENSSL_assert(auth_data);
890 OPENSSL_assert(auth_forward);
892 # ifdef SCTP_AUTHENTICATION_EVENT
894 memset(&event, 0, sizeof(event));
895 event.se_assoc_id = 0;
896 event.se_type = SCTP_AUTHENTICATION_EVENT;
899 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENT, &event,
900 sizeof(struct sctp_event));
906 sockopt_len = (socklen_t) sizeof(struct sctp_event_subscribe);
907 ret = getsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event, &sockopt_len);
913 event.sctp_authentication_event = 1;
916 setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &event,
917 sizeof(struct sctp_event_subscribe));
926 * Disable partial delivery by setting the min size larger than the max
927 * record size of 2^14 + 2048 + 13
930 setsockopt(fd, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT, &optval,
940 int BIO_dgram_is_sctp(BIO *bio)
942 return (BIO_method_type(bio) == BIO_TYPE_DGRAM_SCTP);
945 static int dgram_sctp_new(BIO *bi)
947 bio_dgram_sctp_data *data = NULL;
951 data = OPENSSL_zalloc(sizeof(*data));
954 # ifdef SCTP_PR_SCTP_NONE
955 data->prinfo.pr_policy = SCTP_PR_SCTP_NONE;
963 static int dgram_sctp_free(BIO *a)
965 bio_dgram_sctp_data *data;
972 data = (bio_dgram_sctp_data *) a->ptr;
979 # ifdef SCTP_AUTHENTICATION_EVENT
980 void dgram_sctp_handle_auth_free_key_event(BIO *b,
981 union sctp_notification *snp)
984 struct sctp_authkey_event *authkeyevent = &snp->sn_auth_event;
986 if (authkeyevent->auth_indication == SCTP_AUTH_FREE_KEY) {
987 struct sctp_authkeyid authkeyid;
990 authkeyid.scact_keynumber = authkeyevent->auth_keynumber;
991 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
992 &authkeyid, sizeof(struct sctp_authkeyid));
997 static int dgram_sctp_read(BIO *b, char *out, int outl)
999 int ret = 0, n = 0, i, optval;
1001 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1002 union sctp_notification *snp;
1005 struct cmsghdr *cmsg;
1009 clear_socket_error();
1012 memset(&data->rcvinfo, 0, sizeof(data->rcvinfo));
1015 msg.msg_name = NULL;
1016 msg.msg_namelen = 0;
1019 msg.msg_control = cmsgbuf;
1020 msg.msg_controllen = 512;
1022 n = recvmsg(b->num, &msg, 0);
1030 if (msg.msg_controllen > 0) {
1031 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg;
1032 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1033 if (cmsg->cmsg_level != IPPROTO_SCTP)
1035 # ifdef SCTP_RCVINFO
1036 if (cmsg->cmsg_type == SCTP_RCVINFO) {
1037 struct sctp_rcvinfo *rcvinfo;
1039 rcvinfo = (struct sctp_rcvinfo *)CMSG_DATA(cmsg);
1040 data->rcvinfo.rcv_sid = rcvinfo->rcv_sid;
1041 data->rcvinfo.rcv_ssn = rcvinfo->rcv_ssn;
1042 data->rcvinfo.rcv_flags = rcvinfo->rcv_flags;
1043 data->rcvinfo.rcv_ppid = rcvinfo->rcv_ppid;
1044 data->rcvinfo.rcv_tsn = rcvinfo->rcv_tsn;
1045 data->rcvinfo.rcv_cumtsn = rcvinfo->rcv_cumtsn;
1046 data->rcvinfo.rcv_context = rcvinfo->rcv_context;
1050 if (cmsg->cmsg_type == SCTP_SNDRCV) {
1051 struct sctp_sndrcvinfo *sndrcvinfo;
1054 (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1055 data->rcvinfo.rcv_sid = sndrcvinfo->sinfo_stream;
1056 data->rcvinfo.rcv_ssn = sndrcvinfo->sinfo_ssn;
1057 data->rcvinfo.rcv_flags = sndrcvinfo->sinfo_flags;
1058 data->rcvinfo.rcv_ppid = sndrcvinfo->sinfo_ppid;
1059 data->rcvinfo.rcv_tsn = sndrcvinfo->sinfo_tsn;
1060 data->rcvinfo.rcv_cumtsn = sndrcvinfo->sinfo_cumtsn;
1061 data->rcvinfo.rcv_context = sndrcvinfo->sinfo_context;
1067 if (msg.msg_flags & MSG_NOTIFICATION) {
1068 snp = (union sctp_notification *)out;
1069 if (snp->sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1071 struct sctp_event event;
1073 struct sctp_event_subscribe event;
1074 socklen_t eventsize;
1077 /* disable sender dry event */
1079 memset(&event, 0, sizeof(event));
1080 event.se_assoc_id = 0;
1081 event.se_type = SCTP_SENDER_DRY_EVENT;
1083 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1084 sizeof(struct sctp_event));
1090 eventsize = sizeof(struct sctp_event_subscribe);
1091 i = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1098 event.sctp_sender_dry_event = 0;
1100 i = setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1101 sizeof(struct sctp_event_subscribe));
1108 # ifdef SCTP_AUTHENTICATION_EVENT
1109 if (snp->sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1110 dgram_sctp_handle_auth_free_key_event(b, snp);
1113 if (data->handle_notifications != NULL)
1114 data->handle_notifications(b, data->notification_context,
1117 memset(out, 0, outl);
1121 while ((msg.msg_flags & MSG_NOTIFICATION) && (msg.msg_flags & MSG_EOR)
1124 if (ret > 0 && !(msg.msg_flags & MSG_EOR)) {
1125 /* Partial message read, this should never happen! */
1128 * The buffer was too small, this means the peer sent a message
1129 * that was larger than allowed.
1135 * Test if socket buffer can handle max record size (2^14 + 2048
1138 optlen = (socklen_t) sizeof(int);
1139 ret = getsockopt(b->num, SOL_SOCKET, SO_RCVBUF, &optval, &optlen);
1141 OPENSSL_assert(optval >= 18445);
1144 * Test if SCTP doesn't partially deliver below max record size
1145 * (2^14 + 2048 + 13)
1147 optlen = (socklen_t) sizeof(int);
1149 getsockopt(b->num, IPPROTO_SCTP, SCTP_PARTIAL_DELIVERY_POINT,
1152 OPENSSL_assert(optval >= 18445);
1155 * Partially delivered notification??? Probably a bug....
1157 OPENSSL_assert(!(msg.msg_flags & MSG_NOTIFICATION));
1160 * Everything seems ok till now, so it's most likely a message
1161 * dropped by PR-SCTP.
1163 memset(out, 0, outl);
1164 BIO_set_retry_read(b);
1168 BIO_clear_retry_flags(b);
1170 if (BIO_dgram_should_retry(ret)) {
1171 BIO_set_retry_read(b);
1172 data->_errno = get_last_socket_error();
1176 /* Test if peer uses SCTP-AUTH before continuing */
1177 if (!data->peer_auth_tested) {
1178 int ii, auth_data = 0, auth_forward = 0;
1180 struct sctp_authchunks *authchunks;
1183 (socklen_t) (sizeof(sctp_assoc_t) + 256 * sizeof(uint8_t));
1184 authchunks = OPENSSL_malloc(optlen);
1185 if (authchunks == NULL) {
1186 BIOerr(BIO_F_DGRAM_SCTP_READ, ERR_R_MALLOC_FAILURE);
1189 memset(authchunks, 0, optlen);
1190 ii = getsockopt(b->num, IPPROTO_SCTP, SCTP_PEER_AUTH_CHUNKS,
1191 authchunks, &optlen);
1194 for (p = (unsigned char *)authchunks->gauth_chunks;
1195 p < (unsigned char *)authchunks + optlen;
1196 p += sizeof(uint8_t)) {
1197 if (*p == OPENSSL_SCTP_DATA_CHUNK_TYPE)
1199 if (*p == OPENSSL_SCTP_FORWARD_CUM_TSN_CHUNK_TYPE)
1203 OPENSSL_free(authchunks);
1205 if (!auth_data || !auth_forward) {
1206 BIOerr(BIO_F_DGRAM_SCTP_READ, BIO_R_CONNECT_ERROR);
1210 data->peer_auth_tested = 1;
1217 * dgram_sctp_write - send message on SCTP socket
1218 * @b: BIO to write to
1220 * @inl: amount of bytes in @in to send
1222 * Returns -1 on error or the sent amount of bytes on success
1224 static int dgram_sctp_write(BIO *b, const char *in, int inl)
1227 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1228 struct bio_dgram_sctp_sndinfo *sinfo = &(data->sndinfo);
1229 struct bio_dgram_sctp_prinfo *pinfo = &(data->prinfo);
1230 struct bio_dgram_sctp_sndinfo handshake_sinfo;
1231 struct iovec iov[1];
1233 struct cmsghdr *cmsg;
1234 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1235 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo)) +
1236 CMSG_SPACE(sizeof(struct sctp_prinfo))];
1237 struct sctp_sndinfo *sndinfo;
1238 struct sctp_prinfo *prinfo;
1240 char cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1241 struct sctp_sndrcvinfo *sndrcvinfo;
1244 clear_socket_error();
1247 * If we're send anything else than application data, disable all user
1248 * parameters and flags.
1251 memset(&handshake_sinfo, 0, sizeof(handshake_sinfo));
1252 # ifdef SCTP_SACK_IMMEDIATELY
1253 handshake_sinfo.snd_flags = SCTP_SACK_IMMEDIATELY;
1255 sinfo = &handshake_sinfo;
1258 /* We can only send a shutdown alert if the socket is dry */
1259 if (data->save_shutdown) {
1260 ret = BIO_dgram_sctp_wait_for_dry(b);
1264 BIO_clear_retry_flags(b);
1265 BIO_set_retry_write(b);
1270 iov[0].iov_base = (char *)in;
1271 iov[0].iov_len = inl;
1272 msg.msg_name = NULL;
1273 msg.msg_namelen = 0;
1276 msg.msg_control = (caddr_t) cmsgbuf;
1277 msg.msg_controllen = 0;
1279 # if defined(SCTP_SNDINFO) && defined(SCTP_PRINFO)
1280 cmsg = (struct cmsghdr *)cmsgbuf;
1281 cmsg->cmsg_level = IPPROTO_SCTP;
1282 cmsg->cmsg_type = SCTP_SNDINFO;
1283 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndinfo));
1284 sndinfo = (struct sctp_sndinfo *)CMSG_DATA(cmsg);
1285 memset(sndinfo, 0, sizeof(*sndinfo));
1286 sndinfo->snd_sid = sinfo->snd_sid;
1287 sndinfo->snd_flags = sinfo->snd_flags;
1288 sndinfo->snd_ppid = sinfo->snd_ppid;
1289 sndinfo->snd_context = sinfo->snd_context;
1290 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndinfo));
1293 (struct cmsghdr *)&cmsgbuf[CMSG_SPACE(sizeof(struct sctp_sndinfo))];
1294 cmsg->cmsg_level = IPPROTO_SCTP;
1295 cmsg->cmsg_type = SCTP_PRINFO;
1296 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_prinfo));
1297 prinfo = (struct sctp_prinfo *)CMSG_DATA(cmsg);
1298 memset(prinfo, 0, sizeof(*prinfo));
1299 prinfo->pr_policy = pinfo->pr_policy;
1300 prinfo->pr_value = pinfo->pr_value;
1301 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_prinfo));
1303 cmsg = (struct cmsghdr *)cmsgbuf;
1304 cmsg->cmsg_level = IPPROTO_SCTP;
1305 cmsg->cmsg_type = SCTP_SNDRCV;
1306 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1307 sndrcvinfo = (struct sctp_sndrcvinfo *)CMSG_DATA(cmsg);
1308 memset(sndrcvinfo, 0, sizeof(*sndrcvinfo));
1309 sndrcvinfo->sinfo_stream = sinfo->snd_sid;
1310 sndrcvinfo->sinfo_flags = sinfo->snd_flags;
1312 sndrcvinfo->sinfo_flags |= pinfo->pr_policy;
1314 sndrcvinfo->sinfo_ppid = sinfo->snd_ppid;
1315 sndrcvinfo->sinfo_context = sinfo->snd_context;
1316 sndrcvinfo->sinfo_timetolive = pinfo->pr_value;
1317 msg.msg_controllen += CMSG_SPACE(sizeof(struct sctp_sndrcvinfo));
1320 ret = sendmsg(b->num, &msg, 0);
1322 BIO_clear_retry_flags(b);
1324 if (BIO_dgram_should_retry(ret)) {
1325 BIO_set_retry_write(b);
1326 data->_errno = get_last_socket_error();
1332 static long dgram_sctp_ctrl(BIO *b, int cmd, long num, void *ptr)
1335 bio_dgram_sctp_data *data = NULL;
1336 socklen_t sockopt_len = 0;
1337 struct sctp_authkeyid authkeyid;
1338 struct sctp_authkey *authkey = NULL;
1340 data = (bio_dgram_sctp_data *) b->ptr;
1343 case BIO_CTRL_DGRAM_QUERY_MTU:
1345 * Set to maximum (2^14) and ignore user input to enable transport
1346 * protocol fragmentation. Returns always 2^14.
1351 case BIO_CTRL_DGRAM_SET_MTU:
1353 * Set to maximum (2^14) and ignore input to enable transport
1354 * protocol fragmentation. Returns always 2^14.
1359 case BIO_CTRL_DGRAM_SET_CONNECTED:
1360 case BIO_CTRL_DGRAM_CONNECT:
1361 /* Returns always -1. */
1364 case BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT:
1366 * SCTP doesn't need the DTLS timer Returns always 1.
1369 case BIO_CTRL_DGRAM_GET_MTU_OVERHEAD:
1371 * We allow transport protocol fragmentation so this is irrelevant
1375 case BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE:
1377 data->in_handshake = 1;
1379 data->in_handshake = 0;
1382 setsockopt(b->num, IPPROTO_SCTP, SCTP_NODELAY,
1383 &data->in_handshake, sizeof(int));
1385 case BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY:
1387 * New shared key for SCTP AUTH. Returns 0 on success, -1 otherwise.
1390 /* Get active key */
1391 sockopt_len = sizeof(struct sctp_authkeyid);
1393 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1399 sockopt_len = sizeof(struct sctp_authkey) + 64 * sizeof(uint8_t);
1400 authkey = OPENSSL_malloc(sockopt_len);
1401 if (authkey == NULL) {
1405 memset(authkey, 0, sockopt_len);
1406 authkey->sca_keynumber = authkeyid.scact_keynumber + 1;
1407 # ifndef __FreeBSD__
1409 * This field is missing in FreeBSD 8.2 and earlier, and FreeBSD 8.3
1410 * and higher work without it.
1412 authkey->sca_keylength = 64;
1414 memcpy(&authkey->sca_key[0], ptr, 64 * sizeof(uint8_t));
1417 setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_KEY, authkey,
1419 OPENSSL_free(authkey);
1424 /* Reset active key */
1425 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1426 &authkeyid, sizeof(struct sctp_authkeyid));
1431 case BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY:
1432 /* Returns 0 on success, -1 otherwise. */
1434 /* Get active key */
1435 sockopt_len = sizeof(struct sctp_authkeyid);
1437 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY, &authkeyid,
1442 /* Set active key */
1443 authkeyid.scact_keynumber = authkeyid.scact_keynumber + 1;
1444 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1445 &authkeyid, sizeof(struct sctp_authkeyid));
1450 * CCS has been sent, so remember that and fall through to check if
1451 * we need to deactivate an old key
1456 case BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD:
1457 /* Returns 0 on success, -1 otherwise. */
1460 * Has this command really been called or is this just a
1463 if (cmd == BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD)
1467 * CSS has been both, received and sent, so deactivate an old key
1469 if (data->ccs_rcvd == 1 && data->ccs_sent == 1) {
1470 /* Get active key */
1471 sockopt_len = sizeof(struct sctp_authkeyid);
1473 getsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_ACTIVE_KEY,
1474 &authkeyid, &sockopt_len);
1479 * Deactivate key or delete second last key if
1480 * SCTP_AUTHENTICATION_EVENT is not available.
1482 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1483 # ifdef SCTP_AUTH_DEACTIVATE_KEY
1484 sockopt_len = sizeof(struct sctp_authkeyid);
1485 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DEACTIVATE_KEY,
1486 &authkeyid, sockopt_len);
1490 # ifndef SCTP_AUTHENTICATION_EVENT
1491 if (authkeyid.scact_keynumber > 0) {
1492 authkeyid.scact_keynumber = authkeyid.scact_keynumber - 1;
1493 ret = setsockopt(b->num, IPPROTO_SCTP, SCTP_AUTH_DELETE_KEY,
1494 &authkeyid, sizeof(struct sctp_authkeyid));
1504 case BIO_CTRL_DGRAM_SCTP_GET_SNDINFO:
1505 /* Returns the size of the copied struct. */
1506 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1507 num = sizeof(struct bio_dgram_sctp_sndinfo);
1509 memcpy(ptr, &(data->sndinfo), num);
1512 case BIO_CTRL_DGRAM_SCTP_SET_SNDINFO:
1513 /* Returns the size of the copied struct. */
1514 if (num > (long)sizeof(struct bio_dgram_sctp_sndinfo))
1515 num = sizeof(struct bio_dgram_sctp_sndinfo);
1517 memcpy(&(data->sndinfo), ptr, num);
1519 case BIO_CTRL_DGRAM_SCTP_GET_RCVINFO:
1520 /* Returns the size of the copied struct. */
1521 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1522 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1524 memcpy(ptr, &data->rcvinfo, num);
1528 case BIO_CTRL_DGRAM_SCTP_SET_RCVINFO:
1529 /* Returns the size of the copied struct. */
1530 if (num > (long)sizeof(struct bio_dgram_sctp_rcvinfo))
1531 num = sizeof(struct bio_dgram_sctp_rcvinfo);
1533 memcpy(&(data->rcvinfo), ptr, num);
1535 case BIO_CTRL_DGRAM_SCTP_GET_PRINFO:
1536 /* Returns the size of the copied struct. */
1537 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1538 num = sizeof(struct bio_dgram_sctp_prinfo);
1540 memcpy(ptr, &(data->prinfo), num);
1543 case BIO_CTRL_DGRAM_SCTP_SET_PRINFO:
1544 /* Returns the size of the copied struct. */
1545 if (num > (long)sizeof(struct bio_dgram_sctp_prinfo))
1546 num = sizeof(struct bio_dgram_sctp_prinfo);
1548 memcpy(&(data->prinfo), ptr, num);
1550 case BIO_CTRL_DGRAM_SCTP_SAVE_SHUTDOWN:
1551 /* Returns always 1. */
1553 data->save_shutdown = 1;
1555 data->save_shutdown = 0;
1560 * Pass to default ctrl function to process SCTP unspecific commands
1562 ret = dgram_ctrl(b, cmd, num, ptr);
1568 int BIO_dgram_sctp_notification_cb(BIO *b,
1569 void (*handle_notifications) (BIO *bio,
1575 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1577 if (handle_notifications != NULL) {
1578 data->handle_notifications = handle_notifications;
1579 data->notification_context = context;
1587 * BIO_dgram_sctp_wait_for_dry - Wait for SCTP SENDER_DRY event
1588 * @b: The BIO to check for the dry event
1590 * Wait until the peer confirms all packets have been received, and so that
1591 * our kernel doesn't have anything to send anymore. This is only received by
1592 * the peer's kernel, not the application.
1596 * 0 when not dry yet
1599 int BIO_dgram_sctp_wait_for_dry(BIO *b)
1604 union sctp_notification snp;
1608 struct sctp_event event;
1610 struct sctp_event_subscribe event;
1611 socklen_t eventsize;
1613 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1615 /* set sender dry event */
1617 memset(&event, 0, sizeof(event));
1618 event.se_assoc_id = 0;
1619 event.se_type = SCTP_SENDER_DRY_EVENT;
1622 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1623 sizeof(struct sctp_event));
1625 eventsize = sizeof(struct sctp_event_subscribe);
1626 ret = getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event, &eventsize);
1630 event.sctp_sender_dry_event = 1;
1633 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1634 sizeof(struct sctp_event_subscribe));
1639 /* peek for notification */
1640 memset(&snp, 0, sizeof(snp));
1641 iov.iov_base = (char *)&snp;
1642 iov.iov_len = sizeof(union sctp_notification);
1643 msg.msg_name = NULL;
1644 msg.msg_namelen = 0;
1647 msg.msg_control = NULL;
1648 msg.msg_controllen = 0;
1651 n = recvmsg(b->num, &msg, MSG_PEEK);
1653 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1654 && (get_last_socket_error() != EWOULDBLOCK))
1660 /* if we find a notification, process it and try again if necessary */
1661 while (msg.msg_flags & MSG_NOTIFICATION) {
1662 memset(&snp, 0, sizeof(snp));
1663 iov.iov_base = (char *)&snp;
1664 iov.iov_len = sizeof(union sctp_notification);
1665 msg.msg_name = NULL;
1666 msg.msg_namelen = 0;
1669 msg.msg_control = NULL;
1670 msg.msg_controllen = 0;
1673 n = recvmsg(b->num, &msg, 0);
1675 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1676 && (get_last_socket_error() != EWOULDBLOCK))
1682 if (snp.sn_header.sn_type == SCTP_SENDER_DRY_EVENT) {
1685 /* disable sender dry event */
1687 memset(&event, 0, sizeof(event));
1688 event.se_assoc_id = 0;
1689 event.se_type = SCTP_SENDER_DRY_EVENT;
1692 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENT, &event,
1693 sizeof(struct sctp_event));
1695 eventsize = (socklen_t) sizeof(struct sctp_event_subscribe);
1697 getsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1702 event.sctp_sender_dry_event = 0;
1705 setsockopt(b->num, IPPROTO_SCTP, SCTP_EVENTS, &event,
1706 sizeof(struct sctp_event_subscribe));
1711 # ifdef SCTP_AUTHENTICATION_EVENT
1712 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1713 dgram_sctp_handle_auth_free_key_event(b, &snp);
1716 if (data->handle_notifications != NULL)
1717 data->handle_notifications(b, data->notification_context,
1720 /* found notification, peek again */
1721 memset(&snp, 0, sizeof(snp));
1722 iov.iov_base = (char *)&snp;
1723 iov.iov_len = sizeof(union sctp_notification);
1724 msg.msg_name = NULL;
1725 msg.msg_namelen = 0;
1728 msg.msg_control = NULL;
1729 msg.msg_controllen = 0;
1732 /* if we have seen the dry already, don't wait */
1734 sockflags = fcntl(b->num, F_GETFL, 0);
1735 fcntl(b->num, F_SETFL, O_NONBLOCK);
1738 n = recvmsg(b->num, &msg, MSG_PEEK);
1741 fcntl(b->num, F_SETFL, sockflags);
1745 if ((n < 0) && (get_last_socket_error() != EAGAIN)
1746 && (get_last_socket_error() != EWOULDBLOCK))
1753 /* read anything else */
1757 int BIO_dgram_sctp_msg_waiting(BIO *b)
1760 union sctp_notification snp;
1763 bio_dgram_sctp_data *data = (bio_dgram_sctp_data *) b->ptr;
1765 /* Check if there are any messages waiting to be read */
1767 memset(&snp, 0, sizeof(snp));
1768 iov.iov_base = (char *)&snp;
1769 iov.iov_len = sizeof(union sctp_notification);
1770 msg.msg_name = NULL;
1771 msg.msg_namelen = 0;
1774 msg.msg_control = NULL;
1775 msg.msg_controllen = 0;
1778 sockflags = fcntl(b->num, F_GETFL, 0);
1779 fcntl(b->num, F_SETFL, O_NONBLOCK);
1780 n = recvmsg(b->num, &msg, MSG_PEEK);
1781 fcntl(b->num, F_SETFL, sockflags);
1783 /* if notification, process and try again */
1784 if (n > 0 && (msg.msg_flags & MSG_NOTIFICATION)) {
1785 # ifdef SCTP_AUTHENTICATION_EVENT
1786 if (snp.sn_header.sn_type == SCTP_AUTHENTICATION_EVENT)
1787 dgram_sctp_handle_auth_free_key_event(b, &snp);
1790 memset(&snp, 0, sizeof(snp));
1791 iov.iov_base = (char *)&snp;
1792 iov.iov_len = sizeof(union sctp_notification);
1793 msg.msg_name = NULL;
1794 msg.msg_namelen = 0;
1797 msg.msg_control = NULL;
1798 msg.msg_controllen = 0;
1800 n = recvmsg(b->num, &msg, 0);
1802 if (data->handle_notifications != NULL)
1803 data->handle_notifications(b, data->notification_context,
1807 } while (n > 0 && (msg.msg_flags & MSG_NOTIFICATION));
1809 /* Return 1 if there is a message to be read, return 0 otherwise. */
1816 static int dgram_sctp_puts(BIO *bp, const char *str)
1821 ret = dgram_sctp_write(bp, str, n);
1826 static int BIO_dgram_should_retry(int i)
1830 if ((i == 0) || (i == -1)) {
1831 err = get_last_socket_error();
1833 # if defined(OPENSSL_SYS_WINDOWS)
1835 * If the socket return value (i) is -1 and err is unexpectedly 0 at
1836 * this point, the error code was overwritten by another system call
1837 * before this error handling is called.
1841 return (BIO_dgram_non_fatal_error(err));
1846 int BIO_dgram_non_fatal_error(int err)
1849 # if defined(OPENSSL_SYS_WINDOWS)
1850 # if defined(WSAEWOULDBLOCK)
1851 case WSAEWOULDBLOCK:
1856 # ifdef WSAEWOULDBLOCK
1857 # if WSAEWOULDBLOCK != EWOULDBLOCK
1870 # if EWOULDBLOCK != EAGAIN
1895 static void get_current_time(struct timeval *t)
1897 # if defined(_WIN32)
1900 unsigned __int64 ul;
1905 SystemTimeToFileTime(&st, &now.ft);
1907 now.ul -= 116444736000000000ULL;
1909 now.ul -= 116444736000000000UI64; /* re-bias to 1/1/1970 */
1911 t->tv_sec = (long)(now.ul / 10000000);
1912 t->tv_usec = ((int)(now.ul % 10000000)) / 10;
1913 # elif defined(OPENSSL_SYS_VMS)
1916 t->tv_sec = (long)tb.time;
1917 t->tv_usec = (long)tb.millitm * 1000;
1919 gettimeofday(t, NULL);