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_snsd.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /**************************************
  2. * AUTHOR: Federico Tomassini *
  3. * Copyright (C) Federico Tomassini *
  4. * Contact effetom@gmail.com *
  5. ***********************************************
  6. ******* BEGIN 4/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. #include "includes.h"
  21. #include "llist.c"
  22. #include "andns_snsd.h"
  23. #include "err_errno.h"
  24. #include "andna.h"
  25. #include "log.h"
  26. /* h2ip functions */
  27. /*
  28. * Given a a hostname hash, makes a resolution
  29. * call (service=0) and search the main ip entry,
  30. * storing it to snsd_node dst.
  31. *
  32. * Returns:
  33. * 0
  34. * -1
  35. */
  36. int
  37. snsd_main_ip(u_int * hname_hash, snsd_node * dst)
  38. {
  39. snsd_service *ss;
  40. snsd_prio *sp;
  41. snsd_node *sn;
  42. int records;
  43. ss = andna_resolve_hash(hname_hash, 0, 0, &records);
  44. if (!ss)
  45. err_ret(ERR_SNDMRF, -1);
  46. if (!(sp = ss->prio)) {
  47. goto destroy_return;
  48. }
  49. list_for(sp) {
  50. sn = sp->node;
  51. list_for(sn)
  52. if (sn->flags & SNSD_NODE_MAIN_IP) {
  53. memcpy(dst, sn, sizeof(snsd_node));
  54. snsd_service_llist_del(&ss);
  55. return 0;
  56. }
  57. }
  58. goto destroy_return;
  59. destroy_return:
  60. snsd_service_llist_del(&ss);
  61. err_ret(ERR_SNDMRF, -1);
  62. }
  63. /*
  64. * Convert a snsd_node to a binary ip.
  65. * If snsd_node does not contain a ip, but a hostname hash,
  66. * calls another resolution with service=0.
  67. *
  68. * Returns:
  69. * bytes writed
  70. *
  71. */
  72. int
  73. snsd_node_to_data(char *buf, snsd_node * sn, u_char prio, int iplen,
  74. int recursion)
  75. {
  76. int res;
  77. int family;
  78. if (recursion != -1) {
  79. *buf |= sn->weight & 0x3f;
  80. *(buf + 1) |= prio;
  81. }
  82. if (!(sn->flags & SNSD_NODE_HNAME)) {
  83. *buf |= 0x40;
  84. if (sn->flags & SNSD_NODE_MAIN_IP)
  85. *buf |= 0x80;
  86. memcpy(buf + 2, sn->record, iplen);
  87. family = (iplen == 4) ? AF_INET : AF_INET6;
  88. inet_htonl((u_int *) (buf + 2), family);
  89. return iplen + 2;
  90. } else if (recursion) {
  91. snsd_node snt;
  92. res = snsd_main_ip((u_int*)sn->record, &snt);
  93. if (!res) { /* I love recursion */
  94. res = snsd_node_to_data(buf, &snt, prio, iplen, -1);
  95. return res;
  96. }
  97. }
  98. memcpy(buf + 2, sn->record, ANDNS_HASH_H);
  99. return ANDNS_HASH_H + 2;
  100. }
  101. /*
  102. * Converts a snsd_node struct to andns data.
  103. * data means a packed answer.
  104. * buf has to be ANDNS_MAX_ANSW_IP_LEN long.
  105. *
  106. * returns -1 on error, answer len otherwise.
  107. *
  108. * O B S O L E T E
  109. size_t snsd_node_to_aansw(char *buf,snsd_node *sn,u_char prio,int iplen)
  110. {
  111. int res;
  112. res=snsd_node_to_data(buf+2,sn,iplen);
  113. if (res==-1) {
  114. error(err_str);
  115. return -1;
  116. }
  117. if (sn->flags & SNSD_NODE_MAIN_IP)
  118. *buf|=0x80;
  119. *buf++=sn->weight;
  120. *buf=prio;
  121. return 0;
  122. }
  123. */
  124. /*
  125. * Converts a snsd_prio list to andns data.
  126. * data means a set of contiguous answers ready
  127. * to be sent.
  128. *
  129. * Returns the number of bytes writed to buf.
  130. * The size is computable with iplen.
  131. *
  132. * buf has to be long enough, ie, you have to count node
  133. * in prio list and take ANDNS_MAX_ANSW_IP_LEN * n space.
  134. *
  135. */
  136. int
  137. snsd_prio_to_aansws(char *buf, snsd_prio * sp, int iplen, int recursion,
  138. int *count)
  139. {
  140. int res = 0;
  141. snsd_node *sn;
  142. int c = 0;
  143. if (!sp || !buf)
  144. return 0;
  145. sn = sp->node;
  146. list_for(sn) {
  147. res += snsd_node_to_data(buf + res, sn, sp->prio,
  148. iplen, recursion);
  149. c++;
  150. }
  151. *count = c;
  152. return res;
  153. }
  154. int
  155. snsd_service_to_aansws(char *buf, snsd_service * ss, int iplen, int *count,
  156. int recursion)
  157. {
  158. int family, c = 0;
  159. uint16_t service;
  160. uint8_t prio, proto;
  161. snsd_prio *sp;
  162. snsd_node *sn;
  163. char *rem;
  164. snsd_node snt;
  165. if (!ss || !buf)
  166. return 0;
  167. rem = buf;
  168. list_for(ss) {
  169. service = htons(ss->service);
  170. proto = ss->proto;
  171. sp = ss->prio;
  172. list_for(sp) {
  173. prio = sp->prio;
  174. sn = sp->node;
  175. list_for(sn) {
  176. if (sn->flags & SNSD_NODE_MAIN_IP)
  177. (*buf) |= 0xc0;
  178. else if (sn->flags & SNSD_NODE_IP)
  179. (*buf) |= 0x40;
  180. if (proto == ANDNS_SNSD_PROTO_UDP)
  181. (*buf) |= 0x20;
  182. *buf++ |= (sn->weight & 0x1f);
  183. *buf++ |= prio;
  184. memcpy(buf, &service, 2);
  185. buf += 2;
  186. if (sn->flags & SNSD_NODE_MAIN_IP ||
  187. sn->flags & SNSD_NODE_IP) {
  188. memcpy(buf, sn->record, iplen);
  189. family = (iplen == 4) ? AF_INET : AF_INET6;
  190. inet_htonl((u_int *) buf, family);
  191. buf += iplen;
  192. } else {
  193. if (recursion && !snsd_main_ip((u_int*)sn->record, &snt)) {
  194. memcpy(buf, snt.record, iplen);
  195. *(buf - 4) |= 0x40;
  196. family = (iplen == 4) ? AF_INET : AF_INET6;
  197. inet_htonl((u_int *) buf, family);
  198. buf += iplen;
  199. } else {
  200. memcpy(buf, sn->record, ANDNS_HASH_H);
  201. buf += ANDNS_HASH_H;
  202. }
  203. /* service=strlen((char*)sn->record);
  204. temp=htons(service);
  205. memcpy(buf,&temp,2);
  206. memcpy(buf+2,sn->record,service);
  207. buf+=ANDNS_HASH_H;
  208. res=snsd_main_ip(sn->record,&snt);
  209. if (res) {
  210. buf-=4;
  211. continue;
  212. }
  213. memcpy(buf,snt.record,iplen);
  214. family=(iplen==4)?AF_INET:AF_INET6;
  215. inet_htonl((u_int*)buf,family);
  216. buf+=iplen; */
  217. }
  218. c++;
  219. }
  220. }
  221. }
  222. *count = c;
  223. return (int) (buf - rem);
  224. }
  225. /*
  226. * Given a dns_packet, this function add an answer to it
  227. * and returns 0;
  228. * Otherwise returns -1.
  229. */
  230. int
  231. snsd_node_to_dansw(dns_pkt * dp, snsd_node * sn, int iplen)
  232. {
  233. char temp[18];
  234. dns_pkt_a *dpa;
  235. snsd_node snt;
  236. int res;
  237. if (!(sn->flags & SNSD_NODE_HNAME)) {
  238. if (!(res = snsd_main_ip((u_int*)sn->record, &snt))) {
  239. return -1;
  240. }
  241. }
  242. memcpy(temp, sn->record, iplen);
  243. inet_htonl((u_int *) (temp), (iplen == 4) ? AF_INET : AF_INET6);
  244. dpa = DP_ADD_ANSWER(dp);
  245. dns_a_default_fill(dp, dpa);
  246. dpa->rdlength = iplen;
  247. memcpy(dpa->rdata, temp, iplen);
  248. return 0;
  249. }
  250. /*
  251. * Converts a snsd_prio struct, adding a set of answers to
  252. * the dns_packet dp.
  253. * Returns the number of answers added to dp.
  254. */
  255. int
  256. snsd_prio_to_dansws(dns_pkt * dp, snsd_prio * sp, int iplen)
  257. {
  258. int res = 0;
  259. snsd_node *sn;
  260. sn = sp->node;
  261. list_for(sn)
  262. if (!snsd_node_to_dansw(dp, sn, iplen))
  263. res++;
  264. return res;
  265. }
  266. /* ip2h functions */
  267. /*
  268. * Converts a lcl_cache struct to a set of dns answers.
  269. * Returns the number of answers added.
  270. */
  271. int
  272. lcl_cache_to_dansws(dns_pkt * dp, lcl_cache * lc)
  273. {
  274. dns_pkt_a *dpa;
  275. int res = 0;
  276. list_for(lc) {
  277. dpa = DP_ADD_ANSWER(dp);
  278. dns_a_default_fill(dp, dpa);
  279. strcpy(dpa->rdata, lc->hostname);
  280. res++;
  281. }
  282. if (lc)
  283. lcl_cache_free(lc);
  284. return res;
  285. }
  286. /*
  287. * Converts a lcl_cache to andns data.
  288. * Returns the number of bytes writed.
  289. */
  290. size_t
  291. lcl_cache_to_aansws(char *buf, lcl_cache * lc, int *count)
  292. {
  293. uint16_t slen;
  294. size_t ret = 0;
  295. int lcount = 0;
  296. lcl_cache *lcl = lc;
  297. list_for(lcl) {
  298. slen = strlen(lc->hostname);
  299. ret += 2 + slen;
  300. slen = htons(slen);
  301. memcpy(buf, &slen, 2);
  302. buf += 2;
  303. strcpy(buf, lc->hostname);
  304. lcount++;
  305. }
  306. *count = lcount;
  307. lcl_cache_free(lc);
  308. return ret;
  309. }