]> git.meshlink.io Git - catta/blob - avahi-client/client.c
* Move avahi-core/llist.h to avahi-common/llist.h, not installed.
[catta] / avahi-client / client.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <avahi-client/client.h>
27 #include <avahi-common/dbus.h>
28 #include <avahi-common/llist.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #define DBUS_API_SUBJECT_TO_CHANGE
34 #include <dbus/dbus.h>
35 #include <dbus/dbus-glib-lowlevel.h>
36
37 #include <stdlib.h>
38
39 struct _AvahiClient
40 {
41     DBusConnection *bus;
42     AVAHI_LLIST_HEAD(AvahiEntryGroup, groups);
43 };
44
45 struct _AvahiEntryGroup {
46     char *path;
47     AvahiClient *parent;
48     AVAHI_LLIST_FIELDS(AvahiEntryGroup, groups);
49 };
50
51 static DBusHandlerResult
52 filter_func (DBusConnection *bus, DBusMessage *message, void *data)
53 {
54     DBusError error;
55     
56     g_assert (bus != NULL);
57     g_assert (message != NULL);
58
59     printf ("dbus: interface=%s, path=%s, member=%s\n",
60             dbus_message_get_interface (message),
61             dbus_message_get_path (message),
62             dbus_message_get_member (message));
63
64     dbus_error_init (&error);
65
66     if (dbus_message_is_signal(message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
67         gchar *name, *old, *new;
68         dbus_message_get_args(message, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID);
69         
70         if (dbus_error_is_set (&error)) {
71             fprintf(stderr, "Failed to parse NameOwnerChanged message: %s", error.message);
72             dbus_error_free (&error);
73             goto out;
74         }
75
76         if (strcmp (name, AVAHI_DBUS_NAME) == 0) {
77             if (old == NULL && new != NULL) {
78                 fprintf(stderr, "Avahi Daemon connected\n");
79             } else if (old != NULL && new == NULL) {
80                 fprintf(stderr, "Avahi Daemon disconnected\n");
81             }
82         }
83
84         return DBUS_HANDLER_RESULT_HANDLED;
85     }
86
87 out: 
88     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
89 }
90
91 AvahiClient *
92 avahi_client_new ()
93 {
94     AvahiClient *tmp;
95     DBusError error;
96
97     tmp = g_new (AvahiClient, 1);
98
99     if (tmp == NULL)
100         goto fail;
101
102     AVAHI_LLIST_HEAD_INIT(AvahiEntryGroup, tmp->groups);
103
104     dbus_error_init (&error);
105
106     tmp->bus = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
107
108     dbus_connection_setup_with_g_main (tmp->bus, NULL);
109
110     if (dbus_error_is_set (&error)) {
111         fprintf(stderr, "Error getting system d-bus: %s\n", error.message);
112         dbus_error_free (&error);
113         goto fail;
114     }
115
116     dbus_connection_set_exit_on_disconnect (tmp->bus, FALSE);
117
118     if (!dbus_connection_add_filter (tmp->bus, filter_func, tmp, NULL))
119     {
120         fprintf (stderr, "Failed to add d-bus filter\n");
121         goto fail;
122     }
123
124     dbus_bus_add_match (tmp->bus,
125             "type='signal', "
126             "interface='" AVAHI_DBUS_INTERFACE_SERVER "', "
127             "sender='" AVAHI_DBUS_NAME "', "
128             "path='" AVAHI_DBUS_PATH_SERVER "'",
129             &error);
130
131     if (dbus_error_is_set (&error))
132     {
133         fprintf (stderr, "Error adding filter match: %s\n", error.message);
134         dbus_error_free (&error);
135         goto fail;
136
137     }   
138
139     dbus_bus_add_match (tmp->bus,
140             "type='signal', "
141             "interface='" DBUS_INTERFACE_DBUS "', "
142             "sender='" DBUS_SERVICE_DBUS "', "
143             "path='" DBUS_PATH_DBUS "'",
144             &error);
145
146     if (dbus_error_is_set (&error))
147     {
148         fprintf (stderr, "Error adding filter match: %s\n", error.message);
149         dbus_error_free (&error);
150         goto fail;
151
152     }   
153
154     return tmp;
155
156 fail:
157     if (tmp) free (tmp);
158     return NULL;
159 }
160
161 static char*
162 avahi_client_get_string_reply_and_block (AvahiClient *client, char *method, char *param)
163 {
164     DBusMessage *message;
165     DBusMessage *reply;
166     DBusError error;
167     char *ret, *new;
168
169     g_assert (client != NULL);
170     g_assert (method != NULL);
171
172     dbus_error_init (&error);
173
174     message = dbus_message_new_method_call (AVAHI_DBUS_NAME, AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, method);
175
176     if (param != NULL)
177     {
178         if (!dbus_message_append_args (message, DBUS_TYPE_STRING, &param, DBUS_TYPE_INVALID))
179         {
180             fprintf (stderr, "Failed to append string argument to %s message\n", method);
181             return NULL;
182         }
183     }
184     
185     reply = dbus_connection_send_with_reply_and_block (client->bus, message, -1, &error);
186
187     if (dbus_error_is_set (&error))
188     {
189         fprintf (stderr, "Error sending %s message: %s\n", method, error.message);
190         dbus_error_free (&error);
191         dbus_message_unref (message);
192         return NULL;
193     }
194
195     if (reply == NULL)
196     {
197         dbus_message_unref (message);
198         fprintf (stderr, "Could not connect to Avahi daemon\n");
199         return NULL;
200     }
201
202     dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &ret, DBUS_TYPE_INVALID);
203
204     if (dbus_error_is_set (&error))
205     {
206         fprintf (stderr, "Failed to parse %s reply: %s\n", method, error.message);
207         dbus_error_free (&error);
208         return NULL;
209     }
210
211     new = strdup (ret);
212
213     return new;
214 }
215
216 char*
217 avahi_client_get_version_string (AvahiClient *client)
218 {
219     return avahi_client_get_string_reply_and_block (client, "GetVersionString", NULL);
220 }
221
222 char*
223 avahi_client_get_domain_name (AvahiClient *client)
224 {
225     return avahi_client_get_string_reply_and_block (client, "GetDomainName", NULL);
226 }
227
228 char*
229 avahi_client_get_host_name (AvahiClient *client)
230 {
231     return avahi_client_get_string_reply_and_block (client, "GetHostName", NULL);
232 }
233
234 char*
235 avahi_client_get_host_name_fqdn (AvahiClient *client)
236 {
237     return avahi_client_get_string_reply_and_block (client, "GetHostNameFqdn", NULL);
238 }
239
240 AvahiEntryGroup*
241 avahi_entry_group_new (AvahiClient *client)
242 {
243     AvahiEntryGroup *tmp;
244
245     tmp = malloc (sizeof (AvahiEntryGroup));
246
247     tmp->parent = client;
248
249     AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, client->groups, tmp);
250
251     return tmp;
252 fail:
253     if (tmp) free (tmp);
254     return NULL;
255 }