Blame SOURCES/arpwatch-2.1a15-devlookup.patch

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