File:  [ELWIX - Embedded LightWeight unIX -] / embedtools / src / get1steth.c
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Wed Jun 8 12:45:41 2011 UTC (13 years ago) by misho
Branches: MAIN
CVS tags: tools1_1, TOOLS1_0, HEAD
new ver

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

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