Specifying ipv6 interface using glib/gio

427 views Asked by At

I'm trying to use the Gnome glib/gio C library to create a client program to connect to a server via IPv6. My server box has a link local IPv6 address:

inet6 addr: fe80::2d0:c9ff:feda:99e0/64 Scope:Link

So, I to access it, I have to tell the client software which interface to use (eth0 in this case). So the following works (using port 1500):

nc -6 fe80::2d0:c9ff:feda:99e0%eth0 1500

In glib, using the %eth0 notation violates the URI notation:

(process:31159): GLib-GIO-WARNING **: Invalid URI 'none://[fe80:0:0:0:2d0:c9ff:feda:99e0%eth0]:1500'

I've looked in the code and it clearly expects to see the percent escape notation (i.e. the characters '%25') but I can't seem to get the format correct:

** (process:5741): ERROR **: Invalid URI 'none://[fe80:0:0:0:2d0:c9ff:feda:99e0%25eth0]1500'

So, anyone know how to specify the interface?

EDIT: Here's the code

// gchar test[255] = "fe80:0:0:0:2d0:c9ff:feda:99e0%eth0";
// gchar test[255] = "fe80:0:0:0:2d0:c9ff:feda:99e0\%eth0";
// gchar test[255] = "fe80:0:0:0:2d0:c9ff:feda:99e0\x25eth0";
// gchar test[255] = "fe80:0:0:0:2d0:c9ff:feda:99e0\%%25eth0";
gchar test[255] = "[fe80:0:0:0:2d0:c9ff:feda:99e0\%%eth0]";

connection = g_socket_client_connect_to_uri (client,
                                           test,
                                           1500,
                                           NULL,
                                           &error);

EDIT 2: Complete code (with MichaelHampton's input):

#include <glib.h>
#include <gio/gio.h>

int
main (int argc, char *argv[])
{
   /* initialize glib */
  g_type_init ();

  GError * error = NULL;

  /* create a new connection */
  GSocketConnection * connection = NULL;
  GSocketClient * client = g_socket_client_new();

    connection = g_socket_client_connect_to_host (client,
                                                  (gchar*)"fe80::5054:ff:fe1f:6b6c\%br0",
                                                   1500, /* your port goes here */
                                                   NULL,
                                                   &error);

  /* don't forget to check for errors */
  if (error != NULL)
  {
      g_error (error->message);
  }
  else
  {
      g_print ("Connection successful!\n");
  }

  return 0;
}
1

There are 1 answers

4
Michael Hampton On

Ah, you're calling the wrong function. You should be using g_socket_client_connect_to_host to connect directly to a host and port.

Here is a working example:

 connection = g_socket_client_connect_to_host (client,
                                              (gchar*)"fe80::5054:ff:fe1f:6b6c\%br0",
                                               1500, /* your port goes here */
                                               NULL,
                                               &error);

The complete example code, which it looked like you were using, was in the related question: GIO socket-server / -client example