X-Git-Url: http://git.openwrt.org/?a=blobdiff_plain;f=interface.c;h=3904c8918125d786e7119f56c8dfdcb1d8cd608f;hb=26ce7dca8085d34e6c3910680da51695f3508eec;hp=f09329ba17e586f17d5eb15805f4799d7c57bbbf;hpb=9bbaf3c1f7826a7ac637ac0e2e7c8d5b43bac89d;p=project%2Fmdnsd.git diff --git a/interface.c b/interface.c index f09329b..3904c89 100644 --- a/interface.c +++ b/interface.c @@ -34,6 +34,7 @@ #include #include #include +#include "cache.h" #include "interface.h" #include "util.h" #include "dns.h" @@ -41,7 +42,7 @@ #include "service.h" static int -interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len) +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; @@ -69,13 +70,19 @@ interface_send_packet4(struct interface *iface, struct iovec *iov, int iov_len) pkti = (struct in_pktinfo*) CMSG_DATA(cmsg); pkti->ipi_ifindex = iface->ifindex; - a.sin_addr.s_addr = inet_addr(MCAST_ADDR); + if (iface->multicast) { + a.sin_addr.s_addr = inet_addr(MCAST_ADDR); + if (to) + fprintf(stderr, "Ignoring IPv4 address for multicast interface\n"); + } else { + a.sin_addr.s_addr = to->sin_addr.s_addr; + } return sendmsg(fd, &m, 0); } static int -interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len) +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; @@ -103,23 +110,35 @@ interface_send_packet6(struct interface *iface, struct iovec *iov, int iov_len) pkti = (struct in6_pktinfo*) CMSG_DATA(cmsg); pkti->ipi6_ifindex = iface->ifindex; - inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr); + if (iface->multicast) { + inet_pton(AF_INET6, MCAST_ADDR6, &a.sin6_addr); + if (to) + fprintf(stderr, "Ignoring IPv6 address for multicast interface\n"); + } else { + a.sin6_addr = to->sin6_addr; + } return sendmsg(fd, &m, 0); } int -interface_send_packet(struct interface *iface, struct iovec *iov, int iov_len) +interface_send_packet(struct interface *iface, struct sockaddr *to, struct iovec *iov, int iov_len) { + if (!iface->multicast && !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); } if (iface->v6) - return interface_send_packet6(iface, iov, iov_len); + return interface_send_packet6(iface, (struct sockaddr_in6 *)to, iov, iov_len); - return interface_send_packet4(iface, iov, iov_len); + return interface_send_packet4(iface, (struct sockaddr_in *)to, iov, iov_len); } static void interface_close(struct interface *iface) @@ -135,6 +154,7 @@ static void interface_close(struct interface *iface) static void interface_free(struct interface *iface) { + uloop_timeout_cancel(&iface->reconnect); interface_close(iface); free(iface); } @@ -222,7 +242,7 @@ read_socket4(struct uloop_fd *u, unsigned int events) fprintf(stderr, "RX ipv4: %s\n", iface->name); fprintf(stderr, " multicast: %d\n", iface->multicast); inet_ntop(AF_INET, &from.sin_addr, buf, 256); - fprintf(stderr, " src %s:%d\n", buf, from.sin_port); + 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); @@ -232,7 +252,7 @@ read_socket4(struct uloop_fd *u, unsigned int events) 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, from.sin_port, buffer, len); + dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin_port), buffer, len); } static void @@ -299,7 +319,7 @@ read_socket6(struct uloop_fd *u, unsigned int events) fprintf(stderr, "RX ipv6: %s\n", iface->name); fprintf(stderr, " multicast: %d\n", iface->multicast); inet_ntop(AF_INET6, &from.sin6_addr, buf, 256); - fprintf(stderr, " src %s:%d\n", buf, from.sin6_port); + 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); } @@ -307,7 +327,7 @@ read_socket6(struct uloop_fd *u, unsigned int events) 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, from.sin6_port, buffer, len); + dns_handle_packet(iface, (struct sockaddr *) &from, ntohs(from.sin6_port), buffer, len); } static int @@ -389,6 +409,7 @@ static void reconnect_socket4(struct uloop_timeout *timeout) { 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, @@ -404,6 +425,9 @@ reconnect_socket4(struct uloop_timeout *timeout) 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 (setsockopt(iface->fd.fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) fprintf(stderr, "ioctl failed: IP_RECVTTL\n"); @@ -417,7 +441,7 @@ reconnect_socket4(struct uloop_timeout *timeout) uloop_fd_add(&iface->fd, ULOOP_READ); if (iface->multicast) { - dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0); + dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0); announce_init(iface); } @@ -465,7 +489,7 @@ reconnect_socket6(struct uloop_timeout *timeout) uloop_fd_add(&iface->fd, ULOOP_READ); if (iface->multicast) { - dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0); + dns_send_question(iface, NULL, C_DNS_SD, TYPE_PTR, 0); announce_init(iface); } @@ -496,6 +520,7 @@ iface_update_cb(struct vlist_tree *tree, struct vlist_node *node_new, if (node_old) { iface = container_of(node_old, struct interface, node); + cache_cleanup(iface); interface_free(iface); } @@ -620,8 +645,8 @@ void interface_shutdown(void) vlist_for_each_element(&interfaces, iface, node) if (iface->fd.fd > 0 && iface->multicast) { - service_announce(iface, 0); - service_reply_a(iface, 0); + dns_reply_a(iface, NULL, 0); + service_announce_services(iface, NULL, 0); } vlist_for_each_element(&interfaces, iface, node) interface_close(iface);