Azione di scrittura per ping ambito IPV4

Microsoft.Windows.DHCPServer.10.0.TestIPV4Scope.WA (ProbeActionModuleType)

Questa azione di scrittura implementa un ping IPV4 per testare di nuovo un IP di destinazione

Knowledge Base article:

Riepilogo

Questo modulo di tipo probe consente di eseguire un ping di test per l'ambito IPV4 nei membri del gruppo di server DHCP 2016 e 1709+ denominato "Gruppo di server DHCP 2016 e 1709+" con istanze di server che eseguono un sistema operativo Microsoft Windows Server 2016 e 1709+ e su cui è abilitata la funzionalità DHCP.

Configurazione

IP server: IP server da usare per il ping.

IP di destinazione: IP di destinazione da usare per il ping.

Numero di tentativi: numero di tentativi per l'esecuzione del comando ping.

Element properties:

TypeProbeActionModuleType
IsolationAny
AccessibilityPublic
RunAsDefault
OutputTypeMicrosoft.Windows.SerializedObjectData

Member Modules:

ID Module Type TypeId RunAs 
PS ProbeAction Microsoft.Windows.PowerShellTriggerOnlyProbe Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
ServerIPstring$Config/ServerIP$IP server
TargetIPstring$Config/TargetIP$IP di destinazione
RetryTimesint$Config/RetryTimes$Numero di tentativi

Source Code:

<ProbeActionModuleType ID="Microsoft.Windows.DHCPServer.10.0.TestIPV4Scope.WA" Accessibility="Public" Batching="false" PassThrough="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Type" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="ServerIP" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TargetIP" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="RetryTimes" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="ServerIP" Selector="$Config/ServerIP$" ParameterType="string"/>
<OverrideableParameter ID="TargetIP" Selector="$Config/TargetIP$" ParameterType="string"/>
<OverrideableParameter ID="RetryTimes" Selector="$Config/RetryTimes$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<ProbeAction ID="PS" TypeID="Windows!Microsoft.Windows.PowerShellTriggerOnlyProbe">
<ScriptName>TestDHCPIPV4Scopes.10.0.ps1</ScriptName>
<ScriptBody><Script>

param ([String] $type, [String] $serverip, [String] $interfaceip, [Int] $retry)

$SCRIPT_NAME = "TestDHCPIPV4Scopes.10.0"
&lt;#
.SYNOPSIS
DHCP Ping tool for V4
.DESCRIPTION
A detailed description of the function.
.PARAMETER interfaceip
IPv6 Address of the interface to be used - An IPv4 Address
.PARAMETER serverip
IPv6 Address of the server - An IPv4 Address
.PARAMETER renewip
IP Address denoting the scope. XXX.XXX.XXX.XXX
.PARAMETER type
Type of the message (INFORM/DISCOVER/RELEASE/RENEW)
.PARAMETER hwaddr
Hardware address of the client for spoof. XX-XX-XX-XX-XX-XX
.PARAMETER retry
retry count
.EXAMPLE
PS C:\&gt; ./DHCPPing.ps1 -type INFORM -serverip 172.16.36.201 -interfaceip 172.16.36.100 -renewip 192.168.7.0 -hwaddr 12-12-12-12-12-12 -retry 2
.NOTES
Additional information about the function goes here.
.LINK
about_functions_advanced
#&gt;

function Compile-Csharp ([string] $code, [string] $compilerOptions)
{
# Get an instance of the CSharp code provider
$cp = New-Object Microsoft.CSharp.CSharpCodeProvider
$refs = New-Object Collections.ArrayList
$refs.AddRange( @("${framework}System.dll"))
# Build up a compiler params object...
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $true
$cpar.IncludeDebugInformation = $false
$cpar.CompilerOptions = $compilerOptions
$cpar.ReferencedAssemblies.AddRange($refs)
$cr = $cp.CompileAssemblyFromSource($cpar, $code)
if ( $cr.Errors.Count)
{
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors)
{
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce | out-default
}
Throw "INVALID DATA: Errors encountered while compiling code"
}
}

