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 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _nx_tcp_client_socket_port_get                      PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Yuxin Zhou, Microsoft Corporation                                   */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function retrieves the bound port of the specified socket,     */
44 /*    which is especially useful in situations where NX_ANY_PORT was      */
45 /*    used to bind.                                                       */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    socket_ptr                            Pointer to TCP client socket  */
50 /*    port_ptr                              Destination for port bound to */
51 /*                                            this socket                 */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    status                                Completion status             */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    tx_mutex_get                          Obtain protection             */
60 /*    tx_mutex_put                          Release protection            */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application Code                                                    */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Yuxin Zhou               Initial Version 6.0           */
71 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_nx_tcp_client_socket_port_get(NX_TCP_SOCKET * socket_ptr,UINT * port_ptr)75 UINT  _nx_tcp_client_socket_port_get(NX_TCP_SOCKET *socket_ptr, UINT *port_ptr)
76 {
77 
78 NX_IP *ip_ptr;
79 
80 
81     /* Setup IP pointer.  */
82     ip_ptr =  socket_ptr -> nx_tcp_socket_ip_ptr;
83 
84     /* If trace is enabled, insert this event into the trace buffer.  */
85     NX_TRACE_IN_LINE_INSERT(NX_TRACE_TCP_CLIENT_SOCKET_PORT_GET, ip_ptr, socket_ptr, socket_ptr -> nx_tcp_socket_port, 0, NX_TRACE_TCP_EVENTS, 0, 0);
86 
87     /* Obtain the IP mutex so we can examine the bound port.  */
88     tx_mutex_get(&(ip_ptr -> nx_ip_protection), TX_WAIT_FOREVER);
89 
90     /* Determine if the socket has already been bound to port or if a socket bind is
91        already pending from another thread.  */
92     if (!socket_ptr -> nx_tcp_socket_bound_next)
93     {
94 
95         /* Release protection.  */
96         tx_mutex_put(&(ip_ptr -> nx_ip_protection));
97 
98         /* Return a not bound error code.  */
99         return(NX_NOT_BOUND);
100     }
101 
102     /* Return the port number from the socket.  */
103     *port_ptr =  socket_ptr -> nx_tcp_socket_port;
104 
105     /* Release protection.  */
106     tx_mutex_put(&(ip_ptr -> nx_ip_protection));
107 
108     /* Return successful completion status.  */
109     return(NX_SUCCESS);
110 }
111 
112