Skip to content
This repository
Browse code

get_SSH_IP gracefully handles missing, IPv6-formatted environment var

Closes #292 github issue.
  • Loading branch information...
commit c073ad364afd0e883442382b3da9f9aeee2c5701 1 parent 02f5488
Keith Winstein authored July 26, 2012
2  configure.ac
@@ -195,7 +195,7 @@ AC_TYPE_UINTPTR_T
195 195
 # Checks for library functions.
196 196
 AC_FUNC_FORK
197 197
 AC_FUNC_MBRTOWC
198  
-AC_CHECK_FUNCS([gettimeofday setrlimit inet_ntoa iswprint memchr memset nl_langinfo posix_memalign setenv setlocale sigaction socket strchr strdup strerror strtol wcwidth])
  198
+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])
199 199
 
200 200
 AC_SEARCH_LIBS([clock_gettime], [rt], [AC_DEFINE([HAVE_CLOCK_GETTIME], [1], [Define if clock_gettime is available.])])
201 201
 
20  src/frontend/mosh-server.cc
@@ -122,13 +122,29 @@ void spin( void )
122 122
 string get_SSH_IP( void )
123 123
 {
124 124
   const char *SSH_CONNECTION = getenv( "SSH_CONNECTION" );
125  
-  fatal_assert( SSH_CONNECTION );
  125
+  if ( !SSH_CONNECTION ) { /* Older sshds don't set this */
  126
+    fprintf( stderr, "Warning: SSH_CONNECTION not found; binding to any interface.\n" );
  127
+    return string( "0.0.0.0" );
  128
+  }
126 129
   char *SSH_writable = strdup( SSH_CONNECTION );
127 130
   fatal_assert( SSH_writable );
  131
+
128 132
   strtok( SSH_writable, " " );
129 133
   strtok( NULL, " " );
130 134
   const char *local_interface_IP = strtok( NULL, " " );
131  
-  fatal_assert( local_interface_IP );
  135
+  if ( !local_interface_IP ) {
  136
+    fprintf( stderr, "Warning: Could not parse SSH_CONNECTION; binding to any interface.\n" );
  137
+    return string( "0.0.0.0" );
  138
+  }
  139
+
  140
+  /* Strip IPv6 prefix. */
  141
+  const char IPv6_prefix[] = "::ffff:";
  142
+
  143
+  if ( ( strlen( local_interface_IP ) > strlen( IPv6_prefix ) )
  144
+       && ( 0 == strncasecmp( local_interface_IP, IPv6_prefix, strlen( IPv6_prefix ) ) ) ) {
  145
+    return string( local_interface_IP + strlen( IPv6_prefix ) );
  146
+  }
  147
+
132 148
   return string( local_interface_IP );
133 149
 }
134 150
 

0 notes on commit c073ad3

Please sign in to comment.
Something went wrong with that request. Please try again.