Annotation of embedtools/src/get1steth.c, revision 1.1.2.5
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.2 misho 58: int s;
59:
60: while ((ch = getopt(argc, argv, "hvg")) != -1)
61: switch (ch) {
62: case 'g':
63: GetOnly = 1;
64: break;
65: case 'v':
66: Verbose++;
67: break;
68: case 'h':
69: default:
70: Usage();
71: return 1;
72: }
73: argc -= optind;
74: argv += optind;
75:
1.1.2.3 misho 76: openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_CONSOLE | LOG_USER);
1.1.2.2 misho 77:
78: if (argc) {
79: strlcpy(szIface, *argv, MAX_STR);
80: VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
81: } else {
82: getifaddrs(&ifa);
83: for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
84: if (PF_LINK == ifp->ifa_addr->sa_family &&
85: IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
86: strlcpy(szIface, ifp->ifa_name, MAX_STR);
87: sdl = (struct sockaddr_dl*) ifp->ifa_addr;
88: VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface,
89: ether_ntoa((struct ether_addr*) LLADDR(sdl)));
90: break;
91: }
92: }
93: freeifaddrs(ifa);
94: }
95: if (GetOnly) {
96: printf("%s\n", szIface);
97:
98: closelog();
99: return 0;
100: }
101:
1.1.2.5 ! misho 102: s = kldLoad();
! 103: if (s == -1) {
! 104: printf("Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
! 105: return 1;
! 106: } else
! 107: VERB(1) printf("VLAN module ... %s\n", s ? "Loaded" : "Already loaded");
! 108:
1.1.2.3 misho 109: // create vlan
1.1.2.2 misho 110: memset(&ifr, 0, sizeof ifr);
1.1.2.3 misho 111: strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
112: vlr.vlr_tag = MGMT_VTAG;
113:
114: strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
115: ifr.ifr_data = (void *) &vlr;
1.1.2.2 misho 116:
1.1.2.4 misho 117: s = socket(PF_INET, SOCK_STREAM, 0);
1.1.2.2 misho 118: if (-1 == s) {
119: syslog(LOG_ERR, "Error:: socket(LOCAL) #%d - %s\n", errno, strerror(errno));
120: return 2;
121: }
1.1.2.4 misho 122: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
1.1.2.3 misho 123: syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n",
124: MGMT_IFACE, errno, strerror(errno));
1.1.2.2 misho 125: close(s);
126: return 2;
1.1.2.3 misho 127: } else
128: VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
129: // rename iface
130: ifr.ifr_data = MGMT_NAME;
1.1.2.4 misho 131: if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
1.1.2.3 misho 132: syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n",
133: MGMT_NAME, errno, strerror(errno));
134: close(s);
135: return 2;
136: } else
137: VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.1.2.4 misho 138:
1.1.2.3 misho 139: // assign address & up
1.1.2.4 misho 140: memset(&ifra, 0, sizeof ifra);
141: strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
142: sin = (struct sockaddr_in*) &ifra.ifra_addr;
143: sin->sin_len = sizeof ifra.ifra_addr;
144: sin->sin_family = AF_INET;
145: sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
146: sin = (struct sockaddr_in*) &ifra.ifra_mask;
147: sin->sin_len = sizeof ifra.ifra_mask;
148: sin->sin_family = AF_INET;
149: sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
150: if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
151: syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n",
152: MGMT_ADDR, errno, strerror(errno));
153: close(s);
154: return 2;
155: } else
156: VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
1.1.2.2 misho 157:
158: close(s);
159: closelog();
1.1.2.1 misho 160: return 0;
161: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>