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

1.2       misho       1: /*************************************************************************
                      2:  * (C) 2010 AITNET - Sofia/Bulgaria - <office@aitbg.com>
                      3:  *  by Michael Pounov <misho@aitbg.com>
                      4:  *
                      5:  * $Author: misho $
1.4     ! misho       6:  * $Id: get1steth.c,v 1.3.2.1 2012/07/22 22:54:53 misho Exp $
1.2       misho       7:  *
1.3       misho       8:  *************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
1.2       misho      46: #include "global.h"
                     47: #include "get1steth.h"
                     48: 
                     49: 
1.4     ! misho      50: char szIface[STRSIZ];
1.2       misho      51: int Verbose;
                     52: extern char compiled[], compiledby[], compilehost[];
                     53: 
                     54: 
                     55: static void Usage()
                     56: {
                     57:        printf("-= GET_FIRST_ETHERNET =- Get First Ethernet Interface tool\n"
                     58:                "=== %s === %s@%s ===\n\n"
                     59:                "Syntax: get1steth [option] [custom_first_interface]\n\n"
                     60:                "\t-v\t\tVerbose (more -v more verbosity)\n"
                     61:                "\t-g\t\tOnly get first interface, print and exit ...\n"
                     62:                "\n", compiled, compiledby, compilehost);
                     63: }
                     64: 
                     65: #ifdef HAVE_KLDNEXT
                     66: static int kldLoad()
                     67: {
                     68:        struct module_stat mstat;
                     69:        register int i, j;
                     70:        u_char flg = 0;
                     71: 
                     72:        memset(&mstat, 0, sizeof mstat);
                     73:        mstat.version = sizeof mstat;
                     74:        for (i = kldnext(0); i > 0; i = kldnext(i))
                     75:                for (j = kldfirstmod(i); j > 0; j = modfnext(j)) {
                     76:                        if (modstat(j, &mstat) == -1)
                     77:                                continue;
                     78: 
                     79:                        if (!strncmp(MODVLAN, mstat.name, sizeof MODVLAN)) {
                     80:                                flg = 1;
                     81:                                break;
                     82:                        }
                     83:                }
                     84:        if (flg)
                     85:                return 0;
                     86: 
                     87:        if (kldload(MODVLAN) == -1)
                     88:                return -1;
                     89: 
                     90:        return 1;
                     91: }
                     92: #endif
                     93: 
                     94: // -------------------------------
                     95: 
                     96: int main(int argc, char **argv)
                     97: {
                     98:        char ch, GetOnly = 0;
                     99:        struct ifaddrs *ifa, *ifp;
                    100:        struct sockaddr_dl *sdl;
                    101:        struct sockaddr_in *sin;
                    102:        struct ifreq ifr;
                    103:        struct vlanreq vlr;
                    104:        struct ifaliasreq ifra;
                    105:        struct ifmediareq ifmr;
                    106:        int s;
                    107: 
                    108:        while ((ch = getopt(argc, argv, "hvg")) != -1)
                    109:                switch (ch) {
                    110:                        case 'g':
                    111:                                GetOnly = 1;
                    112:                                break;
                    113:                        case 'v':
                    114:                                Verbose++;
                    115:                                break;
                    116:                        case 'h':
                    117:                        default:
                    118:                                Usage();
                    119:                                return 1;
                    120:                }
                    121:        argc -= optind;
                    122:        argv += optind;
                    123: 
                    124:        openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_USER);
                    125: 
                    126:        if (argc) {
1.4     ! misho     127:                strlcpy(szIface, *argv, sizeof szIface);
1.2       misho     128:                VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
                    129:        } else {
                    130:                s = socket(PF_INET, SOCK_DGRAM, 0);
                    131:                if (-1 == s) {
                    132:                        syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
                    133:                        closelog();
                    134:                        return 1;
                    135:                }
                    136: 
                    137:                getifaddrs(&ifa);
                    138:                for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
                    139:                        if (PF_LINK == ifp->ifa_addr->sa_family && 
                    140:                                        IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
                    141:                                memset(&ifmr, 0, sizeof ifmr);
                    142:                                strlcpy(ifmr.ifm_name, ifp->ifa_name, IFNAMSIZ);
                    143:                                if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
                    144:                                        syslog(LOG_ERR, "Error:: media interface=%s ioctl(SIOCGIFMEDIA) #%d - %s\n", 
                    145:                                                        ifmr.ifm_name, errno, strerror(errno));
                    146:                                        close(s);
                    147:                                        closelog();
                    148:                                        return 1;
                    149:                                }
                    150: 
                    151:                                if (IFM_ETHER == IFM_TYPE(ifmr.ifm_current)) {
1.4     ! misho     152:                                        strlcpy(szIface, ifp->ifa_name, sizeof szIface);
1.2       misho     153:                                        sdl = (struct sockaddr_dl*) ifp->ifa_addr;
                    154:                                        VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface, 
                    155:                                                        ether_ntoa((struct ether_addr*) LLADDR(sdl)));
                    156:                                        break;
                    157:                                }
                    158:                        }
                    159:                }
                    160:                freeifaddrs(ifa);
                    161: 
                    162:                close(s);
                    163:        }
                    164:        if (!*szIface) {
                    165:                syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n");
                    166: 
                    167:                closelog();
                    168:                return 1;
                    169:        }
                    170: 
                    171:        if (GetOnly) {
                    172:                printf("%s\n", szIface);
                    173: 
                    174:                closelog();
                    175:                return 0;
                    176:        }
                    177: 
                    178: #ifdef HAVE_KLDNEXT
                    179:        s = kldLoad();
                    180:        if (s == -1) {
                    181:                syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n");
                    182:                return 1;
                    183:        } else
                    184:                VERB(3) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded");
                    185: #endif
                    186: 
                    187:        // create vlan
                    188:        memset(&ifr, 0, sizeof ifr);
                    189:        strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
                    190:        vlr.vlr_tag = MGMT_VTAG;
                    191: 
                    192:        strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
                    193:        ifr.ifr_data = (void *) &vlr;
                    194: 
                    195:        s = socket(PF_INET, SOCK_DGRAM, 0);
                    196:        if (-1 == s) {
                    197:                syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno));
                    198:                closelog();
                    199:                return 2;
                    200:        }
                    201: #ifdef SIOCIFCREATE2
                    202:        if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) {
                    203: #else
                    204:        if (ioctl(s, SIOCIFCREATE, &ifr) == -1 && errno != EEXIST) {
                    205: #endif
                    206:                syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n", 
                    207:                                MGMT_IFACE, errno, strerror(errno));
                    208:                close(s);
                    209:                return 2;
1.3       misho     210:        }
                    211: 
                    212:        memset(&ifra, 0, sizeof ifra);
                    213: #if defined(__FreeBSD__)
1.2       misho     214:        // rename iface
1.3       misho     215:        VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
1.2       misho     216:        ifr.ifr_data = MGMT_NAME;
                    217:        if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) {
                    218:                syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n", 
                    219:                                MGMT_NAME, errno, strerror(errno));
                    220:                close(s);
                    221:                return 2;
                    222:        } else
                    223:                VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
1.3       misho     224:        strlcpy(ifra.ifra_name, MGMT_NAME, IFNAMSIZ);
                    225: #else
                    226:        strlcpy(ifra.ifra_name, MGMT_IFACE, IFNAMSIZ);
                    227: #endif
1.2       misho     228: 
                    229:        // assign address & up
                    230:        sin = (struct sockaddr_in*) &ifra.ifra_addr;
                    231:        sin->sin_len = sizeof ifra.ifra_addr;
                    232:        sin->sin_family = AF_INET;
                    233:        sin->sin_addr.s_addr = inet_addr(MGMT_ADDR);
                    234:        sin = (struct sockaddr_in*) &ifra.ifra_mask;
                    235:        sin->sin_len = sizeof ifra.ifra_mask;
                    236:        sin->sin_family = AF_INET;
                    237:        sin->sin_addr.s_addr = inet_addr(MGMT_MASK);
                    238:        if (ioctl(s, SIOCAIFADDR, &ifra) == -1) {
                    239:                syslog(LOG_ERR, "Error:: IP %s ioctl(SIOCAIFADDR) #%d - %s\n", 
                    240:                                MGMT_ADDR, errno, strerror(errno));
                    241:                close(s);
                    242:                return 2;
                    243:        } else
                    244:                VERB(2) syslog(LOG_NOTICE, "Info:: IP %s\n", MGMT_ADDR);
                    245: 
                    246:        close(s);
                    247:        closelog();
                    248:        return 0;
                    249: }

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