X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/4782a5c5afee47721cc617daa40dd29828342c2b..refs/heads/delphi2010:/dnswin.pas diff --git a/dnswin.pas b/dnswin.pas old mode 100755 new mode 100644 index 7d986d1..847f0fd --- a/dnswin.pas +++ b/dnswin.pas @@ -1,13 +1,16 @@ unit dnswin; interface -uses binipstuff,classes,lcore; + +uses binipstuff,classes,lcore,pgtypes; + +{$include lcoreconfig.inc} //on failure a null string or zeroed out binip will be retuned and error will be //set to a windows error code (error will be left untouched under non error //conditions). -function winforwardlookup(name : string;ipv6preffered : boolean;var error:integer) : tbinip; -function winreverselookup(ip:tbinip;var error:integer):string; +function winforwardlookuplist(name : thostname;familyhint:integer;var error : integer) : tbiniplist; +function winreverselookup(ip:tbinip;var error:integer):thostname; type @@ -17,7 +20,6 @@ type //release should only be called from the main thread tdnswinasync=class(tthread) private - ipv6preffered : boolean; freverse : boolean; error : integer; freewhendone : boolean; @@ -26,10 +28,10 @@ type procedure execute; override; public onrequestdone:tsocketevent; - name : string; - ip : tbinip; + name : thostname; + iplist : tbiniplist; - procedure forwardlookup(name:string;ipv6preffered:boolean); + procedure forwardlookup(name:thostname); procedure reverselookup(ip:tbinip); destructor destroy; override; procedure release; @@ -40,7 +42,8 @@ type implementation uses - lsocket,pgtypes,sysutils,winsock,windows,messages; + { zipplet: moved pgtypes to interface because it's needed for the string definitions } + lsocket,sysutils,winsock,windows,messages; type //taddrinfo = record; //forward declaration @@ -51,55 +54,75 @@ type ai_socktype : longint; ai_protocol : longint; ai_addrlen : taddrint; - ai_canonname : pchar; + ai_canonname : pansichar; ai_addr : pinetsockaddrv; ai_next : paddrinfo; end; ppaddrinfo = ^paddrinfo; - tgetaddrinfo = function(nodename : pchar; servname : pchar; hints : paddrinfo;res : ppaddrinfo) : longint; stdcall; + tgetaddrinfo = function(nodename : pansichar; servname : pansichar; hints : paddrinfo;res : ppaddrinfo) : longint; stdcall; tfreeaddrinfo = procedure(ai : paddrinfo); stdcall; - tgetnameinfo = function(sa:Pinetsockaddrv;salen : longint; host:pchar;hostlen : longint;serv:pchar;servlen:longint;flags:longint) : longint;stdcall; + tgetnameinfo = function(sa:Pinetsockaddrv;salen : longint; host:pansichar;hostlen : longint;serv:pansichar;servlen:longint;flags:longint) : longint;stdcall; var getaddrinfo : tgetaddrinfo; freeaddrinfo : tfreeaddrinfo; getnameinfo : tgetnameinfo; procedure v4onlyfreeaddrinfo(ai : paddrinfo); stdcall; +var + next:paddrinfo; begin - freemem(ai.ai_addr); - freemem(ai); + while assigned(ai) do begin + freemem(ai.ai_addr); + next := ai.ai_next; + freemem(ai); + ai := next; + end; end; type plongint = ^longint; pplongint = ^plongint; -function v4onlygetaddrinfo(nodename : pchar; servname : pchar; hints : paddrinfo;res : ppaddrinfo) : longint; stdcall; +function v4onlygetaddrinfo(nodename : pansichar; servname : pansichar; hints : paddrinfo;res : ppaddrinfo) : longint; stdcall; var - output : paddrinfo; + output,prev,first : paddrinfo; hostent : phostent; + addrlist:^pointer; begin - if hints.ai_family = af_inet then begin + if hints.ai_family <> af_inet6 then begin result := 0; - getmem(output,sizeof(taddrinfo)); - getmem(output.ai_addr,sizeof(tinetsockaddr)); - output.ai_addr.InAddr.family := af_inet; - if servname <> nil then output.ai_addr.InAddr.port := htons(strtoint(servname)) else output.ai_addr.InAddr.port := 0; + + hostent := gethostbyname(nodename); if hostent = nil then begin result := wsagetlasterror; v4onlyfreeaddrinfo(output); exit; end; - output.ai_addr.InAddr.addr := pplongint(hostent.h_addr_list)^^; - output.ai_flags := 0; - output.ai_family := af_inet; - output.ai_socktype := 0; - output.ai_protocol := 0; - output.ai_addrlen := sizeof(tinetsockaddr); - output.ai_canonname := nil; - output.ai_next := nil; - - res^ := output; + addrlist := pointer(hostent.h_addr_list); + + //ipint := pplongint(hostent.h_addr_list)^^; + prev := nil; + first := nil; + repeat + if not assigned(addrlist^) then break; + + getmem(output,sizeof(taddrinfo)); + if assigned(prev) then prev.ai_next := output; + getmem(output.ai_addr,sizeof(tinetsockaddr)); + if servname <> nil then output.ai_addr.InAddr.port := htons(strtoint(servname)) else output.ai_addr.InAddr.port := 0; + output.ai_addr.InAddr.addr := longint(addrlist^^); + inc(integer(addrlist),4); + output.ai_flags := 0; + output.ai_family := af_inet; + output.ai_socktype := 0; + output.ai_protocol := 0; + output.ai_addrlen := sizeof(tinetsockaddr); + output.ai_canonname := nil; + output.ai_next := nil; + prev := output; + if not assigned(first) then first := output; + until false; + res^ := first; end else begin result := WSANO_RECOVERY; end; @@ -110,7 +133,7 @@ begin if a 0 then begin fillchar(result,0,sizeof(result)); error := getaddrinforesult; end; end; -function winreverselookup(ip:tbinip;var error : integer):string; +function winreverselookup(ip:tbinip;var error : integer):thostname; var sa : tinetsockaddrv; getnameinforesult : integer; begin - if ip.family = AF_INET then begin - sa.InAddr.family := AF_INET; - sa.InAddr.port := 1; - sa.InAddr.addr := ip.ip; - end else {$ifdef ipv6}if ip.family = AF_INET6 then begin - sa.InAddr6.sin6_family := AF_INET6; - sa.InAddr6.sin6_port := 1; - sa.InAddr6.sin6_addr := ip.ip6; - end else{$endif} begin - raise exception.create('unrecognised address family'); - end; + makeinaddrv(ip,'1',sa); populateprocvars; setlength(result,1025); - getnameinforesult := getnameinfo(@sa,sizeof(tinetsockaddrv),pchar(result),length(result),nil,0,0); + getnameinforesult := getnameinfo(@sa,sizeof(tinetsockaddrv),pansichar(result),length(result),nil,0,0); if getnameinforesult <> 0 then begin error := getnameinforesult; result := ''; @@ -256,35 +271,36 @@ begin end; end; -procedure tdnswinasync.forwardlookup(name:string;ipv6preffered:boolean); +procedure tdnswinasync.forwardlookup(name:thostname); begin self.name := name; - self.ipv6preffered := ipv6preffered; freverse := false; resume; end; procedure tdnswinasync.reverselookup(ip:tbinip); begin - self.ip := ip; + iplist := biniplist_new; + biniplist_add(iplist,ip); freverse := true; resume; end; + procedure tdnswinasync.execute; var error : integer; + begin error := 0; if reverse then begin - name := winreverselookup(ip,error); + name := winreverselookup(biniplist_get(iplist,0),error); end else begin - ip := winforwardlookup(name,ipv6preffered,error); + iplist := winforwardlookuplist(name,0,error); end; - postmessage(hwnddnswin,wm_user,error,taddrint(self)); end; -destructor tdnswinasync.destroy; +destructor tdnswinasync.destroy; begin WaitFor; inherited destroy;