|
|
484378 |
diff -up ./arpwatch.c.iselect ./arpwatch.c
|
|
|
484378 |
--- ./arpwatch.c.iselect 2012-10-15 16:01:24.701335291 +0200
|
|
|
484378 |
+++ ./arpwatch.c 2012-10-15 16:07:18.626322639 +0200
|
|
|
484378 |
@@ -162,50 +162,52 @@ void dropprivileges(const char* user)
|
|
|
484378 |
}
|
|
|
484378 |
|
|
|
484378 |
char *
|
|
|
484378 |
-get_first_dev(pcap_t **pd, int *linktype, char *errbuf)
|
|
|
484378 |
+try_dev(char *interface, pcap_t **pd, int *linktype, char *errbuf)
|
|
|
484378 |
{
|
|
|
484378 |
- static char interface[IF_NAMESIZE + 1];
|
|
|
484378 |
register int snaplen, timeout;
|
|
|
484378 |
- pcap_if_t *alldevs;
|
|
|
484378 |
- pcap_if_t *dev;
|
|
|
484378 |
- char *ret = NULL;
|
|
|
484378 |
|
|
|
484378 |
snaplen = max(sizeof(struct ether_header),
|
|
|
484378 |
sizeof(struct fddi_header)) + sizeof(struct ether_arp);
|
|
|
484378 |
timeout = 1000;
|
|
|
484378 |
|
|
|
484378 |
- if (pcap_findalldevs(&alldevs, errbuf) == -1) {
|
|
|
484378 |
- (void)fprintf(stderr, "%s: lookup_device: %s\n",
|
|
|
484378 |
- prog, errbuf);
|
|
|
484378 |
- exit(1);
|
|
|
484378 |
+ *pd = pcap_open_live(interface, snaplen, 1, timeout, errbuf);
|
|
|
484378 |
+ if (NULL == pd) {
|
|
|
484378 |
+ syslog(LOG_ERR, "pcap open %s: %s", interface, errbuf);
|
|
|
484378 |
+ return NULL;
|
|
|
484378 |
}
|
|
|
484378 |
+ *linktype = pcap_datalink(*pd);
|
|
|
484378 |
+ /* Must be ethernet or fddi */
|
|
|
484378 |
+ if (*linktype != DLT_EN10MB && *linktype != DLT_FDDI) {
|
|
|
484378 |
+ syslog(LOG_ERR, "(%s) Link layer type %d not ethernet or fddi",
|
|
|
484378 |
+ interface, *linktype);
|
|
|
484378 |
+ pcap_close(*pd);
|
|
|
484378 |
+ return NULL;
|
|
|
484378 |
+ }
|
|
|
484378 |
+ return interface;
|
|
|
484378 |
+}
|
|
|
484378 |
|
|
|
484378 |
- for (dev = alldevs; dev; dev = dev->next) {
|
|
|
484378 |
- strncpy(interface, dev->name, strlen(dev->name)+1);
|
|
|
484378 |
-
|
|
|
484378 |
- *pd = pcap_open_live(interface, snaplen, 1, timeout, errbuf);
|
|
|
484378 |
- if (*pd == NULL) {
|
|
|
484378 |
- syslog(LOG_ERR, "pcap open %s: %s, trying next...", interface, errbuf);
|
|
|
484378 |
- continue;
|
|
|
484378 |
- /* exit(1); */
|
|
|
484378 |
- }
|
|
|
484378 |
+char *
|
|
|
484378 |
+iterate_dev(char *arginterface, pcap_t **pd, int *linktype, char *errbuf)
|
|
|
484378 |
+{
|
|
|
484378 |
+ static char interface[IF_NAMESIZE + 1];
|
|
|
484378 |
+ pcap_if_t *alldevs;
|
|
|
484378 |
+ pcap_if_t *dev;
|
|
|
484378 |
|
|
|
484378 |
- *linktype = pcap_datalink(*pd);
|
|
|
484378 |
- /* Must be ethernet or fddi */
|
|
|
484378 |
- if (*linktype != DLT_EN10MB && *linktype != DLT_FDDI) {
|
|
|
484378 |
- syslog(LOG_ERR, "(%s) Link layer type %d not ethernet or fddi, trying next...",
|
|
|
484378 |
- interface, *linktype);
|
|
|
484378 |
- pcap_close(*pd);
|
|
|
484378 |
+ if (NULL != arginterface) {
|
|
|
484378 |
+ return try_dev(arginterface, pd, linktype, errbuf);
|
|
|
484378 |
+ } else {
|
|
|
484378 |
+ if (pcap_findalldevs(&alldevs, errbuf) == -1) {
|
|
|
484378 |
+ (void)fprintf(stderr, "%s: lookup_device: %s\n",
|
|
|
484378 |
+ prog, errbuf);
|
|
|
484378 |
+ exit(1);
|
|
|
484378 |
}
|
|
|
484378 |
- else {
|
|
|
484378 |
- /* First match, use it */
|
|
|
484378 |
- ret = interface;
|
|
|
484378 |
- break;
|
|
|
484378 |
+ for (dev = alldevs; dev && (arginterface == NULL); dev = dev->next) {
|
|
|
484378 |
+ strncpy(interface, dev->name, strlen(dev->name)+1);
|
|
|
484378 |
+ arginterface = try_dev(interface, pd, linktype, errbuf);
|
|
|
484378 |
}
|
|
|
484378 |
-
|
|
|
484378 |
+ pcap_freealldevs(alldevs);
|
|
|
484378 |
+ return arginterface;
|
|
|
484378 |
}
|
|
|
484378 |
- pcap_freealldevs(alldevs);
|
|
|
484378 |
- return (ret);
|
|
|
484378 |
}
|
|
|
484378 |
|
|
|
484378 |
int
|
|
|
484378 |
@@ -315,8 +317,8 @@ main(int argc, char **argv)
|
|
|
484378 |
} else {
|
|
|
484378 |
|
|
|
484378 |
/* Determine interface if not specified */
|
|
|
484378 |
- if (interface == NULL &&
|
|
|
484378 |
- (interface = get_first_dev(&pd, &linktype, errbuf)) == NULL) {
|
|
|
484378 |
+ interface = iterate_dev(interface, &pd, &linktype, errbuf);
|
|
|
484378 |
+ if (interface == NULL) {
|
|
|
484378 |
(void)fprintf(stderr, "%s: lookup_device: no suitable interface found\n",
|
|
|
484378 |
prog);
|
|
|
484378 |
exit(1);
|