5 * Copyright (C) 2007-2009 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
34 #include <net/ethernet.h>
38 int inet_name2index(const char *name)
46 sk = socket(PF_INET, SOCK_DGRAM, 0);
50 memset(&ifr, 0, sizeof(ifr));
51 strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
53 err = ioctl(sk, SIOCGIFINDEX, &ifr);
60 return ifr.ifr_ifindex;
63 char *inet_index2name(int index)
71 sk = socket(PF_INET, SOCK_DGRAM, 0);
75 memset(&ifr, 0, sizeof(ifr));
76 ifr.ifr_ifindex = index;
78 err = ioctl(sk, SIOCGIFNAME, &ifr);
85 return strdup(ifr.ifr_name);
88 char *inet_index2ident(int index, const char *prefix)
91 struct ether_addr *eth;
98 sk = socket(PF_INET, SOCK_DGRAM, 0);
102 memset(&ifr, 0, sizeof(ifr));
103 ifr.ifr_ifindex = index;
105 err = ioctl(sk, SIOCGIFNAME, &ifr);
108 err = ioctl(sk, SIOCGIFHWADDR, &ifr);
115 len = prefix ? strlen(prefix) + 18 : 18;
121 eth = (void *) &ifr.ifr_hwaddr.sa_data;
122 snprintf(str, len, "%s%02X_%02X_%02X_%02X_%02X_%02X",
123 prefix ? prefix : "",
124 eth->ether_addr_octet[0],
125 eth->ether_addr_octet[1],
126 eth->ether_addr_octet[2],
127 eth->ether_addr_octet[3],
128 eth->ether_addr_octet[4],
129 eth->ether_addr_octet[5]);
134 int inet_ifup(int index)
139 sk = socket(PF_INET, SOCK_DGRAM, 0);
143 memset(&ifr, 0, sizeof(ifr));
144 ifr.ifr_ifindex = index;
146 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
151 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
156 if (ifr.ifr_flags & IFF_UP) {
161 ifr.ifr_flags |= IFF_UP;
163 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0) {
176 int inet_ifdown(int index)
181 sk = socket(PF_INET, SOCK_DGRAM, 0);
185 memset(&ifr, 0, sizeof(ifr));
186 ifr.ifr_ifindex = index;
188 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
193 if (ioctl(sk, SIOCGIFFLAGS, &ifr) < 0) {
198 if (!(ifr.ifr_flags & IFF_UP)) {
203 ifr.ifr_flags &= ~IFF_UP;
205 if (ioctl(sk, SIOCSIFFLAGS, &ifr) < 0)