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

« back to all changes in this revision

Viewing changes to deadwood-3.2.05/sqa/sqa_easydns_bad_truncation/simulate_easydns.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-2012 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\xff\x00\x04\x7f\x7f\x7f\x7f";
36
 
 
37
 
uint32_t get_ip(int argc, char **argv) {
38
 
 
39
 
        uint32_t ip;
40
 
 
41
 
        /* Set the BIND ip and the IP we give everyone */
42
 
        if(argc < 2 || argc > 3) {
43
 
                printf(
44
 
                "Usage: microdns {ip to give out} [{ip of microdns server}]\n"
45
 
                );
46
 
                exit(1);
47
 
                }
48
 
 
49
 
        /* Set the IP we give everyone */
50
 
        ip = inet_addr(argv[1]);
51
 
        ip = ntohl(ip);
52
 
        p[12] = (ip & 0xff000000) >> 24;
53
 
        p[13] = (ip & 0x00ff0000) >> 16;
54
 
        p[14] = (ip & 0x0000ff00) >>  8;
55
 
        p[15] = (ip & 0x000000ff);
56
 
 
57
 
        /* Set the IP we bind to (default is "0", which means "all IPs) */
58
 
        ip = 0;
59
 
        if(argc == 3) {
60
 
                ip = inet_addr(argv[2]);
61
 
        }
62
 
        /* Return the IP we bind to */
63
 
        return ip;
64
 
}
65
 
 
66
 
/* Get port: Get a port locally and return the socket the port is on */
67
 
SOCKET get_port(uint32_t ip, char **argv, struct sockaddr_in *dns_udp) {
68
 
        SOCKET sock;
69
 
        int len_inet;
70
 
 
71
 
        /* Bind to port 53 */
72
 
        sock = socket(AF_INET,SOCK_DGRAM,0);
73
 
        if(sock == -1) {
74
 
                perror("socket error");
75
 
                exit(0);
76
 
        }
77
 
        memset(dns_udp,0,sizeof(struct sockaddr_in));
78
 
        dns_udp->sin_family = AF_INET;
79
 
        dns_udp->sin_port = htons(53);
80
 
        dns_udp->sin_addr.s_addr = ip;
81
 
        if(dns_udp->sin_addr.s_addr == INADDR_NONE) {
82
 
                printf("Problem with bind IP %s\n",argv[2]);
83
 
                exit(0);
84
 
        }
85
 
        len_inet = sizeof(struct sockaddr_in);
86
 
        if(bind(sock,(struct sockaddr *)dns_udp,len_inet) == -1) {
87
 
                perror("bind error");
88
 
                exit(0);
89
 
        }
90
 
 
91
 
        /* Linux kernel bug */
92
 
        /* fcntl(sock, F_SETFL, O_NONBLOCK); */
93
 
 
94
 
        return sock;
95
 
}
96
 
 
97
 
int main(int argc, char **argv) {
98
 
        int a, len_inet;
99
 
        SOCKET sock;
100
 
        char in[512];
101
 
        socklen_t foo = sizeof(in);
102
 
        struct sockaddr_in dns_udp;
103
 
        uint32_t ip = 0; /* 0.0.0.0; default bind IP */
104
 
        int leni = sizeof(struct sockaddr);
105
 
        int truncated_trigger = 1;
106
 
 
107
 
        ip = get_ip(argc, argv);
108
 
        sock = get_port(ip,argv,&dns_udp);
109
 
 
110
 
        /* Now that we know the IP and are on port 53, process incoming
111
 
         * DNS requests */
112
 
        for(;;) {
113
 
                /* Get data from UDP port 53 */
114
 
                len_inet = recvfrom(sock,in,255,0,(struct sockaddr *)&dns_udp,
115
 
                        &foo);
116
 
                /* Roy Arends check: We only answer questions */
117
 
                if(len_inet < 3 || (in[2] & 0x80) != 0x00) {
118
 
                        continue;
119
 
                }
120
 
 
121
 
                /* Prepare the reply */
122
 
                if(len_inet > 12) {
123
 
                        /* Make this an answer */
124
 
                        in[2] |= 0x80;
125
 
                        if(in[11] == 0) { /* EDNS not supported */
126
 
                                /* We add an additional answer */
127
 
                                in[7]++;
128
 
                        } else {
129
 
                                in[3] &= 0xf0; in[3] |= 4; /* NOTIMPL */
130
 
                        }
131
 
                }
132
 
                if(in[11] == 0) {
133
 
                        /* EDNS not supported */
134
 
                        for(a=0;a<16;a++) {
135
 
                                in[len_inet + a] = p[a];
136
 
                        }
137
 
                }
138
 
                if(truncated_trigger == 1) {
139
 
                        truncated_trigger = 0;
140
 
                        in[2] |= 0x03;
141
 
                        len_inet -= 16;
142
 
                        in[7]--;
143
 
                } else { /* Half of the replies are EasyDNS-style truncated */
144
 
                        truncated_trigger = 1;
145
 
                }
146
 
 
147
 
                /* Send the reply */
148
 
                sendto(sock,in,len_inet + 16,0, (struct sockaddr *)&dns_udp,
149
 
                        leni);
150
 
        }
151
 
 
152
 
}
153