⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 u_ippacket.pas

📁 linux program to read packet data
💻 PAS
字号:
(*
 * One Way Network Sniffer (OWNS)
 * Copyright (C) 2001-2002 OWNS
 *
 * http://owns.sourceforge.net/
 * http://www.owns.st
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *)

(* $Id: u_IPPacket.pas,v 1.4 2001/08/05 13:13:45 owns Exp $ *)
unit u_IPPacket;
interface
uses // windows,
     u_EthernetFrame,SysUtils;


////////////////////////////////////////////////////////////////////////////////
//
// Paquet IP
//
////////////////////////////////////////////////////////////////////////////////

type

IP_ADDR = array[0..3] of byte;
PIP_ADDR = ^IP_ADDR;

// IPv6 Header
IP_RHDR = packed record
  Verlen:       Byte; // Version sur 4 bits Longueur de l'entete IP en mot de 32 bits
  Service:      Byte;
  Length:       array[0..1] of Byte;
  Ident:        array[0..1] of Byte;
  Flagoff:      array[0..1] of Byte;
  TimeLive:     Byte;
  Protocol:     Byte;
  Checksum:     array[0..1] of Byte;
  Source:       IP_ADDR;
  Destination:  IP_ADDR;
  Data:         array[0..0] of Byte;   // Options puis donn閑s
end;
PIP_RHDR = ^IP_RHDR;

  TIPPacket = class(TEthernetFrame)
  private
    FpIPHdr: PIP_RHDR;
    function getIPProtocol : Byte;
    function getIPSourceAddr : PIP_ADDR;
    function getIPDestAddr : PIP_ADDR;
    function getIPData : PChar;
    function getIPDataLength : Word;
    function getLEI : Word;
  protected
  public
    constructor Create(uBuffer: PChar); override;
    destructor Destroy; override;

    property IPProtocol: byte read getIPProtocol;
    property IPHeader: PIP_RHDR read FpIPHdr;
    property IPSourceAddr: PIP_ADDR read getIPSourceAddr;
    property IPDestAddr: PIP_ADDR read getIPDestAddr;
    property IPData: PChar read getIPData;
    property IPDataLength : word read getIPDataLength;
  end;

function AreIpAddrEquals(p_addr1 : PIP_ADDR;p_addr2 : PIP_ADDR) : boolean;
function IPToStr(p_IP : PIP_ADDR) : String;

implementation

function AreIpAddrEquals(p_addr1 : PIP_ADDR;p_addr2 : PIP_ADDR) : boolean;
var
  i : Byte;
begin
  result := True;
  for i := 0 To 3 do
    if (p_Addr1[i] <> p_Addr2[i]) Then result := false;
end;

function IPToStr(p_IP : PIP_ADDR) : String;
begin
  result := IntToStr(p_IP[0])+'.'+IntToStr(p_IP[1])+'.'+
            IntToStr(p_IP[2])+'.'+IntToStr(p_IP[3]);
end;

// parse an IP address
// EConvertError is raised if p_IPAddress is not an IP Address
function parseIPAddr(p_IPAddress : String) : IP_ADDR;
var
  l_IPAddress : IP_ADDR;
  i,j : Byte;
  l_Partie : String;
begin
  j := 1;
  l_partie := '';

  for i := 1 to Length(p_IPAddress) do
  begin
    if p_IPAddress[i] = '.' then
    begin
      l_IPAddress[j] := StrToInt(l_partie);
      l_Partie := '';
      Inc(j);
      if (j = 4) then
        raise EConvertError.create('Incorrect IP address');
    end
    else
      l_partie := l_partie+p_IPAddress[i];
  end;
  l_IPAddress[j] := StrToInt(l_partie);
  result := l_IPAddress;
end;


constructor TIPPacket.Create(uBuffer: PChar);
begin
  inherited Create(uBuffer);
  FpIPHdr := PIP_RHDR(EthernetData);
  getLEI;
end;

// donne la longueur de l'entete IP
// 20 minimum
function TIPPacket.getLEI : Word;
begin
  result := (FpIPHdr^.Verlen and $0F) shl 2;
end;

// 1 : ICMP
// 6 : TCP
// 17 : UDP ...
function TIPPacket.getIPProtocol : byte;
begin
  result := FpIPHdr^.Protocol;
end;

// returns the source IP address
function TIPPacket.getIPSourceAddr : PIP_ADDR;
begin
  result := @(FpIPHdr^.Source[0]);

end;

// returns the destination IP address
function TIPPacket.getIPDestAddr : PIP_ADDR;
begin
  result := @(FpIPHdr^.Destination[0]);
end;

// returns IP data
// rem : l'entete fait souvent 20 octets mais pas toujours 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -