gnunet-service-gns_resolver.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2011-2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file gns/gnunet-service-gns_resolver.c
  19. * @brief GNU Name System resolver logic
  20. * @author Martin Schanzenbach
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_dnsstub_lib.h"
  26. #include "gnunet_dht_service.h"
  27. #include "gnunet_gnsrecord_lib.h"
  28. #include "gnunet_namecache_service.h"
  29. #include "gnunet_namestore_service.h"
  30. #include "gnunet_dns_service.h"
  31. #include "gnunet_resolver_service.h"
  32. #include "gnunet_revocation_service.h"
  33. #include "gnunet_dnsparser_lib.h"
  34. #include "gnunet_tun_lib.h"
  35. #include "gnunet_gns_service.h"
  36. #include "gns.h"
  37. #include "gnunet-service-gns_resolver.h"
  38. #include "gnunet-service-gns_shorten.h"
  39. #include "gnunet_vpn_service.h"
  40. /**
  41. * Default DHT timeout for lookups.
  42. */
  43. #define DHT_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  44. /**
  45. * Default timeout for DNS lookups.
  46. */
  47. #define DNS_LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
  48. /**
  49. * Default timeout for VPN redirections.
  50. */
  51. #define VPN_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 30)
  52. /**
  53. * DHT replication level
  54. */
  55. #define DHT_GNS_REPLICATION_LEVEL 5
  56. /**
  57. * How deep do we allow recursions to go before we abort?
  58. */
  59. #define MAX_RECURSION 256
  60. /**
  61. * DLL to hold the authority chain we had to pass in the resolution
  62. * process.
  63. */
  64. struct AuthorityChain
  65. {
  66. /**
  67. * This is a DLL.
  68. */
  69. struct AuthorityChain *prev;
  70. /**
  71. * This is a DLL.
  72. */
  73. struct AuthorityChain *next;
  74. /**
  75. * Resolver handle this entry in the chain belongs to.
  76. */
  77. struct GNS_ResolverHandle *rh;
  78. /**
  79. * label/name corresponding to the authority
  80. */
  81. char *label;
  82. /**
  83. * label/name suggested for shortening to the authority
  84. */
  85. char *suggested_shortening_label;
  86. /**
  87. * Do we already try to shorten this authority?
  88. */
  89. int shortening_started;
  90. /**
  91. * #GNUNET_YES if the authority was a GNS authority,
  92. * #GNUNET_NO if the authority was a DNS authority.
  93. */
  94. int gns_authority;
  95. /**
  96. * Information about the resolver authority for this label.
  97. */
  98. union
  99. {
  100. /**
  101. * The zone of the GNS authority
  102. */
  103. struct GNUNET_CRYPTO_EcdsaPublicKey gns_authority;
  104. struct
  105. {
  106. /**
  107. * Domain of the DNS resolver that is the authority.
  108. * (appended to construct the DNS name to resolve;
  109. * this is NOT the DNS name of the DNS server!).
  110. */
  111. char name[GNUNET_DNSPARSER_MAX_NAME_LENGTH + 1];
  112. /**
  113. * IP address of the DNS resolver that is authoritative.
  114. * (this implementation currently only supports one
  115. * IP at a time).
  116. */
  117. struct sockaddr_storage dns_ip;
  118. } dns_authority;
  119. } authority_info;
  120. };
  121. /**
  122. * A result we got from DNS.
  123. */
  124. struct DnsResult
  125. {
  126. /**
  127. * Kept in DLL.
  128. */
  129. struct DnsResult *next;
  130. /**
  131. * Kept in DLL.
  132. */
  133. struct DnsResult *prev;
  134. /**
  135. * Binary value stored in the DNS record (appended to this struct)
  136. */
  137. const void *data;
  138. /**
  139. * Expiration time for the DNS record, 0 if we didn't
  140. * get anything useful (i.e. 'gethostbyname' was used).
  141. */
  142. uint64_t expiration_time;
  143. /**
  144. * Number of bytes in @e data.
  145. */
  146. size_t data_size;
  147. /**
  148. * Type of the GNS/DNS record.
  149. */
  150. uint32_t record_type;
  151. };
  152. /**
  153. * Closure for #vpn_allocation_cb.
  154. */
  155. struct VpnContext
  156. {
  157. /**
  158. * Which resolution process are we processing.
  159. */
  160. struct GNS_ResolverHandle *rh;
  161. /**
  162. * Handle to the VPN request that we were performing.
  163. */
  164. struct GNUNET_VPN_RedirectionRequest *vpn_request;
  165. /**
  166. * Number of records serialized in @e rd_data.
  167. */
  168. unsigned int rd_count;
  169. /**
  170. * Serialized records.
  171. */
  172. char *rd_data;
  173. /**
  174. * Number of bytes in @e rd_data.
  175. */
  176. size_t rd_data_size;
  177. };
  178. /**
  179. * Information we keep during the resolution of an
  180. * IP address for a DNS server while handling a
  181. * GNS2DNS record.
  182. */
  183. struct Gns2DnsContext
  184. {
  185. /**
  186. * DNS domain in which the resolution will continue
  187. * (first part of the GNS2DNS record).
  188. */
  189. char *ns;
  190. /**
  191. * Handle for the resolution of the IP part of the
  192. * GNS2DNS record. Will return to us the addresses
  193. * of the DNS resolver to use.
  194. */
  195. struct GNS_ResolverHandle *rh;
  196. };
  197. /**
  198. * Handle to a currenty pending resolution. On result (positive or
  199. * negative) the #GNS_ResultProcessor is called.
  200. */
  201. struct GNS_ResolverHandle
  202. {
  203. /**
  204. * DLL
  205. */
  206. struct GNS_ResolverHandle *next;
  207. /**
  208. * DLL
  209. */
  210. struct GNS_ResolverHandle *prev;
  211. /**
  212. * The top-level GNS authoritative zone to query
  213. */
  214. struct GNUNET_CRYPTO_EcdsaPublicKey authority_zone;
  215. /**
  216. * called when resolution phase finishes
  217. */
  218. GNS_ResultProcessor proc;
  219. /**
  220. * closure passed to @e proc
  221. */
  222. void* proc_cls;
  223. /**
  224. * Handle used during GNS2DNS resolution for looking up the
  225. * IP address of the DNS server.
  226. */
  227. struct Gns2DnsContext *g2dc;
  228. /**
  229. * Handle for DHT lookups. should be NULL if no lookups are in progress
  230. */
  231. struct GNUNET_DHT_GetHandle *get_handle;
  232. /**
  233. * Handle to a VPN request, NULL if none is active.
  234. */
  235. struct VpnContext *vpn_ctx;
  236. /**
  237. * Socket for a DNS request, NULL if none is active.
  238. */
  239. struct GNUNET_DNSSTUB_RequestSocket *dns_request;
  240. /**
  241. * Handle for standard DNS resolution, NULL if none is active.
  242. */
  243. struct GNUNET_RESOLVER_RequestHandle *std_resolve;
  244. /**
  245. * Pending Namecache lookup task
  246. */
  247. struct GNUNET_NAMECACHE_QueueEntry *namecache_qe;
  248. /**
  249. * Pending revocation check.
  250. */
  251. struct GNUNET_REVOCATION_Query *rev_check;
  252. /**
  253. * Heap node associated with this lookup. Used to limit number of
  254. * concurrent requests.
  255. */
  256. struct GNUNET_CONTAINER_HeapNode *dht_heap_node;
  257. /**
  258. * DLL to store the authority chain
  259. */
  260. struct AuthorityChain *ac_head;
  261. /**
  262. * DLL to store the authority chain
  263. */
  264. struct AuthorityChain *ac_tail;
  265. /**
  266. * Private key of the shorten zone, NULL to not shorten.
  267. */
  268. struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key;
  269. /**
  270. * ID of a task associated with the resolution process.
  271. */
  272. GNUNET_SCHEDULER_TaskIdentifier task_id;
  273. /**
  274. * The name to resolve
  275. */
  276. char *name;
  277. /**
  278. * DLL of results we got from DNS.
  279. */
  280. struct DnsResult *dns_result_head;
  281. /**
  282. * DLL of results we got from DNS.
  283. */
  284. struct DnsResult *dns_result_tail;
  285. /**
  286. * Current offset in 'name' where we are resolving.
  287. */
  288. size_t name_resolution_pos;
  289. /**
  290. * Use only cache
  291. */
  292. enum GNUNET_GNS_LocalOptions options;
  293. /**
  294. * For SRV and TLSA records, the number of the
  295. * protocol specified in the name. 0 if no protocol was given.
  296. */
  297. int protocol;
  298. /**
  299. * For SRV and TLSA records, the number of the
  300. * service specified in the name. 0 if no service was given.
  301. */
  302. int service;
  303. /**
  304. * Desired type for the resolution.
  305. */
  306. int record_type;
  307. /**
  308. * We increment the loop limiter for each step in a recursive
  309. * resolution. If it passes our threshold (i.e. due to
  310. * self-recursion in the resolution, i.e CNAME fun), we stop.
  311. */
  312. unsigned int loop_limiter;
  313. };
  314. /**
  315. * Active namestore caching operations.
  316. */
  317. struct CacheOps
  318. {
  319. /**
  320. * Organized in a DLL.
  321. */
  322. struct CacheOps *next;
  323. /**
  324. * Organized in a DLL.
  325. */
  326. struct CacheOps *prev;
  327. /**
  328. * Pending Namestore caching task.
  329. */
  330. struct GNUNET_NAMECACHE_QueueEntry *namecache_qe_cache;
  331. };
  332. /**
  333. * Our handle to the namecache service
  334. */
  335. static struct GNUNET_NAMECACHE_Handle *namecache_handle;
  336. /**
  337. * Our handle to the vpn service
  338. */
  339. static struct GNUNET_VPN_Handle *vpn_handle;
  340. /**
  341. * Resolver handle to the dht
  342. */
  343. static struct GNUNET_DHT_Handle *dht_handle;
  344. /**
  345. * Handle to perform DNS lookups.
  346. */
  347. static struct GNUNET_DNSSTUB_Context *dns_handle;
  348. /**
  349. * Heap for limiting parallel DHT lookups
  350. */
  351. static struct GNUNET_CONTAINER_Heap *dht_lookup_heap;
  352. /**
  353. * Maximum amount of parallel queries to the DHT
  354. */
  355. static unsigned long long max_allowed_background_queries;
  356. /**
  357. * Head of resolver lookup list
  358. */
  359. static struct GNS_ResolverHandle *rlh_head;
  360. /**
  361. * Tail of resolver lookup list
  362. */
  363. static struct GNS_ResolverHandle *rlh_tail;
  364. /**
  365. * Organized in a DLL.
  366. */
  367. static struct CacheOps *co_head;
  368. /**
  369. * Organized in a DLL.
  370. */
  371. static struct CacheOps *co_tail;
  372. /**
  373. * Use namecache
  374. */
  375. static int use_cache;
  376. /**
  377. * Global configuration.
  378. */
  379. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  380. /**
  381. * Determine if this name is canonical (is a legal name in a zone, without delegation);
  382. * note that we do not test that the name does not contain illegal characters, we only
  383. * test for delegation. Note that service records (i.e. _foo._srv) are canonical names
  384. * even though they consist of multiple labels.
  385. *
  386. * Examples:
  387. * a.b.gnu = not canonical
  388. * a = canonical
  389. * _foo._srv = canonical
  390. * _f.bar = not canonical
  391. *
  392. * @param name the name to test
  393. * @return #GNUNET_YES if canonical
  394. */
  395. static int
  396. is_canonical (const char *name)
  397. {
  398. const char *pos;
  399. const char *dot;
  400. if (NULL == strchr (name, '.'))
  401. return GNUNET_YES;
  402. if ('_' != name[0])
  403. return GNUNET_NO;
  404. pos = &name[1];
  405. while (NULL != (dot = strchr (pos, '.')))
  406. if ('_' != dot[1])
  407. return GNUNET_NO;
  408. else
  409. pos = dot + 1;
  410. return GNUNET_YES;
  411. }
  412. /* ************************** Resolution **************************** */
  413. /**
  414. * Expands a name ending in .+ with the zone of origin.
  415. *
  416. * @param rh resolution context
  417. * @param name name to modify (to be free'd or returned)
  418. * @return updated name
  419. */
  420. static char *
  421. translate_dot_plus (struct GNS_ResolverHandle *rh,
  422. char *name)
  423. {
  424. char *ret;
  425. size_t s_len = strlen (name);
  426. if (0 != strcmp (&name[s_len - 2],
  427. ".+"))
  428. return name; /* did not end in ".+" */
  429. GNUNET_assert (GNUNET_YES == rh->ac_tail->gns_authority);
  430. GNUNET_asprintf (&ret,
  431. "%.*s.%s",
  432. (int) (s_len - 2),
  433. name,
  434. GNUNET_GNSRECORD_pkey_to_zkey (&rh->ac_tail->authority_info.gns_authority));
  435. GNUNET_free (name);
  436. return ret;
  437. }
  438. /**
  439. * Task scheduled to asynchronously fail a resolution.
  440. *
  441. * @param cls the 'struct GNS_ResolverHandle' of the resolution to fail
  442. * @param tc task context
  443. */
  444. static void
  445. fail_resolution (void *cls,
  446. const struct GNUNET_SCHEDULER_TaskContext *tc)
  447. {
  448. struct GNS_ResolverHandle *rh = cls;
  449. rh->task_id = GNUNET_SCHEDULER_NO_TASK;
  450. rh->proc (rh->proc_cls, 0, NULL);
  451. GNS_resolver_lookup_cancel (rh);
  452. }
  453. #if (defined WINDOWS) || (defined DARWIN)
  454. /* Don't have this on W32, here's a naive implementation
  455. * Was somehow removed on OS X ... */
  456. static void *
  457. memrchr (const void *s,
  458. int c,
  459. size_t n)
  460. {
  461. const unsigned char *ucs = s;
  462. ssize_t i;
  463. for (i = n - 1; i >= 0; i--)
  464. if (c == (int) ucs[i])
  465. return (void *) &ucs[i];
  466. return NULL;
  467. }
  468. #endif
  469. /**
  470. * Get the next, rightmost label from the name that we are trying to resolve,
  471. * and update the resolution position accordingly. Labels usually consist
  472. * of up to 63 characters without a period ("."); however, we use a special
  473. * convention to support SRV and TLSA records where the domain name
  474. * includes an encoding for a service and protocol in the name. The
  475. * syntax (see RFC 2782) here is "_Service._Proto.Name" and in this
  476. * special case we include the "_Service._Proto" in the rightmost label.
  477. * Thus, for "_443._tcp.foo.bar" we first return the label "bar" and then
  478. * the label "_443._tcp.foo". The special case is detected by the
  479. * presence of labels beginning with an underscore. Whenever a label
  480. * begins with an underscore, it is combined with the label to its right
  481. * (and the "." is preserved).
  482. *
  483. * @param rh handle to the resolution operation to get the next label from
  484. * @return NULL if there are no more labels
  485. */
  486. static char *
  487. resolver_lookup_get_next_label (struct GNS_ResolverHandle *rh)
  488. {
  489. const char *rp;
  490. const char *dot;
  491. size_t len;
  492. char *ret;
  493. char *srv_name;
  494. char *proto_name;
  495. struct protoent *pe;
  496. struct servent *se;
  497. if (0 == rh->name_resolution_pos)
  498. return NULL;
  499. dot = memrchr (rh->name,
  500. (int) '.',
  501. rh->name_resolution_pos);
  502. if (NULL == dot)
  503. {
  504. /* done, this was the last one */
  505. len = rh->name_resolution_pos;
  506. rp = rh->name;
  507. rh->name_resolution_pos = 0;
  508. }
  509. else
  510. {
  511. /* advance by one label */
  512. len = rh->name_resolution_pos - (dot - rh->name) - 1;
  513. rp = dot + 1;
  514. rh->name_resolution_pos = dot - rh->name;
  515. }
  516. rh->protocol = 0;
  517. rh->service = 0;
  518. ret = GNUNET_strndup (rp, len);
  519. /* If we have labels starting with underscore with label on
  520. * the right (SRV/DANE/BOX case), determine port/protocol;
  521. * The format of `rh->name` must be "_PORT._PROTOCOL".
  522. */
  523. if ( ('_' == rh->name[0]) &&
  524. (NULL != (dot = memrchr (rh->name,
  525. (int) '.',
  526. rh->name_resolution_pos))) &&
  527. ('_' == dot[1]) &&
  528. (NULL == memrchr (rh->name,
  529. (int) '.',
  530. dot - rh->name)) )
  531. {
  532. srv_name = GNUNET_strndup (&rh->name[1],
  533. (dot - rh->name) - 1);
  534. proto_name = GNUNET_strndup (&dot[2],
  535. rh->name_resolution_pos - (dot - rh->name) - 1);
  536. rh->name_resolution_pos = 0;
  537. pe = getprotobyname (proto_name);
  538. if (NULL == pe)
  539. {
  540. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  541. _("Protocol `%s' unknown, skipping labels.\n"),
  542. proto_name);
  543. GNUNET_free (proto_name);
  544. GNUNET_free (srv_name);
  545. return ret;
  546. }
  547. se = getservbyname (srv_name,
  548. proto_name);
  549. if (NULL == se)
  550. {
  551. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  552. _("Service `%s' unknown for protocol `%s', skipping labels.\n"),
  553. srv_name,
  554. proto_name);
  555. GNUNET_free (proto_name);
  556. GNUNET_free (srv_name);
  557. return ret;
  558. }
  559. rh->protocol = pe->p_proto;
  560. rh->service = se->s_port;
  561. }
  562. return ret;
  563. }
  564. /**
  565. * Gives the cummulative result obtained to the callback and clean up the request.
  566. *
  567. * @param rh resolution process that has culminated in a result
  568. */
  569. static void
  570. transmit_lookup_dns_result (struct GNS_ResolverHandle *rh)
  571. {
  572. struct DnsResult *pos;
  573. unsigned int n;
  574. unsigned int i;
  575. n = 0;
  576. for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
  577. n++;
  578. {
  579. struct GNUNET_GNSRECORD_Data rd[n];
  580. i = 0;
  581. for (pos = rh->dns_result_head; NULL != pos; pos = pos->next)
  582. {
  583. rd[i].data = pos->data;
  584. rd[i].data_size = pos->data_size;
  585. rd[i].record_type = pos->record_type;
  586. if (0 == pos->expiration_time)
  587. {
  588. rd[i].flags = GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
  589. rd[i].expiration_time = 0;
  590. }
  591. else
  592. {
  593. rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
  594. rd[i].expiration_time = pos->expiration_time;
  595. }
  596. i++;
  597. }
  598. GNUNET_assert (i == n);
  599. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  600. "Transmitting standard DNS result with %u records\n",
  601. n);
  602. rh->proc (rh->proc_cls,
  603. n,
  604. rd);
  605. }
  606. GNS_resolver_lookup_cancel (rh);
  607. }
  608. /**
  609. * Add a result from DNS to the records to be returned to the application.
  610. *
  611. * @param rh resolution request to extend with a result
  612. * @param expiration_time expiration time for the answer
  613. * @param record_type DNS record type of the answer
  614. * @param data_size number of bytes in @a data
  615. * @param data binary data to return in DNS record
  616. */
  617. static void
  618. add_dns_result (struct GNS_ResolverHandle *rh,
  619. uint64_t expiration_time,
  620. uint32_t record_type,
  621. size_t data_size,
  622. const void *data)
  623. {
  624. struct DnsResult *res;
  625. res = GNUNET_malloc (sizeof (struct DnsResult) + data_size);
  626. res->expiration_time = expiration_time;
  627. res->data_size = data_size;
  628. res->record_type = record_type;
  629. res->data = &res[1];
  630. memcpy (&res[1], data, data_size);
  631. GNUNET_CONTAINER_DLL_insert (rh->dns_result_head,
  632. rh->dns_result_tail,
  633. res);
  634. }
  635. /**
  636. * We had to do a DNS lookup. Convert the result (if any) and return
  637. * it.
  638. *
  639. * @param cls closure with the `struct GNS_ResolverHandle`
  640. * @param addr one of the addresses of the host, NULL for the last address
  641. * @param addrlen length of the address
  642. */
  643. static void
  644. handle_dns_result (void *cls,
  645. const struct sockaddr *addr,
  646. socklen_t addrlen)
  647. {
  648. struct GNS_ResolverHandle *rh = cls;
  649. const struct sockaddr_in *sa4;
  650. const struct sockaddr_in6 *sa6;
  651. if (NULL == addr)
  652. {
  653. rh->std_resolve = NULL;
  654. transmit_lookup_dns_result (rh);
  655. return;
  656. }
  657. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  658. "Received %u bytes of DNS IP data\n",
  659. addrlen);
  660. switch (addr->sa_family)
  661. {
  662. case AF_INET:
  663. sa4 = (const struct sockaddr_in *) addr;
  664. add_dns_result (rh,
  665. 0 /* expiration time is unknown */,
  666. GNUNET_DNSPARSER_TYPE_A,
  667. sizeof (struct in_addr),
  668. &sa4->sin_addr);
  669. break;
  670. case AF_INET6:
  671. sa6 = (const struct sockaddr_in6 *) addr;
  672. add_dns_result (rh,
  673. 0 /* expiration time is unknown */,
  674. GNUNET_DNSPARSER_TYPE_AAAA,
  675. sizeof (struct in6_addr),
  676. &sa6->sin6_addr);
  677. break;
  678. default:
  679. GNUNET_break (0);
  680. break;
  681. }
  682. }
  683. /**
  684. * Task scheduled to continue with the resolution process.
  685. *
  686. * @param cls the 'struct GNS_ResolverHandle' of the resolution
  687. * @param tc task context
  688. */
  689. static void
  690. recursive_resolution (void *cls,
  691. const struct GNUNET_SCHEDULER_TaskContext *tc);
  692. /**
  693. * Begin the resolution process from 'name', starting with
  694. * the identification of the zone specified by 'name'.
  695. *
  696. * @param rh resolution to perform
  697. */
  698. static void
  699. start_resolver_lookup (struct GNS_ResolverHandle *rh);
  700. /**
  701. * Function called with the result of a DNS resolution.
  702. *
  703. * @param cls the request handle of the resolution that
  704. * we were attempting to make
  705. * @param rs socket that received the response
  706. * @param dns dns response, never NULL
  707. * @param dns_len number of bytes in @a dns
  708. */
  709. static void
  710. dns_result_parser (void *cls,
  711. struct GNUNET_DNSSTUB_RequestSocket *rs,
  712. const struct GNUNET_TUN_DnsHeader *dns,
  713. size_t dns_len)
  714. {
  715. struct GNS_ResolverHandle *rh = cls;
  716. struct GNUNET_DNSPARSER_Packet *p;
  717. const struct GNUNET_DNSPARSER_Record *rec;
  718. unsigned int rd_count;
  719. unsigned int i;
  720. rh->dns_request = NULL;
  721. GNUNET_SCHEDULER_cancel (rh->task_id);
  722. rh->task_id = GNUNET_SCHEDULER_NO_TASK;
  723. p = GNUNET_DNSPARSER_parse ((const char *) dns,
  724. dns_len);
  725. if (NULL == p)
  726. {
  727. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  728. _("Failed to parse DNS response\n"));
  729. rh->proc (rh->proc_cls, 0, NULL);
  730. GNS_resolver_lookup_cancel (rh);
  731. return;
  732. }
  733. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  734. "Received DNS response for `%s' with %u answers\n",
  735. rh->ac_tail->label,
  736. (unsigned int) p->num_answers);
  737. if ( (p->num_answers > 0) &&
  738. (GNUNET_DNSPARSER_TYPE_CNAME == p->answers[0].type) &&
  739. (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
  740. {
  741. GNUNET_free (rh->name);
  742. rh->name = GNUNET_strdup (p->answers[0].data.hostname);
  743. rh->name_resolution_pos = strlen (rh->name);
  744. start_resolver_lookup (rh);
  745. GNUNET_DNSPARSER_free_packet (p);
  746. return;
  747. }
  748. /* convert from (parsed) DNS to (binary) GNS format! */
  749. rd_count = p->num_answers + p->num_authority_records + p->num_additional_records;
  750. {
  751. struct GNUNET_GNSRECORD_Data rd[rd_count];
  752. unsigned int skip;
  753. char buf[UINT16_MAX];
  754. size_t buf_off;
  755. size_t buf_start;
  756. buf_off = 0;
  757. skip = 0;
  758. memset (rd, 0, sizeof (rd));
  759. for (i=0;i<rd_count;i++)
  760. {
  761. if (i < p->num_answers)
  762. rec = &p->answers[i];
  763. else if (i < p->num_answers + p->num_authority_records)
  764. rec = &p->authority_records[i - p->num_answers];
  765. else
  766. rec = &p->authority_records[i - p->num_answers - p->num_authority_records];
  767. /* As we copied the full DNS name to 'rh->ac_tail->label', this
  768. should be the correct check to see if this record is actually
  769. a record for our label... */
  770. if (0 != strcmp (rec->name,
  771. rh->ac_tail->label))
  772. {
  773. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  774. "Dropping record `%s', does not match desired name `%s'\n",
  775. rec->name,
  776. rh->ac_tail->label);
  777. skip++;
  778. continue;
  779. }
  780. rd[i - skip].record_type = rec->type;
  781. rd[i - skip].expiration_time = rec->expiration_time.abs_value_us;
  782. switch (rec->type)
  783. {
  784. case GNUNET_DNSPARSER_TYPE_A:
  785. if (rec->data.raw.data_len != sizeof (struct in_addr))
  786. {
  787. GNUNET_break_op (0);
  788. skip++;
  789. continue;
  790. }
  791. rd[i - skip].data_size = rec->data.raw.data_len;
  792. rd[i - skip].data = rec->data.raw.data;
  793. break;
  794. case GNUNET_DNSPARSER_TYPE_AAAA:
  795. if (rec->data.raw.data_len != sizeof (struct in6_addr))
  796. {
  797. GNUNET_break_op (0);
  798. skip++;
  799. continue;
  800. }
  801. rd[i - skip].data_size = rec->data.raw.data_len;
  802. rd[i - skip].data = rec->data.raw.data;
  803. break;
  804. case GNUNET_DNSPARSER_TYPE_CNAME:
  805. case GNUNET_DNSPARSER_TYPE_PTR:
  806. case GNUNET_DNSPARSER_TYPE_NS:
  807. buf_start = buf_off;
  808. if (GNUNET_OK !=
  809. GNUNET_DNSPARSER_builder_add_name (buf,
  810. sizeof (buf),
  811. &buf_off,
  812. rec->data.hostname))
  813. {
  814. GNUNET_break (0);
  815. skip++;
  816. continue;
  817. }
  818. rd[i - skip].data_size = buf_off - buf_start;
  819. rd[i - skip].data = &buf[buf_start];
  820. break;
  821. case GNUNET_DNSPARSER_TYPE_SOA:
  822. buf_start = buf_off;
  823. if (GNUNET_OK !=
  824. GNUNET_DNSPARSER_builder_add_soa (buf,
  825. sizeof (buf),
  826. &buf_off,
  827. rec->data.soa))
  828. {
  829. GNUNET_break (0);
  830. skip++;
  831. continue;
  832. }
  833. rd[i - skip].data_size = buf_off - buf_start;
  834. rd[i - skip].data = &buf[buf_start];
  835. break;
  836. case GNUNET_DNSPARSER_TYPE_MX:
  837. buf_start = buf_off;
  838. if (GNUNET_OK !=
  839. GNUNET_DNSPARSER_builder_add_mx (buf,
  840. sizeof (buf),
  841. &buf_off,
  842. rec->data.mx))
  843. {
  844. GNUNET_break (0);
  845. skip++;
  846. continue;
  847. }
  848. rd[i - skip].data_size = buf_off - buf_start;
  849. rd[i - skip].data = &buf[buf_start];
  850. break;
  851. case GNUNET_DNSPARSER_TYPE_SRV:
  852. buf_start = buf_off;
  853. if (GNUNET_OK !=
  854. GNUNET_DNSPARSER_builder_add_srv (buf,
  855. sizeof (buf),
  856. &buf_off,
  857. rec->data.srv))
  858. {
  859. GNUNET_break (0);
  860. skip++;
  861. continue;
  862. }
  863. rd[i - skip].data_size = buf_off - buf_start;
  864. rd[i - skip].data = &buf[buf_start];
  865. break;
  866. default:
  867. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  868. _("Skipping record of unsupported type %d\n"),
  869. rec->type);
  870. skip++;
  871. continue;
  872. }
  873. }
  874. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  875. "Returning DNS response for `%s' with %u answers\n",
  876. rh->ac_tail->label,
  877. (unsigned int) p->num_answers);
  878. rh->proc (rh->proc_cls, rd_count - skip, rd);
  879. GNS_resolver_lookup_cancel (rh);
  880. }
  881. GNUNET_DNSPARSER_free_packet (p);
  882. }
  883. /**
  884. * Perform recursive DNS resolution. Asks the given DNS resolver to
  885. * resolve "rh->dns_name", possibly recursively proceeding following
  886. * NS delegations, CNAMES, etc., until 'rh->loop_limiter' bounds us or
  887. * we find the answer.
  888. *
  889. * @param rh resolution information
  890. */
  891. static void
  892. recursive_dns_resolution (struct GNS_ResolverHandle *rh)
  893. {
  894. struct AuthorityChain *ac;
  895. socklen_t sa_len;
  896. struct GNUNET_DNSPARSER_Query *query;
  897. struct GNUNET_DNSPARSER_Packet *p;
  898. char *dns_request;
  899. size_t dns_request_length;
  900. ac = rh->ac_tail;
  901. GNUNET_assert (NULL != ac);
  902. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  903. "Starting DNS lookup for `%s'\n",
  904. ac->label);
  905. GNUNET_assert (GNUNET_NO == ac->gns_authority);
  906. switch (((const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip)->sa_family)
  907. {
  908. case AF_INET:
  909. sa_len = sizeof (struct sockaddr_in);
  910. break;
  911. case AF_INET6:
  912. sa_len = sizeof (struct sockaddr_in6);
  913. break;
  914. default:
  915. GNUNET_break (0);
  916. rh->proc (rh->proc_cls, 0, NULL);
  917. GNS_resolver_lookup_cancel (rh);
  918. return;
  919. }
  920. query = GNUNET_new (struct GNUNET_DNSPARSER_Query);
  921. query->name = GNUNET_strdup (ac->label);
  922. query->type = rh->record_type;
  923. query->dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
  924. p = GNUNET_new (struct GNUNET_DNSPARSER_Packet);
  925. p->queries = query;
  926. p->num_queries = 1;
  927. p->id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
  928. UINT16_MAX);
  929. p->flags.opcode = GNUNET_TUN_DNS_OPCODE_QUERY;
  930. p->flags.recursion_desired = 1;
  931. if (GNUNET_OK !=
  932. GNUNET_DNSPARSER_pack (p, 1024, &dns_request, &dns_request_length))
  933. {
  934. GNUNET_break (0);
  935. rh->proc (rh->proc_cls, 0, NULL);
  936. GNS_resolver_lookup_cancel (rh);
  937. }
  938. else
  939. {
  940. rh->dns_request = GNUNET_DNSSTUB_resolve (dns_handle,
  941. (const struct sockaddr *) &ac->authority_info.dns_authority.dns_ip,
  942. sa_len,
  943. dns_request,
  944. dns_request_length,
  945. &dns_result_parser,
  946. rh);
  947. rh->task_id = GNUNET_SCHEDULER_add_delayed (DNS_LOOKUP_TIMEOUT,
  948. &fail_resolution,
  949. rh);
  950. }
  951. GNUNET_free (dns_request);
  952. GNUNET_DNSPARSER_free_packet (p);
  953. }
  954. /**
  955. * We encountered a CNAME record during our resolution.
  956. * Merge it into our chain.
  957. *
  958. * @param rh resolution we are performing
  959. * @param cname value of the cname record we got for the current
  960. * authority chain tail
  961. */
  962. static void
  963. handle_gns_cname_result (struct GNS_ResolverHandle *rh,
  964. const char *cname)
  965. {
  966. size_t nlen;
  967. char *res;
  968. struct AuthorityChain *ac;
  969. nlen = strlen (cname);
  970. if ( (nlen > 2) &&
  971. (0 == strcmp (".+",
  972. &cname[nlen - 2])) )
  973. {
  974. /* CNAME resolution continues relative to current domain */
  975. if (0 == rh->name_resolution_pos)
  976. {
  977. res = GNUNET_strndup (cname, nlen - 2);
  978. rh->name_resolution_pos = nlen - 2;
  979. }
  980. else
  981. {
  982. GNUNET_asprintf (&res,
  983. "%.*s.%.*s",
  984. (int) rh->name_resolution_pos,
  985. rh->name,
  986. (int) (nlen - 2),
  987. cname);
  988. rh->name_resolution_pos = strlen (res);
  989. }
  990. GNUNET_free (rh->name);
  991. rh->name = res;
  992. ac = GNUNET_new (struct AuthorityChain);
  993. ac->rh = rh;
  994. ac->gns_authority = GNUNET_YES;
  995. ac->authority_info.gns_authority = rh->ac_tail->authority_info.gns_authority;
  996. ac->label = resolver_lookup_get_next_label (rh);
  997. ac->suggested_shortening_label = NULL;
  998. ac->shortening_started = GNUNET_NO;
  999. /* add AC to tail */
  1000. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  1001. rh->ac_tail,
  1002. ac);
  1003. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  1004. rh);
  1005. return;
  1006. }
  1007. /* name is absolute, start from the beginning */
  1008. GNUNET_free (rh->name);
  1009. rh->name = GNUNET_strdup (cname);
  1010. rh->name_resolution_pos = strlen (rh->name);
  1011. start_resolver_lookup (rh);
  1012. }
  1013. /**
  1014. * Process a records that were decrypted from a block.
  1015. *
  1016. * @param cls closure with the 'struct GNS_ResolverHandle'
  1017. * @param rd_count number of entries in @a rd array
  1018. * @param rd array of records with data to store
  1019. */
  1020. static void
  1021. handle_gns_resolution_result (void *cls,
  1022. unsigned int rd_count,
  1023. const struct GNUNET_GNSRECORD_Data *rd);
  1024. /**
  1025. * Callback invoked from the VPN service once a redirection is
  1026. * available. Provides the IP address that can now be used to
  1027. * reach the requested destination. Replaces the "VPN" record
  1028. * with the respective A/AAAA record and continues processing.
  1029. *
  1030. * @param cls closure
  1031. * @param af address family, AF_INET or AF_INET6; AF_UNSPEC on error;
  1032. * will match 'result_af' from the request
  1033. * @param address IP address (struct in_addr or struct in_addr6, depending on 'af')
  1034. * that the VPN allocated for the redirection;
  1035. * traffic to this IP will now be redirected to the
  1036. * specified target peer; NULL on error
  1037. */
  1038. static void
  1039. vpn_allocation_cb (void *cls,
  1040. int af,
  1041. const void *address)
  1042. {
  1043. struct VpnContext *vpn_ctx = cls;
  1044. struct GNS_ResolverHandle *rh = vpn_ctx->rh;
  1045. struct GNUNET_GNSRECORD_Data rd[vpn_ctx->rd_count];
  1046. unsigned int i;
  1047. vpn_ctx->vpn_request = NULL;
  1048. rh->vpn_ctx = NULL;
  1049. GNUNET_assert (GNUNET_OK ==
  1050. GNUNET_GNSRECORD_records_deserialize (vpn_ctx->rd_data_size,
  1051. vpn_ctx->rd_data,
  1052. vpn_ctx->rd_count,
  1053. rd));
  1054. for (i=0;i<vpn_ctx->rd_count;i++)
  1055. {
  1056. if (GNUNET_GNSRECORD_TYPE_VPN == rd[i].record_type)
  1057. {
  1058. switch (af)
  1059. {
  1060. case AF_INET:
  1061. rd[i].record_type = GNUNET_DNSPARSER_TYPE_A;
  1062. rd[i].data_size = sizeof (struct in_addr);
  1063. rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
  1064. rd[i].flags = 0;
  1065. rd[i].data = address;
  1066. break;
  1067. case AF_INET6:
  1068. rd[i].record_type = GNUNET_DNSPARSER_TYPE_AAAA;
  1069. rd[i].expiration_time = GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT).abs_value_us;
  1070. rd[i].flags = 0;
  1071. rd[i].data = address;
  1072. rd[i].data_size = sizeof (struct in6_addr);
  1073. break;
  1074. default:
  1075. GNUNET_assert (0);
  1076. }
  1077. break;
  1078. }
  1079. }
  1080. GNUNET_assert (i < vpn_ctx->rd_count);
  1081. handle_gns_resolution_result (rh,
  1082. vpn_ctx->rd_count,
  1083. rd);
  1084. GNUNET_free (vpn_ctx->rd_data);
  1085. GNUNET_free (vpn_ctx);
  1086. }
  1087. /**
  1088. * We've resolved the IP address for the DNS resolver to use
  1089. * after encountering a GNS2DNS record.
  1090. *
  1091. * TODO: Right now we only foward the request to ONE DNS resolver,
  1092. * even if we get multiple IP addresses back; a correct implementation
  1093. * should try all DNS resolvers.
  1094. *
  1095. * @param cls the `struct GNS_ResolverHandle` where we encountered
  1096. * the GNS2DNS record
  1097. * @param rd_count number of records in @a rd
  1098. * @param rd addresses for the DNS resolver (presumably)
  1099. */
  1100. static void
  1101. handle_gns2dns_result (void *cls,
  1102. unsigned int rd_count,
  1103. const struct GNUNET_GNSRECORD_Data *rd)
  1104. {
  1105. struct GNS_ResolverHandle *rh = cls;
  1106. struct AuthorityChain *ac;
  1107. unsigned int j;
  1108. struct sockaddr *sa;
  1109. struct sockaddr_in v4;
  1110. struct sockaddr_in6 v6;
  1111. size_t sa_len;
  1112. /* find suitable A/AAAA record */
  1113. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1114. "Received %u results for IP address of DNS server for GNS2DNS transition\n",
  1115. rd_count);
  1116. /* enable cleanup of 'rh' handle that comes next... */
  1117. GNUNET_CONTAINER_DLL_insert (rlh_head,
  1118. rlh_tail,
  1119. rh->g2dc->rh);
  1120. rh->g2dc->rh = NULL;
  1121. sa = NULL;
  1122. sa_len = 0;
  1123. for (j=0;j<rd_count;j++)
  1124. {
  1125. switch (rd[j].record_type)
  1126. {
  1127. case GNUNET_DNSPARSER_TYPE_A:
  1128. if (sizeof (struct in_addr) != rd[j].data_size)
  1129. {
  1130. GNUNET_break_op (0);
  1131. rh->proc (rh->proc_cls, 0, NULL);
  1132. GNS_resolver_lookup_cancel (rh);
  1133. return;
  1134. }
  1135. /* FIXME: might want to check if we support IPv4 here,
  1136. and otherwise skip this one and hope we find another */
  1137. memset (&v4, 0, sizeof (v4));
  1138. sa_len = sizeof (v4);
  1139. v4.sin_family = AF_INET;
  1140. v4.sin_port = htons (53);
  1141. #if HAVE_SOCKADDR_IN_SIN_LEN
  1142. v4.sin_len = (u_char) sa_len;
  1143. #endif
  1144. memcpy (&v4.sin_addr,
  1145. rd[j].data,
  1146. sizeof (struct in_addr));
  1147. sa = (struct sockaddr *) &v4;
  1148. break;
  1149. case GNUNET_DNSPARSER_TYPE_AAAA:
  1150. if (sizeof (struct in6_addr) != rd[j].data_size)
  1151. {
  1152. GNUNET_break_op (0);
  1153. rh->proc (rh->proc_cls, 0, NULL);
  1154. GNS_resolver_lookup_cancel (rh);
  1155. return;
  1156. }
  1157. /* FIXME: might want to check if we support IPv6 here,
  1158. and otherwise skip this one and hope we find another */
  1159. memset (&v6, 0, sizeof (v6));
  1160. sa_len = sizeof (v6);
  1161. v6.sin6_family = AF_INET6;
  1162. v6.sin6_port = htons (53);
  1163. #if HAVE_SOCKADDR_IN_SIN_LEN
  1164. v6.sin6_len = (u_char) sa_len;
  1165. #endif
  1166. memcpy (&v6.sin6_addr,
  1167. rd[j].data,
  1168. sizeof (struct in6_addr));
  1169. sa = (struct sockaddr *) &v6;
  1170. break;
  1171. default:
  1172. break;
  1173. }
  1174. if (NULL != sa)
  1175. break;
  1176. }
  1177. if (NULL == sa)
  1178. {
  1179. /* we cannot continue; NS without A/AAAA */
  1180. rh->proc (rh->proc_cls, 0, NULL);
  1181. GNS_resolver_lookup_cancel (rh);
  1182. return;
  1183. }
  1184. /* expand authority chain */
  1185. ac = GNUNET_new (struct AuthorityChain);
  1186. ac->rh = rh;
  1187. strcpy (ac->authority_info.dns_authority.name,
  1188. rh->g2dc->ns);
  1189. memcpy (&ac->authority_info.dns_authority.dns_ip,
  1190. sa,
  1191. sa_len);
  1192. /* for DNS recursion, the label is the full DNS name,
  1193. created from the remainder of the GNS name and the
  1194. name in the NS record */
  1195. GNUNET_asprintf (&ac->label,
  1196. "%.*s%s%s",
  1197. (int) rh->name_resolution_pos,
  1198. rh->name,
  1199. (0 != rh->name_resolution_pos) ? "." : "",
  1200. rh->g2dc->ns);
  1201. GNUNET_free (rh->g2dc->ns);
  1202. GNUNET_free (rh->g2dc);
  1203. rh->g2dc = NULL;
  1204. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1205. "Will continue resolution using DNS server `%s' to resolve `%s'\n",
  1206. GNUNET_a2s (sa, sa_len),
  1207. ac->label);
  1208. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  1209. rh->ac_tail,
  1210. ac);
  1211. if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
  1212. {
  1213. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1214. _("GNS lookup resulted in DNS name that is too long (`%s')\n"),
  1215. ac->label);
  1216. rh->proc (rh->proc_cls, 0, NULL);
  1217. GNS_resolver_lookup_cancel (rh);
  1218. return;
  1219. }
  1220. /* recurse */
  1221. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  1222. rh);
  1223. }
  1224. /**
  1225. * Process a records that were decrypted from a block.
  1226. *
  1227. * @param cls closure with the `struct GNS_ResolverHandle`
  1228. * @param rd_count number of entries in @a rd array
  1229. * @param rd array of records with data to store
  1230. */
  1231. static void
  1232. handle_gns_resolution_result (void *cls,
  1233. unsigned int rd_count,
  1234. const struct GNUNET_GNSRECORD_Data *rd)
  1235. {
  1236. struct GNS_ResolverHandle *rh = cls;
  1237. struct AuthorityChain *ac;
  1238. struct AuthorityChain *shorten_ac;
  1239. unsigned int i;
  1240. char *cname;
  1241. struct VpnContext *vpn_ctx;
  1242. const struct GNUNET_TUN_GnsVpnRecord *vpn;
  1243. const char *vname;
  1244. struct GNUNET_HashCode vhash;
  1245. int af;
  1246. char scratch[UINT16_MAX];
  1247. size_t scratch_off;
  1248. size_t scratch_start;
  1249. size_t off;
  1250. struct GNUNET_GNSRECORD_Data rd_new[rd_count];
  1251. unsigned int rd_off;
  1252. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1253. "Resolution succeeded for `%s' in zone %s, got %u records\n",
  1254. rh->ac_tail->label,
  1255. GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
  1256. rd_count);
  1257. if (0 == rh->name_resolution_pos)
  1258. {
  1259. /* top-level match, are we done yet? */
  1260. if ( (rd_count > 0) &&
  1261. (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
  1262. (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type) )
  1263. {
  1264. off = 0;
  1265. cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
  1266. rd[0].data_size,
  1267. &off);
  1268. if ( (NULL == cname) ||
  1269. (off != rd[0].data_size) )
  1270. {
  1271. GNUNET_break_op (0);
  1272. rh->proc (rh->proc_cls, 0, NULL);
  1273. GNS_resolver_lookup_cancel (rh);
  1274. GNUNET_free_non_null (cname);
  1275. return;
  1276. }
  1277. handle_gns_cname_result (rh,
  1278. cname);
  1279. GNUNET_free (cname);
  1280. return;
  1281. }
  1282. /* If A/AAAA was requested, but we got a VPN
  1283. record, we convert it to A/AAAA using GNUnet VPN */
  1284. if ( (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
  1285. (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type) )
  1286. {
  1287. for (i=0;i<rd_count;i++)
  1288. {
  1289. switch (rd[i].record_type)
  1290. {
  1291. case GNUNET_GNSRECORD_TYPE_VPN:
  1292. {
  1293. af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET : AF_INET6;
  1294. if (sizeof (struct GNUNET_TUN_GnsVpnRecord) >
  1295. rd[i].data_size)
  1296. {
  1297. GNUNET_break_op (0);
  1298. rh->proc (rh->proc_cls, 0, NULL);
  1299. GNS_resolver_lookup_cancel (rh);
  1300. return;
  1301. }
  1302. vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
  1303. vname = (const char *) &vpn[1];
  1304. if ('\0' != vname[rd[i].data_size - 1 - sizeof (struct GNUNET_TUN_GnsVpnRecord)])
  1305. {
  1306. GNUNET_break_op (0);
  1307. rh->proc (rh->proc_cls, 0, NULL);
  1308. GNS_resolver_lookup_cancel (rh);
  1309. return;
  1310. }
  1311. GNUNET_TUN_service_name_to_hash (vname,
  1312. &vhash);
  1313. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1314. "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
  1315. GNUNET_i2s (&vpn->peer),
  1316. vname,
  1317. (int) af,
  1318. (int) ntohs (vpn->proto));
  1319. vpn_ctx = GNUNET_new (struct VpnContext);
  1320. rh->vpn_ctx = vpn_ctx;
  1321. vpn_ctx->rh = rh;
  1322. vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
  1323. rd);
  1324. vpn_ctx->rd_data = GNUNET_malloc (vpn_ctx->rd_data_size);
  1325. vpn_ctx->rd_count = rd_count;
  1326. GNUNET_assert (vpn_ctx->rd_data_size ==
  1327. GNUNET_GNSRECORD_records_serialize (rd_count,
  1328. rd,
  1329. vpn_ctx->rd_data_size,
  1330. vpn_ctx->rd_data));
  1331. vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
  1332. af,
  1333. ntohs (vpn->proto),
  1334. &vpn->peer,
  1335. &vhash,
  1336. GNUNET_TIME_relative_to_absolute (VPN_TIMEOUT),
  1337. &vpn_allocation_cb,
  1338. vpn_ctx);
  1339. return;
  1340. }
  1341. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  1342. {
  1343. /* delegation to DNS */
  1344. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1345. "Found GNS2DNS record, delegating to DNS!\n");
  1346. goto do_recurse;
  1347. }
  1348. default:
  1349. break;
  1350. } /* end: switch */
  1351. } /* end: for rd */
  1352. } /* end: name_resolution_pos */
  1353. /* convert relative names in record values to absolute names,
  1354. using 'scratch' array for memory allocations */
  1355. scratch_off = 0;
  1356. rd_off = 0;
  1357. shorten_ac = rh->ac_tail;
  1358. for (i=0;i<rd_count;i++)
  1359. {
  1360. if ( (0 != rh->protocol) &&
  1361. (0 != rh->service) &&
  1362. (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type) )
  1363. continue; /* we _only_ care about boxed records */
  1364. rd_new[rd_off] = rd[i];
  1365. /* Check if the embedded name(s) end in "+", and if so,
  1366. replace the "+" with the zone at "ac_tail", changing the name
  1367. to a ".zkey". The name is allocated on the 'scratch' array,
  1368. so we can free it afterwards. */
  1369. switch (rd[i].record_type)
  1370. {
  1371. case GNUNET_DNSPARSER_TYPE_CNAME:
  1372. {
  1373. char *cname;
  1374. off = 0;
  1375. cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
  1376. rd[i].data_size,
  1377. &off);
  1378. if ( (NULL == cname) ||
  1379. (off != rd[i].data_size) )
  1380. {
  1381. GNUNET_break_op (0); /* record not well-formed */
  1382. }
  1383. else
  1384. {
  1385. cname = translate_dot_plus (rh, cname);
  1386. GNUNET_break (NULL != cname);
  1387. scratch_start = scratch_off;
  1388. if (GNUNET_OK !=
  1389. GNUNET_DNSPARSER_builder_add_name (scratch,
  1390. sizeof (scratch),
  1391. &scratch_off,
  1392. cname))
  1393. {
  1394. GNUNET_break (0);
  1395. }
  1396. else
  1397. {
  1398. rd_new[rd_off].data = &scratch[scratch_start];
  1399. rd_new[rd_off].data_size = scratch_off - scratch_start;
  1400. rd_off++;
  1401. }
  1402. }
  1403. GNUNET_free_non_null (cname);
  1404. }
  1405. break;
  1406. case GNUNET_DNSPARSER_TYPE_SOA:
  1407. {
  1408. struct GNUNET_DNSPARSER_SoaRecord *soa;
  1409. off = 0;
  1410. soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
  1411. rd[i].data_size,
  1412. &off);
  1413. if ( (NULL == soa) ||
  1414. (off != rd[i].data_size) )
  1415. {
  1416. GNUNET_break_op (0); /* record not well-formed */
  1417. }
  1418. else
  1419. {
  1420. soa->mname = translate_dot_plus (rh, soa->mname);
  1421. soa->rname = translate_dot_plus (rh, soa->rname);
  1422. scratch_start = scratch_off;
  1423. if (GNUNET_OK !=
  1424. GNUNET_DNSPARSER_builder_add_soa (scratch,
  1425. sizeof (scratch),
  1426. &scratch_off,
  1427. soa))
  1428. {
  1429. GNUNET_break (0);
  1430. }
  1431. else
  1432. {
  1433. rd_new[rd_off].data = &scratch[scratch_start];
  1434. rd_new[rd_off].data_size = scratch_off - scratch_start;
  1435. rd_off++;
  1436. }
  1437. }
  1438. if (NULL != soa)
  1439. GNUNET_DNSPARSER_free_soa (soa);
  1440. }
  1441. break;
  1442. case GNUNET_DNSPARSER_TYPE_MX:
  1443. {
  1444. struct GNUNET_DNSPARSER_MxRecord *mx;
  1445. off = 0;
  1446. mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
  1447. rd[i].data_size,
  1448. &off);
  1449. if ( (NULL == mx) ||
  1450. (off != rd[i].data_size) )
  1451. {
  1452. GNUNET_break_op (0); /* record not well-formed */
  1453. }
  1454. else
  1455. {
  1456. mx->mxhost = translate_dot_plus (rh, mx->mxhost);
  1457. scratch_start = scratch_off;
  1458. if (GNUNET_OK !=
  1459. GNUNET_DNSPARSER_builder_add_mx (scratch,
  1460. sizeof (scratch),
  1461. &scratch_off,
  1462. mx))
  1463. {
  1464. GNUNET_break (0);
  1465. }
  1466. else
  1467. {
  1468. rd_new[rd_off].data = &scratch[scratch_start];
  1469. rd_new[rd_off].data_size = scratch_off - scratch_start;
  1470. rd_off++;
  1471. }
  1472. }
  1473. if (NULL != mx)
  1474. GNUNET_DNSPARSER_free_mx (mx);
  1475. }
  1476. break;
  1477. case GNUNET_DNSPARSER_TYPE_SRV:
  1478. {
  1479. struct GNUNET_DNSPARSER_SrvRecord *srv;
  1480. off = 0;
  1481. srv = GNUNET_DNSPARSER_parse_srv (rd[i].data,
  1482. rd[i].data_size,
  1483. &off);
  1484. if ( (NULL == srv) ||
  1485. (off != rd[i].data_size) )
  1486. {
  1487. GNUNET_break_op (0); /* record not well-formed */
  1488. }
  1489. else
  1490. {
  1491. srv->target = translate_dot_plus (rh, srv->target);
  1492. scratch_start = scratch_off;
  1493. if (GNUNET_OK !=
  1494. GNUNET_DNSPARSER_builder_add_srv (scratch,
  1495. sizeof (scratch),
  1496. &scratch_off,
  1497. srv))
  1498. {
  1499. GNUNET_break (0);
  1500. }
  1501. else
  1502. {
  1503. rd_new[rd_off].data = &scratch[scratch_start];
  1504. rd_new[rd_off].data_size = scratch_off - scratch_start;
  1505. rd_off++;
  1506. }
  1507. }
  1508. if (NULL != srv)
  1509. GNUNET_DNSPARSER_free_srv (srv);
  1510. }
  1511. break;
  1512. case GNUNET_GNSRECORD_TYPE_NICK:
  1513. {
  1514. const char *nick;
  1515. nick = rd[i].data;
  1516. if ((rd[i].data_size > 0) &&
  1517. (nick[rd[i].data_size -1] != '\0'))
  1518. {
  1519. GNUNET_break_op (0);
  1520. break;
  1521. }
  1522. if (NULL == shorten_ac->suggested_shortening_label)
  1523. shorten_ac->suggested_shortening_label = GNUNET_strdup (nick);
  1524. break;
  1525. }
  1526. case GNUNET_GNSRECORD_TYPE_PKEY:
  1527. {
  1528. struct GNUNET_CRYPTO_EcdsaPublicKey pub;
  1529. if (rd[i].data_size != sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey))
  1530. {
  1531. GNUNET_break_op (0);
  1532. break;
  1533. }
  1534. memcpy (&pub, rd[i].data, rd[i].data_size);
  1535. rd_off++;
  1536. if (GNUNET_GNSRECORD_TYPE_PKEY != rh->record_type)
  1537. {
  1538. /* try to resolve "+" */
  1539. struct AuthorityChain *ac;
  1540. ac = GNUNET_new (struct AuthorityChain);
  1541. ac->rh = rh;
  1542. ac->gns_authority = GNUNET_YES;
  1543. ac->authority_info.gns_authority = pub;
  1544. ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
  1545. ac->suggested_shortening_label = NULL;
  1546. ac->shortening_started = GNUNET_NO;
  1547. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  1548. rh->ac_tail,
  1549. ac);
  1550. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  1551. rh);
  1552. return;
  1553. }
  1554. }
  1555. break;
  1556. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  1557. {
  1558. /* delegation to DNS */
  1559. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1560. "Found GNS2DNS record, delegating to DNS!\n");
  1561. goto do_recurse;
  1562. }
  1563. case GNUNET_GNSRECORD_TYPE_BOX:
  1564. {
  1565. /* unbox SRV/TLSA records if a specific one was requested */
  1566. if ( (0 != rh->protocol) &&
  1567. (0 != rh->service) &&
  1568. (rd[i].data_size >= sizeof (struct GNUNET_GNSRECORD_BoxRecord)) )
  1569. {
  1570. const struct GNUNET_GNSRECORD_BoxRecord *box;
  1571. box = rd[i].data;
  1572. if ( (ntohs (box->protocol) == rh->protocol) &&
  1573. (ntohs (box->service) == rh->service) )
  1574. {
  1575. /* Box matches, unbox! */
  1576. rd_new[rd_off].record_type = ntohl (box->record_type);
  1577. rd_new[rd_off].data_size -= sizeof (struct GNUNET_GNSRECORD_BoxRecord);
  1578. rd_new[rd_off].data = &box[1];
  1579. rd_off++;
  1580. }
  1581. }
  1582. else
  1583. {
  1584. /* no specific protocol/service specified, preserve all BOX
  1585. records (for modern, GNS-enabled applications) */
  1586. rd_off++;
  1587. }
  1588. }
  1589. default:
  1590. rd_off++;
  1591. break;
  1592. } /* end: switch */
  1593. } /* end: for rd_count */
  1594. /* trigger shortening */
  1595. if ((NULL != rh->shorten_key) &&
  1596. (NULL != shorten_ac) &&
  1597. (GNUNET_NO == shorten_ac->shortening_started) &&
  1598. (NULL != shorten_ac->suggested_shortening_label))
  1599. {
  1600. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1601. "Start shortening for label `%s' based on nick `%s'\n",
  1602. shorten_ac->label,
  1603. shorten_ac->suggested_shortening_label);
  1604. shorten_ac->shortening_started = GNUNET_YES;
  1605. GNS_shorten_start (shorten_ac->label,
  1606. shorten_ac->suggested_shortening_label,
  1607. &shorten_ac->authority_info.gns_authority,
  1608. rh->shorten_key);
  1609. }
  1610. /* yes, we are done, return result */
  1611. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1612. "Returning GNS response for `%s' with %u answers\n",
  1613. rh->ac_tail->label,
  1614. rd_off);
  1615. rh->proc (rh->proc_cls, rd_off, rd_new);
  1616. GNS_resolver_lookup_cancel (rh);
  1617. return;
  1618. }
  1619. do_recurse:
  1620. /* need to recurse, check if we can */
  1621. for (i=0;i<rd_count;i++)
  1622. {
  1623. switch (rd[i].record_type)
  1624. {
  1625. case GNUNET_GNSRECORD_TYPE_PKEY:
  1626. /* delegation to another zone */
  1627. if (sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) !=
  1628. rd[i].data_size)
  1629. {
  1630. GNUNET_break_op (0);
  1631. rh->proc (rh->proc_cls, 0, NULL);
  1632. GNS_resolver_lookup_cancel (rh);
  1633. return;
  1634. }
  1635. /* expand authority chain */
  1636. ac = GNUNET_new (struct AuthorityChain);
  1637. ac->rh = rh;
  1638. ac->gns_authority = GNUNET_YES;
  1639. ac->suggested_shortening_label = NULL;
  1640. ac->shortening_started = GNUNET_NO;
  1641. memcpy (&ac->authority_info.gns_authority,
  1642. rd[i].data,
  1643. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey));
  1644. ac->label = resolver_lookup_get_next_label (rh);
  1645. /* add AC to tail */
  1646. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  1647. rh->ac_tail,
  1648. ac);
  1649. /* recurse */
  1650. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  1651. rh);
  1652. return;
  1653. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  1654. {
  1655. /* resolution continues within DNS */
  1656. struct Gns2DnsContext *g2dc;
  1657. char *ip;
  1658. char *ns;
  1659. off = 0;
  1660. ns = GNUNET_DNSPARSER_parse_name (rd[i].data,
  1661. rd[i].data_size,
  1662. &off);
  1663. ip = GNUNET_DNSPARSER_parse_name (rd[i].data,
  1664. rd[i].data_size,
  1665. &off);
  1666. if ( (NULL == ns) ||
  1667. (NULL == ip) ||
  1668. (off != rd[i].data_size) )
  1669. {
  1670. GNUNET_break_op (0);
  1671. GNUNET_free_non_null (ns);
  1672. GNUNET_free_non_null (ip);
  1673. rh->proc (rh->proc_cls, 0, NULL);
  1674. GNS_resolver_lookup_cancel (rh);
  1675. return;
  1676. }
  1677. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1678. "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition\n",
  1679. ip);
  1680. /* resolve 'ip' to determine the IP(s) of the DNS
  1681. resolver to use */
  1682. g2dc = GNUNET_new (struct Gns2DnsContext);
  1683. g2dc->ns = ns;
  1684. g2dc->rh = GNUNET_new (struct GNS_ResolverHandle);
  1685. g2dc->rh->authority_zone = rh->ac_tail->authority_info.gns_authority;
  1686. ip = translate_dot_plus (rh, ip);
  1687. g2dc->rh->name = ip;
  1688. g2dc->rh->name_resolution_pos = strlen (ip);
  1689. g2dc->rh->proc = &handle_gns2dns_result;
  1690. g2dc->rh->proc_cls = rh;
  1691. g2dc->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
  1692. g2dc->rh->options = GNUNET_GNS_LO_DEFAULT;
  1693. g2dc->rh->loop_limiter = rh->loop_limiter + 1;
  1694. rh->g2dc = g2dc;
  1695. start_resolver_lookup (g2dc->rh);
  1696. return;
  1697. }
  1698. case GNUNET_DNSPARSER_TYPE_CNAME:
  1699. {
  1700. char *cname;
  1701. off = 0;
  1702. cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
  1703. rd[i].data_size,
  1704. &off);
  1705. if ( (NULL == cname) ||
  1706. (off != rd[i].data_size) )
  1707. {
  1708. GNUNET_break_op (0); /* record not well-formed */
  1709. rh->proc (rh->proc_cls, 0, NULL);
  1710. GNS_resolver_lookup_cancel (rh);
  1711. GNUNET_free_non_null (cname);
  1712. return;
  1713. }
  1714. handle_gns_cname_result (rh,
  1715. cname);
  1716. GNUNET_free (cname);
  1717. return;
  1718. }
  1719. /* FIXME: handle DNAME */
  1720. default:
  1721. /* skip */
  1722. break;
  1723. }
  1724. }
  1725. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1726. _("GNS lookup recursion failed (no delegation record found)\n"));
  1727. rh->proc (rh->proc_cls, 0, NULL);
  1728. GNS_resolver_lookup_cancel (rh);
  1729. }
  1730. /**
  1731. * Function called once the namestore has completed the request for
  1732. * caching a block.
  1733. *
  1734. * @param cls closure with the `struct CacheOps`
  1735. * @param success #GNUNET_OK on success
  1736. * @param emsg error message
  1737. */
  1738. static void
  1739. namecache_cache_continuation (void *cls,
  1740. int32_t success,
  1741. const char *emsg)
  1742. {
  1743. struct CacheOps *co = cls;
  1744. co->namecache_qe_cache = NULL;
  1745. if (NULL != emsg)
  1746. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1747. _("Failed to cache GNS resolution: %s\n"),
  1748. emsg);
  1749. GNUNET_CONTAINER_DLL_remove (co_head,
  1750. co_tail,
  1751. co);
  1752. GNUNET_free (co);
  1753. }
  1754. /**
  1755. * Iterator called on each result obtained for a DHT
  1756. * operation that expects a reply
  1757. *
  1758. * @param cls closure with the `struct GNS_ResolverHandle`
  1759. * @param exp when will this value expire
  1760. * @param key key of the result
  1761. * @param get_path peers on reply path (or NULL if not recorded)
  1762. * [0] = datastore's first neighbor, [length - 1] = local peer
  1763. * @param get_path_length number of entries in @a get_path
  1764. * @param put_path peers on the PUT path (or NULL if not recorded)
  1765. * [0] = origin, [length - 1] = datastore
  1766. * @param put_path_length number of entries in @a put_path
  1767. * @param type type of the result
  1768. * @param size number of bytes in data
  1769. * @param data pointer to the result data
  1770. */
  1771. static void
  1772. handle_dht_response (void *cls,
  1773. struct GNUNET_TIME_Absolute exp,
  1774. const struct GNUNET_HashCode *key,
  1775. const struct GNUNET_PeerIdentity *get_path,
  1776. unsigned int get_path_length,
  1777. const struct GNUNET_PeerIdentity *put_path,
  1778. unsigned int put_path_length,
  1779. enum GNUNET_BLOCK_Type type,
  1780. size_t size, const void *data)
  1781. {
  1782. struct GNS_ResolverHandle *rh = cls;
  1783. struct AuthorityChain *ac = rh->ac_tail;
  1784. const struct GNUNET_GNSRECORD_Block *block;
  1785. struct CacheOps *co;
  1786. GNUNET_DHT_get_stop (rh->get_handle);
  1787. rh->get_handle = NULL;
  1788. GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
  1789. rh->dht_heap_node = NULL;
  1790. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1791. "Handling response from the DHT\n");
  1792. if (size < sizeof (struct GNUNET_GNSRECORD_Block))
  1793. {
  1794. /* how did this pass DHT block validation!? */
  1795. GNUNET_break (0);
  1796. rh->proc (rh->proc_cls, 0, NULL);
  1797. GNS_resolver_lookup_cancel (rh);
  1798. return;
  1799. }
  1800. block = data;
  1801. if (size !=
  1802. ntohl (block->purpose.size) +
  1803. sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey) +
  1804. sizeof (struct GNUNET_CRYPTO_EcdsaSignature))
  1805. {
  1806. /* how did this pass DHT block validation!? */
  1807. GNUNET_break (0);
  1808. rh->proc (rh->proc_cls, 0, NULL);
  1809. GNS_resolver_lookup_cancel (rh);
  1810. return;
  1811. }
  1812. if (GNUNET_OK !=
  1813. GNUNET_GNSRECORD_block_decrypt (block,
  1814. &ac->authority_info.gns_authority,
  1815. ac->label,
  1816. &handle_gns_resolution_result,
  1817. rh))
  1818. {
  1819. GNUNET_break_op (0); /* block was ill-formed */
  1820. rh->proc (rh->proc_cls, 0, NULL);
  1821. GNS_resolver_lookup_cancel (rh);
  1822. return;
  1823. }
  1824. if (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us)
  1825. {
  1826. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1827. "Received expired block from the DHT, will not cache it.\n");
  1828. return;
  1829. }
  1830. /* Cache well-formed blocks */
  1831. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1832. "Caching response from the DHT in namecache\n");
  1833. co = GNUNET_new (struct CacheOps);
  1834. co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
  1835. block,
  1836. &namecache_cache_continuation,
  1837. co);
  1838. GNUNET_CONTAINER_DLL_insert (co_head,
  1839. co_tail,
  1840. co);
  1841. }
  1842. /**
  1843. * Initiate a DHT query for a set of GNS records.
  1844. *
  1845. * @param rh resolution handle
  1846. * @param query key to use in the DHT lookup
  1847. */
  1848. static void
  1849. start_dht_request (struct GNS_ResolverHandle *rh,
  1850. const struct GNUNET_HashCode *query)
  1851. {
  1852. struct GNS_ResolverHandle *rx;
  1853. GNUNET_assert (NULL == rh->get_handle);
  1854. rh->get_handle = GNUNET_DHT_get_start (dht_handle,
  1855. GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
  1856. query,
  1857. DHT_GNS_REPLICATION_LEVEL,
  1858. GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
  1859. NULL, 0,
  1860. &handle_dht_response, rh);
  1861. rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
  1862. rh,
  1863. GNUNET_TIME_absolute_get ().abs_value_us);
  1864. if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) > max_allowed_background_queries)
  1865. {
  1866. /* fail longest-standing DHT request */
  1867. rx = GNUNET_CONTAINER_heap_peek (dht_lookup_heap);
  1868. GNUNET_assert (NULL != rx);
  1869. rx->proc (rx->proc_cls, 0, NULL);
  1870. GNS_resolver_lookup_cancel (rx);
  1871. }
  1872. }
  1873. /**
  1874. * Process a records that were decrypted from a block that we got from
  1875. * the namecache. Simply calls #handle_gns_resolution_result().
  1876. *
  1877. * @param cls closure with the `struct GNS_ResolverHandle`
  1878. * @param rd_count number of entries in @a rd array
  1879. * @param rd array of records with data to store
  1880. */
  1881. static void
  1882. handle_gns_namecache_resolution_result (void *cls,
  1883. unsigned int rd_count,
  1884. const struct GNUNET_GNSRECORD_Data *rd)
  1885. {
  1886. struct GNS_ResolverHandle *rh = cls;
  1887. handle_gns_resolution_result (rh,
  1888. rd_count,
  1889. rd);
  1890. }
  1891. /**
  1892. * Process a record that was stored in the namecache.
  1893. *
  1894. * @param cls closure with the `struct GNS_ResolverHandle`
  1895. * @param block block that was stored in the namecache
  1896. */
  1897. static void
  1898. handle_namecache_block_response (void *cls,
  1899. const struct GNUNET_GNSRECORD_Block *block)
  1900. {
  1901. struct GNS_ResolverHandle *rh = cls;
  1902. struct AuthorityChain *ac = rh->ac_tail;
  1903. const char *label = ac->label;
  1904. const struct GNUNET_CRYPTO_EcdsaPublicKey *auth = &ac->authority_info.gns_authority;
  1905. struct GNUNET_HashCode query;
  1906. GNUNET_assert (NULL != rh->namecache_qe);
  1907. rh->namecache_qe = NULL;
  1908. if ( ( (GNUNET_GNS_LO_DEFAULT == rh->options) ||
  1909. ( (GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
  1910. (ac != rh->ac_head) ) ) &&
  1911. ( (NULL == block) ||
  1912. (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) ) )
  1913. {
  1914. /* namecache knows nothing; try DHT lookup */
  1915. GNUNET_GNSRECORD_query_from_public_key (auth,
  1916. label,
  1917. &query);
  1918. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1919. "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
  1920. ac->label,
  1921. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
  1922. GNUNET_h2s (&query));
  1923. start_dht_request (rh, &query);
  1924. return;
  1925. }
  1926. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1927. "Received result from namecache for label `%s'\n",
  1928. ac->label);
  1929. if ( (NULL == block) ||
  1930. (0 == GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh (block->expiration_time)).rel_value_us) )
  1931. {
  1932. /* DHT not permitted and no local result, fail */
  1933. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1934. "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
  1935. ac->label,
  1936. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  1937. rh->proc (rh->proc_cls, 0, NULL);
  1938. GNS_resolver_lookup_cancel (rh);
  1939. return;
  1940. }
  1941. if (GNUNET_OK !=
  1942. GNUNET_GNSRECORD_block_decrypt (block,
  1943. auth,
  1944. label,
  1945. &handle_gns_namecache_resolution_result,
  1946. rh))
  1947. {
  1948. GNUNET_break_op (0); /* block was ill-formed */
  1949. /* try DHT instead */
  1950. GNUNET_GNSRECORD_query_from_public_key (auth,
  1951. label,
  1952. &query);
  1953. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1954. "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
  1955. ac->label,
  1956. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
  1957. GNUNET_h2s (&query));
  1958. start_dht_request (rh, &query);
  1959. return;
  1960. }
  1961. }
  1962. /**
  1963. * Lookup tail of our authority chain in the namecache.
  1964. *
  1965. * @param rh query we are processing
  1966. */
  1967. static void
  1968. recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
  1969. {
  1970. struct AuthorityChain *ac = rh->ac_tail;
  1971. struct GNUNET_HashCode query;
  1972. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1973. "Starting GNS resolution for `%s' in zone %s\n",
  1974. ac->label,
  1975. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  1976. GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
  1977. ac->label,
  1978. &query);
  1979. if (GNUNET_YES == use_cache)
  1980. {
  1981. rh->namecache_qe
  1982. = GNUNET_NAMECACHE_lookup_block (namecache_handle,
  1983. &query,
  1984. &handle_namecache_block_response,
  1985. rh);
  1986. GNUNET_assert (NULL != rh->namecache_qe);
  1987. }
  1988. else
  1989. {
  1990. start_dht_request (rh, &query);
  1991. }
  1992. }
  1993. /**
  1994. * Function called with the result from a revocation check.
  1995. *
  1996. * @param cls the `struct GNS_ResovlerHandle`
  1997. * @param is_valid #GNUNET_YES if the zone was not yet revoked
  1998. */
  1999. static void
  2000. handle_revocation_result (void *cls,
  2001. int is_valid)
  2002. {
  2003. struct GNS_ResolverHandle *rh = cls;
  2004. struct AuthorityChain *ac = rh->ac_tail;
  2005. rh->rev_check = NULL;
  2006. if (GNUNET_YES != is_valid)
  2007. {
  2008. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2009. _("Zone %s was revoked, resolution fails\n"),
  2010. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  2011. rh->proc (rh->proc_cls, 0, NULL);
  2012. GNS_resolver_lookup_cancel (rh);
  2013. return;
  2014. }
  2015. recursive_gns_resolution_namecache (rh);
  2016. }
  2017. /**
  2018. * Perform revocation check on tail of our authority chain.
  2019. *
  2020. * @param rh query we are processing
  2021. */
  2022. static void
  2023. recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
  2024. {
  2025. struct AuthorityChain *ac = rh->ac_tail;
  2026. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2027. "Starting revocation check for zone %s\n",
  2028. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  2029. rh->rev_check = GNUNET_REVOCATION_query (cfg,
  2030. &ac->authority_info.gns_authority,
  2031. &handle_revocation_result,
  2032. rh);
  2033. GNUNET_assert (NULL != rh->rev_check);
  2034. }
  2035. /**
  2036. * Task scheduled to continue with the resolution process.
  2037. *
  2038. * @param cls the `struct GNS_ResolverHandle` of the resolution
  2039. * @param tc task context
  2040. */
  2041. static void
  2042. recursive_resolution (void *cls,
  2043. const struct GNUNET_SCHEDULER_TaskContext *tc)
  2044. {
  2045. struct GNS_ResolverHandle *rh = cls;
  2046. rh->task_id = GNUNET_SCHEDULER_NO_TASK;
  2047. if (MAX_RECURSION < rh->loop_limiter++)
  2048. {
  2049. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2050. "Encountered unbounded recursion resolving `%s'\n",
  2051. rh->name);
  2052. rh->proc (rh->proc_cls, 0, NULL);
  2053. GNS_resolver_lookup_cancel (rh);
  2054. return;
  2055. }
  2056. if (GNUNET_YES == rh->ac_tail->gns_authority)
  2057. recursive_gns_resolution_revocation (rh);
  2058. else
  2059. recursive_dns_resolution (rh);
  2060. }
  2061. /**
  2062. * Begin the resolution process from 'name', starting with
  2063. * the identification of the zone specified by 'name'.
  2064. *
  2065. * @param rh resolution to perform
  2066. */
  2067. static void
  2068. start_resolver_lookup (struct GNS_ResolverHandle *rh)
  2069. {
  2070. struct AuthorityChain *ac;
  2071. char *y;
  2072. struct in_addr v4;
  2073. struct in6_addr v6;
  2074. if (1 == inet_pton (AF_INET,
  2075. rh->name,
  2076. &v4))
  2077. {
  2078. /* name is IPv4 address, pretend it's an A record */
  2079. struct GNUNET_GNSRECORD_Data rd;
  2080. rd.data = &v4;
  2081. rd.data_size = sizeof (v4);
  2082. rd.expiration_time = UINT64_MAX;
  2083. rd.record_type = GNUNET_DNSPARSER_TYPE_A;
  2084. rd.flags = 0;
  2085. rh->proc (rh->proc_cls, 1, &rd);
  2086. GNS_resolver_lookup_cancel (rh);
  2087. return;
  2088. }
  2089. if (1 == inet_pton (AF_INET6,
  2090. rh->name,
  2091. &v6))
  2092. {
  2093. /* name is IPv6 address, pretend it's an AAAA record */
  2094. struct GNUNET_GNSRECORD_Data rd;
  2095. rd.data = &v6;
  2096. rd.data_size = sizeof (v6);
  2097. rd.expiration_time = UINT64_MAX;
  2098. rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
  2099. rd.flags = 0;
  2100. rh->proc (rh->proc_cls, 1, &rd);
  2101. GNS_resolver_lookup_cancel (rh);
  2102. return;
  2103. }
  2104. if ( ( (GNUNET_YES == is_canonical (rh->name)) &&
  2105. (0 != strcmp (GNUNET_GNS_TLD, rh->name)) ) ||
  2106. ( (GNUNET_YES != is_gnu_tld (rh->name)) &&
  2107. (GNUNET_YES != is_zkey_tld (rh->name)) ) )
  2108. {
  2109. /* use standard DNS lookup */
  2110. int af;
  2111. switch (rh->record_type)
  2112. {
  2113. case GNUNET_DNSPARSER_TYPE_A:
  2114. af = AF_INET;
  2115. break;
  2116. case GNUNET_DNSPARSER_TYPE_AAAA:
  2117. af = AF_INET6;
  2118. break;
  2119. default:
  2120. af = AF_UNSPEC;
  2121. break;
  2122. }
  2123. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2124. "Doing standard DNS lookup for `%s'\n",
  2125. rh->name);
  2126. rh->std_resolve = GNUNET_RESOLVER_ip_get (rh->name,
  2127. af,
  2128. DNS_LOOKUP_TIMEOUT,
  2129. &handle_dns_result,
  2130. rh);
  2131. return;
  2132. }
  2133. if (is_zkey_tld (rh->name))
  2134. {
  2135. /* Name ends with ".zkey", try to replace authority zone with zkey
  2136. authority */
  2137. GNUNET_free (resolver_lookup_get_next_label (rh)); /* will return "zkey" */
  2138. y = resolver_lookup_get_next_label (rh); /* will return 'y' coordinate */
  2139. if ( (NULL == y) ||
  2140. (GNUNET_OK !=
  2141. GNUNET_CRYPTO_ecdsa_public_key_from_string (y,
  2142. strlen (y),
  2143. &rh->authority_zone)) )
  2144. {
  2145. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2146. _("Hostname `%s' is not well-formed, resolution fails\n"),
  2147. rh->name);
  2148. rh->task_id = GNUNET_SCHEDULER_add_now (&fail_resolution, rh);
  2149. }
  2150. GNUNET_free_non_null (y);
  2151. }
  2152. else
  2153. {
  2154. /* Name ends with ".gnu", eat ".gnu" and continue with resolution */
  2155. GNUNET_free (resolver_lookup_get_next_label (rh));
  2156. }
  2157. ac = GNUNET_new (struct AuthorityChain);
  2158. ac->rh = rh;
  2159. ac->label = resolver_lookup_get_next_label (rh);
  2160. ac->suggested_shortening_label = NULL;
  2161. if (NULL == ac->label)
  2162. /* name was just "gnu", so we default to label '+' */
  2163. ac->label = GNUNET_strdup (GNUNET_GNS_MASTERZONE_STR);
  2164. ac->gns_authority = GNUNET_YES;
  2165. ac->authority_info.gns_authority = rh->authority_zone;
  2166. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  2167. rh->ac_tail,
  2168. ac);
  2169. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  2170. rh);
  2171. }
  2172. /**
  2173. * Lookup of a record in a specific zone calls lookup result processor
  2174. * on result.
  2175. *
  2176. * @param zone the zone to perform the lookup in
  2177. * @param record_type the record type to look up
  2178. * @param name the name to look up
  2179. * @param shorten_key a private key for use with PSEU import (can be NULL)
  2180. * @param options local options to control local lookup
  2181. * @param proc the processor to call on result
  2182. * @param proc_cls the closure to pass to @a proc
  2183. * @return handle to cancel operation
  2184. */
  2185. struct GNS_ResolverHandle *
  2186. GNS_resolver_lookup (const struct GNUNET_CRYPTO_EcdsaPublicKey *zone,
  2187. uint32_t record_type,
  2188. const char *name,
  2189. const struct GNUNET_CRYPTO_EcdsaPrivateKey *shorten_key,
  2190. enum GNUNET_GNS_LocalOptions options,
  2191. GNS_ResultProcessor proc, void *proc_cls)
  2192. {
  2193. struct GNS_ResolverHandle *rh;
  2194. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2195. (NULL == shorten_key)
  2196. ? "Starting lookup for `%s' with shortening disabled\n"
  2197. : "Starting lookup for `%s' with shortening enabled\n",
  2198. name);
  2199. rh = GNUNET_new (struct GNS_ResolverHandle);
  2200. GNUNET_CONTAINER_DLL_insert (rlh_head,
  2201. rlh_tail,
  2202. rh);
  2203. rh->authority_zone = *zone;
  2204. rh->proc = proc;
  2205. rh->proc_cls = proc_cls;
  2206. rh->options = options;
  2207. rh->record_type = record_type;
  2208. rh->name = GNUNET_strdup (name);
  2209. rh->name_resolution_pos = strlen (name);
  2210. if (NULL != shorten_key)
  2211. {
  2212. rh->shorten_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
  2213. *rh->shorten_key = *shorten_key;
  2214. }
  2215. start_resolver_lookup (rh);
  2216. return rh;
  2217. }
  2218. /**
  2219. * Cancel active resolution (i.e. client disconnected).
  2220. *
  2221. * @param rh resolution to abort
  2222. */
  2223. void
  2224. GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
  2225. {
  2226. struct DnsResult *dr;
  2227. struct AuthorityChain *ac;
  2228. struct VpnContext *vpn_ctx;
  2229. GNUNET_CONTAINER_DLL_remove (rlh_head,
  2230. rlh_tail,
  2231. rh);
  2232. while (NULL != (ac = rh->ac_head))
  2233. {
  2234. GNUNET_CONTAINER_DLL_remove (rh->ac_head,
  2235. rh->ac_tail,
  2236. ac);
  2237. GNUNET_free (ac->label);
  2238. GNUNET_free_non_null (ac->suggested_shortening_label);
  2239. GNUNET_free (ac);
  2240. }
  2241. if (NULL != rh->g2dc)
  2242. {
  2243. /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
  2244. using GNS_resolver_lookup_cancel here, we need to
  2245. add it first... */
  2246. if (NULL != rh->g2dc->rh)
  2247. {
  2248. GNUNET_CONTAINER_DLL_insert (rlh_head,
  2249. rlh_tail,
  2250. rh->g2dc->rh);
  2251. GNS_resolver_lookup_cancel (rh->g2dc->rh);
  2252. rh->g2dc->rh = NULL;
  2253. }
  2254. GNUNET_free (rh->g2dc->ns);
  2255. GNUNET_free (rh->g2dc);
  2256. rh->g2dc = NULL;
  2257. }
  2258. if (GNUNET_SCHEDULER_NO_TASK != rh->task_id)
  2259. {
  2260. GNUNET_SCHEDULER_cancel (rh->task_id);
  2261. rh->task_id = GNUNET_SCHEDULER_NO_TASK;
  2262. }
  2263. if (NULL != rh->get_handle)
  2264. {
  2265. GNUNET_DHT_get_stop (rh->get_handle);
  2266. rh->get_handle = NULL;
  2267. }
  2268. if (NULL != rh->dht_heap_node)
  2269. {
  2270. GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
  2271. rh->dht_heap_node = NULL;
  2272. }
  2273. if (NULL != (vpn_ctx = rh->vpn_ctx))
  2274. {
  2275. GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
  2276. GNUNET_free (vpn_ctx->rd_data);
  2277. GNUNET_free (vpn_ctx);
  2278. }
  2279. if (NULL != rh->dns_request)
  2280. {
  2281. GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
  2282. rh->dns_request = NULL;
  2283. }
  2284. if (NULL != rh->namecache_qe)
  2285. {
  2286. GNUNET_NAMECACHE_cancel (rh->namecache_qe);
  2287. rh->namecache_qe = NULL;
  2288. }
  2289. if (NULL != rh->rev_check)
  2290. {
  2291. GNUNET_REVOCATION_query_cancel (rh->rev_check);
  2292. rh->rev_check = NULL;
  2293. }
  2294. if (NULL != rh->std_resolve)
  2295. {
  2296. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2297. "Canceling standard DNS resolution\n");
  2298. GNUNET_RESOLVER_request_cancel (rh->std_resolve);
  2299. rh->std_resolve = NULL;
  2300. }
  2301. while (NULL != (dr = rh->dns_result_head))
  2302. {
  2303. GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
  2304. rh->dns_result_tail,
  2305. dr);
  2306. GNUNET_free (dr);
  2307. }
  2308. GNUNET_free_non_null (rh->shorten_key);
  2309. GNUNET_free (rh->name);
  2310. GNUNET_free (rh);
  2311. }
  2312. /* ***************** Resolver initialization ********************* */
  2313. /**
  2314. * Initialize the resolver
  2315. *
  2316. * @param nc the namecache handle
  2317. * @param dht the dht handle
  2318. * @param c configuration handle
  2319. * @param max_bg_queries maximum number of parallel background queries in dht
  2320. */
  2321. void
  2322. GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
  2323. struct GNUNET_DHT_Handle *dht,
  2324. const struct GNUNET_CONFIGURATION_Handle *c,
  2325. unsigned long long max_bg_queries)
  2326. {
  2327. char *dns_ip;
  2328. cfg = c;
  2329. namecache_handle = nc;
  2330. dht_handle = dht;
  2331. dht_lookup_heap =
  2332. GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  2333. max_allowed_background_queries = max_bg_queries;
  2334. if (GNUNET_SYSERR == (use_cache = GNUNET_CONFIGURATION_get_value_yesno (c,
  2335. "gns",
  2336. "USE_CACHE")))
  2337. use_cache = GNUNET_YES;
  2338. if (GNUNET_NO == use_cache)
  2339. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Namecache disabled\n");
  2340. if (GNUNET_OK !=
  2341. GNUNET_CONFIGURATION_get_value_string (c,
  2342. "gns",
  2343. "DNS_RESOLVER",
  2344. &dns_ip))
  2345. {
  2346. /* user did not specify DNS resolver, use 8.8.8.8 */
  2347. dns_ip = GNUNET_strdup ("8.8.8.8");
  2348. }
  2349. dns_handle = GNUNET_DNSSTUB_start (dns_ip);
  2350. GNUNET_free (dns_ip);
  2351. vpn_handle = GNUNET_VPN_connect (cfg);
  2352. }
  2353. /**
  2354. * Shutdown resolver
  2355. */
  2356. void
  2357. GNS_resolver_done ()
  2358. {
  2359. struct GNS_ResolverHandle *rh;
  2360. struct CacheOps *co;
  2361. /* abort active resolutions */
  2362. while (NULL != (rh = rlh_head))
  2363. {
  2364. rh->proc (rh->proc_cls, 0, NULL);
  2365. GNS_resolver_lookup_cancel (rh);
  2366. }
  2367. while (NULL != (co = co_head))
  2368. {
  2369. GNUNET_CONTAINER_DLL_remove (co_head,
  2370. co_tail,
  2371. co);
  2372. GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
  2373. GNUNET_free (co);
  2374. }
  2375. GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
  2376. dht_lookup_heap = NULL;
  2377. GNUNET_DNSSTUB_stop (dns_handle);
  2378. dns_handle = NULL;
  2379. GNUNET_VPN_disconnect (vpn_handle);
  2380. vpn_handle = NULL;
  2381. dht_handle = NULL;
  2382. namecache_handle = NULL;
  2383. }
  2384. /* *************** common helper functions (do not really belong here) *********** */
  2385. /**
  2386. * Checks if @a name ends in ".TLD"
  2387. *
  2388. * @param name the name to check
  2389. * @param tld the TLD to check for
  2390. * @return GNUNET_YES or GNUNET_NO
  2391. */
  2392. int
  2393. is_tld (const char* name, const char* tld)
  2394. {
  2395. size_t offset = 0;
  2396. if (strlen (name) <= strlen (tld))
  2397. return GNUNET_NO;
  2398. offset = strlen (name) - strlen (tld);
  2399. if (0 != strcmp (name + offset, tld))
  2400. return GNUNET_NO;
  2401. return GNUNET_YES;
  2402. }
  2403. /* end of gnunet-service-gns_resolver.c */