@@ -1666,27 +1666,39 @@ static int ztls_poll_prepare_ctx(struct net_context *ctx,
1666
1666
}
1667
1667
1668
1668
if (pfd -> events & ZSOCK_POLLIN ) {
1669
- if (!IS_LISTENING (ctx )) {
1670
- /* If there already is mbedTLS data to read, there is no
1671
- * need to set the k_poll_event object. Return EALREADY
1672
- * so we won't block in the k_poll.
1673
- */
1674
- if (mbedtls_ssl_get_bytes_avail (& ctx -> tls -> ssl ) > 0 ) {
1675
- errno = EALREADY ;
1676
- return -1 ;
1677
- }
1678
- }
1679
-
1680
1669
if (* pev == pev_end ) {
1681
1670
errno = ENOMEM ;
1682
1671
return -1 ;
1683
1672
}
1684
1673
1685
- (* pev )-> obj = & ctx -> recv_q ;
1686
- (* pev )-> type = K_POLL_TYPE_FIFO_DATA_AVAILABLE ;
1674
+ /* DTLS client should wait for the handshake to complete before
1675
+ * it actually starts to poll for data.
1676
+ */
1677
+ if (net_context_get_type (ctx ) == SOCK_DGRAM &&
1678
+ ctx -> tls -> options .role == MBEDTLS_SSL_IS_CLIENT &&
1679
+ !is_handshake_complete (ctx )) {
1680
+ (* pev )-> obj = & ctx -> tls -> tls_established ;
1681
+ (* pev )-> type = K_POLL_TYPE_SEM_AVAILABLE ;
1682
+ } else {
1683
+ /* Otherwise, monitor fifo for data/connections. */
1684
+ (* pev )-> obj = & ctx -> recv_q ;
1685
+ (* pev )-> type = K_POLL_TYPE_FIFO_DATA_AVAILABLE ;
1686
+ }
1687
+
1687
1688
(* pev )-> mode = K_POLL_MODE_NOTIFY_ONLY ;
1688
1689
(* pev )-> state = K_POLL_STATE_NOT_READY ;
1689
1690
(* pev )++ ;
1691
+
1692
+ /* If there already is mbedTLS data to read, there is no
1693
+ * need to set the k_poll_event object. Return EALREADY
1694
+ * so we won't block in the k_poll.
1695
+ */
1696
+ if (!IS_LISTENING (ctx )) {
1697
+ if (mbedtls_ssl_get_bytes_avail (& ctx -> tls -> ssl ) > 0 ) {
1698
+ errno = EALREADY ;
1699
+ return -1 ;
1700
+ }
1701
+ }
1690
1702
}
1691
1703
1692
1704
return 0 ;
@@ -1707,19 +1719,34 @@ static int ztls_poll_update_ctx(struct net_context *ctx,
1707
1719
}
1708
1720
1709
1721
if (pfd -> events & ZSOCK_POLLIN ) {
1722
+ /* Check if socket was waiting for the handshake to complete. */
1723
+ if ((* pev )-> obj == & ctx -> tls -> tls_established ) {
1724
+ if ((* pev )-> state == K_POLL_STATE_NOT_READY ) {
1725
+ goto next ;
1726
+ }
1727
+
1728
+ /* Reconfigure the poll event to wait for data now. */
1729
+ (* pev )-> obj = & ctx -> recv_q ;
1730
+ (* pev )-> type = K_POLL_TYPE_FIFO_DATA_AVAILABLE ;
1731
+ (* pev )-> mode = K_POLL_MODE_NOTIFY_ONLY ;
1732
+ (* pev )-> state = K_POLL_STATE_NOT_READY ;
1733
+
1734
+ goto again ;
1735
+ }
1736
+
1710
1737
if (!IS_LISTENING (ctx )) {
1711
1738
/* Already had TLS data to read on socket. */
1712
1739
if (mbedtls_ssl_get_bytes_avail (& ctx -> tls -> ssl ) > 0 ) {
1713
1740
pfd -> revents |= ZSOCK_POLLIN ;
1714
- return 0 ;
1741
+ goto next ;
1715
1742
}
1716
1743
}
1717
1744
1718
1745
/* Some encrypted data received on the socket. */
1719
- if ((( * pev ) ++ )-> state != K_POLL_STATE_NOT_READY ) {
1746
+ if ((* pev )-> state != K_POLL_STATE_NOT_READY ) {
1720
1747
if (IS_LISTENING (ctx )) {
1721
1748
pfd -> revents |= ZSOCK_POLLIN ;
1722
- return 0 ;
1749
+ goto next ;
1723
1750
}
1724
1751
1725
1752
/* EAGAIN might happen during or just after
@@ -1728,25 +1755,35 @@ static int ztls_poll_update_ctx(struct net_context *ctx,
1728
1755
if (recv (pfd -> fd , NULL , 0 , ZSOCK_MSG_DONTWAIT ) < 0 &&
1729
1756
errno != EAGAIN ) {
1730
1757
pfd -> revents |= ZSOCK_POLLERR ;
1731
- return 0 ;
1758
+ goto next ;
1732
1759
}
1733
1760
1734
1761
if (mbedtls_ssl_get_bytes_avail (& ctx -> tls -> ssl ) > 0 ||
1735
1762
sock_is_eof (ctx )) {
1736
1763
pfd -> revents |= ZSOCK_POLLIN ;
1737
- return 0 ;
1764
+ goto next ;
1738
1765
}
1739
1766
1740
1767
/* Received encrypted data, but still not enough
1741
1768
* to decrypt it and return data through socket,
1742
1769
* ask for retry.
1743
1770
*/
1744
- errno = EAGAIN ;
1745
- return -1 ;
1771
+
1772
+ (* pev )-> state = K_POLL_STATE_NOT_READY ;
1773
+ goto again ;
1746
1774
}
1747
1775
}
1748
1776
1749
1777
return 0 ;
1778
+
1779
+ next :
1780
+ (* pev )++ ;
1781
+ return 0 ;
1782
+
1783
+ again :
1784
+ (* pev )++ ;
1785
+ errno = EAGAIN ;
1786
+ return -1 ;
1750
1787
}
1751
1788
1752
1789
int ztls_getsockopt_ctx (struct net_context * ctx , int level , int optname ,
0 commit comments