move announce state to struct interface
[project/mdnsd.git] / dns.c
diff --git a/dns.c b/dns.c
index 1d8a830..b37f82e 100644 (file)
--- a/dns.c
+++ b/dns.c
 
 #include <libubox/uloop.h>
 #include <libubox/usock.h>
+#include <libubox/utils.h>
 
 #include "announce.h"
 #include "util.h"
 #include "dns.h"
+#include "interface.h"
 
-char *rdata_buffer;
-static char *name_buffer;
+char rdata_buffer[MAX_DATA_LEN + 1];
+static char name_buffer[MAX_NAME_LEN + 1];
 
 const char*
 dns_type_string(uint16_t type)
@@ -65,43 +67,38 @@ dns_type_string(uint16_t type)
 }
 
 void
-dns_send_question(struct uloop_fd *u, char *question, int type)
+dns_send_question(struct interface *iface, const char *question, int type)
 {
-       unsigned char buffer[MAX_NAME_LEN];
-       struct dns_header h = { 0 };
-       struct dns_question q = { 0 };
-       struct msghdr m = { 0 };
-       struct iovec iov[3] = { {0}, {0}, {0} };
-       struct sockaddr_in a = { 0 };
+       static struct dns_header h = {
+               .questions = cpu_to_be16(1),
+       };
+       static struct dns_question q = {
+               .class = cpu_to_be16(1),
+       };
+       static struct iovec iov[] = {
+               {
+                       .iov_base = &h,
+                       .iov_len = sizeof(h),
+               },
+               {
+                       .iov_base = name_buffer,
+               },
+               {
+                       .iov_base = &q,
+                       .iov_len = sizeof(q),
+               }
+       };
        int len;
 
-       a.sin_family = AF_INET;
-       a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
-       a.sin_port = htons(MCAST_PORT);
-
-       h.questions = __cpu_to_be16(1);
        q.type = __cpu_to_be16(type);
-       q.class = __cpu_to_be16(1);
 
-       len = dn_comp(question, buffer, MAX_NAME_LEN, NULL, NULL);
+       len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
        if (len < 1)
                return;
 
-       m.msg_name = &a;
-       m.msg_namelen = sizeof(struct sockaddr_in);
-       m.msg_iov = iov;
-       m.msg_iovlen = 3;
-
-       iov[0].iov_base = &h;
-       iov[0].iov_len = sizeof(struct dns_header);
-
-       iov[1].iov_base = buffer;
        iov[1].iov_len = len;
 
-       iov[2].iov_base = &q;
-       iov[2].iov_len = sizeof(struct dns_question);
-
-       if (sendmsg(u->fd, &m, 0) < 0)
+       if (interface_send_packet(iface, iov, ARRAY_SIZE(iov)) < 0)
                fprintf(stderr, "failed to send question\n");
        else
                DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
@@ -127,7 +124,7 @@ dns_init_answer(void)
 }
 
 void
-dns_add_answer(int type, uint8_t *rdata, uint16_t rdlength)
+dns_add_answer(int type, const uint8_t *rdata, uint16_t rdlength)
 {
        struct dns_reply *a = &dns_reply[dns_answer_cnt];
        if (dns_answer_cnt == MAX_ANSWER)
@@ -139,34 +136,20 @@ dns_add_answer(int type, uint8_t *rdata, uint16_t rdlength)
 }
 
 void
-dns_send_answer(struct uloop_fd *u, char *answer)
+dns_send_answer(struct interface *iface, const char *answer)
 {
        uint8_t buffer[256];
        struct dns_header h = { 0 };
-       struct msghdr m = { 0 };
        struct iovec *iov;
-       struct sockaddr_in in = { 0 };
        int len, i;
 
        if (!dns_answer_cnt)
                return;
 
-       in.sin_family = AF_INET;
-       in.sin_addr.s_addr = inet_addr(MCAST_ADDR);
-       in.sin_port = htons(MCAST_PORT);
-
        h.answers = __cpu_to_be16(dns_answer_cnt);
        h.flags = __cpu_to_be16(0x8400);
 
-       iov = malloc(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
-       if (!iov)
-               return;
-
-       m.msg_name = &in;
-       m.msg_namelen = sizeof(struct sockaddr_in);
-       m.msg_iov = iov;
-       m.msg_iovlen = (dns_answer_cnt * 3) + 1;
-
+       iov = alloca(sizeof(struct iovec) * ((dns_answer_cnt * 3) + 1));
        iov[0].iov_base = &h;
        iov[0].iov_len = sizeof(struct dns_header);
 
@@ -196,7 +179,7 @@ dns_send_answer(struct uloop_fd *u, char *answer)
                DBG(1, "A <- %s %s\n", dns_type_string(dns_reply[i].type), answer);
        }
 
-       if (sendmsg(u->fd, &m, 0) < 0)
+       if (interface_send_packet(iface, iov, (dns_answer_cnt * 3) + 1) < 0)
                fprintf(stderr, "failed to send question\n");
 
        for (i = 0; i < dns_answer_cnt; i++) {
@@ -207,7 +190,7 @@ dns_send_answer(struct uloop_fd *u, char *answer)
 }
 
 static int
-scan_name(uint8_t *buffer, int len)
+scan_name(const uint8_t *buffer, int len)
 {
        int offset = 0;
 
@@ -290,7 +273,7 @@ dns_consume_answer(uint8_t **data, int *len)
 }
 
 char*
-dns_consume_name(uint8_t *base, int blen, uint8_t **data, int *len)
+dns_consume_name(const uint8_t *base, int blen, uint8_t **data, int *len)
 {
        int nlen = scan_name(*data, *len);
 
@@ -307,25 +290,3 @@ dns_consume_name(uint8_t *base, int blen, uint8_t **data, int *len)
 
        return name_buffer;
 }
-
-int
-dns_init(void)
-{
-       name_buffer = malloc(MAX_NAME_LEN + 1);
-       rdata_buffer = malloc(MAX_DATA_LEN + 1);
-
-       if (!name_buffer || !rdata_buffer)
-               return -1;
-
-       memset(name_buffer, 0, MAX_NAME_LEN + 1);
-       memset(rdata_buffer, 0, MAX_NAME_LEN + 1);
-
-       return 0;
-}
-
-void
-dns_cleanup(void)
-{
-       free(name_buffer);
-       free(rdata_buffer);
-}