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

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;
1.1.2.3 ! misho      28:        struct vlanreq vlr;
1.1.2.2   misho      29:        int s;
                     30: 
                     31:        while ((ch = getopt(argc, argv, "hvg")) != -1)
                     32:                switch (ch) {
                     33:                        case 'g':
                     34:                                GetOnly = 1;
                     35:                                break;
                     36:                        case 'v':
                     37:                                Verbose++;
                     38:                                break;
                     39:                        case 'h':
                     40:                        default:
                     41:                                Usage();
                     42:                                return 1;
                     43:                }
                     44:        argc -= optind;
                     45:        argv += optind;
                     46: 
1.1.2.3 ! misho      47:        openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_CONSOLE | LOG_USER);
1.1.2.2   misho      48: 
                     49:        if (argc) {
                     50:                strlcpy(szIface, *argv, MAX_STR);
                     51:                VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface);
                     52:        } else {
                     53:                getifaddrs(&ifa);
                     54:                for (ifp = ifa; ifp; ifp = ifp->ifa_next) {
                     55:                        if (PF_LINK == ifp->ifa_addr->sa_family && 
                     56:                                        IFT_ETHER == ((struct sockaddr_dl*) ifp->ifa_addr)->sdl_type) {
                     57:                                strlcpy(szIface, ifp->ifa_name, MAX_STR);
                     58:                                sdl = (struct sockaddr_dl*) ifp->ifa_addr;
                     59:                                VERB(2) syslog(LOG_NOTICE, "Info:: Get first interface=%s MAC=%s\n", szIface, 
                     60:                                                ether_ntoa((struct ether_addr*) LLADDR(sdl)));
                     61:                                break;
                     62:                        }
                     63:                }
                     64:                freeifaddrs(ifa);
                     65:        }
                     66:        if (GetOnly) {
                     67:                printf("%s\n", szIface);
                     68: 
                     69:                closelog();
                     70:                return 0;
                     71:        }
                     72: 
1.1.2.3 ! misho      73:        // create vlan
1.1.2.2   misho      74:        memset(&ifr, 0, sizeof ifr);
1.1.2.3 ! misho      75:        strlcpy(vlr.vlr_parent, szIface, IFNAMSIZ);
        !            76:        vlr.vlr_tag = MGMT_VTAG;
        !            77: 
        !            78:        strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ);
        !            79:        ifr.ifr_data = (void *) &vlr;
1.1.2.2   misho      80: 
                     81:        s = socket(PF_LOCAL, SOCK_DGRAM, 0);
                     82:        if (-1 == s) {
                     83:                syslog(LOG_ERR, "Error:: socket(LOCAL) #%d - %s\n", errno, strerror(errno));
                     84:                return 2;
                     85:        }
                     86:        if (ioctl(s, SIOCIFCREATE2, &ifr) == -1) {
1.1.2.3 ! misho      87:                syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n", 
        !            88:                                MGMT_IFACE, errno, strerror(errno));
1.1.2.2   misho      89:                close(s);
                     90:                return 2;
1.1.2.3 ! misho      91:        } else
        !            92:                VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE);
        !            93:        // rename iface
        !            94:        ifr.ifr_data = MGMT_NAME;
        !            95:        if (ioctl(s, SIOCSIFNAME, &ifr) == -1) {
        !            96:                syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n", 
        !            97:                                MGMT_NAME, errno, strerror(errno));
        !            98:                close(s);
        !            99:                return 2;
        !           100:        } else
        !           101:                VERB(2) syslog(LOG_NOTICE, "Info:: Managment interface=%s\n", MGMT_NAME);
        !           102:        // assign address & up
        !           103:        ifr.ifr_flags |= IFF_UP;
1.1.2.2   misho     104: 
                    105:        close(s);
                    106:        closelog();
1.1.2.1   misho     107:        return 0;
                    108: }

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