From c61ea8df9e9690b59fd42846f112a7a03f1946fe Mon Sep 17 00:00:00 2001 From: BLINDAUER Emmanuel Date: Sun, 12 Nov 2017 11:27:52 +0100 Subject: [PATCH] Extract the exact ip from the client_ip --- sesman/sessionrecord.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sesman/sessionrecord.c b/sesman/sessionrecord.c index 4336994f..22dbd3d8 100644 --- a/sesman/sessionrecord.c +++ b/sesman/sessionrecord.c @@ -50,6 +50,26 @@ add_xtmp_entry(int pid, const char *line, const char *user, const char *rhostnam { _utmp ut; struct timeval tv; + char *hostname = 0; + + /* The string rhostname containt too much data, only get the ip + * the format is + * "2001:123:12:1234:1234:1234:1234:1234:53194 - socket: 12" + * "::ffff:99.99.9.999:51165 - socket: 12" + * "99.99.9.999:51165 - socket: 12" + * + * So the IP is the string up the two last colons + */ + int i = strlen(rhostname) - 1; + while ((i>0) && (rhostname[i] != ':')) { + i--; + } + i--; + while ((i>0) && (rhostname[i] != ':')) { + i--; + } + + hostname = strndup(rhostname, i); memset(&ut, 0, sizeof(ut)); @@ -60,7 +80,7 @@ add_xtmp_entry(int pid, const char *line, const char *user, const char *rhostnam ut.ut_tv.tv_usec = tv.tv_usec; strncpy(ut.ut_line, line , sizeof(ut.ut_line)); strncpy(ut.ut_user, user , sizeof(ut.ut_user)); - strncpy(ut.ut_host, rhostname, sizeof(ut.ut_host)); + strncpy(ut.ut_host, hostname, sizeof(ut.ut_host)); /* utmp */ setutxent();