setting svn:exports
[jnettop.git] / jnettop / jdevice.c
blobfcfe668d55c1f525ad3138b5d557a7a4c3be615e
1 /*
2 * jnettop, network online traffic visualiser
3 * Copyright (C) 2002-2005 Jakub Skopal
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * $Header$
23 #include "jbase.h"
24 #include "jdevice.h"
26 gint jdevice_DevicesCount;
27 jbase_device *jdevice_Devices;
29 gboolean jdevice_LookupDevices() {
30 #if HAVE_PCAP_FINDALLDEVS
31 pcap_if_t *head, *t;
32 int i;
33 if (pcap_findalldevs(&head, pcap_errbuf) != 0) {
34 fprintf(stderr, "pcap_findalldevs: %s\n", pcap_errbuf);
35 return FALSE;
37 jdevice_DevicesCount = 0;
38 t = head;
39 while (t) {
40 jdevice_DevicesCount ++;
41 t = t->next;
43 jdevice_Devices = g_new0(jbase_device, jdevice_DevicesCount);
44 t = head;
45 i = 0;
46 while (t) {
47 jdevice_Devices[i++].name = g_strndup((const gchar*)t->name, strlen(t->name));
48 t = t->next;
50 pcap_freealldevs(head);
51 #else
52 char *name;
53 jdevice_DevicesCount = 1;
54 jdevice_Devices = g_new(jbase_device, 1);
55 name = pcap_lookupdev(pcap_errbuf);
56 if (!name) {
57 fprintf(stderr, "pcap_lookupdev: %s\n", pcap_errbuf);
58 return FALSE;
60 jdevice_Devices[0].name = g_strndup((const gchar*)name, strlen(name));
61 #endif
62 return TRUE;
65 jbase_device * jdevice_CreateSingleDevice(const gchar *deviceName) {
66 jdevice_DevicesCount = 1;
67 jdevice_Devices = g_new(jbase_device, 1);
68 jdevice_Devices[0].name = g_strndup(deviceName, strlen(deviceName));
69 return jdevice_Devices;
72 gboolean jdevice_CheckDevices() {
73 struct ifreq ifr;
74 int s,i;
76 memset(&ifr, 0, sizeof(struct ifreq));
77 s = socket(PF_INET, SOCK_DGRAM, 0);
78 if (s==-1) {
79 fprintf(stderr, "Could not open datagram socket used to discover HW addresses of interfaces: %s\n", strerror(errno));
80 return FALSE;
82 for (i=0; i<jdevice_DevicesCount; i++) {
83 strncpy(ifr.ifr_name, jdevice_Devices[i].name, IFNAMSIZ);
84 #ifdef SIOCGIFHWADDR
85 ifr.ifr_hwaddr.sa_family = AF_UNSPEC;
86 if (ioctl(s, SIOCGIFHWADDR, &ifr) >= 0) {
87 memcpy(&jdevice_Devices[i].hwaddr, &ifr.ifr_hwaddr, sizeof(struct sockaddr));
88 #else
89 if (ioctl(s, SIOCGIFADDR, &ifr) >= 0) {
90 memcpy(&jdevice_Devices[i].hwaddr, &ifr.ifr_addr, sizeof(struct sockaddr));
91 #endif
92 } else {
93 fprintf(stderr, "Could not get HW address of interface %s: %s\n", jdevice_Devices[i].name, strerror(errno));
96 close(s);
97 return TRUE;