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