fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
dns_dfd.c
Go to the documentation of this file.
1#include "error.h"
2#include "alloc.h"
3#include "byte.h"
4#include "dnsresolv.h"
5
13int dns_domain_fromdot(char **out,const char *buf,unsigned int n)
14{
15 char label[63];
16 unsigned int labellen = 0; /* <= sizeof label */
17 char name[255];
18 unsigned int namelen = 0; /* <= sizeof name */
19 char ch;
20 char *x;
21
22 errno = EPROTO;
23
24 for (;;) {
25 if (!n) break;
26 ch = *buf++; --n;
27 if (ch == '.') {
28 if (labellen) {
29 if (namelen + labellen + 1 > sizeof(name)) return 0;
30 name[namelen++] = labellen;
31 byte_copy(name + namelen,labellen,label);
32 namelen += labellen;
33 labellen = 0;
34 }
35 continue;
36 }
37 if (ch == '\\') { // octal -> decimal
38 if (!n) break;
39 ch = *buf++; --n;
40 if ((ch >= '0') && (ch <= '7')) {
41 ch -= '0';
42 if (n && (*buf >= '0') && (*buf <= '7')) {
43 ch <<= 3;
44 ch += *buf - '0';
45 ++buf; --n;
46 if (n && (*buf >= '0') && (*buf <= '7')) {
47 ch <<= 3;
48 ch += *buf - '0';
49 ++buf; --n;
50 }
51 }
52 }
53 }
54 if (labellen >= sizeof(label)) return 0;
55 label[labellen++] = ch;
56 }
57
58 if (labellen) {
59 if (namelen + labellen + 1 > sizeof(name)) return 0;
60 name[namelen++] = labellen;
61 byte_copy(name + namelen,labellen,label);
62 namelen += labellen;
63 labellen = 0;
64 }
65
66 if (namelen + 1 > sizeof(name)) return 0;
67 name[namelen++] = 0;
68
69 x = alloc(namelen);
70 if (!x) return DNS_MEM;
71 byte_copy(x,namelen,name);
72
73 if (*out) alloc_free(*out);
74 *out = x;
75 return 1;
76}
int dns_domain_fromdot(char **out, const char *buf, unsigned int n)
Definition: dns_dfd.c:13
#define EPROTO
Definition: error.h:7
void alloc_free(void *)
Definition: alloc.c:45
void * alloc(unsigned int)
Definition: alloc.c:27
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
#define DNS_MEM
Definition: dnsresolv.h:43