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/avl.h>
30 #include <libubox/uloop.h>
31 #include <libubox/avl-cmp.h>
32 #include <libubox/blobmsg_json.h>
57 static const struct blobmsg_policy service_policy
[__SERVICE_MAX
] = {
58 [SERVICE_PORT
] = { .name
= "port", .type
= BLOBMSG_TYPE_INT32
},
59 [SERVICE_TXT
] = { .name
= "txt", .type
= BLOBMSG_TYPE_ARRAY
},
62 static const struct uci_blob_param_list service_attr_list
= {
63 .n_params
= __SERVICE_MAX
,
64 .params
= service_policy
,
67 static struct blob_buf b
;
68 static struct avl_tree services
;
69 char *hostname
= NULL
;
70 static char *sdudp
= "_services._dns-sd._udp.local";
71 static char *sdtcp
= "_services._dns-sd._tcp.local";
74 service_name(char *domain
)
76 static char buffer
[256];
78 snprintf(buffer
, sizeof(buffer
), "%s.%s", hostname
, domain
);
84 service_send_ptr(struct uloop_fd
*u
, struct service
*s
)
86 unsigned char buffer
[MAX_NAME_LEN
];
87 char *host
= service_name(s
->service
);
88 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
93 dns_add_answer(TYPE_PTR
, buffer
, len
);
97 service_send_ptr_c(struct uloop_fd
*u
, char *host
)
99 unsigned char buffer
[MAX_NAME_LEN
];
100 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
105 dns_add_answer(TYPE_PTR
, buffer
, len
);
109 service_send_a(struct uloop_fd
*u
)
111 unsigned char buffer
[MAX_NAME_LEN
];
112 char *host
= service_name("local");
113 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
116 if (!inet_aton(iface_ip
, &in
)) {
117 fprintf(stderr
, "%s is not valid\n", iface_ip
);
124 dns_add_answer(TYPE_A
, (uint8_t *) &in
.s_addr
, 4);
128 service_send_srv(struct uloop_fd
*u
, struct service
*s
)
130 unsigned char buffer
[MAX_NAME_LEN
];
131 struct dns_srv_data
*sd
;
132 char *host
= service_name("local");
133 int len
= dn_comp(host
, buffer
, MAX_NAME_LEN
, NULL
, NULL
);
138 sd
= malloc(len
+ sizeof(struct dns_srv_data
));
142 memset(sd
, 0, sizeof(struct dns_srv_data
));
143 sd
->port
= cpu_to_be16(s
->port
);
144 memcpy(&sd
[1], buffer
, len
);
145 host
= service_name(s
->service
);
146 dns_add_answer(TYPE_SRV
, (uint8_t *) sd
, len
+ sizeof(struct dns_srv_data
));
150 #define TOUT_LOOKUP 60
153 service_timeout(struct service
*s
)
155 time_t t
= time(NULL
);
157 if (t
- s
->t
<= TOUT_LOOKUP
)
166 service_reply_a(struct uloop_fd
*u
, int type
)
173 dns_send_answer(u
, service_name("local"));
177 service_reply(struct uloop_fd
*u
, char *match
)
181 avl_for_each_element(&services
, s
, avl
) {
182 char *host
= service_name(s
->service
);
183 char *service
= strstr(host
, "._");
185 if (!s
->active
|| !service
|| !service_timeout(s
))
190 if (match
&& strcmp(match
, s
->service
))
194 service_send_ptr(u
, s
);
195 dns_send_answer(u
, service
);
198 service_send_srv(u
, s
);
199 if (s
->txt
&& s
->txt_len
)
200 dns_add_answer(TYPE_TXT
, (uint8_t *) s
->txt
, s
->txt_len
);
201 dns_send_answer(u
, host
);
209 dns_send_answer(u
, service_name("local"));
213 service_announce_services(struct uloop_fd
*u
, char *service
)
218 if (!strcmp(service
, sdudp
))
220 else if (strcmp(service
, sdtcp
))
223 avl_for_each_element(&services
, s
, avl
) {
224 if (!strstr(s
->service
, "._tcp") && tcp
)
226 if (!strstr(s
->service
, "._udp") && !tcp
)
230 service_send_ptr_c(u
, s
->service
);
232 dns_send_answer(u
, sdtcp
);
234 dns_send_answer(u
, sdudp
);
235 service_reply(u
, s
->service
);
240 service_announce(struct uloop_fd
*u
)
242 service_announce_services(u
, sdudp
);
243 service_announce_services(u
, sdtcp
);
247 service_load(char *path
)
249 struct blob_attr
*txt
, *cur
, *_tb
[__SERVICE_MAX
];
253 if (glob(path
, GLOB_NOESCAPE
| GLOB_MARK
, NULL
, &gl
))
256 for (i
= 0; i
< gl
.gl_pathc
; i
++) {
257 blob_buf_init(&b
, 0);
259 if (!blobmsg_add_json_from_file(&b
, gl
.gl_pathv
[i
]))
261 blob_for_each_attr(cur
, b
.head
, rem
) {
263 char *d_service
, *d_txt
, *d_daemon
;
266 blobmsg_parse(service_policy
, ARRAY_SIZE(service_policy
),
267 _tb
, blobmsg_data(cur
), blobmsg_data_len(cur
));
268 if (!_tb
[SERVICE_PORT
] || !_tb
[SERVICE_TXT
])
271 s
= calloc_a(sizeof(*s
),
272 &d_daemon
, strlen(gl
.gl_pathv
[i
]) + 1,
273 &d_service
, strlen(blobmsg_name(cur
)) + 1,
274 &d_txt
, blobmsg_data_len(_tb
[SERVICE_TXT
]));
278 s
->port
= blobmsg_get_u32(_tb
[SERVICE_PORT
]);
279 s
->service
= d_service
;
280 s
->daemon
= d_daemon
;
281 strcpy(s
->service
, blobmsg_name(cur
));
282 strcpy(s
->daemon
, gl
.gl_pathv
[i
]);
283 s
->avl
.key
= s
->service
;
286 avl_insert(&services
, &s
->avl
);
288 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
)
289 s
->txt_len
+= 1 + strlen(blobmsg_get_string(txt
));
294 d_txt
= s
->txt
= malloc(s
->txt_len
);
298 blobmsg_for_each_attr(txt
, _tb
[SERVICE_TXT
], rem2
) {
299 int len
= strlen(blobmsg_get_string(txt
));
306 memcpy(d_txt
, blobmsg_get_string(txt
), len
);
317 hostname
= get_hostname();
318 avl_init(&services
, avl_strcmp
, true, NULL
);
319 service_load("/tmp/run/mdnsd/*");
323 service_cleanup(void)