1 /*
2  * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NATIVE_NET_CONN_API_H
17 #define NATIVE_NET_CONN_API_H
18 
19 /**
20  * @addtogroup NetConnection
21  * @{
22  *
23  * @brief Provide C interface for the data network connection module of network management.
24  *
25  * @since 11
26  * @version 1.0
27  */
28 
29 /**
30  * @file net_connection.h
31  *
32  * @brief Provide C interface for the data network connection module of network management.
33  *
34  * @kit NetworkKit
35  * @syscap SystemCapability.Communication.NetManager.Core
36  * @library libnet_connection.so
37  * @since 11
38  * @version 1.0
39  */
40 
41 #include <netdb.h>
42 
43 #include "net_connection_type.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 /**
50  * @brief Checks whether a default activated data network is available.
51  *
52  * @param hasDefaultNet Pointer to the result that specifies whether a default activated data network is available.
53  * @return 0 - Success. 201 - Missing permissions.
54  *         401 - Parameter error. 2100002 - Unable to connect to service.
55  *         2100003 - Internal error.
56  * @permission ohos.permission.GET_NETWORK_INFO
57  * @syscap SystemCapability.Communication.NetManager.Core
58  * @since 11
59  * @version 1.0
60  */
61 int32_t OH_NetConn_HasDefaultNet(int32_t *hasDefaultNet);
62 
63 /**
64  * @brief Obtains the default activated data network.
65  *
66  * @param netHandle Pointer to the network handle that contains the network ID.
67  * @return 0 - Success. 201 - Missing permissions.
68  *         401 - Parameter error. 2100002 - Unable to connect to service.
69  *         2100003 - Internal error.
70  * @permission ohos.permission.GET_NETWORK_INFO
71  * @syscap SystemCapability.Communication.NetManager.Core
72  * @since 11
73  * @version 1.0
74  */
75 int32_t OH_NetConn_GetDefaultNet(NetConn_NetHandle *netHandle);
76 
77 /**
78  * @brief Checks whether metering is enabled for the default data network.
79  *
80  * @param isMetered Pointer to the result that specifies whether metering is enabled.
81  * @return 0 - Success. 201 - Missing permissions.
82  *         401 - Parameter error. 2100002 - Unable to connect to service.
83  *         2100003 - Internal error.
84  * @permission ohos.permission.GET_NETWORK_INFO
85  * @syscap SystemCapability.Communication.NetManager.Core
86  * @since 11
87  * @version 1.0
88  */
89 int32_t OH_NetConn_IsDefaultNetMetered(int32_t *isMetered);
90 
91 /**
92  * @brief Obtains the connection properties of a data network.
93  *
94  * @param netHandle Pointer to the network handle that contains the network ID.
95  * @param prop Pointer to the connection properties.
96  * @return 0 - Success. 201 - Missing permissions.
97  *         401 - Parameter error. 2100002 - Unable to connect to service.
98  *         2100003 - Internal error.
99  * @permission ohos.permission.GET_NETWORK_INFO
100  * @syscap SystemCapability.Communication.NetManager.Core
101  * @since 11
102  * @version 1.0
103  */
104 int32_t OH_NetConn_GetConnectionProperties(NetConn_NetHandle *netHandle, NetConn_ConnectionProperties *prop);
105 
106 /**
107  * @brief Obtains the capabilities of a data network.
108  *
109  * @param netHandle Pointer to the network handle that contains the network ID.
110  * @param netCapacities Pointer to the network capabilities.
111  * @return 0 - Success. 201 - Missing permissions.
112  *         401 - Parameter error. 2100002 - Unable to connect to service.
113  *         2100003 - Internal error.
114  * @permission ohos.permission.GET_NETWORK_INFO
115  * @syscap SystemCapability.Communication.NetManager.Core
116  * @since 11
117  * @version 1.0
118  */
119 int32_t OH_NetConn_GetNetCapabilities(NetConn_NetHandle *netHandle, NetConn_NetCapabilities *netCapabilities);
120 
121 /**
122  * @brief Obtains the default http proxy.
123  *
124  * @param httpProxy Pointer to the HTTP proxy.
125  * @return 0 - Success. 201 - Missing permissions.
126  *         401 - Parameter error. 2100002 - Unable to connect to service.
127  *         2100003 - Internal error.
128  * @syscap SystemCapability.Communication.NetManager.Core
129  * @since 11
130  * @version 1.0
131  */
132 int32_t OH_NetConn_GetDefaultHttpProxy(NetConn_HttpProxy *httpProxy);
133 
134 /**
135  * @brief Get DNS result with netId.
136  *
137  * @param host The host name to query.
138  * @param serv Service name.
139  * @param hint Pointer to the addrinfo structure.
140  * @param res Store DNS query results and return them in a linked list format.
141  * @param netId DNS query netId, 0 is used for default netid query.
142  * @return 0 - Success.
143  * @return 201 - Missing permissions.
144  * @return 401 - Parameter error.
145  * @return 2100002 - Unable to connect to service.
146  * @return 2100003 - Internal error.
147  * @permission ohos.permission.INTERNET
148  * @syscap SystemCapability.Communication.NetManager.Core
149  * @since 11
150  * @version 1.0
151  */
152 int32_t OH_NetConn_GetAddrInfo(char *host, char *serv, struct addrinfo *hint, struct addrinfo **res, int32_t netId);
153 
154 /**
155  * @brief Free DNS result.
156  *
157  * @param res DNS query result chain header.
158  * @return 0 - Success.
159  * @return 201 - Missing permissions.
160  * @return 401 - Parameter error.
161  * @return 2100002 - Unable to connect to service.
162  * @return 2100003 - Internal error.
163  * @permission ohos.permission.INTERNET
164  * @syscap SystemCapability.Communication.NetManager.Core
165  * @since 11
166  * @version 1.0
167  */
168 int32_t OH_NetConn_FreeDnsResult(struct addrinfo *res);
169 
170 /**
171  * @brief Queries all activated data networks.
172  *
173  * @param netHandleList Network handle that stores the network ID list.
174  * @return 0 - Success. 201 - Missing permissions.
175  *         401 - Parameter error. 2100002 - Unable to connect to service.
176  *         2100003 - Internal error.
177  * @permission ohos.permission.GET_NETWORK_INFO
178  * @syscap SystemCapability.Communication.NetManager.Core
179  * @since 11
180  * @version 1.0
181  */
182 int32_t OH_NetConn_GetAllNets(NetConn_NetHandleList *netHandleList);
183 
184 /**
185  * @brief Registers a custom DNS resolver.
186  *
187  * @param resolver Pointer to the custom DNS resolver.
188  * @return 0 - Success. 201 - Missing permissions.
189  *         401 - Parameter error. 2100002 - Unable to connect to service.
190  *         2100003 - Internal error.
191  * @permission ohos.permission.INTERNET
192  * @syscap SystemCapability.Communication.NetManager.Core
193  * @deprecated since 13
194  * @useinstead OH_NetConn_RegisterDnsResolver
195  * @since 11
196  * @version 1.0
197  */
198 int32_t OHOS_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver);
199 
200 /**
201  * @brief Unregisters a custom DNS resolver.
202  *
203  * @return 0 - Success. 201 - Missing permissions.
204  *         401 - Parameter error. 2100002 - Unable to connect to service.
205  *         2100003 - Internal error.
206  * @permission ohos.permission.INTERNET
207  * @syscap SystemCapability.Communication.NetManager.Core
208  * @deprecated since 13
209  * @useinstead OH_NetConn_UnregisterDnsResolver
210  * @since 11
211  * @version 1.0
212  */
213 int32_t OHOS_NetConn_UnregisterDnsResolver(void);
214 
215 /**
216  * @brief Registers a custom DNS resolver.
217  *
218  * @param resolver Pointer to the custom DNS resolver.
219  * @return Returns the result code.
220  *         {@link NETMANAGER_EXT_SUCCESS} if the operation is successful.
221  *         {@link NETMANAGER_ERR_PERMISSION_DENIED} Missing permissions, add permission.
222  *         {@link NETMANAGER_ERR_PARAMETER_ERROR} Parameter error. Please enter a correct parameter.
223  * @permission ohos.permission.INTERNET
224  * @syscap SystemCapability.Communication.NetManager.Core
225  * @since 13
226  * @version 1.0
227  */
228 int32_t OH_NetConn_RegisterDnsResolver(OH_NetConn_CustomDnsResolver resolver);
229 
230 /**
231  * @brief Unregisters a custom DNS resolver.
232  *
233  * @return 0 - Success. 201 - Missing permissions.
234  *         401 - Parameter error. 2100002 - Unable to connect to service.
235  *         2100003 - Internal error.
236  * @permission ohos.permission.INTERNET
237  * @syscap SystemCapability.Communication.NetManager.Core
238  * @since 13
239  * @version 1.0
240  */
241 int32_t OH_NetConn_UnregisterDnsResolver(void);
242 
243 /**
244  * @brief Binds a socket to the specific network.
245  *
246  * @param socketFd Socket constructed by user.
247  * @param netHandle Pointer to the network handle that contains the network ID.
248  * @return 0 - Success.
249  *         401 - Parameter error.
250  *         2100002 - Unable to connect to service.
251  *         2100003 - Internal error.
252  * @syscap SystemCapability.Communication.NetManager.Core
253  * @since 12
254  * @version 1.0
255  */
256 int32_t OH_NetConn_BindSocket(int32_t socketFd, NetConn_NetHandle *netHandle);
257 
258 int32_t OH_NetConn_RegisterNetConnCallback(NetConn_NetSpecifier *specifier, NetConn_NetConnCallback *netConnCallback,
259                                            uint32_t timeout, uint32_t *callbackId);
260 
261 int32_t OH_NetConn_RegisterDefaultNetConnCallback(NetConn_NetConnCallback *netConnCallback, uint32_t *callbackId);
262 
263 int32_t OH_NetConn_UnregisterNetConnCallback(uint32_t callBackId);
264 
265 int32_t OH_NetConn_SetAppHttpProxy(NetConn_HttpProxy *httpProxy);
266 
267 int32_t OH_NetConn_RegisterAppHttpProxyCallback(OH_NetConn_AppHttpProxyChange appHttpProxyChange, uint32_t *callbackId);
268 
269 void OH_NetConn_UnregisterAppHttpProxyCallback(uint32_t callbackId);
270 
271 #ifdef __cplusplus
272 }
273 #endif
274 
275 /** @} */
276 #endif /* NATIVE_NET_CONN_API_H */
277