This commit is contained in:
parent
41a058cd16
commit
3dc4e7e08d
1 changed files with 28 additions and 13 deletions
|
|
@ -138,7 +138,9 @@ static int setup_sockopts(int fd, int arr_idx, int sys_ifindex, int af) {
|
|||
if (af == AF_INET) {
|
||||
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, &off, sizeof(off)) < 0)
|
||||
perror("IP_MULTICAST_LOOP");
|
||||
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &sys_ifindex, sizeof(sys_ifindex)) < 0)
|
||||
struct ip_mreqn mreq_if = {0};
|
||||
mreq_if.imr_ifindex = sys_ifindex;
|
||||
if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, &mreq_if, sizeof(mreq_if)) < 0)
|
||||
perror("IP_MULTICAST_IF");
|
||||
} else {
|
||||
if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &off, sizeof(off)) < 0)
|
||||
|
|
@ -205,6 +207,19 @@ static void repeat(const char *buf, int len, int src_idx, int af) {
|
|||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
struct sockaddr_in dst4 = {0};
|
||||
struct sockaddr_in6 dst6 = {0};
|
||||
struct sockaddr *dstop = NULL;
|
||||
socklen_t dstlen = 0;
|
||||
|
||||
dst4.sin_family = AF_INET;
|
||||
dst4.sin_port = htons(MDNS_PORT);
|
||||
inet_pton(AF_INET, MDNS_GROUP_IP, &dst4.sin_addr);
|
||||
|
||||
dst6.sin6_family = AF_INET6;
|
||||
dst6.sin6_port = htons(MDNS_PORT);
|
||||
inet_pton(AF_INET6, MDNS_GROUP_IP6, &dst6.sin6_addr);
|
||||
|
||||
for (int i = 0; i < n_ifaces; i++) {
|
||||
if (i == src_idx) continue;
|
||||
|
||||
|
|
@ -214,7 +229,10 @@ static void repeat(const char *buf, int len, int src_idx, int af) {
|
|||
if (af == AF_INET) {
|
||||
struct in_pktinfo pktinfo = {0};
|
||||
pktinfo.ipi_ifindex = ifaces[i].index;
|
||||
inet_pton(AF_INET, MDNS_GROUP_IP, &pktinfo.ipi_spec_dst);
|
||||
pktinfo.ipi_spec_dst.s_addr = INADDR_ANY;
|
||||
|
||||
dstop = (struct sockaddr *)&dst4;
|
||||
dstlen = sizeof(dst4);
|
||||
|
||||
msg.msg_control = cmsg_buf;
|
||||
msg.msg_controllen = sizeof(cmsg_buf);
|
||||
|
|
@ -225,22 +243,19 @@ static void repeat(const char *buf, int len, int src_idx, int af) {
|
|||
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
|
||||
memcpy(CMSG_DATA(cmsg), &pktinfo, sizeof(pktinfo));
|
||||
msg.msg_controllen = cmsg->cmsg_len;
|
||||
|
||||
msg.msg_name = NULL;
|
||||
msg.msg_namelen = 0;
|
||||
} else {
|
||||
struct sockaddr_in6 dst = {0};
|
||||
dst.sin6_family = AF_INET6;
|
||||
dst.sin6_port = htons(MDNS_PORT);
|
||||
inet_pton(AF_INET6, MDNS_GROUP_IP6, &dst.sin6_addr);
|
||||
dst.sin6_scope_id = ifaces[i].index;
|
||||
dst6.sin6_scope_id = ifaces[i].index;
|
||||
|
||||
dstop = (struct sockaddr *)&dst6;
|
||||
dstlen = sizeof(dst6);
|
||||
|
||||
msg.msg_name = &dst;
|
||||
msg.msg_namelen = sizeof(dst);
|
||||
msg.msg_control = NULL;
|
||||
msg.msg_controllen = 0;
|
||||
}
|
||||
|
||||
msg.msg_name = dstop;
|
||||
msg.msg_namelen = dstlen;
|
||||
|
||||
ssize_t sent = sendmsg(fd, &msg, 0);
|
||||
if (sent < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||
fprintf(stderr, "sendmsg(%s): %s\n", ifaces[i].name, strerror(errno));
|
||||
|
|
|
|||
Loading…
Reference in a new issue