blob: 19509a60ac7f70fa96bba6630104f9f2b9f8ab02 [file] [log] [blame]
Simon Kelley59546082012-01-06 20:02:04 +00001/* dnsmasq is Copyright (c) 2000-2012 Simon Kelley
Simon Kelley3d8df262005-08-29 12:19:27 +01002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
Simon Kelley824af852008-02-12 20:43:05 +00005 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
Simon Kelley3d8df262005-08-29 12:19:27 +01008 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
Simon Kelley824af852008-02-12 20:43:05 +000012
Simon Kelley73a08a22009-02-05 20:28:08 +000013 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
Simon Kelley3d8df262005-08-29 12:19:27 +010015*/
16
17#include "dnsmasq.h"
18
19#ifdef HAVE_DBUS
20
Simon Kelley3d8df262005-08-29 12:19:27 +010021#include <dbus/dbus.h>
22
Simon Kelleyad094272012-08-10 17:10:54 +010023const char* introspection_xml_template =
Simon Kelley73a08a22009-02-05 20:28:08 +000024"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
25"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
26"<node name=\"" DNSMASQ_PATH "\">\n"
27" <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
28" <method name=\"Introspect\">\n"
29" <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
30" </method>\n"
31" </interface>\n"
Simon Kelleyad094272012-08-10 17:10:54 +010032" <interface name=\"%s\">\n"
Simon Kelley73a08a22009-02-05 20:28:08 +000033" <method name=\"ClearCache\">\n"
34" </method>\n"
35" <method name=\"GetVersion\">\n"
36" <arg name=\"version\" direction=\"out\" type=\"s\"/>\n"
37" </method>\n"
38" <method name=\"SetServers\">\n"
39" <arg name=\"servers\" direction=\"in\" type=\"av\"/>\n"
40" </method>\n"
Simon Kelleyfaafb3f2012-09-20 14:17:39 +010041" <method name=\"SetServersEx\">\n"
42" <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n"
43" </method>\n"
Simon Kelley73a08a22009-02-05 20:28:08 +000044" <signal name=\"DhcpLeaseAdded\">\n"
45" <arg name=\"ipaddr\" type=\"s\"/>\n"
46" <arg name=\"hwaddr\" type=\"s\"/>\n"
47" <arg name=\"hostname\" type=\"s\"/>\n"
48" </signal>\n"
49" <signal name=\"DhcpLeaseDeleted\">\n"
50" <arg name=\"ipaddr\" type=\"s\"/>\n"
51" <arg name=\"hwaddr\" type=\"s\"/>\n"
52" <arg name=\"hostname\" type=\"s\"/>\n"
53" </signal>\n"
54" <signal name=\"DhcpLeaseUpdated\">\n"
55" <arg name=\"ipaddr\" type=\"s\"/>\n"
56" <arg name=\"hwaddr\" type=\"s\"/>\n"
57" <arg name=\"hostname\" type=\"s\"/>\n"
58" </signal>\n"
59" </interface>\n"
60"</node>\n";
61
Simon Kelleyad094272012-08-10 17:10:54 +010062static char *introspection_xml = NULL;
63
Simon Kelley3d8df262005-08-29 12:19:27 +010064struct watch {
65 DBusWatch *watch;
66 struct watch *next;
67};
68
69
70static dbus_bool_t add_watch(DBusWatch *watch, void *data)
71{
Simon Kelley3d8df262005-08-29 12:19:27 +010072 struct watch *w;
73
74 for (w = daemon->watches; w; w = w->next)
75 if (w->watch == watch)
76 return TRUE;
77
Simon Kelley5aabfc72007-08-29 11:24:47 +010078 if (!(w = whine_malloc(sizeof(struct watch))))
Simon Kelley3d8df262005-08-29 12:19:27 +010079 return FALSE;
80
81 w->watch = watch;
82 w->next = daemon->watches;
83 daemon->watches = w;
84
Simon Kelley5aabfc72007-08-29 11:24:47 +010085 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +010086 return TRUE;
87}
88
89static void remove_watch(DBusWatch *watch, void *data)
90{
Simon Kelley3d8df262005-08-29 12:19:27 +010091 struct watch **up, *w;
92
93 for (up = &(daemon->watches), w = daemon->watches; w; w = w->next)
94 if (w->watch == watch)
95 {
96 *up = w->next;
97 free(w);
98 }
99 else
100 up = &(w->next);
Simon Kelley5aabfc72007-08-29 11:24:47 +0100101
102 w = data; /* no warning */
Simon Kelley3d8df262005-08-29 12:19:27 +0100103}
104
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100105static void add_update_server(union mysockaddr *addr,
106 union mysockaddr *source_addr,
107 const char *interface,
108 const char *domain)
109{
110 struct server *serv;
111
112 /* See if there is a suitable candidate, and unmark */
113 for (serv = daemon->servers; serv; serv = serv->next)
114 if ((serv->flags & SERV_FROM_DBUS) &&
115 (serv->flags & SERV_MARK))
116 {
117 if (domain)
118 {
119 if (!(serv->flags & SERV_HAS_DOMAIN) || !hostname_isequal(domain, serv->domain))
120 continue;
121 }
122 else
123 {
124 if (serv->flags & SERV_HAS_DOMAIN)
125 continue;
126 }
127
128 serv->flags &= ~SERV_MARK;
129
130 break;
131 }
132
133 if (!serv && (serv = whine_malloc(sizeof (struct server))))
134 {
135 /* Not found, create a new one. */
136 memset(serv, 0, sizeof(struct server));
137
138 if (domain && !(serv->domain = whine_malloc(strlen(domain)+1)))
139 {
140 free(serv);
141 serv = NULL;
142 }
143 else
144 {
145 serv->next = daemon->servers;
146 daemon->servers = serv;
147 serv->flags = SERV_FROM_DBUS;
148 if (domain)
149 {
150 strcpy(serv->domain, domain);
151 serv->flags |= SERV_HAS_DOMAIN;
152 }
153 }
154 }
155
156 if (serv)
157 {
158 if (interface)
159 strcpy(serv->interface, interface);
160 else
161 serv->interface[0] = 0;
162
163 if (source_addr->in.sin_family == AF_INET &&
164 addr->in.sin_addr.s_addr == 0 &&
165 serv->domain)
166 serv->flags |= SERV_NO_ADDR;
167 else
168 {
169 serv->flags &= ~SERV_NO_ADDR;
170 serv->addr = *addr;
171 serv->source_addr = *source_addr;
172 }
173 }
174}
175
176static void mark_dbus(void)
177{
178 struct server *serv;
179
180 /* mark everything from DBUS */
181 for (serv = daemon->servers; serv; serv = serv->next)
182 if (serv->flags & SERV_FROM_DBUS)
183 serv->flags |= SERV_MARK;
184}
185
186static void cleanup_dbus()
Simon Kelley3d8df262005-08-29 12:19:27 +0100187{
188 struct server *serv, *tmp, **up;
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100189
190 /* unlink and free anything still marked. */
191 for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp)
192 {
193 tmp = serv->next;
194 if (serv->flags & SERV_MARK)
195 {
196 server_gone(serv);
197 *up = serv->next;
198 if (serv->domain)
199 free(serv->domain);
200 free(serv);
201 }
202 else
203 up = &serv->next;
204 }
205}
206
207static void dbus_read_servers(DBusMessage *message)
208{
Simon Kelley3d8df262005-08-29 12:19:27 +0100209 DBusMessageIter iter;
210 union mysockaddr addr, source_addr;
211 char *domain;
212
213 dbus_message_iter_init(message, &iter);
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100214
215 mark_dbus();
216
Simon Kelley3d8df262005-08-29 12:19:27 +0100217 while (1)
218 {
219 int skip = 0;
220
221 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32)
222 {
223 u32 a;
224
225 dbus_message_iter_get_basic(&iter, &a);
226 dbus_message_iter_next (&iter);
227
228#ifdef HAVE_SOCKADDR_SA_LEN
229 source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in);
230#endif
231 addr.in.sin_addr.s_addr = ntohl(a);
232 source_addr.in.sin_family = addr.in.sin_family = AF_INET;
233 addr.in.sin_port = htons(NAMESERVER_PORT);
234 source_addr.in.sin_addr.s_addr = INADDR_ANY;
235 source_addr.in.sin_port = htons(daemon->query_port);
236 }
237 else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE)
238 {
239 unsigned char p[sizeof(struct in6_addr)];
240 unsigned int i;
241
242 skip = 1;
243
244 for(i = 0; i < sizeof(struct in6_addr); i++)
245 {
246 dbus_message_iter_get_basic(&iter, &p[i]);
247 dbus_message_iter_next (&iter);
248 if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE)
249 break;
250 }
251
252#ifndef HAVE_IPV6
Simon Kelleyf2621c72007-04-29 19:47:21 +0100253 my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support"));
Simon Kelley3d8df262005-08-29 12:19:27 +0100254#else
255 if (i == sizeof(struct in6_addr)-1)
256 {
Simon Kelley824af852008-02-12 20:43:05 +0000257 memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr));
Simon Kelley3d8df262005-08-29 12:19:27 +0100258#ifdef HAVE_SOCKADDR_SA_LEN
Simon Kelley9e038942008-05-30 20:06:34 +0100259 source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6);
Simon Kelley3d8df262005-08-29 12:19:27 +0100260#endif
261 source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6;
262 addr.in6.sin6_port = htons(NAMESERVER_PORT);
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100263 source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0;
264 source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0;
Simon Kelley3d8df262005-08-29 12:19:27 +0100265 source_addr.in6.sin6_addr = in6addr_any;
266 source_addr.in6.sin6_port = htons(daemon->query_port);
267 skip = 0;
268 }
269#endif
270 }
271 else
272 /* At the end */
273 break;
274
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100275 /* process each domain */
Simon Kelley3d8df262005-08-29 12:19:27 +0100276 do {
277 if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING)
278 {
279 dbus_message_iter_get_basic(&iter, &domain);
280 dbus_message_iter_next (&iter);
281 }
282 else
283 domain = NULL;
284
285 if (!skip)
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100286 add_update_server(&addr, &source_addr, NULL, domain);
287
288 } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING);
Simon Kelley3d8df262005-08-29 12:19:27 +0100289 }
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100290
Simon Kelley3d8df262005-08-29 12:19:27 +0100291 /* unlink and free anything still marked. */
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100292 cleanup_dbus();
293}
294
295static DBusMessage* dbus_read_servers_ex(DBusMessage *message)
296{
297 DBusMessageIter iter, array_iter, string_iter;
298 DBusMessage *error = NULL;
299 const char *addr_err;
300
301 if (!dbus_message_iter_init(message, &iter))
Simon Kelley3d8df262005-08-29 12:19:27 +0100302 {
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100303 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
304 "Failed to initialize dbus message iter");
Simon Kelley3d8df262005-08-29 12:19:27 +0100305 }
306
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100307 /* check that the message contains an array of arrays */
308 if ((dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) ||
309 (dbus_message_iter_get_element_type(&iter) != DBUS_TYPE_ARRAY))
310 {
311 return dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
312 "Expected array of string arrays");
313 }
314
315 mark_dbus();
316
317 /* array_iter points to each "as" element in the outer array */
318 dbus_message_iter_recurse(&iter, &array_iter);
319 while (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_INVALID)
320 {
321 const char *str = NULL;
322 union mysockaddr addr, source_addr;
323 char interface[IF_NAMESIZE];
324 char *str_addr;
325
326 /* check the types of the struct and its elements */
327 if ((dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY) ||
328 (dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_STRING))
329 {
330 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
331 "Expected inner array of strings");
332 break;
333 }
334
335 /* string_iter points to each "s" element in the inner array */
336 dbus_message_iter_recurse(&array_iter, &string_iter);
337 if (dbus_message_iter_get_arg_type(&string_iter) != DBUS_TYPE_STRING)
338 {
339 /* no IP address given */
340 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
341 "Expected IP address");
342 break;
343 }
344
345 dbus_message_iter_get_basic(&string_iter, &str);
346 if (!str || !strlen (str))
347 {
348 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
349 "Empty IP address");
350 break;
351 }
352
353 memset(&addr, 0, sizeof(addr));
354 memset(&source_addr, 0, sizeof(source_addr));
355 memset(&interface, 0, sizeof(interface));
356
357 /* dup the string because it gets modified during parsing */
358 str_addr = strdup(str);
359 if (!str_addr)
360 {
361 error = dbus_message_new_error(message, DBUS_ERROR_INVALID_ARGS,
362 "Out of memory parsing IP address");
363 break;
364 }
365
366 /* parse the IP address */
367 addr_err = parse_server(str_addr, &addr, &source_addr, &interface, NULL);
368 free(str_addr);
369
370 if (addr_err)
371 {
372 error = dbus_message_new_error_printf(message, DBUS_ERROR_INVALID_ARGS,
373 "Invalid IP address '%s': %s",
374 str, addr_err);
375 break;
376 }
377
378 /* jump past the address to the domain list (if any) */
379 dbus_message_iter_next (&string_iter);
380
381 /* parse domains and add each server/domain pair to the list */
382 do {
383 str = NULL;
384 if (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING)
385 dbus_message_iter_get_basic(&string_iter, &str);
386 dbus_message_iter_next (&string_iter);
387
388 add_update_server(&addr, &source_addr, interface, str);
389 } while (dbus_message_iter_get_arg_type(&string_iter) == DBUS_TYPE_STRING);
390
391 /* jump to next element in outer array */
392 dbus_message_iter_next(&array_iter);
393 }
394
395 cleanup_dbus();
396
397 return error;
Simon Kelley3d8df262005-08-29 12:19:27 +0100398}
399
Simon Kelley5e9e0ef2006-04-17 14:24:29 +0100400DBusHandlerResult message_handler(DBusConnection *connection,
401 DBusMessage *message,
402 void *user_data)
Simon Kelley3d8df262005-08-29 12:19:27 +0100403{
404 char *method = (char *)dbus_message_get_member(message);
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100405 DBusMessage *reply = NULL;
Simon Kelley5aabfc72007-08-29 11:24:47 +0100406
Simon Kelley73a08a22009-02-05 20:28:08 +0000407 if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
408 {
Simon Kelleyad094272012-08-10 17:10:54 +0100409 /* string length: "%s" provides space for termination zero */
410 if (!introspection_xml &&
411 (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name))))
412 sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name);
413
414 if (introspection_xml)
415 {
416 reply = dbus_message_new_method_return(message);
Simon Kelleyad094272012-08-10 17:10:54 +0100417 dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID);
Simon Kelleyad094272012-08-10 17:10:54 +0100418 }
Simon Kelley73a08a22009-02-05 20:28:08 +0000419 }
420 else if (strcmp(method, "GetVersion") == 0)
Simon Kelley3d8df262005-08-29 12:19:27 +0100421 {
422 char *v = VERSION;
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100423 reply = dbus_message_new_method_return(message);
Simon Kelley3d8df262005-08-29 12:19:27 +0100424
425 dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID);
Simon Kelley3d8df262005-08-29 12:19:27 +0100426 }
427 else if (strcmp(method, "SetServers") == 0)
428 {
Simon Kelleyf2621c72007-04-29 19:47:21 +0100429 my_syslog(LOG_INFO, _("setting upstream servers from DBus"));
Simon Kelley5aabfc72007-08-29 11:24:47 +0100430 dbus_read_servers(message);
431 check_servers();
Simon Kelley3d8df262005-08-29 12:19:27 +0100432 }
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100433 else if (strcmp(method, "SetServersEx") == 0)
434 {
435 my_syslog(LOG_INFO, _("setting upstream servers from DBus"));
436 reply = dbus_read_servers_ex(message);
437 check_servers();
438 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100439 else if (strcmp(method, "ClearCache") == 0)
Simon Kelley5aabfc72007-08-29 11:24:47 +0100440 clear_cache_and_reload(dnsmasq_time());
Simon Kelley3d8df262005-08-29 12:19:27 +0100441 else
442 return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
443
Simon Kelley5aabfc72007-08-29 11:24:47 +0100444 method = user_data; /* no warning */
445
Simon Kelleyfaafb3f2012-09-20 14:17:39 +0100446 /* If no reply or no error, return nothing */
447 if (!reply)
448 reply = dbus_message_new_method_return(message);
449
450 if (reply)
451 {
452 dbus_connection_send (connection, reply, NULL);
453 dbus_message_unref (reply);
454 }
455
Simon Kelley3d8df262005-08-29 12:19:27 +0100456 return (DBUS_HANDLER_RESULT_HANDLED);
Simon Kelley3d8df262005-08-29 12:19:27 +0100457}
458
459
460/* returns NULL or error message, may fail silently if dbus daemon not yet up. */
Simon Kelley5aabfc72007-08-29 11:24:47 +0100461char *dbus_init(void)
Simon Kelley3d8df262005-08-29 12:19:27 +0100462{
463 DBusConnection *connection = NULL;
464 DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL };
465 DBusError dbus_error;
466 DBusMessage *message;
467
468 dbus_error_init (&dbus_error);
469 if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error)))
470 return NULL;
Simon Kelley7cebd202006-05-06 14:13:33 +0100471
Simon Kelley3d8df262005-08-29 12:19:27 +0100472 dbus_connection_set_exit_on_disconnect(connection, FALSE);
473 dbus_connection_set_watch_functions(connection, add_watch, remove_watch,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100474 NULL, NULL, NULL);
Simon Kelley3d8df262005-08-29 12:19:27 +0100475 dbus_error_init (&dbus_error);
Simon Kelleyad094272012-08-10 17:10:54 +0100476 dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error);
Simon Kelley3d8df262005-08-29 12:19:27 +0100477 if (dbus_error_is_set (&dbus_error))
478 return (char *)dbus_error.message;
479
480 if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH,
Simon Kelley5aabfc72007-08-29 11:24:47 +0100481 &dnsmasq_vtable, NULL))
Simon Kelleyb8187c82005-11-26 21:46:27 +0000482 return _("could not register a DBus message handler");
Simon Kelley3d8df262005-08-29 12:19:27 +0100483
484 daemon->dbus = connection;
485
Simon Kelleyad094272012-08-10 17:10:54 +0100486 if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up")))
Simon Kelley9009d742008-11-14 20:04:27 +0000487 {
488 dbus_connection_send(connection, message, NULL);
489 dbus_message_unref(message);
490 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100491
492 return NULL;
493}
494
495
Simon Kelley5aabfc72007-08-29 11:24:47 +0100496void set_dbus_listeners(int *maxfdp,
Simon Kelley16972692006-10-16 20:04:18 +0100497 fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100498{
499 struct watch *w;
500
501 for (w = daemon->watches; w; w = w->next)
502 if (dbus_watch_get_enabled(w->watch))
503 {
504 unsigned int flags = dbus_watch_get_flags(w->watch);
Simon Kelley824af852008-02-12 20:43:05 +0000505 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100506
Simon Kelley16972692006-10-16 20:04:18 +0100507 bump_maxfd(fd, maxfdp);
Simon Kelley3d8df262005-08-29 12:19:27 +0100508
509 if (flags & DBUS_WATCH_READABLE)
510 FD_SET(fd, rset);
511
512 if (flags & DBUS_WATCH_WRITABLE)
513 FD_SET(fd, wset);
514
515 FD_SET(fd, eset);
516 }
Simon Kelley3d8df262005-08-29 12:19:27 +0100517}
518
Simon Kelley5aabfc72007-08-29 11:24:47 +0100519void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset)
Simon Kelley3d8df262005-08-29 12:19:27 +0100520{
521 DBusConnection *connection = (DBusConnection *)daemon->dbus;
522 struct watch *w;
523
524 for (w = daemon->watches; w; w = w->next)
525 if (dbus_watch_get_enabled(w->watch))
526 {
527 unsigned int flags = 0;
Simon Kelley824af852008-02-12 20:43:05 +0000528 int fd = dbus_watch_get_unix_fd(w->watch);
Simon Kelley3d8df262005-08-29 12:19:27 +0100529
530 if (FD_ISSET(fd, rset))
531 flags |= DBUS_WATCH_READABLE;
532
533 if (FD_ISSET(fd, wset))
534 flags |= DBUS_WATCH_WRITABLE;
535
536 if (FD_ISSET(fd, eset))
537 flags |= DBUS_WATCH_ERROR;
538
539 if (flags != 0)
540 dbus_watch_handle(w->watch, flags);
541 }
542
543 if (connection)
544 {
545 dbus_connection_ref (connection);
546 while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS);
547 dbus_connection_unref (connection);
548 }
549}
550
Simon Kelley316e2732010-01-22 20:16:09 +0000551#ifdef HAVE_DHCP
Simon Kelley1f15b812009-10-13 17:49:32 +0100552void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname)
Simon Kelley9009d742008-11-14 20:04:27 +0000553{
554 DBusConnection *connection = (DBusConnection *)daemon->dbus;
555 DBusMessage* message = NULL;
556 DBusMessageIter args;
Simon Kelley6ffeff82012-03-07 10:32:35 +0000557 char *action_str, *mac = daemon->namebuff;
Simon Kelley1f15b812009-10-13 17:49:32 +0100558 unsigned char *p;
559 int i;
Simon Kelley9009d742008-11-14 20:04:27 +0000560
561 if (!connection)
562 return;
Simon Kelley1f15b812009-10-13 17:49:32 +0100563
564 if (!hostname)
565 hostname = "";
566
Simon Kelley6ffeff82012-03-07 10:32:35 +0000567#ifdef HAVE_DHCP6
568 if (lease->flags & (LEASE_TA | LEASE_NA))
569 {
570 print_mac(mac, lease->clid, lease->clid_len);
571 inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN);
572 }
573 else
574#endif
575 {
576 p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len,
577 lease->hwaddr, lease->clid_len, lease->clid, &i);
578 print_mac(mac, p, i);
579 inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN);
580 }
581
Simon Kelley9009d742008-11-14 20:04:27 +0000582 if (action == ACTION_DEL)
583 action_str = "DhcpLeaseDeleted";
584 else if (action == ACTION_ADD)
585 action_str = "DhcpLeaseAdded";
586 else if (action == ACTION_OLD)
587 action_str = "DhcpLeaseUpdated";
588 else
589 return;
590
Simon Kelleyad094272012-08-10 17:10:54 +0100591 if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str)))
Simon Kelley9009d742008-11-14 20:04:27 +0000592 return;
593
594 dbus_message_iter_init_append(message, &args);
Simon Kelley6ffeff82012-03-07 10:32:35 +0000595
596 if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) &&
Simon Kelley9009d742008-11-14 20:04:27 +0000597 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) &&
598 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname))
599 dbus_connection_send(connection, message, NULL);
600
601 dbus_message_unref(message);
602}
Simon Kelley316e2732010-01-22 20:16:09 +0000603#endif
Simon Kelley9009d742008-11-14 20:04:27 +0000604
Simon Kelley3d8df262005-08-29 12:19:27 +0100605#endif