1 /**
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef IMS_MEDIA_NW_UTIL_H
18 #define IMS_MEDIA_NW_UTIL_H
19 
20 #include <ImsMediaDefine.h>
21 
22 class ImsMediaNetworkUtil
23 {
24 public:
25     /**
26      * @brief Get the local Ip address and port from the socket
27      *
28      * @param nSocketFD The socket file descriptor to extract the ip and port
29      * @param pIPAddress The ip address formed in text
30      * @param len The length of the ip address array
31      * @param port The port to extract from the socket
32      * @return true Returns when there is no error to extract the data
33      * @return false  Returns when there is error returns with the arguments
34      */
35     static bool getLocalIpPortFromSocket(
36             const int nSocketFD, char* pIPAddress, int len, unsigned int& port);
37 
38     /**
39      * @brief Get the remote Ip address and port from the socket connected to
40      *
41      * @param nSocketFD The socket file descriptor to extract the ip and port
42      * @param pIPAddress The ip address formed in text
43      * @param len The length of the ip address array
44      * @param port The port to extract from the socket
45      * @return true Returns when there is no error to extract the data
46      * @return false  Returns when there is error returns with the arguments
47      */
48     static bool getRemoteIpPortFromSocket(
49             const int nSocketFD, char* pIPAddress, int len, unsigned int& port);
50 
51     /**
52      * @brief create socket with the given local ip address and the port information
53      *
54      * @param pIPAddr The local Ip address formed in text
55      * @param port The local port number
56      * @param af The ip version
57      * @return int Returns socket file descriptor, it is -1 when it is invalid to create socket with
58      * the given function arguments
59      */
60     static int openSocket(const char* pIPAddr, unsigned int port, int af);
61 
62     /**
63      * @brief connect extisting socket to certain remote address with the given arguments
64      *
65      * @param socketFd The target socket fd to connect the remote ip address and port number
66      * @param pIPAddr The remote Ip address form in text
67      * @param port The remote port number
68      * @param af The ip version
69      * @return true Returns when the socket connects succefully
70      * @return false Returns when the socket connects fail
71      */
72     static bool connectSocket(const int socketFd, const char* pIPAddr, unsigned int port, int af);
73 
74     /**
75      * @brief close the socket
76      *
77      * @param socketFd The socket file descriptor
78      */
79     static void closeSocket(int& socketFd);
80 };
81 
82 #endif  // IMS_MEDIA_NW_UTIL_H