Blame SOURCES/arpwatch-2.1a15-devlookup.patch

1b6bc8
--- arpwatch-2.1a15-dist/arpwatch.c	2012-07-23 09:55:35.832458313 +0200
1b6bc8
+++ arpwatch-2.1a15-new/arpwatch.c	2012-07-24 11:36:59.013953071 +0200
1b6bc8
@@ -161,15 +161,63 @@ void dropprivileges(const char* user)
1b6bc8
 	syslog(LOG_DEBUG, "Running as uid=%d gid=%d", getuid(), getgid());
1b6bc8
 }
1b6bc8
 
1b6bc8
+char *
1b6bc8
+get_first_dev(pcap_t **pd, int *linktype, char *errbuf)
1b6bc8
+{
1b6bc8
+	static char interface[IF_NAMESIZE + 1];
1b6bc8
+	register int snaplen, timeout;
1b6bc8
+	pcap_if_t *alldevs;
1b6bc8
+	pcap_if_t *dev;
1b6bc8
+	char *ret = NULL;
1b6bc8
+
1b6bc8
+	snaplen = max(sizeof(struct ether_header),
1b6bc8
+				  sizeof(struct fddi_header)) + sizeof(struct ether_arp);
1b6bc8
+	timeout = 1000;
1b6bc8
+
1b6bc8
+	if (pcap_findalldevs(&alldevs, errbuf) == -1) {
1b6bc8
+		(void)fprintf(stderr, "%s: lookup_device: %s\n",
1b6bc8
+					  prog, errbuf);
1b6bc8
+		exit(1);
1b6bc8
+	}
1b6bc8
+
1b6bc8
+	for (dev = alldevs; dev; dev = dev->next) {
1b6bc8
+		strncpy(interface, dev->name, strlen(dev->name)+1);
1b6bc8
+
1b6bc8
+		*pd = pcap_open_live(interface, snaplen, 1, timeout, errbuf);
1b6bc8
+		if (*pd == NULL) {
1b6bc8
+			syslog(LOG_ERR, "pcap open %s: %s, trying next...", interface, errbuf);
1b6bc8
+			continue;
1b6bc8
+			/* exit(1); */
1b6bc8
+		}
1b6bc8
+
1b6bc8
+		*linktype = pcap_datalink(*pd);
1b6bc8
+		/* Must be ethernet or fddi */
1b6bc8
+		if (*linktype != DLT_EN10MB && *linktype != DLT_FDDI) {
1b6bc8
+			syslog(LOG_ERR, "(%s) Link layer type %d not ethernet or fddi, trying next...",
1b6bc8
+				   interface, *linktype);
1b6bc8
+			pcap_close(*pd);
1b6bc8
+		}
1b6bc8
+		else {
1b6bc8
+			/* First match, use it */
1b6bc8
+			ret = interface;
1b6bc8
+			break;
1b6bc8
+		}
1b6bc8
+
1b6bc8
+	}
1b6bc8
+	pcap_freealldevs(alldevs);
1b6bc8
+	return (ret);
1b6bc8
+}
1b6bc8
+
1b6bc8
 int
1b6bc8
 main(int argc, char **argv)
1b6bc8
 {
1b6bc8
 	register char *cp;
1b6bc8
-	register int op, pid, snaplen, timeout, linktype, status;
1b6bc8
+	register int op, pid, status;
1b6bc8
+	int linktype;
1b6bc8
 #ifdef TIOCNOTTY
1b6bc8
 	register int fd;
1b6bc8
 #endif
1b6bc8
-	register pcap_t *pd;
1b6bc8
+	pcap_t *pd;
1b6bc8
 	register char *interface, *rfilename;
1b6bc8
 	struct bpf_program code;
1b6bc8
 	char errbuf[PCAP_ERRBUF_SIZE];
1b6bc8
@@ -189,6 +237,7 @@ main(int argc, char **argv)
1b6bc8
 
1b6bc8
 	opterr = 0;
1b6bc8
 	interface = NULL;
1b6bc8
+	linktype = -1;
1b6bc8
 	rfilename = NULL;
1b6bc8
 	pd = NULL;
1b6bc8
 	while ((op = getopt(argc, argv, "df:i:n:Nr:u:e:s:")) != EOF)
1b6bc8
@@ -264,11 +313,12 @@ main(int argc, char **argv)
1b6bc8
 		net = 0;
1b6bc8
 		netmask = 0;
1b6bc8
 	} else {
1b6bc8
+
1b6bc8
 		/* Determine interface if not specified */
1b6bc8
 		if (interface == NULL &&
1b6bc8
-		    (interface = pcap_lookupdev(errbuf)) == NULL) {
1b6bc8
-			(void)fprintf(stderr, "%s: lookup_device: %s\n",
1b6bc8
-			    prog, errbuf);
1b6bc8
+			(interface = get_first_dev(&pd, &linktype, errbuf)) == NULL) {
1b6bc8
+			(void)fprintf(stderr, "%s: lookup_device: no suitable interface found\n",
1b6bc8
+						  prog);
1b6bc8
 			exit(1);
1b6bc8
 		}
1b6bc8
 
1b6bc8
@@ -317,10 +367,6 @@ main(int argc, char **argv)
1b6bc8
 		}
1b6bc8
 		swapped = pcap_is_swapped(pd);
1b6bc8
 	} else {
1b6bc8
-		snaplen = max(sizeof(struct ether_header),
1b6bc8
-		    sizeof(struct fddi_header)) + sizeof(struct ether_arp);
1b6bc8
-		timeout = 1000;
1b6bc8
-		pd = pcap_open_live(interface, snaplen, 1, timeout, errbuf);
1b6bc8
 		if (pd == NULL) {
1b6bc8
 			syslog(LOG_ERR, "pcap open %s: %s", interface, errbuf);
1b6bc8
 			exit(1);
1b6bc8
@@ -340,14 +386,6 @@ main(int argc, char **argv)
1b6bc8
 		dropprivileges( serveruser );
1b6bc8
 	}
1b6bc8
 
1b6bc8
-	/* Must be ethernet or fddi */
1b6bc8
-	linktype = pcap_datalink(pd);
1b6bc8
-	if (linktype != DLT_EN10MB && linktype != DLT_FDDI) {
1b6bc8
-		syslog(LOG_ERR, "Link layer type %d not ethernet or fddi",
1b6bc8
-		    linktype);
1b6bc8
-		exit(1);
1b6bc8
-	}
1b6bc8
-
1b6bc8
 	/* Compile and install filter */
1b6bc8
 	if (pcap_compile(pd, &code, "arp or rarp", 1, netmask) < 0) {
1b6bc8
 		syslog(LOG_ERR, "pcap_compile: %s", pcap_geterr(pd));