Annotation of embedtools/src/get1steth.c, revision 1.4.2.1
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.1 ! misho 6: * $Id: get1steth.c,v 1.4 2013/01/18 12:58:14 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.1 ! misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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.1 ! misho 189: /* up parent interface */
1.2 misho 190:
1.4.2.1 ! misho 191: /* create vlan */
1.2 misho 192: memset(&ifr, 0, sizeof ifr);
193: strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
194: vlr.vlr_tag = MGMT_VTAG;
195:
196: strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
197: ifr.ifr_data = (void *) &vlr;
198:
199: s = socket(PF_INET, SOCK_DGRAM, 0);
200: if (-1 == s) {
201: syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
202: closelog();
203: return 2;
204: }
205: #ifdef SIOCIFCREATE2
206: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
207: #else
208: if (ioctl(s, SIOCIFCREATE, &ifr) == -1 && errno != EEXIST) {
209: #endif
210: syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n",
211: MGMT_IFACE, errno, strerror(errno));
212: close(s);
213: return 2;
1.3 misho 214: }
215:
216: memset(&ifra, 0, sizeof ifra);
217: #if defined(__FreeBSD__)
1.4.2.1 ! misho 218: /* rename iface */
1.3 misho 219: VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
1.2 misho 220: ifr.ifr_data = MGMT_NAME;
221: if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
222: syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n",
223: MGMT_NAME, errno, strerror(errno));
224: close(s);
225: return 2;
226: } else
227: VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.3 misho 228: strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
229: #else
230: strlcpy(ifra.ifra_name, MGMT_IFACE, IFNAMSIZ);
231: #endif
1.2 misho 232:
1.4.2.1 ! misho 233: /* assign address & up */
1.2 misho 234: sin = (struct sockaddr_in*) &ifra.ifra_addr;
235: sin->sin_len = sizeof ifra.ifra_addr;
236: sin->sin_family = AF_INET;
237: sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
238: sin = (struct sockaddr_in*) &ifra.ifra_mask;
239: sin->sin_len = sizeof ifra.ifra_mask;
240: sin->sin_family = AF_INET;
241: sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
242: if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
243: syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n",
244: MGMT_ADDR, errno, strerror(errno));
245: close(s);
246: return 2;
247: } else
248: VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
249:
250: close(s);
251: closelog();
252: return 0;
253: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>