5 * Copyright (C) 2007 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
32 #include <sys/ioctl.h>
33 #include <arpa/inet.h>
34 #include <netinet/in.h>
39 int __net_ifaddr(int ifindex, struct in_addr *addr)
44 sk = socket(PF_INET, SOCK_DGRAM, 0);
48 memset(&ifr, 0, sizeof(ifr));
49 ifr.ifr_ifindex = ifindex;
51 if (ioctl(sk, SIOCGIFNAME, &ifr) < 0) {
56 if (ioctl(sk, SIOCGIFADDR, &ifr) < 0) {
63 *addr = ((struct sockaddr_in *) (&ifr.ifr_addr))->sin_addr;
68 char *__net_ifname(int ifindex)
73 sk = socket(PF_INET, SOCK_DGRAM, 0);
77 memset(&ifr, 0, sizeof(ifr));
78 ifr.ifr_ifindex = ifindex;
80 err = ioctl(sk, SIOCGIFNAME, &ifr);
87 return strdup(ifr.ifr_name);
90 void __net_free(void *ptr)
96 int __net_clear(int ifindex)
98 char *ifname, cmd[128];
100 ifname = __net_ifname(ifindex);
104 sprintf(cmd, "resolvconf -d %s", ifname);
105 printf("[NET] %s\n", cmd);
108 sprintf(cmd, "ip addr flush dev %s", ifname);
109 printf("[NET] %s\n", cmd);
117 int __net_set(int ifindex, struct in_addr *addr, struct in_addr *mask,
118 struct in_addr *route, struct in_addr *bcast,
119 struct in_addr *namesrv)
121 char *ifname, cmd[128], msk[32], brd[32];
123 ifname = __net_ifname(ifindex);
127 __net_clear(ifindex);
129 sprintf(msk, "%s", "24");
130 sprintf(brd, "%s", inet_ntoa(*bcast));
131 sprintf(cmd, "ip addr add %s/%s brd %s dev %s",
132 inet_ntoa(*addr), msk, brd, ifname);
133 printf("[NET] %s\n", cmd);
136 sprintf(cmd, "ip route add default via %s dev %s",
137 inet_ntoa(*route), ifname);
138 printf("[NET] %s\n", cmd);
141 sprintf(cmd, "echo \"nameserver %s\" | resolvconf -a %s",
142 inet_ntoa(*namesrv), ifname);
143 printf("[NET] %s\n", cmd);