fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_packet.c
Go to the documentation of this file.
1#include "error.h"
2#include "dnsresolv.h"
3
12unsigned int dns_packet_copy(const char *buf,unsigned int len,unsigned int pos,char *out,unsigned int outlen)
13{
14 while (outlen) {
15 if (pos >= len) { errno = EPROTO; return 0; }
16 *out = buf[pos++];
17 ++out; --outlen;
18 }
19 return pos;
20}
21
22unsigned int dns_packet_skipname(const char *buf,unsigned int len,unsigned int pos)
23{
24 unsigned char ch;
25
26 for (;;) {
27 if (pos >= len) break;
28 ch = buf[pos++];
29 if (ch >= 192) return pos + 1;
30 if (ch >= 64) break;
31 if (!ch) return pos;
32 pos += ch;
33 }
34
35 errno = EPROTO;
36 return 0;
37}
38
39unsigned int dns_packet_getname(const char *buf,unsigned int len,unsigned int pos,char **d)
40{
41 unsigned int loop = 0;
42 unsigned int state = 0;
43 unsigned int firstcompress = 0;
44 unsigned int where;
45 unsigned char ch;
46 char name[255];
47 unsigned int namelen = 0;
48
49 for (;;) {
50 if (pos >= len) goto PROTO;
51 ch = buf[pos++];
52 if (++loop >= 1000) goto PROTO;
53
54 if (state) {
55 if (namelen + 1 > sizeof(name)) goto PROTO;
56 name[namelen++] = ch;
57 --state;
58 } else {
59 while (ch >= 192) {
60 where = ch; where -= 192; where <<= 8;
61 if (pos >= len) goto PROTO;
62 ch = buf[pos++];
63 if (!firstcompress) firstcompress = pos;
64 pos = where + ch;
65 if (pos >= len) goto PROTO;
66 ch = buf[pos++];
67 if (++loop >= 1000) goto PROTO;
68 }
69 if (ch >= 64) goto PROTO;
70 if (namelen + 1 > sizeof(name)) goto PROTO;
71 name[namelen++] = ch;
72 if (!ch) break;
73 state = ch;
74 }
75 }
76
77 if (!dns_domain_copy(d,name)) return 0;
78
79 if (firstcompress) return firstcompress;
80 return pos;
81
82 PROTO:
83 errno = EPROTO;
84 return 0;
85}
unsigned int dns_packet_getname(const char *buf, unsigned int len, unsigned int pos, char **d)
Definition: dns_packet.c:39
unsigned int dns_packet_copy(const char *buf, unsigned int len, unsigned int pos, char *out, unsigned int outlen)
Definition: dns_packet.c:12
unsigned int dns_packet_skipname(const char *buf, unsigned int len, unsigned int pos)
Definition: dns_packet.c:22
#define EPROTO
Definition: error.h:7
int dns_domain_copy(char **, const char *)
Definition: dns_domain.c:32