Sunteți pe pagina 1din 4

//this function shall start the listening service for the UDP client

//s is a class or structure that shall contain a refernce to the UDP client

//class of .NET, as well as an endpoint refernce.

private void IniListnerCallBack()


{
try
{
// start teh recieve call back method
s.u.BeginReceive(new AsyncCallback(OnDataRecieved), s);
}
catch (Exception ex)
{
if (IsListening == true)
Console.WriteLine(ex.Message);
}
}

// This is the call back function that shall be triggered


// when data has been recieved.
//var asyn shall contain an instance of the UPD structure (UDPstate)
// returned by the callback

public void OnDataRecieved(IAsyncResult asyn)


{
Byte[] receiveBytes;
UdpClient u;
IPEndPoint e;

try
{
//get the udp client

u = (UdpClient)((UdpState)(asyn.AsyncState)).u;
//get the endpoint (shall contain refernce about the client)

e = (IPEndPoint)((UdpState)(asyn.AsyncState)).e;
//stop the callback and get the number of bytes recieved

receiveBytes = u.EndReceive(asyn, ref e);


//raise the event with the data recieved to the DHCP class

DataRcvd(receiveBytes, e);
}
catch (Exception ex)
{
if (IsListening == true)
Console.WriteLine(ex.Message);
}
finally
{
u = null;
e = null;
receiveBytes = null;
// recall the call back, ie go and listen for more data

IniListnerCallBack();
}
}

public struct DHCPstruct


{
public byte D_op; //Op code: 1 = bootRequest, 2 = BootReply

public byte D_htype; //Hardware Address Type: 1 = 10MB ethernet

public byte D_hlen; //hardware address length: length of MACID

public byte D_hops; //Hw options

public byte[] D_xid; //transaction id (5),

public byte[] D_secs; //elapsed time from trying to boot (3)

public byte[] D_flags; //flags (3)

public byte[] D_ciaddr; // client IP (5)

public byte[] D_yiaddr; // your client IP (5)

public byte[] D_siaddr; // Server IP (5)

public byte[] D_giaddr; // relay agent IP (5)

public byte[] D_chaddr; // Client HW address (16)

public byte[] D_sname; // Optional server host name (64)

public byte[] D_file; // Boot file name (128)

public byte[] M_Cookie; // Magic cookie (4)

public byte[] D_options; //options (rest)


}

//pass over a byte as convert it


//using the predefined stream reader function

//Data is an array containing the udp data sent.

public cDHCPStruct(byte[] Data)


{
System.IO.BinaryReader rdr;
System.IO.MemoryStream stm =
new System.IO.MemoryStream(Data, 0, Data.Length);
try
{ //read data

dStruct.D_op = rdr.ReadByte();
dStruct.D_htype = rdr.ReadByte();
dStruct.D_hlen = rdr.ReadByte();
dStruct.D_hops = rdr.ReadByte();
dStruct.D_xid = rdr.ReadBytes(4);
dStruct.D_secs = rdr.ReadBytes(2);
dStruct.D_flags = rdr.ReadBytes(2);
dStruct.D_ciaddr = rdr.ReadBytes(4);
dStruct.D_yiaddr = rdr.ReadBytes(4);
dStruct.D_siaddr = rdr.ReadBytes(4);
dStruct.D_giaddr = rdr.ReadBytes(4);
dStruct.D_chaddr = rdr.ReadBytes(16);
dStruct.D_sname = rdr.ReadBytes(64);
dStruct.D_file = rdr.ReadBytes(128);
dStruct.M_Cookie = rdr.ReadBytes(4);
//read the rest of the data, which shall determine the dhcp

//options

dStruct.D_options = rdr.ReadBytes(Data.Length - OPTION_OFFSET);


}
catch(Exception ex)
{
Console.WriteLine (ex.Message);
}
}

//end option is the mark that shall signify the end of the message

public enum DHCPOptionEnum


{
SubnetMask = 1,
TimeOffset = 2,
Router = 3,
TimeServer = 4,
NameServer = 5,
DomainNameServer = 6,
LogServer = 7,
CookieServer = 8,
LPRServer = 9,
ImpressServer = 10,
ResourceLocServer = 11,
HostName = 12,
BootFileSize = 13,
MeritDump = 14,
DomainName = 15,
SwapServer = 16,
RootPath = 17,
ExtensionsPath = 18,
IpForwarding = 19,
NonLocalSourceRouting = 20,
PolicyFilter = 21,
MaximumDatagramReAssemblySize = 22,
DefaultIPTimeToLive = 23,
PathMTUAgingTimeout = 24,
PathMTUPlateauTable = 25,
InterfaceMTU = 26,
AllSubnetsAreLocal = 27,
BroadcastAddress = 28,
PerformMaskDiscovery = 29,
MaskSupplier = 30,
PerformRouterDiscovery = 31,
RouterSolicitationAddress = 32,
StaticRoute = 33,
TrailerEncapsulation = 34,
ARPCacheTimeout = 35,
EthernetEncapsulation = 36,
TCPDefaultTTL = 37,
TCPKeepaliveInterval = 38,
TCPKeepaliveGarbage = 39,
NetworkInformationServiceDomain = 40,
NetworkInformationServers = 41,
NetworkTimeProtocolServers = 42,
VendorSpecificInformation = 43,
NetBIOSoverTCPIPNameServer = 44,
NetBIOSoverTCPIPDatagramDistributionServer = 45,
NetBIOSoverTCPIPNodeType = 46,
NetBIOSoverTCPIPScope = 47,
XWindowSystemFontServer = 48,
XWindowSystemDisplayManager = 49,
RequestedIPAddress = 50,
IPAddressLeaseTime = 51,
OptionOverload = 52,
DHCPMessageTYPE = 53,
ServerIdentifier = 54,
ParameterRequestList = 55,
Message = 56,
MaximumDHCPMessageSize = 57,
RenewalTimeValue_T1 = 58,
RebindingTimeValue_T2 = 59,
Vendorclassidentifier = 60,
ClientIdentifier = 61,
NetworkInformationServicePlusDomain = 64,
NetworkInformationServicePlusServers = 65,
TFTPServerName = 66,
BootfileName = 67,
MobileIPHomeAgent = 68,
SMTPServer = 69,
POP3Server = 70,
NNTPServer = 71,
DefaultWWWServer = 72,
DefaultFingerServer = 73,
DefaultIRCServer = 74,
StreetTalkServer = 75,
STDAServer = 76,
END_Option = 255
}

S-ar putea să vă placă și