• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #include <osTest.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <net/if.h>
39 #include <fcntl.h>
40 
41 static char gDefaultNetif[IFNAMSIZ] = "eth0";
42 
InitIfreq(struct ifreq * ifr)43 static void InitIfreq(struct ifreq *ifr)
44 {
45     *ifr = (struct ifreq){{0}};
46     (void)strncpy_s(ifr->ifr_name, sizeof(ifr->ifr_name) - 1, gDefaultNetif, sizeof(ifr->ifr_name) - 1);
47     ifr->ifr_name[sizeof(ifr->ifr_name) - 1] = '\0';
48 }
49 
IfIndex2Name(int fd,unsigned index,char * name)50 static char *IfIndex2Name(int fd, unsigned index, char *name)
51 {
52 #if SUPPORT_IF_INDEX_TO_NAME
53     return if_indextoname(index, name);
54 #else
55     struct ifreq ifr;
56     int ret;
57 
58     ifr.ifr_ifindex = index;
59     ret = ioctl(fd, SIOCGIFNAME, &ifr);
60     if (ret < 0) {
61         return NULL;
62     }
63     ret = strncpy_s(name, IF_NAMESIZE - 1, ifr.ifr_name, IF_NAMESIZE - 1);
64     if (ret < 0) {
65         return NULL;
66     }
67     name[IF_NAMESIZE - 1] = '\0';
68     return name;
69 #endif
70 }
71 
IfName2Index(int fd,const char * name)72 static unsigned IfName2Index(int fd, const char *name)
73 {
74 #if SUPPORT_IF_NAME_TO_INDEX
75     return if_nametoindex(name);
76 #else
77     struct ifreq ifr;
78     int ret;
79 
80     ret = strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name) - 1, name, sizeof(ifr.ifr_name) - 1);
81     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
82     ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
83     ret = ioctl(fd, SIOCGIFINDEX, &ifr);
84     return ret < 0 ? 0 : ifr.ifr_ifindex;
85 #endif
86 }
87 
IoctlTestInternal(int sfd)88 static int IoctlTestInternal(int sfd)
89 {
90     struct ifreq ifr = {{0}};
91     char ifName[IFNAMSIZ] = {0}, *p = NULL;
92     unsigned int loIndex = 0;
93     unsigned int lanIndex = 0;
94     int maxIndex = 256;
95     int ret;
96 
97     for (int i = 0; i < maxIndex; ++i) {
98         p = IfIndex2Name(sfd, i, ifName);
99         if (p) {
100             if (strcmp(p, "lo") == 0) {
101                 loIndex = i;
102             } else {
103                 lanIndex = i;
104             }
105         }
106     }
107 
108     LogPrintln("ifindex of lo: %u, ifindex of lan: %u", loIndex, lanIndex);
109     ICUNIT_ASSERT_NOT_EQUAL(loIndex, 0, errno);
110     ICUNIT_ASSERT_NOT_EQUAL(lanIndex, 0, errno);
111 
112     p = IfIndex2Name(sfd, loIndex, ifName);
113     LogPrintln("ifindex %u: %s", loIndex, p);
114     ICUNIT_ASSERT_NOT_EQUAL(p, NULL, errno);
115 
116     p = IfIndex2Name(sfd, lanIndex, ifName);
117     LogPrintln("ifindex %u: %s", lanIndex, p);
118     ICUNIT_ASSERT_NOT_EQUAL(p, NULL, errno);
119 
120     ret = strncpy_s(gDefaultNetif, sizeof(gDefaultNetif) - 1, p, sizeof(gDefaultNetif) - 1);
121     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
122     gDefaultNetif[sizeof(gDefaultNetif) - 1] = '\0';
123 
124     ret = static_cast<int>(IfName2Index(sfd, p));
125     LogPrintln("index of %s: %d", p, ret);
126     ICUNIT_ASSERT_NOT_EQUAL(ret, 0, errno);
127 
128     ifr.ifr_ifindex = lanIndex;
129     ret = ioctl(sfd, SIOCGIFNAME, &ifr);
130     ICUNIT_ASSERT_EQUAL(ret, 0, errno);
131     LogPrintln("name of ifindex %u: %s", lanIndex, ifr.ifr_name);
132 
133     InitIfreq(&ifr);
134     ret = ioctl(sfd, SIOCGIFINDEX, &ifr);
135     ICUNIT_ASSERT_EQUAL(ret, 0, errno);
136     LogPrintln("index of ifname %s: %d", ifr.ifr_name, ifr.ifr_ifindex);
137 
138     InitIfreq(&ifr);
139     ret = ioctl(sfd, SIOCGIFHWADDR, &ifr);
140     ICUNIT_ASSERT_EQUAL(ret, 0, errno);
141     LogPrintln("hwaddr: %02hhX:%02hhX:%02hhX:XX:XX:XX", ifr.ifr_hwaddr.sa_data[0], ifr.ifr_hwaddr.sa_data[1], ifr.ifr_hwaddr.sa_data[2]);
142 
143     InitIfreq(&ifr);
144     ret = ioctl(sfd, SIOCGIFFLAGS, &ifr);
145     ICUNIT_ASSERT_EQUAL(ret, 0, errno);
146     LogPrintln("FLAGS of ifname %s: %#x, IFF_PROMISC: %d", ifr.ifr_name, ifr.ifr_flags, !!(ifr.ifr_flags & IFF_PROMISC));
147 
148     if (ifr.ifr_flags & IFF_PROMISC) {
149         ifr.ifr_flags &= ~(IFF_PROMISC);
150     } else {
151         ifr.ifr_flags |= IFF_PROMISC;
152     }
153     LogPrintln("SIOCSIFFLAGS FLAGS: %#x", ifr.ifr_flags);
154     ret = ioctl(sfd, SIOCSIFFLAGS, &ifr);
155     if (ret == -1) {
156         ICUNIT_ASSERT_EQUAL(errno, EPERM, errno);
157     } else {
158         ICUNIT_ASSERT_EQUAL(ret, 0, errno);
159     }
160 
161     InitIfreq(&ifr);
162     ret = ioctl(sfd, SIOCGIFFLAGS, &ifr);
163     ICUNIT_ASSERT_EQUAL(ret, 0, errno);
164     LogPrintln("FLAGS of ifname %s: %#x, IFF_PROMISC: %d", ifr.ifr_name, ifr.ifr_flags, !!(ifr.ifr_flags & IFF_PROMISC));
165 
166     ret = fcntl(sfd, F_GETFL, 0);
167     ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
168 
169     ret = fcntl(sfd, F_SETFL, ret | O_NONBLOCK);
170     ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
171 
172     return ICUNIT_SUCCESS;
173 }
174 
IoctlTest(void)175 static int IoctlTest(void)
176 {
177     int sfd;
178 
179     sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
180     LogPrintln("socket(PF_INET, SOCK_STREAM, IPPROTO_TCP): %d", sfd);
181     ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, errno);
182 
183     (void)IoctlTestInternal(sfd);
184 
185     (void)close(sfd);
186     return ICUNIT_SUCCESS;
187 }
188 
NetSocketTest010(void)189 void NetSocketTest010(void)
190 {
191     TEST_ADD_CASE(__FUNCTION__, IoctlTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
192 }
193