aboutsummaryrefslogtreecommitdiff
path: root/server.c
blob: b754265a5d279aff84c3f3cea1b3dc453cb612a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "edns0.h"
#include "byte.h"
#include "case.h"
#include "env.h"
#include "buffer.h"
#include "strerr.h"
#include "ip4.h"
#include "ip6.h"
#include "uint16.h"
#include "ndelay.h"
#include "socket.h"
#include "droproot.h"
#include "qlog.h"
#include "response.h"
#include "dns.h"
#include "alloc.h"
#include "iopause.h"
#include "str.h"

extern char *fatal;
extern char *starting;
extern int respond(char *,char *,char *);
extern void initialize(void);

static char ip[16];
static uint16 port;

static char buf[513];
static int len;

static char *q;

void nomem()
{
  strerr_die2x(111,fatal,"out of memory");
}

static int doit(void)
{
  unsigned int pos;
  char header[12];
  char qtype[2];
  char qclass[2];

  if (len >= sizeof buf) goto NOQ;
  pos = dns_packet_copy(buf,len,0,header,12); if (!pos) goto NOQ;
  if (header[2] & 128) goto NOQ;
  if (header[4]) goto NOQ;
  if (header[5] != 1) goto NOQ;

  pos = dns_packet_getname(buf,len,pos,&q); if (!pos) goto NOQ;
  pos = dns_packet_copy(buf,len,pos,qtype,2); if (!pos) goto NOQ;
  pos = dns_packet_copy(buf,len,pos,qclass,2); if (!pos) goto NOQ;

  if (!response_query(q,qtype,qclass)) goto NOQ;
  response_id(header);
  if (byte_equal(qclass,2,DNS_C_IN))
    response[2] |= 4;
  else
    if (byte_diff(qclass,2,DNS_C_ANY)) goto WEIRDCLASS;
  response[3] &= ~128;
  if (!(header[2] & 1)) response[2] &= ~1;

  if (header[2] & 126) goto NOTIMP;
  if (byte_equal(qtype,2,DNS_T_AXFR)) goto NOTIMP;

  pos = check_edns0(header, buf, len, pos);
  if (!pos) goto NOQ;

  case_lowerb(q,dns_domain_length(q));
  if (!respond(q,qtype,ip)) {
    qlog(ip,port,header,q,qtype," - ");
    return 0;
  }
  qlog(ip,port,header,q,qtype," + ");
  return 1;

  NOTIMP:
  response[3] &= ~15;
  response[3] |= 4;
  qlog(ip,port,header,q,qtype," I ");
  return 1;

  WEIRDCLASS:
  response[3] &= ~15;
  response[3] |= 1;
  qlog(ip,port,header,q,qtype," C ");
  return 1;

  NOQ:
  qlog(ip,port,"\0\0","","\0\0"," / ");
  return 0;
}

int main()
{
  char *x;
  int *udp53;
  unsigned int off;
  unsigned int cnt;
  iopause_fd *iop;

  x = env_get("IP");
  if (!x)
    strerr_die2x(111,fatal,"$IP not set");
  off=cnt=0;
  while (x[off]) {
    unsigned int l;
    char dummy[16];
    l=ip6_scan(x+off,dummy);
    if (!l)
      strerr_die3x(111,fatal,"unable to parse IP address ",x+off);
    cnt++;
    if (!x[off+l]) break;
    if (x[off+l]=='%')
      while (x[off+l] && x[off+l]!=',') ++l;
    if (x[off+l]!=',')
      strerr_die3x(111,fatal,"unable to parse IP address ",x+off);
    off+=l+1;
  }
  udp53=(int *) alloc(sizeof(int) *cnt);
  if (!udp53) nomem();
  iop=(iopause_fd *) alloc(sizeof(*iop) * cnt);
  if (!iop) nomem();

  off=cnt=0;
  while (x[off]) {
    unsigned int l;
    uint32 ifid=0;
    l=ip6_scan(x+off,ip);
    udp53[cnt] = socket_udp6();
    if (udp53[cnt] == -1)
      strerr_die2sys(111,fatal,"unable to create UDP socket: ");
    if (x[off+l]=='%') {
      char* interface=x+off+l+1;
      int Len=str_chr(interface,',');
      if (interface[Len]) {
	interface[Len]=0;
	ifid=socket_getifidx(interface);
	interface[Len]=',';
      } else
	ifid=socket_getifidx(interface);
      l+=Len;
    }
    if (socket_bind6_reuse(udp53[cnt],ip,53,ifid) == -1)
      strerr_die2sys(111,fatal,"unable to bind UDP socket: ");
    ndelay_off(udp53[cnt]);
    socket_tryreservein(udp53[cnt],65536);
    iop[cnt].fd=udp53[cnt];
    iop[cnt].events=IOPAUSE_READ;
    cnt++;
    if (!x[off+l]) break;
    off+=l+1;
  }
  droproot(fatal);

  initialize();

  buffer_putsflush(buffer_2,starting);

  for (;;) {
    struct taia stamp;
    struct taia deadline;
    unsigned int i;
    uint32 ifid;
    taia_now(&stamp);
    taia_uint(&deadline,300);
    taia_add(&deadline,&deadline,&stamp);
    iopause(iop,cnt,&deadline,&stamp);
    for (i=0;i<cnt;i++)
      if (iop[i].revents) {
	len = socket_recv6(udp53[i],buf,sizeof buf,ip,&port,&ifid);
	if (len < 0) continue;
	if (!doit()) continue;
	if (response_len > max_response_len) response_tc();
	socket_send6(udp53[i],response,response_len,ip,port,ifid);
	/* may block for buffer space; if it fails, too bad */
      }
  }
}