djbdnscurve6  38
djbdnscurve6
dns_packet.c
Go to the documentation of this file.
1 /*
2 DNS should have used LZ77 instead of its own sophomoric compression algorithm.
3 */
4 
5 #include "error.h"
6 #include "dns.h"
7 
8 unsigned int dns_packet_copy(const char *buf,unsigned int len,unsigned int pos,char *out,unsigned int outlen)
9 {
10  while (outlen) {
11  if (pos >= len) { errno = EPROTO ; return 0; }
12  *out = buf[pos++];
13  ++out; --outlen;
14  }
15  return pos;
16 }
17 
18 unsigned int dns_packet_skipname(const char *buf,unsigned int len,unsigned int pos)
19 {
20  unsigned char ch;
21 
22  for (;;) {
23  if (pos >= len) break;
24  ch = buf[pos++];
25  if (ch >= 192) return pos + 1;
26  if (ch >= 64) break;
27  if (!ch) return pos;
28  pos += ch;
29  }
30 
31  errno = EPROTO;
32  return 0;
33 }
34 
35 unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int pos,char **d)
36 {
37  unsigned int loop = 0;
38  unsigned int state = 0;
39  unsigned int firstcompress = 0;
40  unsigned int where;
41  unsigned char ch;
42  char name[255];
43  unsigned int namelen = 0;
44 
45  for (;;) {
46  if (pos >= len) goto PROTO; ch = buf[pos++];
47  if (++loop >= 1000) goto PROTO;
48 
49  if (state) {
50  if (namelen + 1 > sizeof(name)) goto PROTO; name[namelen++] = ch;
51  --state;
52  } else {
53  while (ch >= 192) {
54  where = ch; where -= 192; where <<= 8;
55  if (pos >= len) goto PROTO; ch = buf[pos++];
56  if (!firstcompress) firstcompress = pos;
57  pos = where + ch;
58  if (pos >= len) goto PROTO; ch = buf[pos++];
59  if (++loop >= 1000) goto PROTO;
60  }
61  if (ch >= 64) goto PROTO;
62  if (namelen + 1 > sizeof(name)) goto PROTO; name[namelen++] = ch;
63  if (!ch) break;
64  state = ch;
65  }
66  }
67 
68  if (!dns_domain_copy(d,name)) return 0;
69 
70  if (firstcompress) return firstcompress;
71  return pos;
72 
73  PROTO:
74  errno = EPROTO;
75  return 0;
76 }
uint16 len
Definition: axfrdns.c:302
char buf[MSGSIZE]
Definition: axfrdns.c:301
int dns_domain_copy(char **, const char *)
Definition: dns_domain.c:25
unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d)
Definition: dns_packet.c:35
unsigned int dns_packet_copy(const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
Definition: dns_packet.c:8
unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos)
Definition: dns_packet.c:18
char name[DNS_NAME6_DOMAIN]
Definition: dnsfilter.c:52
void out(const char *s, unsigned int len)
Definition: generic-conf.c:54
void d(const char *home, const char *subdir, int uid, int gid, int mode)