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 (void)strncpy_s(ifr.ifr_name, sizeof(ifr.ifr_name) - 1, name, sizeof(ifr.ifr_name) - 1);
81 ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
82 ret = ioctl(fd, SIOCGIFINDEX, &ifr);
83 return ret < 0 ? 0 : ifr.ifr_ifindex;
84 #endif
85 }
86
IoctlTestInternal(int sfd)87 static int IoctlTestInternal(int sfd)
88 {
89 struct ifreq ifr = {{0}};
90 char ifName[IFNAMSIZ] = {0}, *p = NULL;
91 unsigned int loIndex = 0;
92 unsigned int lanIndex = 0;
93 int maxIndex = 256;
94 int ret;
95
96 for (int i = 0; i < maxIndex; ++i) {
97 p = IfIndex2Name(sfd, i, ifName);
98 if (p) {
99 if (strcmp(p, "lo") == 0) {
100 loIndex = i;
101 } else {
102 lanIndex = i;
103 }
104 }
105 }
106
107 LogPrintln("ifindex of lo: %u, ifindex of lan: %u", loIndex, lanIndex);
108 ICUNIT_ASSERT_NOT_EQUAL(loIndex, 0, errno);
109 ICUNIT_ASSERT_NOT_EQUAL(lanIndex, 0, errno);
110
111 p = IfIndex2Name(sfd, loIndex, ifName);
112 LogPrintln("ifindex %u: %s", loIndex, p);
113 ICUNIT_ASSERT_NOT_EQUAL(p, NULL, errno);
114
115 p = IfIndex2Name(sfd, lanIndex, ifName);
116 LogPrintln("ifindex %u: %s", lanIndex, p);
117 ICUNIT_ASSERT_NOT_EQUAL(p, NULL, errno);
118
119 ret = strncpy_s(gDefaultNetif, sizeof(gDefaultNetif) -1, p, sizeof(gDefaultNetif) - 1);
120 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
121 gDefaultNetif[sizeof(gDefaultNetif) - 1] = '\0';
122
123 ret = (int)IfName2Index(sfd, p);
124 LogPrintln("index of %s: %d", p, ret);
125 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, errno);
126
127 ifr.ifr_ifindex = lanIndex;
128 ret = ioctl(sfd, SIOCGIFNAME, &ifr);
129 ICUNIT_ASSERT_EQUAL(ret, 0, errno);
130 LogPrintln("name of ifindex %u: %s", lanIndex, ifr.ifr_name);
131
132 InitIfreq(&ifr);
133 ret = ioctl(sfd, SIOCGIFINDEX, &ifr);
134 ICUNIT_ASSERT_EQUAL(ret, 0, errno);
135 LogPrintln("index of ifname %s: %d", ifr.ifr_name, ifr.ifr_ifindex);
136
137 InitIfreq(&ifr);
138 ret = ioctl(sfd, SIOCGIFHWADDR, &ifr);
139 ICUNIT_ASSERT_EQUAL(ret, 0, errno);
140 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]);
141
142 InitIfreq(&ifr);
143 ret = ioctl(sfd, SIOCGIFFLAGS, &ifr);
144 ICUNIT_ASSERT_EQUAL(ret, 0, errno);
145 LogPrintln("FLAGS of ifname %s: %#x, IFF_PROMISC: %d", ifr.ifr_name, ifr.ifr_flags, !!(ifr.ifr_flags & IFF_PROMISC));
146
147 if (ifr.ifr_flags & IFF_PROMISC) {
148 ifr.ifr_flags &= ~(IFF_PROMISC);
149 } else {
150 ifr.ifr_flags |= IFF_PROMISC;
151 }
152 LogPrintln("SIOCSIFFLAGS FLAGS: %#x", ifr.ifr_flags);
153 ret = ioctl(sfd, SIOCSIFFLAGS, &ifr);
154 if (ret == -1) {
155 ICUNIT_ASSERT_EQUAL(errno, EPERM, errno);
156 } else {
157 ICUNIT_ASSERT_EQUAL(ret, 0, errno);
158 }
159
160 InitIfreq(&ifr);
161 ret = ioctl(sfd, SIOCGIFFLAGS, &ifr);
162 ICUNIT_ASSERT_EQUAL(ret, 0, errno);
163 LogPrintln("FLAGS of ifname %s: %#x, IFF_PROMISC: %d", ifr.ifr_name, ifr.ifr_flags, !!(ifr.ifr_flags & IFF_PROMISC));
164
165 ret = fcntl(sfd, F_GETFL, 0);
166 ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
167
168 ret = fcntl(sfd, F_SETFL, ret | O_NONBLOCK);
169 ICUNIT_ASSERT_EQUAL(ret < 0, 0, errno);
170
171 return ICUNIT_SUCCESS;
172 }
173
IoctlTest(void)174 static int IoctlTest(void)
175 {
176 int sfd;
177
178 sfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
179 LogPrintln("socket(PF_INET, SOCK_STREAM, IPPROTO_TCP): %d", sfd);
180 ICUNIT_ASSERT_NOT_EQUAL(sfd, -1, errno);
181
182 (void)IoctlTestInternal(sfd);
183
184 (void)close(sfd);
185 return ICUNIT_SUCCESS;
186 }
187
NetSocketTest010(void)188 void NetSocketTest010(void)
189 {
190 TEST_ADD_CASE(__FUNCTION__, IoctlTest, TEST_POSIX, TEST_TCP, TEST_LEVEL0, TEST_FUNCTION);
191 }
192