function call-DHCPMessage
{
$code = @'
namespace DhcpMessage
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Net.NetworkInformation;

enum BOOT_SIZE
{
BOOT_FILE_SIZE = 128,
BOOT_SERVER_SIZE = 64
}

public enum DHCP_MESSAGE_TYPE
{
DHCP_DISCOVER_MESSAGE = 1,
DHCP_OFFER_MESSAGE = 2,
DHCP_REQUEST_MESSAGE = 3,
DHCP_DECLINE_MESSAGE = 4,
DHCP_ACK_MESSAGE = 5,
DHCP_NACK_MESSAGE = 6,
DHCP_RELEASE_MESSAGE = 7,
DHCP_INFORM_MESSAGE = 8,
}

public enum DHCP_MESSAGE_OPTION
{
OPTION_SUBNET_MASK = 1,
OPTION_GATEWAY = 3,
OPTION_DOMAIN_NAME_SERVER = 6,
OPTION_DOMAIN_NAME = 15,
OPTION_WINS_SERVER = 44,
OPTION_REQUESTED_IP = 50,
OPTION_MESSAGE_TYPE = 53,
OPTION_SERVER_ID = 54,
OPTION_PARAMETER_REQUEST_LIST = 55,
OPTION_VENDORCLASS_ID = 60,
OPTION_CLIENT_ID = 61,
OPTION_USERCLASS_ID = 77,
OPTION_RELAY_AGENT = 82,
OPTION_CSR = 121,
OPTION_END = 255
}

public struct _OPTION
{
public byte OptionType;
public byte OptionLength;
public byte[] OptionValue;
};


public class DhcpObject
{
public byte Operation;
public byte HardwareAddressType;
public byte HardwareAddressLength;
public byte HopCount;
public UInt32 TransactionID;
public UInt16 SecondsSinceBoot;
public UInt16 Reserved;
public UInt32 ClientIpAddress;
public UInt32 YourIpAddress;
public UInt32 BootstrapServerAddress;
public UInt32 RelayAgentIpAddress;
public byte[] HardwareAddress;
public byte[] HostName;
public byte[] BootFileName;
public _OPTION Option;

public byte[] DhcpSendMessageBuffer;
public int DhcpSendMessageLength;
public byte[] DhcpSendMessageOptionBuffer;
public int DhcpSendMessageOptionLength;

public byte[][] DhcpRecvMessageBuffer;
public int[] DhcpRecvMessageLength;
public byte[][] DhcpRecvMessageOptionBuffer;
public int[] DhcpRecvMessageOptionLength;

public EndPoint[] RecvEndPoints;
public int NoRecvMessage;

DateTime sendTime;
DateTime[] recvTime;

public DhcpObject()
{
Operation = 1; //Request
HardwareAddress = new byte[16];
HostName = new byte[(int)BOOT_SIZE.BOOT_SERVER_SIZE];
BootFileName = new byte[(int)BOOT_SIZE.BOOT_FILE_SIZE];
Option = new _OPTION();
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;

DhcpSendMessageLength = 0;
DhcpSendMessageOptionLength = 0;
DhcpRecvMessageBuffer = new byte[12][];
DhcpRecvMessageLength = new int[12];
DhcpRecvMessageOptionBuffer = new byte[12][];
DhcpRecvMessageOptionLength = new int[12];
NoRecvMessage = 0;
RecvEndPoints = new IPEndPoint[12];
sendTime = new DateTime();
recvTime= new DateTime[12];
}


public void SetSendTime()
{
sendTime = DateTime.Now;
}
public void SetRecvTime(int pos)
{
recvTime[pos] = DateTime.Now;
}

public double FindTimeDifference(int pos)
{
TimeSpan tmp = recvTime[pos].Subtract(sendTime);
return tmp.TotalMilliseconds;
}

internal void AddMessageType(byte p)
{
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_MESSAGE_TYPE;
Option.OptionLength = 1;
Option.OptionValue = new byte[Option.OptionLength];
Option.OptionValue[0] = p;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}
public void SetServerIDbyByte(byte[] serverid)
{
int index = 0;
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_SERVER_ID;
Option.OptionLength = (byte)serverid.Length;
Option.OptionValue = new byte[Option.OptionLength];
foreach(byte byteVal in serverid)
Option.OptionValue[index++] = byteVal;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}
public void SetRequestedIPbyByte(byte[] requestedip)
{

int index = 0;
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_REQUESTED_IP;
Option.OptionLength = 4;
Option.OptionValue = new byte[Option.OptionLength];
foreach(byte byteVal in requestedip)
Option.OptionValue[index++] = byteVal;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}
public void SetRequestedIPbyString(String requestedip)
{
char[] splitChar = { '.' };
int index = 0;
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_REQUESTED_IP;
Option.OptionLength = 4;
Option.OptionValue = new byte[Option.OptionLength];
String[] tempStr = requestedip.Split(splitChar, 4);
foreach (String str in tempStr)
Option.OptionValue[index++] = (byte)Convert.ToInt16(str, 10);
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}

private void WriteOptionToBuffer(_OPTION Option)
{
byte[] tempOptionBuffer = new byte[Option.OptionLength + 2];
int index = 0;
//converting Option to bytebuffer
tempOptionBuffer[0] = Option.OptionType;
tempOptionBuffer[1] = Option.OptionLength;
for (; index &lt; Option.OptionLength; index++)
tempOptionBuffer[index + 2] = Option.OptionValue[index];
//concatenating the Option to the end of Option buffer
byte[] tempByte = new byte[DhcpSendMessageOptionLength];
index = 0;
if (null != DhcpSendMessageOptionBuffer)
{
foreach (byte byteValue in DhcpSendMessageOptionBuffer)
tempByte[index++] = byteValue;
}
DhcpSendMessageOptionBuffer = new byte[DhcpSendMessageOptionLength + Option.OptionLength + 2];

index = 0;
if (null != tempByte)
{
foreach (byte byteValue in tempByte)
DhcpSendMessageOptionBuffer[index++] = byteValue;
}
foreach (byte byteValue in tempOptionBuffer)
DhcpSendMessageOptionBuffer[index++] = byteValue;
DhcpSendMessageOptionLength = DhcpSendMessageOptionLength + Option.OptionLength + 2;
}

public int PackDhcpDataandOption()
{
byte[] tempByte = new byte[4];
int index = 0;
DhcpSendMessageBuffer = new byte[236 + DhcpSendMessageOptionLength + 5];
DhcpSendMessageBuffer[index++] = this.Operation;
DhcpSendMessageBuffer[index++] = this.HardwareAddressType;
DhcpSendMessageBuffer[index++] = this.HardwareAddressLength;
DhcpSendMessageBuffer[index++] = this.HopCount;
//Copying Transation ID
tempByte = BitConverter.GetBytes(this.TransactionID);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Seconds since boot
tempByte = BitConverter.GetBytes(this.SecondsSinceBoot);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Flags
tempByte = BitConverter.GetBytes(this.Reserved);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Client IP Address
tempByte = BitConverter.GetBytes(this.ClientIpAddress);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Your IP Address
tempByte = BitConverter.GetBytes(this.YourIpAddress);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying BootStrap Server Addresss
tempByte = BitConverter.GetBytes(this.BootstrapServerAddress);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Relay Agent IP Address
tempByte = BitConverter.GetBytes(this.RelayAgentIpAddress);
foreach (byte byteValue in tempByte)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Hardware Address
foreach (byte byteValue in this.HardwareAddress)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Hostname
foreach (byte byteValue in this.HostName)
DhcpSendMessageBuffer[index++] = byteValue;
//Copying Boot File name
foreach (byte byteValue in this.BootFileName)
DhcpSendMessageBuffer[index++] = byteValue;
//Appending Magic Cookie
DhcpSendMessageBuffer[index++] = 99;
DhcpSendMessageBuffer[index++] = 130;
DhcpSendMessageBuffer[index++] = 83;
DhcpSendMessageBuffer[index++] = 99;
//Copying Options
foreach (byte byteValue in this.DhcpSendMessageOptionBuffer)
DhcpSendMessageBuffer[index++] = byteValue;
DhcpSendMessageBuffer[index] = 255;
DhcpSendMessageLength = 236 + DhcpSendMessageOptionLength + 5;
return (DhcpSendMessageLength);
}

internal UInt32 convertBigtoLittle(UInt32 value)
{
byte[] tmpByte = new byte[4];
tmpByte = BitConverter.GetBytes(value);
Array.Reverse(tmpByte);
value = BitConverter.ToUInt32(tmpByte, 0);
return value;
}

public void UnPackDataandOtions(int PacketLength, int pos)
{
int index = 0, i = 0;
Operation = DhcpRecvMessageBuffer[pos][index++];
HardwareAddressType = DhcpRecvMessageBuffer[pos][index++];
HardwareAddressLength = DhcpRecvMessageBuffer[pos][index++];
HopCount = DhcpRecvMessageBuffer[pos][index++];
TransactionID = BitConverter.ToUInt32(DhcpRecvMessageBuffer[pos], index);
//TransactionID = convertBigtoLittle(TransactionID);
index += 4;
SecondsSinceBoot = BitConverter.ToUInt16(DhcpRecvMessageBuffer[pos], index);
index += 2;
Reserved = BitConverter.ToUInt16(DhcpRecvMessageBuffer[pos], index);
index += 2;
ClientIpAddress = BitConverter.ToUInt32(DhcpRecvMessageBuffer[pos], index);
index += 4;
YourIpAddress = BitConverter.ToUInt32(DhcpRecvMessageBuffer[pos], index);
index += 4;
BootstrapServerAddress = BitConverter.ToUInt32(DhcpRecvMessageBuffer[pos], index);
index += 4;
RelayAgentIpAddress = BitConverter.ToUInt32(DhcpRecvMessageBuffer[pos], index);
index += 4;
for (i = 0; i &lt; 16; i++)
HardwareAddress[i] = DhcpRecvMessageBuffer[pos][index++];
for (i = 0; i &lt; (int)BOOT_SIZE.BOOT_SERVER_SIZE; i++)
HostName[i] = DhcpRecvMessageBuffer[pos][index++];
for (i = 0; i &lt; (int)BOOT_SIZE.BOOT_FILE_SIZE; i++)
BootFileName[i] = DhcpRecvMessageBuffer[pos][index++];
DhcpRecvMessageOptionBuffer[pos] = new byte[4096];
index+=4; //TODO magic Cookie;
for (i = 0; index &lt; PacketLength; i++,index++)
DhcpRecvMessageOptionBuffer[pos][i] = DhcpRecvMessageBuffer[pos][index];
DhcpRecvMessageOptionLength[pos] = i;
}

public byte[] GetRecvMessageOption(byte OptionType, int pos)
{
byte[] OptionByteValue = null;
int index = 0;
while (index &lt; DhcpRecvMessageOptionLength[pos])
{
if (DhcpRecvMessageOptionBuffer[pos][index] == OptionType)
{

OptionByteValue = new byte[DhcpRecvMessageOptionBuffer[pos][++index]];
index++;
for (int i = 0; i &lt; OptionByteValue.Length; i++)
{
OptionByteValue[i] = DhcpRecvMessageOptionBuffer[pos][index++];
}
break;
}
else
{
index = index + DhcpRecvMessageOptionBuffer[pos][index + 1] + 2;
}
}
return OptionByteValue;
}

public void SetHWAddress(String cardType)
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
String hwaddr = "";
for (int i = 0; i &lt; nics.Length; i++)
{
if (nics[i].NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
hwaddr = nics[i].GetPhysicalAddress().ToString();
break;
}
}
HardwareAddressLength = 6;
HardwareAddressType = 1;
hwaddr.Replace(":", "");
for (int index = 0; index &lt; hwaddr.Length; index += 2)
{
String str = new String(hwaddr.ToCharArray(), index, 2);
HardwareAddress[index / 2] = (byte)Convert.ToInt16(str, 16);
}
}

public void CreateNewTransactionID()
{
byte[] rndNos = new byte[4];
Random rnd = new Random();
rnd.NextBytes(rndNos);
TransactionID = (UInt32)(((UInt32)rndNos[3]) | ((UInt32)rndNos[2]) &lt;&lt; 8 | ((UInt32)rndNos[1]) &lt;&lt; 16 | ((UInt32)rndNos[0]) &lt;&lt; 24);
}

public void SetClientID(String hwaddr)
{
byte[] HA = new byte[6];
int i = 0;
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
Option.OptionType = 61;
Option.OptionLength = 7;
Option.OptionValue = new byte[Option.OptionLength];
Option.OptionValue[i++] = 1;
hwaddr = hwaddr.Replace("-", "");
for (int index = 0; index &lt; hwaddr.Length; index += 2)
{
String str = new String(hwaddr.ToCharArray(), index, 2);
HA[index / 2] = (byte)Convert.ToInt16(str, 16);
}
foreach(byte byteVal in HA)
Option.OptionValue[i++] = byteVal;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}

internal void SetHostName(string p)
{
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
int index = 0;
char[] charValue = p.ToCharArray();
Option.OptionType = 12;
Option.OptionLength = (byte)p.Length;
Option.OptionValue = new byte[Option.OptionLength];
foreach (char Character in charValue)
Option.OptionValue[index++] = (byte)Character;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}

internal void SetVendorClassIdentifier(string p)
{
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
int index = 0;
char[] charValue = p.ToCharArray();
Option.OptionType = 60;
Option.OptionLength = (byte)p.Length;
Option.OptionValue = new byte[Option.OptionLength];
foreach (char Character in charValue)
Option.OptionValue[index++] = (byte)Character;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}

internal void SetParameterRequestList()
{
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
Option.OptionType = 55;
Option.OptionLength = 5;
Option.OptionValue = new byte[] {0x01, 0x0F, 0x06, 0x03, 0x2C};
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}

internal void SetVendorSpecificInformation(byte[] p)
{
if (Option.OptionType != (byte)DHCP_MESSAGE_OPTION.OPTION_END)
{
//failsafe
WriteOptionToBuffer(Option);
}
int index = 0;
Option.OptionType = 43;
Option.OptionLength = 2;
Option.OptionValue = new byte[Option.OptionLength];
//foreach (byte byteValue in p)
// Option.OptionValue[index++] = byteValue;
Option.OptionValue[0] = 0x5E;
Option.OptionValue[0] = 0x00;
WriteOptionToBuffer(Option);
Option.OptionType = (byte)DHCP_MESSAGE_OPTION.OPTION_END;
}

public void SetCIAddr(string HostIPAddr)
{
char[] splitChar = { '.' };
int index = 0;
if (null == HostIPAddr)
{
ClientIpAddress = 0;
return;
}
String[] tempStr = HostIPAddr.Split(splitChar, 4);
byte[] byteValue = new byte[4];
foreach (String str in tempStr)
byteValue[index++] = (byte)Convert.ToInt16(str, 10);
ClientIpAddress = (UInt32)(((UInt32)byteValue[3]) &lt;&lt; 24 | ((UInt32)byteValue[2]) &lt;&lt; 16 | ((UInt32)byteValue[1]) &lt;&lt; 8 | ((UInt32)byteValue[0]));
}

public void SetYourAddr(string YourIPAddr)
{
char[] splitChar = { '.' };
int index = 0;
if (null == YourIPAddr)
{
YourIpAddress = 0;
return;
}
String[] tempStr = YourIPAddr.Split(splitChar, 4);
byte[] byteValue = new byte[4];
foreach (String str in tempStr)
byteValue[index++] = (byte)Convert.ToInt16(str, 10);
YourIpAddress = (UInt32)(((UInt32)byteValue[3]) &lt;&lt; 24 | ((UInt32)byteValue[2]) &lt;&lt; 16 | ((UInt32)byteValue[1]) &lt;&lt; 8 | ((UInt32)byteValue[0]));
}

internal void SetServerAddr(string ServerIPAddr)
{
char[] splitChar = { '.' };
int index = 0;
if (null == ServerIPAddr)
{
BootstrapServerAddress = 0;
return;
}
String[] tempStr = ServerIPAddr.Split(splitChar, 4);
byte[] byteValue = new byte[4];
foreach (String str in tempStr)
byteValue[index++] = (byte)Convert.ToInt16(str, 10);
BootstrapServerAddress = (UInt32)(((UInt32)byteValue[3]) &lt;&lt; 24 | ((UInt32)byteValue[2]) &lt;&lt; 16 | ((UInt32)byteValue[1]) &lt;&lt; 8 | ((UInt32)byteValue[0]));
}
public void ActasRelayAgent(string RelayAgentIPAddr)
{
char[] splitChar = { '.' };
int index = 0;

if (null == RelayAgentIPAddr)
{
RelayAgentIpAddress = 0;
return;
}
String[] tempStr = RelayAgentIPAddr.Split(splitChar, 4);
byte[] byteValue = new byte[4];
foreach (String str in tempStr)
byteValue[index++] = (byte)Convert.ToInt16(str, 10);
RelayAgentIpAddress = (UInt32)(((UInt32)byteValue[3]) &lt;&lt; 24 | ((UInt32)byteValue[2]) &lt;&lt; 16 | ((UInt32)byteValue[1]) &lt;&lt; 8 | ((UInt32)byteValue[0]));
}

public String GetGIAddr()
{
byte[] tmpByte = BitConverter.GetBytes(RelayAgentIpAddress);
String tmpStr = Convert.ToString(tmpByte[0]) + "." + Convert.ToString(tmpByte[1]) + "." + Convert.ToString(tmpByte[2]) + "." + Convert.ToString(tmpByte[3]);
return tmpStr;
}

public String GetYourAddr()
{
byte[] tmpByte = BitConverter.GetBytes(YourIpAddress);
String tmpStr = Convert.ToString(tmpByte[0]) + "." + Convert.ToString(tmpByte[1]) + "." + Convert.ToString(tmpByte[2]) + "." + Convert.ToString(tmpByte[3]);
return tmpStr;
}

public String GetServerAddr()
{
byte[] tmpByte = BitConverter.GetBytes(BootstrapServerAddress);
String tmpStr = Convert.ToString(tmpByte[0]) + "." + Convert.ToString(tmpByte[1]) + "." + Convert.ToString(tmpByte[2]) + "." + Convert.ToString(tmpByte[3]);
return tmpStr;
}

public String GetCIAddr()
{
byte[] tmpByte = BitConverter.GetBytes(ClientIpAddress);
String tmpStr = Convert.ToString(tmpByte[0]) + "." + Convert.ToString(tmpByte[1]) + "." + Convert.ToString(tmpByte[2]) + "." + Convert.ToString(tmpByte[3]);
return tmpStr;
}
public void SetBroadCastFlag(bool broadcastFlag)
{
if (broadcastFlag)
Reserved = (UInt16)0x0080;
else
Reserved = (UInt16)0x0000;
}
internal bool GetBroadCastFlag()
{
if ((Reserved &amp; 0x8000) == 0x8000)
return true;
else
return false;
}
internal void ClearClientIPAddr()
{
ClientIpAddress = 0x00000000;
}
internal void ClearYourIPAddr()
{
YourIpAddress = 0x00000000;
}
internal void ClearServerIPAddr()
{
BootstrapServerAddress = 0x00000000;
}
internal void ClearRelayAgentIPAddr()
{
RelayAgentIpAddress = 0x00000000;
}
public String GetDNSName(byte[] dnsname)
{
System.Text.Encoding enc = System.Text.Encoding.ASCII;
return enc.GetString(dnsname);
}
}
}

