• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 /**
17  * @addtogroup NetConnection
18  * @{
19  *
20  * @brief Provides the data structures for the C APIs of the network connection module for network management.
21  *
22  * @since 11
23  * @version 1.0
24  */
25 
26 /**
27  * @file net_connection_type.h
28  * @brief Defines the data structures for the C APIs of the network connection module.
29  *
30  * @library libnet_connection.so
31  * @kit NetworkKit
32  * @syscap SystemCapability.Communication.NetManager.Core
33  * @since 11
34  * @version 1.0
35  *
36  */
37 
38 #ifndef NATIVE_NET_CONN_TYPE_H
39 #define NATIVE_NET_CONN_TYPE_H
40 
41 #include <stdbool.h>
42 #include <stdint.h>
43 #include <netdb.h>
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 #define NETCONN_MAX_NET_SIZE 32
50 #define NETCONN_MAX_BEARER_TYPE_SIZE 32
51 #define NETCONN_MAX_CAP_SIZE 32
52 #define NETCONN_MAX_ADDR_SIZE 32
53 #define NETCONN_MAX_ROUTE_SIZE 64
54 #define NETCONN_MAX_EXCLUSION_SIZE 256
55 #define NETCONN_MAX_STR_LEN 256
56 
57 /**
58  * @brief Defines network capabilities.
59  *
60  * @since 11
61  * @version 1.0
62  */
63 typedef enum NetConn_NetCap {
64     /** MMS */
65     NETCONN_NET_CAPABILITY_MMS = 0,
66     /** Not Metered */
67     NETCONN_NET_CAPABILITY_NOT_METERED = 11,
68     /** Internet */
69     NETCONN_NET_CAPABILITY_INTERNET = 12,
70     /** Not VPN */
71     NETCONN_NET_CAPABILITY_NOT_VPN = 15,
72     /** Validated */
73     NETCONN_NET_CAPABILITY_VALIDATED = 16,
74     /**
75     * Portal
76     * @since 12
77     */
78     NETCONN_NET_CAPABILITY_PORTAL = 17,
79     /**
80      * In checking network connectivity.
81      * @since 12
82      */
83     NETCONN_NET_CAPABILITY_CHECKING_CONNECTIVITY = 31
84 } NetConn_NetCap;
85 
86 /**
87  * @brief Defines network bearer types.
88  *
89  * @since 11
90  * @version 1.0
91  */
92 typedef enum NetConn_NetBearerType {
93     /** Cellular network */
94     NETCONN_BEARER_CELLULAR = 0,
95     /** WIFI */
96     NETCONN_BEARER_WIFI = 1,
97     /**
98      * Bluetooth
99      * @since 12
100      */
101     NETCONN_BEARER_BLUETOOTH = 2,
102     /** Ethernet */
103     NETCONN_BEARER_ETHERNET = 3,
104     /**
105      * VPN
106      * @since 12
107      */
108     NETCONN_BEARER_VPN = 4,
109 } NetConn_NetBearerType;
110 
111 /**
112  * @brief Enumerates NetConn error codes.
113  *
114  * @since 15
115  */
116 typedef enum NetConn_ErrorCode {
117     /** @error Success return code on success*/
118     NETCONN_SUCCESS = 0,
119     /** @error Permission verification failed */
120     NETCONN_PERMISSION_DENIED = 201,
121     /** @error Parameter check failed */
122     NETCONN_PARAMETER_ERROR = 401,
123     /** @error Failed to connect to the service */
124     NETCONN_OPERATION_FAILED = 2100002,
125     /**
126      * @error System internal error.
127      * 1. Memory-related error, for example, insufficient memory or memory data copy failures.
128      * 2. Null pointer error, for example, using memory that has already been released.
129      */
130     NETCONN_INTERNAL_ERROR = 2100003
131 } NetConn_ErrorCode;
132 
133 /**
134  * @brief Defines the network handle.
135  *
136  * @since 11
137  * @version 1.0
138  */
139 typedef struct NetConn_NetHandle {
140     /** Network ID */
141     int32_t netId;
142 } NetConn_NetHandle;
143 
144 /**
145  * @brief Defines all network capabilities.
146  *
147  * @since 11
148  * @version 1.0
149  */
150 typedef struct NetConn_NetCapabilities {
151     /** Uplink bandwidth */
152     uint32_t linkUpBandwidthKbps;
153     /** Downlink bandwidth */
154     uint32_t linkDownBandwidthKbps;
155     /** Network capability list */
156     NetConn_NetCap netCaps[NETCONN_MAX_CAP_SIZE];
157     /** Actual size of the network capability list */
158     int32_t netCapsSize;
159     /** Bearer type list */
160     NetConn_NetBearerType bearerTypes[NETCONN_MAX_BEARER_TYPE_SIZE];
161     /** Actual size of the bearer type list */
162     int32_t bearerTypesSize;
163 } NetConn_NetCapabilities;
164 
165 /**
166  * @brief Defines the network address.
167  *
168  * @since 11
169  * @version 1.0
170  */
171 typedef struct NetConn_NetAddr {
172     /** Network address family */
173     uint8_t family;
174     /** Prefix length */
175     uint8_t prefixlen;
176     /** Port number */
177     uint8_t port;
178     /** Address */
179     char address[NETCONN_MAX_STR_LEN];
180 } NetConn_NetAddr;
181 
182 /**
183  * @brief Defines the route configuration information.
184  *
185  * @since 11
186  * @version 1.0
187  */
188 typedef struct NetConn_Route {
189     /** Network interface */
190     char iface[NETCONN_MAX_STR_LEN];
191     /** Destination address */
192     NetConn_NetAddr destination;
193     /** Gateway address */
194     NetConn_NetAddr gateway;
195     /** Gateway exists or not */
196     int32_t hasGateway;
197     /** Default route or not */
198     int32_t isDefaultRoute;
199 } NetConn_Route;
200 
201 /**
202  * @brief Defines the proxy configuration information.
203  *
204  * @since 11
205  * @version 1.0
206  */
207 typedef struct NetConn_HttpProxy {
208     /** Host name */
209     char host[NETCONN_MAX_STR_LEN];
210     /** Exclusion list of proxy servers */
211     char exclusionList[NETCONN_MAX_EXCLUSION_SIZE][NETCONN_MAX_STR_LEN];
212     /** Actual size of the exclusion list */
213     int32_t exclusionListSize;
214     /** Port number */
215     uint16_t port;
216 } NetConn_HttpProxy;
217 
218 /**
219  * @brief Defines the network connection properties.
220  *
221  * @since 11
222  * @version 1.0
223  */
224 typedef struct NetConn_ConnectionProperties {
225     /** Network interface name */
226     char ifaceName[NETCONN_MAX_STR_LEN];
227     /** Domain name of the network connection */
228     char domain[NETCONN_MAX_STR_LEN];
229     /** TCP buffer size */
230     char tcpBufferSizes[NETCONN_MAX_STR_LEN];
231     /** MTU */
232     uint16_t mtu;
233     /** Address list */
234     NetConn_NetAddr netAddrList[NETCONN_MAX_ADDR_SIZE];
235     /** Actual size of the address list */
236     int32_t netAddrListSize;
237     /** DNS list */
238     NetConn_NetAddr dnsList[NETCONN_MAX_ADDR_SIZE];
239     /** Actual size of the DNS list */
240     int32_t dnsListSize;
241     /** Route list */
242     NetConn_Route routeList[NETCONN_MAX_ROUTE_SIZE];
243     /** Actual size of the route list */
244     int32_t routeListSize;
245     /** HTTP proxy information */
246     NetConn_HttpProxy httpProxy;
247 } NetConn_ConnectionProperties;
248 
249 /**
250  * @brief Defines the network handle list.
251  *
252  * @since 11
253  * @version 1.0
254  */
255 typedef struct NetConn_NetHandleList {
256     /** Network handle list */
257     NetConn_NetHandle netHandles[NETCONN_MAX_NET_SIZE];
258     /** Actual size of the network handle list */
259     int32_t netHandleListSize;
260 } NetConn_NetHandleList;
261 
262 /**
263  * @brief Pointer to the custom DNS resolver.
264  *
265  * @param host The host name to query.
266  * @param serv Service name.
267  * @param hint Pointer to the addrinfo structure.
268  * @param res Store DNS query results and return them in a linked list format.
269  *
270  * @since 11
271  * @version 1.0
272  */
273 typedef int (*OH_NetConn_CustomDnsResolver)(const char *host, const char *serv,
274     const struct addrinfo *hint, struct addrinfo **res);
275 
276 /**
277  * @brief Callback for application’s http proxy information changed.
278  *
279  * @param proxy The changed proxy information, may be a null pointer.
280  *
281  * @since 12
282  * @version 1.0
283  */
284 typedef void (*OH_NetConn_AppHttpProxyChange)(NetConn_HttpProxy *proxy);
285 
286 /**
287  * @brief Definition of network specifier.
288  *
289  * @since 12
290  * @version 1.0
291  */
292 typedef struct NetConn_NetSpecifier {
293     /** Network capabilities. */
294     NetConn_NetCapabilities caps;
295     /** Network identifier */
296     char *bearerPrivateIdentifier;
297 } NetConn_NetSpecifier;
298 
299 /**
300  * @brief Callback for network available.
301  *
302  * @param netHandle The network handle.
303  *
304  * @since 12
305  * @version 1.0
306  */
307 typedef void (*OH_NetConn_NetworkAvailable)(NetConn_NetHandle *netHandle);
308 
309 /**
310  * @brief Callback for network capabilities changed.
311  *
312  * @param netHandle The network handle.
313  * @param netCapabilities The network capabilities.
314  *
315  * @since 12
316  * @version 1.0
317  */
318 typedef void (*OH_NetConn_NetCapabilitiesChange)(NetConn_NetHandle *netHandle,
319                                                  NetConn_NetCapabilities *netCapabilities);
320 
321 /**
322  * @brief Callback for network connection properties changed.
323  *
324  * @param netHandle The network handle.
325  * @param connConnetionProperties The network connection properties.
326  *
327  * @since 12
328  * @version 1.0
329  */
330 typedef void (*OH_NetConn_NetConnectionPropertiesChange)(NetConn_NetHandle *netHandle,
331                                                          NetConn_ConnectionProperties *connConnetionProperties);
332 
333 /**
334  * @brief Callback for network lost.
335  *
336  * @param netHandle The network handle.
337  *
338  * @since 12
339  * @version 1.0
340  */
341 typedef void (*OH_NetConn_NetLost)(NetConn_NetHandle *netHandle);
342 
343 /**
344  * @brief Callback for network unavailable, this function invoked while network can not be available in given timeout.
345  *
346  * @since 12
347  * @version 1.0
348  */
349 typedef void (*OH_NetConn_NetUnavailable)(void);
350 
351 /**
352  * @brief Callback for network blocked status changed.
353  *
354  * @param netHandle The network handle.
355  * @param blocked The flag used to indicate whether the network will be blocked.
356  *
357  * @since 12
358  * @version 1.0
359  */
360 typedef void (*OH_NetConn_NetBlockStatusChange)(NetConn_NetHandle *netHandle, bool blocked);
361 
362 /**
363  * @brief Defines the network connection callbacks.
364  *
365  * @since 12
366  * @version 1.0
367  */
368 typedef struct NetConn_NetConnCallback {
369     /** Callback for network available */
370     OH_NetConn_NetworkAvailable onNetworkAvailable;
371     /** Callback for network capabilities changed */
372     OH_NetConn_NetCapabilitiesChange onNetCapabilitiesChange;
373     /** Callback for network connection properties changed */
374     OH_NetConn_NetConnectionPropertiesChange onConnetionProperties;
375     /** Callback for network lost */
376     OH_NetConn_NetLost onNetLost;
377     /** Callback for network unavailable, this function invoked while network can not be available in given timeout */
378     OH_NetConn_NetUnavailable onNetUnavailable;
379     /** Callback for network blocked status changed */
380     OH_NetConn_NetBlockStatusChange onNetBlockStatusChange;
381 } NetConn_NetConnCallback;
382 
383 #ifdef __cplusplus
384 }
385 #endif
386 
387 /** @} */
388 #endif /* NATIVE_NET_CONN_TYPE_H */