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/vlist.h> 21 #include <libubox/uloop.h> 22 23 #include "util.h" 24 #include "ubus.h" 25 #include "cache.h" 26 #include "service.h" 27 #include "interface.h" 28 29 static struct ubus_auto_conn conn; 30 static struct blob_buf b; 31 32 static int 33 umdns_reload(struct ubus_context *ctx, struct ubus_object *obj, 34 struct ubus_request_data *req, const char *method, 35 struct blob_attr *msg) 36 { 37 service_init(1); 38 return 0; 39 } 40 41 static int 42 umdns_update(struct ubus_context *ctx, struct ubus_object *obj, 43 struct ubus_request_data *req, const char *method, 44 struct blob_attr *msg) 45 { 46 cache_update(); 47 return 0; 48 } 49 50 enum { 51 BROWSE_SERVICE, 52 BROWSE_ARRAY, 53 BROWSE_MAX 54 }; 55 56 static const struct blobmsg_policy browse_policy[] = { 57 [BROWSE_SERVICE] = { "service", BLOBMSG_TYPE_STRING }, 58 [BROWSE_ARRAY] = { "array", BLOBMSG_TYPE_BOOL }, 59 }; 60 61 static int 62 umdns_browse(struct ubus_context *ctx, struct ubus_object *obj, 63 struct ubus_request_data *req, const char *method, 64 struct blob_attr *msg) 65 { 66 struct cache_service *s, *q; 67 char *buffer = (char *) mdns_buf; 68 struct blob_attr *data[BROWSE_MAX]; 69 void *c1 = NULL, *c2; 70 char *service = NULL; 71 int array = 0; 72 73 blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg)); 74 if (data[BROWSE_SERVICE]) 75 service = blobmsg_get_string(data[BROWSE_SERVICE]); 76 if (data[BROWSE_ARRAY]) 77 array = blobmsg_get_u8(data[BROWSE_ARRAY]); 78 79 blob_buf_init(&b, 0); 80 avl_for_each_element(&services, s, avl) { 81 char *local; 82 83 snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key); 84 local = strstr(buffer, ".local"); 85 if (local) 86 *local = '\0'; 87 if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp")) 88 continue; 89 if (service && strcmp(buffer, service)) 90 continue; 91 if (!c1) { 92 c1 = blobmsg_open_table(&b, buffer); 93 } 94 snprintf(buffer, MAX_NAME_LEN, "%s", s->entry); 95 local = strstr(buffer, "._"); 96 if (local) 97 *local = '\0'; 98 c2 = blobmsg_open_table(&b, buffer); 99 strncat(buffer, ".local", MAX_NAME_LEN); 100 blobmsg_add_string(&b, "iface", s->iface->name); 101 cache_dump_records(&b, buffer, array); 102 cache_dump_records(&b, s->entry, array); 103 blobmsg_close_table(&b, c2); 104 q = avl_next_element(s, avl); 105 if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) { 106 blobmsg_close_table(&b, c1); 107 c1 = NULL; 108 } 109 } 110 ubus_send_reply(ctx, req, b.head); 111 112 return UBUS_STATUS_OK; 113 } 114 115 static int 116 umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj, 117 struct ubus_request_data *req, const char *method, 118 struct blob_attr *msg) 119 { 120 struct cache_record *prev = NULL; 121 struct cache_record *r; 122 void *c; 123 124 blob_buf_init(&b, 0); 125 avl_for_each_element(&records, r, avl) { 126 if (r->type != TYPE_A && r->type != TYPE_AAAA) 127 continue; 128 /* Query each domain just once */ 129 if (!prev || strcmp(r->record, prev->record)) { 130 c = blobmsg_open_table(&b, r->record); 131 cache_dump_records(&b, r->record, false); 132 blobmsg_close_table(&b, c); 133 } 134 prev = r; 135 } 136 ubus_send_reply(ctx, req, b.head); 137 138 return UBUS_STATUS_OK; 139 } 140 141 enum { 142 CFG_INTERFACES, 143 CFG_KEEP, 144 CFG_MAX 145 }; 146 147 static const struct blobmsg_policy config_policy[] = { 148 [CFG_INTERFACES] = { "interfaces", BLOBMSG_TYPE_ARRAY }, 149 [CFG_KEEP] = { "keep", BLOBMSG_TYPE_BOOL }, 150 }; 151 152 static int 153 umdns_set_config(struct ubus_context *ctx, struct ubus_object *obj, 154 struct ubus_request_data *req, const char *method, 155 struct blob_attr *msg) 156 { 157 struct blob_attr *data[CFG_MAX], *cur; 158 int rem, keep = false; 159 160 blobmsg_parse(config_policy, CFG_MAX, data, blob_data(msg), blob_len(msg)); 161 if (!data[CFG_INTERFACES]) 162 return UBUS_STATUS_INVALID_ARGUMENT; 163 164 if (!blobmsg_check_attr_list(data[CFG_INTERFACES], BLOBMSG_TYPE_STRING)) 165 return UBUS_STATUS_INVALID_ARGUMENT; 166 167 keep = data[CFG_KEEP] && blobmsg_get_bool(data[CFG_KEEP]); 168 if (!keep) { 169 vlist_update(&interfaces); 170 ubus_notify(ctx, obj, "set_config", NULL, 1000); 171 } 172 173 blobmsg_for_each_attr(cur, data[CFG_INTERFACES], rem) 174 interface_add(blobmsg_data(cur)); 175 176 if (!keep) 177 vlist_flush(&interfaces); 178 179 return 0; 180 } 181 182 enum query_attr { 183 QUERY_QUESTION, 184 QUERY_IFACE, 185 QUERY_TYPE, 186 QUERY_MAX 187 }; 188 189 static const struct blobmsg_policy query_policy[QUERY_MAX] = { 190 [QUERY_QUESTION]= { "question", BLOBMSG_TYPE_STRING }, 191 [QUERY_IFACE] = { "interface", BLOBMSG_TYPE_STRING }, 192 [QUERY_TYPE] = { "type", BLOBMSG_TYPE_INT32 }, 193 }; 194 195 static int 196 umdns_query(struct ubus_context *ctx, struct ubus_object *obj, 197 struct ubus_request_data *req, const char *method, 198 struct blob_attr *msg) 199 { 200 struct blob_attr *tb[QUERY_MAX], *c; 201 const char *question = C_DNS_SD; 202 const char *ifname; 203 int type = TYPE_ANY; 204 205 blobmsg_parse(query_policy, QUERY_MAX, tb, blob_data(msg), blob_len(msg)); 206 207 if (!(c = tb[QUERY_IFACE])) 208 return UBUS_STATUS_INVALID_ARGUMENT; 209 210 ifname = blobmsg_get_string(c); 211 212 if ((c = tb[QUERY_QUESTION])) 213 question = blobmsg_get_string(c); 214 215 if ((c = tb[QUERY_TYPE])) 216 type = blobmsg_get_u32(c); 217 218 struct interface *iface_v4 = interface_get(ifname, 0, 1); 219 struct interface *iface_v6 = interface_get(ifname, 1, 1); 220 221 if (!iface_v4 && !iface_v6) 222 return UBUS_STATUS_NOT_FOUND; 223 224 if (!strcmp(method, "query")) { 225 if (iface_v4) 226 dns_send_question(iface_v4, NULL, question, type, 1); 227 228 if (iface_v6) 229 dns_send_question(iface_v6, NULL, question, type, 1); 230 231 return UBUS_STATUS_OK; 232 } else if (!strcmp(method, "fetch")) { 233 blob_buf_init(&b, 0); 234 void *k = blobmsg_open_array(&b, "records"); 235 cache_dump_recursive(&b, question, type, iface_v4 ? iface_v4 : iface_v6); 236 blobmsg_close_array(&b, k); 237 ubus_send_reply(ctx, req, b.head); 238 return UBUS_STATUS_OK; 239 } else { 240 return UBUS_STATUS_INVALID_ARGUMENT; 241 } 242 } 243 244 245 static const struct ubus_method umdns_methods[] = { 246 UBUS_METHOD("set_config", umdns_set_config, config_policy), 247 UBUS_METHOD("query", umdns_query, query_policy), 248 UBUS_METHOD("fetch", umdns_query, query_policy), 249 UBUS_METHOD("browse", umdns_browse, browse_policy), 250 UBUS_METHOD_NOARG("update", umdns_update), 251 UBUS_METHOD_NOARG("hosts", umdns_hosts), 252 UBUS_METHOD_NOARG("reload", umdns_reload), 253 }; 254 255 static struct ubus_object_type umdns_object_type = 256 UBUS_OBJECT_TYPE("umdns", umdns_methods); 257 258 static struct ubus_object umdns_object = { 259 .name = "umdns", 260 .type = &umdns_object_type, 261 .methods = umdns_methods, 262 .n_methods = ARRAY_SIZE(umdns_methods), 263 }; 264 265 static void 266 ubus_connect_handler(struct ubus_context *ctx) 267 { 268 int ret; 269 270 ret = ubus_add_object(ctx, &umdns_object); 271 if (ret) 272 fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret)); 273 } 274 275 void 276 ubus_startup(void) 277 { 278 conn.cb = ubus_connect_handler; 279 ubus_auto_connect(&conn); 280 } 281 282 int ubus_service_list(ubus_data_handler_t cb) 283 { 284 uint32_t id; 285 int ret; 286 287 blob_buf_init(&b, 0); 288 ret = ubus_lookup_id(&conn.ctx, "service", &id); 289 if (ret) 290 return ret; 291 292 return ubus_invoke(&conn.ctx, id, "list", b.head, cb, NULL, 5 * 1000); 293 } 294
This page was automatically generated by LXR 0.3.1. • OpenWrt