namespace DhcpSocket
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Net.NetworkInformation;
using DhcpMessage;

public enum PORTS
{
DHCP_SERVER_PORT = 67,
DHCP_CLIENT_PORT = 68
}
public class DhcpSocket
{
IPEndPoint IPEnd;
public IPAddress LocalIPAddr, DestIPAddr;
public int LocalPort, DestPort;
Socket socket;
public bool useBroadCast;
public int RetryCount;

public DhcpSocket()
{
RetryCount = 1;
}
public void InitWinSock(String MachineType)
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
if (useBroadCast)
{
DestIPAddr = new IPAddress(0xFFFFFFFF);
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
}
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
socket.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, true);
switch (MachineType)
{
case "CLIENT":
SetLocalandDestPorts(PORTS.DHCP_CLIENT_PORT, PORTS.DHCP_SERVER_PORT);
break;
case "RELAY_AGENT":
SetLocalandDestPorts(PORTS.DHCP_SERVER_PORT, PORTS.DHCP_SERVER_PORT);
break;
}

IPEnd = new IPEndPoint(LocalIPAddr, LocalPort);
socket.Bind(IPEnd);
}

public String GetIPAddress()
{
String IPAddr = "";
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
IPAddr = ip.ToString();
}
}
return IPAddr;
}

