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
26 gint jdevice_DevicesCount
;
27 jbase_device
*jdevice_Devices
;
29 gboolean
jdevice_LookupDevices() {
30 #if HAVE_PCAP_FINDALLDEVS
33 if (pcap_findalldevs(&head
, pcap_errbuf
) != 0) {
34 fprintf(stderr
, "pcap_findalldevs: %s\n", pcap_errbuf
);
37 jdevice_DevicesCount
= 0;
40 jdevice_DevicesCount
++;
43 jdevice_Devices
= g_new0(jbase_device
, jdevice_DevicesCount
);
47 jdevice_Devices
[i
++].name
= g_strndup((const gchar
*)t
->name
, strlen(t
->name
));
50 pcap_freealldevs(head
);
53 jdevice_DevicesCount
= 1;
54 jdevice_Devices
= g_new(jbase_device
, 1);
55 name
= pcap_lookupdev(pcap_errbuf
);
57 fprintf(stderr
, "pcap_lookupdev: %s\n", pcap_errbuf
);
60 jdevice_Devices
[0].name
= g_strndup((const gchar
*)name
, strlen(name
));
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() {
76 memset(&ifr
, 0, sizeof(struct ifreq
));
77 s
= socket(PF_INET
, SOCK_DGRAM
, 0);
79 fprintf(stderr
, "Could not open datagram socket used to discover HW addresses of interfaces: %s\n", strerror(errno
));
82 for (i
=0; i
<jdevice_DevicesCount
; i
++) {
83 strncpy(ifr
.ifr_name
, jdevice_Devices
[i
].name
, IFNAMSIZ
);
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
));
89 if (ioctl(s
, SIOCGIFADDR
, &ifr
) >= 0) {
90 memcpy(&jdevice_Devices
[i
].hwaddr
, &ifr
.ifr_addr
, sizeof(struct sockaddr
));
93 fprintf(stderr
, "Could not get HW address of interface %s: %s\n", jdevice_Devices
[i
].name
, strerror(errno
));