Git-Url:
http://git.frugalware.org/gitweb/gitweb.cgi?p=frugalwareutils.git;a=commitdiff;h=c9c275b5b8c5fc5c39a4b7e8570114b4cc95b3bf
commit c9c275b5b8c5fc5c39a4b7e8570114b4cc95b3bf
Author: Miklos Vajna <[EMAIL PROTECTED]>
Date: Tue Mar 25 15:23:53 2008 +0100
libfwnetconfig: new fwnet_iflist() function
- closes #2874
diff --git a/libfwnetconfig/Makefile b/libfwnetconfig/Makefile
index 9776b8a..a420a9c 100644
--- a/libfwnetconfig/Makefile
+++ b/libfwnetconfig/Makefile
@@ -22,11 +22,12 @@ CFLAGS += $(shell pkg-config --cflags glib-2.0)
LDFLAGS += $(shell pkg-config --libs glib-2.0)
# fwutil
-CFLAGS += -I../libfwutil
-LDFLAGS += -L../libfwutil -lfwutil
+CFLAGS += -I../libfwutil -I/usr/include/pci
+LDFLAGS += -L../libfwutil -lfwutil -lpci -lz
#SUBDIRS = po
LIBNAME = netconfig
+LIBOBJS = pci.o
-include ../Makefile.inc
-include ../Makefile.lib.inc
diff --git a/libfwnetconfig/libfwnetconfig.c b/libfwnetconfig/libfwnetconfig.c
index 1724b23..71bb268 100644
--- a/libfwnetconfig/libfwnetconfig.c
+++ b/libfwnetconfig/libfwnetconfig.c
@@ -218,6 +218,32 @@ int fwnet_is_dhcp(fwnet_interface_t *iface)
return(dhcp);
}
+/** Returns a list of interfaces available with descriptions.
+ * @return NULL on failure, GList of fwnet_interface_desc_t on success
+ */
+GList *fwnet_iflist()
+{
+ GList *ret = NULL;
+ char desc[128];
+ DIR *dir;
+ struct dirent *ent;
+
+ // /sys/class/net/
+ dir = opendir(FWNET_IFPATH);
+ if(!dir)
+ return NULL;
+ while((ent = readdir(dir)))
+ {
+ if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..") ||
!strcmp(ent->d_name, "lo"))
+ continue;
+ fwnet_ifdesc(ent->d_name, desc, sizeof(desc));
+ ret = g_list_append(ret, g_strdup(ent->d_name));
+ ret = g_list_append(ret, g_strdup(desc));
+ }
+ closedir(dir);
+ return ret;
+}
+
/** Shut down an interface
* @param iface the interface struct pointer
* @return 1 on failure, 0 on success
diff --git a/libfwnetconfig/libfwnetconfig.h b/libfwnetconfig/libfwnetconfig.h
index 97b4534..48b4bb0 100644
--- a/libfwnetconfig/libfwnetconfig.h
+++ b/libfwnetconfig/libfwnetconfig.h
@@ -34,6 +34,7 @@
#define FWNET_VERSIONFILE "/etc/frugalware-release"
#define FWNET_PATH "/etc/sysconfig/network"
#define FWNET_LOCK "/var/run/netconfig"
+#define FWNET_IFPATH "/sys/class/net"
typedef struct __fwnet_interface_t {
char name[IF_NAMESIZE+1];
@@ -67,6 +68,7 @@ typedef struct __fwnet_profile_t {
int fwnet_listprofiles(void);
fwnet_profile_t *fwnet_parseprofile(char *fn);
int fwnet_is_dhcp(fwnet_interface_t *iface);
+GList *fwnet_iflist();
int fwnet_ifdown(fwnet_interface_t *iface, fwnet_profile_t *profile);
int fwnet_ifup(fwnet_interface_t *iface, fwnet_profile_t *profile);
int fwnet_setdns(fwnet_profile_t* profile);
diff --git a/libfwnetconfig/pci.c b/libfwnetconfig/pci.c
new file mode 100644
index 0000000..f4a4fc5
--- /dev/null
+++ b/libfwnetconfig/pci.c
@@ -0,0 +1,77 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <limits.h>
+#include <fcntl.h>
+#include <pci.h>
+#include <linux/sockios.h>
+
+/* hack, so we may include kernel's ethtool.h */
+typedef unsigned long long __u64;
+typedef __uint32_t __u32; /* ditto */
+typedef __uint16_t __u16; /* ditto */
+typedef __uint8_t __u8; /* ditto */
+
+#include <linux/ethtool.h>
+
+int fwnet_ifdesc(const char *iface, char *desc, int size)
+{
+ struct ifreq ifr;
+ int fd, err, len, device, vendor;
+ struct ethtool_drvinfo drvinfo;
+ char buf[512], path[PATH_MAX];
+ struct pci_access *pacc;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strcpy(ifr.ifr_name, iface);
+
+ fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd < 0)
+ {
+ perror("Cannot get control socket");
+ return 1;
+ }
+ drvinfo.cmd = ETHTOOL_GDRVINFO;
+ ifr.ifr_data = (caddr_t)&drvinfo;
+ err = ioctl(fd, SIOCETHTOOL, &ifr);
+ if (err < 0)
+ {
+ perror("Cannot get driver information");
+ printf("%d\n", errno);
+ return 2;
+ }
+ close(fd);
+ snprintf(path, PATH_MAX-1, "/sys/bus/pci/devices/%s/vendor",
drvinfo.bus_info);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ {
+ perror("Cannot open the vendor file");
+ return 3;
+ }
+ len = read(fd, buf, sizeof(buf));
+ buf[len-1] = '\0';
+ close(fd);
+ sscanf(buf,"%X", &vendor);
+ snprintf(path, PATH_MAX, "/sys/bus/pci/devices/%s/device",
drvinfo.bus_info);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ {
+ perror("Cannot open the device file");
+ return 3;
+ }
+ len = read(fd, buf, sizeof(buf));
+ buf[len-1] = '\0';
+ close(fd);
+ sscanf(buf,"%X", &device);
+ pacc = pci_alloc();
+ pci_init(pacc);
+ pci_lookup_name(pacc, desc, size,
+ PCI_LOOKUP_VENDOR | PCI_LOOKUP_DEVICE,
+ vendor, device);
+ pci_cleanup(pacc);
+ return(0);
+}
_______________________________________________
Frugalware-git mailing list
[email protected]
http://frugalware.org/mailman/listinfo/frugalware-git