http://www.mediawiki.org/wiki/Special:Code/MediaWiki/58255

Revision: 58255
Author:   mark
Date:     2009-10-28 13:35:45 +0000 (Wed, 28 Oct 2009)

Log Message:
-----------
Initial import

Added Paths:
-----------
    trunk/tools/geoiplogtag/
    trunk/tools/geoiplogtag/Makefile
    trunk/tools/geoiplogtag/geoiplogtag.c

Added: trunk/tools/geoiplogtag/Makefile
===================================================================
--- trunk/tools/geoiplogtag/Makefile                            (rev 0)
+++ trunk/tools/geoiplogtag/Makefile    2009-10-28 13:35:45 UTC (rev 58255)
@@ -0,0 +1,16 @@
+SHELL = /bin/sh
+.SUFFIXES: .c .o
+TARGETS = geoiplogtag
+
+%.o:   %.c
+               $(CC) -c -o $@ $<
+
+geoiplogtag:   geoiplogtag.o
+                               $(CC) -o $@ -lGeoIP $<
+
+.PHONY: clean
+
+clean:
+               rm -f *.o $(TARGETS)
+
+all:   $(TARGETS)              
\ No newline at end of file

Added: trunk/tools/geoiplogtag/geoiplogtag.c
===================================================================
--- trunk/tools/geoiplogtag/geoiplogtag.c                               (rev 0)
+++ trunk/tools/geoiplogtag/geoiplogtag.c       2009-10-28 13:35:45 UTC (rev 
58255)
@@ -0,0 +1,75 @@
+/* geoiplogtag
+ * Tags log lines (received from stdin) containing IP addresses
+ * with the associated country code, using the MaxMind db and library.
+ *  
+ * Copyright (C) 2009 Mark Bergsma <m...@wikimedia.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <GeoIP.h>
+
+/* Maximum length of an IPv4 address */
+#define MAX_ADDR_LEN   15
+
+int main(int argc, char *argv[]) {
+       size_t linelen = 0;
+       ssize_t readlen;
+       char *line = NULL, *p, *q, addr[MAX_ADDR_LEN+1];
+       int i, fieldidx = 0;
+       GeoIP *gi;
+       
+       /* The (optional) 1st argument is the ip address field index */
+       if (argc > 1 && sscanf(argv[1], "%ud", &fieldidx) > 0)
+               fieldidx--;
+       
+       /* Open the GeoIP database as a mmap cached file */
+       gi = GeoIP_new(GEOIP_MMAP_CACHE|GEOIP_CHECK_CACHE);
+
+       while ((readlen = getline(&line, &linelen, stdin)) != -1) {
+               /* Remove the newline at the end */
+               line[readlen-1] = '\0';
+               p = line;
+               
+               /* Find the requested field */
+               for (i=0; i<fieldidx; i++) {
+                       if ((p = strchr(p, ' ')) != NULL)
+                               p++;
+                       else
+                               break;
+               };
+               
+               /* Contain the field and copy to an aux var */
+               if (p != NULL && (q = strchrnul(p, ' ')) != NULL && q-p <= 
MAX_ADDR_LEN) {
+                       strncpy(addr, p, q-p);
+                       addr[q-p] = '\0';
+                       
+                       /* Output line and add country code to the end */
+                       printf("%s %s\n", line, GeoIP_country_code_by_addr(gi, 
addr));
+               }
+               else {
+                       /* Output original line unmodified */
+                       printf("%s\n", line);
+               }
+       }
+       
+       free(line);     
+       return 0;
+}



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to