Annotation of embedtools/src/get1steth.c, revision 1.1.2.6
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:
1.1.2.6 ! misho 79: s = socket(PF_INET, SOCK_STREAM, 0);
! 80: if (-1 == s) {
! 81: syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
! 82: closelog();
! 83: return 1;
! 84: }
1.1.2.2 misho 85: if (argc) {
86: strlcpy(szIface, *argv, MAX_STR);
87: VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
88: } else {
89: getifaddrs(&ifa);
90: for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
91: if (PF_LINK == ifp->ifa_addr->sa_family &&
92: IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
1.1.2.6 ! misho 93: memset(&ifmr, 0, sizeof ifmr);
! 94: strlcpy(ifmr.ifm_name, ifp->ifa_name, IFNAMSIZ);
! 95: if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
! 96: syslog(LOG_ERR, "Error:: media interface=%s ioctl(SIOCGIFMEDIA) #%d - %s\n",
! 97: ifmr.ifm_name, errno, strerror(errno));
! 98: close(s);
! 99: closelog();
! 100: return 1;
! 101: }
! 102:
! 103: if (IFM_ETHER == IFM_TYPE(ifmr.ifm_current)) {
! 104: strlcpy(szIface, ifp->ifa_name, MAX_STR);
! 105: sdl = (struct sockaddr_dl*) ifp->ifa_addr;
! 106: VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface,
! 107: ether_ntoa((struct ether_addr*) LLADDR(sdl)));
! 108: break;
! 109: }
1.1.2.2 misho 110: }
111: }
112: freeifaddrs(ifa);
113: }
1.1.2.6 ! misho 114: if (!*szIface) {
! 115: syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n");
! 116:
! 117: close(s);
! 118: closelog();
! 119: return 1;
! 120: }
! 121:
1.1.2.2 misho 122: if (GetOnly) {
123: printf("%s\n", szIface);
124:
1.1.2.6 ! misho 125: close(s);
1.1.2.2 misho 126: closelog();
127: return 0;
128: }
129:
1.1.2.5 misho 130: s = kldLoad();
131: if (s == -1) {
1.1.2.6 ! misho 132: syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
! 133: close(s);
1.1.2.5 misho 134: return 1;
135: } else
1.1.2.6 ! misho 136: VERB(1) 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.4 misho 146: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
1.1.2.3 misho 147: syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n",
148: MGMT_IFACE, errno, strerror(errno));
1.1.2.2 misho 149: close(s);
150: return 2;
1.1.2.3 misho 151: } else
152: VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
153: // rename iface
154: ifr.ifr_data = MGMT_NAME;
1.1.2.4 misho 155: if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
1.1.2.3 misho 156: syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n",
157: MGMT_NAME, errno, strerror(errno));
158: close(s);
159: return 2;
160: } else
161: VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.1.2.4 misho 162:
1.1.2.3 misho 163: // assign address & up
1.1.2.4 misho 164: memset(&ifra, 0, sizeof ifra);
165: strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
166: sin = (struct sockaddr_in*) &ifra.ifra_addr;
167: sin->sin_len = sizeof ifra.ifra_addr;
168: sin->sin_family = AF_INET;
169: sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
170: sin = (struct sockaddr_in*) &ifra.ifra_mask;
171: sin->sin_len = sizeof ifra.ifra_mask;
172: sin->sin_family = AF_INET;
173: sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
174: if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
175: syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n",
176: MGMT_ADDR, errno, strerror(errno));
177: close(s);
178: return 2;
179: } else
180: VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
1.1.2.2 misho 181:
182: close(s);
183: closelog();
1.1.2.1 misho 184: return 0;
185: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>