/* * Paglo Crawler * Copyright (C) 2006-2008 Paglo Labs Inc. All rights reserved. * www.paglo.com * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ //--------------------------------------------------------------------------- #ifndef PacketUtilsH #define PacketUtilsH //--------------------------------------------------------------------------- #ifdef HAVE_CONFIG_H # include "config.h" #endif #include using namespace std; #if defined(WIN32) # include # include # include #else # ifdef HAVE_SYS_SOCKET_H # include # endif # include # ifdef HAVE_NETINET_IN_SYSTM_H # include # endif # ifdef HAVE_NETINET_IP_H # include # endif #endif #pragma pack(push, 1) /* * Ethernet header. */ typedef struct { unsigned char EthDest[6]; unsigned char EthSource[6]; unsigned short EthProto; } TEthHeader; //--------------------------------------------------------------------------- /* * Ethernet LLC header. */ typedef struct { unsigned char DSAP; unsigned char SSAP; unsigned char Control; } TLLCHeader; //--------------------------------------------------------------------------- /* * Ethernet SNAP header. */ typedef struct { unsigned char OrgCode[3]; unsigned short PID; } TSNAPHeader; //--------------------------------------------------------------------------- /* * IPv4 header. */ typedef struct { unsigned char HeaderLen:4; unsigned char Version:4; unsigned char TOS; short Length; unsigned short ID; short FragmentOffset; unsigned char TTL; unsigned char Protocol; unsigned short Checksum; unsigned long Source; unsigned long Dest; } TIPv4Header; //--------------------------------------------------------------------------- #define TCP_FIN 0x01 #define TCP_SYN 0x02 #define TCP_RST 0x04 #define TCP_PSH 0x08 #define TCP_ACK 0x10 #define TCP_URG 0x20 /* * IPv4 TCP header. */ typedef struct { unsigned short SourcePort; unsigned short DestPort; unsigned long SeqNum; unsigned long AckNum; unsigned char Unused:4; unsigned char Offset:4; unsigned char Flags; unsigned short Window; unsigned short Checksum; unsigned short UrgPointer; } TTCPv4Header; //--------------------------------------------------------------------------- typedef struct { unsigned short SourcePort; unsigned short DestPort; unsigned short Length; unsigned short Checksum; } TUDPv4Header; //--------------------------------------------------------------------------- /* * IPv4 psuedo header. */ typedef struct { unsigned long SourceIP; unsigned long DestIP; unsigned char Checksum; unsigned char Protocol; unsigned short Length; } TIPPsuedoHeader; //--------------------------------------------------------------------------- /* * IPv4 ARP header. */ typedef struct { unsigned char EthDest[6]; unsigned char EthSource[6]; unsigned short EthProto; unsigned short ARPHardwareType; unsigned short ARPProtocolType; unsigned char ARPHardwareAddressLength; unsigned char ARPProtocolAddressLength; unsigned short ARPOpcode; unsigned char ARPSourceHW[6]; unsigned long ARPSourceProto; unsigned char ARPTargetHW[6]; unsigned long ARPTargetProto; } TARPHeader; //--------------------------------------------------------------------------- /* * IPv4 ICMP header. */ typedef struct { unsigned char Type; unsigned char Code; unsigned short Checksum; unsigned short ID; unsigned short Sequence; } TICMPHeader; #pragma pack(pop) //--------------------------------------------------------------------------- /* * Class to contain handling of IPv4 or IPv6 addresses. */ class TIPAddress { protected: sockaddr_in IPv4Address; sockaddr_in6 IPv6Address; sockaddr *Address; /* * Initialize the internal address. */ void InitAddress(sockaddr *Addr); public: TIPAddress(); /* * Initialize based on a sockaddr. */ TIPAddress(sockaddr *Addr); /* * Initialize based on a name. */ TIPAddress(string Name); /* * Initialize based on an IPv4 binary value. */ TIPAddress(unsigned long Addr); /* * Copy constructor. */ TIPAddress(TIPAddress const &IPAddr); /* * Test the address type. */ bool IsIPv4(); bool IsIPv6(); /* * Return a binary value (in host endianess). */ unsigned long IPv4Value(); /* * How many bits are set? */ unsigned short Bits(void); /* * Create a printable string. */ string Print(); /* * Return information necessary for creating and connecting a socket. */ int AddressFamily(); sockaddr *AddressData(); int AddressDataLength(); /* * Returns true if the IP address is not all zeros. */ bool Valid(); /* * Assignment operator. */ void operator =(TIPAddress const &CopyFrom); /* * Comparisons. */ bool operator ==(TIPAddress &IPAddr); bool operator !=(TIPAddress &IPAddr); bool operator <(TIPAddress &IPAddr); bool operator <=(TIPAddress &IPAddr); bool operator >(TIPAddress &IPAddr); bool operator >=(TIPAddress &IPAddr); }; //--------------------------------------------------------------------------- class TCIDRBlock { public: TCIDRBlock(void) {} TCIDRBlock(TIPAddress &A, TIPAddress &M){ Address = A; Mask = M; } TCIDRBlock(std::string CIDRStr); ~TCIDRBlock(void){} void SetAddress(TIPAddress &A){ Address = A; } TIPAddress GetAddress(void){ return Address; } void SetMask(TIPAddress &M){ Mask = M; } TIPAddress GetMask(void){ return Mask; } bool Includes(TIPAddress &); std::string Print(void); bool operator ==(TCIDRBlock &Block); private: TIPAddress Address; TIPAddress Mask; }; //--------------------------------------------------------------------------- /* * Class for manipulating MAC addresses. */ class TMacAddress { protected: unsigned char AddressData[6]; public: TMacAddress(); TMacAddress(string Address); TMacAddress(unsigned char Address[6]); /* * Return a binary value. */ unsigned char *Value(); /* * Create a printable string. */ string Print(); /* * Returns true if the MAC address is not all zeros. */ bool Valid(); /* * Assignment operator. */ void operator =(unsigned char *Address); /* * Equivalence. */ bool operator ==(TMacAddress &MacAddr); bool operator !=(TMacAddress &MacAddr); }; //--------------------------------------------------------------------------- /* * Convert addresses to strings. */ #ifdef WIN32 string __fastcall PrintIPv4Address(unsigned long Addr); string __fastcall PrintMacAddress(unsigned char *Address); #else string PrintIPv4Address(unsigned long Addr); string PrintMacAddress(unsigned char *Address); #endif /* * Get the time since startup in milliseconds. */ unsigned long MilliSecondTimer(); /* * Find the MAC address of the local interface. */ void LocalMacAddress(const std::string &InterfaceName, TMacAddress &MacAddress); /* * Construct a TCP SYN packet. Buffer must be at least 52 bytes in size. */ void ConstructTCP(unsigned char *Buffer, TMacAddress SourceMac, TMacAddress DestMac, TIPAddress SourceIP, TIPAddress DestIP, unsigned short SourcePort, unsigned short DestPort, unsigned char Flags); int PartialChecksum(unsigned char *Data, int Length, int Checksum); unsigned short CompleteChecksum(int Checksum); #endif