public void SetLocalandDestAddr(String localIPAddr, String destIPAddr)
{
char[] splitChar = { '.' };
int index = 0;

String[] tempStr = localIPAddr.Split(splitChar, 4);
byte[] byteValue = new byte[4];
foreach (String str in tempStr)
byteValue[index++] = (byte)Convert.ToInt16(str, 10);
long ipaddr = (long)(((UInt32)byteValue[3]) &lt;&lt; 24 | ((UInt32)byteValue[2]) &lt;&lt; 16 | ((UInt32)byteValue[1]) &lt;&lt; 8 | ((UInt32)byteValue[0]));
LocalIPAddr = new IPAddress(ipaddr);

index = 0;

tempStr = destIPAddr.Split(splitChar, 4);
byteValue = new byte[4];
foreach (String str in tempStr)
byteValue[index++] = (byte)Convert.ToInt16(str, 10);
ipaddr = (UInt32)(((UInt32)byteValue[3]) &lt;&lt; 24 | ((UInt32)byteValue[2]) &lt;&lt; 16 | ((UInt32)byteValue[1]) &lt;&lt; 8 | ((UInt32)byteValue[0]));
DestIPAddr = new IPAddress(ipaddr);
}

public void SetLocalandDestPorts(PORTS lport, PORTS dport)
{
LocalPort = (int)lport;
DestPort = (int)dport;
}

