18. november 2008 - 08:03Der er
22 kommentarer og 1 løsning
Finde ip på 'ekstern' host
Kan man finde IP nummer på en ekstern host? Jeg kan kun få WinSock funktionen GetHostByName til at finde den lokale host synes jeg. Det er måske et eller andet DNS opslag der skal til.
interface uses Windows, WinSock; type IStringTokenizer = interface ['{8C216E9D-984E-4E38-893F-0A222AC547DA}'] function GetTokenCounts: Integer; function HasMoreTokens: Boolean; function NextToken: string; property countTokens: Integer read getTokenCounts; end;
function FirstDelimiter(const Delimiters: string; const Str: string): Integer; overload; function StringTokenizer(const Str: string; const delim: string): IStringTokenizer; function IpAdressToDOWRD(const IpAdress: string): DWORD; function DOWRDToIpadress(const Value: DWORD): string; function GetIpFromHostname(const Hostname: string): string; function GetDWORDFromHostname(const Hostname: string): DWORD;
implementation uses SysUtils;
type TStringTokenizer = class(TInterfacedObject, IStringTokenizer) private FString: string; FDelim: string; FCurPos: Integer; public constructor Create(const Str: string; delim: string); function GetTokenCounts: Integer; function HasMoreTokens: Boolean; function NextToken: string; property CountTokens: Integer read GetTokenCounts; end;
function _IndexOf(const ch: Char; const Str: string): Integer; overload; var I: Integer; begin Result := 0; I := 1; while I <= Length(Str) do begin if Str[I] = ch then begin Result := I; Exit; end;
Inc(I); end; end;
function FirstDelimiter(const Delimiters: string; const Str: string): Integer; var I: Integer; begin Result := 0; I := 1; while I <= Length(Str) do begin if Str[I] in LeadBytes then Inc(I) else if _IndexOf(Str[I], delimiters) > 0 then begin Result := I; Exit; end; Inc(I); end; end;
function StringTokenizer(const Str: string; const delim: string): IStringTokenizer; begin Result := TStringTokenizer.Create(Str, delim); end;
constructor TStringTokenizer.Create(const Str: string; delim: string); begin FString := Str; FDelim := delim; FCurPos := 1; { Point FCurPos to beginning of next Token } while (FCurPos <= Length(FString)) and (_IndexOf(FString[FCurPos], FDelim) > 0) do Inc(FCurPos); end;
function TStringTokenizer.GetTokenCounts: Integer; var I: Integer; Cnt: Integer; begin Cnt := 0; I := 1; while I <= Length(FString) do begin while (I <= Length(FString)) and (_IndexOf(FString[I], FDelim) > 0) do Inc(I); if I <= Length(FString) then Inc(Cnt); while (I <= Length(FString)) and (_IndexOf(FString[I], FDelim) = 0) do Inc(I); end; Result := Cnt; end;
function TStringTokenizer.HasMoreTokens: Boolean; begin Result := FCurPos < Length(FString); end;
function TStringTokenizer.NextToken: string; var endPos: Integer; begin if FCurPos > Length(FString) then begin Result := ''; Exit; end; endPos := FCurPos;
while (endPos <= Length(FString)) and (_IndexOf(FString[endPos], FDelim) = 0) do Inc(endPos);
Result := Copy(FString, FCurPos, endPos - FCurPos); { Point FCurPos to beginning of next Token } FCurPos := endPos;
while (FCurPos <= Length(FString)) and (_IndexOf(FString[FCurPos], FDelim) > 0) do Inc(FCurPos); end;
function IpAdressToDOWRD(const IpAdress: string): DWORD; begin Result := DWORD(ntohl(inet_addr(Pointer(IpAdress)))); end;
function DOWRDToIpadress(const Value: DWORD): string; begin Result := iNet_ntoa(in_addr(Value)); end;
function GetIpFromHostname(const Hostname: string): string; var MyWSAData: WSADATA; RHost: pHostent; Ptr: Pointer; Remote_Addr: In_Addr; begin WSAStartup($0101, MyWSAData); RHost := gethostbyname(pointer(hostname));
Ptr := RHost^.H_Addr_List^;
Move(Ptr^, Remote_Addr, 4); Result := inet_ntoa(Remote_Addr); WSACleanup; end;
function GetDWORDFromHostname(const Hostname: string): DWORD; var Ip: string; begin Ip := GetIpFromHostname(Hostname); Result := IpAdressToDOWRD(Ip); end; end.
Det hjalp lidt - nu går det kun ned hvis jeg prøver at finde IP på en host der ikke findes. Det er linien Ptr := RHost^.H_Addr_List^; der får det til at crache RHost er nil men jeg kan ikke få den til at teste på det synes jeg.
function GetIpFromHostname(const Hostname: str): str; var MyWSAData: WSADATA; RHost: pHostent; p:pChar;//ellers: "pAnsiChar" Remote_Addr: In_Addr; begin result:=''; wsaStartup($0101, MyWSAData); //det bør være nok at anføre som pChar ovenfor, og så kalde med pointer her. p:=pointer(hostname); if (p<>nil) and (p^<>#0) then begin rHost:=getHostByName(p); p:=rHost^.H_Addr_List^; if (p<>nil) and (p^<>#0) then move(p^,remote_addr,4); end; result:=inet_ntoa(remote_addr); wsaCleanup; end;
Ting der indeholder noget fornuftigt er ikke spam :-)
/Hugo
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.