Simon Kelley | 5954608 | 2012-01-06 20:02:04 +0000 | [diff] [blame] | 1 | /* dnsmasq is Copyright (c) 2000-2012 Simon Kelley |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 2 | |
| 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 Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 5 | the Free Software Foundation; version 2 dated June, 1991, or |
| 6 | (at your option) version 3 dated 29 June, 2007. |
| 7 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 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. |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 12 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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/>. |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include "dnsmasq.h" |
| 18 | |
| 19 | #ifdef HAVE_DBUS |
| 20 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 21 | #include <dbus/dbus.h> |
| 22 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 23 | const char* introspection_xml_template = |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 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" |
| 30 | " </method>\n" |
| 31 | " </interface>\n" |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 32 | " <interface name=\"%s\">\n" |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 33 | " <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" |
| 41 | " <signal name=\"DhcpLeaseAdded\">\n" |
| 42 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 43 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 44 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 45 | " </signal>\n" |
| 46 | " <signal name=\"DhcpLeaseDeleted\">\n" |
| 47 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 48 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 49 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 50 | " </signal>\n" |
| 51 | " <signal name=\"DhcpLeaseUpdated\">\n" |
| 52 | " <arg name=\"ipaddr\" type=\"s\"/>\n" |
| 53 | " <arg name=\"hwaddr\" type=\"s\"/>\n" |
| 54 | " <arg name=\"hostname\" type=\"s\"/>\n" |
| 55 | " </signal>\n" |
| 56 | " </interface>\n" |
| 57 | "</node>\n"; |
| 58 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 59 | static char *introspection_xml = NULL; |
| 60 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 61 | struct watch { |
| 62 | DBusWatch *watch; |
| 63 | struct watch *next; |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | static dbus_bool_t add_watch(DBusWatch *watch, void *data) |
| 68 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 69 | struct watch *w; |
| 70 | |
| 71 | for (w = daemon->watches; w; w = w->next) |
| 72 | if (w->watch == watch) |
| 73 | return TRUE; |
| 74 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 75 | if (!(w = whine_malloc(sizeof(struct watch)))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 76 | return FALSE; |
| 77 | |
| 78 | w->watch = watch; |
| 79 | w->next = daemon->watches; |
| 80 | daemon->watches = w; |
| 81 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 82 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 83 | return TRUE; |
| 84 | } |
| 85 | |
| 86 | static void remove_watch(DBusWatch *watch, void *data) |
| 87 | { |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 88 | struct watch **up, *w; |
| 89 | |
| 90 | for (up = &(daemon->watches), w = daemon->watches; w; w = w->next) |
| 91 | if (w->watch == watch) |
| 92 | { |
| 93 | *up = w->next; |
| 94 | free(w); |
| 95 | } |
| 96 | else |
| 97 | up = &(w->next); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 98 | |
| 99 | w = data; /* no warning */ |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 102 | static void dbus_read_servers(DBusMessage *message) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 103 | { |
| 104 | struct server *serv, *tmp, **up; |
| 105 | DBusMessageIter iter; |
| 106 | union mysockaddr addr, source_addr; |
| 107 | char *domain; |
| 108 | |
| 109 | dbus_message_iter_init(message, &iter); |
| 110 | |
| 111 | /* mark everything from DBUS */ |
| 112 | for (serv = daemon->servers; serv; serv = serv->next) |
| 113 | if (serv->flags & SERV_FROM_DBUS) |
| 114 | serv->flags |= SERV_MARK; |
| 115 | |
| 116 | while (1) |
| 117 | { |
| 118 | int skip = 0; |
| 119 | |
| 120 | if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_UINT32) |
| 121 | { |
| 122 | u32 a; |
| 123 | |
| 124 | dbus_message_iter_get_basic(&iter, &a); |
| 125 | dbus_message_iter_next (&iter); |
| 126 | |
| 127 | #ifdef HAVE_SOCKADDR_SA_LEN |
| 128 | source_addr.in.sin_len = addr.in.sin_len = sizeof(struct sockaddr_in); |
| 129 | #endif |
| 130 | addr.in.sin_addr.s_addr = ntohl(a); |
| 131 | source_addr.in.sin_family = addr.in.sin_family = AF_INET; |
| 132 | addr.in.sin_port = htons(NAMESERVER_PORT); |
| 133 | source_addr.in.sin_addr.s_addr = INADDR_ANY; |
| 134 | source_addr.in.sin_port = htons(daemon->query_port); |
| 135 | } |
| 136 | else if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_BYTE) |
| 137 | { |
| 138 | unsigned char p[sizeof(struct in6_addr)]; |
| 139 | unsigned int i; |
| 140 | |
| 141 | skip = 1; |
| 142 | |
| 143 | for(i = 0; i < sizeof(struct in6_addr); i++) |
| 144 | { |
| 145 | dbus_message_iter_get_basic(&iter, &p[i]); |
| 146 | dbus_message_iter_next (&iter); |
| 147 | if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_BYTE) |
| 148 | break; |
| 149 | } |
| 150 | |
| 151 | #ifndef HAVE_IPV6 |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 152 | my_syslog(LOG_WARNING, _("attempt to set an IPv6 server address via DBus - no IPv6 support")); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 153 | #else |
| 154 | if (i == sizeof(struct in6_addr)-1) |
| 155 | { |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 156 | memcpy(&addr.in6.sin6_addr, p, sizeof(struct in6_addr)); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 157 | #ifdef HAVE_SOCKADDR_SA_LEN |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 158 | source_addr.in6.sin6_len = addr.in6.sin6_len = sizeof(struct sockaddr_in6); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 159 | #endif |
| 160 | source_addr.in6.sin6_family = addr.in6.sin6_family = AF_INET6; |
| 161 | addr.in6.sin6_port = htons(NAMESERVER_PORT); |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 162 | source_addr.in6.sin6_flowinfo = addr.in6.sin6_flowinfo = 0; |
| 163 | source_addr.in6.sin6_scope_id = addr.in6.sin6_scope_id = 0; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 164 | source_addr.in6.sin6_addr = in6addr_any; |
| 165 | source_addr.in6.sin6_port = htons(daemon->query_port); |
| 166 | skip = 0; |
| 167 | } |
| 168 | #endif |
| 169 | } |
| 170 | else |
| 171 | /* At the end */ |
| 172 | break; |
| 173 | |
| 174 | do { |
| 175 | if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING) |
| 176 | { |
| 177 | dbus_message_iter_get_basic(&iter, &domain); |
| 178 | dbus_message_iter_next (&iter); |
| 179 | } |
| 180 | else |
| 181 | domain = NULL; |
| 182 | |
| 183 | if (!skip) |
| 184 | { |
| 185 | /* See if this is already there, and unmark */ |
| 186 | for (serv = daemon->servers; serv; serv = serv->next) |
| 187 | if ((serv->flags & SERV_FROM_DBUS) && |
| 188 | (serv->flags & SERV_MARK)) |
| 189 | { |
| 190 | if (!(serv->flags & SERV_HAS_DOMAIN) && !domain) |
| 191 | { |
| 192 | serv->flags &= ~SERV_MARK; |
| 193 | break; |
| 194 | } |
| 195 | if ((serv->flags & SERV_HAS_DOMAIN) && |
| 196 | domain && |
| 197 | hostname_isequal(domain, serv->domain)) |
| 198 | { |
| 199 | serv->flags &= ~SERV_MARK; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 204 | if (!serv && (serv = whine_malloc(sizeof (struct server)))) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 205 | { |
| 206 | /* Not found, create a new one. */ |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 207 | memset(serv, 0, sizeof(struct server)); |
| 208 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 209 | if (domain) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 210 | serv->domain = whine_malloc(strlen(domain)+1); |
Simon Kelley | 9e03894 | 2008-05-30 20:06:34 +0100 | [diff] [blame] | 211 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 212 | if (domain && !serv->domain) |
| 213 | { |
| 214 | free(serv); |
| 215 | serv = NULL; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | serv->next = daemon->servers; |
| 220 | daemon->servers = serv; |
| 221 | serv->flags = SERV_FROM_DBUS; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 222 | if (domain) |
| 223 | { |
| 224 | strcpy(serv->domain, domain); |
| 225 | serv->flags |= SERV_HAS_DOMAIN; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if (serv) |
| 231 | { |
| 232 | if (source_addr.in.sin_family == AF_INET && |
| 233 | addr.in.sin_addr.s_addr == 0 && |
| 234 | serv->domain) |
| 235 | serv->flags |= SERV_NO_ADDR; |
| 236 | else |
| 237 | { |
| 238 | serv->flags &= ~SERV_NO_ADDR; |
| 239 | serv->addr = addr; |
| 240 | serv->source_addr = source_addr; |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | } while (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_STRING); |
| 245 | } |
| 246 | |
| 247 | /* unlink and free anything still marked. */ |
| 248 | for (serv = daemon->servers, up = &daemon->servers; serv; serv = tmp) |
| 249 | { |
| 250 | tmp = serv->next; |
| 251 | if (serv->flags & SERV_MARK) |
| 252 | { |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 253 | server_gone(serv); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 254 | *up = serv->next; |
| 255 | free(serv); |
| 256 | } |
| 257 | else |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 258 | up = &serv->next; |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | } |
| 262 | |
Simon Kelley | 5e9e0ef | 2006-04-17 14:24:29 +0100 | [diff] [blame] | 263 | DBusHandlerResult message_handler(DBusConnection *connection, |
| 264 | DBusMessage *message, |
| 265 | void *user_data) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 266 | { |
| 267 | char *method = (char *)dbus_message_get_member(message); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 268 | |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 269 | if (dbus_message_is_method_call(message, DBUS_INTERFACE_INTROSPECTABLE, "Introspect")) |
| 270 | { |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 271 | DBusMessage *reply; |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 272 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 273 | /* string length: "%s" provides space for termination zero */ |
| 274 | if (!introspection_xml && |
| 275 | (introspection_xml = whine_malloc(strlen(introspection_xml_template) + strlen(daemon->dbus_name)))) |
| 276 | sprintf(introspection_xml, introspection_xml_template, daemon->dbus_name); |
| 277 | |
| 278 | if (introspection_xml) |
| 279 | { |
| 280 | reply = dbus_message_new_method_return(message); |
| 281 | |
| 282 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &introspection_xml, DBUS_TYPE_INVALID); |
| 283 | dbus_connection_send (connection, reply, NULL); |
| 284 | dbus_message_unref (reply); |
| 285 | } |
Simon Kelley | 73a08a2 | 2009-02-05 20:28:08 +0000 | [diff] [blame] | 286 | } |
| 287 | else if (strcmp(method, "GetVersion") == 0) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 288 | { |
| 289 | char *v = VERSION; |
| 290 | DBusMessage *reply = dbus_message_new_method_return(message); |
| 291 | |
| 292 | dbus_message_append_args(reply, DBUS_TYPE_STRING, &v, DBUS_TYPE_INVALID); |
| 293 | dbus_connection_send (connection, reply, NULL); |
| 294 | dbus_message_unref (reply); |
| 295 | } |
| 296 | else if (strcmp(method, "SetServers") == 0) |
| 297 | { |
Simon Kelley | f2621c7 | 2007-04-29 19:47:21 +0100 | [diff] [blame] | 298 | my_syslog(LOG_INFO, _("setting upstream servers from DBus")); |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 299 | dbus_read_servers(message); |
| 300 | check_servers(); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 301 | } |
| 302 | else if (strcmp(method, "ClearCache") == 0) |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 303 | clear_cache_and_reload(dnsmasq_time()); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 304 | else |
| 305 | return (DBUS_HANDLER_RESULT_NOT_YET_HANDLED); |
| 306 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 307 | method = user_data; /* no warning */ |
| 308 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 309 | return (DBUS_HANDLER_RESULT_HANDLED); |
| 310 | |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /* returns NULL or error message, may fail silently if dbus daemon not yet up. */ |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 315 | char *dbus_init(void) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 316 | { |
| 317 | DBusConnection *connection = NULL; |
| 318 | DBusObjectPathVTable dnsmasq_vtable = {NULL, &message_handler, NULL, NULL, NULL, NULL }; |
| 319 | DBusError dbus_error; |
| 320 | DBusMessage *message; |
| 321 | |
| 322 | dbus_error_init (&dbus_error); |
| 323 | if (!(connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error))) |
| 324 | return NULL; |
Simon Kelley | 7cebd20 | 2006-05-06 14:13:33 +0100 | [diff] [blame] | 325 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 326 | dbus_connection_set_exit_on_disconnect(connection, FALSE); |
| 327 | dbus_connection_set_watch_functions(connection, add_watch, remove_watch, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 328 | NULL, NULL, NULL); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 329 | dbus_error_init (&dbus_error); |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 330 | dbus_bus_request_name (connection, daemon->dbus_name, 0, &dbus_error); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 331 | if (dbus_error_is_set (&dbus_error)) |
| 332 | return (char *)dbus_error.message; |
| 333 | |
| 334 | if (!dbus_connection_register_object_path(connection, DNSMASQ_PATH, |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 335 | &dnsmasq_vtable, NULL)) |
Simon Kelley | b8187c8 | 2005-11-26 21:46:27 +0000 | [diff] [blame] | 336 | return _("could not register a DBus message handler"); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 337 | |
| 338 | daemon->dbus = connection; |
| 339 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 340 | if ((message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, "Up"))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 341 | { |
| 342 | dbus_connection_send(connection, message, NULL); |
| 343 | dbus_message_unref(message); |
| 344 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 345 | |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 350 | void set_dbus_listeners(int *maxfdp, |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 351 | fd_set *rset, fd_set *wset, fd_set *eset) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 352 | { |
| 353 | struct watch *w; |
| 354 | |
| 355 | for (w = daemon->watches; w; w = w->next) |
| 356 | if (dbus_watch_get_enabled(w->watch)) |
| 357 | { |
| 358 | unsigned int flags = dbus_watch_get_flags(w->watch); |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 359 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 360 | |
Simon Kelley | 1697269 | 2006-10-16 20:04:18 +0100 | [diff] [blame] | 361 | bump_maxfd(fd, maxfdp); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 362 | |
| 363 | if (flags & DBUS_WATCH_READABLE) |
| 364 | FD_SET(fd, rset); |
| 365 | |
| 366 | if (flags & DBUS_WATCH_WRITABLE) |
| 367 | FD_SET(fd, wset); |
| 368 | |
| 369 | FD_SET(fd, eset); |
| 370 | } |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Simon Kelley | 5aabfc7 | 2007-08-29 11:24:47 +0100 | [diff] [blame] | 373 | void check_dbus_listeners(fd_set *rset, fd_set *wset, fd_set *eset) |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 374 | { |
| 375 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 376 | struct watch *w; |
| 377 | |
| 378 | for (w = daemon->watches; w; w = w->next) |
| 379 | if (dbus_watch_get_enabled(w->watch)) |
| 380 | { |
| 381 | unsigned int flags = 0; |
Simon Kelley | 824af85 | 2008-02-12 20:43:05 +0000 | [diff] [blame] | 382 | int fd = dbus_watch_get_unix_fd(w->watch); |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 383 | |
| 384 | if (FD_ISSET(fd, rset)) |
| 385 | flags |= DBUS_WATCH_READABLE; |
| 386 | |
| 387 | if (FD_ISSET(fd, wset)) |
| 388 | flags |= DBUS_WATCH_WRITABLE; |
| 389 | |
| 390 | if (FD_ISSET(fd, eset)) |
| 391 | flags |= DBUS_WATCH_ERROR; |
| 392 | |
| 393 | if (flags != 0) |
| 394 | dbus_watch_handle(w->watch, flags); |
| 395 | } |
| 396 | |
| 397 | if (connection) |
| 398 | { |
| 399 | dbus_connection_ref (connection); |
| 400 | while (dbus_connection_dispatch (connection) == DBUS_DISPATCH_DATA_REMAINS); |
| 401 | dbus_connection_unref (connection); |
| 402 | } |
| 403 | } |
| 404 | |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 405 | #ifdef HAVE_DHCP |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 406 | void emit_dbus_signal(int action, struct dhcp_lease *lease, char *hostname) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 407 | { |
| 408 | DBusConnection *connection = (DBusConnection *)daemon->dbus; |
| 409 | DBusMessage* message = NULL; |
| 410 | DBusMessageIter args; |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 411 | char *action_str, *mac = daemon->namebuff; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 412 | unsigned char *p; |
| 413 | int i; |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 414 | |
| 415 | if (!connection) |
| 416 | return; |
Simon Kelley | 1f15b81 | 2009-10-13 17:49:32 +0100 | [diff] [blame] | 417 | |
| 418 | if (!hostname) |
| 419 | hostname = ""; |
| 420 | |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 421 | #ifdef HAVE_DHCP6 |
| 422 | if (lease->flags & (LEASE_TA | LEASE_NA)) |
| 423 | { |
| 424 | print_mac(mac, lease->clid, lease->clid_len); |
| 425 | inet_ntop(AF_INET6, lease->hwaddr, daemon->addrbuff, ADDRSTRLEN); |
| 426 | } |
| 427 | else |
| 428 | #endif |
| 429 | { |
| 430 | p = extended_hwaddr(lease->hwaddr_type, lease->hwaddr_len, |
| 431 | lease->hwaddr, lease->clid_len, lease->clid, &i); |
| 432 | print_mac(mac, p, i); |
| 433 | inet_ntop(AF_INET, &lease->addr, daemon->addrbuff, ADDRSTRLEN); |
| 434 | } |
| 435 | |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 436 | if (action == ACTION_DEL) |
| 437 | action_str = "DhcpLeaseDeleted"; |
| 438 | else if (action == ACTION_ADD) |
| 439 | action_str = "DhcpLeaseAdded"; |
| 440 | else if (action == ACTION_OLD) |
| 441 | action_str = "DhcpLeaseUpdated"; |
| 442 | else |
| 443 | return; |
| 444 | |
Simon Kelley | ad09427 | 2012-08-10 17:10:54 +0100 | [diff] [blame] | 445 | if (!(message = dbus_message_new_signal(DNSMASQ_PATH, daemon->dbus_name, action_str))) |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 446 | return; |
| 447 | |
| 448 | dbus_message_iter_init_append(message, &args); |
Simon Kelley | 6ffeff8 | 2012-03-07 10:32:35 +0000 | [diff] [blame] | 449 | |
| 450 | if (dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &daemon->addrbuff) && |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 451 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &mac) && |
| 452 | dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &hostname)) |
| 453 | dbus_connection_send(connection, message, NULL); |
| 454 | |
| 455 | dbus_message_unref(message); |
| 456 | } |
Simon Kelley | 316e273 | 2010-01-22 20:16:09 +0000 | [diff] [blame] | 457 | #endif |
Simon Kelley | 9009d74 | 2008-11-14 20:04:27 +0000 | [diff] [blame] | 458 | |
Simon Kelley | 3d8df26 | 2005-08-29 12:19:27 +0100 | [diff] [blame] | 459 | #endif |