File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / get1steth.c
Revision 1.1.2.8: download - view: text, annotated - select for diffs - revision graph
Tue Mar 23 13:29:38 2010 UTC (14 years, 3 months ago) by misho
Branches: tools1_0
added forgoten changes

    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: 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: 
   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;
   54: 	struct sockaddr_in *sin;
   55: 	struct ifreq ifr;
   56: 	struct vlanreq vlr;
   57: 	struct ifaliasreq ifra;
   58: 	struct ifmediareq ifmr;
   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: 
   77: 	openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_CONSOLE | LOG_USER);
   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 {
   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: 
   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) {
   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: 				}
  111: 			}
  112: 		}
  113: 		freeifaddrs(ifa);
  114: 
  115: 		close(s);
  116: 	}
  117: 	if (!*szIface) {
  118: 		syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n");
  119: 
  120: 		closelog();
  121: 		return 1;
  122: 	}
  123: 
  124: 	if (GetOnly) {
  125: 		printf("%s\n", szIface);
  126: 
  127: 		closelog();
  128: 		return 0;
  129: 	}
  130: 
  131: 	s = kldLoad();
  132: 	if (s == -1) {
  133: 		syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
  134: 		return 1;
  135: 	} else
  136: 		VERB(3) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded");
  137: 
  138: 	// create vlan
  139: 	memset(&ifr, 0, sizeof ifr);
  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;
  145: 
  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: 	}
  152: 	if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
  153: 		syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n", 
  154: 				MGMT_IFACE, errno, strerror(errno));
  155: 		close(s);
  156: 		return 2;
  157: 	} else
  158: 		VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
  159: 	// rename iface
  160: 	ifr.ifr_data = MGMT_NAME;
  161: 	if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
  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);
  168: 
  169: 	// assign address & up
  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);
  187: 
  188: 	close(s);
  189: 	closelog();
  190: 	return 0;
  191: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>