• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/types.h>
23 #include <unistd.h>
24 
GetNetworkIfIp(int32_t fd,struct ifreq * req,char * ip,char * netmask,uint32_t len)25 static int32_t GetNetworkIfIp(int32_t fd, struct ifreq *req, char *ip, char *netmask, uint32_t len)
26 {
27     if (ioctl(fd, SIOCGIFFLAGS, (char*)req) < 0) {
28         HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl SIOCGIFFLAGS fail, errno = %d", errno);
29         return SOFTBUS_ERR;
30     }
31     if (!((uint16_t)req->ifr_flags & IFF_UP)) {
32         HILOG_ERROR(SOFTBUS_HILOG_ID, "interface is not up");
33         return SOFTBUS_ERR;
34     }
35 
36     /* get IP of this interface */
37     if (ioctl(fd, SIOCGIFADDR, (char*)req) < 0) {
38         HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl SIOCGIFADDR fail, errno = %d", errno);
39         return SOFTBUS_ERR;
40     }
41     struct sockaddr_in *sockAddr = (struct sockaddr_in *)&(req->ifr_addr);
42     if (inet_ntop(sockAddr->sin_family, &sockAddr->sin_addr, ip, len) == NULL) {
43         HILOG_ERROR(SOFTBUS_HILOG_ID, "convert ip addr to string failed");
44         return SOFTBUS_ERR;
45     }
46 
47     /* get netmask of this interface */
48     if (netmask != NULL) {
49         if (ioctl(fd, SIOCGIFNETMASK, (char*)req) < 0) {
50             HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl SIOCGIFNETMASK fail, errno = %d", errno);
51             return SOFTBUS_ERR;
52         }
53         sockAddr = (struct sockaddr_in *)&(req->ifr_netmask);
54         if (inet_ntop(sockAddr->sin_family, &sockAddr->sin_addr, netmask, len) == NULL) {
55             HILOG_ERROR(SOFTBUS_HILOG_ID, "convert netmask addr to string failed");
56             return SOFTBUS_ERR;
57         }
58     }
59     return SOFTBUS_OK;
60 }
61 
GetNetworkIpByIfName(const char * ifName,char * ip,char * netmask,uint32_t len)62 int32_t GetNetworkIpByIfName(const char *ifName, char *ip, char *netmask, uint32_t len)
63 {
64     if (ifName == NULL || ip == NULL) {
65         HILOG_ERROR(SOFTBUS_HILOG_ID, "ifName or ip buffer is NULL!");
66         return SOFTBUS_INVALID_PARAM;
67     }
68     int32_t fd = socket(AF_INET, SOCK_DGRAM, 0);
69     if (fd < 0) {
70         HILOG_ERROR(SOFTBUS_HILOG_ID, "open socket failed");
71         return SOFTBUS_ERR;
72     }
73     struct ifreq ifr;
74     if (strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name), ifName, strlen(ifName)) != EOK) {
75         HILOG_ERROR(SOFTBUS_HILOG_ID, "copy netIfName:%{public}s fail", ifName);
76         close(fd);
77         return SOFTBUS_ERR;
78     }
79     if (GetNetworkIfIp(fd, &ifr, ip, netmask, len) != SOFTBUS_OK) {
80         HILOG_ERROR(SOFTBUS_HILOG_ID, "GetNetworkIfIp ifName:%{public}s fail", ifName);
81         close(fd);
82         return SOFTBUS_ERR;
83     }
84     close(fd);
85     return SOFTBUS_OK;
86 }
87