call announce_free from interface_free
[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 void
49 mdns_add_records(const char *name)
50 {
51 struct cache_record *r, *q = avl_find_element(&records, name, r, avl);
52 const char *txt;
53 char buffer[MAX_NAME_LEN];
54
55 if (!q)
56 return;
57
58 do {
59 r = q;
60 switch (r->type) {
61 case TYPE_TXT:
62 if (r->txt && strlen(r->txt)) {
63 txt = r->txt;
64 do {
65 blobmsg_add_string(&b, "txt", txt);
66 txt = &txt[strlen(txt) + 1];
67 } while (*txt);
68 }
69 break;
70
71 case TYPE_SRV:
72 if (r->port)
73 blobmsg_add_u32(&b, "port", r->port);
74 break;
75
76 case TYPE_A:
77 if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
78 blobmsg_add_string(&b, "ipv4", buffer);
79 break;
80
81 case TYPE_AAAA:
82 if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
83 blobmsg_add_string(&b, "ipv6", buffer);
84 break;
85 }
86 q = avl_next_element(r, avl);
87 } while (q && !strcmp(r->record, q->record));
88 }
89
90 static int
91 mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
92 struct ubus_request_data *req, const char *method,
93 struct blob_attr *msg)
94 {
95 struct cache_entry *s, *q;
96 char buffer[MAX_NAME_LEN];
97 void *c1 = NULL, *c2;
98
99 blob_buf_init(&b, 0);
100 avl_for_each_element(&entries, s, avl) {
101 char *local;
102 if (*((char *) s->avl.key) != '_')
103 continue;
104 snprintf(buffer, MAX_NAME_LEN, s->avl.key);
105 local = strstr(buffer, ".local");
106 if (local)
107 *local = '\0';
108 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
109 continue;
110
111 if (!c1) {
112 c1 = blobmsg_open_table(&b, buffer);
113 }
114 snprintf(buffer, MAX_NAME_LEN, s->entry);
115 local = strstr(buffer, "._");
116 if (local)
117 *local = '\0';
118 c2 = blobmsg_open_table(&b, buffer);
119 strncat(buffer, ".local", MAX_NAME_LEN);
120 mdns_add_records(buffer);
121 mdns_add_records(s->entry);
122 blobmsg_close_table(&b, c2);
123 q = avl_next_element(s, avl);
124 if (!q || avl_is_last(&entries, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
125 blobmsg_close_table(&b, c1);
126 c1 = NULL;
127 }
128 }
129 ubus_send_reply(ctx, req, b.head);
130
131 return UBUS_STATUS_OK;
132 }
133
134 static int
135 mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
136 struct ubus_request_data *req, const char *method,
137 struct blob_attr *msg)
138 {
139 struct cache_entry *s;
140 char buffer[MAX_NAME_LEN];
141 void *c;
142
143 blob_buf_init(&b, 0);
144 avl_for_each_element(&entries, s, avl) {
145 char *local;
146 if (*((char *) s->avl.key) == '_')
147 continue;
148 snprintf(buffer, MAX_NAME_LEN, s->entry);
149 local = strstr(buffer, "._");
150 if (local)
151 *local = '\0';
152 c = blobmsg_open_table(&b, buffer);
153 strncat(buffer, ".local", MAX_NAME_LEN);
154 mdns_add_records(buffer);
155 mdns_add_records(s->entry);
156 blobmsg_close_table(&b, c);
157 }
158 ubus_send_reply(ctx, req, b.head);
159
160 return UBUS_STATUS_OK;
161 }
162
163 static const struct ubus_method mdns_methods[] = {
164 UBUS_METHOD_NOARG("scan", mdns_scan),
165 UBUS_METHOD_NOARG("browse", mdns_browse),
166 UBUS_METHOD_NOARG("hosts", mdns_hosts),
167 UBUS_METHOD_NOARG("reload", mdns_reload),
168 };
169
170 static struct ubus_object_type mdns_object_type =
171 UBUS_OBJECT_TYPE("mdns", mdns_methods);
172
173 static struct ubus_object mdns_object = {
174 .name = "mdns",
175 .type = &mdns_object_type,
176 .methods = mdns_methods,
177 .n_methods = ARRAY_SIZE(mdns_methods),
178 };
179
180 static void
181 ubus_connect_handler(struct ubus_context *ctx)
182 {
183 int ret;
184
185 ret = ubus_add_object(ctx, &mdns_object);
186 if (ret)
187 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
188 }
189
190 void
191 ubus_startup(void)
192 {
193 conn.cb = ubus_connect_handler;
194 ubus_auto_connect(&conn);
195 }