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 <sys/ioctl.h>
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22
GetNetworkIfIp(int32_t fd,struct ifreq * req,char * ip,char * netmask,uint32_t len)23 static int32_t GetNetworkIfIp(int32_t fd, struct ifreq *req, char *ip, char *netmask, uint32_t len)
24 {
25 if (ioctl(fd, SIOCGIFFLAGS, (char*)req) < 0) {
26 HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl SIOCGIFFLAGS fail, errno = %d", errno);
27 return SOFTBUS_ERR;
28 }
29 if (!((uint16_t)req->ifr_flags & IFF_UP)) {
30 HILOG_ERROR(SOFTBUS_HILOG_ID, "interface is not up");
31 return SOFTBUS_ERR;
32 }
33
34 /* get IP of this interface */
35 if (ioctl(fd, SIOCGIFADDR, (char*)req) < 0) {
36 HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl SIOCGIFADDR fail, errno = %d", errno);
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 HILOG_ERROR(SOFTBUS_HILOG_ID, "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 HILOG_ERROR(SOFTBUS_HILOG_ID, "ioctl SIOCGIFNETMASK fail, errno = %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 HILOG_ERROR(SOFTBUS_HILOG_ID, "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 HILOG_ERROR(SOFTBUS_HILOG_ID, "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 HILOG_ERROR(SOFTBUS_HILOG_ID, "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 HILOG_ERROR(SOFTBUS_HILOG_ID, "copy netIfName:%{public}s fail", ifName);
74 close(fd);
75 return SOFTBUS_ERR;
76 }
77 if (GetNetworkIfIp(fd, &ifr, ip, netmask, len) != SOFTBUS_OK) {
78 HILOG_ERROR(SOFTBUS_HILOG_ID, "GetNetworkIfIp ifName:%{public}s fail", ifName);
79 close(fd);
80 return SOFTBUS_ERR;
81 }
82 close(fd);
83 return SOFTBUS_OK;
84 }
85