resolv.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  1. /*++
  2. Copyright (c) 2015 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. resolv.c
  9. Abstract:
  10. This module implements the standard DNS resolver functions.
  11. Author:
  12. Evan Green 23-Jan-2015
  13. Environment:
  14. User Mode C Library
  15. --*/
  16. //
  17. // ------------------------------------------------------------------- Includes
  18. //
  19. #include "libcp.h"
  20. #include <minoca/devinfo/net.h>
  21. #include <arpa/inet.h>
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <limits.h>
  26. #include <netdb.h>
  27. #include <poll.h>
  28. #include <resolv.h>
  29. #include <stdlib.h>
  30. #include "net.h"
  31. //
  32. // --------------------------------------------------------------------- Macros
  33. //
  34. //
  35. // This macro evaluates to non-zero if the given character is a special
  36. // character (per the DNS spec).
  37. //
  38. #define DNS_SPECIAL_CHARACTER(_Character) \
  39. (((_Character) == '"') || ((_Character) == '.') || \
  40. ((_Character) == ';') || ((_Character) == '\\') || \
  41. ((_Character) == '@') || ((_Character) == '$'))
  42. //
  43. // This macro evaluates to non-zero if the given character is printable
  44. // according to the DNS spec.
  45. //
  46. #define DNS_PRINTABLE_CHARACTER(_Character) \
  47. (((_Character) > ' ') && ((_Character) < 0x7F))
  48. //
  49. // ---------------------------------------------------------------- Definitions
  50. //
  51. //
  52. // Define the name of an environment variable to use as a DNS server address,
  53. // overriding the configuration.
  54. //
  55. #define DNS_DNSCACHEIP_VARIABLE "DNSCACHEIP"
  56. //
  57. // Define the maximum size of the resolver configuration file.
  58. //
  59. #define DNS_RESOLVER_CONFIGURATION_MAX 4096
  60. //
  61. // Define the maximum number of supported local domains.
  62. //
  63. #define DNS_DOMAIN_COUNT 8
  64. //
  65. // Define the maximum size of a DNS query.
  66. //
  67. #define DNS_QUERY_MAX 512
  68. //
  69. // Define the maximum size of a DNS name component.
  70. //
  71. #define DNS_COMPONENT_MAX 63
  72. //
  73. // Define the maximum number of times in a row to call a hook.
  74. //
  75. #define DNS_MAX_HOOK_CALLS 50
  76. //
  77. // ------------------------------------------------------ Data Type Definitions
  78. //
  79. //
  80. // ----------------------------------------------- Internal Function Prototypes
  81. //
  82. INT
  83. ClpDnsReadStartFiles (
  84. res_state State
  85. );
  86. int
  87. ClpDnsParseSocketAddress (
  88. PSTR Address,
  89. PVOID SocketAddress
  90. );
  91. INT
  92. ClpDnsAddConfiguredServers (
  93. res_state State,
  94. NET_DOMAIN_TYPE Domain
  95. );
  96. INT
  97. ClpDnsMatchQueries (
  98. PUCHAR Buffer1,
  99. PUCHAR Buffer1End,
  100. PUCHAR Buffer2,
  101. PUCHAR Buffer2End
  102. );
  103. INT
  104. ClpDnsIsNameInQuery (
  105. PUCHAR Name,
  106. INT Type,
  107. INT Class,
  108. PUCHAR Buffer,
  109. PUCHAR BufferEnd
  110. );
  111. INT
  112. ClpDnsIsSameName (
  113. PCHAR Name1,
  114. PCHAR Name2
  115. );
  116. INT
  117. ClpDnsMakeNameCanonical (
  118. PCHAR Source,
  119. PCHAR Destination,
  120. UINTN DestinationSize
  121. );
  122. INT
  123. ClpDnsIsNameServer (
  124. res_state State,
  125. struct sockaddr_in6 *Address
  126. );
  127. INT
  128. ClpDnsCompressName (
  129. PUCHAR Source,
  130. PUCHAR Destination,
  131. UINTN DestinationSize,
  132. PUCHAR *DomainNames,
  133. PUCHAR *LastDomainName
  134. );
  135. INT
  136. ClpDnsDecompressName (
  137. PUCHAR Message,
  138. PUCHAR MessageEnd,
  139. PUCHAR Source,
  140. PUCHAR Destination,
  141. UINTN DestinationSize
  142. );
  143. INT
  144. ClpDnsPackName (
  145. PUCHAR Source,
  146. PUCHAR Destination,
  147. UINTN DestinationSize,
  148. PUCHAR *DomainNames,
  149. PUCHAR *LastDomainName
  150. );
  151. INT
  152. ClpDnsUnpackName (
  153. PUCHAR Message,
  154. PUCHAR MessageEnd,
  155. PUCHAR Source,
  156. PUCHAR Destination,
  157. UINTN DestinationSize
  158. );
  159. INT
  160. ClpDnsEncodeName (
  161. PUCHAR Source,
  162. PUCHAR Destination,
  163. UINTN DestinationSize
  164. );
  165. INT
  166. ClpDnsDecodeName (
  167. PUCHAR Source,
  168. PUCHAR Destination,
  169. UINTN DestinationSize
  170. );
  171. INT
  172. ClpDnsFindName (
  173. PUCHAR Domain,
  174. PUCHAR Message,
  175. PUCHAR *DomainNames,
  176. PUCHAR *LastDomainName
  177. );
  178. INT
  179. ClpDnsSkipName (
  180. PUCHAR *Name,
  181. PUCHAR MessageEnd
  182. );
  183. INT
  184. ClpCompareIp4Addresses (
  185. struct sockaddr_in *Address1,
  186. struct sockaddr_in *Address2
  187. );
  188. //
  189. // -------------------------------------------------------------------- Globals
  190. //
  191. //
  192. // Store the resolver state, somewhat accessible by applications.
  193. //
  194. LIBC_API struct __res_state _res;
  195. //
  196. // Store the local domains.
  197. //
  198. int ClDnsSearch;
  199. PSTR ClDnsDomains[DNS_DOMAIN_COUNT];
  200. //
  201. // ------------------------------------------------------------------ Functions
  202. //
  203. //
  204. // TODO: Handle locking on resolver functions when threading is implemented.
  205. //
  206. LIBC_API
  207. int
  208. res_init (
  209. void
  210. )
  211. /*++
  212. Routine Description:
  213. This routine initializes the global resolver state.
  214. Arguments:
  215. None.
  216. Return Value:
  217. 0 on success.
  218. -1 on error, and errno will be set to contain more information.
  219. --*/
  220. {
  221. return res_ninit(&_res);
  222. }
  223. LIBC_API
  224. int
  225. res_search (
  226. const char *DomainName,
  227. int Class,
  228. int Type,
  229. u_char *Answer,
  230. int AnswerLength
  231. )
  232. /*++
  233. Routine Description:
  234. This routine constructs a query, sends it to the DNS server, awaits a
  235. response, and performs preliminary checks on the reply. It is the same as
  236. res_nquery, except that it also implements the default and search rules
  237. controlled by the RES_DEFNAMES and RES_DNSRCH options. It returns the first
  238. successful reply.
  239. Arguments:
  240. DomainName - Supplies the domain name to query for.
  241. Class - Supplies the class to put in the query.
  242. Type - Supplies the type to put in the query.
  243. Answer - Supplies a pointer where the answer will be returned.
  244. AnswerLength - Supplies the length of the answer buffer.
  245. Return Value:
  246. Returns the size of the response on success.
  247. -1 on failure.
  248. --*/
  249. {
  250. return res_nsearch(&_res, DomainName, Class, Type, Answer, AnswerLength);
  251. }
  252. LIBC_API
  253. int
  254. res_query (
  255. const char *DomainName,
  256. int Class,
  257. int Type,
  258. u_char *Answer,
  259. int AnswerLength
  260. )
  261. /*++
  262. Routine Description:
  263. This routine constructs a query, sends it to the DNS server, awaits a
  264. response, and performs preliminary checks on the reply.
  265. Arguments:
  266. DomainName - Supplies the domain name to query for.
  267. Class - Supplies the class to put in the query.
  268. Type - Supplies the type to put in the query.
  269. Answer - Supplies a pointer where the answer will be returned.
  270. AnswerLength - Supplies the length of the answer buffer.
  271. Return Value:
  272. Returns the size of the response on success.
  273. -1 on failure.
  274. --*/
  275. {
  276. return res_nquery(&_res, DomainName, Class, Type, Answer, AnswerLength);
  277. }
  278. LIBC_API
  279. int
  280. res_mkquery (
  281. int Op,
  282. const char *DomainName,
  283. int Class,
  284. int Type,
  285. u_char *Data,
  286. int DataLength,
  287. struct rrec *NewRecord,
  288. u_char *Buffer,
  289. int BufferLength
  290. )
  291. /*++
  292. Routine Description:
  293. This routine constructs a DNS query from the given parameters.
  294. Arguments:
  295. Op - Supplies the operation to perform. This is usually QUERY but can be
  296. any op from nameser.h.
  297. DomainName - Supplies the domain name to query for.
  298. Class - Supplies the class to put in the query.
  299. Type - Supplies the type to put in the query.
  300. Data - Supplies an unused data pointer.
  301. DataLength - Supplies the length of the data.
  302. NewRecord - Supplies a new record pointer, currently unused.
  303. Buffer - Supplies a pointer where the DNS query will be returned.
  304. BufferLength - Supplies the length of the return buffer in bytes.
  305. Return Value:
  306. Returns the size of the query created, or -1 on failure.
  307. --*/
  308. {
  309. int Result;
  310. Result = res_nmkquery(&_res,
  311. Op,
  312. DomainName,
  313. Class,
  314. Type,
  315. Data,
  316. DataLength,
  317. NewRecord,
  318. Buffer,
  319. BufferLength);
  320. return Result;
  321. }
  322. LIBC_API
  323. int
  324. res_send (
  325. const u_char *Message,
  326. int MessageLength,
  327. u_char *Answer,
  328. int AnswerLength
  329. )
  330. /*++
  331. Routine Description:
  332. This routine sends a message to the currently configured DNS server and
  333. returns the reply.
  334. Arguments:
  335. Message - Supplies a pointer to the message to send.
  336. MessageLength - Supplies the length of the message in bytes.
  337. Answer - Supplies a pointer where the answer will be returned.
  338. AnswerLength - Supplies the length of the answer buffer in bytes.
  339. Return Value:
  340. Returns the length of the reply message on success.
  341. -1 on failure.
  342. --*/
  343. {
  344. return res_nsend(&_res, Message, MessageLength, Answer, AnswerLength);
  345. }
  346. LIBC_API
  347. void
  348. res_close (
  349. void
  350. )
  351. /*++
  352. Routine Description:
  353. This routine closes the socket for the global resolver state.
  354. Arguments:
  355. None.
  356. Return Value:
  357. None.
  358. --*/
  359. {
  360. return res_nclose(&_res);
  361. }
  362. //
  363. // These resolver interface functions operate on a state pointer passed in,
  364. // rather than a global object.
  365. //
  366. LIBC_API
  367. int
  368. res_ninit (
  369. res_state State
  370. )
  371. /*++
  372. Routine Description:
  373. This routine initializes the resolver state.
  374. Arguments:
  375. State - Supplies the state to initialize, a pointer type.
  376. Return Value:
  377. 0 on success.
  378. -1 on error, and errno will be set to contain more information.
  379. --*/
  380. {
  381. int Result;
  382. State->nscount = 0;
  383. Result = ClpDnsReadStartFiles(State);
  384. if (Result < 0) {
  385. return -1;
  386. }
  387. if ((State->options & RES_INIT) == 0) {
  388. State->retry = 1;
  389. State->retrans = RES_TIMEOUT;
  390. State->options |= RES_INIT;
  391. State->_sock = -1;
  392. }
  393. return Result;
  394. }
  395. LIBC_API
  396. int
  397. res_nsearch (
  398. res_state State,
  399. const char *DomainName,
  400. int Class,
  401. int Type,
  402. u_char *Answer,
  403. int AnswerLength
  404. )
  405. /*++
  406. Routine Description:
  407. This routine constructs a query, sends it to the DNS server, awaits a
  408. response, and performs preliminary checks on the reply. It is the same as
  409. res_nquery, except that it also implements the default and search rules
  410. controlled by the RES_DEFNAMES and RES_DNSRCH options. It returns the first
  411. successful reply.
  412. Arguments:
  413. State - Supplies the state, a pointer type.
  414. DomainName - Supplies the domain name to query for.
  415. Class - Supplies the class to put in the query.
  416. Type - Supplies the type to put in the query.
  417. Answer - Supplies a pointer where the answer will be returned.
  418. AnswerLength - Supplies the length of the answer buffer.
  419. Return Value:
  420. Returns the size of the response on success.
  421. -1 on failure.
  422. --*/
  423. {
  424. //
  425. // For now, this is the same as res_nquery.
  426. //
  427. return res_nquery(State, DomainName, Class, Type, Answer, AnswerLength);
  428. }
  429. LIBC_API
  430. int
  431. res_nquery (
  432. res_state State,
  433. const char *DomainName,
  434. int Class,
  435. int Type,
  436. u_char *Answer,
  437. int AnswerLength
  438. )
  439. /*++
  440. Routine Description:
  441. This routine constructs a query, sends it to the DNS server, awaits a
  442. response, and performs preliminary checks on the reply.
  443. Arguments:
  444. State - Supplies the state, a pointer type.
  445. DomainName - Supplies the domain name to query for.
  446. Class - Supplies the class to put in the query.
  447. Type - Supplies the type to put in the query.
  448. Answer - Supplies a pointer where the answer will be returned.
  449. AnswerLength - Supplies the length of the answer buffer.
  450. Return Value:
  451. Returns the size of the response on success.
  452. -1 on failure.
  453. --*/
  454. {
  455. UCHAR Buffer[DNS_QUERY_MAX];
  456. PDNS_HEADER Header;
  457. UINT ResponseCode;
  458. int Result;
  459. UINT Size;
  460. Header = (PDNS_HEADER)Answer;
  461. Header->Flags = 0;
  462. Result = res_nmkquery(State,
  463. QUERY,
  464. DomainName,
  465. Class,
  466. Type,
  467. NULL,
  468. 0,
  469. NULL,
  470. Buffer,
  471. sizeof(Buffer));
  472. if (Result < 0) {
  473. return Result;
  474. }
  475. Size = Result;
  476. Result = res_nsend(State, Buffer, Size, Answer, AnswerLength);
  477. if (Result < 0) {
  478. return Result;
  479. }
  480. ResponseCode = (Header->Flags >> DNS_HEADER_RESPONSE_SHIFT) &
  481. DNS_HEADER_RESPONSE_MASK;
  482. if ((ResponseCode != DNS_HEADER_RESPONSE_SUCCESS) ||
  483. (ntohs(Header->AnswerCount) == 0)) {
  484. if (ResponseCode == DNS_HEADER_RESPONSE_NAME_ERROR) {
  485. errno = ENOENT;
  486. return -1;
  487. } else if (ResponseCode == DNS_HEADER_RESPONSE_SERVER_FAILURE) {
  488. errno = EAGAIN;
  489. return -1;
  490. } else if (ResponseCode == DNS_HEADER_RESPONSE_SUCCESS) {
  491. errno = ENODATA;
  492. return -1;
  493. } else {
  494. errno = ENOTRECOVERABLE;
  495. return -1;
  496. }
  497. }
  498. Size = Result;
  499. return Size;
  500. }
  501. LIBC_API
  502. int
  503. res_nmkquery (
  504. res_state State,
  505. int Op,
  506. const char *DomainName,
  507. int Class,
  508. int Type,
  509. u_char *Data,
  510. int DataLength,
  511. struct rrec *NewRecord,
  512. u_char *Buffer,
  513. int BufferLength
  514. )
  515. /*++
  516. Routine Description:
  517. This routine constructs a DNS query from the given parameters.
  518. Arguments:
  519. State - Supplies the state, a pointer type.
  520. Op - Supplies the operation to perform. This is usually QUERY but can be
  521. any op from nameser.h.
  522. DomainName - Supplies the domain name to query for.
  523. Class - Supplies the class to put in the query.
  524. Type - Supplies the type to put in the query.
  525. Data - Supplies an unused data pointer.
  526. DataLength - Supplies the length of the data.
  527. NewRecord - Supplies a new record pointer, currently unused.
  528. Buffer - Supplies a pointer where the DNS query will be returned.
  529. BufferLength - Supplies the length of the return buffer in bytes.
  530. Return Value:
  531. Returns the size of the query created, or -1 on failure.
  532. --*/
  533. {
  534. PUCHAR CurrentByte;
  535. DNS_HEADER DnsHeader;
  536. UINTN NameLength;
  537. UCHAR Packet[DNS_QUERY_MAX];
  538. PSTR Search;
  539. UINTN Size;
  540. if ((State->options & RES_INIT) == 0) {
  541. if (res_ninit(State) < 0) {
  542. return -1;
  543. }
  544. }
  545. memset(&DnsHeader, 0, sizeof(DNS_HEADER));
  546. DnsHeader.Identifier = time(NULL) ^ rand() ^ getpid();
  547. DnsHeader.Flags = (Op << DNS_HEADER_OPCODE_SHIFT);
  548. if ((State->options & RES_RECURSE) != 0) {
  549. DnsHeader.Flags |= DNS_HEADER_FLAG_RECURSION_DESIRED;
  550. }
  551. DnsHeader.QuestionCount = htons(1);
  552. memcpy(Packet, &DnsHeader, sizeof(DNS_HEADER));
  553. CurrentByte = Packet + sizeof(DNS_HEADER);
  554. NameLength = 0;
  555. while (*DomainName != '\0') {
  556. //
  557. // Skip dots.
  558. //
  559. while (*DomainName == '.') {
  560. DomainName += 1;
  561. }
  562. //
  563. // Find the next dot.
  564. //
  565. Search = (PSTR)DomainName;
  566. while ((*Search != '\0') && (*Search != '.')) {
  567. Search += 1;
  568. }
  569. if (Search - DomainName > DNS_COMPONENT_MAX) {
  570. return -1;
  571. }
  572. Size = (UINTN)(Search - DomainName);
  573. *CurrentByte = Size;
  574. if (Size == 0) {
  575. break;
  576. }
  577. NameLength += *CurrentByte + 1;
  578. if (NameLength >= DNS_MAX_NAME) {
  579. return -1;
  580. }
  581. CurrentByte += 1;
  582. memcpy(CurrentByte, DomainName, Size);
  583. CurrentByte += Size;
  584. if (*Search == '\0') {
  585. *CurrentByte = '\0';
  586. break;
  587. }
  588. DomainName = Search;
  589. }
  590. //
  591. // Terminate the name.
  592. //
  593. CurrentByte += 1;
  594. *CurrentByte = '\0';
  595. //
  596. // Add the type and class.
  597. //
  598. CurrentByte += 1;
  599. *CurrentByte = Type;
  600. CurrentByte += 1;
  601. *CurrentByte = 0;
  602. CurrentByte += 1;
  603. *CurrentByte = Class;
  604. CurrentByte += 1;
  605. //
  606. // If the generated packet is too big, fail.
  607. //
  608. Size = (UINTN)(CurrentByte - Packet);
  609. if (Size > BufferLength) {
  610. return -1;
  611. }
  612. //
  613. // Copy the packet over and return.
  614. //
  615. memcpy(Buffer, Packet, Size);
  616. return Size;
  617. }
  618. LIBC_API
  619. int
  620. res_nsend (
  621. res_state State,
  622. const u_char *Message,
  623. int MessageLength,
  624. u_char *Answer,
  625. int AnswerLength
  626. )
  627. /*++
  628. Routine Description:
  629. This routine sends a message to the currently configured DNS server and
  630. returns the reply.
  631. Arguments:
  632. State - Supplies the resolver state, a pointer type.
  633. Message - Supplies a pointer to the message to send.
  634. MessageLength - Supplies the length of the message in bytes.
  635. Answer - Supplies a pointer where the answer will be returned.
  636. AnswerLength - Supplies the length of the answer buffer in bytes.
  637. Return Value:
  638. Returns the length of the reply message on success.
  639. -1 on failure.
  640. --*/
  641. {
  642. res_sendhookact Action;
  643. PDNS_HEADER AnswerHeader;
  644. UINTN BadNameServer;
  645. ssize_t BytesSent;
  646. BOOL ConnectionReset;
  647. PVOID CurrentBuffer;
  648. time_t CurrentTime;
  649. fd_set DescriptorMask;
  650. BOOL Done;
  651. INT Error;
  652. time_t Finish;
  653. struct sockaddr_in From;
  654. socklen_t FromLength;
  655. BOOL GotSomewhere;
  656. int HighestDescriptor;
  657. struct iovec IoVector[2];
  658. struct sockaddr_in Ip4Address;
  659. CHAR Junk[DNS_QUERY_MAX];
  660. ssize_t Length;
  661. INT Loops;
  662. int NewSocket;
  663. PDNS_HEADER QueryHeader;
  664. UCHAR ResponseCode;
  665. int ResponseLength;
  666. int Result;
  667. INT Seconds;
  668. struct sockaddr_in *ServerAddress;
  669. INTN ServerIndex;
  670. socklen_t SocketSize;
  671. time_t Start;
  672. UINTN Timeout;
  673. struct timeval TimeValue;
  674. BOOL Truncated;
  675. UINTN Try;
  676. BOOL VirtualCircuit;
  677. if ((State->options & RES_INIT) == 0) {
  678. if (res_ninit(State) < 0) {
  679. return -1;
  680. }
  681. }
  682. if (AnswerLength < sizeof(DNS_HEADER)) {
  683. errno = EINVAL;
  684. return -1;
  685. }
  686. VirtualCircuit = FALSE;
  687. if (((State->options & RES_USEVC) != 0) ||
  688. (MessageLength > DNS_QUERY_MAX)) {
  689. VirtualCircuit = TRUE;
  690. }
  691. AnswerHeader = (PDNS_HEADER)Answer;
  692. BadNameServer = 0;
  693. ConnectionReset = FALSE;
  694. Error = ETIMEDOUT;
  695. GotSomewhere = FALSE;
  696. HighestDescriptor = FD_SETSIZE - 1;
  697. QueryHeader = (PDNS_HEADER)Message;
  698. ResponseLength = 0;
  699. //
  700. // Rotate through name servers if desired.
  701. //
  702. if ((State->nscount > 0) && ((State->options & RES_ROTATE) != 0)) {
  703. Ip4Address = State->nsaddr_list[0];
  704. for (ServerIndex = 0;
  705. ServerIndex < State->nscount - 1;
  706. ServerIndex += 1) {
  707. State->nsaddr_list[ServerIndex] =
  708. State->nsaddr_list[ServerIndex + 1];
  709. }
  710. State->nsaddr_list[ServerIndex] = Ip4Address;
  711. }
  712. //
  713. // Loop trying to send a request and get a response.
  714. //
  715. for (Try = 0; Try < State->retry; Try += 1) {
  716. //
  717. // Loop over each name server in the list.
  718. //
  719. for (ServerIndex = 0; ServerIndex < State->nscount; ServerIndex += 1) {
  720. ServerAddress = &(State->nsaddr_list[ServerIndex]);
  721. if ((BadNameServer & (1L << ServerIndex)) != 0) {
  722. res_nclose(State);
  723. continue;
  724. }
  725. //
  726. // Call the query hook if it's set.
  727. //
  728. if (State->qhook != NULL) {
  729. Done = FALSE;
  730. Loops = 0;
  731. do {
  732. Action = State->qhook(&ServerAddress,
  733. &Message,
  734. &MessageLength,
  735. Answer,
  736. AnswerLength,
  737. &ResponseLength);
  738. switch (Action) {
  739. case res_goahead:
  740. Done = TRUE;
  741. break;
  742. case res_nextns:
  743. res_nclose(State);
  744. Done = TRUE;
  745. break;
  746. case res_done:
  747. return ResponseLength;
  748. case res_modified:
  749. if (Loops < DNS_MAX_HOOK_CALLS) {
  750. break;
  751. }
  752. //
  753. // Fall through.
  754. //
  755. case res_error:
  756. default:
  757. return -EINVAL;
  758. }
  759. } while (Done == FALSE);
  760. if (Action == res_nextns) {
  761. continue;
  762. }
  763. }
  764. if ((State->options & RES_DEBUG) != 0) {
  765. fprintf(stderr,
  766. "res_send: Querying server %d, try %d.\n",
  767. ServerIndex,
  768. Try);
  769. }
  770. if (VirtualCircuit != FALSE) {
  771. //
  772. // Only try once on a virtual circuit.
  773. //
  774. Try = State->retry;
  775. Truncated = FALSE;
  776. //
  777. // Ensure this is still the expected connection.
  778. //
  779. if ((State->_sock >= 0) &&
  780. ((State->_flags & RES_F_VC) != 0)) {
  781. SocketSize = sizeof(Ip4Address);
  782. Result = getpeername(State->_sock,
  783. (struct sockaddr *)&Ip4Address,
  784. &SocketSize);
  785. if ((Result != 0) ||
  786. (ClpCompareIp4Addresses(&Ip4Address,
  787. ServerAddress) == 0)) {
  788. res_nclose(State);
  789. State->_flags &= ~RES_F_VC;
  790. }
  791. }
  792. //
  793. // Fire up a connection.
  794. //
  795. if ((State->_sock < 0) ||
  796. ((State->_flags & RES_F_VC) == 0)) {
  797. if (State->_sock >= 0) {
  798. res_nclose(State);
  799. }
  800. State->_sock = socket(PF_INET, SOCK_STREAM, 0);
  801. if ((State->_sock < 0) ||
  802. (State->_sock > HighestDescriptor)) {
  803. Error = errno;
  804. if ((State->options & RES_DEBUG) != 0) {
  805. perror("res_send: Failed to open socket");
  806. }
  807. return -Error;
  808. }
  809. errno = 0;
  810. Result = connect(State->_sock,
  811. (struct sockaddr *)ServerAddress,
  812. sizeof(*ServerAddress));
  813. if (Result < 0) {
  814. Error = errno;
  815. if ((State->options & RES_DEBUG) != 0) {
  816. perror("res_send: Failed to connect");
  817. }
  818. BadNameServer |= (1L << ServerIndex);
  819. res_nclose(State);
  820. continue;
  821. }
  822. State->_flags |= RES_F_VC;
  823. }
  824. //
  825. // Write out the request.
  826. //
  827. IoVector[0].iov_base = &MessageLength;
  828. IoVector[0].iov_len = INT16SZ;
  829. IoVector[1].iov_base = (void *)Message;
  830. IoVector[1].iov_len = MessageLength;
  831. if (writev(State->_sock, IoVector, 2) !=
  832. (MessageLength + INT16SZ)) {
  833. Error = errno;
  834. if ((State->options & RES_DEBUG) != 0) {
  835. perror("res_send: Failed to writev");
  836. }
  837. BadNameServer |= 1L << ServerIndex;
  838. res_nclose(State);
  839. continue;
  840. }
  841. //
  842. // Read the length and response.
  843. //
  844. Length = INT16SZ;
  845. ResponseLength = 0;
  846. CurrentBuffer = &ResponseLength;
  847. BytesSent = 0;
  848. while (Length != 0) {
  849. do {
  850. BytesSent = read(State->_sock, CurrentBuffer, Length);
  851. } while ((BytesSent < 0) && (errno == EINTR));
  852. if (BytesSent <= 0) {
  853. break;
  854. }
  855. CurrentBuffer += BytesSent;
  856. Length -= BytesSent;
  857. }
  858. if (BytesSent <= 0) {
  859. Error = errno;
  860. if ((State->options & RES_DEBUG) != 0) {
  861. perror("res_send: Failed to read");
  862. }
  863. res_nclose(State);
  864. //
  865. // Give one retry a shot.
  866. //
  867. if ((errno == ECONNREFUSED) && (ConnectionReset == FALSE)) {
  868. ConnectionReset = TRUE;
  869. res_nclose(State);
  870. ServerIndex -= 1;
  871. continue;
  872. }
  873. }
  874. Length = ResponseLength;
  875. if (ResponseLength > AnswerLength) {
  876. if ((State->options & RES_DEBUG) != 0) {
  877. fprintf(stderr, "res_send: Response truncated.\n");
  878. }
  879. Truncated = TRUE;
  880. Length = AnswerLength;
  881. }
  882. //
  883. // Handle an undersized message.
  884. //
  885. if (Length < sizeof(DNS_HEADER)) {
  886. if ((State->options & RES_DEBUG) != 0) {
  887. fprintf(stderr, "res_send: Undersized response.\n");
  888. }
  889. Error = ENOSPC;
  890. BadNameServer |= (1L << ServerIndex);
  891. res_nclose(State);
  892. continue;
  893. }
  894. CurrentBuffer = Answer;
  895. while (Length != 0) {
  896. do {
  897. BytesSent = read(State->_sock, CurrentBuffer, Length);
  898. } while ((BytesSent < 0) && (errno == EINTR));
  899. if (BytesSent <= 0) {
  900. break;
  901. }
  902. CurrentBuffer += BytesSent;
  903. Length -= BytesSent;
  904. }
  905. if (BytesSent <= 0) {
  906. Error = errno;
  907. if ((State->options & RES_DEBUG) != 0) {
  908. perror("res_send: Failed to read");
  909. }
  910. res_nclose(State);
  911. continue;
  912. }
  913. //
  914. // Flush out the rest of the answer if the response was
  915. // truncated so things don't get out of sync.
  916. //
  917. if (Truncated != FALSE) {
  918. AnswerHeader->Flags |= DNS_HEADER_FLAG_TRUNCATION;
  919. Length = ResponseLength - AnswerLength;
  920. while (Length != 0) {
  921. BytesSent = Length;
  922. if (BytesSent > sizeof(Junk)) {
  923. BytesSent = sizeof(Junk);
  924. }
  925. do {
  926. BytesSent = read(State->_sock, Junk, BytesSent);
  927. } while ((BytesSent < 0) && (errno == EINTR));
  928. if (BytesSent > 0) {
  929. Length -= BytesSent;
  930. } else {
  931. break;
  932. }
  933. }
  934. }
  935. //
  936. // Validate the response ID.
  937. //
  938. if (AnswerHeader->Identifier != QueryHeader->Identifier) {
  939. if ((State->options & RES_DEBUG) != 0) {
  940. fprintf(stderr, "res_send: Unexpected response.\n");
  941. }
  942. continue;
  943. }
  944. //
  945. // This is not a virtual circuit, use datagrams.
  946. //
  947. } else {
  948. //
  949. // Create a socket if there is none.
  950. //
  951. if ((State->_sock < 0) ||
  952. ((State->_flags & RES_F_VC) != 0)) {
  953. if ((State->_flags & RES_F_VC) != 0) {
  954. res_nclose(State);
  955. }
  956. State->_sock = socket(PF_INET, SOCK_DGRAM, 0);
  957. if ((State->_sock < 0) ||
  958. (State->_sock > HighestDescriptor)) {
  959. Error = errno;
  960. if ((State->options & RES_DEBUG) != 0) {
  961. perror("res_send: Failed to create socket");
  962. }
  963. return -Error;
  964. }
  965. State->_flags &= ~RES_F_CONN;
  966. }
  967. if ((State->nscount == 1) ||
  968. ((Try == 0) && (ServerIndex == 0))) {
  969. //
  970. // Only connect if there's no possibility of receiving a
  971. // response from another server.
  972. //
  973. if ((State->_flags & RES_F_CONN) == 0) {
  974. Result = connect(State->_sock,
  975. (struct sockaddr *)ServerAddress,
  976. sizeof(*ServerAddress));
  977. if (Result != 0) {
  978. if ((State->options & RES_DEBUG) != 0) {
  979. perror("res_send: Failed to connect");
  980. }
  981. BadNameServer |= 1L << ServerIndex;
  982. res_nclose(State);
  983. continue;
  984. }
  985. State->_flags |= RES_F_CONN;
  986. }
  987. //
  988. // Fire off the request.
  989. //
  990. BytesSent = send(State->_sock,
  991. (const char *)Message,
  992. MessageLength,
  993. 0);
  994. if (BytesSent != MessageLength) {
  995. if ((State->options & RES_DEBUG) != 0) {
  996. perror("res_send: Failed to send");
  997. }
  998. BadNameServer |= 1L << ServerIndex;
  999. res_nclose(State);
  1000. continue;
  1001. }
  1002. } else {
  1003. //
  1004. // Disconnect if trying to receive to responses from
  1005. // multiple servers.
  1006. //
  1007. if ((State->_flags & RES_F_CONN) != 0) {
  1008. SocketSize = sizeof(Ip4Address);
  1009. NewSocket = socket(PF_INET, SOCK_DGRAM, 0);
  1010. Result = getsockname(State->_sock,
  1011. (struct sockaddr *)&Ip4Address,
  1012. &SocketSize);
  1013. if (NewSocket < 0) {
  1014. Error = errno;
  1015. if ((State->options & RES_DEBUG) != 0) {
  1016. perror("res_send: Failed to create socket");
  1017. }
  1018. return -Error;
  1019. }
  1020. dup2(NewSocket, State->_sock);
  1021. close(NewSocket);
  1022. if (Result == 0) {
  1023. //
  1024. // Re-bind to the original port.
  1025. //
  1026. Ip4Address.sin_addr.s_addr = htonl(0);
  1027. bind(State->_sock,
  1028. (struct sockaddr *)&Ip4Address,
  1029. SocketSize);
  1030. }
  1031. State->_flags &= ~RES_F_CONN;
  1032. errno = 0;
  1033. }
  1034. //
  1035. // Fire off the request.
  1036. //
  1037. Result = sendto(State->_sock,
  1038. Message,
  1039. MessageLength,
  1040. 0,
  1041. (struct sockaddr *)ServerAddress,
  1042. sizeof(*ServerAddress));
  1043. if (Result != MessageLength) {
  1044. if ((State->options & RES_DEBUG) != 0) {
  1045. perror("res_send: Failed to send");
  1046. }
  1047. BadNameServer |= 1L << ServerIndex;
  1048. res_nclose(State);
  1049. continue;
  1050. }
  1051. }
  1052. if ((State->_sock < 0) || (State->_sock > HighestDescriptor)) {
  1053. if ((State->options & RES_DEBUG) != 0) {
  1054. perror("res_send: Invalid socket");
  1055. }
  1056. res_nclose(State);
  1057. continue;
  1058. }
  1059. //
  1060. // Wait for a reply.
  1061. //
  1062. Seconds = State->retry << Try;
  1063. if (Try > 0) {
  1064. Seconds /= State->nscount;
  1065. }
  1066. if (Seconds == 0) {
  1067. Seconds = 1;
  1068. }
  1069. time(&CurrentTime);
  1070. Start = CurrentTime;
  1071. Timeout = Seconds;
  1072. Finish = Start + Timeout;
  1073. while (TRUE) {
  1074. FD_ZERO(&DescriptorMask);
  1075. FD_SET(State->_sock, &DescriptorMask);
  1076. TimeValue.tv_sec = Timeout;
  1077. TimeValue.tv_usec = 0;
  1078. Result = select(State->_sock + 1,
  1079. &DescriptorMask,
  1080. NULL,
  1081. NULL,
  1082. &TimeValue);
  1083. if (Result == 0) {
  1084. if ((State->options & RES_DEBUG) != 0) {
  1085. fprintf(stderr, "res_send: DNS Server Timeout\n");
  1086. }
  1087. GotSomewhere = TRUE;
  1088. break;
  1089. }
  1090. if (Result < 0) {
  1091. if (errno == EINTR) {
  1092. time(&CurrentTime);
  1093. if (Finish >= CurrentTime) {
  1094. Timeout = Finish - CurrentTime;
  1095. continue;
  1096. }
  1097. }
  1098. if ((State->options & RES_DEBUG) != 0) {
  1099. perror("res_send: select failed");
  1100. }
  1101. res_nclose(State);
  1102. break;
  1103. }
  1104. errno = 0;
  1105. //
  1106. // Grab that response.
  1107. //
  1108. FromLength = sizeof(struct sockaddr_in);
  1109. ResponseLength = recvfrom(State->_sock,
  1110. Answer,
  1111. AnswerLength,
  1112. 0,
  1113. (struct sockaddr *)&From,
  1114. &FromLength);
  1115. if (ResponseLength <= 0) {
  1116. if ((State->options & RES_DEBUG) != 0) {
  1117. perror("res_send: select failed");
  1118. }
  1119. res_nclose(State);
  1120. Result = -1;
  1121. break;
  1122. }
  1123. GotSomewhere = TRUE;
  1124. if (ResponseLength < sizeof(DNS_HEADER)) {
  1125. if ((State->options & RES_DEBUG) != 0) {
  1126. fprintf(stderr, "res_send: Undersized packet.\n");
  1127. }
  1128. Error = ENOSPC;
  1129. BadNameServer |= 1L << ServerIndex;
  1130. res_nclose(State);
  1131. Result = -1;
  1132. break;
  1133. }
  1134. //
  1135. // Ignore any answers whose IDs don't match.
  1136. //
  1137. if (QueryHeader->Identifier != AnswerHeader->Identifier) {
  1138. if ((State->options & RES_DEBUG) != 0) {
  1139. fprintf(stderr, "res_send: Ignoring packet.\n");
  1140. }
  1141. continue;
  1142. }
  1143. //
  1144. // Ensure the response came from the server.
  1145. //
  1146. if ((State->options & RES_INSECURE1) == 0) {
  1147. Result = ClpDnsIsNameServer(
  1148. State,
  1149. (struct sockaddr_in6 *)&From);
  1150. if (Result == 0) {
  1151. if ((State->options & RES_DEBUG) != 0) {
  1152. fprintf(stderr,
  1153. "res_send: Ignoring packet from "
  1154. "unknown server.\n");
  1155. }
  1156. continue;
  1157. }
  1158. }
  1159. //
  1160. // Ensure the response matches the query.
  1161. //
  1162. if ((State->options & RES_INSECURE2) == 0) {
  1163. Result = ClpDnsMatchQueries(
  1164. (PUCHAR)Message,
  1165. (PUCHAR)Message + MessageLength,
  1166. Answer,
  1167. Answer + AnswerLength);
  1168. if (Result == 0) {
  1169. if ((State->options & RES_DEBUG) != 0) {
  1170. fprintf(stderr,
  1171. "res_send: Ignoring packet from "
  1172. "mismatched query.\n");
  1173. }
  1174. continue;
  1175. }
  1176. }
  1177. break;
  1178. }
  1179. //
  1180. // If a response failed to come in, go to the next name server.
  1181. //
  1182. if (Result <= 0) {
  1183. continue;
  1184. }
  1185. //
  1186. // See if the server rejected the query.
  1187. //
  1188. ResponseCode = (AnswerHeader->Flags >>
  1189. DNS_HEADER_RESPONSE_SHIFT) &
  1190. DNS_HEADER_RESPONSE_MASK;
  1191. if ((ResponseCode == DNS_HEADER_RESPONSE_SERVER_FAILURE) ||
  1192. (ResponseCode ==
  1193. DNS_HEADER_RESPONSE_NOT_IMPLEMENTED) ||
  1194. (ResponseCode == DNS_HEADER_RESPONSE_REFUSED)) {
  1195. if ((State->options & RES_DEBUG) != 0) {
  1196. fprintf(stderr,
  1197. "res_send: Server rejected query: %d.\n",
  1198. ResponseCode);
  1199. }
  1200. BadNameServer |= 1L << ServerIndex;
  1201. res_nclose(State);
  1202. if (State->pfcode == 0) {
  1203. continue;
  1204. }
  1205. }
  1206. //
  1207. // Handle truncation.
  1208. //
  1209. if (((State->options & RES_IGNTC) == 0) &&
  1210. ((AnswerHeader->Flags & DNS_HEADER_FLAG_TRUNCATION) != 0)) {
  1211. if ((State->options & RES_DEBUG) != 0) {
  1212. fprintf(stderr,
  1213. "res_send: Response truncated.\n");
  1214. }
  1215. //
  1216. // Get the rest of the answer using TCP on the same server.
  1217. //
  1218. VirtualCircuit = TRUE;
  1219. res_nclose(State);
  1220. ServerIndex -= 1;
  1221. continue;
  1222. }
  1223. }
  1224. if ((State->options & RES_DEBUG) != 0) {
  1225. fprintf(stderr, "res_send: Got answer.\n");
  1226. }
  1227. //
  1228. // Potentially close the socket.
  1229. //
  1230. if (((VirtualCircuit != FALSE) &&
  1231. (((State->options & RES_USEVC) == 0) || (ServerIndex != 0))) ||
  1232. ((State->options & RES_STAYOPEN) == 0)) {
  1233. res_nclose(State);
  1234. }
  1235. //
  1236. // Call the response hook.
  1237. //
  1238. if (State->rhook != NULL) {
  1239. Done = FALSE;
  1240. Loops = 0;
  1241. do {
  1242. Action = State->rhook(ServerAddress,
  1243. Message,
  1244. MessageLength,
  1245. Answer,
  1246. AnswerLength,
  1247. &ResponseLength);
  1248. switch (Action) {
  1249. case res_goahead:
  1250. case res_done:
  1251. Done = TRUE;
  1252. break;
  1253. case res_nextns:
  1254. res_nclose(State);
  1255. break;
  1256. case res_modified:
  1257. if (Loops < DNS_MAX_HOOK_CALLS) {
  1258. break;
  1259. }
  1260. //
  1261. // Fall through.
  1262. //
  1263. case res_error:
  1264. default:
  1265. return -EINVAL;
  1266. }
  1267. } while (Done == FALSE);
  1268. if (Action == res_nextns) {
  1269. continue;
  1270. }
  1271. }
  1272. return AnswerLength;
  1273. }
  1274. }
  1275. res_nclose(State);
  1276. if (VirtualCircuit == FALSE) {
  1277. if (GotSomewhere == FALSE) {
  1278. Error = ECONNREFUSED;
  1279. } else {
  1280. errno = ETIMEDOUT;
  1281. }
  1282. }
  1283. assert(Error > 0);
  1284. return -Error;
  1285. }
  1286. LIBC_API
  1287. void
  1288. res_nclose (
  1289. res_state State
  1290. )
  1291. /*++
  1292. Routine Description:
  1293. This routine closes the socket for the given resolver state.
  1294. Arguments:
  1295. State - Supplies a pointer to the state to close.
  1296. Return Value:
  1297. None.
  1298. --*/
  1299. {
  1300. if (State->_sock >= 0) {
  1301. close(State->_sock);
  1302. State->_sock = -1;
  1303. State->_flags &= ~(RES_F_VC | RES_F_CONN);
  1304. }
  1305. return;
  1306. }
  1307. LIBC_API
  1308. int
  1309. dn_expand (
  1310. const u_char *Message,
  1311. const u_char *MessageEnd,
  1312. const u_char *Source,
  1313. u_char *Destination,
  1314. unsigned int DestinationSize
  1315. )
  1316. /*++
  1317. Routine Description:
  1318. This routine expands a DNS name in compressed format.
  1319. Arguments:
  1320. Message - Supplies a pointer to the DNS query or result.
  1321. MessageEnd - Supplies one beyond the last valid byte in the message.
  1322. Source - Supplies a pointer to the compressed name to decompress.
  1323. Destination - Supplies a pointer where the decompressed name will be
  1324. returned on success.
  1325. DestinationSize - Supplies the size of the decompressed name buffer in
  1326. bytes.
  1327. Return Value:
  1328. Returns the size of the compressed name.
  1329. -1 on error.
  1330. --*/
  1331. {
  1332. int NameSize;
  1333. NameSize = ClpDnsDecompressName((PUCHAR)Message,
  1334. (PUCHAR)MessageEnd,
  1335. (PUCHAR)Source,
  1336. Destination,
  1337. DestinationSize);
  1338. if ((NameSize > 0) && (Destination[0] == '.')) {
  1339. Destination[0] = '\0';
  1340. }
  1341. return NameSize;
  1342. }
  1343. LIBC_API
  1344. int
  1345. dn_comp (
  1346. const char *Source,
  1347. u_char *Destination,
  1348. unsigned int DestinationSize,
  1349. u_char **DomainNames,
  1350. u_char **LastDomainName
  1351. )
  1352. /*++
  1353. Routine Description:
  1354. This routine compresses a name for a format suitable for DNS queries and
  1355. responses.
  1356. Arguments:
  1357. Source - Supplies the source name to compress.
  1358. Destination - Supplies a pointer where the compressed name will be returned
  1359. on success.
  1360. DestinationSize - Supplies the size of the destination buffer on success.
  1361. DomainNames - Supplies an array of previously compressed names in the
  1362. message. The first pointer must point to the beginning of the message.
  1363. The list ends with NULL.
  1364. LastDomainName - Supplies one beyond the end of the array of domain name
  1365. pointers.
  1366. Return Value:
  1367. Returns the size of the compressed name.
  1368. -1 on error.
  1369. --*/
  1370. {
  1371. int NameSize;
  1372. NameSize = ClpDnsCompressName((PUCHAR)Source,
  1373. Destination,
  1374. DestinationSize,
  1375. DomainNames,
  1376. LastDomainName);
  1377. return NameSize;
  1378. }
  1379. LIBC_API
  1380. int
  1381. dn_skipname (
  1382. const u_char *Name,
  1383. const u_char *MessageEnd
  1384. )
  1385. /*++
  1386. Routine Description:
  1387. This routine skips over a compressed DNS name.
  1388. Arguments:
  1389. Name - Supplies a pointer to the compressed name.
  1390. MessageEnd - Supplies a pointer one byte beyond the last valid byte in the
  1391. query or response.
  1392. Return Value:
  1393. Returns the size of the compressed name.
  1394. -1 on error.
  1395. --*/
  1396. {
  1397. PUCHAR NameEnd;
  1398. NameEnd = (PUCHAR)Name;
  1399. if (ClpDnsSkipName(&NameEnd, (PUCHAR)MessageEnd) < 0) {
  1400. return -1;
  1401. }
  1402. return NameEnd - Name;
  1403. }
  1404. //
  1405. // --------------------------------------------------------- Internal Functions
  1406. //
  1407. INT
  1408. ClpDnsReadStartFiles (
  1409. res_state State
  1410. )
  1411. /*++
  1412. Routine Description:
  1413. This routine reads the resolver configuration file and sets up the global
  1414. resolver state.
  1415. Arguments:
  1416. State - Supplies a pointer to the state to initialize.
  1417. Return Value:
  1418. 0 on success.
  1419. -1 on failure.
  1420. --*/
  1421. {
  1422. CHAR Buffer[DNS_RESOLVER_CONFIGURATION_MAX];
  1423. PSTR CacheIpAddress;
  1424. PSTR CurrentBuffer;
  1425. int DnsDomainCount;
  1426. PSTR End;
  1427. int File;
  1428. ssize_t Length;
  1429. char OriginalCharacter;
  1430. int Result;
  1431. PSTR Search;
  1432. //
  1433. // If there are already name servers set up, then don't do anything.
  1434. //
  1435. if (State->nscount > 0) {
  1436. return 0;
  1437. }
  1438. CacheIpAddress = getenv(DNS_DNSCACHEIP_VARIABLE);
  1439. ClDnsSearch = 0;
  1440. if (CacheIpAddress != NULL) {
  1441. if (ClpDnsParseSocketAddress(CacheIpAddress, State->nsaddr_list) != 0) {
  1442. State->nscount += 1;
  1443. }
  1444. }
  1445. State->options = RES_RECURSE;
  1446. File = open(_PATH_RESCONF, O_RDONLY);
  1447. if (File < 0) {
  1448. goto DnsReadStartFilesEnd;
  1449. }
  1450. do {
  1451. Length = read(File, Buffer, DNS_RESOLVER_CONFIGURATION_MAX);
  1452. } while ((Length < 0) && (errno == EINTR));
  1453. close(File);
  1454. DnsDomainCount = sizeof(ClDnsDomains) / sizeof(ClDnsDomains[0]);
  1455. CurrentBuffer = Buffer;
  1456. End = Buffer + Length;
  1457. while (CurrentBuffer < End) {
  1458. if (strncmp(CurrentBuffer, "nameserver", 10) == 0) {
  1459. CurrentBuffer += 10;
  1460. //
  1461. // Loop through every name server listed on the line.
  1462. //
  1463. while ((CurrentBuffer < End) && (*CurrentBuffer != '\n')) {
  1464. //
  1465. // Get past blank space.
  1466. //
  1467. while ((CurrentBuffer < End) &&
  1468. (isblank(*CurrentBuffer) != 0)) {
  1469. CurrentBuffer += 1;
  1470. }
  1471. Search = CurrentBuffer;
  1472. while ((Search < End) && (isspace(*Search) == 0)) {
  1473. Search += 1;
  1474. }
  1475. if (Search >= End) {
  1476. break;
  1477. }
  1478. OriginalCharacter = *Search;
  1479. *Search = '\0';
  1480. Result = ClpDnsParseSocketAddress(
  1481. CurrentBuffer,
  1482. &(State->nsaddr_list[State->nscount]));
  1483. if (Result != 0) {
  1484. if (State->nscount < MAXNS) {
  1485. State->nscount += 1;
  1486. }
  1487. }
  1488. *Search = OriginalCharacter;
  1489. CurrentBuffer = Search;
  1490. }
  1491. } else if (((strncmp(CurrentBuffer, "search", 6) == 0) ||
  1492. (strncmp(CurrentBuffer, "domain", 6) == 0)) &&
  1493. (ClDnsSearch < DnsDomainCount)) {
  1494. CurrentBuffer += 6;
  1495. //
  1496. // Loop through all search or domain entries on this line.
  1497. //
  1498. while ((CurrentBuffer < End) && (*CurrentBuffer != '\n')) {
  1499. //
  1500. // Get past blank space or commas.
  1501. //
  1502. while ((CurrentBuffer < End) &&
  1503. ((*CurrentBuffer == ',') ||
  1504. (isblank(*CurrentBuffer) != 0))) {
  1505. CurrentBuffer += 1;
  1506. }
  1507. ClDnsDomains[ClDnsSearch] = CurrentBuffer;
  1508. while ((CurrentBuffer < End) &&
  1509. ((*CurrentBuffer == '.') ||
  1510. (*CurrentBuffer == '-') ||
  1511. (isalnum(*CurrentBuffer) != 0))) {
  1512. CurrentBuffer += 1;
  1513. }
  1514. OriginalCharacter = *CurrentBuffer;
  1515. if (CurrentBuffer < End) {
  1516. *CurrentBuffer = '\0';
  1517. }
  1518. if (ClDnsDomains[ClDnsSearch] < CurrentBuffer) {
  1519. ClDnsDomains[ClDnsSearch] =
  1520. strdup(ClDnsDomains[ClDnsSearch]);
  1521. if (ClDnsDomains[ClDnsSearch] != NULL) {
  1522. ClDnsSearch += 1;
  1523. }
  1524. }
  1525. if (CurrentBuffer < End) {
  1526. *CurrentBuffer = OriginalCharacter;
  1527. }
  1528. }
  1529. continue;
  1530. }
  1531. //
  1532. // Scan past the rest of the line, and any newlines.
  1533. //
  1534. while ((CurrentBuffer < End) && (*CurrentBuffer != '\n')) {
  1535. CurrentBuffer += 1;
  1536. }
  1537. while ((CurrentBuffer < End) && (*CurrentBuffer == '\n')) {
  1538. CurrentBuffer += 1;
  1539. }
  1540. }
  1541. DnsReadStartFilesEnd:
  1542. //
  1543. // Add DNS servers from the network link configuration itself.
  1544. //
  1545. Result = ClpDnsAddConfiguredServers(State, NetDomainIp4);
  1546. if (Result != 0) {
  1547. errno = Result;
  1548. return -1;
  1549. }
  1550. return 0;
  1551. }
  1552. int
  1553. ClpDnsParseSocketAddress (
  1554. PSTR Address,
  1555. PVOID SocketAddress
  1556. )
  1557. /*++
  1558. Routine Description:
  1559. This routine attempts to convert an address string into a socket address.
  1560. Arguments:
  1561. Address - Supplies the address string to convert.
  1562. SocketAddress - Supplies a pointer where the socket address will be
  1563. returned on success (either sockaddr_in or sockaddr_in6).
  1564. Return Value:
  1565. Non-zero on success.
  1566. Zero on failure.
  1567. --*/
  1568. {
  1569. struct sockaddr_in Ip4Address;
  1570. struct sockaddr_in6 Ip6Address;
  1571. int Result;
  1572. Result = 0;
  1573. memset(&Ip4Address, 0, sizeof(Ip4Address));
  1574. if (inet_pton(AF_INET, Address, &(Ip4Address.sin_addr)) != 0) {
  1575. Ip4Address.sin_port = htons(DNS_PORT_NUMBER);
  1576. Ip4Address.sin_family = AF_INET;
  1577. memcpy(SocketAddress, &Ip4Address, sizeof(struct sockaddr_in));
  1578. Result = 1;
  1579. } else {
  1580. memset(&Ip6Address, 0, sizeof(Ip6Address));
  1581. Result = inet_pton(AF_INET6, Address, &Ip6Address);
  1582. if (Result != 0) {
  1583. Ip6Address.sin6_port = htons(DNS_PORT_NUMBER);
  1584. Ip6Address.sin6_family = AF_INET6;
  1585. memcpy(SocketAddress, &Ip6Address, sizeof(struct sockaddr_in6));
  1586. }
  1587. }
  1588. return Result;
  1589. }
  1590. INT
  1591. ClpDnsAddConfiguredServers (
  1592. res_state State,
  1593. NET_DOMAIN_TYPE Domain
  1594. )
  1595. /*++
  1596. Routine Description:
  1597. This routine gets the known DNS server addresses from the system.
  1598. Arguments:
  1599. State - Supplies the state pointer to add the servers to.
  1600. Domain - Supplies the network domain to get DNS servers for.
  1601. Return Value:
  1602. 0 on success.
  1603. Returns an error code on failure.
  1604. --*/
  1605. {
  1606. BOOL AddedOne;
  1607. socklen_t AddressLength;
  1608. ULONG DeviceCount;
  1609. ULONG DeviceIndex;
  1610. DEVICE_INFORMATION_RESULT *Devices;
  1611. NETWORK_DEVICE_INFORMATION Information;
  1612. PVOID NewBuffer;
  1613. INT Result;
  1614. NET_DOMAIN_TYPE ServerDomain;
  1615. ULONG ServerIndex;
  1616. UINTN Size;
  1617. KSTATUS Status;
  1618. Devices = NULL;
  1619. if (State->nscount == MAXNS) {
  1620. Status = STATUS_SUCCESS;
  1621. goto DnsAddConfiguredServersEnd;
  1622. }
  1623. //
  1624. // Get the array of devices that return network device information.
  1625. //
  1626. DeviceCount = NETWORK_DEVICE_COUNT_ESTIMATE;
  1627. Devices = malloc(sizeof(DEVICE_INFORMATION_RESULT) * DeviceCount);
  1628. if (Devices == NULL) {
  1629. Status = STATUS_INSUFFICIENT_RESOURCES;
  1630. goto DnsAddConfiguredServersEnd;
  1631. }
  1632. Status = OsLocateDeviceInformation(&ClNetworkDeviceInformationUuid,
  1633. NULL,
  1634. Devices,
  1635. &DeviceCount);
  1636. if (!KSUCCESS(Status)) {
  1637. if (Status == STATUS_BUFFER_TOO_SMALL) {
  1638. DeviceCount += NETWORK_DEVICE_COUNT_ESTIMATE;
  1639. NewBuffer = realloc(
  1640. Devices,
  1641. sizeof(DEVICE_INFORMATION_RESULT) * DeviceCount);
  1642. if (NewBuffer == NULL) {
  1643. Status = STATUS_INSUFFICIENT_RESOURCES;
  1644. goto DnsAddConfiguredServersEnd;
  1645. }
  1646. NewBuffer = Devices;
  1647. Status = OsLocateDeviceInformation(&ClNetworkDeviceInformationUuid,
  1648. NULL,
  1649. Devices,
  1650. &DeviceCount);
  1651. if (!KSUCCESS(Status)) {
  1652. goto DnsAddConfiguredServersEnd;
  1653. }
  1654. } else {
  1655. goto DnsAddConfiguredServersEnd;
  1656. }
  1657. }
  1658. if (DeviceCount == 0) {
  1659. Status = STATUS_NO_NETWORK_CONNECTION;
  1660. goto DnsAddConfiguredServersEnd;
  1661. }
  1662. //
  1663. // Loop through all the network devices.
  1664. //
  1665. AddedOne = FALSE;
  1666. memset(&Information, 0, sizeof(NETWORK_DEVICE_INFORMATION));
  1667. Information.Version = NETWORK_DEVICE_INFORMATION_VERSION;
  1668. Information.Domain = Domain;
  1669. for (DeviceIndex = 0; DeviceIndex < DeviceCount; DeviceIndex += 1) {
  1670. Size = sizeof(NETWORK_DEVICE_INFORMATION);
  1671. Status = OsGetSetDeviceInformation(Devices[DeviceIndex].DeviceId,
  1672. &ClNetworkDeviceInformationUuid,
  1673. &Information,
  1674. &Size,
  1675. FALSE);
  1676. if (!KSUCCESS(Status)) {
  1677. continue;
  1678. }
  1679. if (((Information.Flags & NETWORK_DEVICE_FLAG_MEDIA_CONNECTED) == 0) ||
  1680. ((Information.Flags & NETWORK_DEVICE_FLAG_CONFIGURED) == 0)) {
  1681. continue;
  1682. }
  1683. //
  1684. // Loop through every listed server.
  1685. //
  1686. for (ServerIndex = 0;
  1687. ServerIndex < Information.DnsServerCount;
  1688. ServerIndex += 1) {
  1689. Information.DnsServers[ServerIndex].Port = DNS_PORT_NUMBER;
  1690. ServerDomain = Information.DnsServers[ServerIndex].Domain;
  1691. //
  1692. // TODO: Support IPv6 address in __res_state._u._ext.nsaddrs.
  1693. //
  1694. if ((State->nscount < MAXNS) &&
  1695. (ServerDomain == NetDomainIp4)) {
  1696. AddressLength = sizeof(struct sockaddr_in);
  1697. Status = ClConvertFromNetworkAddress(
  1698. &(Information.DnsServers[ServerIndex]),
  1699. (struct sockaddr *)&(State->nsaddr_list[State->nscount]),
  1700. &AddressLength,
  1701. NULL,
  1702. 0);
  1703. if (KSUCCESS(Status)) {
  1704. AddedOne = TRUE;
  1705. State->nscount += 1;
  1706. }
  1707. continue;
  1708. }
  1709. }
  1710. }
  1711. if (AddedOne == FALSE) {
  1712. Status = STATUS_NOT_FOUND;
  1713. goto DnsAddConfiguredServersEnd;
  1714. }
  1715. Status = STATUS_SUCCESS;
  1716. DnsAddConfiguredServersEnd:
  1717. if (Devices != NULL) {
  1718. free(Devices);
  1719. }
  1720. Result = 0;
  1721. if (!KSUCCESS(Status)) {
  1722. Result = ClConvertKstatusToErrorNumber(Status);
  1723. }
  1724. return Result;
  1725. }
  1726. INT
  1727. ClpDnsMatchQueries (
  1728. PUCHAR Buffer1,
  1729. PUCHAR Buffer1End,
  1730. PUCHAR Buffer2,
  1731. PUCHAR Buffer2End
  1732. )
  1733. /*++
  1734. Routine Description:
  1735. This routine determines if two DNS queries match each other.
  1736. Arguments:
  1737. Buffer1 - Supplies a pointer to the first DNS query.
  1738. Buffer1End - Supplies one beyond the last valid byte of the first DNS query.
  1739. Buffer2 - Supplies a pointer to the second DNS query.
  1740. Buffer2End - Supplies one beyond the last valid byte of the second DNS
  1741. query.
  1742. Return Value:
  1743. -1 on error.
  1744. 0 if the queries do not match.
  1745. 1 if the queries match.
  1746. --*/
  1747. {
  1748. INT Class;
  1749. PUCHAR CurrentPointer;
  1750. PDNS_HEADER Header1;
  1751. PDNS_HEADER Header2;
  1752. UCHAR Name[DNS_MAX_NAME];
  1753. int NameSize;
  1754. UINT Op1;
  1755. UINT Op2;
  1756. INT QuestionCount;
  1757. INT Type;
  1758. Header1 = (PDNS_HEADER)Buffer1;
  1759. Header2 = (PDNS_HEADER)Buffer2;
  1760. CurrentPointer = Buffer1 + sizeof(DNS_HEADER);
  1761. if ((Buffer1 + sizeof(DNS_HEADER) > Buffer1End) ||
  1762. (Buffer2 + sizeof(DNS_HEADER) > Buffer2End)) {
  1763. return -1;
  1764. }
  1765. QuestionCount = ntohs(Header1->QuestionCount);
  1766. Op1 = (Header1->Flags >> DNS_HEADER_OPCODE_SHIFT) & DNS_HEADER_OPCODE_MASK;
  1767. Op2 = (Header2->Flags >> DNS_HEADER_OPCODE_SHIFT) & DNS_HEADER_OPCODE_MASK;
  1768. if ((Op1 == DNS_HEADER_OPCODE_UPDATE) &&
  1769. (Op2 == DNS_HEADER_OPCODE_UPDATE)) {
  1770. return 1;
  1771. }
  1772. if (ntohs(Header2->QuestionCount) != QuestionCount) {
  1773. return 0;
  1774. }
  1775. while (QuestionCount > 0) {
  1776. QuestionCount -= 1;
  1777. NameSize = dn_expand(Buffer1,
  1778. Buffer1End,
  1779. CurrentPointer,
  1780. Name,
  1781. sizeof(Name));
  1782. if (NameSize < 0) {
  1783. return -1;
  1784. }
  1785. CurrentPointer += NameSize;
  1786. if (CurrentPointer + (2 * INT16SZ) > Buffer1End) {
  1787. return -1;
  1788. }
  1789. Type = READ_UNALIGNED16(CurrentPointer);
  1790. CurrentPointer += INT16SZ;
  1791. Class = READ_UNALIGNED16(CurrentPointer);
  1792. CurrentPointer += INT16SZ;
  1793. if (ClpDnsIsNameInQuery(Name, Type, Class, Buffer2, Buffer2End) == 0) {
  1794. return 0;
  1795. }
  1796. }
  1797. return 1;
  1798. }
  1799. INT
  1800. ClpDnsIsNameInQuery (
  1801. PUCHAR Name,
  1802. INT Type,
  1803. INT Class,
  1804. PUCHAR Buffer,
  1805. PUCHAR BufferEnd
  1806. )
  1807. /*++
  1808. Routine Description:
  1809. This routine determines if the given name, type, and class are located in
  1810. the query section of the given packet.
  1811. Arguments:
  1812. Name - Supplies a pointer to the name to look for.
  1813. Type - Supplies the type to look for.
  1814. Class - Supplies the class to look for.
  1815. Buffer - Supplies a pointer to the DNS query.
  1816. BufferEnd - Supplies one beyond the last valid byte of the DNS query.
  1817. Return Value:
  1818. -1 on error.
  1819. 0 if the name was not found.
  1820. 1 if the name was found.
  1821. --*/
  1822. {
  1823. PUCHAR CurrentPointer;
  1824. PDNS_HEADER Header;
  1825. INT PacketClass;
  1826. UCHAR PacketName[DNS_MAX_NAME];
  1827. INT PacketNameSize;
  1828. INT PacketType;
  1829. INT QuestionCount;
  1830. Header = (PDNS_HEADER)Buffer;
  1831. CurrentPointer = Buffer + sizeof(DNS_HEADER);
  1832. QuestionCount = ntohs(Header->QuestionCount);
  1833. while (QuestionCount > 0) {
  1834. QuestionCount -= 1;
  1835. PacketNameSize = dn_expand(Buffer,
  1836. BufferEnd,
  1837. CurrentPointer,
  1838. PacketName,
  1839. sizeof(PacketName));
  1840. if (PacketNameSize < 0) {
  1841. return -1;
  1842. }
  1843. CurrentPointer += PacketNameSize;
  1844. if (CurrentPointer + (2 * INT16SZ) > BufferEnd) {
  1845. return -1;
  1846. }
  1847. PacketType = READ_UNALIGNED16(CurrentPointer);
  1848. CurrentPointer += INT16SZ;
  1849. PacketClass = READ_UNALIGNED16(CurrentPointer);
  1850. CurrentPointer += INT16SZ;
  1851. if ((PacketType == Type) && (PacketClass == Class) &&
  1852. (ClpDnsIsSameName((PSTR)PacketName, (PSTR)Name) == 1)) {
  1853. return 1;
  1854. }
  1855. }
  1856. return 0;
  1857. }
  1858. INT
  1859. ClpDnsIsSameName (
  1860. PCHAR Name1,
  1861. PCHAR Name2
  1862. )
  1863. /*++
  1864. Routine Description:
  1865. This routine determines if the two domain names are the same.
  1866. Arguments:
  1867. Name1 - Supplies a pointer to the first name.
  1868. Name2 - Supplies a pointer to the second name.
  1869. Return Value:
  1870. -1 on error.
  1871. 0 if the names are not the same.
  1872. 1 if the names are the same.
  1873. --*/
  1874. {
  1875. CHAR CanonicalName1[DNS_MAX_NAME];
  1876. CHAR CanonicalName2[DNS_MAX_NAME];
  1877. INT Result;
  1878. Result = ClpDnsMakeNameCanonical(Name1,
  1879. CanonicalName1,
  1880. sizeof(CanonicalName1));
  1881. if (Result < 0) {
  1882. return Result;
  1883. }
  1884. Result = ClpDnsMakeNameCanonical(Name2,
  1885. CanonicalName2,
  1886. sizeof(CanonicalName2));
  1887. if (Result < 0) {
  1888. return Result;
  1889. }
  1890. if (strcasecmp(CanonicalName1, CanonicalName2) == 0) {
  1891. return 1;
  1892. }
  1893. return 0;
  1894. }
  1895. INT
  1896. ClpDnsMakeNameCanonical (
  1897. PCHAR Source,
  1898. PCHAR Destination,
  1899. UINTN DestinationSize
  1900. )
  1901. /*++
  1902. Routine Description:
  1903. This routine makes a canonical copy of the given domain name, removing
  1904. extra dots but making sure a dot is at the end.
  1905. Arguments:
  1906. Source - Supplies a pointer to the name to canonicalize.
  1907. Destination - Supplies a pointer where the canonicalized name will be
  1908. returned.
  1909. DestinationSize - Supplies the number of bytes in the destination buffer.
  1910. Return Value:
  1911. -1 on error.
  1912. 0 on success.
  1913. --*/
  1914. {
  1915. size_t Length;
  1916. Length = strlen(Source);
  1917. if (Length + sizeof(".") > DestinationSize) {
  1918. return -1;
  1919. }
  1920. strcpy(Destination, Source);
  1921. while ((Length > 0) && (Destination[Length - 1] == '.')) {
  1922. if ((Length > 1) && (Destination[Length - 2] == '\\') &&
  1923. ((Length < 2) || (Destination[Length - 3] != '\\'))) {
  1924. break;
  1925. }
  1926. Length -= 1;
  1927. Destination[Length] = '\0';
  1928. }
  1929. Destination[Length] = '.';
  1930. Length += 1;
  1931. Destination[Length] = '\0';
  1932. return 0;
  1933. }
  1934. INT
  1935. ClpDnsIsNameServer (
  1936. res_state State,
  1937. struct sockaddr_in6 *Address
  1938. )
  1939. /*++
  1940. Routine Description:
  1941. This routine determines if the given address is in the list of name servers.
  1942. Arguments:
  1943. State - Supplies the state (pointer) containing the acceptable name
  1944. servers.
  1945. Address - Supplies a pointer to the address to check.
  1946. Return Value:
  1947. -1 on error.
  1948. 0 on success.
  1949. --*/
  1950. {
  1951. struct sockaddr_in *Ip4Address;
  1952. struct sockaddr_in *Ip4ServerAddress;
  1953. struct sockaddr_in6 *Ip6ServerAddress;
  1954. int Result;
  1955. UINTN ServerIndex;
  1956. if (Address->sin6_family == AF_INET) {
  1957. Ip4Address = (struct sockaddr_in *)Address;
  1958. for (ServerIndex = 0; ServerIndex < State->nscount; ServerIndex += 1) {
  1959. Ip4ServerAddress = &(State->nsaddr_list[ServerIndex]);
  1960. if ((Ip4ServerAddress->sin_family == Ip4Address->sin_family) &&
  1961. (Ip4ServerAddress->sin_port = Ip4Address->sin_port) &&
  1962. (Ip4ServerAddress->sin_addr.s_addr ==
  1963. Ip4Address->sin_addr.s_addr)) {
  1964. return 1;
  1965. }
  1966. }
  1967. } else if (Address->sin6_family == AF_INET6) {
  1968. for (ServerIndex = 0; ServerIndex < MAXNS; ServerIndex += 1) {
  1969. Ip6ServerAddress = State->_u._ext.nsaddrs[ServerIndex];
  1970. if ((Ip6ServerAddress != NULL) &&
  1971. (Ip6ServerAddress->sin6_family == AF_INET6) &&
  1972. (Ip6ServerAddress->sin6_port == Address->sin6_port)) {
  1973. //
  1974. // It matches if it's not the ANY address and it matches the
  1975. // server.
  1976. //
  1977. Result = memcmp(&(Ip6ServerAddress->sin6_addr),
  1978. &in6addr_any,
  1979. sizeof(struct in6_addr));
  1980. if (Result == 0) {
  1981. Result = memcmp(&(Ip6ServerAddress->sin6_addr),
  1982. &(Address->sin6_addr),
  1983. sizeof(struct in6_addr));
  1984. if (Result != 0) {
  1985. return 1;
  1986. }
  1987. }
  1988. }
  1989. }
  1990. }
  1991. return 0;
  1992. }
  1993. INT
  1994. ClpDnsCompressName (
  1995. PUCHAR Source,
  1996. PUCHAR Destination,
  1997. UINTN DestinationSize,
  1998. PUCHAR *DomainNames,
  1999. PUCHAR *LastDomainName
  2000. )
  2001. /*++
  2002. Routine Description:
  2003. This routine compresses a name for a format suitable for DNS queries and
  2004. responses.
  2005. Arguments:
  2006. Source - Supplies the source name to compress.
  2007. Destination - Supplies a pointer where the compressed name will be returned
  2008. on success.
  2009. DestinationSize - Supplies the size of the destination buffer on success.
  2010. DomainNames - Supplies an array of previously compressed names in the
  2011. message. The first pointer must point to the beginning of the message.
  2012. The list ends with NULL.
  2013. LastDomainName - Supplies one beyond the end of the array of domain name
  2014. pointers.
  2015. Return Value:
  2016. Returns the size of the compressed name.
  2017. -1 on error.
  2018. --*/
  2019. {
  2020. UCHAR Name[DNS_MAX_NAME];
  2021. INT Result;
  2022. if (ClpDnsEncodeName(Source, Name, DNS_MAX_NAME) == -1) {
  2023. return -1;
  2024. }
  2025. Result = ClpDnsPackName(Name,
  2026. Destination,
  2027. DestinationSize,
  2028. DomainNames,
  2029. LastDomainName);
  2030. return Result;
  2031. }
  2032. INT
  2033. ClpDnsDecompressName (
  2034. PUCHAR Message,
  2035. PUCHAR MessageEnd,
  2036. PUCHAR Source,
  2037. PUCHAR Destination,
  2038. UINTN DestinationSize
  2039. )
  2040. /*++
  2041. Routine Description:
  2042. This routine expands a compressed name to presentation format.
  2043. Arguments:
  2044. Message - Supplies a pointer to the beginning of the DNS query or response.
  2045. MessageEnd - Supplies a pointer one beyond the last valid byte in the DNS
  2046. query or response.
  2047. Source - Supplies a pointer within the message to the name to expand.
  2048. Destination - Supplies a pointer where the decompressed name will be
  2049. returned on success.
  2050. DestinationSize - Supplies the size of the destination buffer in bytes.
  2051. Return Value:
  2052. Returns the number of bytes read out of the source buffer, or -1 on error.
  2053. --*/
  2054. {
  2055. UCHAR Name[DNS_MAX_NAME];
  2056. INT NameSize;
  2057. INT Result;
  2058. NameSize = ClpDnsUnpackName(Message,
  2059. MessageEnd,
  2060. Source,
  2061. Name,
  2062. sizeof(Name));
  2063. if (NameSize < 0) {
  2064. return -1;
  2065. }
  2066. Result = ClpDnsDecodeName(Name, Destination, DestinationSize);
  2067. if (Result < 0) {
  2068. return -1;
  2069. }
  2070. return NameSize;
  2071. }
  2072. INT
  2073. ClpDnsPackName (
  2074. PUCHAR Source,
  2075. PUCHAR Destination,
  2076. UINTN DestinationSize,
  2077. PUCHAR *DomainNames,
  2078. PUCHAR *LastDomainName
  2079. )
  2080. /*++
  2081. Routine Description:
  2082. This routine compresses a name for a format suitable for DNS queries and
  2083. responses.
  2084. Arguments:
  2085. Source - Supplies the source name to compress.
  2086. Destination - Supplies a pointer where the compressed name will be returned
  2087. on success.
  2088. DestinationSize - Supplies the size of the destination buffer on success.
  2089. DomainNames - Supplies an array of previously compressed names in the
  2090. message. The first pointer must point to the beginning of the message.
  2091. The list ends with NULL.
  2092. LastDomainName - Supplies one beyond the end of the array of domain name
  2093. pointers.
  2094. Return Value:
  2095. Returns the size of the compressed name.
  2096. -1 on error.
  2097. --*/
  2098. {
  2099. PUCHAR *CurrentDomain;
  2100. PUCHAR DestinationPointer;
  2101. PUCHAR End;
  2102. PUCHAR *LastDomain;
  2103. INT Length;
  2104. PUCHAR Message;
  2105. INT Result;
  2106. UINT Size;
  2107. PUCHAR SourcePointer;
  2108. CurrentDomain = NULL;
  2109. DestinationPointer = Destination;
  2110. End = Destination + DestinationSize;
  2111. LastDomain = NULL;
  2112. Message = NULL;
  2113. Result = -1;
  2114. SourcePointer = Source;
  2115. if (DomainNames != NULL) {
  2116. Message = DomainNames[0];
  2117. DomainNames += 1;
  2118. if (Message != NULL) {
  2119. CurrentDomain = DomainNames;
  2120. while (*CurrentDomain != NULL) {
  2121. CurrentDomain += 1;
  2122. }
  2123. LastDomain = CurrentDomain;
  2124. }
  2125. }
  2126. //
  2127. // Make sure the domain looks good.
  2128. //
  2129. Length = 0;
  2130. do {
  2131. Size = *SourcePointer;
  2132. if ((Size & DNS_COMPRESSION_MASK) != 0) {
  2133. goto DnsPackNameEnd;
  2134. }
  2135. Length += Size + 1;
  2136. if (Length > MAXCDNAME) {
  2137. goto DnsPackNameEnd;
  2138. }
  2139. SourcePointer += Size + 1;
  2140. } while (Size != 0);
  2141. SourcePointer = Source;
  2142. do {
  2143. Size = *SourcePointer;
  2144. if ((Size != 0) && (Message != NULL)) {
  2145. Length = ClpDnsFindName(SourcePointer,
  2146. Message,
  2147. DomainNames,
  2148. LastDomain);
  2149. if (Length >= 0) {
  2150. if (DestinationPointer + 1 >= End) {
  2151. goto DnsPackNameEnd;
  2152. }
  2153. *DestinationPointer = (Length >> 8) | DNS_COMPRESSION_VALUE;
  2154. DestinationPointer += 1;
  2155. *DestinationPointer = (Length & 0xFF);
  2156. return DestinationPointer - Destination;
  2157. }
  2158. if ((LastDomainName != NULL) &&
  2159. (CurrentDomain < (LastDomainName - 1))) {
  2160. *CurrentDomain = DestinationPointer;
  2161. CurrentDomain += 1;
  2162. *CurrentDomain = NULL;
  2163. }
  2164. }
  2165. //
  2166. // Copy the label.
  2167. //
  2168. if ((Size & DNS_COMPRESSION_MASK) != 0) {
  2169. goto DnsPackNameEnd;
  2170. }
  2171. if (DestinationPointer + 1 + Size >= End) {
  2172. goto DnsPackNameEnd;
  2173. }
  2174. memcpy(DestinationPointer, SourcePointer, Size + 1);
  2175. SourcePointer += Size + 1;
  2176. DestinationPointer += Size + 1;
  2177. } while (Size != 0);
  2178. if (DestinationPointer > End) {
  2179. if (Message != NULL) {
  2180. *LastDomain = NULL;
  2181. goto DnsPackNameEnd;
  2182. }
  2183. }
  2184. Result = 0;
  2185. DnsPackNameEnd:
  2186. if (Result != 0) {
  2187. errno = EMSGSIZE;
  2188. }
  2189. return Result;
  2190. }
  2191. INT
  2192. ClpDnsUnpackName (
  2193. PUCHAR Message,
  2194. PUCHAR MessageEnd,
  2195. PUCHAR Source,
  2196. PUCHAR Destination,
  2197. UINTN DestinationSize
  2198. )
  2199. /*++
  2200. Routine Description:
  2201. This routine unpacks a name from a source that might be compressed.
  2202. Arguments:
  2203. Message - Supplies a pointer to the beginning of the DNS query or response.
  2204. MessageEnd - Supplies a pointer one beyond the last valid byte in the DNS
  2205. query or response.
  2206. Source - Supplies a pointer within the message to the name to expand.
  2207. Destination - Supplies a pointer where the decompressed name will be
  2208. returned on success.
  2209. DestinationSize - Supplies the size of the destination buffer in bytes.
  2210. Return Value:
  2211. Returns the number of bytes read out of the source buffer, or -1 on error.
  2212. --*/
  2213. {
  2214. UINT Byte;
  2215. INT Checked;
  2216. PUCHAR DestinationLimit;
  2217. PUCHAR DestinationPointer;
  2218. INT Length;
  2219. PUCHAR SourcePointer;
  2220. Checked = 0;
  2221. DestinationLimit = Destination + DestinationSize;
  2222. DestinationPointer = Destination;
  2223. Length = -1;
  2224. SourcePointer = Source;
  2225. if ((SourcePointer < Message) || (SourcePointer >= MessageEnd)) {
  2226. errno = EMSGSIZE;
  2227. return -1;
  2228. }
  2229. //
  2230. // Loop getting labels in the domain name.
  2231. //
  2232. while (TRUE) {
  2233. Byte = *SourcePointer;
  2234. SourcePointer += 1;
  2235. if (Byte == '\0') {
  2236. break;
  2237. }
  2238. switch (Byte & DNS_COMPRESSION_MASK) {
  2239. case 0:
  2240. if (((DestinationPointer + Byte + 1) >= DestinationLimit) ||
  2241. (SourcePointer + Byte >= MessageEnd)) {
  2242. errno = EMSGSIZE;
  2243. return -1;
  2244. }
  2245. Checked += Byte + 1;
  2246. *DestinationPointer = Byte;
  2247. DestinationPointer += 1;
  2248. memcpy(DestinationPointer, SourcePointer, Byte);
  2249. DestinationPointer += Byte;
  2250. SourcePointer += Byte;
  2251. break;
  2252. case DNS_COMPRESSION_VALUE:
  2253. if (SourcePointer >= MessageEnd) {
  2254. errno = EMSGSIZE;
  2255. return -1;
  2256. }
  2257. if (Length < 0) {
  2258. Length = SourcePointer - Source + 1;
  2259. }
  2260. SourcePointer = Message +
  2261. (((Byte & (~DNS_COMPRESSION_MASK)) <<
  2262. BITS_PER_BYTE) |
  2263. (*SourcePointer & 0xFF));
  2264. if ((SourcePointer < Message) || (SourcePointer >= MessageEnd)) {
  2265. errno = EMSGSIZE;
  2266. return -1;
  2267. }
  2268. Checked += 2;
  2269. //
  2270. // Check for loops in the compressed name.
  2271. //
  2272. if (Checked >= MessageEnd - Message) {
  2273. errno = EMSGSIZE;
  2274. return -1;
  2275. }
  2276. break;
  2277. default:
  2278. errno = EMSGSIZE;
  2279. return -1;
  2280. }
  2281. }
  2282. *DestinationPointer = '\0';
  2283. if (Length < 0) {
  2284. Length = SourcePointer - Source;
  2285. }
  2286. return Length;
  2287. }
  2288. INT
  2289. ClpDnsEncodeName (
  2290. PUCHAR Source,
  2291. PUCHAR Destination,
  2292. UINTN DestinationSize
  2293. )
  2294. /*++
  2295. Routine Description:
  2296. This routine converts an ASCII string into an encoded name.
  2297. Arguments:
  2298. Source - Supplies a pointer to the name to encode.
  2299. Destination - Supplies a pointer where the encoded name will be returned on
  2300. success.
  2301. DestinationSize - Supplies the size of the destination buffer in bytes.
  2302. Return Value:
  2303. 0 if the string was not fully qualified.
  2304. 1 if the string was fully qualified.
  2305. -1 on failure, and errno will be set to contain more information.
  2306. --*/
  2307. {
  2308. INT Character;
  2309. PUCHAR Current;
  2310. PUCHAR End;
  2311. BOOL Escaped;
  2312. PUCHAR Label;
  2313. INT Value;
  2314. Current = Destination;
  2315. Escaped = FALSE;
  2316. End = Destination + DestinationSize;
  2317. Label = Destination + 1;
  2318. while (TRUE) {
  2319. Character = *Source;
  2320. Source += 1;
  2321. if (Character == '\0') {
  2322. break;
  2323. }
  2324. if (Escaped != FALSE) {
  2325. if (isdigit(Character)) {
  2326. Value = (Character - '0') * 100;
  2327. Character = *Source;
  2328. Source += 1;
  2329. if ((Character == '\0') || (!isdigit(Character))) {
  2330. errno = EMSGSIZE;
  2331. return -1;
  2332. }
  2333. Value += (Character - '0') * 10;
  2334. Character = *Source;
  2335. Source += 1;
  2336. if ((Character == '\0') || (!isdigit(Character))) {
  2337. errno = EMSGSIZE;
  2338. return -1;
  2339. }
  2340. Value += Character - '0';
  2341. if (Value > 0xFF) {
  2342. errno = EMSGSIZE;
  2343. return -1;
  2344. }
  2345. Character = Value;
  2346. }
  2347. Escaped = FALSE;
  2348. } else if (Character == '\\') {
  2349. Escaped = TRUE;
  2350. continue;
  2351. } else if (Character == '.') {
  2352. Character = Current - Label - 1;
  2353. //
  2354. // Watch out for the leg being too big (or off the end).
  2355. //
  2356. if (((Character & DNS_COMPRESSION_MASK) != 0) ||
  2357. (Label >= End)) {
  2358. errno = EMSGSIZE;
  2359. return -1;
  2360. }
  2361. *Label = Character;
  2362. //
  2363. // Handle a fully qualified name.
  2364. //
  2365. if (*Source == '\0') {
  2366. if (Character != 0) {
  2367. if (Current >= End) {
  2368. errno = EMSGSIZE;
  2369. return -1;
  2370. }
  2371. *Current = '\0';
  2372. Current += 1;
  2373. }
  2374. if ((Current - Destination) > MAXCDNAME) {
  2375. errno = EMSGSIZE;
  2376. return -1;
  2377. }
  2378. return 1;
  2379. }
  2380. if ((Character == 0) || (*Source == '.')) {
  2381. errno = EMSGSIZE;
  2382. return -1;
  2383. }
  2384. Label = Current;
  2385. Current += 1;
  2386. continue;
  2387. }
  2388. if (Current >= End) {
  2389. errno = EMSGSIZE;
  2390. return -1;
  2391. }
  2392. *Current = Character;
  2393. Current += 1;
  2394. }
  2395. Character = Current - Label - 1;
  2396. if (((Character & DNS_COMPRESSION_MASK) != 0) ||
  2397. (Label >= End)) {
  2398. errno = EMSGSIZE;
  2399. return -1;
  2400. }
  2401. if (Label >= End) {
  2402. errno = EMSGSIZE;
  2403. return -1;
  2404. }
  2405. *Label = Character;
  2406. if (Character != 0) {
  2407. if (Current >= End) {
  2408. errno = EMSGSIZE;
  2409. return -1;
  2410. }
  2411. *Current = '\0';
  2412. Current += 1;
  2413. }
  2414. if ((Current - Destination) > MAXCDNAME) {
  2415. errno = EMSGSIZE;
  2416. return -1;
  2417. }
  2418. return 0;
  2419. }
  2420. INT
  2421. ClpDnsDecodeName (
  2422. PUCHAR Source,
  2423. PUCHAR Destination,
  2424. UINTN DestinationSize
  2425. )
  2426. /*++
  2427. Routine Description:
  2428. This routine converts an encoded name to a printable ASCII name.
  2429. Arguments:
  2430. Source - Supplies a pointer to the name to decode.
  2431. Destination - Supplies a pointer where the decoded name will be returned on
  2432. success.
  2433. DestinationSize - Supplies the size of the destination buffer in bytes.
  2434. Return Value:
  2435. Returns the number of bytes written to the buffer, or -1 on error.
  2436. --*/
  2437. {
  2438. UCHAR Character;
  2439. PUCHAR CurrentPointer;
  2440. PUCHAR DestinationPointer;
  2441. PUCHAR End;
  2442. UINT Size;
  2443. CurrentPointer = Source;
  2444. DestinationPointer = Destination;
  2445. End = Destination + DestinationSize;
  2446. while (TRUE) {
  2447. Size = *CurrentPointer;
  2448. CurrentPointer += 1;
  2449. if (Size == 0) {
  2450. break;
  2451. }
  2452. //
  2453. // The name is supposed to already be decompressed.
  2454. //
  2455. if ((Size & DNS_COMPRESSION_MASK) != 0) {
  2456. errno = EMSGSIZE;
  2457. return -1;
  2458. }
  2459. if (DestinationPointer != Destination) {
  2460. if (DestinationPointer >= End) {
  2461. errno = EMSGSIZE;
  2462. return -1;
  2463. }
  2464. *DestinationPointer = '.';
  2465. DestinationPointer += 1;
  2466. }
  2467. if (DestinationPointer + Size >= End) {
  2468. errno = EMSGSIZE;
  2469. return -1;
  2470. }
  2471. while (Size > 0) {
  2472. Character = *CurrentPointer;
  2473. CurrentPointer += 1;
  2474. if (DNS_SPECIAL_CHARACTER(Character)) {
  2475. if (DestinationPointer + 1 >= End) {
  2476. errno = EMSGSIZE;
  2477. return -1;
  2478. }
  2479. *DestinationPointer = '\\';
  2480. DestinationPointer += 1;
  2481. *DestinationPointer = Character;
  2482. DestinationPointer += 1;
  2483. } else if (!DNS_PRINTABLE_CHARACTER(Character)) {
  2484. if (DestinationPointer + 3 >= End) {
  2485. errno = EMSGSIZE;
  2486. return -1;
  2487. }
  2488. *DestinationPointer = '\\';
  2489. DestinationPointer += 1;
  2490. *DestinationPointer = '0' + (Character / 100);
  2491. DestinationPointer += 1;
  2492. *DestinationPointer = '0' + ((Character % 100) / 10);
  2493. DestinationPointer += 1;
  2494. *DestinationPointer = '0' + (Character % 10);
  2495. DestinationPointer += 1;
  2496. } else {
  2497. if (DestinationPointer >= End) {
  2498. errno = EMSGSIZE;
  2499. return -1;
  2500. }
  2501. *DestinationPointer = Character;
  2502. DestinationPointer += 1;
  2503. }
  2504. Size -= 1;
  2505. }
  2506. }
  2507. if (DestinationPointer == Destination) {
  2508. if (DestinationPointer >= End) {
  2509. errno = EMSGSIZE;
  2510. return -1;
  2511. }
  2512. *DestinationPointer = '.';
  2513. DestinationPointer += 1;
  2514. }
  2515. if (DestinationPointer >= End) {
  2516. errno = EMSGSIZE;
  2517. return -1;
  2518. }
  2519. *DestinationPointer = '\0';
  2520. DestinationPointer += 1;
  2521. return (DestinationPointer - Destination);
  2522. }
  2523. INT
  2524. ClpDnsFindName (
  2525. PUCHAR Domain,
  2526. PUCHAR Message,
  2527. PUCHAR *DomainNames,
  2528. PUCHAR *LastDomainName
  2529. )
  2530. /*++
  2531. Routine Description:
  2532. This routine attempts to find the counted label name in an array of
  2533. compressed names.
  2534. Arguments:
  2535. Domain - Supplies the domain to search for.
  2536. Message - Supplies a pointer to the start of the query or response.
  2537. DomainNames - Supplies an array of compressed names to search.
  2538. LastDomainName - Supplies one beyond the last element in the domain names
  2539. array.
  2540. Return Value:
  2541. Returns the offset from the start of the message if found.
  2542. -1 if not found.
  2543. --*/
  2544. {
  2545. PUCHAR *CurrentDomain;
  2546. PUCHAR CurrentPointer;
  2547. PUCHAR DomainName;
  2548. UINT Size;
  2549. PUCHAR Start;
  2550. CurrentDomain = DomainNames;
  2551. while (CurrentDomain < LastDomainName) {
  2552. DomainName = Domain;
  2553. CurrentPointer = *CurrentDomain;
  2554. Start = *CurrentDomain;
  2555. Size = *CurrentPointer;
  2556. CurrentPointer += 1;
  2557. while (Size != 0) {
  2558. if ((Size & DNS_COMPRESSION_MASK) == 0) {
  2559. if (*DomainName != Size) {
  2560. break;
  2561. }
  2562. DomainName += 1;
  2563. while (Size > 0) {
  2564. if (tolower(*DomainName) != tolower(*CurrentPointer)) {
  2565. break;
  2566. }
  2567. DomainName += 1;
  2568. CurrentPointer += 1;
  2569. Size -= 1;
  2570. }
  2571. if (Size != 0) {
  2572. break;
  2573. }
  2574. if ((*DomainName == '\0') && (*CurrentPointer == '\0')) {
  2575. return Start - Message;
  2576. }
  2577. if (*DomainName == '\0') {
  2578. break;
  2579. }
  2580. } else if ((Size & DNS_COMPRESSION_MASK) == DNS_COMPRESSION_VALUE) {
  2581. CurrentPointer = Message +
  2582. (((Size & (~DNS_COMPRESSION_MASK)) <<
  2583. BITS_PER_BYTE) |
  2584. *CurrentPointer);
  2585. } else {
  2586. errno = EMSGSIZE;
  2587. return -1;
  2588. }
  2589. }
  2590. CurrentDomain += 1;
  2591. }
  2592. errno = ENOENT;
  2593. return -1;
  2594. }
  2595. INT
  2596. ClpDnsSkipName (
  2597. PUCHAR *Name,
  2598. PUCHAR MessageEnd
  2599. )
  2600. /*++
  2601. Routine Description:
  2602. This routine skips a compressed DNS name.
  2603. Arguments:
  2604. Name - Supplies a pointer that on input points to the name to skip. On
  2605. successful output, this will point after the compressed name.
  2606. MessageEnd - Supplies a pointer to the end of the DNS query or response,
  2607. to avoid buffer overruns.
  2608. Return Value:
  2609. 0 on success.
  2610. -1 on failure, an errno will be set to contain more information.
  2611. --*/
  2612. {
  2613. PUCHAR Current;
  2614. UINT Size;
  2615. Current = *Name;
  2616. while (Current <= MessageEnd) {
  2617. Size = *Current;
  2618. Current += 1;
  2619. if (Size == 0) {
  2620. break;
  2621. }
  2622. switch (Size & DNS_COMPRESSION_MASK) {
  2623. case 0:
  2624. Current += Size;
  2625. continue;
  2626. case DNS_COMPRESSION_VALUE:
  2627. Current += 1;
  2628. break;
  2629. default:
  2630. errno = EMSGSIZE;
  2631. return -1;
  2632. }
  2633. break;
  2634. }
  2635. if (Current > MessageEnd) {
  2636. errno = EMSGSIZE;
  2637. return -1;
  2638. }
  2639. *Name = Current;
  2640. return 0;
  2641. }
  2642. INT
  2643. ClpCompareIp4Addresses (
  2644. struct sockaddr_in *Address1,
  2645. struct sockaddr_in *Address2
  2646. )
  2647. /*++
  2648. Routine Description:
  2649. This routine compares two IPv4 addresses.
  2650. Arguments:
  2651. Address1 - Supplies a pointer to the first address.
  2652. Address2 - Supplies a pointer to the second address.
  2653. Return Value:
  2654. TRUE if the addresses are the same.
  2655. FALSE if the addresses are different.
  2656. --*/
  2657. {
  2658. if ((Address1->sin_family == Address2->sin_family) &&
  2659. (Address1->sin_port == Address2->sin_port) &&
  2660. (Address1->sin_addr.s_addr == Address2->sin_addr.s_addr)) {
  2661. return TRUE;
  2662. }
  2663. return FALSE;
  2664. }