Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6.  
  7. namespace SocketSniffConsole {
  8.     public enum IPFlags {
  9.         Reserved = 0x80,
  10.         DoNotFragment = 0x40,
  11.         MoreFragments = 0x20
  12.     }
  13.  
  14.     public enum PacketProtocol {
  15.         Tcp = 6,
  16.         Udp = 57
  17.     }
  18.  
  19.     public class IPPacket {
  20.         public int Version { get; set; }
  21.         public int HeaderLength { get; set; }
  22.         public int TypeOfService { get; set; }
  23.         public int TotalLength { get; set; }
  24.         public int Identification { get; set; }
  25.         public IPFlags Flags { get; set; }
  26.         public int FragmentOffset { get; set; }
  27.         public int TimeToLive { get; set; }
  28.         public PacketProtocol Protocol { get; set; }
  29.         public int Checksum { get; set; }
  30.         public IPAddress Source { get; set; }
  31.         public IPAddress Destination { get; set; }
  32.         public byte[] Options { get; set; }
  33.         public byte[] Payload { get; set; }
  34.  
  35.         public IPPacket() {
  36.         }
  37.  
  38.         public static IPPacket Parse(byte[] raw) {
  39.             IPPacket packet;
  40.  
  41.             packet = new IPPacket();
  42.             packet.Version = (raw[0] & 0xF0) >> 4;
  43.             packet.HeaderLength = (raw[0] & 0x0F);
  44.             packet.TypeOfService = raw[1];
  45.             packet.TotalLength = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(raw, 2)); // LOL
  46.             packet.Identification = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(raw, 4));
  47.             packet.Flags = (IPFlags)(raw[6] & 0x70);
  48.             packet.FragmentOffset = (BitConverter.ToInt32(raw, 6) & 0x1FFF);
  49.             packet.TimeToLive = raw[8];
  50.             packet.Protocol = (PacketProtocol)raw[9];
  51.             packet.Checksum = (int)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(raw, 10));
  52.             packet.Source = new IPAddress(new byte[] { raw[12], raw[13], raw[14], raw[15] });
  53.             packet.Destination = new IPAddress(new byte[] { raw[16], raw[17], raw[18], raw[19] });
  54.  
  55.             int optionsLength = packet.HeaderLength * 5 - 20;
  56.             packet.Options = new byte[optionsLength];
  57.             if (optionsLength > 0)
  58.                 Array.Copy(raw, 20, packet.Options, 0,  optionsLength);
  59.  
  60.             int payloadLength = packet.TotalLength - 20 - optionsLength;
  61.             packet.Payload = new byte[payloadLength];
  62.             Array.Copy(raw, 20 + optionsLength, packet.Payload, 0, payloadLength);
  63.             return packet;
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement