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_ERR;
30     }
31     if (!((uint16_t)req->ifr_flags & IFF_UP)) {
32         return SOFTBUS_ERR;
33     }
34 
35     /* get IP of this interface */
36     if (ioctl(fd, SIOCGIFADDR, (char *)req) < 0) {
37         return SOFTBUS_ERR;
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_ERR;
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_ERR;
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_ERR;
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_ERR;
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_ERR;
76     }
77     if (GetNetworkIfIp(fd, &ifr, ip, netmask, len) != SOFTBUS_OK) {
78         close(fd);
79         return SOFTBUS_ERR;
80     }
81     close(fd);
82     return SOFTBUS_OK;
83 }
84