OSDN Git Service

nic vendor and model
authorwifiextender <router@archlinux.info>
Wed, 10 Aug 2016 15:00:20 +0000 (17:00 +0200)
committerwifiextender <router@archlinux.info>
Wed, 10 Aug 2016 15:00:20 +0000 (17:00 +0200)
bootstrap
m4/net.m4 [new file with mode: 0644]
m4/the_rest_funcs.m4
src/include/functions_constants.h
src/net.c
src/net.h
src/options.c

index 91d0db9..7bd6983 100644 (file)
--- a/bootstrap
+++ b/bootstrap
@@ -149,7 +149,8 @@ _gen_files() {
       ../m4/mpd.m4 \
       ../m4/the_rest_funcs.m4 \
       ../m4/typez.m4 \
-      ../m4/x11.m4
+      ../m4/x11.m4 \
+      ../m4/net.m4
   ')
 
 
diff --git a/m4/net.m4 b/m4/net.m4
new file mode 100644 (file)
index 0000000..845ba25
--- /dev/null
+++ b/m4/net.m4
@@ -0,0 +1,123 @@
+dnl Copyright 08/10/2015
+dnl Aaron Caffrey https://github.com/wifiextender
+
+dnl This program is free software; you can redistribute it and/or modify
+dnl it under the terms of the GNU General Public License as published by
+dnl the Free Software Foundation; either version 2 of the License, or
+dnl (at your option) any later version.
+
+dnl This program is distributed in the hope that it will be useful,
+dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
+dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+dnl GNU General Public License for more details.
+
+dnl You should have received a copy of the GNU General Public License
+dnl along with this program; if not, write to the Free Software
+dnl Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+dnl MA 02110-1301, USA.
+
+
+dnl TEST_NET() function in configure.ac
+dnl
+dnl Allow the user to compile the program
+dnl without net related functions, thus
+dnl decreasing the required dependencies.
+dnl
+dnl Did not included tests for some of the Net
+dnl headers as they fail to compile just by including
+dnl them in gcc, but pass the tests in clang. Is there
+dnl are any _POSIX_SOURCE m4 alternative to compile a
+dnl test case on the fly ?
+AC_DEFUN([TEST_NET],[
+  WITH_NET=1
+  dnl WITH_PCI=1
+  dnl PCI_LIBS=""
+
+  AC_ARG_WITH([net],
+    AS_HELP_STRING([--with-net],
+      [Net funcs]),
+    [],
+    [with_net=yes]
+  )
+
+  AS_IF([test "x$with_net" = "xno"], [
+    WITH_NET=0
+  ])
+
+  AS_IF([test "x$with_net" = "xyes"], [
+
+    AC_CHECK_HEADERS([    \
+      linux/if_link.h     \
+      ifaddrs.h           \
+      arpa/inet.h         \
+      netpacket/packet.h  \
+      sys/socket.h        \
+      net/if.h            \
+      sys/ioctl.h         \
+      linux/sockios.h     \
+      linux/ethtool.h     \
+      netdb.h             \
+    ],[],[
+      ERR([Missing core header files.])
+    ])
+
+    AC_CHECK_FUNCS([ \
+      getifaddrs     \
+      freeifaddrs    \
+      getaddrinfo    \
+      freeaddrinfo   \
+      socket         \
+      ioctl          \
+      inet_ntop      \
+    ],[],[
+      ERR([Missing core library functions.])
+    ])
+
+    NOTIFY([getifaddrs])
+    AC_COMPILE_IFELSE([
+      AC_LANG_SOURCE([[
+        #include <ifaddrs.h>
+        int main(void) {
+          struct ifaddrs *ifaddr;
+          if (-1 == getifaddrs(&ifaddr)) {
+            return 0;
+          }
+          freeifaddrs(ifaddr);
+          return 0;
+        }
+      ]])
+    ],[],[
+      COMPILE_FAILED([getifaddrs])
+      ]
+    )
+
+  ])
+
+  dnl AC_ARG_WITH([pci],
+  dnl   AS_HELP_STRING([--with-pci],
+  dnl     [PCI funcs]),
+  dnl   [],
+  dnl   [with_pci=yes]
+  dnl )
+
+  dnl AS_IF([test "x$with_pci" = "xno"], [
+  dnl   WITH_PCI=0
+  dnl AC_SUBST(PCI_LIBS)
+  dnl ])
+
+  dnl AS_IF([test "x$with_pci" = "xyes"], [
+  dnl   PCI_LIBS="-lpci"
+
+  dnl   AC_CHECK_HEADERS([    \
+  dnl     pci/pci.h           \
+  dnl   ],[],[
+  dnl     ERR([Missing core header files.])
+  dnl   ])
+
+  dnl ])
+
+  dnl AC_SUBST(PCI_LIBS)
+  AC_DEFINE_UNQUOTED([WITH_NET],[$WITH_NET],[Net funcs])
+  dnl AC_DEFINE_UNQUOTED([WITH_PCI],[$WITH_PCI],[PCI funcs])
+
+])
index 4462793..d21d90f 100644 (file)
@@ -24,83 +24,6 @@ AC_DEFUN([NOTIFY],[
 ])
 
 
-dnl TEST_NET() function in configure.ac
-dnl
-dnl Allow the user to compile the program
-dnl without net related functions, thus
-dnl decreasing the required dependencies.
-dnl
-dnl Did not included tests for some of the Net
-dnl headers as they fail to compile just by including
-dnl them in gcc, but pass the tests in clang. Is there
-dnl are any _POSIX_SOURCE m4 alternative to compile a
-dnl test case on the fly ?
-AC_DEFUN([TEST_NET],[
-  WITH_NET=1
-  AC_ARG_WITH([net],
-    AS_HELP_STRING([--with-net],
-      [Net funcs]),
-    [],
-    [with_net=yes]
-  )
-
-  AS_IF([test "x$with_net" = "xno"], [
-    WITH_NET=0
-  ])
-
-  AS_IF([test "x$with_net" = "xyes"], [
-
-    AC_CHECK_HEADERS([    \
-      linux/if_link.h     \
-      ifaddrs.h           \
-      arpa/inet.h         \
-      netpacket/packet.h  \
-      sys/socket.h        \
-      net/if.h            \
-      sys/ioctl.h         \
-      linux/sockios.h     \
-      linux/ethtool.h     \
-      netdb.h             \
-    ],[],[
-      ERR([Missing core header files.])
-    ])
-
-    AC_CHECK_FUNCS([ \
-      getifaddrs     \
-      freeifaddrs    \
-      getaddrinfo    \
-      freeaddrinfo   \
-      socket         \
-      ioctl          \
-      inet_ntop      \
-    ],[],[
-      ERR([Missing core library functions.])
-    ])
-
-    NOTIFY([getifaddrs])
-    AC_COMPILE_IFELSE([
-      AC_LANG_SOURCE([[
-        #include <ifaddrs.h>
-        int main(void) {
-          struct ifaddrs *ifaddr;
-          if (-1 == getifaddrs(&ifaddr)) {
-            return 0;
-          }
-          freeifaddrs(ifaddr);
-          return 0;
-        }
-      ]])
-    ],[],[
-      COMPILE_FAILED([getifaddrs])
-      ]
-    )
-
-  ])
-  AC_DEFINE_UNQUOTED([WITH_NET],[$WITH_NET],[Net funcs])
-
-])
-
-
 dnl TEST_SOME_FUNCS() function in configure.ac
 dnl
 dnl The tests are simple enough, just to
index 20a51b9..fa8ebf6 100644 (file)
@@ -27,6 +27,7 @@
 #define BYTES_TO_MB(x) (x/2/1024) /* block bytes */
 
 #define FMT_UINT "%"PRIuMAX
+#define FMT_UINTX "%"PRIxMAX
 #define UFINT "%"PRIuFAST16
 
 /* stay away from va_list */
 #define BATTERY_USED(x, z)  (BATTERY_NUM(x, z, "now"))
 #define BATTERY_TOTAL(x, z) (BATTERY_NUM(x, z, "full"))
 
+/* NIC model and vendor */
+#define NIC_NUM(x, y, z) (FILL_ARR(x, "%s%s%s%s", \
+  "/sys/class/net/", y, "/device/", z))
+#define NIC_VEND(x, z)  (NIC_NUM(x, z, "vendor"))
+#define NIC_MODEL(x, z) (NIC_NUM(x, z, "device"))
+
 /* exit with error */
 #define CANNOT_OPEN "Could not open"
 #define ERR "Error:"
