get_SSH_IP gracefully handles missing, IPv6-formatted environment var

Closes #292 github issue.
This commit is contained in:
Keith Winstein
2012-07-26 22:54:45 -04:00
parent 02f54881be
commit c073ad364a
2 changed files with 19 additions and 3 deletions
+1 -1
View File
@@ -195,7 +195,7 @@ AC_TYPE_UINTPTR_T
# Checks for library functions. # Checks for library functions.
AC_FUNC_FORK AC_FUNC_FORK
AC_FUNC_MBRTOWC AC_FUNC_MBRTOWC
AC_CHECK_FUNCS([gettimeofday setrlimit inet_ntoa iswprint memchr memset nl_langinfo posix_memalign setenv setlocale sigaction socket strchr strdup strerror strtol wcwidth]) AC_CHECK_FUNCS([gettimeofday setrlimit inet_ntoa iswprint memchr memset nl_langinfo posix_memalign setenv setlocale sigaction socket strchr strdup strncasecmp strtok strerror strtol wcwidth])
AC_SEARCH_LIBS([clock_gettime], [rt], [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define if clock_gettime is available.])]) AC_SEARCH_LIBS([clock_gettime], [rt], [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define if clock_gettime is available.])])
+18 -2
View File
@@ -122,13 +122,29 @@ void spin( void )
string get_SSH_IP( void ) string get_SSH_IP( void )
{ {
const char *SSH_CONNECTION = getenv( "SSH_CONNECTION" ); const char *SSH_CONNECTION = getenv( "SSH_CONNECTION" );
fatal_assert( SSH_CONNECTION ); if ( !SSH_CONNECTION ) { /* Older sshds don't set this */
fprintf( stderr, "Warning: SSH_CONNECTION not found; binding to any interface.\n" );
return string( "0.0.0.0" );
}
char *SSH_writable = strdup( SSH_CONNECTION ); char *SSH_writable = strdup( SSH_CONNECTION );
fatal_assert( SSH_writable ); fatal_assert( SSH_writable );
strtok( SSH_writable, " " ); strtok( SSH_writable, " " );
strtok( NULL, " " ); strtok( NULL, " " );
const char *local_interface_IP = strtok( NULL, " " ); const char *local_interface_IP = strtok( NULL, " " );
fatal_assert( local_interface_IP ); if ( !local_interface_IP ) {
fprintf( stderr, "Warning: Could not parse SSH_CONNECTION; binding to any interface.\n" );
return string( "0.0.0.0" );
}
/* Strip IPv6 prefix. */
const char IPv6_prefix[] = "::ffff:";
if ( ( strlen( local_interface_IP ) > strlen( IPv6_prefix ) )
&& ( 0 == strncasecmp( local_interface_IP, IPv6_prefix, strlen( IPv6_prefix ) ) ) ) {
return string( local_interface_IP + strlen( IPv6_prefix ) );
}
return string( local_interface_IP ); return string( local_interface_IP );
} }