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