The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
dnslib.h
Go to the documentation of this file.
1  /**************************************
2  * AUTHOR: Federico Tomassini *
3  * Copyright (C) Federico Tomassini *
4  * Contact effetom@gmail.com *
5  ***********************************************
6  ******* BEGIN 3/2006 ********
7 *************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details. *
18 * *
19 ************************************************************************/
20 #ifndef DNSLIB_H
21 #define DNSLIB_H
22 
23 #include <string.h>
24 #include <stdint.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
27 
28 #define LBL_PTR_MASK 0xC0 /* Network byte order */
29 #define LBL_PTR_OFF_MASK 0x3f /* N.b. order */
30 #define LBL_PTR(c) ((c)&LBL_PTR_MASK) /* AND whith 0xC000 */
31 
32 #define MAX_RECURSION_PTR 20
33 
34 /* PREFIXES FOR PTR QUERY */
35 #define DNS_INV_PREFIX ".IN-ADDR.ARPA"
36 #define DNS_INV_PREFIX6 ".IP6.ARPA"
37 #define OLD_DNS_INV_PREFIX6 ".IP6.INT" /* For backward compatibility */
38 
39 /* DNS QUERY-TYPE: others type will be discarded */
40 
41 #define T_AAAA 28 /* h->ip IPV6 */
42 #define T_A 1 /* h->ip IPV4 */
43 #define T_PTR 12 /* ip->h */
44 #define T_MX 15 /* h->mx */
45 /* RCODES */
46 #define DNS_RCODE_NOERR 0 /* No error */
47 #define DNS_RCODE_EINTRPRT 1 /* Intepret error */
48 #define DNS_RCODE_ESRVFAIL 2 /* Server failure */
49 #define DNS_RCODE_ENSDMN 3 /* No such domain */
50 #define DNS_RCODE_ENIMPL 4 /* Not implemented */
51 #define DNS_RCODE_ERFSD 5 /* Refused */
52 
53 /* INET CLASS */
54 #define C_IN 1
55 
56 /* RFC */
57 #define DNS_MAX_SZ 512
58 #define DNS_HDR_SZ 12
59 #define DNS_MAX_LABELS 63
60 #define DNS_MAX_HNAME_LEN 255
61 #define DNS_TTL 86400;
62 
63 #define min(x,y) ((x)<(y))?(x):(y)
64 
65 typedef struct dns_pkt_hdr {
66  uint16_t id;
67  uint8_t qr;
68  uint8_t opcode;
69  uint8_t aa;
70  uint8_t tc;
71  uint8_t rd;
72  uint8_t ra;
73  uint8_t z;
74  uint8_t rcode;
75  uint8_t qdcount;
76  uint8_t ancount;
77  uint8_t nscount;
78  uint8_t arcount;
79 } dns_pkt_hdr;
80 #define DNS_PKT_HDR_SZ sizeof(dns_pkt_hdr)
81 
82 /* DNS_PKT_HDR MACROS */
83 #define DP_QDCOUNT(dp) ((dp)->pkt_hdr).qdcount
84 #define DP_ANCOUNT(dp) ((dp)->pkt_hdr).ancount
85 #define DP_NSCOUNT(dp) ((dp)->pkt_hdr).nscount
86 #define DP_ARCOUNT(dp) ((dp)->pkt_hdr).arcount
87 
88 
89 struct dns_pkt_qst {
91  uint16_t qtype;
92  uint16_t qclass;
93  struct dns_pkt_qst *next;
94 };
95 typedef struct dns_pkt_qst dns_pkt_qst;
96 #define DNS_PKT_QST_SZ sizeof(dns_pkt_qst)
97 
98 struct dns_pkt_a
99 {
101  uint16_t type;
102  uint16_t cl;
103  uint32_t ttl;
104  uint16_t rdlength;
106  struct dns_pkt_a *next;
107 };
108 typedef struct dns_pkt_a dns_pkt_a;
109 #define DNS_PKT_A_SZ sizeof(dns_pkt_a)
110 
111 typedef struct dns_pkt
112 {
118 } dns_pkt;
119 #define DNS_PKT_SZ sizeof(dns_pkt)
120 
121 /* USER MACRO */
122 #define DNS_GET_ID(dp) (dp)->pkt_hdr.id
123 #define DNS_GET_QR(dp) (dp)->pkt_hdr.qr
124 #define DNS_GET_OPCODE(dp) (dp)->pkt_hdr.opcode
125 #define DNS_GET_AA(dp) (dp)->pkt_hdr.aa
126 #define DNS_GET_TC(dp) (dp)->pkt_hdr.tc
127 #define DNS_GET_RD(dp) (dp)->pkt_hdr.rd
128 #define DNS_GET_RA(dp) (dp)->pkt_hdr.ra
129 #define DNS_GET_Z(dp) (dp)->pkt_hdr.z
130 #define DNS_GET_RCODE(dp) (dp)->pkt_hdr.rcode
131 #define DNS_GET_QDCOUNT(dp) (dp)->pkt_hdr.qdcount
132 #define DNS_GET_ANCOUNT(dp) (dp)->pkt_hdr.ancount
133 #define DNS_GET_NSCOUNT(dp) (dp)->pkt_hdr.nscount
134 #define DNS_GET_ARCOUNT(dp) (dp)->pkt_hdr.arcount
135 
136 #define DNS_SET_ID(dp,x) (dp)->pkt_hdr.id=x
137 #define DNS_SET_QR(dp,x) (dp)->pkt_hdr.qr=x
138 #define DNS_SET_OPCODE(dp,x) (dp)->pkt_hdr.opcode=x
139 #define DNS_SET_AA(dp,x) (dp)->pkt_hdr.aa=x
140 #define DNS_SET_TC(dp,x) (dp)->pkt_hdr.tc=x
141 #define DNS_SET_RD(dp,x) (dp)->pkt_hdr.rd=x
142 #define DNS_SET_RA(dp,x) (dp)->pkt_hdr.ra=x
143 #define DNS_SET_Z(dp,x) (dp)->pkt_hdr.z=x
144 #define DNS_SET_RCODE(dp,x) (dp)->pkt_hdr.rcode=x
145 #define DNS_SET_QDCOUNT(dp,x) (dp)->pkt_hdr.qdcount=x
146 #define DNS_SET_ANCOUNT(dp,x) (dp)->pkt_hdr.ancount=x
147 #define DNS_SET_NSCOUNT(dp,x) (dp)->pkt_hdr.nscount=x
148 #define DNS_SET_ARCOUNT(dp,x) (dp)->pkt_hdr.arcount=x
149 
150 #define DP_ADD_ANSWER(dp) dns_add_a(&((dp)->pkt_answ));DP_ANCOUNT(dp)+=1;
151 #define DP_ADD_AUTH(dp) dns_add_a(&((dp)->pkt_auth));DP_NSCOUNT(dp)+=1;
152 #define DP_ADD_ADD(dp) dns_add_a(&((dp)->pkt_add));DP_ARCOUNT(dp)+=1;
153 
154 
155  /* Functions */
156 int getlblptr(char *buf);
157 int read_label_octet(const char *src,char *dst,int limit);
158 int lbltoname(char *buf,char *start_pkt,char *dst,int limit);
159 int swap_straddr(char *src,char *dst);
160 int swap_straddr6(char *src,char *dst);
161 int rm_inv_prefix(char *src,char *dst) ;
162 int add_inv_prefix(char *s,int family);
163 int swapped_straddr(char *src,char *dst) ;
164 int swapped_straddr_pref(char *src,char *dst,int family);
165 int nametolbl(char *name,char *dst);
166 int d_hdr_u(char *buf,dns_pkt_hdr *dph);
167 int d_qst_u(char *start_buf,char *buf,dns_pkt *dp,int limit_len);
168 int d_qsts_u(char *start_buf,char *buf,dns_pkt *dp,int limit_len);
169 int d_a_u(char *start_buf,char *buf,dns_pkt_a **dpa_orig,int limit_len);
170 int d_as_u(char *start_buf,char *buf,dns_pkt_a **dpa,int limit_len,int count);
171 int d_u(char *buf,int pktlen,dns_pkt **dpp);
172 int d_hdr_p(dns_pkt *dp,char *buf);
173 int d_qst_p(dns_pkt_qst *dpq,char *buf, int limitlen);
174 int d_qsts_p(dns_pkt *dp,char *buf,int limitlen);
175 int d_a_p(dns_pkt_a *dpa,char *buf,int limitlen);
176 int d_as_p(dns_pkt_a *dpa,char *buf,int limitlen,int count);
177 int d_p(dns_pkt *dp,char *buf);
178 dns_pkt* create_dns_pkt(void);
182 void dns_del_last_qst(dns_pkt *dp);
184 void dns_a_default_fill(dns_pkt *dp,dns_pkt_a *dpa);
185 void destroy_dns_pkt(dns_pkt *dp);
186 
187 
188 #endif /* DNSLIB_H */
189 
dns_pkt_hdr pkt_hdr
Definition: dnslib.h:113
dns_pkt_a * dns_add_a(dns_pkt_a **dpa)
Definition: dnslib.c:822
int d_qst_p(dns_pkt_qst *dpq, char *buf, int limitlen)
Definition: dnslib.c:608
int d_u(char *buf, int pktlen, dns_pkt **dpp)
Definition: dnslib.c:513
uint32_t ttl
Definition: dnslib.h:103
int swapped_straddr_pref(char *src, char *dst, int family)
Definition: dnslib.c:246
uint8_t opcode
Definition: dnslib.h:68
dns_pkt_a * create_dns_pkt_a(void)
Definition: dnslib.c:784
void dns_del_last_qst(dns_pkt *dp)
Definition: dnslib.c:807
Definition: dnslib.h:65
dns_pkt_a * pkt_add
Definition: dnslib.h:117
uint8_t ancount
Definition: dnslib.h:76
int read_label_octet(const char *src, char *dst, int limit)
Definition: dnslib.c:64
void dns_a_default_fill(dns_pkt *dp, dns_pkt_a *dpa)
Definition: dnslib.c:841
char rdata[255]
Definition: dnslib.h:105
Definition: dnslib.h:89
int nametolbl(char *name, char *dst)
Definition: dnslib.c:267
int add_inv_prefix(char *s, int family)
Definition: dnslib.c:214
uint16_t qclass
Definition: dnslib.h:92
char name[255]
Definition: dnslib.h:100
uint8_t qr
Definition: dnslib.h:67
uint16_t id
Definition: dnslib.h:66
Definition: dnslib.h:98
uint8_t z
Definition: dnslib.h:73
uint8_t nscount
Definition: dnslib.h:77
int d_qsts_p(dns_pkt *dp, char *buf, int limitlen)
Definition: dnslib.c:635
uint8_t rcode
Definition: dnslib.h:74
int swapped_straddr(char *src, char *dst)
Definition: dnslib.c:226
int d_hdr_u(char *buf, dns_pkt_hdr *dph)
Definition: dnslib.c:305
uint8_t aa
Definition: dnslib.h:69
struct dns_pkt dns_pkt
int d_a_u(char *start_buf, char *buf, dns_pkt_a **dpa_orig, int limit_len)
Definition: dnslib.c:411
int swap_straddr6(char *src, char *dst)
Definition: dnslib.c:178
uint16_t cl
Definition: dnslib.h:102
uint8_t qdcount
Definition: dnslib.h:75
uint16_t rdlength
Definition: dnslib.h:104
uint8_t arcount
Definition: dnslib.h:78
dns_pkt_a * pkt_auth
Definition: dnslib.h:116
uint8_t ra
Definition: dnslib.h:72
int d_p(dns_pkt *dp, char *buf)
Definition: dnslib.c:730
dns_pkt_qst * create_dns_pkt_qst(void)
Definition: dnslib.c:776
Definition: dnslib.h:111
dns_pkt_qst * pkt_qst
Definition: dnslib.h:114
dns_pkt * create_dns_pkt(void)
Definition: dnslib.c:764
char qname[255]
Definition: dnslib.h:90
int d_as_u(char *start_buf, char *buf, dns_pkt_a **dpa, int limit_len, int count)
Definition: dnslib.c:488
dns_pkt_qst * dns_add_qst(dns_pkt *dp)
Definition: dnslib.c:794
int d_qst_u(char *start_buf, char *buf, dns_pkt *dp, int limit_len)
Definition: dnslib.c:353
uint16_t qtype
Definition: dnslib.h:91
void destroy_dns_pkt(dns_pkt *dp)
Definition: dnslib.c:848
int d_as_p(dns_pkt_a *dpa, char *buf, int limitlen, int count)
Definition: dnslib.c:704
uint16_t type
Definition: dnslib.h:101
int swap_straddr(char *src, char *dst)
Definition: dnslib.c:136
int d_hdr_p(dns_pkt *dp, char *buf)
Definition: dnslib.c:564
dns_pkt_a * pkt_answ
Definition: dnslib.h:115
int d_qsts_u(char *start_buf, char *buf, dns_pkt *dp, int limit_len)
Definition: dnslib.c:390
int rm_inv_prefix(char *src, char *dst)
Definition: dnslib.c:190
uint8_t tc
Definition: dnslib.h:70
int family
Definition: if.c:34
#define DNS_MAX_HNAME_LEN
Definition: dnslib.h:60
uint8_t rd
Definition: dnslib.h:71
struct dns_pkt_qst * next
Definition: dnslib.h:93
struct dns_pkt_a * next
Definition: dnslib.h:106
int lbltoname(char *buf, char *start_pkt, char *dst, int limit)
Definition: dnslib.c:85
int d_a_p(dns_pkt_a *dpa, char *buf, int limitlen)
Definition: dnslib.c:652
int getlblptr(char *buf)
Definition: dnslib.c:34
struct dns_pkt_hdr dns_pkt_hdr