ubus: constify name argument to mdns_add_records
[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 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 char *type = cache_lookup_name(buffer);
111 c1 = blobmsg_open_table(&b, buffer);
112 if (type)
113 blobmsg_add_string(&b, ".desc", type);
114 }
115 snprintf(buffer, MAX_NAME_LEN, s->entry);
116 local = strstr(buffer, "._");
117 if (local)
118 *local = '\0';
119 c2 = blobmsg_open_table(&b, buffer);
120 strncat(buffer, ".local", MAX_NAME_LEN);
121 mdns_add_records(buffer);
122 mdns_add_records(s->entry);
123 blobmsg_close_table(&b, c2);
124 q = avl_next_element(s, avl);
125 if (!q || avl_is_last(&entries, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
126 blobmsg_close_table(&b, c1);
127 c1 = NULL;
128 }
129 }
130 ubus_send_reply(ctx, req, b.head);
131
132 return UBUS_STATUS_OK;
133 }
134
135 static int
136 mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
137 struct ubus_request_data *req, const char *method,
138 struct blob_attr *msg)
139 {
140 struct cache_entry *s;
141 char buffer[MAX_NAME_LEN];
142 void *c;
143
144 blob_buf_init(&b, 0);
145 avl_for_each_element(&entries, s, avl) {
146 char *local;
147 if (*((char *) s->avl.key) == '_')
148 continue;
149 snprintf(buffer, MAX_NAME_LEN, s->entry);
150 local = strstr(buffer, "._");
151 if (local)
152 *local = '\0';
153 c = blobmsg_open_table(&b, buffer);
154 strncat(buffer, ".local", MAX_NAME_LEN);
155 mdns_add_records(buffer);
156 mdns_add_records(s->entry);
157 blobmsg_close_table(&b, c);
158 }
159 ubus_send_reply(ctx, req, b.head);
160
161 return UBUS_STATUS_OK;
162 }
163
164 static const struct ubus_method mdns_methods[] = {
165 UBUS_METHOD_NOARG("scan", mdns_scan),
166 UBUS_METHOD_NOARG("browse", mdns_browse),
167 UBUS_METHOD_NOARG("hosts", mdns_hosts),
168 UBUS_METHOD_NOARG("reload", mdns_reload),
169 };
170
171 static struct ubus_object_type mdns_object_type =
172 UBUS_OBJECT_TYPE("mdns", mdns_methods);
173
174 static struct ubus_object mdns_object = {
175 .name = "mdns",
176 .type = &mdns_object_type,
177 .methods = mdns_methods,
178 .n_methods = ARRAY_SIZE(mdns_methods),
179 };
180
181 static void
182 ubus_connect_handler(struct ubus_context *ctx)
183 {
184 int ret;
185
186 ret = ubus_add_object(ctx, &mdns_object);
187 if (ret)
188 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
189 }
190
191 void
192 ubus_startup(void)
193 {
194 conn.cb = ubus_connect_handler;
195 ubus_auto_connect(&conn);
196 }