X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fiface.c;h=7972a5c02e3ff3bf4403624fe2ffeac4f216458b;hb=ad7b145ebe82c3a53632eaa6ec75b014d91a7bea;hp=2448678f8b3e87243b93ac0216fd673d4ab4a333;hpb=28d336020ca1f6dbb88d64cac3ffdd1a67ee3de7;p=catta diff --git a/avahi-core/iface.c b/avahi-core/iface.c index 2448678..7972a5c 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -36,27 +36,40 @@ #include "dns.h" #include "socket.h" #include "announce.h" +#include "util.h" +#include "log.h" -static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, int remove) { +static void update_address_rr(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a, gboolean remove) { g_assert(m); g_assert(a); - if (!avahi_interface_address_relevant(a) || remove) { + if (avahi_interface_address_relevant(a) && + !remove && + m->server->config.publish_addresses && + (m->server->state == AVAHI_SERVER_RUNNING || + m->server->state == AVAHI_SERVER_REGISTERING)) { + + if (!a->entry_group) { + a->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); + avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, a->interface->protocol, 0, NULL, &a->address); + avahi_entry_group_commit(a->entry_group); + } + } else { + if (a->entry_group) { + + if (avahi_entry_group_get_state(a->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING) + avahi_server_decrease_host_rr_pending(m->server); + avahi_entry_group_free(a->entry_group); a->entry_group = NULL; } - } else { - if (!a->entry_group) { - a->entry_group = avahi_entry_group_new(m->server, NULL, NULL); - avahi_server_add_address(m->server, a->entry_group, a->interface->hardware->index, AF_UNSPEC, 0, NULL, &a->address); - avahi_entry_group_commit(a->entry_group); - } - } + } } -static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int remove) { +static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean remove) { AvahiInterfaceAddress *a; + g_assert(m); g_assert(i); @@ -64,7 +77,7 @@ static void update_interface_rr(AvahiInterfaceMonitor *m, AvahiInterface *i, int update_address_rr(m, a, remove); } -static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, int remove) { +static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gboolean remove) { AvahiInterface *i; g_assert(m); @@ -72,6 +85,36 @@ static void update_hw_interface_rr(AvahiInterfaceMonitor *m, AvahiHwInterface *h for (i = hw->interfaces; i; i = i->by_hardware_next) update_interface_rr(m, i, remove); + + if (!remove && + m->server->config.publish_workstation && + (m->server->state == AVAHI_SERVER_RUNNING || + m->server->state == AVAHI_SERVER_REGISTERING)) { + + if (!hw->entry_group) { + gchar *name; + gchar *t = avahi_format_mac_address(hw->mac_address, hw->mac_address_size); + name = g_strdup_printf("%s [%s]", m->server->host_name, t); + g_free(t); + + hw->entry_group = avahi_entry_group_new(m->server, avahi_host_rr_entry_group_callback, NULL); + avahi_server_add_service(m->server, hw->entry_group, hw->index, AF_UNSPEC, "_workstation._tcp", name, NULL, NULL, 9, NULL); + avahi_entry_group_commit(hw->entry_group); + + g_free(name); + } + + } else { + + if (hw->entry_group) { + + if (avahi_entry_group_get_state(hw->entry_group) == AVAHI_ENTRY_GROUP_REGISTERING) + avahi_server_decrease_host_rr_pending(m->server); + + avahi_entry_group_free(hw->entry_group); + hw->entry_group = NULL; + } + } } static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) { @@ -79,10 +122,8 @@ static void free_address(AvahiInterfaceMonitor *m, AvahiInterfaceAddress *a) { g_assert(a); g_assert(a->interface); + update_address_rr(m, a, TRUE); AVAHI_LLIST_REMOVE(AvahiInterfaceAddress, address, a->interface->addresses, a); - - if (a->entry_group) - avahi_entry_group_free(a->entry_group); g_free(a); } @@ -91,14 +132,13 @@ static void free_interface(AvahiInterfaceMonitor *m, AvahiInterface *i, gboolean g_assert(m); g_assert(i); - g_message("removing interface %s.%i", i->hardware->name, i->protocol); avahi_goodbye_interface(m->server, i, send_goodbye); - g_message("forcing responses..."); avahi_response_scheduler_force(i->response_scheduler); - g_message("done"); g_assert(!i->announcements); + update_interface_rr(m, i, TRUE); + while (i->addresses) free_address(m, i->addresses); @@ -117,6 +157,8 @@ static void free_hw_interface(AvahiInterfaceMonitor *m, AvahiHwInterface *hw, gb g_assert(m); g_assert(hw); + update_hw_interface_rr(m, hw, TRUE); + while (hw->interfaces) free_interface(m, hw->interfaces, send_goodbye); @@ -150,7 +192,7 @@ static int netlink_list_items(AvahiNetlink *nl, guint16 type, guint *ret_seq) { n = (struct nlmsghdr*) req; n->nlmsg_len = NLMSG_LENGTH(sizeof(struct rtgenmsg)); n->nlmsg_type = type; - n->nlmsg_flags = NLM_F_ROOT|NLM_F_MATCH|NLM_F_REQUEST; + n->nlmsg_flags = NLM_F_ROOT/*|NLM_F_MATCH*/|NLM_F_REQUEST; n->nlmsg_pid = 0; gen = NLMSG_DATA(n); @@ -194,22 +236,23 @@ static void check_interface_relevant(AvahiInterfaceMonitor *m, AvahiInterface *i b = avahi_interface_relevant(i); if (b && !i->announcing) { - g_message("New relevant interface %s.%i", i->hardware->name, i->protocol); + avahi_log_debug("New relevant interface %s.%i (#%i)", i->hardware->name, i->protocol, i->hardware->index); if (i->protocol == AF_INET) - avahi_mdns_mcast_join_ipv4 (i->hardware->index, m->server->fd_ipv4); + avahi_mdns_mcast_join_ipv4(i->hardware->index, m->server->fd_ipv4); if (i->protocol == AF_INET6) - avahi_mdns_mcast_join_ipv6 (i->hardware->index, m->server->fd_ipv6); + avahi_mdns_mcast_join_ipv6(i->hardware->index, m->server->fd_ipv6); i->announcing = TRUE; avahi_announce_interface(m->server, i); + avahi_browser_new_interface(m->server, i); } else if (!b && i->announcing) { - g_message("Interface %s.%i no longer relevant", i->hardware->name, i->protocol); + avahi_log_debug("Interface %s.%i no longer relevant", i->hardware->name, i->protocol); if (i->protocol == AF_INET) - avahi_mdns_mcast_leave_ipv4 (i->hardware->index, m->server->fd_ipv4); + avahi_mdns_mcast_leave_ipv4(i->hardware->index, m->server->fd_ipv4); if (i->protocol == AF_INET6) - avahi_mdns_mcast_leave_ipv6 (i->hardware->index, m->server->fd_ipv6); + avahi_mdns_mcast_leave_ipv6(i->hardware->index, m->server->fd_ipv6); avahi_goodbye_interface(m->server, i, FALSE); avahi_response_scheduler_clear(i->response_scheduler); @@ -254,6 +297,8 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { hw->flags = 0; hw->mtu = 1500; hw->index = ifinfomsg->ifi_index; + hw->mac_address_size = 0; + hw->entry_group = NULL; AVAHI_LLIST_HEAD_INIT(AvahiInterface, hw->interfaces); AVAHI_LLIST_PREPEND(AvahiHwInterface, hardware, m->hw_interfaces, hw); @@ -282,6 +327,15 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { g_assert(RTA_PAYLOAD(a) == sizeof(unsigned int)); hw->mtu = *((unsigned int*) RTA_DATA(a)); break; + + case IFLA_ADDRESS: { + hw->mac_address_size = RTA_PAYLOAD(a); + if (hw->mac_address_size > AVAHI_MAX_MAC_ADDRESS) + hw->mac_address_size = AVAHI_MAX_MAC_ADDRESS; + + memcpy(hw->mac_address, RTA_DATA(a), hw->mac_address_size); + break; + } default: ; @@ -313,7 +367,7 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { struct rtattr *a = NULL; size_t l; AvahiAddress raddr; - int raddr_valid = 0; + gboolean raddr_valid = FALSE; if (ifaddrmsg->ifa_family != AF_INET && ifaddrmsg->ifa_family != AF_INET6) return; @@ -323,10 +377,11 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { raddr.family = ifaddrmsg->ifa_family; - l = NLMSG_PAYLOAD(n, sizeof(struct ifinfomsg)); + l = NLMSG_PAYLOAD(n, sizeof(struct ifaddrmsg)); a = IFA_RTA(ifaddrmsg); while (RTA_OK(a, l)) { + switch(a->rta_type) { case IFA_ADDRESS: if ((raddr.family == AF_INET6 && RTA_PAYLOAD(a) != 16) || @@ -334,7 +389,7 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { return; memcpy(raddr.data.data, RTA_DATA(a), RTA_PAYLOAD(a)); - raddr_valid = 1; + raddr_valid = TRUE; break; @@ -344,7 +399,6 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { a = RTA_NEXT(a, l); } - if (!raddr_valid) return; @@ -385,19 +439,19 @@ static void callback(AvahiNetlink *nl, struct nlmsghdr *n, gpointer userdata) { m->list = LIST_DONE; if (netlink_list_items(m->netlink, RTM_GETADDR, &m->query_addr_seq) < 0) - g_warning("NETLINK: Failed to list addrs: %s", strerror(errno)); + avahi_log_warn("NETLINK: Failed to list addrs: %s", strerror(errno)); else m->list = LIST_ADDR; } else { m->list = LIST_DONE; - g_message("Enumeration complete"); + avahi_log_debug("Enumeration complete"); } } else if (n->nlmsg_type == NLMSG_ERROR && (n->nlmsg_seq == m->query_link_seq || n->nlmsg_seq == m->query_addr_seq)) { struct nlmsgerr *e = NLMSG_DATA (n); if (e->error) - g_warning("NETLINK: Failed to browse: %s", strerror(-e->error)); + avahi_log_warn("NETLINK: Failed to browse: %s", strerror(-e->error)); } } @@ -483,17 +537,17 @@ AvahiHwInterface* avahi_interface_monitor_get_hw_interface(AvahiInterfaceMonitor void avahi_interface_send_packet_unicast(AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port) { g_assert(i); g_assert(p); - char t[64]; +/* char t[64]; */ if (!avahi_interface_relevant(i)) return; g_assert(!a || a->family == i->protocol); - if (a) - g_message("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); - else - g_message("multicast sending on '%s.%i'", i->hardware->name, i->protocol); +/* if (a) */ +/* avahi_log_debug("unicast sending on '%s.%i' to %s:%u", i->hardware->name, i->protocol, avahi_address_snprint(t, sizeof(t), a), port); */ +/* else */ +/* avahi_log_debug("multicast sending on '%s.%i'", i->hardware->name, i->protocol); */ if (i->protocol == AF_INET && i->monitor->server->fd_ipv4 >= 0) avahi_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, p, a ? &a->data.ipv4 : NULL, port); @@ -552,14 +606,33 @@ void avahi_dump_caches(AvahiInterfaceMonitor *m, FILE *f) { } gboolean avahi_interface_relevant(AvahiInterface *i) { + AvahiInterfaceAddress *a; + gboolean relevant_address; + g_assert(i); + relevant_address = FALSE; + + for (a = i->addresses; a; a = a->address_next) + if (avahi_interface_address_relevant(a)) { + relevant_address = TRUE; + break; + } + +/* avahi_log_debug("%p. iface-relevant: %i %i %i %i %i %i", i, relevant_address, */ +/* (i->hardware->flags & IFF_UP), */ +/* (i->hardware->flags & IFF_RUNNING), */ +/* !(i->hardware->flags & IFF_LOOPBACK), */ +/* (i->hardware->flags & IFF_MULTICAST), */ +/* !(i->hardware->flags & IFF_POINTOPOINT)); */ + return (i->hardware->flags & IFF_UP) && - (i->hardware->flags & IFF_RUNNING) && + (!i->monitor->server->config.use_iff_running || (i->hardware->flags & IFF_RUNNING)) && !(i->hardware->flags & IFF_LOOPBACK) && (i->hardware->flags & IFF_MULTICAST) && - i->addresses; + !(i->hardware->flags & IFF_POINTOPOINT) && + relevant_address; } gboolean avahi_interface_address_relevant(AvahiInterfaceAddress *a) { @@ -581,7 +654,6 @@ gboolean avahi_interface_match(AvahiInterface *i, gint index, guchar protocol) { return TRUE; } - void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guchar protocol, AvahiInterfaceMonitorWalkCallback callback, gpointer userdata) { g_assert(m); g_assert(callback); @@ -611,3 +683,26 @@ void avahi_interface_monitor_walk(AvahiInterfaceMonitor *m, gint interface, guch callback(m, i, userdata); } } + +void avahi_update_host_rrs(AvahiInterfaceMonitor *m, gboolean remove) { + AvahiHwInterface *hw; + + g_assert(m); + + for (hw = m->hw_interfaces; hw; hw = hw->hardware_next) + update_hw_interface_rr(m, hw, remove); +} + +gboolean avahi_address_is_local(AvahiInterfaceMonitor *m, const AvahiAddress *a) { + AvahiInterface *i; + AvahiInterfaceAddress *ia; + g_assert(m); + g_assert(a); + + for (i = m->interfaces; i; i = i->interface_next) + for (ia = i->addresses; ia; ia = ia->address_next) + if (avahi_address_cmp(a, &ia->address) == 0) + return TRUE; + + return FALSE; +}