Annotation of embedtools/src/get1steth.c, revision 1.4.2.3
1.2 misho 1: /*************************************************************************
2: * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
3: * by Michael Pounov <misho@aitbg.com>
4: *
5: * $Author: misho $
1.4.2.3 ! misho 6: * $Id: get1steth.c,v 1.4.2.2 2013/10/14 07:21:38 misho Exp $
1.2 misho 7: *
1.3 misho 8: *************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.4.2.3 ! misho 15: Copyright 2004 - 2014
1.3 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
1.2 misho 46: #include "global.h"
47: #include "get1steth.h"
48:
49:
1.4 misho 50: char szIface[STRSIZ];
1.2 misho 51: int Verbose;
52: extern char compiled[], compiledby[], compilehost[];
53:
54:
1.4.2.1 misho 55: static void
56: Usage()
1.2 misho 57: {
58: printf("-= GET_FIRST_ETHERNET =- Get First Ethernet Interface tool\n"
59: "=== %s === %s@%s ===\n\n"
60: "Syntax: get1steth [option] [custom_first_interface]\n\n"
61: "\t-v\t\tVerbose (more -v more verbosity)\n"
62: "\t-g\t\tOnly get first interface, print and exit ...\n"
63: "\n", compiled, compiledby, compilehost);
64: }
65:
66: #ifdef HAVE_KLDNEXT
1.4.2.1 misho 67: static int
68: kldLoad()
1.2 misho 69: {
70: struct module_stat mstat;
71: register int i, j;
72: u_char flg = 0;
73:
74: memset(&mstat, 0, sizeof mstat);
75: mstat.version = sizeof mstat;
76: for (i = kldnext(0); i > 0; i = kldnext(i))
77: for (j = kldfirstmod(i); j > 0; j = modfnext(j)) {
78: if (modstat(j, &mstat) == -1)
79: continue;
80:
81: if (!strncmp(MODVLAN, mstat.name, sizeof MODVLAN)) {
82: flg = 1;
83: break;
84: }
85: }
86: if (flg)
87: return 0;
88:
89: if (kldload(MODVLAN) == -1)
90: return -1;
91:
92: return 1;
93: }
94: #endif
95:
96: // -------------------------------
97:
1.4.2.1 misho 98: int
99: main(int argc, char **argv)
1.2 misho 100: {
101: char ch, GetOnly = 0;
102: struct ifaddrs *ifa, *ifp;
103: struct sockaddr_dl *sdl;
104: struct sockaddr_in *sin;
105: struct ifreq ifr;
106: struct vlanreq vlr;
107: struct ifaliasreq ifra;
108: struct ifmediareq ifmr;
109: int s;
110:
111: while ((ch = getopt(argc, argv, "hvg")) != -1)
112: switch (ch) {
113: case 'g':
114: GetOnly = 1;
115: break;
116: case 'v':
117: Verbose++;
118: break;
119: case 'h':
120: default:
121: Usage();
122: return 1;
123: }
124: argc -= optind;
125: argv += optind;
126:
127: openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_USER);
128:
129: if (argc) {
1.4 misho 130: strlcpy(szIface, *argv, sizeof szIface);
1.2 misho 131: VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
132: } else {
133: s = socket(PF_INET, SOCK_DGRAM, 0);
134: if (-1 == s) {
135: syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
136: closelog();
137: return 1;
138: }
139:
140: getifaddrs(&ifa);
141: for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
142: if (PF_LINK == ifp->ifa_addr->sa_family &&
143: IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
144: memset(&ifmr, 0, sizeof ifmr);
145: strlcpy(ifmr.ifm_name, ifp->ifa_name, IFNAMSIZ);
146: if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
147: syslog(LOG_ERR, "Error:: media interface=%s ioctl(SIOCGIFMEDIA) #%d - %s\n",
148: ifmr.ifm_name, errno, strerror(errno));
149: close(s);
150: closelog();
151: return 1;
152: }
153:
154: if (IFM_ETHER == IFM_TYPE(ifmr.ifm_current)) {
1.4 misho 155: strlcpy(szIface, ifp->ifa_name, sizeof szIface);
1.2 misho 156: sdl = (struct sockaddr_dl*) ifp->ifa_addr;
157: VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface,
158: ether_ntoa((struct ether_addr*) LLADDR(sdl)));
159: break;
160: }
161: }
162: }
163: freeifaddrs(ifa);
164:
165: close(s);
166: }
167: if (!*szIface) {
168: syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n");
169:
170: closelog();
171: return 1;
172: }
173:
174: if (GetOnly) {
175: printf("%s\n", szIface);
176:
177: closelog();
178: return 0;
179: }
180:
181: #ifdef HAVE_KLDNEXT
182: s = kldLoad();
183: if (s == -1) {
184: syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
185: return 1;
186: } else
187: VERB(3) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded");
188: #endif
1.4.2.2 misho 189:
190: s = socket(PF_INET, SOCK_DGRAM, 0);
191: if (-1 == s) {
192: syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
193: closelog();
194: return 2;
195: }
196:
1.4.2.1 misho 197: /* up parent interface */
1.4.2.2 misho 198: memset(&ifr, 0, sizeof ifr);
199: strlcpy(ifr.ifr_name, szIface, IFNAMSIZ);
200: ifr.ifr_flags |= IFF_UP;
201:
202: if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1 && errno != EEXIST) {
203: syslog(LOG_ERR, "Error:: UP interface=%s ioctl(SIOCSIFFLAGS) #%d - %s\n",
204: szIface, errno, strerror(errno));
205: close(s);
206: return 2;
207: }
1.2 misho 208:
1.4.2.1 misho 209: /* create vlan */
1.2 misho 210: memset(&ifr, 0, sizeof ifr);
211: strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
212: vlr.vlr_tag = MGMT_VTAG;
213:
214: strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
215: ifr.ifr_data = (void *) &vlr;
216:
217: #ifdef SIOCIFCREATE2
218: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
219: #else
220: if (ioctl(s, SIOCIFCREATE, &ifr) == -1 && errno != EEXIST) {
221: #endif
222: syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n",
223: MGMT_IFACE, errno, strerror(errno));
224: close(s);
225: return 2;
1.3 misho 226: }
227:
228: memset(&ifra, 0, sizeof ifra);
229: #if defined(__FreeBSD__)
1.4.2.1 misho 230: /* rename iface */
1.3 misho 231: VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
1.2 misho 232: ifr.ifr_data = MGMT_NAME;
233: if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
234: syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n",
235: MGMT_NAME, errno, strerror(errno));
236: close(s);
237: return 2;
238: } else
239: VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.3 misho 240: strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
241: #else
242: strlcpy(ifra.ifra_name, MGMT_IFACE, IFNAMSIZ);
243: #endif
1.2 misho 244:
1.4.2.1 misho 245: /* assign address & up */
1.2 misho 246: sin = (struct sockaddr_in*) &ifra.ifra_addr;
247: sin->sin_len = sizeof ifra.ifra_addr;
248: sin->sin_family = AF_INET;
249: sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
250: sin = (struct sockaddr_in*) &ifra.ifra_mask;
251: sin->sin_len = sizeof ifra.ifra_mask;
252: sin->sin_family = AF_INET;
253: sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
254: if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
255: syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n",
256: MGMT_ADDR, errno, strerror(errno));
257: close(s);
258: return 2;
259: } else
260: VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
261:
262: close(s);
263: closelog();
264: return 0;
265: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>