public void SendPacket(byte[] p, int PacketLength)
{
try
{
IPEnd = new IPEndPoint(DestIPAddr, DestPort);

socket.SendTo(p, IPEnd);
}
catch(SocketException)
{
Console.WriteLine("Not an appropriate server.");
}
}

public void RecvPacket(ref DhcpMessage.DhcpObject DhcpObj)
{
int index = 0;
EndPoint EP = new IPEndPoint(0,0);
ArrayList listenlist = new ArrayList();
listenlist.Add(socket);
Socket.Select(listenlist, null, null, 10000);
if (null == listenlist)
Console.WriteLine("Error");
socket.ReceiveTimeout = 1000;
do
{
try
{
DhcpObj.DhcpRecvMessageBuffer[index] = new byte[4096];
for(int j = 0; j &lt; RetryCount; j++)
{
DhcpObj.DhcpRecvMessageLength[index] = socket.ReceiveFrom(DhcpObj.DhcpRecvMessageBuffer[index],ref EP);
if(DhcpObj.DhcpRecvMessageLength[index] != 0)
break;
}
if(DhcpObj.DhcpRecvMessageBuffer[index][0] == 0x01)
continue;
DhcpObj.SetRecvTime(index);
DhcpObj.RecvEndPoints[index] = EP;
DhcpObj.NoRecvMessage++;
index++;
}
catch(SocketException){
socket.Close();
break;
}
}while(index &lt; 12);
}
}
}

