~ubuntu-branches/ubuntu/wily/maradns/wily-proposed

« back to all changes in this revision

Viewing changes to deadwood-3.2.05/sqa/sqa_root_upstream/microdns_rd_must_be_clear.c

  • Committer: Package Import Robot
  • Author(s): Dariusz Dwornikowski, Tomasz Buchert, Dariusz Dwornikowski
  • Date: 2015-03-27 18:34:08 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20150327183408-wnfachdkdjt96yu6
Tags: 2.0.11-1
[ Tomasz Buchert ]
* Imported Upstream version 2.0.11

[ Dariusz Dwornikowski ]
* d/patches: 
  - refreshed all patches for new deadwood version
  - removed generating of random prime on build (Closes: #785536) 
* d/rules: date taken from changelog (Closes: #785535)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2009-2011 Sam Trenholme
2
 
 *
3
 
 * TERMS
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions
7
 
 * are met:
8
 
 *
9
 
 * 1. Redistributions of source code must retain the above copyright
10
 
 *    notice, this list of conditions and the following disclaimer.
11
 
 * 2. Redistributions in binary form must reproduce the above copyright
12
 
 *    notice, this list of conditions and the following disclaimer in the
13
 
 *    documentation and/or other materials provided with the distribution.
14
 
 *
15
 
 * This software is provided 'as is' with no guarantees of correctness or
16
 
 * fitness for purpose.
17
 
 */
18
 
 
19
 
#include <sys/socket.h>
20
 
#include <netinet/in.h>
21
 
#include <arpa/inet.h>
22
 
#include <string.h>
23
 
#include <unistd.h>
24
 
#include <fcntl.h>
25
 
#include <stdlib.h>
26
 
#include <stdio.h>
27
 
#include <stdint.h>
28
 
 
29
 
/* We use a special SOCKET type for easier Windows porting */
30
 
#define SOCKET int
31
 
 
32
 
/* This is the header placed before the 4-byte IP; we change the last four
33
 
 * bytes to set the IP we give out in replies */
34
 
char p[17] =
35
 
"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04\x7f\x7f\x7f\x7f";
36
 
 
37
 
/* microdns: This is a tiny DNS server that does only one thing: It
38
 
   always sends a given IPv4 IP to any and all queries sent to the server.
39
 
   The IP to send the user is given in the first argument; the second optional
40
 
   argument is the IP of this tiny DNS server.  If the second argument is not
41
 
   given, microdns binds to "0.0.0.0": All the IP addresses the server has.
42
 
 
43
 
   For example, to have micrdns always give the IP address 10.1.2.3 on the
44
 
   IP 127.0.0.1:
45
 
 
46
 
        microdns 10.1.2.3 127.0.0.1
47
 
 
48
 
 */
49
 
 
50
 
/* Based on command-line arguments, set the IP we will bind to and the
51
 
   IP we send over the pipe */
52
 
uint32_t get_ip(int argc, char **argv) {
53
 
 
54
 
        uint32_t ip;
55
 
 
56
 
        /* Set the BIND ip and the IP we give everyone */
57
 
        if(argc < 2 || argc > 3) {
58
 
                printf(
59
 
                "Usage: microdns {ip to give out} [{ip of microdns server}]\n"
60
 
                );
61
 
                exit(1);
62
 
                }
63
 
 
64
 
        /* Set the IP we give everyone */
65
 
        ip = inet_addr(argv[1]);
66
 
        ip = ntohl(ip);
67
 
        p[12] = (ip & 0xff000000) >> 24;
68
 
        p[13] = (ip & 0x00ff0000) >> 16;
69
 
        p[14] = (ip & 0x0000ff00) >>  8;
70
 
        p[15] = (ip & 0x000000ff);
71
 
 
72
 
        /* Set the IP we bind to (default is "0", which means "all IPs) */
73
 
        ip = 0;
74
 
        if(argc == 3) {
75
 
                ip = inet_addr(argv[2]);
76
 
        }
77
 
        /* Return the IP we bind to */
78
 
        return ip;
79
 
}
80
 
 
81
 
/* Get port: Get a port locally and return the socket the port is on */
82
 
SOCKET get_port(uint32_t ip, char **argv, struct sockaddr_in *dns_udp) {
83
 
        SOCKET sock;
84
 
        int len_inet;
85
 
 
86
 
        /* Bind to port 53 */
87
 
        sock = socket(AF_INET,SOCK_DGRAM,0);
88
 
        if(sock == -1) {
89
 
                perror("socket error");
90
 
                exit(0);
91
 
        }
92
 
        memset(dns_udp,0,sizeof(struct sockaddr_in));
93
 
        dns_udp->sin_family = AF_INET;
94
 
        dns_udp->sin_port = htons(53);
95
 
        dns_udp->sin_addr.s_addr = ip;
96
 
        if(dns_udp->sin_addr.s_addr == INADDR_NONE) {
97
 
                printf("Problem with bind IP %s\n",argv[2]);
98
 
                exit(0);
99
 
        }
100
 
        len_inet = sizeof(struct sockaddr_in);
101
 
        if(bind(sock,(struct sockaddr *)dns_udp,len_inet) == -1) {
102
 
                perror("bind error");
103
 
                exit(0);
104
 
        }
105
 
 
106
 
        /* Linux kernel bug */
107
 
        /* fcntl(sock, F_SETFL, O_NONBLOCK); */
108
 
 
109
 
        return sock;
110
 
}
111
 
 
112
 
int main(int argc, char **argv) {
113
 
        int a, len_inet;
114
 
        SOCKET sock;
115
 
        char in[512];
116
 
        socklen_t foo = sizeof(in);
117
 
        struct sockaddr_in dns_udp;
118
 
        uint32_t ip = 0; /* 0.0.0.0; default bind IP */
119
 
        int leni = sizeof(struct sockaddr);
120
 
        int good = 1;
121
 
 
122
 
        ip = get_ip(argc, argv);
123
 
        sock = get_port(ip,argv,&dns_udp);
124
 
 
125
 
        /* Now that we know the IP and are on port 53, process incoming
126
 
         * DNS requests */
127
 
        for(;;) {
128
 
                good = 1;
129
 
                /* Get data from UDP port 53 */
130
 
                len_inet = recvfrom(sock,in,255,0,(struct sockaddr *)&dns_udp,
131
 
                        &foo);
132
 
                /* Roy Arends check: We only answer questions */
133
 
                if(len_inet < 3 || (in[2] & 0x80) != 0x00) {
134
 
                        continue;
135
 
                }
136
 
 
137
 
                if((in[2] & 0x01) == 0x01) { /* RD bit is set */
138
 
                        good = 0;
139
 
                }
140
 
 
141
 
                /* Prepare the reply */
142
 
                if(len_inet > 12) {
143
 
                        /* Make this an answer */
144
 
                        in[2] |= 0x80;
145
 
                        if(good == 1) {
146
 
                                /* We add an additional answer */
147
 
                                in[7]++;
148
 
                        } else {
149
 
                                in[3] &= 0xf0; in[3] |= 2; /* SERVFAIL */
150
 
                        }
151
 
                }
152
 
                if(good == 1) {
153
 
                        for(a=0;a<16;a++) {
154
 
                                in[len_inet + a] = p[a];
155
 
                        }
156
 
                }
157
 
 
158
 
                /* Send the reply */
159
 
                sendto(sock,in,len_inet + 16,0, (struct sockaddr *)&dns_udp,
160
 
                        leni);
161
 
        }
162
 
 
163
 
}
164