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

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

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