namespace DhcpPacket
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Net.NetworkInformation;
using DhcpMessage;

public class DhcpPackage
{
public void ConstructMessage(ref DhcpMessage.DhcpObject DhcpObj, short Msg_Type)
{

String HostIPAddr = "", RelayAgentIPAddr = "";
switch (Msg_Type)
{
case (short)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_DISCOVER_MESSAGE:
DhcpObj.AddMessageType((byte)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_DISCOVER_MESSAGE);
DhcpObj.Operation = 0x01;
DhcpObj.SetBroadCastFlag(false);
DhcpObj.SetHWAddress("ETHERNET");
DhcpObj.SetHostName(Dns.GetHostName());
DhcpObj.SetVendorClassIdentifier("MSFT 5.0");
DhcpObj.SetParameterRequestList();
break;
case (short)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_INFORM_MESSAGE:
DhcpObj.AddMessageType((byte)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_INFORM_MESSAGE);
DhcpObj.Operation = 0x01;
DhcpObj.SetBroadCastFlag(true);
DhcpObj.SetHWAddress("ETHERNET");
byte[] p = new byte[2];
p[0] = 0x5E;
p[1] = 0x00;
DhcpObj.SetVendorSpecificInformation(p);
DhcpObj.SetParameterRequestList();
break;
case (short)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_RELEASE_MESSAGE:
DhcpObj.AddMessageType((byte)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_RELEASE_MESSAGE);
DhcpObj.Operation = 0x01;
DhcpObj.SetBroadCastFlag(false);
DhcpObj.SetHWAddress("ETHERNET");
DhcpObj.SetHostName(Dns.GetHostName());
DhcpObj.SetVendorClassIdentifier("MSFT 5.0");
DhcpObj.SetParameterRequestList();
break;
case (short)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_REQUEST_MESSAGE:
DhcpObj.AddMessageType((byte)DhcpMessage.DHCP_MESSAGE_TYPE.DHCP_REQUEST_MESSAGE);
DhcpObj.Operation = 0x01;
DhcpObj.SetBroadCastFlag(true);
DhcpObj.SetHWAddress("ETHERNET");
DhcpObj.SetHostName(Dns.GetHostName());
DhcpObj.SetVendorClassIdentifier("MSFT 5.0");
DhcpObj.SetParameterRequestList();
break;
}

}
}
}
namespace DhcpReturnObject
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class DhcpReturnObject
{
public String ServerIP;
public double Time_MS;
public String YourIPAddress;
public String[] DNSServer;
public String[] WinsServer;
public String[] Gateway;
public String SubnetMask;
public String DNSName;
public int ExitCode;

public DhcpReturnObject()
{
ServerIP = "255.255.255.255";
Time_MS = 0.0F;
YourIPAddress = "0.0.0.0";
SubnetMask = "";
ExitCode = 0;
DNSName = "";
}

public void SetServerIP(String serveraddr)
{
ServerIP = serveraddr;
}
public void SetTime(double time)
{
Time_MS = time;
}
public void SetDNSName(String dnsname)
{
DNSName = dnsname;
}
public void SetYourIPAddress(String youraddr)
{
YourIPAddress = youraddr;
}
public void SetDNS(byte[] dns)
{
int NoofDNS = dns.Length / 4;
DNSServer = new String[NoofDNS];
for(int index = 0; index &lt; NoofDNS; index++)
DNSServer[index] = Convert.ToString(dns[index * 4 + 0]) + "." + Convert.ToString(dns[index * 4 + 1]) + "." + Convert.ToString(dns[index * 4 + 2]) + "." + Convert.ToString(dns[index * 4 + 3]);
}
public void SetWINS(byte[] wins)
{
int Noofwins = wins.Length / 4;
WinsServer = new String[Noofwins];
for(int index = 0; index &lt; Noofwins; index++)
WinsServer[index] = Convert.ToString(wins[index * 4 + 0]) + "." + Convert.ToString(wins[index * 4 + 1]) + "." + Convert.ToString(wins[index * 4 + 2]) + "." + Convert.ToString(wins[index * 4 + 3]);
}
public void SetGateway(byte[] gate)
{
int Noofgate = gate.Length / 4;
Gateway = new String[Noofgate];
for(int index = 0; index &lt; Noofgate; index++)
Gateway[index] = Convert.ToString(gate[index * 4 + 0]) + "." + Convert.ToString(gate[index * 4 + 1]) + "." + Convert.ToString(gate[index * 4 + 2]) + "." + Convert.ToString(gate[index * 4 + 3]);
}
public void SetSubnetMask(byte[] subnetmask)
{
SubnetMask = Convert.ToString(subnetmask[0]) + "." + Convert.ToString(subnetmask[1]) + "." + Convert.ToString(subnetmask[2]) + "." + Convert.ToString(subnetmask[3]);
}
public void CleanUp()
{
ServerIP = "";
Time_MS = 0.0F;
YourIPAddress = "";
SubnetMask = "";
DNSServer = null;
ExitCode = 1;
WinsServer = null;
Gateway = null;
}
}
}
'@
Compile-Csharp $code "/target:library"
}

