gnunet-service-gns_resolver.c 88 KB

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