1: #include "global.h"
2: #include "get1steth.h"
3:
4:
5: char szIface[MAX_STR];
6: int Verbose;
7: extern char compiled[], compiledby[], compilehost[];
8:
9:
10: static void Usage()
11: {
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:
20: // -------------------------------
21:
22: int main(int argc, char **argv)
23: {
24: char ch, GetOnly = 0;
25: struct ifaddrs *ifa, *ifp;
26: struct sockaddr_dl *sdl;
27: struct sockaddr_in *sin;
28: struct ifreq ifr;
29: struct vlanreq vlr;
30: struct ifaliasreq ifra;
31: int s;
32:
33: while ((ch = getopt(argc, argv, "hvg")) != -1)
34: switch (ch) {
35: case 'g':
36: GetOnly = 1;
37: break;
38: case 'v':
39: Verbose++;
40: break;
41: case 'h':
42: default:
43: Usage();
44: return 1;
45: }
46: argc -= optind;
47: argv += optind;
48:
49: openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_CONSOLE | LOG_USER);
50:
51: if (argc) {
52: strlcpy(szIface, *argv, MAX_STR);
53: VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
54: } else {
55: getifaddrs(&ifa);
56: for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
57: if (PF_LINK == ifp->ifa_addr->sa_family &&
58: IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
59: strlcpy(szIface, ifp->ifa_name, MAX_STR);
60: sdl = (struct sockaddr_dl*) ifp->ifa_addr;
61: VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface,
62: ether_ntoa((struct ether_addr*) LLADDR(sdl)));
63: break;
64: }
65: }
66: freeifaddrs(ifa);
67: }
68: if (GetOnly) {
69: printf("%s\n", szIface);
70:
71: closelog();
72: return 0;
73: }
74:
75: // create vlan
76: memset(&ifr, 0, sizeof ifr);
77: strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
78: vlr.vlr_tag = MGMT_VTAG;
79:
80: strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
81: ifr.ifr_data = (void *) &vlr;
82:
83: s = socket(PF_INET, SOCK_STREAM, 0);
84: if (-1 == s) {
85: syslog(LOG_ERR, "Error:: socket(LOCAL) #%d - %s\n", errno, strerror(errno));
86: return 2;
87: }
88: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
89: syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n",
90: MGMT_IFACE, errno, strerror(errno));
91: close(s);
92: return 2;
93: } else
94: VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
95: // rename iface
96: ifr.ifr_data = MGMT_NAME;
97: if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
98: syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n",
99: MGMT_NAME, errno, strerror(errno));
100: close(s);
101: return 2;
102: } else
103: VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
104:
105: // assign address & up
106: memset(&ifra, 0, sizeof ifra);
107: strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
108: sin = (struct sockaddr_in*) &ifra.ifra_addr;
109: sin->sin_len = sizeof ifra.ifra_addr;
110: sin->sin_family = AF_INET;
111: sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
112: sin = (struct sockaddr_in*) &ifra.ifra_mask;
113: sin->sin_len = sizeof ifra.ifra_mask;
114: sin->sin_family = AF_INET;
115: sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
116: if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
117: syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n",
118: MGMT_ADDR, errno, strerror(errno));
119: close(s);
120: return 2;
121: } else
122: VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
123:
124: close(s);
125: closelog();
126: return 0;
127: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>