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 <netinet/in.h>
16 #include <arpa/nameser.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
29 #include <libubox/vlist.h>
30 #include <libubox/uloop.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/blobmsg_json.h>
37 #include "interface.h"
46 struct vlist_node node
;
58 static const struct blobmsg_policy service_policy
[__SERVICE_MAX
] = {
59 [SERVICE_PORT
] = { .name
= "port", .type
= BLOBMSG_TYPE_INT32
},
60 [SERVICE_TXT
] = { .name
= "txt", .type
= BLOBMSG_TYPE_ARRAY
},
63 static const struct uci_blob_param_list service_attr_list
= {
64 .n_params
= __SERVICE_MAX
,
65 .params
= service_policy
,
69 service_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
70 struct vlist_node
*node_old
);
72 static struct blob_buf b
;
73 static VLIST_TREE(services
, avl_strcmp
, service_update
, false, false);
74 char *hostname
= NULL
;
75 static char *sdudp
= "_services._dns-sd._udp.local";
76 static char *sdtcp
= "_services._dns-sd._tcp.local";
79 service_name(const char *domain
)
81 static char buffer
[256];
83 snprintf(buffer
, sizeof(buffer
), "%s.%s", hostname
, domain
);
89 service_send_ptr(struct uloop_fd
*u
, struct service
*s
)
91 unsigned char buffer
[MAX_NAME_LEN
];
92 const char *host
= service_name(s
->service
);
93 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
98 dns_add_answer(TYPE_PTR
, buffer
, len
);
102 service_send_ptr_c(struct uloop_fd
*u
, const char *host
)
104 unsigned char buffer
[MAX_NAME_LEN
];
105 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
110 dns_add_answer(TYPE_PTR
, buffer
, len
);
114 service_send_a(struct uloop_fd
*u
)
116 unsigned char buffer
[MAX_NAME_LEN
];
117 char *host
= service_name("local");
118 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
121 if (!inet_aton(cur_iface
->ip
, &in
)) {
122 fprintf(stderr
, "%s is not valid\n", cur_iface
->ip
);
129 dns_add_answer(TYPE_A
, (uint8_t *) &in
.s_addr
, 4);
133 service_send_srv(struct uloop_fd
*u
, struct service
*s
)
135 unsigned char buffer
[MAX_NAME_LEN
];
136 struct dns_srv_data
*sd
;
137 char *host
= service_name("local");
138 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
143 sd
= calloc(1, len
+ sizeof(struct dns_srv_data
));
147 sd
->port
= cpu_to_be16(s
->port
);
148 memcpy(&sd
[1], buffer
, len
);
149 host
= service_name(s
->service
);
150 dns_add_answer(TYPE_SRV
, (uint8_t *) sd
, len
+ sizeof(struct dns_srv_data
));
154 #define TOUT_LOOKUP 60
157 service_timeout(struct service
*s
)
159 time_t t
= time(NULL
);
161 if (t
- s
->t
<= TOUT_LOOKUP
)
170 service_reply_a(struct uloop_fd
*u
, int type
)
177 dns_send_answer(u
, service_name("local"));
181 service_reply(struct uloop_fd
*u
, const char *match
)
185 vlist_for_each_element(&services
, s
, node
) {
186 char *host
= service_name(s
->service
);
187 char *service
= strstr(host
, "._");
189 if (!s
->active
|| !service
|| !service_timeout(s
))
194 if (match
&& strcmp(match
, s
->service
))
198 service_send_ptr(u
, s
);
199 dns_send_answer(u
, service
);
202 service_send_srv(u
, s
);
203 if (s
->txt
&& s
->txt_len
)
204 dns_add_answer(TYPE_TXT
, (uint8_t *) s
->txt
, s
->txt_len
);
205 dns_send_answer(u
, host
);
213 dns_send_answer(u
, service_name("local"));
217 service_announce_services(struct uloop_fd
*u
, const char *service
)
222 if (!strcmp(service
, sdudp
))
224 else if (strcmp(service
, sdtcp
))
227 vlist_for_each_element(&services
, s
, node
) {
228 if (!strstr(s
->service
, "._tcp") && tcp
)
230 if (!strstr(s
->service
, "._udp") && !tcp
)
234 service_send_ptr_c(u
, s
->service
);
236 dns_send_answer(u
, sdtcp
);
238 dns_send_answer(u
, sdudp
);
239 service_reply(u
, s
->service
);
244 service_announce(struct uloop_fd
*u
)
246 service_announce_services(u
, sdudp
);
247 service_announce_services(u
, sdtcp
);
251 service_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
252 struct vlist_node
*node_old
)
259 s
= container_of(node_old
, struct service
, node
);
264 service_load(char *path
)
266 struct blob_attr
*txt
, *cur
, *_tb
[__SERVICE_MAX
];
270 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
273 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
274 blob_buf_init(&b
, 0);
276 if (!blobmsg_add_json_from_file(&b
, gl
.gl_pathv
[i
]))
278 blob_for_each_attr(cur
, b
.head
, rem
) {
280 char *d_service
, *d_daemon
;
285 blobmsg_parse(service_policy
, ARRAY_SIZE(service_policy
),
286 _tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
287 if (!_tb
[SERVICE_PORT
] || !_tb
[SERVICE_TXT
])
290 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
)
291 txt_len
+= 1 + strlen(blobmsg_get_string(txt
));
293 s
= calloc_a(sizeof(*s
),
294 &d_daemon
, strlen(gl
.gl_pathv
[i
]) + 1,
295 &d_service
, strlen(blobmsg_name(cur
)) + 1,
300 s
->port
= blobmsg_get_u32(_tb
[SERVICE_PORT
]);
301 s
->service
= strcpy(d_service
, blobmsg_name(cur
));
302 s
->daemon
= strcpy(d_daemon
, gl
.gl_pathv
[i
]);
305 s
->txt_len
= txt_len
;
308 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
) {
309 int len
= strlen(blobmsg_get_string(txt
));
316 memcpy(d_txt
, blobmsg_get_string(txt
), len
);
320 vlist_add(&services
, &s
->node
, s
->service
);
329 hostname
= get_hostname();
331 vlist_update(&services
);
332 service_load("/tmp/run/mdnsd/*");
333 vlist_flush(&services
);
337 service_cleanup(void)
339 vlist_flush(&services
);