]> git.meshlink.io Git - catta/blob - avahi-client/client.c
6ff099a662bb919d658a05348fe79eaba5372a1c
[catta] / avahi-client / client.c
1 #include <avahi-client/client.h>
2 #include <avahi-common/dbus.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <glib.h>
6 #include <string.h>
7
8 #define DBUS_API_SUBJECT_TO_CHANGE
9 #include <dbus/dbus.h>
10 #include <dbus/dbus-glib-lowlevel.h>
11
12 #include <stdlib.h>
13
14 struct _AvahiClientPriv
15 {
16     DBusConnection *bus;
17 };
18
19 static DBusHandlerResult
20 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
21 {
22     DBusError error;
23     
24     g_assert (bus != NULL);
25     g_assert (message != NULL);
26
27     printf ("dbus: interface=%s, path=%s, member=%s\n",
28             dbus_message_get_interface (message),
29             dbus_message_get_path (message),
30             dbus_message_get_member (message));
31
32     dbus_error_init (&error);
33
34     if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
35         gchar *name, *old, *new;
36         dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
37         
38         if (dbus_error_is_set (&error)) {
39             fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
40             dbus_error_free (&error);
41             goto out;
42         }
43
44         if (strcmp (name, AVAHI_DBUS_NAME) < 0) {
45             if (old == NULL && new != NULL) {
46                 fprintf(stderr, "Avahi Daemon reconnected\n");
47             } else if (old != NULL && new == NULL) {
48                 fprintf(stderr, "Avahi Daemon disconnected\n");
49             }
50         }
51
52         return DBUS_HANDLER_RESULT_HANDLED;
53     }
54
55 out: 
56     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
57 }
58
59 static int _dbus_add_match (DBusConnection *bus, char *type, char *interface, char *sender, char *path)
60 {
61     DBusError error;
62     char *filter;
63
64     g_assert (bus != NULL);
65
66     dbus_error_init (&error);
67     filter = g_strdup_printf ("type='%s', interface='%s', sender='%s', path='%s'", type, interface, sender, path);
68     dbus_bus_add_match (bus, filter, &error);
69     g_free (filter);
70
71     if (dbus_error_is_set (&error))
72     {
73         fprintf (stderr, "Error adding filter match: %s\n", error.message);
74         dbus_error_free (&error);
75         return FALSE;
76     }
77
78     return TRUE;
79 }
80
81 AvahiClient *
82 avahi_client_new ()
83 {
84     AvahiClient *tmp;
85     DBusError error;
86
87     tmp = malloc (sizeof (AvahiClient));
88     tmp->priv = malloc (sizeof (AvahiClientPriv));
89
90     g_assert (tmp != NULL);
91     g_assert (tmp->priv != NULL);
92     
93     dbus_error_init (&error);
94
95     tmp->priv->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
96
97     dbus_connection_setup_with_g_main (tmp->priv->bus, NULL);
98
99     if (dbus_error_is_set (&error)) {
100         fprintf(stderr, "Error getting system d-bus: %s\n", error.message);
101         dbus_error_free (&error);
102         goto fail;
103     }
104
105     dbus_connection_set_exit_on_disconnect (tmp->priv->bus, FALSE);
106
107     if (!dbus_connection_add_filter (tmp->priv->bus, filter_func, tmp, NULL))
108     {
109         fprintf (stderr, "Failed to add d-bus filter\n");
110         goto fail;
111     }
112
113     if (!_dbus_add_match (tmp->priv->bus, "signal", AVAHI_DBUS_INTERFACE_SERVER, AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER)) goto fail;
114     if (!_dbus_add_match (tmp->priv->bus, "signal", DBUS_INTERFACE_DBUS, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS)) goto fail;
115
116     return tmp;
117
118 fail:
119     if (tmp->priv) free (tmp->priv);
120     if (tmp) free (tmp);
121     return NULL;
122 }
123
124 static char*
125 avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char *param)
126 {
127     DBusMessage *message;
128     DBusMessage *reply;
129     DBusError error;
130     char *ret;
131
132     g_assert (client != NULL);
133     g_assert (method != NULL);
134
135     dbus_error_init (&error);
136
137     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
138     fprintf (stderr, "message = dbus_message_new_method_call (%s, %s, %s, %s)\n", AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
139
140     if (param != NULL)
141     {
142         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID))
143         {
144             fprintf (stderr, "Failed to append string argument to %s message\n", method);
145             return NULL;
146         }
147     }
148     
149     reply = dbus_connection_send_with_reply_and_block (client->priv->bus, message, -1, &error);
150
151     if (dbus_error_is_set (&error))
152     {
153         fprintf (stderr, "Error sending %s message: %s\n", method, error.message);
154         dbus_error_free (&error);
155         dbus_message_unref (message);
156         return NULL;
157     }
158
159     if (reply == NULL)
160     {
161         dbus_message_unref (message);
162         fprintf (stderr, "Could not connect to Avahi daemon\n");
163         return NULL;
164     }
165
166     dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret);
167
168     if (dbus_error_is_set (&error))
169     {
170         fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message);
171         dbus_error_free (&error);
172         return NULL;
173     }
174
175     return ret;
176 }
177
178 char*
179 avahi_client_get_version_string (AvahiClient *client)
180 {
181     return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
182 }
183
184 char*
185 avahi_client_get_domain_name (AvahiClient *client)
186 {
187     return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
188 }
189
190 char*
191 avahi_client_get_host_name (AvahiClient *client)
192 {
193     return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
194 }
195
196 char*
197 avahi_client_get_host_name_fqdn (AvahiClient *client)
198 {
199     return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
200 }
201
202 char*
203 avahi_client_get_alternative_host_name (AvahiClient *client, char *host)
204 {
205     return avahi_client_get_string_reply_and_block (client, "GetAlternativeHostName", host);
206 }
207
208 char*
209 avahi_client_get_alternative_service_name (AvahiClient *client, char *service)
210 {
211     return avahi_client_get_string_reply_and_block (client, "GetAlternativeServiceName", service);
212 }