X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fmdnsd.git;a=blobdiff_plain;f=interface.c;h=a09e7ce093a54f51d99bcadbeb3ae2c44a7df4a8;hp=a4daa244add550d386fa9a745974d47aa106db6d;hb=496aeba797c7d0246e88eedadff080fcaffd9013;hpb=b7e5cb7ab91a9487ec71a14b706b5589cefe9052 diff --git a/interface.c b/interface.c index a4daa24..a09e7ce 100644 --- a/interface.c +++ b/interface.c @@ -41,7 +41,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 +69,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 +109,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,10 +153,30 @@ static void interface_close(struct interface *iface) static void interface_free(struct interface *iface) { + uloop_timeout_cancel(&iface->reconnect); interface_close(iface); free(iface); } +static int +interface_valid_src(void *ip1, void *mask, void *ip2, int len) +{ + uint8_t *i1 = ip1; + uint8_t *i2 = ip2; + uint8_t *m = mask; + int i; + + if (cfg_no_subnet) + return 0; + + for (i = 0; i < len; i++, i1++, i2++, m++) { + if ((*i1 & *m) != (*i2 & *m)) + return -1; + } + + return 0; +} + static void read_socket4(struct uloop_fd *u, unsigned int events) { @@ -212,8 +250,8 @@ 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 - dns_handle_packet(iface, buffer, len, 0); + 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); } static void @@ -287,8 +325,8 @@ 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 - dns_handle_packet(iface, buffer, len, 0); + 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); } static int @@ -370,6 +408,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, @@ -385,6 +424,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"); @@ -398,7 +440,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, 1); + dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0); announce_init(iface); } @@ -446,7 +488,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, 1); + dns_send_question(iface, "_services._dns-sd._udp.local", TYPE_PTR, 0); announce_init(iface); } @@ -538,15 +580,20 @@ int interface_add(const char *name) unicast = _interface_add(name, 0, 0); if (!unicast) continue; - sa = (struct sockaddr_in *) ifa->ifa_addr; - memcpy(&unicast->v4_addr, &sa->sin_addr, sizeof(unicast->v4_addr)); - inet_ntop(AF_INET, &sa->sin_addr, unicast->v4_addrs, sizeof(unicast->v4_addrs)); - 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; @@ -566,14 +613,19 @@ int interface_add(const char *name) unicast = _interface_add(name, 0, 1); if (!unicast) continue; - memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_addr)); - inet_ntop(AF_INET6, &sa6->sin6_addr, unicast->v6_addrs, sizeof(unicast->v6_addrs)); - v6 = _interface_add(name, 1, 1); if (!v6) continue; + memcpy(&v6->v6_addr, &sa6->sin6_addr, sizeof(v6->v6_addr)); + memcpy(&unicast->v6_addr, &sa6->sin6_addr, sizeof(unicast->v6_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)); + + 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)); v6->peer = unicast; unicast->peer = v6; @@ -591,11 +643,20 @@ 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); } +struct interface* +interface_get(const char *name, int v6, int multicast) +{ + char id_buf[32]; + snprintf(id_buf, sizeof(id_buf), "%d_%d_%s", multicast, v6, name); + struct interface *iface = vlist_find(&interfaces, id_buf, iface, node); + return iface; +} + VLIST_TREE(interfaces, avl_strcmp, iface_update_cb, false, false);