X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/4782a5c5afee47721cc617daa40dd29828342c2b..8dbf997db86622966c80c12998c2b39577de949f:/dnscore.pas?ds=inline diff --git a/dnscore.pas b/dnscore.pas index bb4fab4..ef4c2f1 100755 --- a/dnscore.pas +++ b/dnscore.pas @@ -54,25 +54,33 @@ } unit dnscore; - - {$ifdef fpc}{$mode delphi}{$endif} - - - +{$include lcoreconfig.inc} interface uses binipstuff,classes,pgtypes; var usewindns : boolean = {$ifdef win32}true{$else}false{$endif}; -//hint to users of this unit that they should use windows dns instead. -//May be disabled by applications if desired. (e.g. if setting a custom -//dnsserverlist). +{hint to users of this unit that they should use windows dns instead. +May be disabled by applications if desired. (e.g. if setting a custom +dnsserverlist). -//note: this unit will not be able to self populate it's dns server list on -//older versions of windows. +note: this unit will not be able to self populate it's dns server list on +older versions of windows.} + +const + useaf_default=0; + useaf_preferv4=1; + useaf_preferv6=2; + useaf_v4=3; + useaf_v6=4; +{ +hint to users of this unit to how to deal with connecting to hostnames regarding ipv4 or ipv6 usage +can be set by apps as desired +} +var useaf:integer = useaf_default; const maxnamelength=127; @@ -115,6 +123,7 @@ type parsepacket:boolean; resultstr:string; resultbin:tbinip; + resultlist:tbiniplist; resultaction:integer; numrr1:array[0..3] of integer; numrr2:integer; @@ -147,7 +156,9 @@ type //if you must but please document them at the same time --plugwash //function buildrequest(const name:string;var packet:tdnspacket;requesttype:word):integer; -//function makereversename(const binip:tbinip):string; + +//returns the DNS name used to reverse look up an IP, such as 4.3.2.1.in-addr.arpa for 1.2.3.4 +function makereversename(const binip:tbinip):string; procedure setstate_request_init(const name:string;var state:tdnsstate); @@ -337,25 +348,37 @@ end; {==============================================================================} -procedure setstate_return(const rrp:trrpointer;len:integer;var state:tdnsstate); -var - a:integer; +function getipfromrr(const rrp:trrpointer;len:integer):tbinip; begin - state.resultaction := action_done; - state.resultstr := ''; + fillchar(result,sizeof(result),0); case trr(rrp.p^).requesttype of querytype_a: begin if htons(trr(rrp.p^).datalen) <> 4 then exit; - move(trr(rrp.p^).data,state.resultbin.ip,4); - state.resultbin.family :=AF_INET; + move(trr(rrp.p^).data,result.ip,4); + result.family :=AF_INET; end; {$ifdef ipv6} querytype_aaaa: begin if htons(trr(rrp.p^).datalen) <> 16 then exit; - state.resultbin.family := AF_INET6; - move(trr(rrp.p^).data,state.resultbin.ip6,16); + result.family := AF_INET6; + move(trr(rrp.p^).data,result.ip6,16); end; {$endif} + else + {} + end; +end; + +procedure setstate_return(const rrp:trrpointer;len:integer;var state:tdnsstate); +var + a:integer; +begin + state.resultaction := action_done; + state.resultstr := ''; + case trr(rrp.p^).requesttype of + querytype_a{$ifdef ipv6},querytype_aaaa{$endif}: begin + state.resultbin := getipfromrr(rrp,len); + end; else {other reply types (PTR, MX) return a hostname} state.resultstr := decodename(state.recvpacket,state.recvpacketlen,taddrint(rrp.p)-taddrint(@state.recvpacket)+10,0,a); @@ -456,6 +479,19 @@ begin goto failure; end; + {if we requested A or AAAA build a list of all replies} + if (state.requesttype = querytype_a) or (state.requesttype = querytype_aaaa) then begin + state.resultlist := biniplist_new; + for a := state.numrr1[0] to (state.numrr1[0]+state.numrr1[1]-1) do begin + rrptemp := @state.rrdata[1+a*sizeof(trrpointer)]; + rrtemp := rrptemp.p; + b := rrptemp.len; + if rrtemp.requesttype = state.requesttype then begin + biniplist_add(state.resultlist,getipfromrr(rrptemp^,b)); + end; + end; + end; + {- check for items of the requested type in answer section, if so return success first} for a := state.numrr1[0] to (state.numrr1[0]+state.numrr1[1]-1) do begin rrptemp := @state.rrdata[1+a*sizeof(trrpointer)]; @@ -587,6 +623,7 @@ begin {$ifdef win32} if iphlpapi=0 then iphlpapi := loadlibrary('iphlpapi.dll'); if not assigned(getnetworkparams) then @getnetworkparams := getprocaddress(iphlpapi,'GetNetworkParams'); + if not assigned(getnetworkparams) then exit; fixed_info_len := 0; if GetNetworkParams(nil,@fixed_info_len)<>ERROR_BUFFER_OVERFLOW then exit; //fixed_info_len :=sizeof(tfixed_info); @@ -635,7 +672,7 @@ begin end; function getcurrentsystemnameserver(var id:integer):string; -var +var counter : integer; begin