6d0cdeaa4c0496a42257dcf3387b3ecc1bc29374
[project/mdnsd.git] / ubus.c
1 /*
2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
7 *
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.
12 */
13
14 #include <sys/types.h>
15 #include <arpa/inet.h>
16
17 #include <stdio.h>
18
19 #include <libubus.h>
20 #include <libubox/avl.h>
21 #include <libubox/uloop.h>
22
23 #include "ubus.h"
24 #include "cache.h"
25 #include "service.h"
26
27 static struct ubus_auto_conn conn;
28 static struct blob_buf b;
29
30 static int
31 mdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
32 struct ubus_request_data *req, const char *method,
33 struct blob_attr *msg)
34 {
35 service_init();
36 return 0;
37 }
38
39 static int
40 mdns_scan(struct ubus_context *ctx, struct ubus_object *obj,
41 struct ubus_request_data *req, const char *method,
42 struct blob_attr *msg)
43 {
44 cache_scan();
45 return 0;
46 }
47
48 static int
49 mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
50 struct ubus_request_data *req, const char *method,
51 struct blob_attr *msg)
52 {
53 struct cache_entry *s, *q;
54 char buffer[MAX_NAME_LEN];
55 void *c1 = NULL, *c2;
56
57 blob_buf_init(&b, 0);
58 avl_for_each_element(&entries, s, avl) {
59 char *local;
60 if (*((char *) s->avl.key) != '_')
61 continue;
62 snprintf(buffer, MAX_NAME_LEN, s->avl.key);
63 local = strstr(buffer, ".local");
64 if (local)
65 *local = '\0';
66 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
67 continue;
68
69 if (!c1) {
70 c1 = blobmsg_open_table(&b, buffer);
71 }
72 snprintf(buffer, MAX_NAME_LEN, s->entry);
73 local = strstr(buffer, "._");
74 if (local)
75 *local = '\0';
76 c2 = blobmsg_open_table(&b, buffer);
77 strncat(buffer, ".local", MAX_NAME_LEN);
78 cache_dump_records(&b, buffer);
79 cache_dump_records(&b, s->entry);
80 blobmsg_close_table(&b, c2);
81 q = avl_next_element(s, avl);
82 if (!q || avl_is_last(&entries, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
83 blobmsg_close_table(&b, c1);
84 c1 = NULL;
85 }
86 }
87 ubus_send_reply(ctx, req, b.head);
88
89 return UBUS_STATUS_OK;
90 }
91
92 static int
93 mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
94 struct ubus_request_data *req, const char *method,
95 struct blob_attr *msg)
96 {
97 struct cache_entry *s;
98 char buffer[MAX_NAME_LEN];
99 void *c;
100
101 blob_buf_init(&b, 0);
102 avl_for_each_element(&entries, s, avl) {
103 char *local;
104 if (*((char *) s->avl.key) == '_')
105 continue;
106 snprintf(buffer, MAX_NAME_LEN, s->entry);
107 local = strstr(buffer, "._");
108 if (local)
109 *local = '\0';
110 c = blobmsg_open_table(&b, buffer);
111 strncat(buffer, ".local", MAX_NAME_LEN);
112 cache_dump_records(&b, buffer);
113 cache_dump_records(&b, s->entry);
114 blobmsg_close_table(&b, c);
115 }
116 ubus_send_reply(ctx, req, b.head);
117
118 return UBUS_STATUS_OK;
119 }
120
121 static const struct ubus_method mdns_methods[] = {
122 UBUS_METHOD_NOARG("scan", mdns_scan),
123 UBUS_METHOD_NOARG("browse", mdns_browse),
124 UBUS_METHOD_NOARG("hosts", mdns_hosts),
125 UBUS_METHOD_NOARG("reload", mdns_reload),
126 };
127
128 static struct ubus_object_type mdns_object_type =
129 UBUS_OBJECT_TYPE("mdns", mdns_methods);
130
131 static struct ubus_object mdns_object = {
132 .name = "mdns",
133 .type = &mdns_object_type,
134 .methods = mdns_methods,
135 .n_methods = ARRAY_SIZE(mdns_methods),
136 };
137
138 static void
139 ubus_connect_handler(struct ubus_context *ctx)
140 {
141 int ret;
142
143 ret = ubus_add_object(ctx, &mdns_object);
144 if (ret)
145 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
146 }
147
148 void
149 ubus_startup(void)
150 {
151 conn.cb = ubus_connect_handler;
152 ubus_auto_connect(&conn);
153 }