Annotation of embedtools/src/get1steth.c, revision 1.1.2.2
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:
! 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 ifreq ifr;
! 28: int s;
! 29:
! 30: while ((ch = getopt(argc, argv, "hvg")) != -1)
! 31: switch (ch) {
! 32: case 'g':
! 33: GetOnly = 1;
! 34: break;
! 35: case 'v':
! 36: Verbose++;
! 37: break;
! 38: case 'h':
! 39: default:
! 40: Usage();
! 41: return 1;
! 42: }
! 43: argc -= optind;
! 44: argv += optind;
! 45:
! 46: openlog("get1steth", LOG_CONS, LOG_CONSOLE);
! 47:
! 48: if (argc) {
! 49: strlcpy(szIface, *argv, MAX_STR);
! 50: VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
! 51: } else {
! 52: getifaddrs(&ifa);
! 53: for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
! 54: if (PF_LINK == ifp->ifa_addr->sa_family &&
! 55: IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
! 56: strlcpy(szIface, ifp->ifa_name, MAX_STR);
! 57: sdl = (struct sockaddr_dl*) ifp->ifa_addr;
! 58: VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface,
! 59: ether_ntoa((struct ether_addr*) LLADDR(sdl)));
! 60: break;
! 61: }
! 62: }
! 63: freeifaddrs(ifa);
! 64: }
! 65: if (GetOnly) {
! 66: printf("%s\n", szIface);
! 67:
! 68: closelog();
! 69: return 0;
! 70: }
! 71:
! 72: memset(&ifr, 0, sizeof ifr);
! 73: strlcpy(ifr.ifr_name, szIface, sizeof ifr.ifr_name);
! 74:
! 75: s = socket(PF_LOCAL, SOCK_DGRAM, 0);
! 76: if (-1 == s) {
! 77: syslog(LOG_ERR, "Error:: socket(LOCAL) #%d - %s\n", errno, strerror(errno));
! 78: return 2;
! 79: }
! 80: if (ioctl(s, SIOCIFCREATE2, &ifr) == -1) {
! 81: syslog(LOG_ERR, "Error:: ioctl(SIOCIFCREATE2) #%d - %s\n", errno, strerror(errno));
! 82: close(s);
! 83: return 2;
! 84: }
! 85:
! 86: close(s);
! 87: closelog();
1.1.2.1 misho 88: return 0;
89: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>