index 6b4aa25..84e72d4 100644 (file)
--- a/src/net.c
+++ b/src/net.c
 
 #if WITH_NET == 1
 
-#include <arpa/inet.h>
 #include <netdb.h>
 /* #include <sys/types.h> */
 #include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <arpa/inet.h>
 #include <ifaddrs.h>
 #include <linux/if_link.h>
 #include <netpacket/packet.h>
 #include <net/if.h>
-#include <sys/ioctl.h>
 #include <linux/sockios.h>
 #include <linux/ethtool.h>
 #include <linux/if.h>
 
+/* #if WITH_PCI == 1 */
+/* #include <pci/pci.h> */
+/* #endif /1* WITH_PCI *1/ */
+
 #endif /* WITH_NET */
 
 #include "include/headers.h"
@@ -56,6 +60,7 @@ get_net(char *str1, char *str2, unsigned char num) {
   if (-1 == getifaddrs(&ifaddr)) {
     FUNC_FAILED("getifaddrs()");
   }
+  FILL_STR_ARR(1, str1, "Null");
 
   for (ifa = ifaddr; NULL != ifa; ifa = ifa->ifa_next) {
     if (NULL == ifa->ifa_addr) {
@@ -109,7 +114,6 @@ get_net(char *str1, char *str2, unsigned char num) {
               20 == infiniband
             */
             if (6 != mac->sll_halen) {
-              FILL_STR_ARR(1, str1, "Null");
               break;
             }
             FILL_ARR(str1, "%02x:%02x:%02x:%02x:%02x:%02x",
@@ -119,9 +123,6 @@ get_net(char *str1, char *str2, unsigned char num) {
           } else if (7 == num) { /* link speed */
 
             get_link_speed(str1, str2);
-            if ('\0' == str1[0]) {
-              FILL_STR_ARR(1, str1, "Null");
-            }
           }
           break;
         }
@@ -214,3 +215,73 @@ get_ip_lookup(char *str1, char *str2) {
   RECOMPILE_WITH("net");
 #endif /* WITH_NET */
 }
+
+
+/* void */
+/* get_nic_info(char *str1, char *str2) { */
+/* #if WITH_PCI == 1 */
+
+/*   uintmax_t vendor = 0, model = 0; */
+/*   char temp[VLA]; */
+/*   struct pci_access *pacc = NULL; */
+/*   struct pci_dev *dev; */
+
+/*   NIC_VEND(temp, str2); */
+/*   FILE *fp = fopen(temp, "r"); */
+/*   if (NULL == fp) { */
+/*     exit_with_err(CANNOT_OPEN, temp); */
+/*   } */
+
+/* #pragma GCC diagnostic push */
+/* #pragma GCC diagnostic ignored "-Wunused-result" */
+/*   fscanf(fp, FMT_UINTX, &vendor); /1* hex *1/ */
+/* #pragma GCC diagnostic pop */
+/*   fclose(fp); */
+
+/*   NIC_MODEL(temp, str2); */
+/*   if (NULL == (fp = fopen(temp, "r"))) { */
+/*     exit_with_err(CANNOT_OPEN, temp); */
+/*   } */
+
+/* #pragma GCC diagnostic push */
+/* #pragma GCC diagnostic ignored "-Wunused-result" */
+/*   fscanf(fp, FMT_UINTX, &model); /1* hex *1/ */
+/* #pragma GCC diagnostic pop */
+/*   fclose(fp); */
+
+/*   pacc = pci_alloc(); */
+/*   if (NULL == pacc) { */
+/*     goto error; */
+/*   } */
+
+/*   pci_init(pacc); */
+/*   if (NULL == pacc) { */
+/*     goto error; */
+/*   } */
+/*   pci_scan_bus(pacc); */
+
+/*   for (dev = pacc->devices; NULL != dev; dev = dev->next) { */
+/*     pci_fill_info(dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS); */
+
+/*     if ((uintmax_t)vendor == (uintmax_t)dev->vendor_id && */
+/*         (uintmax_t)model == (uintmax_t)dev->device_id) { */
+
+/*       pci_lookup_name(pacc, temp, VLA, */
+/*         PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE, */
+/*         (uintmax_t)vendor, (uintmax_t)model); */
+
+/*       FILL_STR_ARR(1, str1, temp); */
+/*       break; */
+/*     } */
+/*   } */
+
+/* error: */
+/*   if (NULL != pacc) { */
+/*     pci_cleanup(pacc); */
+/*   } */
+/*   return; */
+
+/* #else */
+/*   RECOMPILE_WITH("pci"); */
+/* #endif /1* WITH_PCI *1/ */
+/* } */
index bef4541..604d886 100644 (file)
--- a/src/net.h
+++ b/src/net.h
@@ -26,5 +26,6 @@
 void get_net(char *, char *, unsigned char num);
 void get_link_speed(char *, char *);
 void get_ip_lookup(char *, char *);
+/* void get_nic_info(char *, char *); */
 
 #endif /* NET_H_ */
index 14fb53d..95a39df 100644 (file)
@@ -58,6 +58,7 @@ static const struct argp_option options[] = {
   { .name = "ipcast",       .key = 'D', .arg = "eth0", .doc = "The NIC broadcast address."                               },
   { .name = "iplink",       .key = 'e', .arg = "eth0", .doc = "The NIC link speed (useful for wireless/wifi)."           },
   { .name = "iplookup",     .key = 'E', .arg = "site", .doc = "Mini website IP lookup."                                  },
+  /* { .name = "nicinfo",      .key = 'G', .arg = "eth0", .doc = "The NIC vendor and model."                                }, */
   { .name = "statio",       .key = 'S', .arg = "sda",  .doc = "Read and written MBs to the drive so far."                },
   { .doc = NULL }
 };
@@ -247,6 +248,14 @@ parse_opt(int key, char *arg, struct argp_state *state) {
       }
       break;
 
+    /* case 'G': */
+    /*   { */
+    /*     char nic_info[VLA]; */
+    /*     get_nic_info(nic_info, arg); */
+    /*     GLUE(arguments->all, FMT_KERN, nic_info); */
+    /*   } */
+    /*   break; */
+
     case 'E':
       {
         char ip_lookup[VLA];