function validate-IPAddress
{
if($args[0] -match "(\b(([01]?\d?\d|2[0-4]\d|25[0-5])\.){3}([01]?\d?\d|2[0-4]\d|25[0-5])\b)")
{
return $true
}
else
{
return $false
}
}

function validate-HardwareAddress
{
if($args[0] -match "(\b(\d|[A-F])(\d|[A-F])-(\d|[A-F])(\d|[A-F])-(\d|[A-F])(\d|[A-F])-(\d|[A-F])(\d|[A-F])-(\d|[A-F])(\d|[A-F])-(\d|[A-F])(\d|[A-F])\b)")
{
return $true
}
else
{
return $false
}
}



$localaddr = "0.0.0.0"
$ciaddr = "0.0.0.0"
$RAaddr = "0.0.0.0"

call-DHCPMessage
$DhcpObj = New-Object DhcpMessage.DhcpObject
$DhcpPacker = New-Object DhcpPacket.DhcpPackage
$DhcpConnector = New-Object DhcpSocket.DhcpSocket
$DhcpRetObj = New-Object DhcpReturnObject.DhcpReturnObject


if($retry -ne 0)
{
$DhcpConnector.RetryCount = $retry
}

if($renewip -ne [System.String]::Empty)
{
$res = validate-IPAddress $renewip
if($res -eq $false)
{
Write-Host "$renewip is a invalid ip"
exit
}
$ciaddr = $renewip
$DhcpObj.SetCIAddr($ciaddr);
$RA = $true
}

if($serverip -ne [System.String]::Empty)
{
$res = validate-IPAddress $serverip
if($res -eq $false)
{
Write-Host "$serverip is a invalid ip address"
exit
}
$serveraddr = $serverip
$DhcpConnector.useBroadCast = $false
}
else
{
$serveraddr = "255.255.255.255"
$DhcpConnector.useBroadCast = $true
}

if($hwaddr -ne [System.String]::Empty)
{
$res = validate-HardwareAddress $hwaddr
if($res -eq $false)
{
Write-Host "$hwaddr is a invalid hardware address"
exit
}
$DhcpObj.SetClientID($hwaddr);
}

