gnunet-service-gns_resolver.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  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 currently 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_IDENTITY_PublicKey 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 currently 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_IDENTITY_PublicKey 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 (e.g. 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 (like _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 cumulative 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. rd[i].flags = GNUNET_GNSRECORD_RF_NONE;
  655. /**
  656. * If this is a LEHO, we added this before. It must be a supplemental
  657. * record #LSD0001
  658. */
  659. if (GNUNET_GNSRECORD_TYPE_LEHO == rd[i].record_type)
  660. rd[i].flags |= GNUNET_GNSRECORD_RF_SUPPLEMENTAL;
  661. if (0 == pos->expiration_time)
  662. {
  663. rd[i].flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;
  664. rd[i].expiration_time = 0;
  665. }
  666. else
  667. {
  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_IDENTITY_PublicKey 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. (int) (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 (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. struct GNUNET_IDENTITY_PublicKey auth;
  1523. /* delegation to another zone */
  1524. if (GNUNET_OK != GNUNET_GNSRECORD_identity_from_data (rd->data,
  1525. rd->data_size,
  1526. rd->record_type,
  1527. &auth))
  1528. {
  1529. GNUNET_break_op (0);
  1530. fail_resolution (rh);
  1531. return;
  1532. }
  1533. /* expand authority chain */
  1534. ac = GNUNET_new (struct AuthorityChain);
  1535. ac->rh = rh;
  1536. ac->gns_authority = GNUNET_YES;
  1537. ac->authority_info.gns_authority = auth;
  1538. ac->label = resolver_lookup_get_next_label (rh);
  1539. /* add AC to tail */
  1540. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  1541. rh->ac_tail,
  1542. ac);
  1543. /* recurse */
  1544. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  1545. rh);
  1546. }
  1547. /**
  1548. * We found one or more GNS2DNS records, perform recursive resolution on it.
  1549. * (to be precise, one or more records in @a rd is GNS2DNS, there may be others,
  1550. * so this function still needs to check which ones are GNS2DNS).
  1551. *
  1552. * @param rh resolution handle
  1553. * @param rd_count length of the @a rd array
  1554. * @param rd record with PKEY to resolve recursively
  1555. * @return #GNUNET_OK if this worked, #GNUNET_SYSERR if no GNS2DNS records were in @a rd
  1556. */
  1557. static int
  1558. recursive_gns2dns_resolution (struct GNS_ResolverHandle *rh,
  1559. unsigned int rd_count,
  1560. const struct GNUNET_GNSRECORD_Data *rd)
  1561. {
  1562. struct AuthorityChain *ac;
  1563. const char *tld;
  1564. char *ns;
  1565. ns = NULL;
  1566. /* expand authority chain */
  1567. ac = GNUNET_new (struct AuthorityChain);
  1568. ac->rh = rh;
  1569. ac->authority_info.dns_authority.dns_handle = GNUNET_DNSSTUB_start (4);
  1570. for (unsigned int i = 0; i < rd_count; i++)
  1571. {
  1572. char *ip;
  1573. char *n;
  1574. size_t off;
  1575. struct Gns2DnsPending *gp;
  1576. struct GNUNET_IDENTITY_PublicKey zone;
  1577. struct sockaddr_in v4;
  1578. struct sockaddr_in6 v6;
  1579. if (GNUNET_GNSRECORD_TYPE_GNS2DNS != rd[i].record_type)
  1580. {
  1581. /**
  1582. * Records other than GNS2DNS not allowed
  1583. */
  1584. if (NULL != ns)
  1585. GNUNET_free (ns);
  1586. return GNUNET_SYSERR;
  1587. }
  1588. off = 0;
  1589. n = GNUNET_DNSPARSER_parse_name (rd[i].data,
  1590. rd[i].data_size,
  1591. &off);
  1592. ip = GNUNET_strdup (&((const char *) rd[i].data)[off]);
  1593. if ((NULL == n) ||
  1594. (NULL == ip))
  1595. {
  1596. GNUNET_break_op (0);
  1597. if (NULL != n)
  1598. GNUNET_free (n);
  1599. if (NULL != ip)
  1600. GNUNET_free (ip);
  1601. continue;
  1602. }
  1603. off += strlen (ip) + 1;
  1604. if (off != rd[i].data_size)
  1605. {
  1606. GNUNET_break_op (0);
  1607. continue;
  1608. }
  1609. /* resolve 'ip' to determine the IP(s) of the DNS
  1610. resolver to use for lookup of 'ns' */
  1611. if (NULL != ns)
  1612. {
  1613. if (0 != strcasecmp (ns,
  1614. n))
  1615. {
  1616. /* NS values must all be the same for all GNS2DNS records,
  1617. anything else leads to insanity */
  1618. GNUNET_break_op (0);
  1619. GNUNET_free (n);
  1620. GNUNET_free (ip);
  1621. continue;
  1622. }
  1623. GNUNET_free (n);
  1624. }
  1625. else
  1626. {
  1627. ns = n;
  1628. }
  1629. /* check if 'ip' is already an IPv4/IPv6 address */
  1630. if ((1 == inet_pton (AF_INET,
  1631. ip,
  1632. &v4)) ||
  1633. (1 == inet_pton (AF_INET6,
  1634. ip,
  1635. &v6)))
  1636. {
  1637. GNUNET_break (GNUNET_OK ==
  1638. GNUNET_DNSSTUB_add_dns_ip (
  1639. ac->authority_info.dns_authority.dns_handle,
  1640. ip));
  1641. ac->authority_info.dns_authority.found = GNUNET_YES;
  1642. GNUNET_free (ip);
  1643. continue;
  1644. }
  1645. tld = GNS_get_tld (ip);
  1646. if ((0 != strcmp (tld, "+")) &&
  1647. (GNUNET_OK != GNUNET_GNSRECORD_zkey_to_pkey (tld, &zone)))
  1648. {
  1649. /* 'ip' is a DNS name */
  1650. gp = GNUNET_new (struct Gns2DnsPending);
  1651. gp->ac = ac;
  1652. GNUNET_CONTAINER_DLL_insert (ac->authority_info.dns_authority.gp_head,
  1653. ac->authority_info.dns_authority.gp_tail,
  1654. gp);
  1655. gp->dns_rh = GNUNET_RESOLVER_ip_get (ip,
  1656. AF_UNSPEC,
  1657. GNUNET_TIME_UNIT_FOREVER_REL,
  1658. &handle_gns2dns_ip,
  1659. gp);
  1660. GNUNET_free (ip);
  1661. continue;
  1662. }
  1663. /* 'ip' should be a GNS name */
  1664. gp = GNUNET_new (struct Gns2DnsPending);
  1665. gp->ac = ac;
  1666. GNUNET_CONTAINER_DLL_insert (ac->authority_info.dns_authority.gp_head,
  1667. ac->authority_info.dns_authority.gp_tail,
  1668. gp);
  1669. gp->rh = GNUNET_new (struct GNS_ResolverHandle);
  1670. if (0 == strcmp (tld, "+"))
  1671. {
  1672. ip = translate_dot_plus (rh,
  1673. ip);
  1674. tld = GNS_get_tld (ip);
  1675. if (GNUNET_OK !=
  1676. GNUNET_GNSRECORD_zkey_to_pkey (tld,
  1677. &zone))
  1678. {
  1679. GNUNET_break_op (0);
  1680. GNUNET_free (ip);
  1681. continue;
  1682. }
  1683. }
  1684. gp->rh->authority_zone = zone;
  1685. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1686. "Resolving `%s' to determine IP address of DNS server for GNS2DNS transition for `%s'\n",
  1687. ip,
  1688. ns);
  1689. gp->rh->name = ip;
  1690. gp->rh->name_resolution_pos = strlen (ip) - strlen (tld) - 1;
  1691. gp->rh->proc = &handle_gns2dns_result;
  1692. gp->rh->proc_cls = gp;
  1693. gp->rh->record_type = GNUNET_GNSRECORD_TYPE_ANY;
  1694. gp->rh->options = GNUNET_GNS_LO_DEFAULT;
  1695. gp->rh->loop_limiter = rh->loop_limiter + 1;
  1696. gp->rh->loop_threshold = rh->loop_threshold;
  1697. gp->rh->task_id
  1698. = GNUNET_SCHEDULER_add_now (&start_resolver_lookup,
  1699. gp->rh);
  1700. } /* end 'for all records' */
  1701. if (NULL == ns)
  1702. {
  1703. /* not a single GNS2DNS record found */
  1704. GNUNET_free (ac);
  1705. return GNUNET_SYSERR;
  1706. }
  1707. GNUNET_assert (strlen (ns) <= GNUNET_DNSPARSER_MAX_NAME_LENGTH);
  1708. strcpy (ac->authority_info.dns_authority.name,
  1709. ns);
  1710. /* for DNS recursion, the label is the full DNS name,
  1711. created from the remainder of the GNS name and the
  1712. name in the NS record */
  1713. GNUNET_asprintf (&ac->label,
  1714. "%.*s%s%s",
  1715. (int) rh->name_resolution_pos,
  1716. rh->name,
  1717. (0 != rh->name_resolution_pos) ? "." : "",
  1718. ns);
  1719. GNUNET_free (ns);
  1720. {
  1721. /* the GNS name is UTF-8 and may include multibyte chars.
  1722. * We have to convert the combined name to a DNS-compatible IDNA.
  1723. */
  1724. char *tmp = ac->label;
  1725. if (IDNA_SUCCESS != idna_to_ascii_8z (tmp,
  1726. &ac->label,
  1727. IDNA_ALLOW_UNASSIGNED))
  1728. {
  1729. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1730. _ ("Name `%s' cannot be converted to IDNA."),
  1731. tmp);
  1732. GNUNET_free (tmp);
  1733. GNUNET_free (ac);
  1734. return GNUNET_SYSERR;
  1735. }
  1736. GNUNET_free (tmp);
  1737. }
  1738. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  1739. rh->ac_tail,
  1740. ac);
  1741. if (strlen (ac->label) > GNUNET_DNSPARSER_MAX_NAME_LENGTH)
  1742. {
  1743. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1744. _ ("GNS lookup resulted in DNS name that is too long (`%s')\n"),
  1745. ac->label);
  1746. GNUNET_free (ac->label);
  1747. GNUNET_free (ac);
  1748. return GNUNET_SYSERR;
  1749. }
  1750. continue_with_gns2dns (ac);
  1751. return GNUNET_OK;
  1752. }
  1753. /**
  1754. * Process a records that were decrypted from a block.
  1755. *
  1756. * @param cls closure with the `struct GNS_ResolverHandle`
  1757. * @param rd_count number of entries in @a rd array
  1758. * @param rd array of records with data to store
  1759. */
  1760. static void
  1761. handle_gns_resolution_result (void *cls,
  1762. unsigned int rd_count,
  1763. const struct GNUNET_GNSRECORD_Data *rd)
  1764. {
  1765. struct GNS_ResolverHandle *rh = cls;
  1766. char *cname;
  1767. struct VpnContext *vpn_ctx;
  1768. const struct GNUNET_TUN_GnsVpnRecord *vpn;
  1769. const char *vname;
  1770. struct GNUNET_HashCode vhash;
  1771. int af;
  1772. char scratch[UINT16_MAX];
  1773. size_t scratch_off;
  1774. size_t scratch_start;
  1775. size_t off;
  1776. struct GNUNET_GNSRECORD_Data rd_new[rd_count];
  1777. unsigned int rd_off;
  1778. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  1779. "Resolution succeeded for `%s' in zone %s, got %u records\n",
  1780. rh->ac_tail->label,
  1781. GNUNET_GNSRECORD_z2s (&rh->ac_tail->authority_info.gns_authority),
  1782. rd_count);
  1783. if (0 == rd_count)
  1784. {
  1785. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  1786. _ ("GNS lookup failed (zero records found for `%s')\n"),
  1787. rh->name);
  1788. fail_resolution (rh);
  1789. return;
  1790. }
  1791. if (0 == rh->name_resolution_pos)
  1792. {
  1793. /* top-level match, are we done yet? */
  1794. if ((rd_count > 0) &&
  1795. (GNUNET_DNSPARSER_TYPE_CNAME == rd[0].record_type) &&
  1796. (GNUNET_DNSPARSER_TYPE_CNAME != rh->record_type))
  1797. {
  1798. off = 0;
  1799. cname = GNUNET_DNSPARSER_parse_name (rd[0].data,
  1800. rd[0].data_size,
  1801. &off);
  1802. if ((NULL == cname) ||
  1803. (off != rd[0].data_size))
  1804. {
  1805. GNUNET_break_op (0);
  1806. GNUNET_free (cname);
  1807. fail_resolution (rh);
  1808. return;
  1809. }
  1810. handle_gns_cname_result (rh,
  1811. cname);
  1812. GNUNET_free (cname);
  1813. return;
  1814. }
  1815. /* If A/AAAA was requested, but we got a VPN
  1816. record, we convert it to A/AAAA using GNUnet VPN */
  1817. if ((GNUNET_DNSPARSER_TYPE_A == rh->record_type) ||
  1818. (GNUNET_DNSPARSER_TYPE_AAAA == rh->record_type))
  1819. {
  1820. for (unsigned int i = 0; i < rd_count; i++)
  1821. {
  1822. switch (rd[i].record_type)
  1823. {
  1824. case GNUNET_GNSRECORD_TYPE_VPN:
  1825. {
  1826. af = (GNUNET_DNSPARSER_TYPE_A == rh->record_type) ? AF_INET :
  1827. AF_INET6;
  1828. if (sizeof(struct GNUNET_TUN_GnsVpnRecord) >
  1829. rd[i].data_size)
  1830. {
  1831. GNUNET_break_op (0);
  1832. fail_resolution (rh);
  1833. return;
  1834. }
  1835. vpn = (const struct GNUNET_TUN_GnsVpnRecord *) rd[i].data;
  1836. vname = (const char *) &vpn[1];
  1837. if ('\0' != vname[rd[i].data_size - 1 - sizeof(struct
  1838. GNUNET_TUN_GnsVpnRecord)
  1839. ])
  1840. {
  1841. GNUNET_break_op (0);
  1842. fail_resolution (rh);
  1843. return;
  1844. }
  1845. GNUNET_TUN_service_name_to_hash (vname,
  1846. &vhash);
  1847. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1848. "Attempting VPN allocation for %s-%s (AF: %d, proto %d)\n",
  1849. GNUNET_i2s (&vpn->peer),
  1850. vname,
  1851. (int) af,
  1852. (int) ntohs (vpn->proto));
  1853. vpn_ctx = GNUNET_new (struct VpnContext);
  1854. rh->vpn_ctx = vpn_ctx;
  1855. vpn_ctx->rh = rh;
  1856. vpn_ctx->rd_data_size = GNUNET_GNSRECORD_records_get_size (rd_count,
  1857. rd);
  1858. if (vpn_ctx->rd_data_size < 0)
  1859. {
  1860. GNUNET_break_op (0);
  1861. GNUNET_free (vpn_ctx);
  1862. fail_resolution (rh);
  1863. return;
  1864. }
  1865. vpn_ctx->rd_data = GNUNET_malloc ((size_t) vpn_ctx->rd_data_size);
  1866. vpn_ctx->rd_count = rd_count;
  1867. GNUNET_assert (vpn_ctx->rd_data_size ==
  1868. GNUNET_GNSRECORD_records_serialize (rd_count,
  1869. rd,
  1870. (size_t) vpn_ctx
  1871. ->rd_data_size,
  1872. vpn_ctx->rd_data));
  1873. vpn_ctx->vpn_request = GNUNET_VPN_redirect_to_peer (vpn_handle,
  1874. af,
  1875. ntohs (
  1876. vpn->proto),
  1877. &vpn->peer,
  1878. &vhash,
  1879. GNUNET_TIME_relative_to_absolute (
  1880. VPN_TIMEOUT),
  1881. &
  1882. vpn_allocation_cb,
  1883. vpn_ctx);
  1884. return;
  1885. }
  1886. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  1887. {
  1888. /* delegation to DNS */
  1889. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  1890. "Found GNS2DNS record, delegating to DNS!\n");
  1891. if (GNUNET_OK ==
  1892. recursive_gns2dns_resolution (rh,
  1893. rd_count,
  1894. rd))
  1895. return;
  1896. else
  1897. goto fail;
  1898. }
  1899. default:
  1900. break;
  1901. } /* end: switch */
  1902. } /* end: for rd */
  1903. } /* end: name_resolution_pos */
  1904. /* convert relative names in record values to absolute names,
  1905. using 'scratch' array for memory allocations */
  1906. scratch_off = 0;
  1907. rd_off = 0;
  1908. for (unsigned int i = 0; i < rd_count; i++)
  1909. {
  1910. GNUNET_assert (rd_off <= i);
  1911. if ((0 != rh->protocol) &&
  1912. (0 != rh->service) &&
  1913. (GNUNET_GNSRECORD_TYPE_BOX != rd[i].record_type))
  1914. continue; /* we _only_ care about boxed records */
  1915. GNUNET_assert (rd_off < rd_count);
  1916. rd_new[rd_off] = rd[i];
  1917. /* Check if the embedded name(s) end in "+", and if so,
  1918. replace the "+" with the zone at "ac_tail", changing the name
  1919. to a ".ZONEKEY". The name is allocated on the 'scratch' array,
  1920. so we can free it afterwards. */
  1921. switch (rd[i].record_type)
  1922. {
  1923. case GNUNET_DNSPARSER_TYPE_CNAME:
  1924. {
  1925. char *cname;
  1926. off = 0;
  1927. cname = GNUNET_DNSPARSER_parse_name (rd[i].data,
  1928. rd[i].data_size,
  1929. &off);
  1930. if ((NULL == cname) ||
  1931. (off != rd[i].data_size))
  1932. {
  1933. GNUNET_break_op (0); /* record not well-formed */
  1934. }
  1935. else
  1936. {
  1937. cname = translate_dot_plus (rh, cname);
  1938. GNUNET_break (NULL != cname);
  1939. scratch_start = scratch_off;
  1940. if (GNUNET_OK !=
  1941. GNUNET_DNSPARSER_builder_add_name (scratch,
  1942. sizeof(scratch),
  1943. &scratch_off,
  1944. cname))
  1945. {
  1946. GNUNET_break (0);
  1947. }
  1948. else
  1949. {
  1950. GNUNET_assert (rd_off < rd_count);
  1951. rd_new[rd_off].data = &scratch[scratch_start];
  1952. rd_new[rd_off].data_size = scratch_off - scratch_start;
  1953. rd_off++;
  1954. }
  1955. }
  1956. GNUNET_free (cname);
  1957. }
  1958. break;
  1959. case GNUNET_DNSPARSER_TYPE_SOA:
  1960. {
  1961. struct GNUNET_DNSPARSER_SoaRecord *soa;
  1962. off = 0;
  1963. soa = GNUNET_DNSPARSER_parse_soa (rd[i].data,
  1964. rd[i].data_size,
  1965. &off);
  1966. if ((NULL == soa) ||
  1967. (off != rd[i].data_size))
  1968. {
  1969. GNUNET_break_op (0); /* record not well-formed */
  1970. }
  1971. else
  1972. {
  1973. soa->mname = translate_dot_plus (rh, soa->mname);
  1974. soa->rname = translate_dot_plus (rh, soa->rname);
  1975. scratch_start = scratch_off;
  1976. if (GNUNET_OK !=
  1977. GNUNET_DNSPARSER_builder_add_soa (scratch,
  1978. sizeof(scratch),
  1979. &scratch_off,
  1980. soa))
  1981. {
  1982. GNUNET_break (0);
  1983. }
  1984. else
  1985. {
  1986. GNUNET_assert (rd_off < rd_count);
  1987. rd_new[rd_off].data = &scratch[scratch_start];
  1988. rd_new[rd_off].data_size = scratch_off - scratch_start;
  1989. rd_off++;
  1990. }
  1991. }
  1992. if (NULL != soa)
  1993. GNUNET_DNSPARSER_free_soa (soa);
  1994. }
  1995. break;
  1996. case GNUNET_DNSPARSER_TYPE_MX:
  1997. {
  1998. struct GNUNET_DNSPARSER_MxRecord *mx;
  1999. off = 0;
  2000. mx = GNUNET_DNSPARSER_parse_mx (rd[i].data,
  2001. rd[i].data_size,
  2002. &off);
  2003. if ((NULL == mx) ||
  2004. (off != rd[i].data_size))
  2005. {
  2006. GNUNET_break_op (0); /* record not well-formed */
  2007. }
  2008. else
  2009. {
  2010. mx->mxhost = translate_dot_plus (rh, mx->mxhost);
  2011. scratch_start = scratch_off;
  2012. if (GNUNET_OK !=
  2013. GNUNET_DNSPARSER_builder_add_mx (scratch,
  2014. sizeof(scratch),
  2015. &scratch_off,
  2016. mx))
  2017. {
  2018. GNUNET_break (0);
  2019. }
  2020. else
  2021. {
  2022. GNUNET_assert (rd_off < rd_count);
  2023. rd_new[rd_off].data = &scratch[scratch_start];
  2024. rd_new[rd_off].data_size = scratch_off - scratch_start;
  2025. rd_off++;
  2026. }
  2027. }
  2028. if (NULL != mx)
  2029. GNUNET_DNSPARSER_free_mx (mx);
  2030. }
  2031. break;
  2032. case GNUNET_DNSPARSER_TYPE_SRV:
  2033. {
  2034. struct GNUNET_DNSPARSER_SrvRecord *srv;
  2035. off = 0;
  2036. srv = GNUNET_DNSPARSER_parse_srv (rd[i].data,
  2037. rd[i].data_size,
  2038. &off);
  2039. if ((NULL == srv) ||
  2040. (off != rd[i].data_size))
  2041. {
  2042. GNUNET_break_op (0); /* record not well-formed */
  2043. }
  2044. else
  2045. {
  2046. srv->target = translate_dot_plus (rh, srv->target);
  2047. scratch_start = scratch_off;
  2048. if (GNUNET_OK !=
  2049. GNUNET_DNSPARSER_builder_add_srv (scratch,
  2050. sizeof(scratch),
  2051. &scratch_off,
  2052. srv))
  2053. {
  2054. GNUNET_break (0);
  2055. }
  2056. else
  2057. {
  2058. GNUNET_assert (rd_off < rd_count);
  2059. rd_new[rd_off].data = &scratch[scratch_start];
  2060. rd_new[rd_off].data_size = scratch_off - scratch_start;
  2061. rd_off++;
  2062. }
  2063. }
  2064. if (NULL != srv)
  2065. GNUNET_DNSPARSER_free_srv (srv);
  2066. }
  2067. break;
  2068. case GNUNET_GNSRECORD_TYPE_PKEY:
  2069. case GNUNET_GNSRECORD_TYPE_EDKEY:
  2070. {
  2071. struct GNUNET_IDENTITY_PublicKey pubkey;
  2072. if (rd[i].data_size < sizeof(uint32_t))
  2073. {
  2074. GNUNET_break_op (0);
  2075. break;
  2076. }
  2077. if (GNUNET_OK !=
  2078. GNUNET_GNSRECORD_identity_from_data (rd[i].data,
  2079. rd[i].data_size,
  2080. rd[i].record_type,
  2081. &pubkey))
  2082. {
  2083. GNUNET_break_op (0);
  2084. break;
  2085. }
  2086. rd_off++;
  2087. if (rd[i].record_type != rh->record_type)
  2088. {
  2089. /* try to resolve "@" */
  2090. struct AuthorityChain *ac;
  2091. ac = GNUNET_new (struct AuthorityChain);
  2092. ac->rh = rh;
  2093. ac->gns_authority = GNUNET_YES;
  2094. ac->authority_info.gns_authority = pubkey;
  2095. ac->label = GNUNET_strdup (GNUNET_GNS_EMPTY_LABEL_AT);
  2096. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  2097. rh->ac_tail,
  2098. ac);
  2099. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  2100. rh);
  2101. return;
  2102. }
  2103. }
  2104. break;
  2105. case GNUNET_GNSRECORD_TYPE_GNS2DNS:
  2106. {
  2107. /* delegation to DNS */
  2108. if (GNUNET_GNSRECORD_TYPE_GNS2DNS == rh->record_type)
  2109. {
  2110. rd_off++;
  2111. break; /* do not follow to DNS, we wanted the GNS2DNS record! */
  2112. }
  2113. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2114. "Found GNS2DNS record, delegating to DNS!\n");
  2115. if (GNUNET_OK ==
  2116. recursive_gns2dns_resolution (rh,
  2117. rd_count,
  2118. rd))
  2119. return;
  2120. else
  2121. goto fail;
  2122. }
  2123. case GNUNET_GNSRECORD_TYPE_BOX:
  2124. {
  2125. /* unbox SRV/TLSA records if a specific one was requested */
  2126. if ((0 != rh->protocol) &&
  2127. (0 != rh->service) &&
  2128. (rd[i].data_size >= sizeof(struct GNUNET_GNSRECORD_BoxRecord)))
  2129. {
  2130. const struct GNUNET_GNSRECORD_BoxRecord *box;
  2131. box = rd[i].data;
  2132. if ((ntohs (box->protocol) == rh->protocol) &&
  2133. (ntohs (box->service) == rh->service))
  2134. {
  2135. /* Box matches, unbox! */
  2136. GNUNET_assert (rd_off < rd_count);
  2137. rd_new[rd_off].record_type = ntohl (box->record_type);
  2138. rd_new[rd_off].data_size -= sizeof(struct
  2139. GNUNET_GNSRECORD_BoxRecord);
  2140. rd_new[rd_off].data = &box[1];
  2141. rd_off++;
  2142. }
  2143. }
  2144. else
  2145. {
  2146. /* no specific protocol/service specified, preserve all BOX
  2147. records (for modern, GNS-enabled applications) */
  2148. rd_off++;
  2149. }
  2150. break;
  2151. }
  2152. default:
  2153. rd_off++;
  2154. break;
  2155. } /* end: switch */
  2156. } /* end: for rd_count */
  2157. /* yes, we are done, return result */
  2158. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2159. "Returning GNS response for `%s' with %u answers\n",
  2160. rh->ac_tail->label,
  2161. rd_off);
  2162. rh->proc (rh->proc_cls,
  2163. rd_off,
  2164. rd_new);
  2165. rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
  2166. rh);
  2167. return;
  2168. }
  2169. switch (rd[0].record_type)
  2170. {
  2171. case GNUNET_DNSPARSER_TYPE_CNAME:
  2172. GNUNET_break_op (1 == rd_count); /* CNAME should be unique */
  2173. recursive_cname_resolution (rh,
  2174. &rd[0]);
  2175. return;
  2176. case GNUNET_GNSRECORD_TYPE_PKEY:
  2177. case GNUNET_GNSRECORD_TYPE_EDKEY:
  2178. GNUNET_break_op (1 == rd_count); /* PKEY should be unique */
  2179. recursive_pkey_resolution (rh,
  2180. &rd[0]);
  2181. return;
  2182. default:
  2183. if (GNUNET_OK ==
  2184. recursive_gns2dns_resolution (rh,
  2185. rd_count,
  2186. rd))
  2187. return;
  2188. break;
  2189. }
  2190. fail:
  2191. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2192. _ ("GNS lookup recursion failed (no delegation record found)\n"));
  2193. fail_resolution (rh);
  2194. }
  2195. /**
  2196. * Function called once the namestore has completed the request for
  2197. * caching a block.
  2198. *
  2199. * @param cls closure with the `struct CacheOps`
  2200. * @param success #GNUNET_OK on success
  2201. * @param emsg error message
  2202. */
  2203. static void
  2204. namecache_cache_continuation (void *cls,
  2205. int32_t success,
  2206. const char *emsg)
  2207. {
  2208. struct CacheOps *co = cls;
  2209. co->namecache_qe_cache = NULL;
  2210. if (GNUNET_OK != success)
  2211. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2212. _ ("Failed to cache GNS resolution: %s\n"),
  2213. emsg);
  2214. GNUNET_CONTAINER_DLL_remove (co_head,
  2215. co_tail,
  2216. co);
  2217. GNUNET_free (co);
  2218. }
  2219. /**
  2220. * Iterator called on each result obtained for a DHT
  2221. * operation that expects a reply
  2222. *
  2223. * @param cls closure with the `struct GNS_ResolverHandle`
  2224. * @param exp when will this value expire
  2225. * @param key key of the result
  2226. * @param get_path peers on reply path (or NULL if not recorded)
  2227. * [0] = datastore's first neighbor, [length - 1] = local peer
  2228. * @param get_path_length number of entries in @a get_path
  2229. * @param put_path peers on the PUT path (or NULL if not recorded)
  2230. * [0] = origin, [length - 1] = datastore
  2231. * @param put_path_length number of entries in @a put_path
  2232. * @param type type of the result
  2233. * @param size number of bytes in data
  2234. * @param data pointer to the result data
  2235. */
  2236. static void
  2237. handle_dht_response (void *cls,
  2238. struct GNUNET_TIME_Absolute exp,
  2239. const struct GNUNET_HashCode *key,
  2240. const struct GNUNET_PeerIdentity *get_path,
  2241. unsigned int get_path_length,
  2242. const struct GNUNET_PeerIdentity *put_path,
  2243. unsigned int put_path_length,
  2244. enum GNUNET_BLOCK_Type type,
  2245. size_t size,
  2246. const void *data)
  2247. {
  2248. struct GNS_ResolverHandle *rh = cls;
  2249. struct AuthorityChain *ac = rh->ac_tail;
  2250. const struct GNUNET_GNSRECORD_Block *block;
  2251. struct CacheOps *co;
  2252. (void) exp;
  2253. (void) key;
  2254. (void) get_path;
  2255. (void) get_path_length;
  2256. (void) put_path;
  2257. (void) put_path_length;
  2258. (void) type;
  2259. GNUNET_DHT_get_stop (rh->get_handle);
  2260. rh->get_handle = NULL;
  2261. GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
  2262. rh->dht_heap_node = NULL;
  2263. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2264. "Handling response from the DHT\n");
  2265. if (size < sizeof(struct GNUNET_GNSRECORD_Block))
  2266. {
  2267. /* how did this pass DHT block validation!? */
  2268. GNUNET_break (0);
  2269. fail_resolution (rh);
  2270. return;
  2271. }
  2272. block = data;
  2273. if (size != GNUNET_GNSRECORD_block_get_size (block))
  2274. {
  2275. /* how did this pass DHT block validation!? */
  2276. GNUNET_break (0);
  2277. fail_resolution (rh);
  2278. return;
  2279. }
  2280. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2281. "Decrypting DHT block of size %lu for `%s', expires %s\n",
  2282. GNUNET_GNSRECORD_block_get_size (block),
  2283. rh->name,
  2284. GNUNET_STRINGS_absolute_time_to_string (exp));
  2285. if (GNUNET_OK !=
  2286. GNUNET_GNSRECORD_block_decrypt (block,
  2287. &ac->authority_info.gns_authority,
  2288. ac->label,
  2289. &handle_gns_resolution_result,
  2290. rh))
  2291. {
  2292. GNUNET_break_op (0); /* block was ill-formed */
  2293. fail_resolution (rh);
  2294. return;
  2295. }
  2296. if (0 == GNUNET_TIME_absolute_get_remaining (
  2297. GNUNET_GNSRECORD_block_get_expiration (block)).
  2298. rel_value_us)
  2299. {
  2300. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2301. "Received expired block from the DHT, will not cache it.\n");
  2302. return;
  2303. }
  2304. if (GNUNET_YES == disable_cache)
  2305. return;
  2306. /* Cache well-formed blocks */
  2307. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2308. "Caching response from the DHT in namecache\n");
  2309. co = GNUNET_new (struct CacheOps);
  2310. co->namecache_qe_cache = GNUNET_NAMECACHE_block_cache (namecache_handle,
  2311. block,
  2312. &
  2313. namecache_cache_continuation,
  2314. co);
  2315. GNUNET_CONTAINER_DLL_insert (co_head,
  2316. co_tail,
  2317. co);
  2318. }
  2319. /**
  2320. * Initiate a DHT query for a set of GNS records.
  2321. *
  2322. * @param rh resolution handle
  2323. * @param query key to use in the DHT lookup
  2324. */
  2325. static void
  2326. start_dht_request (struct GNS_ResolverHandle *rh,
  2327. const struct GNUNET_HashCode *query)
  2328. {
  2329. struct GNS_ResolverHandle *rx;
  2330. GNUNET_assert (NULL == rh->get_handle);
  2331. rh->get_handle = GNUNET_DHT_get_start (dht_handle,
  2332. GNUNET_BLOCK_TYPE_GNS_NAMERECORD,
  2333. query,
  2334. DHT_GNS_REPLICATION_LEVEL,
  2335. GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
  2336. NULL, 0,
  2337. &handle_dht_response, rh);
  2338. rh->dht_heap_node = GNUNET_CONTAINER_heap_insert (dht_lookup_heap,
  2339. rh,
  2340. GNUNET_TIME_absolute_get ().
  2341. abs_value_us);
  2342. if (GNUNET_CONTAINER_heap_get_size (dht_lookup_heap) >
  2343. max_allowed_background_queries)
  2344. {
  2345. /* fail longest-standing DHT request */
  2346. rx = GNUNET_CONTAINER_heap_remove_root (dht_lookup_heap);
  2347. rx->dht_heap_node = NULL;
  2348. GNUNET_assert (NULL != rx);
  2349. fail_resolution (rx);
  2350. }
  2351. }
  2352. /**
  2353. * Process a records that were decrypted from a block that we got from
  2354. * the namecache. Simply calls #handle_gns_resolution_result().
  2355. *
  2356. * @param cls closure with the `struct GNS_ResolverHandle`
  2357. * @param rd_count number of entries in @a rd array
  2358. * @param rd array of records with data to store
  2359. */
  2360. static void
  2361. handle_gns_namecache_resolution_result (void *cls,
  2362. unsigned int rd_count,
  2363. const struct GNUNET_GNSRECORD_Data *rd)
  2364. {
  2365. struct GNS_ResolverHandle *rh = cls;
  2366. if (0 == rd_count)
  2367. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2368. _ ("GNS namecache returned empty result for `%s'\n"),
  2369. rh->name);
  2370. handle_gns_resolution_result (rh,
  2371. rd_count,
  2372. rd);
  2373. }
  2374. /**
  2375. * Process a record that was stored in the namecache.
  2376. *
  2377. * @param cls closure with the `struct GNS_ResolverHandle`
  2378. * @param block block that was stored in the namecache
  2379. */
  2380. static void
  2381. handle_namecache_block_response (void *cls,
  2382. const struct GNUNET_GNSRECORD_Block *block)
  2383. {
  2384. struct GNS_ResolverHandle *rh = cls;
  2385. struct AuthorityChain *ac = rh->ac_tail;
  2386. const char *label = ac->label;
  2387. const struct GNUNET_IDENTITY_PublicKey *auth =
  2388. &ac->authority_info.gns_authority;
  2389. struct GNUNET_HashCode query;
  2390. GNUNET_assert (NULL != rh->namecache_qe);
  2391. rh->namecache_qe = NULL;
  2392. if (((GNUNET_GNS_LO_DEFAULT == rh->options) ||
  2393. ((GNUNET_GNS_LO_LOCAL_MASTER == rh->options) &&
  2394. (ac != rh->ac_head))) &&
  2395. ((NULL == block) ||
  2396. (0 == GNUNET_TIME_absolute_get_remaining (
  2397. GNUNET_GNSRECORD_block_get_expiration (block)).
  2398. rel_value_us)))
  2399. {
  2400. /* namecache knows nothing; try DHT lookup */
  2401. GNUNET_GNSRECORD_query_from_public_key (auth,
  2402. label,
  2403. &query);
  2404. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2405. "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
  2406. ac->label,
  2407. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
  2408. GNUNET_h2s (&query));
  2409. start_dht_request (rh, &query);
  2410. return;
  2411. }
  2412. if ((NULL == block) ||
  2413. (0 == GNUNET_TIME_absolute_get_remaining (
  2414. GNUNET_GNSRECORD_block_get_expiration (block)).
  2415. rel_value_us))
  2416. {
  2417. /* DHT not permitted and no local result, fail */
  2418. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2419. "Resolution failed for `%s' in zone %s (DHT lookup not permitted by configuration)\n",
  2420. ac->label,
  2421. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  2422. fail_resolution (rh);
  2423. return;
  2424. }
  2425. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2426. "Received result from namecache for label `%s'\n",
  2427. ac->label);
  2428. if (GNUNET_OK !=
  2429. GNUNET_GNSRECORD_block_decrypt (block,
  2430. auth,
  2431. label,
  2432. &handle_gns_namecache_resolution_result,
  2433. rh))
  2434. {
  2435. GNUNET_break_op (0); /* block was ill-formed */
  2436. /* try DHT instead */
  2437. GNUNET_GNSRECORD_query_from_public_key (auth,
  2438. label,
  2439. &query);
  2440. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2441. "Starting DHT lookup for `%s' in zone `%s' under key `%s'\n",
  2442. ac->label,
  2443. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority),
  2444. GNUNET_h2s (&query));
  2445. start_dht_request (rh, &query);
  2446. return;
  2447. }
  2448. }
  2449. /**
  2450. * Lookup tail of our authority chain in the namecache.
  2451. *
  2452. * @param rh query we are processing
  2453. */
  2454. static void
  2455. recursive_gns_resolution_namecache (struct GNS_ResolverHandle *rh)
  2456. {
  2457. struct AuthorityChain *ac = rh->ac_tail;
  2458. struct GNUNET_HashCode query;
  2459. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2460. "Starting GNS resolution for `%s' in zone %s\n",
  2461. ac->label,
  2462. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  2463. GNUNET_GNSRECORD_query_from_public_key (&ac->authority_info.gns_authority,
  2464. ac->label,
  2465. &query);
  2466. if (GNUNET_YES != disable_cache)
  2467. {
  2468. rh->namecache_qe
  2469. = GNUNET_NAMECACHE_lookup_block (namecache_handle,
  2470. &query,
  2471. &handle_namecache_block_response,
  2472. rh);
  2473. GNUNET_assert (NULL != rh->namecache_qe);
  2474. }
  2475. else
  2476. {
  2477. start_dht_request (rh,
  2478. &query);
  2479. }
  2480. }
  2481. /**
  2482. * Function called with the result from a revocation check.
  2483. *
  2484. * @param cls the `struct GNS_ResovlerHandle`
  2485. * @param is_valid #GNUNET_YES if the zone was not yet revoked
  2486. */
  2487. static void
  2488. handle_revocation_result (void *cls,
  2489. int is_valid)
  2490. {
  2491. struct GNS_ResolverHandle *rh = cls;
  2492. struct AuthorityChain *ac = rh->ac_tail;
  2493. rh->rev_check = NULL;
  2494. if (GNUNET_YES != is_valid)
  2495. {
  2496. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2497. _ ("Zone %s was revoked, resolution fails\n"),
  2498. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  2499. fail_resolution (rh);
  2500. return;
  2501. }
  2502. recursive_gns_resolution_namecache (rh);
  2503. }
  2504. /**
  2505. * Perform revocation check on tail of our authority chain.
  2506. *
  2507. * @param rh query we are processing
  2508. */
  2509. static void
  2510. recursive_gns_resolution_revocation (struct GNS_ResolverHandle *rh)
  2511. {
  2512. struct AuthorityChain *ac = rh->ac_tail;
  2513. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2514. "Starting revocation check for zone %s\n",
  2515. GNUNET_GNSRECORD_z2s (&ac->authority_info.gns_authority));
  2516. rh->rev_check = GNUNET_REVOCATION_query (cfg,
  2517. &ac->authority_info.gns_authority,
  2518. &handle_revocation_result,
  2519. rh);
  2520. GNUNET_assert (NULL != rh->rev_check);
  2521. }
  2522. /**
  2523. * Task scheduled to continue with the resolution process.
  2524. *
  2525. * @param cls the `struct GNS_ResolverHandle` of the resolution
  2526. */
  2527. static void
  2528. recursive_resolution (void *cls)
  2529. {
  2530. struct GNS_ResolverHandle *rh = cls;
  2531. rh->task_id = NULL;
  2532. if (rh->loop_threshold < rh->loop_limiter++)
  2533. {
  2534. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2535. "Encountered unbounded recursion resolving `%s'\n",
  2536. rh->name);
  2537. fail_resolution (rh);
  2538. return;
  2539. }
  2540. if (GNUNET_YES == rh->ac_tail->gns_authority)
  2541. recursive_gns_resolution_revocation (rh);
  2542. else
  2543. recursive_dns_resolution (rh);
  2544. }
  2545. /**
  2546. * Begin the resolution process from 'name', starting with
  2547. * the identification of the zone specified by 'name'.
  2548. *
  2549. * @param cls the `struct GNS_ResolverHandle`
  2550. */
  2551. static void
  2552. start_resolver_lookup (void *cls)
  2553. {
  2554. struct GNS_ResolverHandle *rh = cls;
  2555. struct AuthorityChain *ac;
  2556. struct in_addr v4;
  2557. struct in6_addr v6;
  2558. rh->task_id = NULL;
  2559. if (1 == inet_pton (AF_INET,
  2560. rh->name,
  2561. &v4))
  2562. {
  2563. /* name is IPv4 address, pretend it's an A record */
  2564. struct GNUNET_GNSRECORD_Data rd;
  2565. rd.data = &v4;
  2566. rd.data_size = sizeof(v4);
  2567. rd.expiration_time = UINT64_MAX;
  2568. rd.record_type = GNUNET_DNSPARSER_TYPE_A;
  2569. rd.flags = 0;
  2570. rh->proc (rh->proc_cls,
  2571. 1,
  2572. &rd);
  2573. GNUNET_assert (NULL == rh->task_id);
  2574. rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
  2575. rh);
  2576. return;
  2577. }
  2578. if (1 == inet_pton (AF_INET6,
  2579. rh->name,
  2580. &v6))
  2581. {
  2582. /* name is IPv6 address, pretend it's an AAAA record */
  2583. struct GNUNET_GNSRECORD_Data rd;
  2584. rd.data = &v6;
  2585. rd.data_size = sizeof(v6);
  2586. rd.expiration_time = UINT64_MAX;
  2587. rd.record_type = GNUNET_DNSPARSER_TYPE_AAAA;
  2588. rd.flags = 0;
  2589. rh->proc (rh->proc_cls,
  2590. 1,
  2591. &rd);
  2592. GNUNET_assert (NULL == rh->task_id);
  2593. rh->task_id = GNUNET_SCHEDULER_add_now (&GNS_resolver_lookup_cancel_,
  2594. rh);
  2595. return;
  2596. }
  2597. ac = GNUNET_new (struct AuthorityChain);
  2598. ac->rh = rh;
  2599. ac->label = resolver_lookup_get_next_label (rh);
  2600. if (NULL == ac->label)
  2601. /* name was just the "TLD", so we default to label
  2602. #GNUNET_GNS_EMPTY_LABEL_AT */
  2603. ac->label = GNUNET_strdup (GNUNET_GNS_EMPTY_LABEL_AT);
  2604. ac->gns_authority = GNUNET_YES;
  2605. ac->authority_info.gns_authority = rh->authority_zone;
  2606. GNUNET_CONTAINER_DLL_insert_tail (rh->ac_head,
  2607. rh->ac_tail,
  2608. ac);
  2609. rh->task_id = GNUNET_SCHEDULER_add_now (&recursive_resolution,
  2610. rh);
  2611. }
  2612. /**
  2613. * Lookup of a record in a specific zone calls lookup result processor
  2614. * on result.
  2615. *
  2616. * @param zone the zone to perform the lookup in
  2617. * @param record_type the record type to look up
  2618. * @param name the name to look up
  2619. * @param options local options to control local lookup
  2620. * @param recursion_depth_limit how many zones to traverse
  2621. * at most
  2622. * @param proc the processor to call on result
  2623. * @param proc_cls the closure to pass to @a proc
  2624. * @return handle to cancel operation
  2625. */
  2626. struct GNS_ResolverHandle *
  2627. GNS_resolver_lookup (const struct GNUNET_IDENTITY_PublicKey *zone,
  2628. uint32_t record_type,
  2629. const char *name,
  2630. enum GNUNET_GNS_LocalOptions options,
  2631. uint16_t recursion_depth_limit,
  2632. GNS_ResultProcessor proc,
  2633. void *proc_cls)
  2634. {
  2635. struct GNS_ResolverHandle *rh;
  2636. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2637. "Starting lookup for `%s'\n",
  2638. name);
  2639. rh = GNUNET_new (struct GNS_ResolverHandle);
  2640. GNUNET_CONTAINER_DLL_insert (rlh_head,
  2641. rlh_tail,
  2642. rh);
  2643. rh->authority_zone = *zone;
  2644. rh->proc = proc;
  2645. rh->proc_cls = proc_cls;
  2646. rh->options = options;
  2647. rh->record_type = record_type;
  2648. rh->name = GNUNET_strdup (name);
  2649. rh->name_resolution_pos = strlen (name);
  2650. rh->loop_threshold = recursion_depth_limit;
  2651. rh->task_id = GNUNET_SCHEDULER_add_now (&start_resolver_lookup,
  2652. rh);
  2653. return rh;
  2654. }
  2655. /**
  2656. * Cancel active resolution (i.e. client disconnected).
  2657. *
  2658. * @param rh resolution to abort
  2659. */
  2660. void
  2661. GNS_resolver_lookup_cancel (struct GNS_ResolverHandle *rh)
  2662. {
  2663. struct DnsResult *dr;
  2664. struct AuthorityChain *ac;
  2665. struct VpnContext *vpn_ctx;
  2666. GNUNET_CONTAINER_DLL_remove (rlh_head,
  2667. rlh_tail,
  2668. rh);
  2669. if (NULL != rh->dns_request)
  2670. {
  2671. GNUNET_DNSSTUB_resolve_cancel (rh->dns_request);
  2672. rh->dns_request = NULL;
  2673. }
  2674. while (NULL != (ac = rh->ac_head))
  2675. {
  2676. GNUNET_CONTAINER_DLL_remove (rh->ac_head,
  2677. rh->ac_tail,
  2678. ac);
  2679. if (GNUNET_NO == ac->gns_authority)
  2680. {
  2681. struct Gns2DnsPending *gp;
  2682. while (NULL != (gp = ac->authority_info.dns_authority.gp_head))
  2683. {
  2684. GNUNET_CONTAINER_DLL_remove (ac->authority_info.dns_authority.gp_head,
  2685. ac->authority_info.dns_authority.gp_tail,
  2686. gp);
  2687. if (NULL != gp->rh)
  2688. {
  2689. /* rh->g2dc->rh is NOT in the DLL yet, so to enable us
  2690. using GNS_resolver_lookup_cancel here, we need to
  2691. add it first... */
  2692. GNUNET_CONTAINER_DLL_insert (rlh_head,
  2693. rlh_tail,
  2694. gp->rh);
  2695. GNUNET_assert (NULL == gp->rh->task_id);
  2696. gp->rh->task_id = GNUNET_SCHEDULER_add_now (
  2697. &GNS_resolver_lookup_cancel_,
  2698. gp->rh);
  2699. gp->rh = NULL;
  2700. }
  2701. if (NULL != gp->dns_rh)
  2702. {
  2703. GNUNET_RESOLVER_request_cancel (gp->dns_rh);
  2704. gp->dns_rh = NULL;
  2705. }
  2706. GNUNET_free (gp);
  2707. }
  2708. GNUNET_DNSSTUB_stop (ac->authority_info.dns_authority.dns_handle);
  2709. }
  2710. GNUNET_free (ac->label);
  2711. GNUNET_free (ac);
  2712. }
  2713. if (NULL != rh->task_id)
  2714. {
  2715. GNUNET_SCHEDULER_cancel (rh->task_id);
  2716. rh->task_id = NULL;
  2717. }
  2718. if (NULL != rh->get_handle)
  2719. {
  2720. GNUNET_DHT_get_stop (rh->get_handle);
  2721. rh->get_handle = NULL;
  2722. }
  2723. if (NULL != rh->dht_heap_node)
  2724. {
  2725. GNUNET_CONTAINER_heap_remove_node (rh->dht_heap_node);
  2726. rh->dht_heap_node = NULL;
  2727. }
  2728. if (NULL != (vpn_ctx = rh->vpn_ctx))
  2729. {
  2730. GNUNET_VPN_cancel_request (vpn_ctx->vpn_request);
  2731. GNUNET_free (vpn_ctx->rd_data);
  2732. GNUNET_free (vpn_ctx);
  2733. }
  2734. if (NULL != rh->namecache_qe)
  2735. {
  2736. GNUNET_NAMECACHE_cancel (rh->namecache_qe);
  2737. rh->namecache_qe = NULL;
  2738. }
  2739. if (NULL != rh->rev_check)
  2740. {
  2741. GNUNET_REVOCATION_query_cancel (rh->rev_check);
  2742. rh->rev_check = NULL;
  2743. }
  2744. if (NULL != rh->std_resolve)
  2745. {
  2746. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  2747. "Canceling standard DNS resolution\n");
  2748. GNUNET_RESOLVER_request_cancel (rh->std_resolve);
  2749. rh->std_resolve = NULL;
  2750. }
  2751. while (NULL != (dr = rh->dns_result_head))
  2752. {
  2753. GNUNET_CONTAINER_DLL_remove (rh->dns_result_head,
  2754. rh->dns_result_tail,
  2755. dr);
  2756. GNUNET_free (dr);
  2757. }
  2758. GNUNET_free (rh->leho);
  2759. GNUNET_free (rh->name);
  2760. GNUNET_free (rh);
  2761. }
  2762. /* ***************** Resolver initialization ********************* */
  2763. /**
  2764. * Initialize the resolver
  2765. *
  2766. * @param nc the namecache handle
  2767. * @param dht the dht handle
  2768. * @param c configuration handle
  2769. * @param max_bg_queries maximum number of parallel background queries in dht
  2770. */
  2771. void
  2772. GNS_resolver_init (struct GNUNET_NAMECACHE_Handle *nc,
  2773. struct GNUNET_DHT_Handle *dht,
  2774. const struct GNUNET_CONFIGURATION_Handle *c,
  2775. unsigned long long max_bg_queries)
  2776. {
  2777. cfg = c;
  2778. namecache_handle = nc;
  2779. dht_handle = dht;
  2780. dht_lookup_heap =
  2781. GNUNET_CONTAINER_heap_create (GNUNET_CONTAINER_HEAP_ORDER_MIN);
  2782. max_allowed_background_queries = max_bg_queries;
  2783. disable_cache = GNUNET_CONFIGURATION_get_value_yesno (cfg,
  2784. "namecache",
  2785. "DISABLE");
  2786. if (GNUNET_YES == disable_cache)
  2787. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  2788. "Namecache disabled\n");
  2789. vpn_handle = GNUNET_VPN_connect (cfg);
  2790. }
  2791. /**
  2792. * Shutdown resolver
  2793. */
  2794. void
  2795. GNS_resolver_done ()
  2796. {
  2797. struct GNS_ResolverHandle *rh;
  2798. struct CacheOps *co;
  2799. /* abort active resolutions */
  2800. while (NULL != (rh = rlh_head))
  2801. {
  2802. rh->proc (rh->proc_cls,
  2803. 0,
  2804. NULL);
  2805. GNS_resolver_lookup_cancel (rh);
  2806. }
  2807. while (NULL != (co = co_head))
  2808. {
  2809. GNUNET_CONTAINER_DLL_remove (co_head,
  2810. co_tail,
  2811. co);
  2812. GNUNET_NAMECACHE_cancel (co->namecache_qe_cache);
  2813. GNUNET_free (co);
  2814. }
  2815. GNUNET_CONTAINER_heap_destroy (dht_lookup_heap);
  2816. dht_lookup_heap = NULL;
  2817. GNUNET_VPN_disconnect (vpn_handle);
  2818. vpn_handle = NULL;
  2819. dht_handle = NULL;
  2820. namecache_handle = NULL;
  2821. }
  2822. /* end of gnunet-service-gns_resolver.c */