2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
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
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.
14 #include <sys/types.h>
15 #include <arpa/nameser.h>
16 #include <sys/socket.h>
26 #include <libubox/vlist.h>
27 #include <libubox/uloop.h>
28 #include <libubox/avl-cmp.h>
29 #include <libubox/blobmsg_json.h>
34 #include "interface.h"
43 struct vlist_node node
;
55 static const struct blobmsg_policy service_policy
[__SERVICE_MAX
] = {
56 [SERVICE_PORT
] = { .name
= "port", .type
= BLOBMSG_TYPE_INT32
},
57 [SERVICE_TXT
] = { .name
= "txt", .type
= BLOBMSG_TYPE_ARRAY
},
60 static const struct uci_blob_param_list service_attr_list
= {
61 .n_params
= __SERVICE_MAX
,
62 .params
= service_policy
,
66 service_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
67 struct vlist_node
*node_old
);
69 static struct blob_buf b
;
70 static VLIST_TREE(services
, avl_strcmp
, service_update
, false, false);
71 char *hostname
= NULL
;
72 static char *sdudp
= "_services._dns-sd._udp.local";
73 static char *sdtcp
= "_services._dns-sd._tcp.local";
76 service_name(const char *domain
)
78 static char buffer
[256];
80 snprintf(buffer
, sizeof(buffer
), "%s.%s", hostname
, domain
);
86 service_add_ptr(struct service
*s
)
88 unsigned char buffer
[MAX_NAME_LEN
];
89 const char *host
= service_name(s
->service
);
90 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
95 dns_add_answer(TYPE_PTR
, buffer
, len
);
99 service_add_ptr_c(const char *host
)
101 unsigned char buffer
[MAX_NAME_LEN
];
102 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
107 dns_add_answer(TYPE_PTR
, buffer
, len
);
111 service_send_a(struct interface
*iface
)
113 unsigned char buffer
[MAX_NAME_LEN
];
114 char *host
= service_name("local");
115 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
120 dns_add_answer(TYPE_A
, (uint8_t *) &iface
->v4_addr
.s_addr
, 4);
124 service_add_srv(struct service
*s
)
126 unsigned char buffer
[MAX_NAME_LEN
];
127 struct dns_srv_data
*sd
;
128 char *host
= service_name("local");
129 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
134 sd
= calloc(1, len
+ sizeof(struct dns_srv_data
));
138 sd
->port
= cpu_to_be16(s
->port
);
139 memcpy(&sd
[1], buffer
, len
);
140 host
= service_name(s
->service
);
141 dns_add_answer(TYPE_SRV
, (uint8_t *) sd
, len
+ sizeof(struct dns_srv_data
));
145 #define TOUT_LOOKUP 60
148 service_timeout(struct service
*s
)
150 time_t t
= time(NULL
);
152 if (t
- s
->t
<= TOUT_LOOKUP
)
161 service_reply_a(struct interface
*iface
, int type
)
167 service_send_a(iface
);
168 dns_send_answer(iface
, service_name("local"));
172 service_reply(struct interface
*iface
, const char *match
)
176 vlist_for_each_element(&services
, s
, node
) {
177 char *host
= service_name(s
->service
);
178 char *service
= strstr(host
, "._");
180 if (!s
->active
|| !service
|| !service_timeout(s
))
185 if (match
&& strcmp(match
, s
->service
))
190 dns_send_answer(iface
, service
);
194 if (s
->txt
&& s
->txt_len
)
195 dns_add_answer(TYPE_TXT
, (uint8_t *) s
->txt
, s
->txt_len
);
196 dns_send_answer(iface
, host
);
203 service_send_a(iface
);
204 dns_send_answer(iface
, service_name("local"));
208 service_announce_services(struct interface
*iface
, const char *service
)
213 if (!strcmp(service
, sdudp
))
215 else if (strcmp(service
, sdtcp
))
218 vlist_for_each_element(&services
, s
, node
) {
219 if (!strstr(s
->service
, "._tcp") && tcp
)
221 if (!strstr(s
->service
, "._udp") && !tcp
)
225 service_add_ptr_c(s
->service
);
227 dns_send_answer(iface
, sdtcp
);
229 dns_send_answer(iface
, sdudp
);
230 service_reply(iface
, s
->service
);
235 service_announce(struct interface
*iface
)
237 service_announce_services(iface
, sdudp
);
238 service_announce_services(iface
, sdtcp
);
242 service_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
243 struct vlist_node
*node_old
)
250 s
= container_of(node_old
, struct service
, node
);
255 service_load(char *path
)
257 struct blob_attr
*txt
, *cur
, *_tb
[__SERVICE_MAX
];
261 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
264 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
265 blob_buf_init(&b
, 0);
267 if (!blobmsg_add_json_from_file(&b
, gl
.gl_pathv
[i
]))
269 blob_for_each_attr(cur
, b
.head
, rem
) {
271 char *d_service
, *d_daemon
;
276 blobmsg_parse(service_policy
, ARRAY_SIZE(service_policy
),
277 _tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
278 if (!_tb
[SERVICE_PORT
] || !_tb
[SERVICE_TXT
])
281 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
)
282 txt_len
+= 1 + strlen(blobmsg_get_string(txt
));
284 s
= calloc_a(sizeof(*s
),
285 &d_daemon
, strlen(gl
.gl_pathv
[i
]) + 1,
286 &d_service
, strlen(blobmsg_name(cur
)) + 1,
291 s
->port
= blobmsg_get_u32(_tb
[SERVICE_PORT
]);
292 s
->service
= strcpy(d_service
, blobmsg_name(cur
));
293 s
->daemon
= strcpy(d_daemon
, gl
.gl_pathv
[i
]);
296 s
->txt_len
= txt_len
;
299 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
) {
300 int len
= strlen(blobmsg_get_string(txt
));
307 memcpy(d_txt
, blobmsg_get_string(txt
), len
);
311 vlist_add(&services
, &s
->node
, s
->service
);
320 hostname
= get_hostname();
322 vlist_update(&services
);
323 service_load("/tmp/run/mdnsd/*");
324 vlist_flush(&services
);
328 service_cleanup(void)
330 vlist_flush(&services
);