]> git.meshlink.io Git - catta/blobdiff - socket.c
add support for dots and backslashes in domain names (required for DNS-SD)
[catta] / socket.c
index a77f6315564c3be800e63b3fc95305ae58ab800f..54433d6e026042fd54cbf3561dacc73c1913cb43 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -39,13 +39,13 @@ gint flx_open_socket_ipv4(void) {
     }
     
     ttl = 255;
-    if (setsockopt(fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
+    if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) {
         g_warning("IP_MULTICAST_TTL failed: %s\n", strerror(errno));
         goto fail;
     }
 
     ttl = 255;
-    if (setsockopt(fd, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
+    if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) {
         g_warning("IP_TTL failed: %s\n", strerror(errno));
         goto fail;
     }
@@ -56,6 +56,13 @@ gint flx_open_socket_ipv4(void) {
         goto fail;
     }
 
+    yes = 1;
+    if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
+        g_warning("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
+        goto fail;
+    }
+
+    
     memset(&local, 0, sizeof(local));
     local.sin_family = AF_INET;
     local.sin_port = htons(MDNS_PORT);
@@ -70,19 +77,19 @@ gint flx_open_socket_ipv4(void) {
     mreq.imr_address.s_addr = htonl(INADDR_ANY);
     mreq.imr_ifindex = 0;
     
-    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
+    if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
         g_warning("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
         goto fail;
     }
 
     yes = 1;
-    if (setsockopt(fd, IPPROTO_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
+    if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
         g_warning("IP_RECVTTL failed: %s\n", strerror(errno));
         goto fail;
     }
 
     yes = 1;
-    if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
+    if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) {
         g_warning("IP_PKTINFO failed: %s\n", strerror(errno));
         goto fail;
     }
@@ -130,13 +137,13 @@ gint flx_open_socket_ipv6(void) {
     }
     
     ttl = 255;
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
+    if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
         g_warning("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno));
         goto fail;
     }
 
     ttl = 255;
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
+    if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) {
         g_warning("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno));
         goto fail;
     }
@@ -147,6 +154,18 @@ gint flx_open_socket_ipv6(void) {
         goto fail;
     }
 
+    yes = 1;
+    if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
+        g_warning("IPV6_V6ONLY failed: %s\n", strerror(errno));
+        goto fail;
+    }
+
+    yes = 1;
+    if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
+        g_warning("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno));
+        goto fail;
+    }
+
     memset(&local, 0, sizeof(local));
     local.sin6_family = AF_INET6;
     local.sin6_port = htons(MDNS_PORT);
@@ -160,19 +179,19 @@ gint flx_open_socket_ipv6(void) {
     mreq.ipv6mr_multiaddr = sa.sin6_addr;
     mreq.ipv6mr_interface = 0;
     
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
+    if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
         g_warning("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno));
         goto fail;
     }
 
     yes = 1;
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
+    if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
         g_warning("IPV6_HOPLIMIT failed: %s\n", strerror(errno));
         goto fail;
     }
 
     yes = 1;
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
+    if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) {
         g_warning("IPV6_PKTINFO failed: %s\n", strerror(errno));
         goto fail;
     }
@@ -233,7 +252,7 @@ gint flx_send_dns_packet_ipv4(gint fd, gint interface, flxDnsPacket *p) {
     mdns_mcast_group_ipv4(&sa);
 
     memset(&io, 0, sizeof(io));
-    io.iov_base = p->data;
+    io.iov_base = FLX_DNS_PACKET_DATA(p);
     io.iov_len = p->size;
 
     memset(cmsg_data, 0, sizeof(cmsg_data));
@@ -273,7 +292,7 @@ gint flx_send_dns_packet_ipv6(gint fd, gint interface, flxDnsPacket *p) {
     mdns_mcast_group_ipv6(&sa);
 
     memset(&io, 0, sizeof(io));
-    io.iov_base = p->data;
+    io.iov_base = FLX_DNS_PACKET_DATA(p);
     io.iov_len = p->size;
 
     memset(cmsg_data, 0, sizeof(cmsg_data));
@@ -311,10 +330,10 @@ flxDnsPacket* flx_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint
     g_assert(ret_iface);
     g_assert(ret_ttl);
 
-    p = flx_dns_packet_new();
+    p = flx_dns_packet_new(0);
 
-    io.iov_base = p->data;
-    io.iov_len = sizeof(p->data);
+    io.iov_base = FLX_DNS_PACKET_DATA(p);
+    io.iov_len = p->max_size;
     
     memset(&msg, 0, sizeof(msg));
     msg.msg_name = ret_sa;
@@ -344,7 +363,8 @@ flxDnsPacket* flx_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa, gint
         }
     }
 
-    g_assert(found_iface && found_ttl);
+    g_assert(found_iface);
+    g_assert(found_ttl);
 
     return p;
 
@@ -356,7 +376,7 @@ fail:
 }
 
 flxDnsPacket* flx_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gint *ret_iface, guint8* ret_ttl) {
-    flxDnsPacket *p= NULL;
+    flxDnsPacket *p = NULL;
     struct msghdr msg;
     struct iovec io;
     uint8_t aux[64];
@@ -369,10 +389,10 @@ flxDnsPacket* flx_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gin
     g_assert(ret_iface);
     g_assert(ret_ttl);
 
-    p = flx_dns_packet_new();
+    p = flx_dns_packet_new(0);
 
-    io.iov_base = p->data;
-    io.iov_len = sizeof(p->data);
+    io.iov_base = FLX_DNS_PACKET_DATA(p);
+    io.iov_len = p->max_size;
     
     memset(&msg, 0, sizeof(msg));
     msg.msg_name = ret_sa;
@@ -389,20 +409,21 @@ flxDnsPacket* flx_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa, gin
     p->size = (size_t) l;
     
     *ret_ttl = 0;
-        
-    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg,cmsg)) {
-        if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IPV6_HOPLIMIT) {
+
+    for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+        if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_HOPLIMIT) {
             *ret_ttl = *(uint8_t *) CMSG_DATA(cmsg);
             found_ttl = TRUE;
         }
             
-        if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IPV6_PKTINFO) {
-            *ret_iface = ((struct in_pktinfo*) CMSG_DATA(cmsg))->ipi_ifindex;
+        if (cmsg->cmsg_level == SOL_IPV6 && cmsg->cmsg_type == IPV6_PKTINFO) {
+            *ret_iface = ((struct in6_pktinfo*) CMSG_DATA(cmsg))->ipi6_ifindex;
             found_iface = TRUE;
         }
     }
 
-    g_assert(found_iface && found_ttl);
+    g_assert(found_iface);
+    g_assert(found_ttl);
 
     return p;