Annotation of embedaddon/pimd/libite/ifconfig.c, revision 1.1.1.1
1.1 misho 1: /* Fastinit (finit) ifconfig() implementation.
2: *
3: * Copyright (c) 2008 Claudio Matsuoka <http://helllabs.org/finit/>
4: * Copyright (C) 2009-2014 Joachim Nilsson <joachim.nilsson@westermo.se>
5: *
6: * Permission is hereby granted, free of charge, to any person obtaining a copy
7: * of this software and associated documentation files (the "Software"), to deal
8: * in the Software without restriction, including without limitation the rights
9: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10: * copies of the Software, and to permit persons to whom the Software is
11: * furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included in
14: * all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22: * THE SOFTWARE.
23: */
24:
25: #include <arpa/inet.h>
26: #include <errno.h>
27: #include <net/if.h>
28: #include <netinet/in.h>
29: #include <string.h>
30: #include <sys/ioctl.h>
31: #include <sys/socket.h>
32: #include <unistd.h>
33:
34: extern size_t strlcpy(char *dst, const char *src, size_t siz);
35:
36: /**
37: * ifconfig - Basic ifconfig like operations on an interface
38: * @ifname: Name of interface to operate on
39: * @addr: If @up then set this optional IPv4 address
40: * @mask: If @up and @addr, and @addr is not INADDR_ANY, then set netmask
41: * @up: Control %IFF_UP flag on interface
42: *
43: * Returns:
44: * POSIX OK(0) on success, or non-zero on error.
45: */
46: int ifconfig(char *ifname, char *addr, char *mask, int up)
47: {
48: int sd, ret = -1;
49: struct ifreq ifr;
50: struct sockaddr_in *sin = (struct sockaddr_in *)&ifr.ifr_addr;
51:
52: if ((sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0)
53: return -1;
54:
55: memset(&ifr, 0, sizeof (ifr));
56: strlcpy(ifr.ifr_name, ifname, IFNAMSIZ);
57: ifr.ifr_addr.sa_family = AF_INET;
58:
59: if (up) {
60: if (addr) {
61: if (inet_pton(AF_INET, addr, &sin->sin_addr) == 1)
62: ret = ioctl(sd, SIOCSIFADDR, &ifr);
63: }
64:
65: /* Non-zero IP address */
66: if (mask && addr && strcmp(addr, "0.0.0.0")) {
67: if (inet_pton(AF_INET, mask, &sin->sin_addr) == -1)
68: ret = ioctl(sd, SIOCSIFNETMASK, &ifr);
69: }
70: }
71:
72: if (!ioctl(sd, SIOCGIFFLAGS, &ifr)) {
73: if (up)
74: ifr.ifr_flags |= IFF_UP;
75: else
76: ifr.ifr_flags &= ~IFF_UP;
77:
78: ret = ioctl(sd, SIOCSIFFLAGS, &ifr);
79: }
80:
81: close(sd);
82:
83: return ret;
84: }
85:
86: /**
87: * Local Variables:
88: * version-control: t
89: * indent-tabs-mode: t
90: * c-file-style: "linux"
91: * End:
92: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>