--- /dev/null
+variables:
+ CI_TARGET_BUILD_DEPENDS: umdns
+
+include:
+ - remote: https://gitlab.com/ynezz/openwrt-ci/raw/master/openwrt-ci/gitlab/main.yml
+ - remote: https://gitlab.com/ynezz/openwrt-ci/raw/master/openwrt-ci/gitlab/pipeline.yml
SET(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
-SET(SOURCES main.c dns.c announce.c cache.c service.c util.c ubus.c interface.c)
+SET(SOURCES dns.c announce.c cache.c service.c util.c ubus.c interface.c)
-SET(LIBS ubox ubus resolv blobmsg_json json-c)
+FIND_PATH(ubox_include_dir NAMES libubox/usock.h)
+FIND_PATH(ubus_include_dir NAMES libubus.h)
+FIND_PATH(udebug_include_dir NAMES udebug.h)
+INCLUDE_DIRECTORIES(${ubox_include_dir} ${ubus_include_dir} ${udebug_include_dir})
+
+FIND_LIBRARY(ubox NAMES ubox)
+FIND_LIBRARY(ubus NAMES ubus)
+FIND_LIBRARY(blobmsg_json NAMES blobmsg_json)
+FIND_LIBRARY(json NAMES json json-c)
+FIND_LIBRARY(udebug NAMES udebug)
+
+SET(LIBS ${ubox} ${ubus} ${blobmsg_json} ${json} resolv ${udebug})
IF(DEBUG)
ADD_DEFINITIONS(-DDEBUG -g3)
ENDIF()
-ADD_EXECUTABLE(umdns ${SOURCES})
+ADD_LIBRARY(umdns-lib STATIC ${SOURCES})
+TARGET_LINK_LIBRARIES(umdns-lib ${LIBS})
+
+ADD_EXECUTABLE(umdns main.c)
+TARGET_LINK_LIBRARIES(umdns umdns-lib)
+
+IF(UNIT_TESTING)
+ ENABLE_TESTING()
+ ADD_SUBDIRECTORY(tests)
-TARGET_LINK_LIBRARIES(umdns ${LIBS})
+ IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
+ ADD_LIBRARY(umdns-lib-san STATIC ${SOURCES})
+ TARGET_COMPILE_OPTIONS(umdns-lib-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all)
+ TARGET_LINK_OPTIONS(umdns-lib-san PRIVATE -fsanitize=undefined,address,leak)
+ TARGET_LINK_LIBRARIES(umdns-lib-san ${LIBS})
+
+ ADD_EXECUTABLE(umdns-san main.c ${SOURCES})
+ TARGET_COMPILE_OPTIONS(umdns-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all)
+ TARGET_LINK_OPTIONS(umdns-san PRIVATE -fsanitize=undefined,address,leak)
+ TARGET_LINK_LIBRARIES(umdns-san umdns-lib-san)
+ ENDIF()
+
+ENDIF()
INSTALL(TARGETS umdns
RUNTIME DESTINATION sbin
/* Fall through */
case STATE_ANNOUNCE:
- dns_reply_a(iface, NULL, announce_ttl);
+ dns_reply_a(iface, NULL, announce_ttl, NULL);
+ dns_reply_a_additional(iface, NULL, announce_ttl);
service_announce_services(iface, NULL, announce_ttl);
uloop_timeout_set(timeout, announce_ttl * 800);
break;
static struct uloop_timeout cache_gc;
struct avl_tree services;
-AVL_TREE(records, avl_strcmp, true, NULL);
+
+static int avl_strcasecmp(const void *k1, const void *k2, void *ptr)
+{
+ return strcasecmp(k1, k2);
+}
+
+AVL_TREE(records, avl_strcasecmp, true, NULL);
static void
cache_record_free(struct cache_record *r)
int
cache_init(void)
{
- avl_init(&services, avl_strcmp, true, NULL);
+ avl_init(&services, avl_strcasecmp, true, NULL);
cache_gc.cb = cache_gc_timer;
uloop_timeout_set(&cache_gc, 10000);
avl_insert(&services, &s->avl);
if (!hlen)
- dns_send_question(iface, NULL, entry, TYPE_PTR, iface->multicast);
+ dns_send_question(iface, NULL, entry, TYPE_PTR, interface_multicast(iface));
return s;
}
{
struct cache_record *l = avl_find_element(&records, record, l, avl);
- if (!l)
- return NULL;
-
- while (l && l->record && !strcmp(l->record, record)) {
+ while (l && !strcmp(l->record, record)) {
struct cache_record *r = l;
- l = avl_next_element(l, avl);
+ l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
if (r->type != type)
continue;
{
struct cache_record *l = avl_find_element(&records, record, l, avl);
- if (!l)
- return 0;
-
- while (l && !avl_is_last(&records, &l->avl) && !strcmp(l->record, record)) {
+ while (l && !strcmp(l->record, record)) {
struct cache_record *r = l;
- l = avl_next_element(l, avl);
+ l = !avl_is_last(&records, &l->avl) ? avl_next_element(l, avl) : NULL;
if ((r->type != TYPE_A) && (r->type != TYPE_AAAA))
continue;
return 1;
if (rdlength <= 2)
return;
- memcpy(rdata_buffer, &rdata[1], rdlength);
+ memcpy(rdata_buffer, &rdata[1], rdlength-1);
rdata_buffer[rdlength] = rdata_buffer[rdlength + 1] = '\0';
tlen = rdlength + 1;
p = &rdata_buffer[*rdata];
r->rdlength = dlen;
r->time = now;
r->iface = iface;
- if (iface->v6)
+ if (interface_ipv6(iface))
memcpy(&r->from, from, sizeof(struct sockaddr_in6));
else
memcpy(&r->from, from, sizeof(struct sockaddr_in));
}
void
-cache_dump_records(struct blob_buf *buf, const char *name)
+cache_dump_records(struct blob_buf *buf, const char *name, int array,
+ const char **hostname)
{
struct cache_record *r, *last, *next;
const char *txt;
char buffer[INET6_ADDRSTRLEN];
+ void *c = NULL;
last = avl_last_element(&records, last, avl);
+ for (r = avl_find_element(&records, name, r, avl); r; r = next) {
+ switch (r->type) {
+ case TYPE_A:
+ if (!c && array)
+ c = blobmsg_open_array(buf, "ipv4");
+ if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
+ blobmsg_add_string(buf, "ipv4", buffer);
+ break;
+ }
+
+ if (r == last)
+ break;
+
+ next = avl_next_element(r, avl);
+ if (strcmp(r->record, next->record) != 0)
+ break;
+ }
+
+ if (c) {
+ blobmsg_close_array(buf, c);
+ c = NULL;
+ }
+
+ for (r = avl_find_element(&records, name, r, avl); r; r = next) {
+ switch (r->type) {
+ case TYPE_AAAA:
+ if (!c && array)
+ c = blobmsg_open_array(buf, "ipv6");
+ if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
+ blobmsg_add_string(buf, "ipv6", buffer);
+ break;
+ }
+
+ if (r == last)
+ break;
+
+ next = avl_next_element(r, avl);
+ if (strcmp(r->record, next->record) != 0)
+ break;
+ }
+
+ if (c) {
+ blobmsg_close_array(buf, c);
+ c = NULL;
+ }
+
for (r = avl_find_element(&records, name, r, avl); r; r = next) {
switch (r->type) {
case TYPE_TXT:
if (r->txt && strlen(r->txt)) {
+ if (array)
+ c = blobmsg_open_array(buf, "txt");
+
txt = r->txt;
do {
blobmsg_add_string(buf, "txt", txt);
txt = &txt[strlen(txt) + 1];
} while (*txt);
+ if (array)
+ blobmsg_close_array(buf, c);
}
break;
case TYPE_SRV:
+ if (r->rdata) {
+ blobmsg_add_string(buf, "host", (char *)r->rdata + sizeof(struct dns_srv_data));
+ if (hostname)
+ *hostname = (char *)r->rdata + sizeof(struct dns_srv_data);
+ }
if (r->port)
blobmsg_add_u32(buf, "port", r->port);
break;
-
- case TYPE_A:
- if ((r->rdlength == 4) && inet_ntop(AF_INET, r->rdata, buffer, INET6_ADDRSTRLEN))
- blobmsg_add_string(buf, "ipv4", buffer);
- break;
-
- case TYPE_AAAA:
- if ((r->rdlength == 16) && inet_ntop(AF_INET6, r->rdata, buffer, INET6_ADDRSTRLEN))
- blobmsg_add_string(buf, "ipv6", buffer);
- break;
}
if (r == last)
int blen, char *name, struct dns_answer *a, uint8_t *rdata,
int flush);
int cache_host_is_known(char *record);
-void cache_dump_records(struct blob_buf *buf, const char *name);
+void cache_dump_records(struct blob_buf *buf, const char *name, int array,
+ const char **hostname);
void cache_dump_recursive(struct blob_buf *b, const char *name, uint16_t type, struct interface *iface);
#endif
}
void
-dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl)
+dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl, const char *hostname)
{
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
dns_add_answer(TYPE_A, (uint8_t *) &sa->sin_addr, 4, ttl);
}
if (ifa->ifa_addr->sa_family == AF_INET6) {
- uint8_t ll_prefix[] = {0xfe, 0x80 };
sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
- if (!memcmp(&sa6->sin6_addr, &ll_prefix, 2))
- dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
+ dns_add_answer(TYPE_AAAA, (uint8_t *) &sa6->sin6_addr, 16, ttl);
}
}
- dns_send_answer(iface, to, mdns_hostname_local);
+ dns_send_answer(iface, to, hostname ? hostname : mdns_hostname_local);
freeifaddrs(ifap);
}
+void
+dns_reply_a_additional(struct interface *iface, struct sockaddr *to, int ttl)
+{
+ struct hostname *h;
+
+ vlist_for_each_element(&hostnames, h, node)
+ dns_reply_a(iface, to, ttl, h->hostname);
+}
+
static int
scan_name(const uint8_t *buffer, int len)
{
if (IS_COMPRESSED(l))
return offset + 2;
+ if (l + 1 > len) return -1;
len -= l + 1;
offset += l + 1;
buffer += l + 1;
dns_consume_header(uint8_t **data, int *len)
{
struct dns_header *h = (struct dns_header *) *data;
- uint16_t *swap = (uint16_t *) h;
- int endianess = 6;
if (*len < sizeof(struct dns_header))
return NULL;
- while (endianess--) {
- *swap = be16_to_cpu(*swap);
- swap++;
- }
+ h->id = be16_to_cpu(h->id);
+ h->flags = be16_to_cpu(h->flags);
+ h->questions = be16_to_cpu(h->questions);
+ h->answers = be16_to_cpu(h->answers);
+ h->authority = be16_to_cpu(h->authority);
+ h->additional = be16_to_cpu(h->additional);
*len -= sizeof(struct dns_header);
*data += sizeof(struct dns_header);
dns_consume_question(uint8_t **data, int *len)
{
struct dns_question *q = (struct dns_question *) *data;
- uint16_t *swap = (uint16_t *) q;
- int endianess = 2;
if (*len < sizeof(struct dns_question))
return NULL;
- while (endianess--) {
- *swap = be16_to_cpu(*swap);
- swap++;
- }
+ q->type = be16_to_cpu(q->type);
+ q->class = be16_to_cpu(q->class);
*len -= sizeof(struct dns_question);
*data += sizeof(struct dns_question);
struct dns_answer *a;
uint8_t *rdata;
- if (!name) {
+ if (!name || *rlen < 0) {
fprintf(stderr, "dropping: bad question\n");
return -1;
}
/* TODO: Multicast if more than one quarter of TTL has passed */
if (q->class & CLASS_UNICAST) {
to = from;
- if (iface->multicast)
- iface = iface->peer;
+ if (interface_multicast(iface))
+ iface = interface_get(iface->name, iface->type | SOCKTYPE_BIT_UNICAST);
}
DBG(1, "Q -> %s %s\n", dns_type_string(q->type), name);
switch (q->type) {
case TYPE_ANY:
if (!strcmp(name, mdns_hostname_local)) {
- dns_reply_a(iface, to, announce_ttl);
+ dns_reply_a(iface, to, announce_ttl, NULL);
+ dns_reply_a_additional(iface, to, announce_ttl);
service_reply(iface, to, NULL, NULL, announce_ttl);
}
break;
case TYPE_PTR:
if (!strcmp(name, C_DNS_SD)) {
- dns_reply_a(iface, to, announce_ttl);
+ dns_reply_a(iface, to, announce_ttl, NULL);
+ dns_reply_a_additional(iface, to, announce_ttl);
service_announce_services(iface, to, announce_ttl);
} else {
if (name[0] == '_') {
if (host)
*host = '\0';
if (!strcmp(umdns_host_label, name))
- dns_reply_a(iface, to, announce_ttl);
+ dns_reply_a(iface, to, announce_ttl, NULL);
break;
};
}
return;
}
- if (h->questions && !iface->multicast && port != MCAST_PORT)
+ if (h->questions && !interface_multicast(iface) && port != MCAST_PORT)
/* silently drop unicast questions that dont originate from port 5353 */
return;
char *name = dns_consume_name(buffer, len, &b, &rlen);
struct dns_question *q;
- if (!name) {
+ if (!name || rlen < 0) {
fprintf(stderr, "dropping: bad name\n");
return;
}
uint16_t answers;
uint16_t authority;
uint16_t additional;
-};
+} __attribute__((packed));
struct dns_srv_data {
uint16_t priority;
void dns_init_answer(void);
void dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength, int ttl);
void dns_send_answer(struct interface *iface, struct sockaddr *to, const char *answer);
-void dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl);
+void dns_reply_a(struct interface *iface, struct sockaddr *to, int ttl, const char *hostname);
+void dns_reply_a_additional(struct interface *iface, struct sockaddr *to, int ttl);
const char* dns_type_string(uint16_t type);
void dns_handle_packet(struct interface *iface, struct sockaddr *s, uint16_t port, uint8_t *buf, int len);
#include "announce.h"
#include "service.h"
+static struct uloop_fd ufd[] = {
+ [SOCK_UC_IPV4] = { .fd = -1 },
+ [SOCK_UC_IPV6] = { .fd = -1 },
+ [SOCK_MC_IPV4] = { .fd = -1 },
+ [SOCK_MC_IPV6] = { .fd = -1 },
+};
+
static int
interface_send_packet4(struct interface *iface, struct sockaddr_in *to, struct iovec *iov, int iov_len)
{
static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
- static struct sockaddr_in a;
+ static struct sockaddr_in a = {};
static struct msghdr m = {
.msg_name = (struct sockaddr *) &a,
.msg_namelen = sizeof(a),
};
struct in_pktinfo *pkti;
struct cmsghdr *cmsg;
- int fd = iface->fd.fd;
+ int fd;
a.sin_family = AF_INET;
a.sin_port = htons(MCAST_PORT);
pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
pkti->ipi_ifindex = iface->ifindex;
- if (iface->multicast) {
+ fd = ufd[iface->type].fd;
+ if (interface_multicast(iface)) {
a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
if (to)
fprintf(stderr, "Ignoring IPv4 address for multicast interface\n");
interface_send_packet6(struct interface *iface, struct sockaddr_in6 *to, struct iovec *iov, int iov_len)
{
static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in6_pktinfo)) / sizeof(size_t)) + 1];
- static struct sockaddr_in6 a;
+ static struct sockaddr_in6 a = {};
static struct msghdr m = {
.msg_name = (struct sockaddr *) &a,
.msg_namelen = sizeof(a),
};
struct in6_pktinfo *pkti;
struct cmsghdr *cmsg;
- int fd = iface->fd.fd;
+ int fd;
a.sin6_family = AF_INET6;
a.sin6_port = htons(MCAST_PORT);
+ a.sin6_scope_id = iface->ifindex;
m.msg_iov = iov;
m.msg_iovlen = iov_len;
pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg);
pkti->ipi6_ifindex = iface->ifindex;
- if (iface->multicast) {
+ fd = ufd[iface->type].fd;
+ if (interface_multicast(iface)) {
inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr);
if (to)
fprintf(stderr, "Ignoring IPv6 address for multicast interface\n");
int
interface_send_packet(struct interface *iface, struct sockaddr *to, struct iovec *iov, int iov_len)
{
- if (!iface->multicast && !to) {
+ if (!interface_multicast(iface) && !to) {
fprintf(stderr, "No IP address specified for unicast interface\n");
errno = EINVAL;
return -1;
}
if (debug > 1) {
- fprintf(stderr, "TX ipv%d: %s\n", iface->v6 * 2 + 4, iface->name);
- fprintf(stderr, " multicast: %d\n", iface->multicast);
+ fprintf(stderr, "TX ipv%d: %s\n", interface_ipv6(iface) ? 6 : 4, iface->name);
+ fprintf(stderr, " multicast: %d\n", interface_multicast(iface));
}
- if (iface->v6)
+ if (interface_ipv6(iface))
return interface_send_packet6(iface, (struct sockaddr_in6 *)to, iov, iov_len);
return interface_send_packet4(iface, (struct sockaddr_in *)to, iov, iov_len);
}
-static void interface_close(struct interface *iface)
+static struct interface *interface_lookup(unsigned int ifindex, enum umdns_socket_type type)
{
- if (iface->fd.fd < 0)
- return;
+ struct interface *iface;
- announce_free(iface);
- uloop_fd_delete(&iface->fd);
- close(iface->fd.fd);
- iface->fd.fd = -1;
+ vlist_for_each_element(&interfaces, iface, node)
+ if (iface->ifindex == ifindex && iface->type == type)
+ return iface;
+
+ return NULL;
}
static void interface_free(struct interface *iface)
{
- uloop_timeout_cancel(&iface->reconnect);
- interface_close(iface);
+ cache_cleanup(iface);
+ announce_free(iface);
+ free(iface->addrs.v4);
free(iface);
}
static void
read_socket4(struct uloop_fd *u, unsigned int events)
{
- struct interface *iface = container_of(u, struct interface, fd);
+ enum umdns_socket_type type = (enum umdns_socket_type)(u - ufd);
+ struct interface *iface;
static uint8_t buffer[8 * 1024];
struct iovec iov[1];
char cmsg[CMSG_SPACE(sizeof(struct in_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
struct msghdr msg;
socklen_t len;
struct sockaddr_in from;
- int flags = 0, ifindex = -1;
+ int flags = 0;
uint8_t ttl = 0;
struct in_pktinfo *inp = NULL;
+ bool valid_src = false;
if (u->eof) {
- interface_close(iface);
- uloop_timeout_set(&iface->reconnect, 1000);
+ uloop_end();
return;
}
}
}
+ if (!inp)
+ return;
+
+ iface = interface_lookup(inp->ipi_ifindex, type);
+ if (!iface)
+ return;
+
if (debug > 1) {
char buf[256];
fprintf(stderr, "RX ipv4: %s\n", iface->name);
- fprintf(stderr, " multicast: %d\n", iface->multicast);
+ fprintf(stderr, " multicast: %d\n", interface_multicast(iface));
inet_ntop(AF_INET, &from.sin_addr, buf, 256);
fprintf(stderr, " src %s:%d\n", buf, ntohs(from.sin_port));
inet_ntop(AF_INET, &inp->ipi_spec_dst, buf, 256);
fprintf(stderr, " dst %s\n", buf);
inet_ntop(AF_INET, &inp->ipi_addr, buf, 256);
fprintf(stderr, " real %s\n", buf);
+ fprintf(stderr, " ttl %u\n", ttl);
+ }
+
+ for (size_t i = 0; i < iface->addrs.n_addr; i++) {
+ if (!interface_valid_src((void *)&iface->addrs.v4[i].addr,
+ (void *)&iface->addrs.v4[i].mask,
+ (void *) &from.sin_addr, 4)) {
+ valid_src = true;
+ break;
+ }
}
- if (inp->ipi_ifindex != iface->ifindex)
- fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
- else if (!interface_valid_src((void *) &iface->v4_addr, (void *) &iface->v4_netmask, (void *) &from.sin_addr, 4))
- dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin_port), buffer, len);
+ if (!valid_src)
+ return;
+
+ dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin_port), buffer, len);
}
static void
read_socket6(struct uloop_fd *u, unsigned int events)
{
- struct interface *iface = container_of(u, struct interface, fd);
+ enum umdns_socket_type type = (enum umdns_socket_type)(u - ufd);
+ struct interface *iface;
static uint8_t buffer[8 * 1024];
struct iovec iov[1];
char cmsg6[CMSG_SPACE(sizeof(struct in6_pktinfo)) + CMSG_SPACE(sizeof(int)) + 1];
struct msghdr msg;
socklen_t len;
struct sockaddr_in6 from;
- int flags = 0, ifindex = -1;
+ int flags = 0;
int ttl = 0;
struct in6_pktinfo *inp = NULL;
+ bool valid_src = false;
if (u->eof) {
- interface_close(iface);
- uloop_timeout_set(&iface->reconnect, 1000);
+ uloop_end();
return;
}
}
}
+ if (!inp)
+ return;
+
+ iface = interface_lookup(inp->ipi6_ifindex, type);
+ if (!iface)
+ return;
+
if (debug > 1) {
char buf[256];
fprintf(stderr, "RX ipv6: %s\n", iface->name);
- fprintf(stderr, " multicast: %d\n", iface->multicast);
+ fprintf(stderr, " multicast: %d\n", interface_multicast(iface));
inet_ntop(AF_INET6, &from.sin6_addr, buf, 256);
fprintf(stderr, " src %s:%d\n", buf, ntohs(from.sin6_port));
inet_ntop(AF_INET6, &inp->ipi6_addr, buf, 256);
fprintf(stderr, " dst %s\n", buf);
+ fprintf(stderr, " ttl %u\n", ttl);
}
- if (inp->ipi6_ifindex != iface->ifindex)
- fprintf(stderr, "invalid iface index %d != %d\n", ifindex, iface->ifindex);
- else if (!interface_valid_src((void *) &iface->v6_addr, (void *) &iface->v6_netmask, (void *) &from.sin6_addr, 16))
- dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin6_port), buffer, len);
+ for (size_t i = 0; i < iface->addrs.n_addr; i++) {
+ if (!interface_valid_src((void *)&iface->addrs.v6[i].addr,
+ (void *)&iface->addrs.v6[i].mask,
+ (void *)&from.sin6_addr, 6)) {
+ valid_src = true;
+ break;
+ }
+ }
+
+ if (!valid_src)
+ return;
+
+ dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin6_port), buffer, len);
}
static int
interface_mcast_setup4(struct interface *iface)
{
struct ip_mreqn mreq;
- uint8_t ttl = 255;
- int no = 0;
- struct sockaddr_in sa = { 0 };
- int fd = iface->fd.fd;
+ struct sockaddr_in sa = {};
+ int fd = ufd[SOCK_MC_IPV4].fd;
sa.sin_family = AF_INET;
sa.sin_port = htons(MCAST_PORT);
inet_pton(AF_INET, MCAST_ADDR, &sa.sin_addr);
memset(&mreq, 0, sizeof(mreq));
- mreq.imr_address.s_addr = iface->v4_addr.s_addr;
mreq.imr_multiaddr = sa.sin_addr;
mreq.imr_ifindex = iface->ifindex;
-
- if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
- fprintf(stderr, "ioctl failed: IP_MULTICAST_TTL\n");
+ mreq.imr_address.s_addr = iface->addrs.v4[0].addr.s_addr;
/* Some network drivers have issues with dropping membership of
* mcast groups when the iface is down, but don't allow rejoining
* -- this was copied from avahi --
*/
setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
-
- if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
- fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
- close(fd);
- fd = -1;
- return -1;
- }
-
- if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0)
- fprintf(stderr, "ioctl failed: IP_MULTICAST_LOOP\n");
+ setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
return 0;
}
static int
-interface_socket_setup6(struct interface *iface)
+interface_mcast_setup6(struct interface *iface)
{
struct ipv6_mreq mreq;
- int ttl = 255;
- int no = 0;
- struct sockaddr_in6 sa = { 0 };
- int fd = iface->fd.fd;
+ struct sockaddr_in6 sa = {};
+ int fd = ufd[SOCK_MC_IPV6].fd;
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(MCAST_PORT);
mreq.ipv6mr_multiaddr = sa.sin6_addr;
mreq.ipv6mr_interface = iface->ifindex;
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
- fprintf(stderr, "ioctl failed: IPV6_MULTICAST_HOPS\n");
-
setsockopt(fd, IPPROTO_IPV6, IPV6_LEAVE_GROUP, &mreq, sizeof(mreq));
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
- fprintf(stderr, "failed to join multicast group: %s\n", strerror(errno));
- close(fd);
- fd = -1;
- return -1;
- }
-
- if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0)
- fprintf(stderr, "ioctl failed: IPV6_MULTICAST_LOOP\n");
+ setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
return 0;
}
-static void
-reconnect_socket4(struct uloop_timeout *timeout)
+static void interface_start(struct interface *iface)
{
- struct interface *iface = container_of(timeout, struct interface, reconnect);
- int ttl = 255;
- int yes = 1;
-
- iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV4ONLY,
- (iface->multicast) ? (iface->mcast_addr) : (iface->v4_addrs), "5353");
- if (iface->fd.fd < 0) {
- fprintf(stderr, "failed to add listener %s: %s\n", iface->mcast_addr, strerror(errno));
- goto retry;
- }
-
- if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_BINDTODEVICE, iface->name, strlen(iface->name) < 0))
- fprintf(stderr, "ioctl failed: SO_BINDTODEVICE\n");
-
- if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
- fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
-
- if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0)
- fprintf(stderr, "ioctl failed: IP_TTL\n");
+ if (iface->type & SOCKTYPE_BIT_UNICAST)
+ return;
- if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0)
- fprintf(stderr, "ioctl failed: IP_RECVTTL\n");
+ if (iface->type & SOCKTYPE_BIT_IPV6)
+ interface_mcast_setup6(iface);
+ else
+ interface_mcast_setup4(iface);
- if (setsockopt(iface->fd.fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0)
- fprintf(stderr, "ioctl failed: IP_PKTINFO\n");
+ dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0);
+ announce_init(iface);
+}
- if (iface->multicast && interface_mcast_setup4(iface)) {
- iface->fd.fd = -1;
- goto retry;
- }
+static bool
+iface_equal(struct interface *if_old, struct interface *if_new)
+{
+ size_t addr_size;
- uloop_fd_add(&iface->fd, ULOOP_READ);
- if (iface->multicast) {
- dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0);
- announce_init(iface);
- }
+ if (if_old->ifindex != if_new->ifindex ||
+ if_old->addrs.n_addr != if_new->addrs.n_addr)
+ return false;
- return;
+ if (if_old->type & SOCKTYPE_BIT_IPV6)
+ addr_size = sizeof(*if_old->addrs.v6);
+ else
+ addr_size = sizeof(*if_old->addrs.v4);
+ addr_size *= if_old->addrs.n_addr;
+ if (memcmp(if_old->addrs.v4, if_new->addrs.v4, addr_size) != 0)
+ return false;
-retry:
- uloop_timeout_set(timeout, 1000);
+ return true;
}
static void
-reconnect_socket6(struct uloop_timeout *timeout)
+iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old)
{
- struct interface *iface = container_of(timeout, struct interface, reconnect);
- char mcast_addr[128];
- int ttl = 255;
- int yes = 1;
-
- snprintf(mcast_addr, sizeof(mcast_addr), "%s%%%s", (iface->multicast) ? (iface->mcast_addr) : (iface->v6_addrs), iface->name);
- iface->fd.fd = usock(USOCK_UDP | USOCK_SERVER | USOCK_NONBLOCK | USOCK_IPV6ONLY, mcast_addr, "5353");
- if (iface->fd.fd < 0) {
- fprintf(stderr, "failed to add listener %s: %s\n", mcast_addr, strerror(errno));
- goto retry;
+ struct interface *if_old = container_of_safe(node_old, struct interface, node);
+ struct interface *if_new = container_of_safe(node_new, struct interface, node);
+
+ if (if_old && if_new) {
+ if (!iface_equal(if_old, if_new))
+ cache_cleanup(if_old);
+ free(if_old->addrs.v4);
+ if_old->addrs = if_new->addrs;
+ if_old->ifindex = if_new->ifindex;
+ free(if_new);
+ return;
}
- if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_BINDTODEVICE, iface->name, strlen(iface->name) < 0))
- fprintf(stderr, "ioctl failed: SO_BINDTODEVICE\n");
+ if (if_old)
+ interface_free(if_old);
- if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0)
- fprintf(stderr, "ioctl failed: IPV6_UNICAST_HOPS\n");
+ if (if_new)
+ interface_start(if_new);
+}
- if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)) < 0)
- fprintf(stderr, "ioctl failed: IPV6_RECVPKTINFO\n");
+static int interface_init_socket(enum umdns_socket_type type)
+{
+ struct sockaddr_in6 local6 = {
+ .sin6_family = AF_INET6
+ };
+ struct sockaddr_in local = {
+ .sin_family = AF_INET
+ };
+ uint8_t ttl = 255;
+ int ittl = 255;
+ int yes = 1;
+ int no = 0;
+ int fd;
+ int af = (type & SOCKTYPE_BIT_IPV6) ? AF_INET6 : AF_INET;
- if (setsockopt(iface->fd.fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes)) < 0)
- fprintf(stderr, "ioctl failed: IPV6_RECVHOPLIMIT\n");
+ if (ufd[type].fd >= 0)
+ return 0;
- if (setsockopt(iface->fd.fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0)
- fprintf(stderr, "ioctl failed: SO_REUSEADDR\n");
+ ufd[type].fd = fd = socket(af, SOCK_DGRAM, 0);
+ if (fd < 0)
+ return -1;
- if (iface->multicast && interface_socket_setup6(iface)) {
- iface->fd.fd = -1;
- goto retry;
+ setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
+#ifdef SO_REUSEPORT
+ setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &yes, sizeof(yes));
+#endif
+
+ switch (type) {
+ case SOCK_UC_IPV4:
+ case SOCK_UC_IPV6:
+ break;
+ case SOCK_MC_IPV4:
+ setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
+ setsockopt(fd, IPPROTO_IP, IP_TTL, &ittl, sizeof(ittl));
+ setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &no, sizeof(no));
+ local.sin_port = htons(MCAST_PORT);
+ break;
+ case SOCK_MC_IPV6:
+ setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl));
+ setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl));
+ setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes));
+ setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no));
+ local6.sin6_port = htons(MCAST_PORT);
+ break;
}
- uloop_fd_add(&iface->fd, ULOOP_READ);
+ if (type & SOCKTYPE_BIT_IPV6) {
+ ufd[type].cb = read_socket6;
+ if (bind(fd, (struct sockaddr *)&local6, sizeof(local6)) < 0)
+ goto error;
- if (iface->multicast) {
- dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0);
- announce_init(iface);
- }
+ setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes));
+ setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &yes, sizeof(yes));
+ } else {
+ ufd[type].cb = read_socket4;
+ if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
+ goto error;
- return;
+ setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes));
+ setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes));
+ }
-retry:
- uloop_timeout_set(timeout, 1000);
-}
+ uloop_fd_add(&ufd[type], ULOOP_READ);
+ return 0;
-static void interface_start(struct interface *iface)
-{
- if (iface->v6) {
- iface->fd.cb = read_socket6;
- iface->reconnect.cb = reconnect_socket6;
- } else {
- iface->fd.cb = read_socket4;
- iface->reconnect.cb = reconnect_socket4;
- }
- uloop_timeout_set(&iface->reconnect, 100);
+error:
+ close(ufd[type].fd);
+ return -1;
}
static void
-iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new,
- struct vlist_node *node_old)
+__interface_add(const char *name, enum umdns_socket_type type,
+ struct interface_addr_list *list)
{
struct interface *iface;
+ unsigned int ifindex;
+ char *id_buf;
- if (node_old) {
- iface = container_of(node_old, struct interface, node);
- cache_cleanup(iface);
- interface_free(iface);
- }
-
- if (node_new) {
- iface = container_of(node_new, struct interface, node);
- interface_start(iface);
- }
-}
+ if (interface_init_socket(type))
+ goto error;
-static struct interface* _interface_add(const char *name, int multicast, int v6)
-{
- struct interface *iface;
- char *name_buf;
- char *id_buf;
+ ifindex = if_nametoindex(name);
+ if (!ifindex)
+ goto error;
iface = calloc_a(sizeof(*iface),
- &name_buf, strlen(name) + 1,
- &id_buf, strlen(name) + 5);
-
- sprintf(id_buf, "%d_%d_%s", multicast, v6, name);
- iface->name = strcpy(name_buf, name);
- iface->id = id_buf;
- iface->ifindex = if_nametoindex(name);
- iface->fd.fd = -1;
- iface->multicast = multicast;
- iface->v6 = v6;
- if (v6)
- iface->mcast_addr = MCAST_ADDR6;
- else
- iface->mcast_addr = MCAST_ADDR;
+ &id_buf, strlen(name) + 3);
- if (iface->ifindex <= 0)
- goto error;
+ sprintf(id_buf, "%d_%s", type, name);
+ iface->name = id_buf + 2;
+ iface->ifindex = ifindex;
+ iface->type = type;
+ iface->addrs = *list;
- vlist_add(&interfaces, &iface->node, iface->id);
- return iface;
+ vlist_add(&interfaces, &iface->node, id_buf);
+ return;
error:
- free(iface);
- return NULL;
+ free(list->v4);
}
int interface_add(const char *name)
{
- struct interface *v4 = NULL, *v6 = NULL, *unicast;
struct ifaddrs *ifap, *ifa;
+ struct interface_addr_list addr4 = {}, addr6 = {};
getifaddrs(&ifap);
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (strcmp(ifa->ifa_name, name))
continue;
- if (ifa->ifa_addr->sa_family == AF_INET && !v4) {
- struct sockaddr_in *sa;
+ if (ifa->ifa_addr->sa_family == AF_INET) {
+ struct sockaddr_in *sin;
if (cfg_proto && (cfg_proto != 4))
continue;
- unicast = _interface_add(name, 0, 0);
- if (!unicast)
- continue;
- v4 = _interface_add(name, 1, 0);
- if (!v4)
- continue;
-
- sa = (struct sockaddr_in *) ifa->ifa_addr;
- memcpy(&v4->v4_addr, &sa->sin_addr, sizeof(v4->v4_addr));
- memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr));
-
- inet_ntop(AF_INET, &sa->sin_addr, v4->v4_addrs, sizeof(v4->v4_addrs));
- inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs));
-
- sa = (struct sockaddr_in *) ifa->ifa_netmask;
- memcpy(&unicast->v4_netmask, &sa->sin_addr, sizeof(unicast->v4_netmask));
- memcpy(&v4->v4_netmask, &sa->sin_addr, sizeof(v4->v4_netmask));
-
- v4->peer = unicast;
- unicast->peer = v4;
+ addr4.v4 = realloc(addr4.v4, (addr4.n_addr + 1) * sizeof(*addr4.v4));
+ sin = (struct sockaddr_in *) ifa->ifa_addr;
+ addr4.v4[addr4.n_addr].addr = sin->sin_addr;
+ sin = (struct sockaddr_in *) ifa->ifa_netmask;
+ addr4.v4[addr4.n_addr++].mask = sin->sin_addr;
}
- if (ifa->ifa_addr->sa_family == AF_INET6 && !v6) {
+ if (ifa->ifa_addr->sa_family == AF_INET6) {
uint8_t ll_prefix[] = {0xfe, 0x80 };
- struct sockaddr_in6 *sa6;
+ struct sockaddr_in6 *sin6;
if (cfg_proto && (cfg_proto != 6))
continue;
- sa6 = (struct sockaddr_in6 *) ifa->ifa_addr;
- if (memcmp(&sa6->sin6_addr, &ll_prefix, 2))
- continue;
-
- unicast = _interface_add(name, 0, 1);
- if (!unicast)
- continue;
- v6 = _interface_add(name, 1, 1);
- if (!v6)
+ sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
+ if (memcmp(&sin6->sin6_addr, &ll_prefix, 2))
continue;
- memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr));
- memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr));
+ addr6.v6 = realloc(addr6.v6, (addr6.n_addr + 1) * sizeof(*addr6.v6));
+ sin6 = (struct sockaddr_in6 *) ifa->ifa_addr;
+ addr6.v6[addr6.n_addr].addr = sin6->sin6_addr;
+ sin6 = (struct sockaddr_in6 *) ifa->ifa_netmask;
+ addr6.v6[addr6.n_addr++].mask = sin6->sin6_addr;
+ }
+ }
- inet_ntop(AF_INET6, &sa6->sin6_addr, v6->v6_addrs, sizeof(v6->v6_addrs));
- inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs));
+ freeifaddrs(ifap);
- sa6 = (struct sockaddr_in6 *) ifa->ifa_netmask;
- memcpy(&v6->v6_netmask, &sa6->sin6_addr, sizeof(v6->v6_netmask));
- memcpy(&unicast->v6_netmask, &sa6->sin6_addr, sizeof(unicast->v6_netmask));
+ if (addr4.n_addr) {
+ size_t addr_size = addr4.n_addr * sizeof(*addr4.v4);
+ void *addr_dup = malloc(addr_size);
- v6->peer = unicast;
- unicast->peer = v6;
- }
+ memcpy(addr_dup, addr4.v4, addr_size);
+ __interface_add(name, SOCK_UC_IPV4, &addr4);
+ addr4.v4 = addr_dup;
+ __interface_add(name, SOCK_MC_IPV4, &addr4);
}
- freeifaddrs(ifap);
+ if (addr6.n_addr) {
+ size_t addr_size = addr6.n_addr * sizeof(*addr6.v6);
+ void *addr_dup = malloc(addr_size);
+
+ memcpy(addr_dup, addr6.v6, addr_size);
+ __interface_add(name, SOCK_UC_IPV6, &addr6);
+ addr6.v6 = addr_dup;
+ __interface_add(name, SOCK_MC_IPV6, &addr6);
+ }
- return !v4 && !v6;
+ return !addr4.n_addr && !addr6.n_addr;
}
void interface_shutdown(void)
struct interface *iface;
vlist_for_each_element(&interfaces, iface, node)
- if (iface->fd.fd > 0 && iface->multicast) {
- dns_reply_a(iface, NULL, 0);
+ if (interface_multicast(iface)) {
+ dns_reply_a(iface, NULL, 0, NULL);
+ dns_reply_a_additional(iface, NULL, 0);
service_announce_services(iface, NULL, 0);
}
- vlist_for_each_element(&interfaces, iface, node)
- interface_close(iface);
+
+ for (size_t i = 0; i < ARRAY_SIZE(ufd); i++) {
+ uloop_fd_delete(&ufd[i]);
+ close(ufd[i].fd);
+ ufd[i].fd = -1;
+ }
}
-struct interface*
-interface_get(const char *name, int v6, int multicast)
+struct interface *interface_get(const char *name, enum umdns_socket_type type)
{
char id_buf[32];
- snprintf(id_buf, sizeof(id_buf), "%d_%d_%s", multicast, v6, name);
+ snprintf(id_buf, sizeof(id_buf), "%d_%s", type, name);
struct interface *iface = vlist_find(&interfaces, id_buf, iface, node);
return iface;
}
-VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);
+VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, true, false);
extern struct vlist_tree interfaces;
+#define SOCKTYPE_BIT_IPV6 (1 << 1)
+#define SOCKTYPE_BIT_UNICAST (1 << 0)
+
+enum umdns_socket_type {
+ SOCK_MC_IPV4 = 0,
+ SOCK_UC_IPV4 = SOCKTYPE_BIT_UNICAST,
+ SOCK_MC_IPV6 = SOCKTYPE_BIT_IPV6,
+ SOCK_UC_IPV6 = SOCKTYPE_BIT_IPV6 | SOCKTYPE_BIT_UNICAST,
+};
+
+struct interface_addr_list {
+ union {
+ struct {
+ struct in_addr addr, mask;
+ } *v4;
+ struct {
+ struct in6_addr addr, mask;
+ } *v6;
+ };
+ int n_addr;
+};
+
struct interface {
struct vlist_node node;
- struct interface *peer;
const char *name;
- char *id;
- struct uloop_fd fd;
- struct uloop_timeout reconnect;
-
- int v6;
- int multicast;
+ enum umdns_socket_type type;
int ifindex;
- struct in_addr v4_addr;
- struct in_addr v4_netmask;
- struct in6_addr v6_addr;
- struct in6_addr v6_netmask;
- char v4_addrs[16];
- char v6_addrs[64];
+ struct interface_addr_list addrs;
struct uloop_timeout announce_timer;
int announce_state;
-
- char *mcast_addr;
};
+static inline bool interface_multicast(struct interface *iface)
+{
+ return !(iface->type & SOCKTYPE_BIT_UNICAST);
+}
+
+static inline bool interface_ipv6(struct interface *iface)
+{
+ return !!(iface->type & SOCKTYPE_BIT_IPV6);
+}
+
int interface_add(const char *name);
+int interface_init(void);
void interface_shutdown(void);
int interface_send_packet(struct interface *iface, struct sockaddr *to, struct iovec *iov, int iov_len);
-struct interface* interface_get(const char *name, int v6, int multicast);
+struct interface* interface_get(const char *name, enum umdns_socket_type type);
#endif
#include <netinet/in.h>
#include <arpa/nameser.h>
+#include <udebug.h>
#include <libubus.h>
#include <libubox/uloop.h>
int cfg_proto = 0;
int cfg_no_subnet = 0;
+static struct udebug ud;
+static struct udebug_buf udb;
+static bool udebug_enabled;
+
+static void
+umdns_udebug_vprintf(const char *format, va_list ap)
+{
+ if (!udebug_enabled)
+ return;
+
+ udebug_entry_init(&udb);
+ udebug_entry_vprintf(&udb, format, ap);
+ udebug_entry_add(&udb);
+}
+
+void umdns_udebug_printf(const char *format, ...)
+{
+ va_list ap;
+
+ va_start(ap, format);
+ umdns_udebug_vprintf(format, ap);
+ va_end(ap);
+}
+
+void umdns_udebug_set_enabled(bool val)
+{
+ static const struct udebug_buf_meta meta = {
+ .name = "umdns_log",
+ .format = UDEBUG_FORMAT_STRING,
+ };
+
+ if (udebug_enabled == val)
+ return;
+
+ udebug_enabled = val;
+ if (!val) {
+ udebug_buf_free(&udb);
+ udebug_free(&ud);
+ return;
+ }
+
+ udebug_init(&ud);
+ udebug_auto_connect(&ud, NULL);
+ udebug_buf_init(&udb, 1024, 64 * 1024);
+ udebug_buf_add(&ud, &udb, &meta);
+}
+
static void
signal_shutdown(int signal)
{
#include <resolv.h>
#include <glob.h>
+#include <inttypes.h>
#include <stdio.h>
#include <time.h>
SERVICE_SERVICE,
SERVICE_PORT,
SERVICE_TXT,
+ SERVICE_HOSTNAME,
__SERVICE_MAX,
};
[SERVICE_SERVICE] = { .name = "service", .type = BLOBMSG_TYPE_STRING },
[SERVICE_PORT] = { .name = "port", .type = BLOBMSG_TYPE_INT32 },
[SERVICE_TXT] = { .name = "txt", .type = BLOBMSG_TYPE_ARRAY },
+ [SERVICE_HOSTNAME] = { .name = "hostname", .type = BLOBMSG_TYPE_STRING },
};
static void
service_update(struct vlist_tree *tree, struct vlist_node *node_new,
struct vlist_node *node_old);
+static void
+hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old);
+
static struct blob_buf b;
static VLIST_TREE(services, avl_strcmp, service_update, false, false);
+VLIST_TREE(hostnames, avl_strcmp, hostname_update, false, false);
static int service_init_announce;
/**
time_t t = monotonic_time();
if (t - s->t <= TOUT_LOOKUP) {
- DBG(2, "t=%lu, s->t=%lu, t - s->t = %lu\n", t, s->t, t - s->t);
+ DBG(2, "t=%" PRId64 ", s->t=%" PRId64 ", t - s->t = %" PRId64 "\n", (int64_t)t, (int64_t)s->t, (int64_t)(t - s->t));
return 0;
}
free(s);
}
+static void
+hostname_update(struct vlist_tree *tree, struct vlist_node *node_new,
+ struct vlist_node *node_old)
+{
+ struct interface *iface;
+ struct hostname *h;
+
+ if (!node_old) {
+ h = container_of(node_new, struct hostname, node);
+ vlist_for_each_element(&interfaces, iface, node)
+ dns_reply_a(iface, NULL, announce_ttl, h->hostname);
+ return;
+ }
+
+ h = container_of(node_old, struct hostname, node);
+ if (!node_new)
+ vlist_for_each_element(&interfaces, iface, node)
+ dns_reply_a(iface, NULL, 0, h->hostname);
+
+ free(h);
+}
+
+static void
+service_load_hostname(struct blob_attr *b)
+{
+ struct hostname *h;
+ char *hostname, *d_hostname;
+
+ hostname = blobmsg_get_string(b);
+ h = calloc_a(sizeof(*h), &d_hostname, strlen(hostname) + 1);
+ if (!h)
+ return;
+
+ h->hostname = strcpy(d_hostname, hostname);
+
+ vlist_add(&hostnames, &h->node, h->hostname);
+}
+
static void
service_load_blob(struct blob_attr *b)
{
uint8_t *d_txt;
int rem2;
int txt_len = 0;
+ unsigned int n;
blobmsg_parse(service_policy, ARRAY_SIZE(service_policy),
_tb, blobmsg_data(b), blobmsg_data_len(b));
+
+ if (_tb[SERVICE_HOSTNAME]) {
+ service_load_hostname(_tb[SERVICE_HOSTNAME]);
+ return;
+ }
+
if (!_tb[SERVICE_PORT] || !_tb[SERVICE_SERVICE])
return;
blobmsg_for_each_attr(txt, _tb[SERVICE_TXT], rem2)
txt_len += 1 + strlen(blobmsg_get_string(txt));
+ n = strlen(blobmsg_name(b));
s = calloc_a(sizeof(*s),
- &d_id, strlen(blobmsg_name(b)) + 1,
+ &d_id, n + 1,
&d_instance, _tb[SERVICE_INSTANCE] ? strlen(blobmsg_get_string(_tb[SERVICE_INSTANCE])) + 1 : 0,
&d_service, strlen(blobmsg_get_string(_tb[SERVICE_SERVICE])) + 1,
&d_txt, txt_len);
return;
s->port = blobmsg_get_u32(_tb[SERVICE_PORT]);
- s->id = strcpy(d_id, blobmsg_name(b));
+ s->id = strncpy(d_id, blobmsg_name(b), n);
if (_tb[SERVICE_INSTANCE])
s->instance = strcpy(d_instance, blobmsg_get_string(_tb[SERVICE_INSTANCE]));
else
get_hostname();
vlist_update(&services);
+ vlist_update(&hostnames);
service_load("/etc/umdns/*");
blob_for_each_attr(cur, msg, rem) {
}
}
vlist_flush(&services);
+ vlist_flush(&hostnames);
}
void
#ifndef _SERVICE_H__
#define _SERVICE_H__
+struct hostname {
+ struct vlist_node node;
+
+ const char *hostname;
+};
+extern struct vlist_tree hostnames;
+
extern void service_init(int announce);
extern void service_cleanup(void);
extern void service_reply(struct interface *iface, struct sockaddr *to, const char *instance, const char *service_domain, int ttl);
--- /dev/null
+IF(CMAKE_C_COMPILER_ID STREQUAL "Clang")
+ ADD_SUBDIRECTORY(fuzz)
+
+ ADD_EXECUTABLE(dhpf-san dns_handle_packet_file.c)
+ TARGET_INCLUDE_DIRECTORIES(dhpf-san PRIVATE ${PROJECT_SOURCE_DIR})
+ TARGET_COMPILE_OPTIONS(dhpf-san PRIVATE -g -fno-omit-frame-pointer -fsanitize=undefined,address,leak -fno-sanitize-recover=all)
+ TARGET_LINK_OPTIONS(dhpf-san PRIVATE -fsanitize=undefined,address,leak)
+ TARGET_LINK_LIBRARIES(dhpf-san umdns-lib-san)
+ENDIF()
+
+ADD_EXECUTABLE(dhpf dns_handle_packet_file.c)
+TARGET_INCLUDE_DIRECTORIES(dhpf PRIVATE ${PROJECT_SOURCE_DIR})
+TARGET_LINK_LIBRARIES(dhpf umdns-lib)
--- /dev/null
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "dns.h"
+#include "cache.c"
+#include "interface.h"
+
+int cfg_proto = 0;
+int cfg_no_subnet = 0;
+
+static void fuzz_dns_handle_packet(uint8_t *input, size_t size)
+{
+ struct sockaddr from;
+ struct interface iface;
+ struct cache_service *s, *t;
+
+ memset(&from, 0, sizeof(from));
+ memset(&iface, 0, sizeof(iface));
+
+ cache_init();
+ dns_handle_packet(&iface, &from, 1922, input, size);
+
+ avl_for_each_element_safe(&services, s, avl, t)
+ cache_service_free(s);
+}
+
+int main(int argc, char *argv[])
+{
+ size_t len = 0;
+ FILE *fd = NULL;
+ uint8_t *buf = NULL;
+
+ if (argc != 2) {
+ fprintf(stderr, "Usage: %s <packet.bin>\n", argv[0]);
+ return -1;
+ }
+
+ fd = fopen(argv[1], "r");
+ if (!fd) {
+ perror("unable to open input file\n");
+ return -1;
+ }
+
+ buf = calloc(1, MDNS_BUF_LEN+1);
+ if (!buf)
+ return -1;
+
+ len = fread(buf, 1, MDNS_BUF_LEN, fd);
+
+ fuzz_dns_handle_packet(buf, len);
+
+ fclose(fd);
+ free(buf);
+}
--- /dev/null
+FILE(GLOB test_cases "test-*.c")
+
+MACRO(ADD_FUZZER_TEST name)
+ ADD_EXECUTABLE(${name} ${name}.c)
+ TARGET_COMPILE_OPTIONS(${name} PRIVATE -g -O1 -fno-omit-frame-pointer -fsanitize=fuzzer,address,leak,undefined)
+ TARGET_INCLUDE_DIRECTORIES(${name} PRIVATE ${PROJECT_SOURCE_DIR})
+ TARGET_LINK_OPTIONS(${name} PRIVATE -stdlib=libc++ -fsanitize=fuzzer,address,leak,undefined)
+ TARGET_LINK_LIBRARIES(${name} umdns-lib-san ${LIBS})
+ ADD_TEST(
+ NAME ${name}
+ COMMAND ${name} -max_len=256 -timeout=10 -max_total_time=300 ${CMAKE_CURRENT_SOURCE_DIR}/corpus
+ )
+ENDMACRO(ADD_FUZZER_TEST)
+
+FOREACH(test_case ${test_cases})
+ GET_FILENAME_COMPONENT(test_case ${test_case} NAME_WE)
+ ADD_FUZZER_TEST(${test_case})
+ENDFOREACH(test_case)
--- /dev/null
+"\x0c"
+"\x78"
+"\xc0\xb0"
+"\x80\x01"
+"."
+"_"
--- /dev/null
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "dns.h"
+#include "cache.c"
+#include "interface.h"
+
+int cfg_proto = 0;
+int cfg_no_subnet = 0;
+
+static void fuzz_dns_handle_packet(uint8_t *input, size_t size)
+{
+ struct sockaddr from;
+ struct interface iface;
+ struct cache_service *s, *t;
+
+ memset(&from, 0, sizeof(from));
+ memset(&iface, 0, sizeof(iface));
+
+ cache_init();
+ dns_handle_packet(&iface, &from, 1922, input, size);
+
+ avl_for_each_element_safe(&services, s, avl, t)
+ cache_service_free(s);
+}
+
+int LLVMFuzzerTestOneInput(const uint8_t *input, size_t size)
+{
+ uint8_t *buf = calloc(1, size);
+ if (!buf)
+ return 0;
+
+ memcpy(buf, input, size);
+ fuzz_dns_handle_packet(buf, size);
+ free(buf);
+
+ return 0;
+}
static struct ubus_auto_conn conn;
static struct blob_buf b;
+static struct ubus_subscriber udebug_sub;
static int
umdns_reload(struct ubus_context *ctx, struct ubus_object *obj,
return 0;
}
+enum {
+ BROWSE_SERVICE,
+ BROWSE_ARRAY,
+ BROWSE_ADDRESS,
+ BROWSE_MAX
+};
+
+static const struct blobmsg_policy browse_policy[] = {
+ [BROWSE_SERVICE] = { "service", BLOBMSG_TYPE_STRING },
+ [BROWSE_ARRAY] = { "array", BLOBMSG_TYPE_BOOL },
+ [BROWSE_ADDRESS] = { "address", BLOBMSG_TYPE_BOOL },
+};
+
static int
umdns_browse(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
{
struct cache_service *s, *q;
char *buffer = (char *) mdns_buf;
+ struct blob_attr *data[BROWSE_MAX];
void *c1 = NULL, *c2;
+ char *service = NULL;
+ int array = 0;
+ bool address = true;
+
+ blobmsg_parse(browse_policy, BROWSE_MAX, data, blob_data(msg), blob_len(msg));
+ if (data[BROWSE_SERVICE])
+ service = blobmsg_get_string(data[BROWSE_SERVICE]);
+ if (data[BROWSE_ARRAY])
+ array = blobmsg_get_u8(data[BROWSE_ARRAY]);
+ if (data[BROWSE_ADDRESS])
+ address = blobmsg_get_bool(data[BROWSE_ADDRESS]);
blob_buf_init(&b, 0);
avl_for_each_element(&services, s, avl) {
+ const char *hostname = buffer;
char *local;
snprintf(buffer, MAX_NAME_LEN, "%s", (const char *) s->avl.key);
*local = '\0';
if (!strcmp(buffer, "_tcp") || !strcmp(buffer, "_udp"))
continue;
-
+ if (service && strcmp(buffer, service))
+ continue;
if (!c1) {
c1 = blobmsg_open_table(&b, buffer);
}
*local = '\0';
c2 = blobmsg_open_table(&b, buffer);
strncat(buffer, ".local", MAX_NAME_LEN);
- cache_dump_records(&b, buffer);
- cache_dump_records(&b, s->entry);
+ if (s->iface)
+ blobmsg_add_string(&b, "iface", s->iface->name);
+ cache_dump_records(&b, s->entry, array, &hostname);
+ if (address)
+ cache_dump_records(&b, hostname, array, NULL);
blobmsg_close_table(&b, c2);
q = avl_next_element(s, avl);
if (!q || avl_is_last(&services, &s->avl) || strcmp(s->avl.key, q->avl.key)) {
return UBUS_STATUS_OK;
}
+enum {
+ HOSTS_ARRAY,
+ __HOSTS_MAX
+};
+static const struct blobmsg_policy hosts_policy[] = {
+ [HOSTS_ARRAY] = { "array", BLOBMSG_TYPE_BOOL }
+};
+
static int
umdns_hosts(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg)
{
struct cache_record *prev = NULL;
+ struct blob_attr *tb[__HOSTS_MAX];
struct cache_record *r;
+ bool array = false;
void *c;
+ blobmsg_parse(hosts_policy, __HOSTS_MAX, tb, blobmsg_data(msg), blobmsg_len(msg));
+ if (tb[HOSTS_ARRAY])
+ array = blobmsg_get_bool(tb[HOSTS_ARRAY]);
+
blob_buf_init(&b, 0);
avl_for_each_element(&records, r, avl) {
if (r->type != TYPE_A && r->type != TYPE_AAAA)
/* Query each domain just once */
if (!prev || strcmp(r->record, prev->record)) {
c = blobmsg_open_table(&b, r->record);
- cache_dump_records(&b, r->record);
+ cache_dump_records(&b, r->record, array, NULL);
blobmsg_close_table(&b, c);
}
prev = r;
if ((c = tb[QUERY_TYPE]))
type = blobmsg_get_u32(c);
- struct interface *iface_v4 = interface_get(ifname, 0, 1);
- struct interface *iface_v6 = interface_get(ifname, 1, 1);
+ struct interface *iface_v4 = interface_get(ifname, SOCK_MC_IPV4);
+ struct interface *iface_v6 = interface_get(ifname, SOCK_MC_IPV6);
if (!iface_v4 && !iface_v6)
return UBUS_STATUS_NOT_FOUND;
UBUS_METHOD("set_config", umdns_set_config, config_policy),
UBUS_METHOD("query", umdns_query, query_policy),
UBUS_METHOD("fetch", umdns_query, query_policy),
+ UBUS_METHOD("browse", umdns_browse, browse_policy),
UBUS_METHOD_NOARG("update", umdns_update),
- UBUS_METHOD_NOARG("browse", umdns_browse),
- UBUS_METHOD_NOARG("hosts", umdns_hosts),
+ UBUS_METHOD("hosts", umdns_hosts, hosts_policy),
UBUS_METHOD_NOARG("reload", umdns_reload),
};
.n_methods = ARRAY_SIZE(umdns_methods),
};
+static struct blob_attr *
+find_attr(struct blob_attr *attr, const char *name, enum blobmsg_type type)
+{
+ struct blobmsg_policy policy = { name, type };
+ struct blob_attr *ret;
+
+ if (!attr)
+ return NULL;
+
+ blobmsg_parse_attr(&policy, 1, &ret, attr);
+
+ return ret;
+}
+
+static void
+umdns_udebug_config_cb(struct blob_attr *data)
+{
+ enum {
+ CFG_ATTR_ENABLED,
+ __CFG_ATTR_MAX
+ };
+ static const struct blobmsg_policy policy[__CFG_ATTR_MAX] = {
+ [CFG_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_STRING },
+ };
+ struct blob_attr *tb[__CFG_ATTR_MAX];
+ bool en;
+
+ data = find_attr(data, "service", BLOBMSG_TYPE_TABLE);
+ data = find_attr(data, "umdns", BLOBMSG_TYPE_TABLE);
+ if (!data)
+ return;
+
+ blobmsg_parse_attr(policy, __CFG_ATTR_MAX, tb, data);
+ if (!tb[CFG_ATTR_ENABLED])
+ return;
+
+ en = !!atoi(blobmsg_get_string(tb[CFG_ATTR_ENABLED]));
+ umdns_udebug_set_enabled(en);
+}
+
+static int
+umdns_udebug_notify_cb(struct ubus_context *ctx, struct ubus_object *obj,
+ struct ubus_request_data *req, const char *method,
+ struct blob_attr *msg)
+{
+ umdns_udebug_config_cb(msg);
+
+ return 0;
+}
+
+static void
+umdns_udebug_req_cb(struct ubus_request *req, int type, struct blob_attr *msg)
+{
+ umdns_udebug_config_cb(msg);
+}
+
+static bool
+umdns_udebug_sub_cb(struct ubus_context *ctx, struct ubus_subscriber *sub,
+ const char *path)
+{
+ return !strcmp(path, "udebug");
+}
+
+
static void
ubus_connect_handler(struct ubus_context *ctx)
{
+ uint32_t id;
int ret;
ret = ubus_add_object(ctx, &umdns_object);
if (ret)
fprintf(stderr, "Failed to add object: %s\n", ubus_strerror(ret));
+
+ udebug_sub.cb = umdns_udebug_notify_cb;
+ udebug_sub.new_obj_cb = umdns_udebug_sub_cb;
+ ubus_register_subscriber(&conn.ctx, &udebug_sub);
+ if (ubus_lookup_id(&conn.ctx, "udebug", &id) == 0) {
+ ubus_subscribe(&conn.ctx, &udebug_sub, id);
+ ubus_invoke(&conn.ctx, id, "get_config", NULL, umdns_udebug_req_cb, NULL, 1000);
+ }
}
void
#include <time.h>
#define DBG(level, fmt, ...) do { \
+ umdns_udebug_printf("[%d] [%s:%d] " fmt, level, __func__, __LINE__, ## __VA_ARGS__); \
if (debug >= level) \
fprintf(stderr, "mdnsd: %s (%d): " fmt, __func__, __LINE__, ## __VA_ARGS__); \
} while (0)
extern uint32_t rand_time_delta(uint32_t t);
extern time_t monotonic_time(void);
+void umdns_udebug_set_enabled(bool val);
+void umdns_udebug_printf(const char *format, ...);
+
#endif