1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** NetX Component                                                        */
17 /**                                                                       */
18 /**   Transmission Control Protocol (TCP)                                 */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define NX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "nx_api.h"
29 #include "nx_tcp.h"
30 #include "nx_ipv6.h"
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _nxd_tcp_socket_peer_info_get                       PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Yuxin Zhou, Microsoft Corporation                                   */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function retrieves IP address and port number of the peer      */
45 /*    connected to the specified TCP socket.                              */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    socket_ptr                            Pointer to the TCP sockete    */
50 /*    peer_ip_address                       Pointer to the IP address     */
51 /*                                             of the peer.               */
52 /*    peer_port                             Pointer to the port number    */
53 /*                                             of the peer.               */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    NX_SUCCESS                            Actual completion status      */
58 /*    NX_NOT_CONNECTED                      TCP socket is not connected   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    tx_mutex_get                          Obtain protection             */
63 /*    tx_mutex_put                          Release protection            */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Application Code                                                    */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
74 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*                                                                        */
77 /**************************************************************************/
_nxd_tcp_socket_peer_info_get(NX_TCP_SOCKET * socket_ptr,NXD_ADDRESS * peer_ip_address,ULONG * peer_port)78 UINT  _nxd_tcp_socket_peer_info_get(NX_TCP_SOCKET *socket_ptr,
79                                     NXD_ADDRESS *peer_ip_address,
80                                     ULONG *peer_port)
81 {
82 #ifdef TX_ENABLE_EVENT_TRACE
83 ULONG  ip_address_lsw = 0;
84 #endif /* TX_ENABLE_EVENT_TRACE */
85 NX_IP *ip_ptr;
86 
87 
88     /* Setup IP pointer. */
89     ip_ptr = socket_ptr -> nx_tcp_socket_ip_ptr;
90 
91     /* Obtain the IP mutex so we can examine the bound port.  */
92     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
93 
94     /* Make sure the TCP connection has been established. */
95     if ((socket_ptr -> nx_tcp_socket_state <= NX_TCP_LISTEN_STATE) ||
96         (socket_ptr -> nx_tcp_socket_state > NX_TCP_ESTABLISHED))
97     {
98 
99         /* Release protection.  */
100         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
101 
102         return(NX_NOT_CONNECTED);
103     }
104 
105     /* Determine the peer IP address */
106 
107     /* Assign the IP address type (IPv4 or IPv6) */
108     peer_ip_address -> nxd_ip_version = socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version;
109 
110     /* If address type is IPv4, just copy one word. */
111 #ifndef NX_DISABLE_IPV4
112     if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V4)
113     {
114         peer_ip_address -> nxd_ip_address.v4 = socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v4;
115     }
116 #endif /* !NX_DISABLE_IPV4  */
117 
118 #ifdef FEATURE_NX_IPV6
119     if (socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_version == NX_IP_VERSION_V6)
120     {
121 
122         /* Return the IP address of the peer connected to the TCP socket. */
123         COPY_IPV6_ADDRESS(socket_ptr -> nx_tcp_socket_connect_ip.nxd_ip_address.v6,
124                           peer_ip_address -> nxd_ip_address.v6);
125     }
126 #endif /* FEATURE_NX_IPV6 */
127 
128     /* Determine the peer port number and return the port number of the peer
129        connected to the TCP socket. */
130     *peer_port = socket_ptr -> nx_tcp_socket_connect_port;
131 
132     /* Release protection.  */
133     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
134 
135 #ifdef TX_ENABLE_EVENT_TRACE
136 
137 #ifndef NX_DISABLE_IPV4
138     if (peer_ip_address -> nxd_ip_version == NX_IP_VERSION_V4)
139     {
140         ip_address_lsw = peer_ip_address -> nxd_ip_address.v4;
141     }
142 #endif /* NX_DISABLE_IPV4 */
143 
144 #ifdef FEATURE_NX_IPV6
145     if (peer_ip_address -> nxd_ip_version == NX_IP_VERSION_V6)
146     {
147 
148         ip_address_lsw = peer_ip_address -> nxd_ip_address.v6[3];
149     }
150 
151 #endif /* FEATURE_NX_IPV6 */
152 
153     /* If trace is enabled, insert this event into the trace buffer.  */
154     NX_TRACE_IN_LINE_INSERT(NXD_TRACE_TCP_SOCKET_PEER_INFO_GET, socket_ptr, ip_address_lsw, *peer_port, 0, NX_TRACE_TCP_EVENTS, 0, 0);
155 
156 #endif /* TX_ENABLE_EVENT_TRACE */
157 
158     /* Return successful completion status.  */
159     return(NX_SUCCESS);
160 }
161 
162