--- embedtools/src/get1steth.c 2010/01/01 17:24:04 1.1.2.6 +++ embedtools/src/get1steth.c 2011/06/08 09:33:29 1.1.2.11 @@ -1,3 +1,11 @@ +/************************************************************************* + * (C) 2010 AITNET - Sofia/Bulgaria - + * by Michael Pounov + * + * $Author: misho $ + * $Id: get1steth.c,v 1.1.2.11 2011/06/08 09:33:29 misho Exp $ + * + *************************************************************************/ #include "global.h" #include "get1steth.h" @@ -17,6 +25,7 @@ static void Usage() "\n", compiled, compiledby, compilehost); } +#ifdef HAVE_KLDNEXT static int kldLoad() { struct module_stat mstat; @@ -43,6 +52,7 @@ static int kldLoad() return 1; } +#endif // ------------------------------- @@ -74,18 +84,19 @@ int main(int argc, char **argv) argc -= optind; argv += optind; - openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_CONSOLE | LOG_USER); + openlog("get1steth", LOG_CONS | LOG_PERROR, LOG_USER); - s = socket(PF_INET, SOCK_STREAM, 0); - if (-1 == s) { - syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno)); - closelog(); - return 1; - } if (argc) { strlcpy(szIface, *argv, MAX_STR); VERB(1) syslog(LOG_NOTICE, "Info:: Get CUSTOM first interface %s\n", szIface); } else { + s = socket(PF_INET, SOCK_DGRAM, 0); + if (-1 == s) { + syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno)); + closelog(); + return 1; + } + getifaddrs(&ifa); for (ifp = ifa; ifp; ifp = ifp->ifa_next) { if (PF_LINK == ifp->ifa_addr->sa_family && @@ -110,11 +121,12 @@ int main(int argc, char **argv) } } freeifaddrs(ifa); + + close(s); } if (!*szIface) { syslog(LOG_NOTICE, "Info:: Ethernet interface not found!!!\n"); - close(s); closelog(); return 1; } @@ -122,18 +134,18 @@ int main(int argc, char **argv) if (GetOnly) { printf("%s\n", szIface); - close(s); closelog(); return 0; } +#ifdef HAVE_KLDNEXT s = kldLoad(); if (s == -1) { syslog(LOG_ERR, "Error:: kldload(if_vlan) Can`t operate with vlans ...\n"); - close(s); return 1; } else - VERB(1) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded"); + VERB(3) syslog(LOG_NOTICE, "VLAN module ... %s\n", s ? "Loaded" : "Already loaded"); +#endif // create vlan memset(&ifr, 0, sizeof ifr); @@ -143,7 +155,17 @@ int main(int argc, char **argv) strlcpy(ifr.ifr_name, MGMT_IFACE, IFNAMSIZ); ifr.ifr_data = (void *) &vlr; + s = socket(PF_INET, SOCK_DGRAM, 0); + if (-1 == s) { + syslog(LOG_ERR, "Error:: socket(INET) #%d - %s\n", errno, strerror(errno)); + closelog(); + return 2; + } +#ifdef SIOCIFCREATE2 if (ioctl(s, SIOCIFCREATE2, &ifr) == -1 && errno != EEXIST) { +#else + if (ioctl(s, SIOCIFCREATE, &ifr) == -1 && errno != EEXIST) { +#endif syslog(LOG_ERR, "Error:: Create interface=%s ioctl(SIOCIFCREATE2) #%d - %s\n", MGMT_IFACE, errno, strerror(errno)); close(s); @@ -152,7 +174,11 @@ int main(int argc, char **argv) VERB(2) syslog(LOG_NOTICE, "Info:: Created interface=%s\n", MGMT_IFACE); // rename iface ifr.ifr_data = MGMT_NAME; +#ifdef SIOCSIFNAME if (errno != EEXIST && ioctl(s, SIOCSIFNAME, &ifr) == -1) { +#else + if (errno != EEXIST && ioctl(s, SIOCGIFDESCR, &ifr) == -1) { +#endif syslog(LOG_ERR, "Error:: Managment interface=%s ioctl(SIOCSIFNAME) #%d - %s\n", MGMT_NAME, errno, strerror(errno)); close(s);