Annotation of embedtools/src/get1steth.c, revision 1.1.2.8
1.1.2.1 misho 1: #include "global.h"
1.1.2.2 misho 2: #include "get1steth.h"
1.1.2.1 misho 3:
4:
1.1.2.2 misho 5: char szIface[MAX_STR];
6: int Verbose;
7: extern char compiled[], compiledby[], compilehost[];
8:
9:
10: static void Usage()
1.1.2.1 misho 11: {
1.1.2.2 misho 12: printf("-= GET_FIRST_ETHERNET =- Get First Ethernet Interface tool\n"
13: "=== %s === %s@%s ===\n\n"
14: "Syntax: get1steth [option] [custom_first_interface]\n\n"
15: "\t-v\t\tVerbose (more -v more verbosity)\n"
16: "\t-g\t\tOnly get first interface, print and exit ...\n"
17: "\n", compiled, compiledby, compilehost);
18: }
19:
1.1.2.5 misho 20: static int kldLoad()
21: {
22: struct module_stat mstat;
23: register int i, j;
24: u_char flg = 0;
25:
26: memset(&mstat, 0, sizeof mstat);
27: mstat.version = sizeof mstat;
28: for (i = kldnext(0); i > 0; i = kldnext(i))
29: for (j = kldfirstmod(i); j > 0; j = modfnext(j)) {
30: if (modstat(j, &mstat) == -1)
31: continue;
32:
33: if (!strncmp(MODVLAN, mstat.name, sizeof MODVLAN)) {
34: flg = 1;
35: break;
36: }
37: }
38: if (flg)
39: return 0;
40:
41: if (kldload(MODVLAN) == -1)
42: return -1;
43:
44: return 1;
45: }
46:
1.1.2.2 misho 47: // -------------------------------
48:
49: int main(int argc, char **argv)
50: {
51: char ch, GetOnly = 0;
52: struct ifaddrs *ifa, *ifp;
53: struct sockaddr_dl *sdl;
1.1.2.4 misho 54: struct sockaddr_in *sin;
1.1.2.2 misho 55: struct ifreq ifr;
1.1.2.3 misho 56: struct vlanreq vlr;
1.1.2.4 misho 57: struct ifaliasreq ifra;
1.1.2.6 misho 58: struct ifmediareq ifmr;
1.1.2.2 misho 59: int s;
60:
61: while ((ch = getopt(argc, argv, "hvg")) != -1)
62: switch (ch) {
63: case 'g':
64: GetOnly = 1;
65: break;
66: case 'v':
67: Verbose++;
68: break;
69: case 'h':
70: default:
71: Usage();
72: return 1;
73: }
74: argc -= optind;
75: argv += optind;
76:
1.1.2.3 misho 77: openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_CONSOLE | LOG_USER);
1.1.2.2 misho 78:
79: if (argc) {
80: strlcpy(szIface, *argv, MAX_STR);
81: VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
82: } else {
1.1.2.7 misho 83: s = socket(PF_INET, SOCK_DGRAM, 0);
84: if (-1 == s) {
85: syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
86: closelog();
87: return 1;
88: }
89:
1.1.2.2 misho 90: getifaddrs(&ifa);
91: for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
92: if (PF_LINK == ifp->ifa_addr->sa_family &&
93: IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
1.1.2.6 misho 94: memset(&ifmr, 0, sizeof ifmr);
95: strlcpy(ifmr.ifm_name, ifp->ifa_name, IFNAMSIZ);
96: if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
97: syslog(LOG_ERR, "Error:: media interface=%s ioctl(SIOCGIFMEDIA) #%d - %s\n",
98: ifmr.ifm_name, errno, strerror(errno));
99: close(s);
100: closelog();
101: return 1;
102: }
103:
104: if (IFM_ETHER == IFM_TYPE(ifmr.ifm_current)) {
105: strlcpy(szIface, ifp->ifa_name, MAX_STR);
106: sdl = (struct sockaddr_dl*) ifp->ifa_addr;
107: VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface,
108: ether_ntoa((struct ether_addr*) LLADDR(sdl)));
109: break;
110: }
1.1.2.2 misho 111: }
112: }
113: freeifaddrs(ifa);
1.1.2.7 misho 114:
115: close(s);
1.1.2.2 misho 116: }
1.1.2.6 misho 117: if (!*szIface) {
118: syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n");
119:
120: closelog();
121: return 1;
122: }
123:
1.1.2.2 misho 124: if (GetOnly) {
125: printf("%s\n", szIface);
126:
127: closelog();
128: return 0;
129: }
130:
1.1.2.5 misho 131: s = kldLoad();
132: if (s == -1) {
1.1.2.6 misho 133: syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
1.1.2.5 misho 134: return 1;
135: } else
1.1.2.8 ! misho 136: VERB(3) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded");
1.1.2.5 misho 137:
1.1.2.3 misho 138: // create vlan
1.1.2.2 misho 139: memset(&ifr, 0, sizeof ifr);
1.1.2.3 misho 140: strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
141: vlr.vlr_tag = MGMT_VTAG;
142:
143: strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
144: ifr.ifr_data = (void *) &vlr;
1.1.2.2 misho 145:
1.1.2.7 misho 146: s = socket(PF_INET, SOCK_DGRAM, 0);
147: if (-1 == s) {
148: syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
149: closelog();
150: return 2;
151: }
1.1.2.4 misho 152: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
1.1.2.3 misho 153: syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n",
154: MGMT_IFACE, errno, strerror(errno));
1.1.2.2 misho 155: close(s);
156: return 2;
1.1.2.3 misho 157: } else
158: VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
159: // rename iface
160: ifr.ifr_data = MGMT_NAME;
1.1.2.4 misho 161: if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
1.1.2.3 misho 162: syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n",
163: MGMT_NAME, errno, strerror(errno));
164: close(s);
165: return 2;
166: } else
167: VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.1.2.4 misho 168:
1.1.2.3 misho 169: // assign address & up
1.1.2.4 misho 170: memset(&ifra, 0, sizeof ifra);
171: strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
172: sin = (struct sockaddr_in*) &ifra.ifra_addr;
173: sin->sin_len = sizeof ifra.ifra_addr;
174: sin->sin_family = AF_INET;
175: sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
176: sin = (struct sockaddr_in*) &ifra.ifra_mask;
177: sin->sin_len = sizeof ifra.ifra_mask;
178: sin->sin_family = AF_INET;
179: sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
180: if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
181: syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n",
182: MGMT_ADDR, errno, strerror(errno));
183: close(s);
184: return 2;
185: } else
186: VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
1.1.2.2 misho 187:
188: close(s);
189: closelog();
1.1.2.1 misho 190: return 0;
191: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>