The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
ntkresolv.h
Go to the documentation of this file.
1 #ifndef NTK_RESOLV_H
2 #define NTK_RESOLV_H
3 
4 #include <errno.h>
5 #include <netdb.h>
6 #include <strings.h>
7 #include <arpa/inet.h>
8 #include <sys/socket.h>
9 
10 #include "andns_lib.h"
11 
12 #define VERSION "0.3.3"
13 #define NTK_RESOLV_MAIL_BUGS "https://github.com/Netsukuku/netsukuku/issues"
14 
15 #define NTKRESOLV_PORT 53
16 #define NTKRESOLV_PORT_STR "53"
17 #define MAX_NS 3
18 #define LOCALHOST "localhost"
19 
20 #define NTK_RESOLV_TIMEOUT 15
21 
22 #define MAX_HOSTNAME_LEN 512
23 #define NTKRESOLV_MAX_OBJ_LEN 512
24 
25 
26 //#define ANDNS_MAX_SZ 1024
27 
28 #define min(x,y) (x)<(y)?(x):(y)
29 
30 #define REALM_NTK 0+1
31 #define REALM_INT 1+1
32 #define REALM_NTK_STR "ntk"
33 #define REALM_INT_STR "inet"
34 
35 #define QTYPE_A AT_A
36 #define QTYPE_PTR AT_PTR
37 #define QTYPE_G AT_G
38 #define QTYPE_MX 3
39 #define QTYPE_A_STR "snsd"
40 #define QTYPE_PTR_STR "ptr"
41 #define QTYPE_G_STR "global"
42 #define QTYPE_MX_STR "mx"
43 
44 #define SNSD_PROTO_TCP 0
45 #define SNSD_PROTO_UDP 1
46 #define SNSD_PROTO_TCP_STR "tcp"
47 #define SNSD_PROTO_UDP_STR "udp"
48 
49 #define SNSD_PROTO_DEFAULT SNSD_PROTO_TCP
50 #define SNSD_SERVICE_DEFAULT 0
51 
52 /* NK BIT */
53 #define NK_DNS 0
54 #define NK_NTK 1
55 #define NK_INET 2
56 
57 #define TIME_SCALE 1000000.0
58 #define HELP_STR "help"
59 
60 
61 
63 int QT_LEN=4;
64 
65 #define QTFROMPREF(s) \
66 ({ \
67  int __n,__res=-1; \
68  for (__n=0;__n<QT_LEN;__n++) \
69  if (!strncasecmp(s,QTYPE_STR_LIST[__n],strlen(s))) \
70  {__res=__n;break;} \
71  __res; })
72 #define REALMFROMPREF(s) \
73 ({ \
74  uint8_t __res=0; \
75  if (!strncasecmp(REALM_NTK_STR,s,strlen(s))) \
76  __res=REALM_NTK; \
77  else if (!strncasecmp(REALM_INT_STR,s,strlen(s))) \
78  __res=REALM_INT; \
79  __res; })
80 #define PROTOFROMPREF(s) \
81 ({ \
82  uint8_t __res=-1; \
83  if (!strncasecmp(SNSD_PROTO_UDP_STR,s,strlen(s))) \
84  __res=SNSD_PROTO_UDP; \
85  else if (!strncasecmp(SNSD_PROTO_TCP_STR,s,strlen(s))) \
86  __res=SNSD_PROTO_TCP; \
87  __res; })
88 
89 
90 
91 typedef struct ntkresolv_opts {
93  int16_t port;
94  int8_t silent;
96  uint16_t id;
97  uint8_t hash;
100 
101 #define NTKRESOLV_OPTS_SZ sizeof(ntkresolv_opts)
102 
103 #define QR_STR(ap) ((ap)->qr==0)?"QUERY":"ANSWER"
104 #define QTYPE_STR(ap) \
105 ({ \
106  char *__c; \
107  switch((ap)->qtype) { \
108  case AT_A: \
109  __c="Host2Ip"; \
110  break; \
111  case AT_PTR: \
112  __c="Ip2Host"; \
113  break; \
114  case AT_G: \
115  __c=" Global"; \
116  break; \
117  default: \
118  __c="Unknow"; \
119  break; \
120  } \
121  __c;})
122 #define NK_STR(ap) \
123 ({ \
124  char *__d; \
125  switch((ap)->nk) { \
126  case NK_DNS: \
127  __d="DNS"; \
128  break; \
129  case NK_NTK: \
130  __d="Ntk"; \
131  break; \
132  case NK_INET: \
133  __d="Inet"; \
134  break; \
135  default: \
136  __d="UNKNOW"; \
137  break; \
138  } \
139  __d;})
140 
141 #define RCODE_STR(ap) \
142 ({ \
143  char *__e; \
144  switch((ap)->rcode) { \
145  case ANDNS_RCODE_NOERR: \
146  __e="NoError"; \
147  break; \
148  case ANDNS_RCODE_EINTRPRT: \
149  __e="InError"; \
150  break; \
151  case ANDNS_RCODE_ESRVFAIL: \
152  __e="SrvFail"; \
153  break; \
154  case ANDNS_RCODE_ENSDMN: \
155  __e="NoXHost"; \
156  break; \
157  case ANDNS_RCODE_ENIMPL: \
158  __e="NotImpl"; \
159  break; \
160  case ANDNS_RCODE_ERFSD: \
161  __e="Refused"; \
162  break; \
163  default: \
164  __e="UNKNOW"; \
165  break; \
166  } \
167  __e;})
168 #define IPV_STR(ap) \
169 ({ \
170  char *__f; \
171  switch((ap)->ipv) { \
172  case ANDNS_IPV4: \
173  __f="IPv4"; \
174  break; \
175  case ANDNS_IPV6: \
176  __f="IPv6"; \
177  break; \
178  default: \
179  __f="UNKNOW"; \
180  break; \
181  } \
182  __f;})
183 #define MAX_INT_STR 10
184 #define SERVICE_STR(ap) \
185 ({ \
186  char *__g; \
187  char __t[MAX_INT_STR]; \
188  switch((ap)->qtype) { \
189  case AT_G: \
190  __g="*"; \
191  break; \
192  case AT_PTR: \
193  __g="None"; \
194  break; \
195  case AT_A: \
196  snprintf(__t,MAX_INT_STR,"%d", \
197  ap->service); \
198  __g=__t; \
199  break; \
200  default: \
201  __g="UNKNOW"; \
202  break; \
203  } \
204  __g;})
205 #define PROTO_STR(ap) \
206 ({ \
207  char *__h; \
208  switch((ap)->qtype) { \
209  case AT_G: \
210  __h="*"; \
211  break; \
212  case AT_PTR: \
213  __h="None"; \
214  break; \
215  case AT_A: \
216  if (!ap->service) \
217  __h="None"; \
218  else \
219  __h=ap->p==SNSD_PROTO_TCP? \
220  SNSD_PROTO_TCP_STR: \
221  SNSD_PROTO_UDP_STR; \
222  break; \
223  default: \
224  __h="UNKNOW"; \
225  break; \
226  } \
227  __h;})
228 
229 
230 #define GET_OPT_REALM (globopts.realm==REALM_NTK)?"NTK":"INET"
231 
232 /* CODE UTILS */
233 #define GOP (globopts)
234 #define AMISILENT (GOP.silent)
235 #define GQT (GOP.q)
236 
237 #define say printf
238 #define bye if (!AMISILENT) say("\tBye!\n");
239 
240 #define COMPUTE_TIME diff_time(time_start,time_stop)
241 #define time_report if (!AMISILENT){gettimeofday(&time_stop,NULL); \
242  say("Query time: %f seconds.\n" \
243  ,COMPUTE_TIME);}
244 
245 #define G_ALIGN(len) GQT->qstlength=len;GQT->qstdata=(char*) \
246  xmalloc(len+1); \
247  if (!GQT->qstdata){say("Fatal malloc!\n"); \
248  exit(1);}
249 #define G_SETQST_A(s) G_ALIGN(strlen(s)+1);strcpy(GQT->qstdata,s); \
250  GQT->qstlength=strlen(s);
251 
252 #define NTK_RESOLV_HASH_STR(s,d) \
253 ({ \
254  int __i; \
255  for (__i=0;__i<ANDNS_HASH_H;__i++) \
256  sprintf(d+2*__i,"%02x",((unsigned char*)(s))[__i]); \
257  d[2*ANDNS_HASH_H]=0;})
258 
259 #define NTK_RESOLV_STR_HASH(s,d) \
260 ({ \
261  int __i,__t; \
262  for (__i=0;__i<ANDNS_HASH_H;__i++) { \
263  sscanf(s+2*__i,"%02x",&__t); \
264  d[__i]=(unsigned char)(__t);}})
265 
266 #define NTK_RESOLV_IP_SYMBOL "~"
267 #define NTK_RESOLV_HNAME_SYMBOL "-"
268 #define NTK_RESOLV_SYMBOL(apd) (apd)->m&APD_IP?NTK_RESOLV_IP_SYMBOL: \
269  NTK_RESOLV_HNAME_SYMBOL
270 
271 /* FUNCTIONS */
272 
273 void version(void);
274 void usage(void);
275 void qt_usage(char *arg);
276 void realm_usage(char *arg);
277 void proto_usage(char *arg);
278 void service_and_proto_usage(char *arg);
279 double diff_time(struct timeval a,struct timeval b);
280 void opts_init(void);
281 void opts_set_silent(void);
282 void opts_set_port(char *arg);
283 void opts_set_ns(char *arg);
284 void opts_set_qt(char *arg);
285 void opts_set_realm(char *arg);
286 void opts_set_service_and_proto(char *arg);
287 void opts_set_proto(char *arg) ;
288 void opts_set_recursion(void);
289 void opts_set_hash(void) ;
290 void opts_set_compute_hash(void);
291 void opts_set_parsable_output(void);
292 void opts_set_question(char *arg);
293 void opts_finish(char *arg);
294 void print_headers();
295 void print_question();
296 void ip_bin_to_str(void *data,char *dst);
297 void answer_data_to_str(andns_pkt_data *apd,char *dst);
298 void print_answers();
299 void print_parsable_answers(void);
300 void print_results(void);
301 void do_command(void);
302 void ntkresolv_exit(int i);
303 void ntkresolv_safe_exit(int i);
304 int main(int argc, char **argv);
305 
306 #endif /* NTK_RESOLV_H */
#define QTYPE_G_STR
Definition: ntkresolv.h:41
struct ntkresolv_opts ntkresolv_opts
char * QTYPE_STR_LIST[]
Definition: ntkresolv.h:62
void answer_data_to_str(andns_pkt_data *apd, char *dst)
Definition: ntkresolv.c:394
void proto_usage(char *arg)
Definition: ntkresolv.c:79
#define MAX_HOSTNAME_LEN
Definition: ntkresolv.h:22
void qt_usage(char *arg)
Definition: ntkresolv.c:49
void opts_set_question(char *arg)
Definition: ntkresolv.c:255
void service_and_proto_usage(char *arg)
Definition: ntkresolv.c:95
Definition: ntkresolv.h:91
void opts_set_service_and_proto(char *arg)
Definition: ntkresolv.c:211
andns_pkt * q
Definition: ntkresolv.h:98
#define QTYPE_MX_STR
Definition: ntkresolv.h:42
void print_results(void)
Definition: ntkresolv.c:472
void opts_set_hash(void)
Definition: ntkresolv.c:242
void ntkresolv_safe_exit(int i)
Definition: ntkresolv.c:527
void usage(void)
Definition: netsukuku.c:155
void opts_set_silent(void)
Definition: ntkresolv.c:149
void opts_set_proto(char *arg)
Definition: ntkresolv.c:226
int QT_LEN
Definition: ntkresolv.h:63
#define NTKRESOLV_MAX_OBJ_LEN
Definition: ntkresolv.h:23
#define QTYPE_A_STR
Definition: ntkresolv.h:39
int main(int argc, char **argv)
Definition: netsukuku.c:699
int8_t silent
Definition: ntkresolv.h:94
void print_question()
Definition: ntkresolv.c:360
#define QTYPE_PTR_STR
Definition: ntkresolv.h:40
uint16_t id
Definition: ntkresolv.h:96
void opts_init(void)
Definition: ntkresolv.c:136
void print_headers()
Definition: ntkresolv.c:347
void print_parsable_answers(void)
Definition: ntkresolv.c:442
char nsserver[512]
Definition: ntkresolv.h:92
uint8_t hash
Definition: ntkresolv.h:97
void opts_set_compute_hash(void)
Definition: ntkresolv.c:246
void opts_set_ns(char *arg)
Definition: ntkresolv.c:169
void opts_set_port(char *arg)
Definition: ntkresolv.c:154
void print_answers()
Definition: ntkresolv.c:407
void opts_set_recursion(void)
Definition: ntkresolv.c:238
Definition: andns_lib.h:45
void version(void)
Definition: ntkresolv.c:16
void ntkresolv_exit(int i)
Definition: ntkresolv.c:523
double diff_time(struct timeval a, struct timeval b)
Definition: ntkresolv.c:122
void realm_usage(char *arg)
Definition: ntkresolv.c:66
int16_t port
Definition: ntkresolv.h:93
void ip_bin_to_str(void *data, char *dst)
Definition: ntkresolv.c:366
char obj[512]
Definition: ntkresolv.h:95
void opts_set_qt(char *arg)
Definition: ntkresolv.c:182
void opts_set_realm(char *arg)
Definition: ntkresolv.c:199
void opts_finish(char *arg)
Definition: ntkresolv.c:320
void opts_set_parsable_output(void)
Definition: ntkresolv.c:250
void do_command(void)
Definition: ntkresolv.c:484
Definition: andns_lib.h:64