"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/ubus.c" between
dnsmasq-2.80.tar.gz and dnsmasq-2.81.tar.xz

About: Dnsmasq is a lightweight caching DNS forwarder and DHCP server.

ubus.c  (dnsmasq-2.80):ubus.c  (dnsmasq-2.81.tar.xz)
/* dnsmasq is Copyright (c) 2000-2018 Simon Kelley /* dnsmasq is Copyright (c) 2000-2020 Simon Kelley
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 dated June, 1991, or the Free Software Foundation; version 2 dated June, 1991, or
(at your option) version 3 dated 29 June, 2007. (at your option) version 3 dated 29 June, 2007.
This program is distributed in the hope that it will be useful, This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
skipping to change at line 23 skipping to change at line 23
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "dnsmasq.h" #include "dnsmasq.h"
#ifdef HAVE_UBUS #ifdef HAVE_UBUS
#include <libubus.h> #include <libubus.h>
static struct ubus_context *ubus = NULL;
static struct blob_buf b; static struct blob_buf b;
static int notify;
static int error_logged = 0;
static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj , static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj ,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg); struct blob_attr *msg);
static struct ubus_method ubus_object_methods[] = {
{.name = "metrics", .handler = ubus_handle_metrics}, static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
;
static const struct ubus_method ubus_object_methods[] = {
UBUS_METHOD_NOARG("metrics", ubus_handle_metrics),
}; };
static struct ubus_object_type ubus_object_type = UBUS_OBJECT_TYPE("dnsmasq", ub static struct ubus_object_type ubus_object_type =
us_object_methods); UBUS_OBJECT_TYPE("dnsmasq", ubus_object_methods);
static struct ubus_object ubus_object = { static struct ubus_object ubus_object = {
.name = "dnsmasq", .name = NULL,
.type = &ubus_object_type, .type = &ubus_object_type,
.methods = ubus_object_methods, .methods = ubus_object_methods,
.n_methods = ARRAY_SIZE(ubus_object_methods), .n_methods = ARRAY_SIZE(ubus_object_methods),
.subscribe_cb = ubus_subscribe_cb,
}; };
static void ubus_subscribe_cb(struct ubus_context *ctx, struct ubus_object *obj)
{
(void)ctx;
my_syslog(LOG_DEBUG, _("UBus subscription callback: %s subscriber(s)"), obj->h
as_subscribers ? "1" : "0");
notify = obj->has_subscribers;
}
static void ubus_destroy(struct ubus_context *ubus)
{
// Forces re-initialization when we're reusing the same definitions later on.
ubus_object.id = 0;
ubus_object_type.id = 0;
ubus_free(ubus);
daemon->ubus = NULL;
}
static void ubus_disconnect_cb(struct ubus_context *ubus)
{
int ret;
ret = ubus_reconnect(ubus, NULL);
if (ret)
{
my_syslog(LOG_ERR, _("Cannot reconnect to UBus: %s"), ubus_strerror(ret));
ubus_destroy(ubus);
}
}
void ubus_init()
{
struct ubus_context *ubus = NULL;
int ret = 0;
ubus = ubus_connect(NULL);
if (!ubus)
{
if (!error_logged)
{
my_syslog(LOG_ERR, _("Cannot initialize UBus: connection failed"));
error_logged = 1;
}
ubus_destroy(ubus);
return;
}
ubus_object.name = daemon->ubus_name;
ret = ubus_add_object(ubus, &ubus_object);
if (ret)
{
if (!error_logged)
{
my_syslog(LOG_ERR, _("Cannot add object to UBus: %s"), ubus_strerror(r
et));
error_logged = 1;
}
ubus_destroy(ubus);
return;
}
ubus->connection_lost = ubus_disconnect_cb;
daemon->ubus = ubus;
error_logged = 0;
my_syslog(LOG_INFO, _("Connected to system UBus"));
}
void set_ubus_listeners() void set_ubus_listeners()
{ {
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
if (!ubus) if (!ubus)
return; {
if (!error_logged)
{
my_syslog(LOG_ERR, _("Cannot set UBus listeners: no connection"));
error_logged = 1;
}
return;
}
error_logged = 0;
poll_listen(ubus->sock.fd, POLLIN); poll_listen(ubus->sock.fd, POLLIN);
poll_listen(ubus->sock.fd, POLLERR); poll_listen(ubus->sock.fd, POLLERR);
poll_listen(ubus->sock.fd, POLLHUP); poll_listen(ubus->sock.fd, POLLHUP);
} }
void check_ubus_listeners() void check_ubus_listeners()
{ {
struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
if (!ubus) if (!ubus)
{ {
ubus = ubus_connect(NULL); if (!error_logged)
if (!ubus) {
return; my_syslog(LOG_ERR, _("Cannot poll UBus listeners: no connection"));
ubus_add_object(ubus, &ubus_object); error_logged = 1;
}
return;
} }
error_logged = 0;
if (poll_check(ubus->sock.fd, POLLIN)) if (poll_check(ubus->sock.fd, POLLIN))
ubus_handle_event(ubus); ubus_handle_event(ubus);
if (poll_check(ubus->sock.fd, POLLHUP)) if (poll_check(ubus->sock.fd, POLLHUP | POLLERR))
{ {
ubus_free(ubus); my_syslog(LOG_INFO, _("Disconnecting from UBus"));
ubus = NULL;
ubus_destroy(ubus);
} }
} }
static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj , static int ubus_handle_metrics(struct ubus_context *ctx, struct ubus_object *obj ,
struct ubus_request_data *req, const char *method, struct ubus_request_data *req, const char *method,
struct blob_attr *msg) struct blob_attr *msg)
{ {
int i; int i;
blob_buf_init(&b, 0);
for(i=0; i < __METRIC_MAX; i++) (void)obj;
blobmsg_add_u32(&b, get_metric_name(i), daemon->metrics[i]); (void)method;
(void)msg;
ubus_send_reply(ctx, req, b.head); blob_buf_init(&b, BLOBMSG_TYPE_TABLE);
return 0; for (i=0; i < __METRIC_MAX; i++)
blobmsg_add_u32(&b, get_metric_name(i), daemon->metrics[i]);
return ubus_send_reply(ctx, req, b.head);
} }
void ubus_event_bcast(const char *type, const char *mac, const char *ip, const c har *name, const char *interface) void ubus_event_bcast(const char *type, const char *mac, const char *ip, const c har *name, const char *interface)
{ {
if (!ubus || !ubus_object.has_subscribers) struct ubus_context *ubus = (struct ubus_context *)daemon->ubus;
int ret;
if (!ubus || !notify)
return; return;
blob_buf_init(&b, 0); blob_buf_init(&b, BLOBMSG_TYPE_TABLE);
if (mac) if (mac)
blobmsg_add_string(&b, "mac", mac); blobmsg_add_string(&b, "mac", mac);
if (ip) if (ip)
blobmsg_add_string(&b, "ip", ip); blobmsg_add_string(&b, "ip", ip);
if (name) if (name)
blobmsg_add_string(&b, "name", name); blobmsg_add_string(&b, "name", name);
if (interface) if (interface)
blobmsg_add_string(&b, "interface", interface); blobmsg_add_string(&b, "interface", interface);
ubus_notify(ubus, &ubus_object, type, b.head, -1); ret = ubus_notify(ubus, &ubus_object, type, b.head, -1);
if (!ret)
my_syslog(LOG_ERR, _("Failed to send UBus event: %s"), ubus_strerror(ret));
} }
#endif /* HAVE_UBUS */ #endif /* HAVE_UBUS */
 End of changes. 22 change blocks. 
23 lines changed or deleted 124 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)