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.
15 #include <sys/types.h>
23 #include <sys/types.h>
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/nameser.h>
28 #include <asm/byteorder.h>
31 #include <libubox/usock.h>
32 #include <libubox/uloop.h>
33 #include <libubox/avl-cmp.h>
41 #include "interface.h"
43 static struct uloop_timeout reconnect;
44 char *iface_name = "eth0";
47 read_socket(struct uloop_fd *u, unsigned int events)
49 struct interface *iface = container_of(u, struct interface, fd);
50 static uint8_t buffer[8 * 1024];
57 uloop_timeout_set(&reconnect, 1000);
61 len = read(u->fd, buffer, sizeof(buffer));
63 fprintf(stderr, "read failed: %s\n", strerror(errno));
67 dns_handle_packet(iface, buffer, len);
71 reconnect_socket(struct uloop_timeout *timeout)
73 cur_iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK, MCAST_ADDR, "5353");
74 if (cur_iface->fd.fd < 0) {
75 fprintf(stderr, "failed to add listener: %s\n", strerror(errno));
76 uloop_timeout_set(&reconnect, 1000);
78 if (interface_socket_setup(cur_iface)) {
79 uloop_timeout_set(&reconnect, 1000);
80 cur_iface->fd.fd = -1;
84 uloop_fd_add(&cur_iface->fd, ULOOP_READ);
86 dns_send_question(cur_iface, "_services._dns-sd._udp.local", TYPE_PTR);
87 announce_init(cur_iface);
92 main(int argc, char **argv)
96 while ((ch = getopt(argc, argv, "h:t:i:d")) != -1) {
106 fprintf(stderr, "invalid ttl\n");
122 if (interface_add(iface_name)) {
123 fprintf(stderr, "Failed to add interface %s\n", iface_name);
137 cur_iface->fd.cb = read_socket;
138 reconnect.cb = reconnect_socket;
140 uloop_timeout_set(&reconnect, 100);