CGC_Codes

find net mask

Jun 6th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5.  
  6. public class FindMask
  7. {
  8.     public static void Main()
  9.     {
  10.         byte[] data = new byte[1024];
  11.         int recv;
  12.         Socket host = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.Icmp);
  13.         IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 0);
  14.         EndPoint ep = (EndPoint)iep;
  15.         ICMP packet = new ICMP();
  16.  
  17.         packet.Type = 0x11;
  18.         packet.Code = 0x00;
  19.         packet.Checksum = 0;
  20.         Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 0, 2);
  21.         Buffer.BlockCopy(BitConverter.GetBytes(1), 0, packet.Message, 2, 2);
  22.         Buffer.BlockCopy(BitConverter.GetBytes(0), 0, packet.Message, 4, 4);
  23.         packet.MessageSize = 8;
  24.         int packetsize = packet.MessageSize + 4;
  25.  
  26.         UInt16 chksm = packet.getChecksum();
  27.         packet.Checksum = chksm;
  28.  
  29.         host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000);
  30.         host.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
  31.         host.SendTo(packet.getBytes(), packetsize, SocketFlags.None, iep);
  32.         try
  33.         {
  34.             data = new byte[1024];
  35.             recv = host.ReceiveFrom(data, ref ep);
  36.         }
  37.         catch (SocketException)
  38.         {
  39.             Console.WriteLine("Unable to determine subnet mask for this subnet");
  40.             return;
  41.         }
  42.         ICMP response = new ICMP(data, recv);
  43.         Console.WriteLine("Received ICMP Type {0} packet", response.Type);
  44.         long answer = BitConverter.ToUInt32(response.Message, 4);
  45.         IPAddress netmask = new IPAddress(answer);
  46.         Console.WriteLine("The subnet mask for this subnet is: {0}", netmask.ToString());
  47.     }
  48. }
  49. class ICMP
  50. {
  51.     public byte Type;
  52.     public byte Code;
  53.     public UInt16 Checksum;
  54.     public int MessageSize;
  55.     public byte[] Message = new byte[1024];
  56.  
  57.     public ICMP()
  58.     {
  59.     }
  60.  
  61.     public ICMP(byte[] data, int size)
  62.     {
  63.         Type = data[20];
  64.         Code = data[21];
  65.         Checksum = BitConverter.ToUInt16(data, 22);
  66.         MessageSize = size - 24;
  67.         Buffer.BlockCopy(data, 24, Message, 0, MessageSize);
  68.     }
  69.  
  70.     public byte[] getBytes()
  71.     {
  72.         byte[] data = new byte[MessageSize + 9];
  73.         Buffer.BlockCopy(BitConverter.GetBytes(Type), 0, data, 0, 1);
  74.         Buffer.BlockCopy(BitConverter.GetBytes(Code), 0, data, 1, 1);
  75.         Buffer.BlockCopy(BitConverter.GetBytes(Checksum), 0, data, 2, 2);
  76.         Buffer.BlockCopy(Message, 0, data, 4, MessageSize);
  77.         return data;
  78.     }
  79.  
  80.     public UInt16 getChecksum()
  81.     {
  82.         UInt32 chcksm = 0;
  83.         byte[] data = getBytes();
  84.         int packetsize = MessageSize + 8;
  85.         int index = 0;
  86.  
  87.         while (index < packetsize)
  88.         {
  89.             chcksm += Convert.ToUInt32(BitConverter.ToUInt16(data, index));
  90.             index += 2;
  91.         }
  92.         chcksm = (chcksm >> 16) + (chcksm & 0xffff);
  93.         chcksm += (chcksm >> 16);
  94.         return (UInt16)(~chcksm);
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment