Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / socket-common.c
1 /* #included into both socket-client.c and socket-server.c */
2
3 static const char *unix_socket_address_types[] = {
4   "invalid",
5   "anonymous",
6   "path",
7   "abstract",
8   "padded"
9 };
10
11 static char *
12 socket_address_to_string (GSocketAddress *address)
13 {
14   char *res = NULL;
15
16   if (G_IS_INET_SOCKET_ADDRESS (address))
17     {
18       GInetAddress *inet_address;
19       char *str;
20       int port;
21
22       inet_address = g_inet_socket_address_get_address (G_INET_SOCKET_ADDRESS (address));
23       str = g_inet_address_to_string (inet_address);
24       port = g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (address));
25       res = g_strdup_printf ("%s:%d", str, port);
26       g_free (str);
27     }
28 #ifdef G_OS_UNIX
29   else if (G_IS_UNIX_SOCKET_ADDRESS (address))
30     {
31       GUnixSocketAddress *uaddr = G_UNIX_SOCKET_ADDRESS (address);
32
33       res = g_strdup_printf ("%s:%s",
34                              unix_socket_address_types[g_unix_socket_address_get_address_type (uaddr)],
35                              g_unix_socket_address_get_path (uaddr));
36     }
37 #endif
38
39   return res;
40 }
41
42 GSocketAddress *
43 socket_address_from_string (const char *name)
44 {
45 #ifdef G_OS_UNIX
46   int i, len;
47
48   for (i = 0; i < G_N_ELEMENTS (unix_socket_address_types); i++)
49     {
50       len = strlen (unix_socket_address_types[i]);
51       if (!strncmp (name, unix_socket_address_types[i], len) &&
52           name[len] == ':')
53         {
54           return g_unix_socket_address_new_with_type (name + len + 1, -1,
55                                                       (GUnixSocketAddressType)i);
56         }
57     }
58 #endif
59   return NULL;
60 }