1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific prior written
16 * permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "softbus_lwip_monitor.h"
32
33 #include <lwip/netif.h>
34 #include <securec.h>
35 #include "hdf_dsoftbus_driver.h"
36 #include "module_manager.h"
37
38 #define HDF_LOG_TAG "softbus_lwip_monitor"
39 #define NETIF_STATUS_UP 1
40 #define NETIF_STATUS_DOWN 0
41 #define NETIF_NAME_LENGTH 16
42
43 typedef enum {
44 EVENT_IFACE_STATUS_CHANGED = 0,
45 EVENT_IFACE_ADDR_CHANGED,
46 EVENT_MAX_INDEX
47 } Event;
48
49 typedef struct {
50 uint32_t event;
51 char ifName[NETIF_NAME_LENGTH];
52 union ExtInfo {
53 int32_t status;
54 } extInfo;
55 } LwipMonitorReportInfo;
56
ReportEvent(const LwipMonitorReportInfo * reportInfo)57 static void ReportEvent(const LwipMonitorReportInfo *reportInfo)
58 {
59 struct HdfSBuf *data = HdfSbufObtainDefaultSize();
60
61 if (data == NULL) {
62 dprintf("get sbuf fail\n");
63 return;
64 }
65 if (!HdfSbufWriteBuffer(data, (const void *)reportInfo, sizeof(LwipMonitorReportInfo))) {
66 dprintf("sbuf write report value fail\n");
67 HdfSbufRecycle(data);
68 return;
69 }
70 HdfSoftbusBroadcastEvent(SOFTBUS_MODULE_LWIP_MONITOR, data);
71 HdfSbufRecycle(data);
72 }
73
NetifStatusCallback(struct netif * netif,netif_nsc_reason_t reason,const netif_ext_callback_args_t * args)74 static void NetifStatusCallback(struct netif *netif, netif_nsc_reason_t reason,
75 const netif_ext_callback_args_t *args)
76 {
77 LwipMonitorReportInfo reportInfo;
78 bool needReport = true;
79
80 (void)args;
81 if (netif == NULL) {
82 dprintf("NetifStatusCallback: input netif is NULL\n");
83 return;
84 }
85 dprintf("NetifStatusCallback(%s): nsc event: 0x%x\n", netif->full_name, (uint32_t)reason);
86 if (strncpy_s(reportInfo.ifName, NETIF_NAME_LENGTH, netif->full_name, strlen(netif->full_name)) != EOK) {
87 dprintf("NetifStatusCallback(%s): copy netif full name fail: %s\n", netif->full_name);
88 return;
89 }
90 switch (reason) {
91 case LWIP_NSC_STATUS_CHANGED:
92 reportInfo.event = EVENT_IFACE_STATUS_CHANGED;
93 reportInfo.extInfo.status = args->status_changed.state;
94 dprintf("NetifStatusCallback(%s): nsc status changed: %d\n", netif->full_name, args->status_changed.state);
95 break;
96 case LWIP_NSC_IPV4_ADDRESS_CHANGED:
97 reportInfo.event = EVENT_IFACE_ADDR_CHANGED;
98 dprintf("NetifStatusCallback(%s): nsc ipv4 addr changed\n", netif->full_name);
99 break;
100 case LWIP_NSC_NETIF_ADDED:
101 dprintf("NetifStatusCallback(%s): nsc netif added: %d\n", netif->full_name, netif_is_up(netif));
102 if (netif_is_up(netif)) {
103 reportInfo.event = EVENT_IFACE_STATUS_CHANGED;
104 reportInfo.extInfo.status = NETIF_STATUS_UP;
105 }
106 break;
107 case LWIP_NSC_NETIF_REMOVED:
108 dprintf("NetifStatusCallback(%s): nsc netif removed\n", netif->full_name);
109 reportInfo.event = EVENT_IFACE_STATUS_CHANGED;
110 reportInfo.extInfo.status = NETIF_STATUS_DOWN;
111 break;
112 default:
113 needReport = false;
114 break;
115 }
116 if (needReport) {
117 ReportEvent(&reportInfo);
118 }
119 }
120
SoftbusLwipMonitorInit(struct HdfDeviceObject * device)121 int32_t SoftbusLwipMonitorInit(struct HdfDeviceObject *device)
122 {
123 (void)device;
124 NETIF_DECLARE_EXT_CALLBACK(NetifCallback);
125 netif_add_ext_callback(&NetifCallback, NetifStatusCallback);
126
127 dprintf("SoftbusLwipMonitorInit init success...\n");
128 return HDF_SUCCESS;
129 }
130