fix excessive stack usage
[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 "util.h"
24 #include "ubus.h"
25 #include "cache.h"
26 #include "service.h"
27
28 static struct ubus_auto_conn conn;
29 static struct blob_buf b;
30
31 static int
32 mdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
33 struct ubus_request_data *req, const char *method,
34 struct blob_attr *msg)
35 {
36 service_init();
37 return 0;
38 }
39
40 static int
41 mdns_scan(struct ubus_context *ctx, struct ubus_object *obj,
42 struct ubus_request_data *req, const char *method,
43 struct blob_attr *msg)
44 {
45 cache_scan();
46 return 0;
47 }
48
49 static int
50 mdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
51 struct ubus_request_data *req, const char *method,
52 struct blob_attr *msg)
53 {
54 struct cache_entry *s, *q;
55 char *buffer = (char *) mdns_buf;
56 void *c1 = NULL, *c2;
57
58 blob_buf_init(&b, 0);
59 avl_for_each_element(&entries, s, avl) {
60 char *local;
61 if (*((char *) s->avl.key) != '_')
62 continue;
63 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
64 local = strstr(buffer, ".local");
65 if (local)
66 *local = '\0';
67 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
68 continue;
69
70 if (!c1) {
71 c1 = blobmsg_open_table(&b, buffer);
72 }
73 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->entry);
74 local = strstr(buffer, "._");
75 if (local)
76 *local = '\0';
77 c2 = blobmsg_open_table(&b, buffer);
78 strncat(buffer, ".local", MAX_NAME_LEN);
79 cache_dump_records(&b, buffer);
80 cache_dump_records(&b, s->entry);
81 blobmsg_close_table(&b, c2);
82 q = avl_next_element(s, avl);
83 if (!q || avl_is_last(&entries, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
84 blobmsg_close_table(&b, c1);
85 c1 = NULL;
86 }
87 }
88 ubus_send_reply(ctx, req, b.head);
89
90 return UBUS_STATUS_OK;
91 }
92
93 static int
94 mdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
95 struct ubus_request_data *req, const char *method,
96 struct blob_attr *msg)
97 {
98 struct cache_entry *s;
99 char *buffer = (char *) mdns_buf;
100 void *c;
101
102 blob_buf_init(&b, 0);
103 avl_for_each_element(&entries, s, avl) {
104 char *local;
105 if (*((char *) s->avl.key) == '_')
106 continue;
107 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->entry);
108 local = strstr(buffer, "._");
109 if (local)
110 *local = '\0';
111 c = blobmsg_open_table(&b, buffer);
112 strncat(buffer, ".local", MAX_NAME_LEN);
113 cache_dump_records(&b, buffer);
114 cache_dump_records(&b, s->entry);
115 blobmsg_close_table(&b, c);
116 }
117 ubus_send_reply(ctx, req, b.head);
118
119 return UBUS_STATUS_OK;
120 }
121
122 static const struct ubus_method mdns_methods[] = {
123 UBUS_METHOD_NOARG("scan", mdns_scan),
124 UBUS_METHOD_NOARG("browse", mdns_browse),
125 UBUS_METHOD_NOARG("hosts", mdns_hosts),
126 UBUS_METHOD_NOARG("reload", mdns_reload),
127 };
128
129 static struct ubus_object_type mdns_object_type =
130 UBUS_OBJECT_TYPE("mdns", mdns_methods);
131
132 static struct ubus_object mdns_object = {
133 .name = "mdns",
134 .type = &mdns_object_type,
135 .methods = mdns_methods,
136 .n_methods = ARRAY_SIZE(mdns_methods),
137 };
138
139 static void
140 ubus_connect_handler(struct ubus_context *ctx)
141 {
142 int ret;
143
144 ret = ubus_add_object(ctx, &mdns_object);
145 if (ret)
146 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
147 }
148
149 void
150 ubus_startup(void)
151 {
152 conn.cb = ubus_connect_handler;
153 ubus_auto_connect(&conn);
154 }