dns: use common function for sending packets
authorFelix Fietkau <nbd@openwrt.org>
Tue, 3 Jun 2014 21:21:50 +0000 (23:21 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 3 Jun 2014 21:21:50 +0000 (23:21 +0200)
Signed-off-by: Felix Fietkau <nbd@openwrt.org>
dns.c

diff --git a/dns.c b/dns.c
index 5595384a544b0f86ba326629defd27a47c2a3b27..517825be9daba327c4f75c53d9dc8a841e55cf7d 100644 (file)
--- a/dns.c
+++ b/dns.c
@@ -65,10 +65,43 @@ dns_type_string(uint16_t type)
        return "N/A";
 }
 
+static int
+dns_send_packet(int fd, 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 = {
+               .sin_family = AF_INET,
+               .sin_port = htons(MCAST_PORT),
+       };
+       static struct msghdr m = {
+               .msg_name = (struct sockaddr *) &a,
+               .msg_namelen = sizeof(a),
+               .msg_control = cmsg_data,
+               .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
+       };
+       struct in_pktinfo *pkti;
+       struct cmsghdr *cmsg;
+
+       m.msg_iov = iov;
+       m.msg_iovlen = iov_len;
+
+       memset(cmsg_data, 0, sizeof(cmsg_data));
+       cmsg = CMSG_FIRSTHDR(&m);
+       cmsg->cmsg_len = m.msg_controllen;
+       cmsg->cmsg_level = IPPROTO_IP;
+       cmsg->cmsg_type = IP_PKTINFO;
+
+       pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
+       pkti->ipi_ifindex = iface_index;
+
+       a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
+
+       return sendmsg(fd, &m, 0);
+}
+
 void
 dns_send_question(struct uloop_fd *u, char *question, int type)
 {
-       static size_t cmsg_data[( CMSG_SPACE(sizeof(struct in_pktinfo)) / sizeof(size_t)) + 1];
        static struct dns_header h = {
                .questions = cpu_to_be16(1),
        };
@@ -88,23 +121,8 @@ dns_send_question(struct uloop_fd *u, char *question, int type)
                        .iov_len = sizeof(q),
                }
        };
-       static struct sockaddr_in a = {
-               .sin_family = AF_INET,
-               .sin_port = htons(MCAST_PORT),
-       };
-       static struct msghdr m = {
-               .msg_name = (struct sockaddr *) &a,
-               .msg_namelen = sizeof(a),
-               .msg_iov = iov,
-               .msg_iovlen = ARRAY_SIZE(iov),
-               .msg_control = cmsg_data,
-               .msg_controllen = CMSG_LEN(sizeof(struct in_pktinfo)),
-       };
-       struct in_pktinfo *pkti;
-       struct cmsghdr *cmsg;
        int len;
 
-       a.sin_addr.s_addr = inet_addr(MCAST_ADDR);
        q.type = __cpu_to_be16(type);
 
        len = dn_comp(question, (void *) name_buffer, sizeof(name_buffer), NULL, NULL);
@@ -113,17 +131,7 @@ dns_send_question(struct uloop_fd *u, char *question, int type)
 
        iov[1].iov_len = len;
 
-       memset(cmsg_data, 0, sizeof(cmsg_data));
-
-       cmsg = CMSG_FIRSTHDR(&m);
-       cmsg->cmsg_len = m.msg_controllen;
-       cmsg->cmsg_level = IPPROTO_IP;
-       cmsg->cmsg_type = IP_PKTINFO;
-
-       pkti = (struct in_pktinfo*) CMSG_DATA(cmsg);
-       pkti->ipi_ifindex = iface_index;
-
-       if (sendmsg(u->fd, &m, 0) < 0)
+       if (dns_send_packet(u->fd, iov, ARRAY_SIZE(iov)) < 0)
                fprintf(stderr, "failed to send question\n");
        else
                DBG(1, "Q <- %s %s\n", dns_type_string(type), question);
@@ -165,18 +173,12 @@ dns_send_answer(struct uloop_fd *u, 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);
 
@@ -184,11 +186,6 @@ dns_send_answer(struct uloop_fd *u, char *answer)
        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[0].iov_base = &h;
        iov[0].iov_len = sizeof(struct dns_header);
 
@@ -218,7 +215,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 (dns_send_packet(u->fd, iov, (dns_answer_cnt * 3) + 1) < 0)
                fprintf(stderr, "failed to send question\n");
 
        for (i = 0; i < dns_answer_cnt; i++) {