1 /* dnsmasq is Copyright (c) 2000-2015 Simon Kelley
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
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
8 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.
13 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/>.
21 #include <dbus/dbus.h>
23 const char* introspection_xml_template
=
24 "<!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"
32 " <interface name=\"%s\">\n"
33 " <method name=\"ClearCache\">\n"
35 " <method name=\"GetVersion\">\n"
36 " <arg name=\"version\" direction=\"out\" type=\"s\"/>\n"
39 " <method name=\"GetLoopServers\">\n"
40 " <arg name=\"server\" direction=\"out\" type=\"as\"/>\n"
43 " <method name=\"SetServers\">\n"
44 " <arg name=\"servers\" direction=\"in\" type=\"av\"/>\n"
46 " <method name=\"SetDomainServers\">\n"
47 " <arg name=\"servers\" direction=\"in\" type=\"as\"/>\n"
49 " <method name=\"SetServersEx\">\n"
50 " <arg name=\"servers\" direction=\"in\" type=\"aas\"/>\n"
52 " <method name=\"SetFilterWin2KOption\">\n"
53 " <arg name=\"filterwin2k\" direction=\"in\" type=\"b\"/>\n"
55 " <method name=\"SetBogusPrivOption\">\n"
56 " <arg name=\"boguspriv\" direction=\"in\" type=\"b\"/>\n"
58 " <signal name=\"DhcpLeaseAdded\">\n"
59 " <arg name=\"ipaddr\" type=\"s\"/>\n"
60 " <arg name=\"hwaddr\" type=\"s\"/>\n"
61 " <arg name=\"hostname\" type=\"s\"/>\n"
63 " <signal name=\"DhcpLeaseDeleted\">\n"
64 " <arg name=\"ipaddr\" type=\"s\"/>\n"
65 " <arg name=\"hwaddr\" type=\"s\"/>\n"
66 " <arg name=\"hostname\" type=\"s\"/>\n"
68 " <signal name=\"DhcpLeaseUpdated\">\n"
69 " <arg name=\"ipaddr\" type=\"s\"/>\n"
70 " <arg name=\"hwaddr\" type=\"s\"/>\n"
71 " <arg name=\"hostname\" type=\"s\"/>\n"
74 " <method name=\"AddDhcpLease\">\n"
75 " <arg name=\"ipaddr\" type=\"s\"/>\n"
76 " <arg name=\"hwaddr\" type=\"s\"/>\n"
77 " <arg name=\"hostname\" type=\"ay\"/>\n"
78 " <arg name=\"clid\" type=\"ay\"/>\n"
79 " <arg name=\"lease_duration\" type=\"u\"/>\n"
80 " <arg name=\"ia_id\" type=\"u\"/>\n"
81 " <arg name=\"is_temporary\" type=\"b\"/>\n"
83 " <method name=\"DeleteDhcpLease\">\n"
84 " <arg name=\"ipaddr\" type=\"s\"/>\n"
85 " <arg name=\"success\" type=\"b\" direction=\"out\"/>\n"
91 static char *introspection_xml
= NULL
;
99 static dbus_bool_t
add_watch(DBusWatch
*watch
, void *data
)
103 for (w
= daemon
->watches
; w
; w
= w
->next
)
104 if (w
->watch
== watch
)
107 if (!(w
= whine_malloc(sizeof(struct watch
))))
111 w
->next
= daemon
->watches
;
114 w
= data
; /* no warning */
118 static void remove_watch(DBusWatch
*watch
, void *data
)
120 struct watch
**up
, *w
, *tmp
;
122 for (up
= &(daemon
->watches
), w
= daemon
->watches
; w
; w
= tmp
)
125 if (w
->watch
== watch
)
134 w
= data
; /* no warning */
137 static void dbus_read_servers(DBusMessage
*message
)
139 DBusMessageIter iter
;
140 union mysockaddr addr
, source_addr
;
143 dbus_message_iter_init(message
, &iter
);
145 mark_servers(SERV_FROM_DBUS
);
151 if (dbus_message_iter_get_arg_type(&iter
) == DBUS_TYPE_UINT32
)
155 dbus_message_iter_get_basic(&iter
, &a
);
156 dbus_message_iter_next (&iter
);
158 #ifdef HAVE_SOCKADDR_SA_LEN
159 source_addr
.in
.sin_len
= addr
.in
.sin_len
= sizeof(struct sockaddr_in
);
161 addr
.in
.sin_addr
.s_addr
= ntohl(a
);
162 source_addr
.in
.sin_family
= addr
.in
.sin_family
= AF_INET
;
163 addr
.in
.sin_port
= htons(NAMESERVER_PORT
);
164 source_addr
.in
.sin_addr
.s_addr
= INADDR_ANY
;
165 source_addr
.in
.sin_port
= htons(daemon
->query_port
);
167 else if (dbus_message_iter_get_arg_type(&iter
) == DBUS_TYPE_BYTE
)
169 unsigned char p
[sizeof(struct in6_addr
)];
174 for(i
= 0; i
< sizeof(struct in6_addr
); i
++)
176 dbus_message_iter_get_basic(&iter
, &p
[i
]);
177 dbus_message_iter_next (&iter
);
178 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_BYTE
)
186 my_syslog(LOG_WARNING
, _("attempt to set an IPv6 server address via DBus - no IPv6 support"));
188 if (i
== sizeof(struct in6_addr
))
190 memcpy(&addr
.in6
.sin6_addr
, p
, sizeof(struct in6_addr
));
191 #ifdef HAVE_SOCKADDR_SA_LEN
192 source_addr
.in6
.sin6_len
= addr
.in6
.sin6_len
= sizeof(struct sockaddr_in6
);
194 source_addr
.in6
.sin6_family
= addr
.in6
.sin6_family
= AF_INET6
;
195 addr
.in6
.sin6_port
= htons(NAMESERVER_PORT
);
196 source_addr
.in6
.sin6_flowinfo
= addr
.in6
.sin6_flowinfo
= 0;
197 source_addr
.in6
.sin6_scope_id
= addr
.in6
.sin6_scope_id
= 0;
198 source_addr
.in6
.sin6_addr
= in6addr_any
;
199 source_addr
.in6
.sin6_port
= htons(daemon
->query_port
);
208 /* process each domain */
210 if (dbus_message_iter_get_arg_type(&iter
) == DBUS_TYPE_STRING
)
212 dbus_message_iter_get_basic(&iter
, &domain
);
213 dbus_message_iter_next (&iter
);
219 add_update_server(SERV_FROM_DBUS
, &addr
, &source_addr
, NULL
, domain
);
221 } while (dbus_message_iter_get_arg_type(&iter
) == DBUS_TYPE_STRING
);
224 /* unlink and free anything still marked. */
229 static DBusMessage
*dbus_reply_server_loop(DBusMessage
*message
)
231 DBusMessageIter args
, args_iter
;
233 DBusMessage
*reply
= dbus_message_new_method_return(message
);
235 dbus_message_iter_init_append (reply
, &args
);
236 dbus_message_iter_open_container (&args
, DBUS_TYPE_ARRAY
,DBUS_TYPE_STRING_AS_STRING
, &args_iter
);
238 for (serv
= daemon
->servers
; serv
; serv
= serv
->next
)
239 if (serv
->flags
& SERV_LOOP
)
241 prettyprint_addr(&serv
->addr
, daemon
->addrbuff
);
242 dbus_message_iter_append_basic (&args_iter
, DBUS_TYPE_STRING
, &daemon
->addrbuff
);
245 dbus_message_iter_close_container (&args
, &args_iter
);
251 static DBusMessage
* dbus_read_servers_ex(DBusMessage
*message
, int strings
)
253 DBusMessageIter iter
, array_iter
, string_iter
;
254 DBusMessage
*error
= NULL
;
255 const char *addr_err
;
258 if (!dbus_message_iter_init(message
, &iter
))
260 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
261 "Failed to initialize dbus message iter");
264 /* check that the message contains an array of arrays */
265 if ((dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
) ||
266 (dbus_message_iter_get_element_type(&iter
) != (strings
? DBUS_TYPE_STRING
: DBUS_TYPE_ARRAY
)))
268 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
269 strings
? "Expected array of string" : "Expected array of string arrays");
272 mark_servers(SERV_FROM_DBUS
);
274 /* array_iter points to each "as" element in the outer array */
275 dbus_message_iter_recurse(&iter
, &array_iter
);
276 while (dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_INVALID
)
278 const char *str
= NULL
;
279 union mysockaddr addr
, source_addr
;
281 char interface
[IF_NAMESIZE
];
282 char *str_addr
, *str_domain
= NULL
;
286 dbus_message_iter_get_basic(&array_iter
, &str
);
287 if (!str
|| !strlen (str
))
289 error
= dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
294 /* dup the string because it gets modified during parsing */
297 if (!(dup
= str_domain
= whine_malloc(strlen(str
)+1)))
300 strcpy(str_domain
, str
);
302 /* point to address part of old string for error message */
303 if ((str_addr
= strrchr(str
, '/')))
306 if ((str_addr
= strrchr(str_domain
, '/')))
308 if (*str_domain
!= '/' || str_addr
== str_domain
)
310 error
= dbus_message_new_error_printf(message
,
311 DBUS_ERROR_INVALID_ARGS
,
312 "No domain terminator '%s'",
321 str_addr
= str_domain
;
329 /* check the types of the struct and its elements */
330 if ((dbus_message_iter_get_arg_type(&array_iter
) != DBUS_TYPE_ARRAY
) ||
331 (dbus_message_iter_get_element_type(&array_iter
) != DBUS_TYPE_STRING
))
333 error
= dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
334 "Expected inner array of strings");
338 /* string_iter points to each "s" element in the inner array */
339 dbus_message_iter_recurse(&array_iter
, &string_iter
);
340 if (dbus_message_iter_get_arg_type(&string_iter
) != DBUS_TYPE_STRING
)
342 /* no IP address given */
343 error
= dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
344 "Expected IP address");
348 dbus_message_iter_get_basic(&string_iter
, &str
);
349 if (!str
|| !strlen (str
))
351 error
= dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
356 /* dup the string because it gets modified during parsing */
359 if (!(dup
= str_addr
= whine_malloc(strlen(str
)+1)))
362 strcpy(str_addr
, str
);
365 memset(&addr
, 0, sizeof(addr
));
366 memset(&source_addr
, 0, sizeof(source_addr
));
367 memset(&interface
, 0, sizeof(interface
));
369 /* parse the IP address */
370 if ((addr_err
= parse_server(str_addr
, &addr
, &source_addr
, (char *) &interface
, &flags
)))
372 error
= dbus_message_new_error_printf(message
, DBUS_ERROR_INVALID_ARGS
,
373 "Invalid IP address '%s': %s",
378 /* 0.0.0.0 for server address == NULL, for Dbus */
379 if (addr
.in
.sin_family
== AF_INET
&&
380 addr
.in
.sin_addr
.s_addr
== 0)
381 flags
|= SERV_NO_ADDR
;
390 if ((p
= strchr(str_domain
, '/')))
396 add_update_server(flags
| SERV_FROM_DBUS
, &addr
, &source_addr
, interface
, str_domain
);
397 } while ((str_domain
= p
));
401 /* jump past the address to the domain list (if any) */
402 dbus_message_iter_next (&string_iter
);
404 /* parse domains and add each server/domain pair to the list */
407 if (dbus_message_iter_get_arg_type(&string_iter
) == DBUS_TYPE_STRING
)
408 dbus_message_iter_get_basic(&string_iter
, &str
);
409 dbus_message_iter_next (&string_iter
);
411 add_update_server(flags
| SERV_FROM_DBUS
, &addr
, &source_addr
, interface
, str
);
412 } while (dbus_message_iter_get_arg_type(&string_iter
) == DBUS_TYPE_STRING
);
415 /* jump to next element in outer array */
416 dbus_message_iter_next(&array_iter
);
427 static DBusMessage
*dbus_set_bool(DBusMessage
*message
, int flag
, char *name
)
429 DBusMessageIter iter
;
432 if (!dbus_message_iter_init(message
, &iter
) || dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_BOOLEAN
)
433 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
, "Expected boolean argument");
435 dbus_message_iter_get_basic(&iter
, &enabled
);
439 my_syslog(LOG_INFO
, "Enabling --%s option from D-Bus", name
);
440 set_option_bool(flag
);
444 my_syslog(LOG_INFO
, "Disabling --%s option from D-Bus", name
);
445 reset_option_bool(flag
);
452 static DBusMessage
*dbus_add_lease(DBusMessage
* message
)
454 struct dhcp_lease
*lease
;
455 const char *ipaddr
, *hwaddr
, *hostname
, *tmp
;
456 const unsigned char* clid
;
457 int clid_len
, hostname_len
, hw_len
, hw_type
;
458 dbus_uint32_t expires
, ia_id
;
459 dbus_bool_t is_temporary
;
460 struct all_addr addr
;
461 time_t now
= dnsmasq_time();
462 unsigned char dhcp_chaddr
[DHCP_CHADDR_MAX
];
464 DBusMessageIter iter
, array_iter
;
465 if (!dbus_message_iter_init(message
, &iter
))
466 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
467 "Failed to initialize dbus message iter");
469 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
470 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
471 "Expected string as first argument");
473 dbus_message_iter_get_basic(&iter
, &ipaddr
);
474 dbus_message_iter_next(&iter
);
476 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
477 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
478 "Expected string as second argument");
480 dbus_message_iter_get_basic(&iter
, &hwaddr
);
481 dbus_message_iter_next(&iter
);
483 if ((dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
) ||
484 (dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_BYTE
))
485 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
486 "Expected byte array as third argument");
488 dbus_message_iter_recurse(&iter
, &array_iter
);
489 dbus_message_iter_get_fixed_array(&array_iter
, &hostname
, &hostname_len
);
490 tmp
= memchr(hostname
, '\0', hostname_len
);
493 if (tmp
== &hostname
[hostname_len
- 1])
496 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
497 "Hostname contains an embedded NUL character");
499 dbus_message_iter_next(&iter
);
501 if ((dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_ARRAY
) ||
502 (dbus_message_iter_get_element_type(&iter
) != DBUS_TYPE_BYTE
))
503 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
504 "Expected byte array as fourth argument");
506 dbus_message_iter_recurse(&iter
, &array_iter
);
507 dbus_message_iter_get_fixed_array(&array_iter
, &clid
, &clid_len
);
508 dbus_message_iter_next(&iter
);
510 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_UINT32
)
511 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
512 "Expected uint32 as fifth argument");
514 dbus_message_iter_get_basic(&iter
, &expires
);
515 dbus_message_iter_next(&iter
);
517 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_UINT32
)
518 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
519 "Expected uint32 as sixth argument");
521 dbus_message_iter_get_basic(&iter
, &ia_id
);
522 dbus_message_iter_next(&iter
);
524 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_BOOLEAN
)
525 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
526 "Expected uint32 as sixth argument");
528 dbus_message_iter_get_basic(&iter
, &is_temporary
);
530 if (inet_pton(AF_INET
, ipaddr
, &addr
.addr
.addr4
))
532 if (ia_id
!= 0 || is_temporary
)
533 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
534 "ia_id and is_temporary must be zero for IPv4 lease");
536 if (!(lease
= lease_find_by_addr(addr
.addr
.addr4
)))
537 lease
= lease4_allocate(addr
.addr
.addr4
);
540 else if (inet_pton(AF_INET6
, ipaddr
, &addr
.addr
.addr6
))
542 if (!(lease
= lease6_find_by_addr(&addr
.addr
.addr6
, 128, 0)))
543 lease
= lease6_allocate(&addr
.addr
.addr6
,
544 is_temporary
? LEASE_TA
: LEASE_NA
);
545 lease_set_iaid(lease
, ia_id
);
549 return dbus_message_new_error_printf(message
, DBUS_ERROR_INVALID_ARGS
,
550 "Invalid IP address '%s'", ipaddr
);
552 hw_len
= parse_hex((char*)hwaddr
, dhcp_chaddr
, DHCP_CHADDR_MAX
, NULL
,
554 if (hw_type
== 0 && hw_len
!= 0)
555 hw_type
= ARPHRD_ETHER
;
557 lease_set_hwaddr(lease
, dhcp_chaddr
, clid
, hw_len
, hw_type
,
559 lease_set_expires(lease
, expires
, now
);
560 if (hostname_len
!= 0)
561 lease_set_hostname(lease
, hostname
, 0, get_domain(lease
->addr
), NULL
);
563 lease_update_file(now
);
569 static DBusMessage
*dbus_del_lease(DBusMessage
* message
)
571 struct dhcp_lease
*lease
;
572 DBusMessageIter iter
;
575 struct all_addr addr
;
577 time_t now
= dnsmasq_time();
579 if (!dbus_message_iter_init(message
, &iter
))
580 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
581 "Failed to initialize dbus message iter");
583 if (dbus_message_iter_get_arg_type(&iter
) != DBUS_TYPE_STRING
)
584 return dbus_message_new_error(message
, DBUS_ERROR_INVALID_ARGS
,
585 "Expected string as first argument");
587 dbus_message_iter_get_basic(&iter
, &ipaddr
);
589 if (inet_pton(AF_INET
, ipaddr
, &addr
.addr
.addr4
))
590 lease
= lease_find_by_addr(addr
.addr
.addr4
);
592 else if (inet_pton(AF_INET6
, ipaddr
, &addr
.addr
.addr6
))
593 lease
= lease6_find_by_addr(&addr
.addr
.addr6
, 128, 0);
596 return dbus_message_new_error_printf(message
, DBUS_ERROR_INVALID_ARGS
,
597 "Invalid IP address '%s'", ipaddr
);
601 lease_prune(lease
, now
);
602 lease_update_file(now
);
608 if ((reply
= dbus_message_new_method_return(message
)))
609 dbus_message_append_args(reply
, DBUS_TYPE_BOOLEAN
, &ret
,
617 DBusHandlerResult
message_handler(DBusConnection
*connection
,
618 DBusMessage
*message
,
621 char *method
= (char *)dbus_message_get_member(message
);
622 DBusMessage
*reply
= NULL
;
623 int clear_cache
= 0, new_servers
= 0;
625 if (dbus_message_is_method_call(message
, DBUS_INTERFACE_INTROSPECTABLE
, "Introspect"))
627 /* string length: "%s" provides space for termination zero */
628 if (!introspection_xml
&&
629 (introspection_xml
= whine_malloc(strlen(introspection_xml_template
) + strlen(daemon
->dbus_name
))))
630 sprintf(introspection_xml
, introspection_xml_template
, daemon
->dbus_name
);
632 if (introspection_xml
)
634 reply
= dbus_message_new_method_return(message
);
635 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &introspection_xml
, DBUS_TYPE_INVALID
);
638 else if (strcmp(method
, "GetVersion") == 0)
641 reply
= dbus_message_new_method_return(message
);
643 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &v
, DBUS_TYPE_INVALID
);
646 else if (strcmp(method
, "GetLoopServers") == 0)
648 reply
= dbus_reply_server_loop(message
);
651 else if (strcmp(method
, "SetServers") == 0)
653 dbus_read_servers(message
);
656 else if (strcmp(method
, "SetServersEx") == 0)
658 reply
= dbus_read_servers_ex(message
, 0);
661 else if (strcmp(method
, "SetDomainServers") == 0)
663 reply
= dbus_read_servers_ex(message
, 1);
666 else if (strcmp(method
, "SetFilterWin2KOption") == 0)
668 reply
= dbus_set_bool(message
, OPT_FILTER
, "filterwin2k");
670 else if (strcmp(method
, "SetBogusPrivOption") == 0)
672 reply
= dbus_set_bool(message
, OPT_BOGUSPRIV
, "bogus-priv");
675 else if (strcmp(method
, "AddDhcpLease") == 0)
677 reply
= dbus_add_lease(message
);
679 else if (strcmp(method
, "DeleteDhcpLease") == 0)
681 reply
= dbus_del_lease(message
);
684 else if (strcmp(method
, "ClearCache") == 0)
687 return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED
);
691 my_syslog(LOG_INFO
, _("setting upstream servers from DBus"));
693 if (option_bool(OPT_RELOAD
))
698 clear_cache_and_reload(dnsmasq_time());
700 method
= user_data
; /* no warning */
702 /* If no reply or no error, return nothing */
704 reply
= dbus_message_new_method_return(message
);
708 dbus_connection_send (connection
, reply
, NULL
);
709 dbus_message_unref (reply
);
712 return (DBUS_HANDLER_RESULT_HANDLED
);
716 /* returns NULL or error message, may fail silently if dbus daemon not yet up. */
717 char *dbus_init(void)
719 DBusConnection
*connection
= NULL
;
720 DBusObjectPathVTable dnsmasq_vtable
= {NULL
, &message_handler
, NULL
, NULL
, NULL
, NULL
};
721 DBusError dbus_error
;
722 DBusMessage
*message
;
724 dbus_error_init (&dbus_error
);
725 if (!(connection
= dbus_bus_get (DBUS_BUS_SYSTEM
, &dbus_error
)))
728 dbus_connection_set_exit_on_disconnect(connection
, FALSE
);
729 dbus_connection_set_watch_functions(connection
, add_watch
, remove_watch
,
731 dbus_error_init (&dbus_error
);
732 dbus_bus_request_name (connection
, daemon
->dbus_name
, 0, &dbus_error
);
733 if (dbus_error_is_set (&dbus_error
))
734 return (char *)dbus_error
.message
;
736 if (!dbus_connection_register_object_path(connection
, DNSMASQ_PATH
,
737 &dnsmasq_vtable
, NULL
))
738 return _("could not register a DBus message handler");
740 daemon
->dbus
= connection
;
742 if ((message
= dbus_message_new_signal(DNSMASQ_PATH
, daemon
->dbus_name
, "Up")))
744 dbus_connection_send(connection
, message
, NULL
);
745 dbus_message_unref(message
);
752 void set_dbus_listeners(int *maxfdp
,
753 fd_set
*rset
, fd_set
*wset
, fd_set
*eset
)
757 for (w
= daemon
->watches
; w
; w
= w
->next
)
758 if (dbus_watch_get_enabled(w
->watch
))
760 unsigned int flags
= dbus_watch_get_flags(w
->watch
);
761 int fd
= dbus_watch_get_unix_fd(w
->watch
);
763 bump_maxfd(fd
, maxfdp
);
765 if (flags
& DBUS_WATCH_READABLE
)
768 if (flags
& DBUS_WATCH_WRITABLE
)
775 void check_dbus_listeners(fd_set
*rset
, fd_set
*wset
, fd_set
*eset
)
777 DBusConnection
*connection
= (DBusConnection
*)daemon
->dbus
;
780 for (w
= daemon
->watches
; w
; w
= w
->next
)
781 if (dbus_watch_get_enabled(w
->watch
))
783 unsigned int flags
= 0;
784 int fd
= dbus_watch_get_unix_fd(w
->watch
);
786 if (FD_ISSET(fd
, rset
))
787 flags
|= DBUS_WATCH_READABLE
;
789 if (FD_ISSET(fd
, wset
))
790 flags
|= DBUS_WATCH_WRITABLE
;
792 if (FD_ISSET(fd
, eset
))
793 flags
|= DBUS_WATCH_ERROR
;
796 dbus_watch_handle(w
->watch
, flags
);
801 dbus_connection_ref (connection
);
802 while (dbus_connection_dispatch (connection
) == DBUS_DISPATCH_DATA_REMAINS
);
803 dbus_connection_unref (connection
);
808 void emit_dbus_signal(int action
, struct dhcp_lease
*lease
, char *hostname
)
810 DBusConnection
*connection
= (DBusConnection
*)daemon
->dbus
;
811 DBusMessage
* message
= NULL
;
812 DBusMessageIter args
;
813 char *action_str
, *mac
= daemon
->namebuff
;
824 if (lease
->flags
& (LEASE_TA
| LEASE_NA
))
826 print_mac(mac
, lease
->clid
, lease
->clid_len
);
827 inet_ntop(AF_INET6
, &lease
->addr6
, daemon
->addrbuff
, ADDRSTRLEN
);
832 p
= extended_hwaddr(lease
->hwaddr_type
, lease
->hwaddr_len
,
833 lease
->hwaddr
, lease
->clid_len
, lease
->clid
, &i
);
834 print_mac(mac
, p
, i
);
835 inet_ntop(AF_INET
, &lease
->addr
, daemon
->addrbuff
, ADDRSTRLEN
);
838 if (action
== ACTION_DEL
)
839 action_str
= "DhcpLeaseDeleted";
840 else if (action
== ACTION_ADD
)
841 action_str
= "DhcpLeaseAdded";
842 else if (action
== ACTION_OLD
)
843 action_str
= "DhcpLeaseUpdated";
847 if (!(message
= dbus_message_new_signal(DNSMASQ_PATH
, daemon
->dbus_name
, action_str
)))
850 dbus_message_iter_init_append(message
, &args
);
852 if (dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &daemon
->addrbuff
) &&
853 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &mac
) &&
854 dbus_message_iter_append_basic(&args
, DBUS_TYPE_STRING
, &hostname
))
855 dbus_connection_send(connection
, message
, NULL
);
857 dbus_message_unref(message
);