1 /*
2 * Copyright (c) 2021 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 #include "lnn_ip_utils_adapter.h"
17
18 #include <arpa/inet.h>
19 #include <net/if.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23
24 #include "comm_log.h"
25
GetNetworkIfIp(int32_t fd,struct ifreq * req,char * ip,char * netmask,uint32_t len)26 static int32_t GetNetworkIfIp(int32_t fd, struct ifreq *req, char *ip, char *netmask, uint32_t len)
27 {
28 if (ioctl(fd, SIOCGIFFLAGS, (char *)req) < 0) {
29 return SOFTBUS_NETWORK_IOCTL_FAIL;
30 }
31 if (!((uint16_t)req->ifr_flags & IFF_UP)) {
32 return SOFTBUS_NETWORK_IFF_NOT_UP;
33 }
34
35 /* get IP of this interface */
36 if (ioctl(fd, SIOCGIFADDR, (char *)req) < 0) {
37 return SOFTBUS_NETWORK_IOCTL_FAIL;
38 }
39 struct sockaddr_in *sockAddr = (struct sockaddr_in *)&(req->ifr_addr);
40 if (inet_ntop(sockAddr->sin_family, &sockAddr->sin_addr, ip, len) == NULL) {
41 COMM_LOGE(COMM_ADAPTER, "convert ip addr to string failed");
42 return SOFTBUS_NETWORK_INET_NTOP_FAIL;
43 }
44
45 /* get netmask of this interface */
46 if (netmask != NULL) {
47 if (ioctl(fd, SIOCGIFNETMASK, (char *)req) < 0) {
48 COMM_LOGE(COMM_ADAPTER, "ioctl SIOCGIFNETMASK fail, errno=%{public}d", errno);
49 return SOFTBUS_NETWORK_IOCTL_FAIL;
50 }
51 sockAddr = (struct sockaddr_in *)&(req->ifr_netmask);
52 if (inet_ntop(sockAddr->sin_family, &sockAddr->sin_addr, netmask, len) == NULL) {
53 COMM_LOGE(COMM_ADAPTER, "convert netmask addr to string failed");
54 return SOFTBUS_NETWORK_INET_NTOP_FAIL;
55 }
56 }
57 return SOFTBUS_OK;
58 }
59
GetNetworkIpByIfName(const char * ifName,char * ip,char * netmask,uint32_t len)60 int32_t GetNetworkIpByIfName(const char *ifName, char *ip, char *netmask, uint32_t len)
61 {
62 if (ifName == NULL || ip == NULL) {
63 COMM_LOGE(COMM_ADAPTER, "ifName or ip buffer is NULL!");
64 return SOFTBUS_INVALID_PARAM;
65 }
66 int32_t fd = socket(AF_INET, SOCK_DGRAM, 0);
67 if (fd < 0) {
68 COMM_LOGE(COMM_ADAPTER, "open socket failed");
69 return SOFTBUS_NETWORK_OPEN_SOCKET_FAIL;
70 }
71 struct ifreq ifr;
72 if (strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifName, strlen(ifName)) != EOK) {
73 COMM_LOGE(COMM_ADAPTER, "copy netIfName fail. netIfName=%{public}s", ifName);
74 close(fd);
75 return SOFTBUS_STRCPY_ERR;
76 }
77 int32_t ret = GetNetworkIfIp(fd, &ifr, ip, netmask, len);
78 if (ret != SOFTBUS_OK) {
79 close(fd);
80 return ret;
81 }
82 close(fd);
83 return SOFTBUS_OK;
84 }
85
GetLinkUpStateByIfName(const char * ifName)86 bool GetLinkUpStateByIfName(const char *ifName)
87 {
88 if (ifName == NULL) {
89 COMM_LOGE(COMM_ADAPTER, "ifName buffer is NULL!");
90 return false;
91 }
92 int32_t fd = socket(AF_INET, SOCK_DGRAM, 0);
93 if (fd < 0) {
94 COMM_LOGE(COMM_ADAPTER, "open socket failed");
95 return false;
96 }
97 struct ifreq ifr;
98 if (strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifName, strlen(ifName)) != EOK) {
99 COMM_LOGE(COMM_ADAPTER, "copy netIfName fail. netIfName=%{public}s", ifName);
100 close(fd);
101 return false;
102 }
103 if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
104 COMM_LOGE(COMM_ADAPTER, "open socket failed");
105 close(fd);
106 return false;
107 }
108 if (!((uint16_t)ifr.ifr_flags & IFF_UP)) {
109 COMM_LOGE(COMM_ADAPTER, "ifname flag is not up");
110 close(fd);
111 return false;
112 }
113 close(fd);
114 return true;
115 }
116