1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** NetX Secure Component                                                 */
16 /**                                                                       */
17 /**    Datagram Transport Layer Security (DTLS)                           */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_SECURE_SOURCE_CODE
23 
24 #include "nx_secure_dtls.h"
25 #include "nx_ipv6.h"
26 
27 /**************************************************************************/
28 /*                                                                        */
29 /*  FUNCTION                                               RELEASE        */
30 /*                                                                        */
31 /*    _nx_secure_dtls_session_client_info_get             PORTABLE C      */
32 /*                                                           6.1          */
33 /*  AUTHOR                                                                */
34 /*                                                                        */
35 /*    Timothy Stapko, Microsoft Corporation                               */
36 /*                                                                        */
37 /*  DESCRIPTION                                                           */
38 /*                                                                        */
39 /*    This function returns relevant information about a remote client    */
40 /*    that is connected to a DTLS server session instance.                */
41 /*                                                                        */
42 /*  INPUT                                                                 */
43 /*                                                                        */
44 /*    dtls_session                          DTLS session control block    */
45 /*    client_ip_address                     Return remote host IP address */
46 /*    client_port                           Return remote host UDP port   */
47 /*    local_port                            Return local UDP port         */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    status                                Completion status             */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    COPY_IPV6_ADDRESS                     Copy IPv6 address to return   */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Application Code                                                    */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  05-19-2020     Timothy Stapko           Initial Version 6.0           */
66 /*  09-30-2020     Timothy Stapko           Modified comment(s),          */
67 /*                                            resulting in version 6.1    */
68 /*                                                                        */
69 /**************************************************************************/
_nx_secure_dtls_session_client_info_get(NX_SECURE_DTLS_SESSION * dtls_session,NXD_ADDRESS * client_ip_address,UINT * client_port,UINT * local_port)70 UINT _nx_secure_dtls_session_client_info_get(NX_SECURE_DTLS_SESSION *dtls_session,
71                                              NXD_ADDRESS *client_ip_address, UINT *client_port, UINT *local_port)
72 {
73 #ifdef NX_SECURE_ENABLE_DTLS
74 NX_UDP_SOCKET *socket_ptr;
75 NXD_ADDRESS *ip_address;
76 
77     /* Get our UDP socket. */
78     socket_ptr = dtls_session->nx_secure_dtls_udp_socket;
79 
80     /* Make sure we have received a packet on the UDP socket - otherwise
81        we don't have a DTLS connection and can't get info from a non-existent Client! */
82     if(socket_ptr->nx_udp_socket_receive_head == NX_NULL)
83     {
84         return(NX_NOT_CONNECTED);
85     }
86 
87     /* Get our local IP address for the deep copy below. */
88     ip_address = &(dtls_session->nx_secure_dtls_remote_ip_address);
89 
90     /* Copy address version. */
91     client_ip_address -> nxd_ip_version = ip_address -> nxd_ip_version;
92 
93 #ifndef NX_DISABLE_IPV4
94     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
95     {
96         /* Pickup the source IP address.  */
97         client_ip_address -> nxd_ip_address.v4 = ip_address -> nxd_ip_address.v4;
98     }
99 #endif /* NX_DISABLE_IPV4 */
100 
101 #ifdef FEATURE_NX_IPV6
102     if (ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
103     {
104         COPY_IPV6_ADDRESS(ip_address -> nxd_ip_address.v6,
105                           client_ip_address -> nxd_ip_address.v6);
106     }
107 #endif /* FEATURE_NX_IPV6 */
108 
109     /* Copy the remote and local ports to our return parameters. */
110     *client_port = dtls_session->nx_secure_dtls_remote_port;
111     *local_port = dtls_session->nx_secure_dtls_local_port;
112 
113     return(NX_SUCCESS);
114 #else
115     NX_PARAMETER_NOT_USED(dtls_session);
116     NX_PARAMETER_NOT_USED(client_ip_address);
117     NX_PARAMETER_NOT_USED(client_port);
118     NX_PARAMETER_NOT_USED(local_port);
119 
120     return(NX_NOT_SUPPORTED);
121 #endif /* NX_SECURE_ENABLE_DTLS */
122 }
123 
124