i wanted to be able to run maillogs and other logs through grepcidr
in order to pick out specific address blocks and view the entire
log message... and i found two things.  first the line length was
insufficient, and second grepcidr expects input lines to contain only
ip addresses.

this patch barely deals with the line length issue by just cranking up the
max.

i also do regex matching on the line to test anything which
looks like an ip address... i probably could eliminate the sscanf
but i haven't benchmarked if it's faster to use a pattern like
"([0-9]+)\.([0-9]+\).([0-9]+)\.([0-9]+)" with substring matches or to
use the sscanf...

anyhow... here's my patch.

thanks
-dean

p.s. oh and the Makefile needs dependencies...


Index: grepcidr-1.3/grepcidr.c
===================================================================
--- grepcidr-1.3.orig/grepcidr.c	2005-04-23 15:00:16.000000000 -0700
+++ grepcidr-1.3/grepcidr.c	2006-10-03 23:28:35.517663302 -0700
@@ -25,6 +25,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/types.h>
+#include <regex.h>
 #include "getopt.h"
 
 #define EXIT_OK		0
@@ -164,6 +166,8 @@
 	char line[MAXFIELD];
 	int foundopt;
 	int anymatch = 0;			/* did anything match? for exit code */
+	regex_t ip_regex;
+	int rc;
 
 	if (argc == 1)
 	{
@@ -271,24 +275,47 @@
 				array[item].min = array[item-1].max + 1;	/* overflow possibility */
 		}
 	}
+
+	rc = regcomp(&ip_regex, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+", REG_EXTENDED);
+	if (rc != 0)
+	{
+		regerror(rc, &ip_regex, line, sizeof(line));
+		fprintf(stderr, "regcomp error: %s\n", line);
+		exit(1);
+	}
 	
 	/* Match IPs from input stream to network patterns */
 	while (fgets(line, sizeof(line), inp_stream))
 	{
-		struct netspec key;
-		if ((key.min=ip_to_uint(line)))
-		{
-			int match = 0;
-			if (bsearch(&key, array, patterns, sizeof(struct netspec), netsearch))
-				match = 1;
-			if (invert ^ match)
+		regmatch_t pmatch[1];
+		char *p = line;
+
+		for (;;) {
+			struct netspec key;
+
+			rc = regexec(&ip_regex, p, 1, pmatch, 0);
+			if (rc == REG_NOMATCH) break;
+			if (rc != 0) {
+				regerror(rc, &ip_regex, line, sizeof(line));
+				fprintf(stderr, "regcomp error: %s\n", line);
+				break;
+			}
+			if ((key.min=ip_to_uint(p + pmatch[0].rm_so)))
 			{
-				anymatch = 1;
-				if (counting)
-					counting++;
-				else
-					printf("%s", line);
+				int match = 0;
+				if (bsearch(&key, array, patterns, sizeof(struct netspec), netsearch))
+					match = 1;
+				if (invert ^ match)
+				{
+					anymatch = 1;
+					if (counting)
+						counting++;
+					else
+						printf("%s", line);
+					break;
+				}
 			}
+			p += pmatch[0].rm_eo;
 		}
 	}
 	
Index: grepcidr-1.3/Makefile
===================================================================
--- grepcidr-1.3.orig/Makefile	2006-10-03 23:28:37.841807029 -0700
+++ grepcidr-1.3/Makefile	2006-10-03 23:28:50.930616156 -0700
@@ -11,7 +11,7 @@
 
 # End of settable values
 
-grepcidr:
+grepcidr: grepcidr.c getopt.c
 	$(CC) $(CFLAGS) -o grepcidr grepcidr.c getopt.c
 
 all:	grepcidr
