@@ -248,6 +248,44 @@ static void initAppData(AppData* data) {
248248 data -> useRemoteCursor = FALSE;
249249}
250250
251+ static void parse_host_and_port (const char * input , char * * host , int * port ) {
252+ char * open_bracket = strchr (input , '[' );
253+ char * close_bracket = strchr (input , ']' );
254+
255+ if (open_bracket && close_bracket && close_bracket > open_bracket ) {
256+ // IPv6 address with brackets, allocate and copy IP inside brackets
257+ size_t ip_len = close_bracket - open_bracket - 1 ;
258+ * host = malloc (ip_len + 1 );
259+ strncpy (* host , open_bracket + 1 , ip_len );
260+ (* host )[ip_len ] = '\0' ;
261+ // check for port via last colon
262+ char * colon = strchr (close_bracket , ':' );
263+ if (colon ) {
264+ // Parse port as integer
265+ * port = atoi (colon + 1 );
266+ }
267+ } else {
268+ // IPv4 or IPv6 w/o brackets, look for colons to decide which one
269+ char * first_colon = strchr (input , ':' );
270+ char * last_colon = strrchr (input , ':' );
271+ if (first_colon ) {
272+ // check IPv4 vs IPv6
273+ if (first_colon == last_colon ) {
274+ // one colon, i.e. IPv4 with port, allocate and copy IP (before last colon)
275+ * host = strndup (input , first_colon - input );
276+ // Parse port as integer
277+ * port = atoi (first_colon + 1 );
278+ } else {
279+ // IPv6, just copy input
280+ * host = strdup (input );
281+ }
282+ } else {
283+ // No colon, i.e. IPv4 w/o port, just copy input
284+ * host = strdup (input );
285+ }
286+ }
287+ }
288+
251289rfbClient * rfbGetClient (int bitsPerSample ,int samplesPerPixel ,
252290 int bytesPerPixel ) {
253291#ifdef WIN32
@@ -467,31 +505,17 @@ rfbBool rfbInitClient(rfbClient* client,int* argc,char** argv) {
467505 client -> QoS_DSCP = atoi (argv [i + 1 ]);
468506 j += 2 ;
469507 } else if (i + 1 < * argc && strcmp (argv [i ], "-repeaterdest" ) == 0 ) {
470- char * colon = strchr (argv [i + 1 ],':' );
471-
472508 free (client -> destHost );
473509 client -> destPort = 5900 ;
474510
475- client -> destHost = strdup (argv [i + 1 ]);
476- if (client -> destHost && colon ) {
477- client -> destHost [(int )(colon - argv [i + 1 ])] = '\0' ;
478- client -> destPort = atoi (colon + 1 );
479- }
511+ parse_host_and_port (argv [i ], & client -> destHost , & client -> destPort );
512+
480513 j += 2 ;
481514 } else {
482- char * colon = strrchr (argv [i ],':' );
483-
484515 free (client -> serverHost );
485516
486- if (colon ) {
487- client -> serverHost = strdup (argv [i ]);
488- if (client -> serverHost ) {
489- client -> serverHost [(int )(colon - argv [i ])] = '\0' ;
490- client -> serverPort = atoi (colon + 1 );
491- }
492- } else {
493- client -> serverHost = strdup (argv [i ]);
494- }
517+ parse_host_and_port (argv [i ], & client -> serverHost , & client -> serverPort );
518+
495519 if (client -> serverPort >= 0 && client -> serverPort < 5900 )
496520 client -> serverPort += 5900 ;
497521 }
0 commit comments