1
/* Copyright (c) 2009-2011 Sam Trenholme
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
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.
15
* This software is provided 'as is' with no guarantees of correctness or
16
* fitness for purpose.
19
#include <sys/socket.h>
20
#include <netinet/in.h>
21
#include <arpa/inet.h>
29
/* We use a special SOCKET type for easier Windows porting */
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 */
35
"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04\x7f\x7f\x7f\x7f";
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.
43
For example, to have micrdns always give the IP address 10.1.2.3 on the
46
microdns 10.1.2.3 127.0.0.1
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) {
56
/* Set the BIND ip and the IP we give everyone */
57
if(argc < 2 || argc > 3) {
59
"Usage: microdns {ip to give out} [{ip of microdns server}]\n"
64
/* Set the IP we give everyone */
65
ip = inet_addr(argv[1]);
67
p[12] = (ip & 0xff000000) >> 24;
68
p[13] = (ip & 0x00ff0000) >> 16;
69
p[14] = (ip & 0x0000ff00) >> 8;
70
p[15] = (ip & 0x000000ff);
72
/* Set the IP we bind to (default is "0", which means "all IPs) */
75
ip = inet_addr(argv[2]);
77
/* Return the IP we bind to */
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) {
87
sock = socket(AF_INET,SOCK_DGRAM,0);
89
perror("socket error");
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]);
100
len_inet = sizeof(struct sockaddr_in);
101
if(bind(sock,(struct sockaddr *)dns_udp,len_inet) == -1) {
102
perror("bind error");
106
/* Linux kernel bug */
107
/* fcntl(sock, F_SETFL, O_NONBLOCK); */
112
int main(int argc, char **argv) {
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);
122
ip = get_ip(argc, argv);
123
sock = get_port(ip,argv,&dns_udp);
125
/* Now that we know the IP and are on port 53, process incoming
129
/* Get data from UDP port 53 */
130
len_inet = recvfrom(sock,in,255,0,(struct sockaddr *)&dns_udp,
132
/* Roy Arends check: We only answer questions */
133
if(len_inet < 3 || (in[2] & 0x80) != 0x00) {
137
if((in[2] & 0x01) == 0x01) { /* RD bit is set */
141
/* Prepare the reply */
143
/* Make this an answer */
146
/* We add an additional answer */
149
in[3] &= 0xf0; in[3] |= 2; /* SERVFAIL */
154
in[len_inet + a] = p[a];
159
sendto(sock,in,len_inet + 16,0, (struct sockaddr *)&dns_udp,