if($type -eq "DISCOVER" -or $type -eq "FULLLEASE")
{
$DhcpPacker.ConstructMessage(([ref]$DhcpObj), [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_DISCOVER_MESSAGE);
$DhcpObj.CreateNewTransactionID();
$localaddr = "0.0.0.0"
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
}
elseif($type -eq "INFORM")
{
$DhcpPacker.ConstructMessage(([ref]$DhcpObj), [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_INFORM_MESSAGE);
$DhcpObj.CreateNewTransactionID();
$localaddr = $interfaceip
if($renewip -eq [System.String]::Empty)
{
$ciaddr = $interfaceip
}
$DhcpObj.SetCIAddr($ciaddr);
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
}
elseif($type -eq "RELEASE")
{
$DhcpPacker.ConstructMessage(([ref]$DhcpObj), [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_RELEASE_MESSAGE);
$DhcpObj.CreateNewTransactionID();
$localaddr = $interfaceip
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
}
elseif($type -eq "RENEW")
{
if($renewip -ne [System.String]::Empty)
{
$ciaddr = $renewip
}
$DhcpObj.SetCIAddr($ciaddr);
$DhcpPacker.ConstructMessage(([ref]$DhcpObj), [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_REQUEST_MESSAGE);
$DhcpObj.CreateNewTransactionID();
$localaddr = $interfaceip
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
}
elseif($type -eq "REQUEST")
{
$DhcpPacker.ConstructMessage(([ref]$DhcpObj), [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_REQUEST_MESSAGE);
$DhcpObj.CreateNewTransactionID();
$localaddr = "0.0.0.0"
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
}
else
{
write-host "The message" $type "is not valid."
exit
}



if($RA)
{
$RAaddr = $interfaceip
$DhcpObj.ActasRelayAgent($RAaddr);
$localaddr = $interfaceip
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
$DhcpConnector.InitWinSock("RELAY_AGENT");
}
else
{
$DhcpConnector.InitWinSock("CLIENT");
}
$PacketLength = $DhcpObj.PackDhcpDataandOption();


$DhcpConnector.SendPacket($DhcpObj.DhcpSendMessageBuffer, $PacketLength);
$DhcpObj.SetSendTime();
$DhcpConnector.RecvPacket(([ref]$DhcpObj));
if($type -eq "DISCOVER" -or $type -eq "FULLLEASE")
{
#Write-Host "S.NO Server Address Your IP Address Timespan"
#Write-Host "**** ************** *************** ********"
for($pos = 0; $pos -lt $DhcpObj.NoRecvMessage; $pos++)
{
$DhcpObj.UnPackDataandOtions($DhcpObj.DhcpRecvMessageLength[$pos], $pos);
#Write-Host ("{0,4} {1,14} {2,15} {3,-10:n2}" -f $pos,$DhcpObj.GetServerAddr(),$DhcpObj.GetYourAddr(),$DhcpObj.FindTimeDifference($pos))
$DhcpRetObj.SetServerIP($DhcpObj.GetServerAddr());
$DhcpRetObj.SetTime($DhcpObj.FindTimeDifference($pos));
$DhcpRetObj.SetYourIPAddress($DhcpObj.GetYourAddr());
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_DOMAIN_NAME_SERVER, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetDNS($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_WINS_SERVER, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetWINS($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_GATEWAY, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetGateway($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_DOMAIN_NAME, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetDNSName($DhcpObj.GetDNSName($returnValue));
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_SUBNET_MASK, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetSubnetMask($returnValue);
}
$DhcpRetObj
}
if($DhcpObj.NoRecvMessage -eq 0)
{
$DhcpRetObj.CleanUp();
$DhcpRetObj
}
}
elseif($type -eq "RELEASE")
{
$pos = 0;
$DhcpObj.UnPackDataandOtions($DhcpObj.DhcpRecvMessageLength[$pos], $pos);
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_MESSAGE_TYPE, $pos)
if($returnValue -eq $null)
{
Write-Host "The Message" $type "has been completed succesfully."
}
else
{
Write-Host "The Message" $type "has failed."
}
}
else
{
$pos = 0;
$DhcpObj.UnPackDataandOtions($DhcpObj.DhcpRecvMessageLength[$pos], $pos);
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_MESSAGE_TYPE, $pos)
if($returnValue -eq [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_ACK_MESSAGE)
{
$DhcpRetObj.SetServerIP(([System.Net.IPEndPoint]$DhcpObj.RecvEndPoints[$pos]).Address);
$DhcpRetObj.SetTime($DhcpObj.FindTimeDifference($pos));
$DhcpRetObj.SetYourIPAddress($DhcpObj.GetYourAddr());

$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_DOMAIN_NAME_SERVER, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetDNS($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_WINS_SERVER, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetWINS($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_GATEWAY, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetGateway($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_DOMAIN_NAME, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetDNSName($DhcpObj.GetDNSName($returnValue));
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_SUBNET_MASK, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetSubnetMask($returnValue);
}
$DhcpRetObj
Write-Host "The Message" $type "has been completed succesfully."
break;
}
else
{
$DhcpRetObj.CleanUp();
$DhcpRetObj
Write-Host "The Message" $type "has failed."
}
}

if($type -eq "FULLLEASE")
{
if($serverip -ne [System.String]::Empty)
{
$pos = 0
}
else
{
$pos = Read-Host -Prompt "Select a server"
}
$DhcpObj.UnPackDataandOtions($DhcpObj.DhcpRecvMessageLength[$pos], $pos);
$serverid = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_SERVER_ID, $pos)
$requestedip = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_REQUESTED_IP, $pos)
$DhcpObj.DhcpSendMessageOptionBuffer = $null
$DhcpObj.DhcpSendMessageOptionLength = 0
$DhcpObj.NoRecvMessage = 0
$DhcpPacker.ConstructMessage(([ref]$DhcpObj), [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_REQUEST_MESSAGE);
if($RA)
{
$RAaddr = $interfaceip
$DhcpObj.ActasRelayAgent($RAaddr);
$localaddr = $interfaceip
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
$DhcpConnector.InitWinSock("RELAY_AGENT");
}
else
{
$localaddr = "0.0.0.0"
$DhcpConnector.SetLocalandDestAddr($localaddr, $serveraddr);
$DhcpConnector.InitWinSock("CLIENT");
}
$DhcpObj.SetServerIDbyByte($serverid);
if($hwaddr -ne [System.String]::Empty)
{
$DhcpObj.SetClientID($hwaddr);
}
$DhcpObj.SetRequestedIPbyString($DhcpObj.GetYourAddr());
$PacketLength = $DhcpObj.PackDhcpDataandOption();
$DhcpConnector.SendPacket($DhcpObj.DhcpSendMessageBuffer, $PacketLength);
$DhcpConnector.RecvPacket(([ref]$DhcpObj));
$pos = 0;
$DhcpObj.UnPackDataandOtions($DhcpObj.DhcpRecvMessageLength[$pos], $pos);
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_MESSAGE_TYPE, $pos)
if($returnValue -eq [DhcpMessage.DHCP_MESSAGE_TYPE]::DHCP_ACK_MESSAGE)
{
$DhcpRetObj.SetServerIP(([System.Net.IPEndPoint]$DhcpObj.RecvEndPoints[$pos]).Address);
$DhcpRetObj.SetTime($DhcpObj.FindTimeDifference($pos));
$DhcpRetObj.SetYourIPAddress($DhcpObj.GetYourAddr());
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_DOMAIN_NAME_SERVER, $pos)
if($returnValue -ne $null)
{
$DhcpRetObj.SetDNS($returnValue);
}
$returnValue = $DhcpObj.GetRecvMessageOption([DhcpMessage.DHCP_MESSAGE_OPTION]::OPTION_SUBNET_MASK, $pos)
$DhcpRetObj.SetSubnetMask($returnValue);
$DhcpRetObj
Write-Host "The Message" $type "has been completed succesfully."
}
else
{
$DhcpRetObj.CleanUp();
$DhcpRetObj
Write-Host "The Message" $type "has failed."
}
}</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>type</Name>
<Value>$Config/Type$</Value>
</Parameter>
<Parameter>
<Name>serverip</Name>
<Value>$Config/ServerIP$</Value>
</Parameter>
<Parameter>
<Name>interfaceip</Name>
<Value>$Config/TargetIP$</Value>
</Parameter>
<Parameter>
<Name>retry</Name>
<Value>$Config/RetryTimes$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</ProbeAction>
</MemberModules>
<Composition>
<Node ID="PS"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>Windows!Microsoft.Windows.SerializedObjectData</OutputType>
<TriggerOnly>true</TriggerOnly>
</ProbeActionModuleType>