+ char *name = NULL;
+ uint16_t port = 0;
+ const char *keys[2] = {MESHLINK_MDNS_NAME_KEY, MESHLINK_MDNS_FINGERPRINT_KEY};
+ char *values[2] = {NULL, NULL};
+
+ if(parse_packet(buf, len, &name, mesh->appname, "tcp", &port, 2, keys, values)) {
+ node_t *n = (node_t *)meshlink_get_node(mesh, values[0]);
+
+ if(n) {
+ logger(mesh, MESHLINK_INFO, "Node %s is part of the mesh network.\n", n->name);
+
+ switch(sa.sa.sa_family) {
+ case AF_INET:
+ sa.in.sin_port = port;
+ break;
+
+ case AF_INET6:
+ sa.in6.sin6_port = port;
+ break;
+
+ default:
+ logger(mesh, MESHLINK_WARNING, "Could not resolve node %s to a known address family type.\n", n->name);
+ sa.sa.sa_family = AF_UNKNOWN;
+ break;
+ }
+
+ if(sa.sa.sa_family != AF_UNKNOWN) {
+ n->catta_address = sa;
+ node_add_recent_address(mesh, n, &sa);
+
+ connection_t *c = n->connection;
+
+ if(c && c->outgoing && !c->status.active) {
+ c->outgoing->timeout = 0;
+
+ if(c->outgoing->ev.cb) {
+ timeout_set(&mesh->loop, &c->outgoing->ev, &(struct timespec) {
+ 0, 0
+ });
+ }
+
+ c->last_ping_time = -3600;
+ }
+ }
+ } else {
+ logger(mesh, MESHLINK_WARNING, "Node %s is not part of the mesh network.\n", values[0]);
+ }
+
+ fprintf(stderr, "Got packet from %s port %u\n%s=%s\n%s=%s\n", name, port, keys[0], values[0], keys[1], values[1]);
+ }
+
+ free(name);
+
+ for(int i = 0; i < 2; i++) {
+ free(values[i]);
+ }
+}
+
+static void iface_up(meshlink_handle_t *mesh, int index) {
+ int *p = bsearch(&index, mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(*p), iface_compare);
+
+ if(p) {
+ return;
+ }
+
+ fprintf(stderr, "iface %d up\n", index);
+ mesh->discovery_ifaces = xrealloc(mesh->discovery_ifaces, ++mesh->discovery_iface_count * sizeof(*p));
+ mesh->discovery_ifaces[mesh->discovery_iface_count - 1] = index;
+ qsort(mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(*p), iface_compare);
+
+ // Add multicast membership
+ struct ip_mreqn mreq4 = {
+ .imr_multiaddr = mdns_address_ipv4.in.sin_addr,
+ .imr_ifindex = index,
+ };
+ setsockopt(mesh->discovery_sockets[0].fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq4, sizeof(mreq4));
+ setsockopt(mesh->discovery_sockets[0].fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq4, sizeof(mreq4));
+
+ struct ipv6_mreq mreq6 = {
+ .ipv6mr_multiaddr = mdns_address_ipv6.in6.sin6_addr,
+ .ipv6mr_interface = index,
+ };
+ setsockopt(mesh->discovery_sockets[1].fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6));
+ setsockopt(mesh->discovery_sockets[1].fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6));
+
+ // Send an announcement for all addresses associated with this interface
+ for(int i = 0; i < mesh->discovery_address_count; i++) {
+ if(mesh->discovery_addresses[i].index == index) {
+ send_mdns_packet(mesh, &mesh->discovery_addresses[i]);
+ }
+ }
+
+ handle_network_change(mesh, true);
+}
+
+static void iface_down(meshlink_handle_t *const mesh, int index) {
+ int *p = bsearch(&index, mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(*p), iface_compare);
+
+ if(!p) {
+ return;
+ }
+
+ // Drop multicast membership
+ struct ip_mreqn mreq4 = {
+ .imr_multiaddr = mdns_address_ipv4.in.sin_addr,
+ .imr_ifindex = index,
+ };
+ setsockopt(mesh->discovery_sockets[0].fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq4, sizeof(mreq4));
+
+ struct ipv6_mreq mreq6 = {
+ .ipv6mr_multiaddr = mdns_address_ipv6.in6.sin6_addr,
+ .ipv6mr_interface = index,
+ };
+ setsockopt(mesh->discovery_sockets[1].fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6));
+
+ fprintf(stderr, "iface %d down\n", index);
+ memmove(p, p + 1, (mesh->discovery_ifaces + --mesh->discovery_iface_count - p) * sizeof(*p));
+
+ handle_network_change(mesh, mesh->discovery_iface_count);
+}
+
+static void addr_add(meshlink_handle_t *mesh, const discovery_address_t *addr) {
+ discovery_address_t *p = bsearch(addr, mesh->discovery_addresses, mesh->discovery_address_count, sizeof(*p), address_compare);
+
+ if(p) {
+ return;
+ }
+
+ bool up = bsearch(&addr->index, mesh->discovery_ifaces, mesh->discovery_iface_count, sizeof(int), iface_compare);
+ char *host = NULL, *port = NULL;
+ sockaddr2str(&addr->address, &host, &port);
+ fprintf(stderr, "address %d %s port %s up %d\n", addr->index, host, port, up);
+ free(host);
+ free(port);
+
+ mesh->discovery_addresses = xrealloc(mesh->discovery_addresses, ++mesh->discovery_address_count * sizeof(*p));
+ mesh->discovery_addresses[mesh->discovery_address_count - 1] = *addr;
+ mesh->discovery_addresses[mesh->discovery_address_count - 1].up = up;
+
+ if(up) {
+ send_mdns_packet(mesh, &mesh->discovery_addresses[mesh->discovery_address_count - 1]);
+ }
+
+ qsort(mesh->discovery_addresses, mesh->discovery_address_count, sizeof(*p), address_compare);
+}
+
+static void addr_del(meshlink_handle_t *mesh, const discovery_address_t *addr) {
+ discovery_address_t *p = bsearch(addr, mesh->discovery_addresses, mesh->discovery_address_count, sizeof(*p), address_compare);
+
+ if(!p) {
+ return;
+ }
+
+ char *host = NULL, *port = NULL;
+ sockaddr2str(&addr->address, &host, &port);
+ fprintf(stderr, "address %d %s port %s down\n", addr->index, host, port);
+ free(host);
+ free(port);
+
+ memmove(p, p + 1, (mesh->discovery_addresses + --mesh->discovery_address_count - p) * sizeof(*p));
+}
+
+#if defined(__linux)
+static void netlink_getlink(int fd) {
+ static const struct {
+ struct nlmsghdr nlm;
+ struct ifinfomsg ifi;
+ } msg = {
+ .nlm.nlmsg_len = NLMSG_LENGTH(sizeof(msg.ifi)),
+ .nlm.nlmsg_type = RTM_GETLINK,
+ .nlm.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
+ .nlm.nlmsg_seq = 1,
+ .ifi.ifi_family = AF_UNSPEC,
+ };
+ send(fd, &msg, msg.nlm.nlmsg_len, 0);
+}
+
+static void netlink_getaddr(int fd) {
+ static const struct {
+ struct nlmsghdr nlm;
+ struct ifaddrmsg ifa;
+ } msg = {
+ .nlm.nlmsg_len = NLMSG_LENGTH(sizeof(msg.ifa)),
+ .nlm.nlmsg_type = RTM_GETADDR,
+ .nlm.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST,
+ .nlm.nlmsg_seq = 2,
+ .ifa.ifa_family = AF_UNSPEC,
+ };
+ send(fd, &msg, msg.nlm.nlmsg_len, 0);
+}
+
+
+static void netlink_parse_link(meshlink_handle_t *mesh, const struct nlmsghdr *nlm) {
+ const struct ifinfomsg *ifi = (const struct ifinfomsg *)(nlm + 1);
+
+ if(ifi->ifi_flags & IFF_UP && ifi->ifi_flags & IFF_MULTICAST) {
+ iface_up(mesh, ifi->ifi_index);
+ } else {
+ iface_down(mesh, ifi->ifi_index);
+ }
+}
+
+static void netlink_parse_addr(meshlink_handle_t *mesh, const struct nlmsghdr *nlm) {
+ const struct ifaddrmsg *ifa = (const struct ifaddrmsg *)(nlm + 1);
+ const uint8_t *ptr = (const uint8_t *)(ifa + 1);
+ size_t len = nlm->nlmsg_len - (ptr - (const uint8_t *)nlm);
+
+ while(len >= sizeof(struct rtattr)) {
+ const struct rtattr *rta = (const struct rtattr *)ptr;
+
+ if(rta->rta_len <= 0 || rta->rta_len > len) {
+ break;
+ }
+
+ if(rta->rta_type == IFA_ADDRESS) {
+ discovery_address_t addr = {
+ .index = ifa->ifa_index,
+ };
+
+ if(rta->rta_len == 8) {
+ addr.address.sa.sa_family = AF_INET;
+ memcpy(&addr.address.in.sin_addr, ptr + 4, 4);
+ addr.address.in.sin_port = 5353;
+ } else if(rta->rta_len == 20) {
+ addr.address.sa.sa_family = AF_INET6;
+ memcpy(&addr.address.in6.sin6_addr, ptr + 4, 16);
+ addr.address.in6.sin6_port = 5353;
+ addr.address.in6.sin6_scope_id = ifa->ifa_index;
+ } else {
+ addr.address.sa.sa_family = AF_UNKNOWN;
+ }
+
+ if(addr.address.sa.sa_family != AF_UNKNOWN) {
+ if(nlm->nlmsg_type == RTM_NEWADDR) {
+ addr_add(mesh, &addr);
+ } else {
+ addr_del(mesh, &addr);
+ }
+ }
+ }
+
+ unsigned short rta_len = (rta->rta_len + 3) & ~3;
+ ptr += rta_len;
+ len -= rta_len;