Skip to content

Instantly share code, notes, and snippets.

@bdrewery
Created March 8, 2011 21:34
Show Gist options
  • Save bdrewery/861127 to your computer and use it in GitHub Desktop.
Save bdrewery/861127 to your computer and use it in GitHub Desktop.
Patch to FreeBSD who to show GeoIP Country
Summary:
Makefile | 2 ++
who.c | 34 +++++++++++++++++++++++++++++++++-
2 files changed, 35 insertions(+), 1 deletions(-)
----
Files modified:
commit 9cbed388a891caea998a5be60e13572f17800384
Author: Bryan Drewery <[email protected]>
Date: Tue Mar 8 14:17:18 2011 -0600
* Display Geo location in who output
who.c | 34 +++++++++++++++++++++++++++++++++-
1 files changed, 33 insertions(+), 1 deletions(-)
commit 5f452299f7207eb7ee699f2a3cf61fc3a5cf9fa0
Author: Bryan Drewery <[email protected]>
Date: Tue Mar 8 14:06:58 2011 -0600
* Link in GeoIP when building
Makefile | 2 ++
1 files changed, 2 insertions(+), 0 deletions(-)
----
diff --git ./usr.bin/who.orig/Makefile ./usr.bin/who/Makefile
index 8695ca2..6e34cf0 100644
--- ./usr.bin/who.orig/Makefile
+++ ./usr.bin/who/Makefile
@@ -1,5 +1,7 @@
# @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= who
+LDADD= /usr/local/lib/libGeoIP.so
+CFLAGS= -I/usr/local/include
.include <bsd.prog.mk>
diff --git ./usr.bin/who.orig/who.c ./usr.bin/who/who.c
index 4c1fe0b..1e40267 100644
--- ./usr.bin/who.orig/who.c
+++ ./usr.bin/who/who.c
@@ -47,6 +47,8 @@ __FBSDID("$FreeBSD: src/usr.bin/who/who.c,v 1.22.14.1 2010/12/21 17:10:29 kensmi
#include <unistd.h>
#include <utmp.h>
+#include <GeoIP.h>
+
static void heading(void);
static void process_utmp(FILE *);
static void quick(FILE *);
@@ -62,6 +64,9 @@ static int sflag; /* Show name, line, time */
static int Tflag; /* Show terminal state */
static int uflag; /* Show idle time */
+#define W_DISPGEOSIZE 20
+const char* geoiplookup(const char*);
+
int
main(int argc, char *argv[])
{
@@ -153,6 +158,7 @@ heading(void)
printf("%-*s ", 12, "TIME");
if (uflag)
printf("IDLE ");
+ printf("%-*s", W_DISPGEOSIZE, "GEO");
printf("%-*s", UT_HOSTSIZE, "FROM");
putchar('\n');
}
@@ -199,8 +205,16 @@ row(struct utmp *ut)
else
printf(" old ");
}
- if (*ut->ut_host != '\0')
+ if (*ut->ut_host != '\0') {
+ char host_buf[UT_HOSTSIZE + 1];
+ const char *country_name = NULL;
+ host_buf[UT_HOSTSIZE] = '\0';
+ strncpy(host_buf, ut->ut_host, UT_HOSTSIZE);
+ country_name = geoiplookup(host_buf);
+
+ printf("%-*.*s", W_DISPGEOSIZE, W_DISPGEOSIZE, country_name ? country_name : "-");
printf("(%.*s)", UT_HOSTSIZE, ut->ut_host);
+ }
putchar('\n');
}
@@ -289,6 +303,24 @@ whoami(FILE *fp)
row(&ut);
}
+const char* geoiplookup(const char *name) {
+ const char *country_name = NULL;
+
+ int gip_type = strchr(name, ':') ? GEOIP_COUNTRY_EDITION_V6 : GEOIP_COUNTRY_EDITION;
+ if (GeoIP_db_avail(gip_type)) {
+ GeoIP *gip = GeoIP_open_type(gip_type, GEOIP_STANDARD);
+
+ if (gip) {
+ int country_id = gip_type == GEOIP_COUNTRY_EDITION ? GeoIP_id_by_name(gip, name) : GeoIP_id_by_name_v6(gip, name);
+ if (country_id > 0)
+ country_name = GeoIP_country_name[country_id];
+ GeoIP_delete(gip);
+ }
+ }
+
+ return country_name;
+}
+
static int
ttywidth(void)
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment