1
/* Copyright (c) 2009 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
/* Simple UDP DNS server; this sends "10.11.12.13" as an IP as a reply
20
* to any DNS packet; to test handle_noreply this only answers every
23
#include <sys/socket.h>
24
#include <netinet/in.h>
25
#include <arpa/inet.h>
32
/* This is the IP "10.11.12.13" added to their question */
34
"\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04\x0a\x0b\x0c\x0d";
37
int a, b, len_inet, sock;
38
struct sockaddr_in dns_udp;
41
int localhost = 0x7f000001; /* 127.0.0.1 */
42
int leni = sizeof(struct sockaddr);
44
/* Bind to 127.0.0.1 port 53 */
45
sock = socket(AF_INET,SOCK_DGRAM,0);
46
if(sock == -1) {printf("socket error\n");exit(0);}
47
memset(&dns_udp,0,sizeof(dns_udp));
48
dns_udp.sin_family = AF_INET;
49
dns_udp.sin_port = htons(53);
50
dns_udp.sin_addr.s_addr = htonl(localhost);
51
if(dns_udp.sin_addr.s_addr == INADDR_NONE) {
52
printf("htonl error\n");exit(0);}
53
len_inet = sizeof(dns_udp);
54
if(bind(sock,(struct sockaddr *)&dns_udp,len_inet) == -1) {
55
printf("bind error\n");exit(0);}
57
/* Linux kernel bug */
58
/* fcntl(sock, F_SETFL, O_NONBLOCK); */
61
/* Get data from UDP port 53 */
62
len_inet = recvfrom(sock,in,255,0,(struct sockaddr *)&dns_udp,&foo);
63
/* Roy Arends check: We only answer questions */
64
if(len_inet < 3 || (in[2] & 0x80) != 0x00) {
68
/* Make this an answer */
70
/* We add an additional answer */
74
in[len_inet + a] = p[a];
77
/* Munge the question a little */
78
if(in[12] > 0 && in[12] < 63) {
83
sendto(sock,in,len_inet + 16,0,
84
(struct sockaddr *)&dns_udp,leni);
85
printf("Packet sent\n");