• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# NetConnection
2
3
4## Overview
5
6Provides the C APIs of the network connection module for network management.
7
8**Since**: 11
9
10
11## Summary
12
13
14### File
15
16| Name| Description|
17| -------- | -------- |
18| [net_connection.h](net__connection_8h.md) | Provides the C APIs of the network connection module for network management.|
19| [net_connection_type.h](net__connection__type_8h.md) | Declares the structs for the C APIs of the network connection module.|
20
21
22### Structs
23
24| Name| Description|
25| -------- | -------- |
26| [NetConn_NetHandle](_net_conn___net_handle.md) | Network ID.|
27| [NetConn_NetCapabilities](_net_conn___net_capabilities.md) | Network capability set.|
28| [NetConn_NetAddr](_net_conn___net_addr.md) | Network address.|
29| [NetConn_Route](_net_conn___route.md) | Route configuration.|
30| [NetConn_HttpProxy](_net_conn___http_proxy.md) | Proxy configuration information.|
31| [NetConn_ConnectionProperties](_net_conn___connection_properties.md) | Network connection information.|
32| [NetConn_NetHandleList](_net_conn___net_handle_list.md) | Network list.|
33
34
35### Macros
36
37| Name| Description|
38| -------- | -------- |
39| **NETCONN_MAX_NET_SIZE** | 32 |
40| **NETCONN_MAX_BEARER_TYPE_SIZE** | 32 |
41| **NETCONN_MAX_CAP_SIZE** | 32 |
42| **NETCONN_MAX_ADDR_SIZE** | 32 |
43| **NETCONN_MAX_ROUTE_SIZE** | 64 |
44| **NETCONN_MAX_EXCLUSION_SIZE** | 256 |
45| **NETCONN_MAX_STR_LEN** | 256 |
46
47
48### Types
49
50| Name| Description|
51| -------- | -------- |
52| [NetConn_NetCap](#netconn_netcap) | Network capability set.|
53| [NetConn_NetBearerType](#netconn_netbearertype) | Network carrier type.|
54| [NetConn_NetHandle](#netconn_nethandle) | Network ID.|
55| [NetConn_NetCapabilities](#netconn_netcapabilities) | Network capability set.|
56| [NetConn_NetAddr](#netconn_netaddr) | Network address.|
57| [NetConn_Route](#netconn_route) | Route configuration.|
58| [NetConn_HttpProxy](#netconn_httpproxy) | Proxy configuration information.|
59| [NetConn_ConnectionProperties](#netconn_connectionproperties) | Network connection information.|
60| [NetConn_NetHandleList](#netconn_nethandlelist) | Network list.|
61| (\*[OH_NetConn_CustomDnsResolver](#oh_netconn_customdnsresolver)) (const char \*host, const char \*serv, const struct addrinfo \*hint, struct addrinfo \*\*res) | Pointer to the custom DNS resolver.|
62
63
64### Enums
65
66| Name| Description|
67| -------- | -------- |
68| [NetConn_NetCap](#netconn_netcap) {<br>NETCONN_NET_CAPABILITY_MMS = 0,<br>NETCONN_NET_CAPABILITY_NOT_METERED = 11,<br>NETCONN_NET_CAPABILITY_INTERNET = 12,<br>NETCONN_NET_CAPABILITY_NOT_VPN = 15,<br>NETCONN_NET_CAPABILITY_VALIDATED = 16<br>} | Network capability set.|
69| [NetConn_NetBearerType](#netconn_netbearertype) {<br>NETCONN_BEARER_CELLULAR = 0,<br>NETCONN_BEARER_WIFI = 1,<br>NETCONN_BEARER_ETHERNET = 3<br>} | Network carrier type.|
70
71
72### Functions
73
74| Name| Description|
75| -------- | -------- |
76| [OH_NetConn_HasDefaultNet](#oh_netconn_hasdefaultnet) (int32_t \*hasDefaultNet) | Checks whether a default activated data network is available.|
77| [OH_NetConn_GetDefaultNet](#oh_netconn_getdefaultnet) ([NetConn_NetHandle](_net_conn___net_handle.md) \*netHandle) | Obtains the default activated data network.|
78| [OH_NetConn_IsDefaultNetMetered](#oh_netconn_isdefaultnetmetered) (int32_t \*isMetered) | Checks whether data traffic usage on the current network is metered.|
79| [OH_NetConn_GetConnectionProperties](#oh_netconn_getconnectionproperties) ([NetConn_NetHandle](_net_conn___net_handle.md) \*netHandle, [NetConn_ConnectionProperties](_net_conn___connection_properties.md) \*prop) | Obtains the link information of a data network.|
80| [OH_NetConn_GetNetCapabilities](#oh_netconn_getnetcapabilities) ([NetConn_NetHandle](_net_conn___net_handle.md) \*netHandle, [NetConn_NetCapabilities](_net_conn___net_capabilities.md) \*netCapacities) | Obtains the capabilities of a data network.|
81| [OH_NetConn_GetDefaultHttpProxy](#oh_netconn_getdefaulthttpproxy) ([NetConn_HttpProxy](_net_conn___http_proxy.md) \*httpProxy) | Obtains the default network proxy.|
82| [OH_NetConn_GetAddrInfo](#oh_netconn_getaddrinfo) (char \*host, char \*serv, struct addrinfo \*hint, struct addrinfo \*\*res, int32_t netId) | Obtains the DNS result using **netId**.|
83| [OH_NetConn_FreeDnsResult](#oh_netconn_freednsresult) (struct addrinfo \*res) | Releases the DNS query result.|
84| [OH_NetConn_GetAllNets](#oh_netconn_getallnets) ([NetConn_NetHandleList](_net_conn___net_handle_list.md) \*netHandleList) | Obtains all activated data networks.|
85| [OHOS_NetConn_RegisterDnsResolver](#ohos_netconn_registerdnsresolver) ([OH_NetConn_CustomDnsResolver](#oh_netconn_customdnsresolver) resolver) | Registers a custom DNS resolver.|
86| [OHOS_NetConn_UnregisterDnsResolver](#ohos_netconn_unregisterdnsresolver) (void) | Unregisters a custom DNS resolver.|
87
88
89## Type Description
90
91
92### NetConn_ConnectionProperties
93
94```
95typedef struct NetConn_ConnectionPropertiesNetConn_ConnectionProperties
96```
97
98**Description**
99
100Network connection information.
101
102**Since**: 11
103
104
105### NetConn_HttpProxy
106
107```
108typedef struct NetConn_HttpProxyNetConn_HttpProxy
109```
110
111**Description**
112
113Proxy configuration information.
114
115**Since**: 11
116
117
118### NetConn_NetAddr
119
120```
121typedef struct NetConn_NetAddrNetConn_NetAddr
122```
123
124**Description**
125
126Network address.
127
128**Since**: 11
129
130
131### NetConn_NetBearerType
132
133```
134typedef enum NetConn_NetBearerTypeNetConn_NetBearerType
135```
136
137**Description**
138
139Network carrier type.
140
141**Since**: 11
142
143
144### NetConn_NetCap
145
146```
147typedef enum NetConn_NetCapNetConn_NetCap
148```
149
150**Description**
151
152Network capability set.
153
154**Since**: 11
155
156
157### NetConn_NetCapabilities
158
159```
160typedef struct NetConn_NetCapabilitiesNetConn_NetCapabilities
161```
162
163**Description**
164
165Network capability set.
166
167**Since**: 11
168
169
170### NetConn_NetHandle
171
172```
173typedef struct NetConn_NetHandleNetConn_NetHandle
174```
175
176**Description**
177
178Network ID.
179
180**Since**: 11
181
182
183### NetConn_NetHandleList
184
185```
186typedef struct NetConn_NetHandleListNetConn_NetHandleList
187```
188
189**Description**
190
191Network list.
192
193**Since**: 11
194
195
196### NetConn_Route
197
198```
199typedef struct NetConn_RouteNetConn_Route
200```
201
202**Description**
203
204Route configuration.
205
206**Since**: 11
207
208
209### OH_NetConn_CustomDnsResolver
210
211```
212typedef int(* OH_NetConn_CustomDnsResolver) (const char *host, const char *serv, const struct addrinfo *hint, struct addrinfo **res)
213```
214
215**Description**
216
217Pointer to the custom DNS resolver.
218
219**Since**: 11
220
221**Parameters**
222
223| Name| Description|
224| -------- | -------- |
225| host | Host name.|
226| serv | Service name.|
227| hint | Pointer to the addrinfo structure.|
228| res | DNS query result, which is in the format of linked lists.|
229
230
231## Enum Description
232
233
234### NetConn_NetBearerType
235
236```
237enum NetConn_NetBearerType
238```
239
240**Description**
241
242Network carrier type.
243
244**Since**: 11
245
246| Value| Description|
247| -------- | -------- |
248| NETCONN_BEARER_CELLULAR | Cellular network.|
249| NETCONN_BEARER_WIFI | Wi-Fi.|
250| NETCONN_BEARER_ETHERNET | Ethernet.|
251
252
253### NetConn_NetCap
254
255```
256enum NetConn_NetCap
257```
258
259**Description**
260
261Network capability set.
262
263**Since**: 11
264
265| Value| Description|
266| -------- | -------- |
267| NETCONN_NET_CAPABILITY_MMS | MMS.|
268| NETCONN_NET_CAPABILITY_NOT_METERED | Non-metered network.|
269| NETCONN_NET_CAPABILITY_INTERNET | Internet.|
270| NETCONN_NET_CAPABILITY_NOT_VPN | Non-VPN.|
271| NETCONN_NET_CAPABILITY_VALIDATED | Verified.|
272
273
274## Function Description
275
276
277### OH_NetConn_FreeDnsResult()
278
279```
280int32_t OH_NetConn_FreeDnsResult (struct addrinfo * res)
281```
282
283**Description**
284
285Releases the DNS query result.
286
287**System capability**: SystemCapability.Communication.NetManager.Core
288
289**Since**: 11
290
291**Parameters**
292
293| Name| Description|
294| -------- | -------- |
295| res | Header of the DNS query result, which is in the format of linked lists.|
296
297**Returns**
298
299**0**: success.
300
301**201**: no permission.
302
303**401**: parameter error.
304
305**2100002**: failed to connect to the service.
306
307**2100003**: internal error.
308
309**Required permissions**:
310
311ohos.permission.INTERNET
312
313
314### OH_NetConn_GetAddrInfo()
315
316```
317int32_t OH_NetConn_GetAddrInfo (char * host, char * serv, struct addrinfo * hint, struct addrinfo ** res, int32_t netId )
318```
319
320**Description**
321
322Obtains the DNS result using **netId**.
323
324**System capability**: SystemCapability.Communication.NetManager.Core
325
326**Since**: 11
327
328**Parameters**
329
330| Name| Description|
331| -------- | -------- |
332| host | Host name.|
333| serv | Service name.|
334| hint | Pointer to the addrinfo structure.|
335| res | DNS query result, which is in the format of linked lists.|
336| netId | If **netId** is set to **0**, the default **netid** is used for query.|
337
338**Returns**
339
340**0**: success.
341
342201 - no permission.
343
344**401**: parameter error.
345
346**2100002**: failed to connect to the service.
347
348**2100003**: internal error.
349
350**Required permissions**:
351
352ohos.permission.INTERNET
353
354
355### OH_NetConn_GetAllNets()
356
357```
358int32_t OH_NetConn_GetAllNets (NetConn_NetHandleList * netHandleList)
359```
360
361**Description**
362
363Obtains all activated data networks.
364
365**System capability**: SystemCapability.Communication.NetManager.Core
366
367**Since**: 11
368
369**Parameters**
370
371| Name| Description|
372| -------- | -------- |
373| netHandleList | Network information list.|
374
375**Returns**
376
377**0**: success.
378
379201 - no permission.
380
381**401**: parameter error.
382
383**2100002**: failed to connect to the service.
384
385**2100003**: internal error.
386
387**Required permissions**:
388
389ohos.permission.GET_NETWORK_INFO
390
391
392### OH_NetConn_GetConnectionProperties()
393
394```
395int32_t OH_NetConn_GetConnectionProperties (NetConn_NetHandle * netHandle, NetConn_ConnectionProperties * prop )
396```
397
398**Description**
399
400Obtains the link information of a data network.
401
402**System capability**: SystemCapability.Communication.NetManager.Core
403
404**Since**: 11
405
406**Parameters**
407
408| Name| Description|
409| -------- | -------- |
410| nethandle | Network ID.|
411| prop | Link information.|
412
413**Returns**
414
415**0**: success.
416
417201 - no permission.
418
419**401**: parameter error.
420
421**2100002**: failed to connect to the service.
422
423**2100003**: internal error.
424
425**Required permissions**:
426
427ohos.permission.GET_NETWORK_INFO
428
429
430### OH_NetConn_GetDefaultHttpProxy()
431
432```
433int32_t OH_NetConn_GetDefaultHttpProxy (NetConn_HttpProxy * httpProxy)
434```
435
436**Description**
437
438Obtains the default network proxy.
439
440**System capability**: SystemCapability.Communication.NetManager.Core
441
442**Since**: 11
443
444**Parameters**
445
446| Name| Description|
447| -------- | -------- |
448| httpProxy | Proxy configuration information.|
449
450**Returns**
451
452**0**: success.
453
454201 - no permission.
455
456**401**: parameter error.
457
458**2100002**: failed to connect to the service.
459
460**2100003**: internal error.
461
462
463### OH_NetConn_GetDefaultNet()
464
465```
466int32_t OH_NetConn_GetDefaultNet (NetConn_NetHandle * netHandle)
467```
468
469**Description**
470
471Obtains the default activated data network.
472
473**System capability**: SystemCapability.Communication.NetManager.Core
474
475**Since**: 11
476
477**Parameters**
478
479| Name| Description|
480| -------- | -------- |
481| netHandle | Network ID.|
482
483**Returns**
484
485**0**: success.
486
487201 - no permission.
488
489**401**: parameter error.
490
491**2100002**: failed to connect to the service.
492
493**2100003**: internal error.
494
495**Required permissions**:
496
497ohos.permission.GET_NETWORK_INFO
498
499
500### OH_NetConn_GetNetCapabilities()
501
502```
503int32_t OH_NetConn_GetNetCapabilities (NetConn_NetHandle * netHandle, NetConn_NetCapabilities * netCapacities )
504```
505
506**Description**
507
508Obtains the capabilities of a data network.
509
510**System capability**: SystemCapability.Communication.NetManager.Core
511
512**Since**: 11
513
514**Parameters**
515
516| Name| Description|
517| -------- | -------- |
518| netHandle | Network ID.|
519| netCapacities | Capability set.|
520
521**Returns**
522
523**0**: success.
524
525201 - no permission.
526
527**401**: parameter error.
528
529**2100002**: failed to connect to the service.
530
531**2100003**: internal error.
532
533**Required permissions**:
534
535ohos.permission.GET_NETWORK_INFO
536
537
538### OH_NetConn_HasDefaultNet()
539
540```
541int32_t OH_NetConn_HasDefaultNet (int32_t * hasDefaultNet)
542```
543
544**Description**
545
546Checks whether a default activated data network is available.
547
548**System capability**: SystemCapability.Communication.NetManager.Core
549
550**Since**: 11
551
552**Parameters**
553
554| Name| Description|
555| -------- | -------- |
556| hasDefaultNet | Whether there is a default network.|
557
558**Returns**
559
560**0**: success.
561
562201 - no permission.
563
564**401**: parameter error.
565
566**2100002**: failed to connect to the service.
567
568**2100003**: internal error.
569
570**Required permissions**:
571
572ohos.permission.GET_NETWORK_INFO
573
574
575### OH_NetConn_IsDefaultNetMetered()
576
577```
578int32_t OH_NetConn_IsDefaultNetMetered (int32_t * isMetered)
579```
580
581**Description**
582
583Checks whether metering is enabled for the default data network.
584
585**System capability**: SystemCapability.Communication.NetManager.Core
586
587**Since**: 11
588
589**Parameters**
590
591| Name| Description|
592| -------- | -------- |
593| isMetered | Whether metering is enabled.|
594
595**Returns**
596
597**0**: success.
598
599201 - no permission.
600
601**401**: parameter error.
602
603**2100002**: failed to connect to the service.
604
605**2100003**: internal error.
606
607**Required permissions**:
608
609ohos.permission.GET_NETWORK_INFO
610
611
612### OHOS_NetConn_RegisterDnsResolver()
613
614```
615int32_t OHOS_NetConn_RegisterDnsResolver (OH_NetConn_CustomDnsResolver resolver)
616```
617
618**Description**
619
620Registers a custom DNS resolver.
621
622**System capability**: SystemCapability.Communication.NetManager.Core
623
624**Since**: 11
625
626**Parameters**
627
628| Name| Description|
629| -------- | -------- |
630| resolver | Pointer to the custom DNS resolver.|
631
632**Returns**
633
634**0**: success.
635
636201 - no permission.
637
638**401**: parameter error.
639
640**2100002**: failed to connect to the service.
641
642**2100003**: internal error.
643
644**Required permissions**:
645
646ohos.permission.INTERNET
647
648
649### OHOS_NetConn_UnregisterDnsResolver()
650
651```
652int32_t OHOS_NetConn_UnregisterDnsResolver (void )
653```
654
655**Description**
656
657Unregisters a custom DNS resolver.
658
659**System capability**: SystemCapability.Communication.NetManager.Core
660
661**Since**: 11
662
663**Returns**
664
665**0**: success.
666
667201 - no permission.
668
669**401**: parameter error.
670
671**2100002**: failed to connect to the service.
672
673**2100003**: internal error.
674
675**Required permissions**:
676
677ohos.permission.INTERNET
678