You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

andns_lib.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 ANDNS_LIB_H
  21. #define ANDNS_LIB_H
  22. #include <string.h>
  23. #include <stdint.h>
  24. #include <sys/types.h>
  25. #define ANDNS_MAX_QUESTION_LEN 263 /* TODO */
  26. #define ANDNS_MAX_ANSWER_LEN 516
  27. #define ANDNS_MAX_ANSWERS_NUM 256
  28. #define ANDNS_MAX_PK_LEN ANDNS_MAX_QUESTION_LEN+\
  29. ANDNS_MAX_ANSWERS_NUM*ANDNS_MAX_ANSWER_LEN
  30. #define ANDNS_MAX_DATA_LEN 512
  31. #define ANDNS_MAX_QST_LEN 512
  32. #define ANNDS_DNS_MAZ_QST_LEN 255
  33. #define ANDNS_MAX_ANSW_IP_LEN 20
  34. #define ANDNS_MAX_ANSW_H_LEN 516
  35. #define ANDNS_HASH_H 16
  36. #define ANDNS_COMPR_LEVEL Z_BEST_COMPRESSION
  37. #define ANDNS_COMPR_THRESHOLD 1000
  38. struct andns_pkt_data {
  39. uint8_t m;
  40. uint8_t wg;
  41. uint8_t prio;
  42. uint16_t rdlength;
  43. uint16_t service;
  44. char *rdata;
  45. struct andns_pkt_data *next;
  46. };
  47. typedef struct andns_pkt_data andns_pkt_data;
  48. #define ANDNS_PKT_DATA_SZ sizeof(andns_pkt_data)
  49. #define APD_ALIGN(apd) (apd)->rdata=(char*)xmalloc((apd)->rdlength+1); \
  50. memset((apd)->rdata,0,(apd)->rdlength+1)
  51. #define APD_MAIN_IP 1<<0
  52. #define APD_IP 1<<1
  53. #define APD_TCP 1<<2
  54. #define APD_UDP 1<<3
  55. typedef struct andns_pkt {
  56. uint16_t id;
  57. uint8_t r;
  58. uint8_t qr;
  59. uint8_t p;
  60. uint8_t z;
  61. uint8_t qtype;
  62. uint16_t ancount;
  63. uint8_t ipv;
  64. uint8_t nk;
  65. uint8_t rcode;
  66. uint16_t service;
  67. uint16_t qstlength;
  68. char *qstdata;
  69. andns_pkt_data *pkt_answ;
  70. } andns_pkt;
  71. #define ANDNS_PKT_SZ sizeof(andns_pkt)
  72. #define AP_ALIGN(ap) (ap)->qstdata=(char*)xmalloc((ap)->qstlength)
  73. #define ANDNS_HDR_SZ 4
  74. #define ANDNS_HDR_Z 4
  75. #define ANDNS_MAX_SZ ANDNS_HDR_SZ+ANDNS_MAX_QST_LEN+ANDNS_MAX_QST_LEN+4
  76. #define ANDNS_SET_RCODE(s,c) *((s)+3)=(((*((s)+3))&0xf0)|c)
  77. #define ANDNS_SET_QR(s) (*((s)+2))|=0x80
  78. #define ANDNS_SET_ANCOUNT(s,n) *(s+2)|=((n)>>1);*(s+3)|=((n)<<7);
  79. #define ANDNS_SET_Z(s) *(s+3)|=0x20;
  80. #define ANDNS_UNSET_Z(s) *(s+3)&=0xdf;
  81. /* ANDNS PROTO-TYPE */
  82. #define ANDNS_PROTO_TCP 0
  83. #define ANDNS_PROTO_UDP 1
  84. /* ANDNS QUERY-TYPE */
  85. #define AT_A 0 /* h->ip */
  86. #define AT_PTR 1 /* ip->h */
  87. #define AT_G 2 /* global */
  88. /* RCODES: The rcodes are portable between ANDNS and DNS */
  89. #define ANDNS_RCODE_NOERR 0 /* No error */
  90. #define ANDNS_RCODE_EINTRPRT 1 /* Intepret error */
  91. #define ANDNS_RCODE_ESRVFAIL 2 /* Server failure */
  92. #define ANDNS_RCODE_ENSDMN 3 /* No such domain */
  93. #define ANDNS_RCODE_ENIMPL 4 /* Not implemented */
  94. #define ANDNS_RCODE_ERFSD 5 /* Refused */
  95. /* REALMS TO SEARCH */
  96. #define NTK_REALM 1
  97. #define INET_REALM 2
  98. /* IP VERSION */
  99. #define ANDNS_IPV4 0
  100. #define ANDNS_IPV6 1
  101. int andns_compress(char *src, int srclen);
  102. char *andns_uncompress(char *src, int srclen, int *dstlen);
  103. int a_hdr_u(char *buf, andns_pkt * ap);
  104. int a_qst_u(char *buf, andns_pkt * ap, int limitlen);
  105. int a_answ_u(char *buf, andns_pkt * ap, int limitlen);
  106. int a_answs_u(char *buf, andns_pkt * ap, int limitlen);
  107. int a_u(char *buf, int pktlen, andns_pkt ** app);
  108. int a_hdr_p(andns_pkt * ap, char *buf);
  109. int a_qst_p(andns_pkt * ap, char *buf, int limitlen);
  110. int a_answ_p(andns_pkt * ap, andns_pkt_data * apd, char *buf,
  111. int limitlen);
  112. int a_answs_p(andns_pkt * ap, char *buf, int limitlen);
  113. int a_p(andns_pkt * ap, char *buf);
  114. andns_pkt *create_andns_pkt(void);
  115. andns_pkt_data *create_andns_pkt_data(void);
  116. andns_pkt_data *andns_add_answ(andns_pkt * ap);
  117. void destroy_andns_pkt_data(andns_pkt_data * apd);
  118. void andns_del_answ(andns_pkt * ap);
  119. void destroy_andns_pkt_datas(andns_pkt * ap);
  120. void destroy_andns_pkt(andns_pkt * ap);
  121. #endif /* ANDNS_LIB_H */