Annotation of embedtools/src/get1steth.c, revision 1.1.2.10

1.1.2.9   misho       1: /*************************************************************************
                      2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.1.2.10! misho       6:  * $Id: get1steth.c,v 1.1.2.9 2010/03/24 16:43:01 misho Exp $
1.1.2.9   misho       7:  *
                      8:  *************************************************************************/
1.1.2.1   misho       9: #include "global.h"
1.1.2.2   misho      10: #include "get1steth.h"
1.1.2.1   misho      11: 
                     12: 
1.1.2.2   misho      13: char szIface[MAX_STR];
                     14: int Verbose;
                     15: extern char compiled[], compiledby[], compilehost[];
                     16: 
                     17: 
                     18: static void Usage()
1.1.2.1   misho      19: {
1.1.2.2   misho      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: 
1.1.2.10! misho      28: #ifdef HAVE_KLDNEXT
1.1.2.5   misho      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: }
1.1.2.10! misho      55: #endif
1.1.2.5   misho      56: 
1.1.2.2   misho      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;
1.1.2.4   misho      64:        struct sockaddr_in *sin;
1.1.2.2   misho      65:        struct ifreq ifr;
1.1.2.3   misho      66:        struct vlanreq vlr;
1.1.2.4   misho      67:        struct ifaliasreq ifra;
1.1.2.6   misho      68:        struct ifmediareq ifmr;
1.1.2.2   misho      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: 
1.1.2.10! misho      87:        openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_USER);
1.1.2.2   misho      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 {
1.1.2.7   misho      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: 
1.1.2.2   misho     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) {
1.1.2.6   misho     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:                                }
1.1.2.2   misho     121:                        }
                    122:                }
                    123:                freeifaddrs(ifa);
1.1.2.7   misho     124: 
                    125:                close(s);
1.1.2.2   misho     126:        }
1.1.2.6   misho     127:        if (!*szIface) {
                    128:                syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n");
                    129: 
                    130:                closelog();
                    131:                return 1;
                    132:        }
                    133: 
1.1.2.2   misho     134:        if (GetOnly) {
                    135:                printf("%s\n", szIface);
                    136: 
                    137:                closelog();
                    138:                return 0;
                    139:        }
                    140: 
1.1.2.10! misho     141: #ifdef HAVE_KLDNEXT
1.1.2.5   misho     142:        s = kldLoad();
                    143:        if (s == -1) {
1.1.2.6   misho     144:                syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
1.1.2.5   misho     145:                return 1;
                    146:        } else
1.1.2.8   misho     147:                VERB(3) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded");
1.1.2.10! misho     148: #endif
1.1.2.5   misho     149: 
1.1.2.3   misho     150:        // create vlan
1.1.2.2   misho     151:        memset(&ifr, 0, sizeof ifr);
1.1.2.3   misho     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;
1.1.2.2   misho     157: 
1.1.2.7   misho     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:        }
1.1.2.4   misho     164:        if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
1.1.2.3   misho     165:                syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n", 
                    166:                                MGMT_IFACE, errno, strerror(errno));
1.1.2.2   misho     167:                close(s);
                    168:                return 2;
1.1.2.3   misho     169:        } else
                    170:                VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
                    171:        // rename iface
                    172:        ifr.ifr_data = MGMT_NAME;
1.1.2.4   misho     173:        if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
1.1.2.3   misho     174:                syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n", 
                    175:                                MGMT_NAME, errno, strerror(errno));
                    176:                close(s);
                    177:                return 2;
                    178:        } else
                    179:                VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.1.2.4   misho     180: 
1.1.2.3   misho     181:        // assign address & up
1.1.2.4   misho     182:        memset(&ifra, 0, sizeof ifra);
                    183:        strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
                    184:        sin = (struct sockaddr_in*) &ifra.ifra_addr;
                    185:        sin->sin_len = sizeof ifra.ifra_addr;
                    186:        sin->sin_family = AF_INET;
                    187:        sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
                    188:        sin = (struct sockaddr_in*) &ifra.ifra_mask;
                    189:        sin->sin_len = sizeof ifra.ifra_mask;
                    190:        sin->sin_family = AF_INET;
                    191:        sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
                    192:        if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
                    193:                syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n", 
                    194:                                MGMT_ADDR, errno, strerror(errno));
                    195:                close(s);
                    196:                return 2;
                    197:        } else
                    198:                VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
1.1.2.2   misho     199: 
                    200:        close(s);
                    201:        closelog();
1.1.2.1   misho     202:        return 0;
                    203: }

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