Hello,
I installed the winpkfilter api and tried modifying the passthru application in the samples directory. I am trying to append gps data (latitude/longitude/altitude) to every outgoing udp packet. I think that I am modifying the data correctly, but nothing passes beyond the passthru application. I modified the code in the if case for sending, all other code is the same as the sample. The UDP packet consists of a platform header (31 bytes) and a string of data (something along the lines of this: update UAV-123 10 20 30 40 ID 3-0). I know that data is going out before I start the application, but whenever i run it the data stops flowing. As soon as I close it the data resumes.
// if the packet is to be sent
if (PacketBuffer.m_dwDeviceFlags == Ndisapi.PACKET_FLAG_ON_SEND)
{
// get a pointer to the EF header
pPlatformHeader = (PlatformHeader*)((byte*)pUdpHeader + sizeof(UdpHeader));
// get a pointer to the data and the length of the data
byte* pData = (byte*)pPlatformHeader + sizeof(PlatformHeader);
long dataLength = PacketBuffer.m_Length - (sizeof(ETHER_HEADER) + sizeof(IPHeader) +
sizeof(UdpHeader) + sizeof(PlatformHeader));
// copy the data into a local array
byte[] data = new byte[dataLength];
Marshal.Copy((IntPtr)pData, data, 0, (int)dataLength);
// look to see if the message is an update message
String msg = ASCIIEncoding.ASCII.GetString(data);
if (msg.Contains("update"))
{
// get the platform data from the message
String[] tokens = msg.Split(new char[] { ' ' });
PlatformLatitude = System.Convert.ToDouble(tokens[2]);
PlatformLongitude = System.Convert.ToDouble(tokens[3]);
PlatformAltitude = System.Convert.ToDouble(tokens[4]);
}
PacketBuffer.m_Length += (uint)sizeof(GPSHeader);
// Change the length field of the IP header
ushort IPLen = (ushort)(ntohs(pIpHeader->Len) + (ushort)sizeof(GPSHeader));
pIpHeader->Len = htons(IPLen);
// Recalculate IP checksum
pIpHeader->Sum = 0;
byte[] ipHeader = new byte[sizeof(IPHeader)];
Marshal.Copy((IntPtr)pIpHeader, ipHeader, 0, ipHeader.Length);
ushort newChecksum = ComputeHeaderIpChecksum(ipHeader, 0, ipHeader.Length);
pIpHeader->Sum = htons(newChecksum);
// adjust udp packet length
ushort udpLength = (ushort)(ntohs(pUdpHeader->length) + (ushort)sizeof(GPSHeader));
pUdpHeader->length = htons(udpLength);
// recalc udp checksum
pUdpHeader->th_sum = 0;
// build the outgoing packet
byte[] newPacket = new byte[sizeof(ETHER_HEADER) + sizeof(IPHeader) + sizeof(UdpHeader) +
sizeof(PlatformHeader) + dataLength + sizeof(GPSHeader)];
Marshal.Copy((IntPtr)pEthHeader, newPacket, 0, sizeof(ETHER_HEADER));
Marshal.Copy((IntPtr)pIpHeader, newPacket, sizeof(ETHER_HEADER), sizeof(IPHeader));
Marshal.Copy((IntPtr)pUdpHeader, newPacket, sizeof(ETHER_HEADER) + sizeof(IPHeader),
sizeof(UdpHeader));
Marshal.Copy((IntPtr)pPlatformHeader, newPacket, sizeof(ETHER_HEADER) + sizeof(IPHeader) +
sizeof(UdpHeader), sizeof(PlatformHeader));
Marshal.Copy((IntPtr)pData, newPacket, sizeof(ETHER_HEADER) + sizeof(IPHeader) +
sizeof(UdpHeader) + sizeof(PlatformHeader), (int)dataLength);
int gpsOffset = sizeof(ETHER_HEADER) + sizeof(IPHeader) + sizeof(UdpHeader) +
sizeof(PlatformHeader) + (int)dataLength;
Array.Copy(System.BitConverter.GetBytes(PlatformLatitude), 0, newPacket, gpsOffset, sizeof(double));
Array.Copy(System.BitConverter.GetBytes(PlatformLongitude), 0, newPacket, gpsOffset+8,
sizeof(double));
Array.Copy(System.BitConverter.GetBytes(PlatformAltitude), 0, newPacket, gpsOffset+16, sizeof(double));
IntPtr outgoingPacket = Marshal.AllocHGlobal((IntPtr)(newPacket.Length));
Marshal.Copy(newPacket, 0, outgoingPacket, newPacket.Length);
ETH_REQUEST outgoingRequest = new ETH_REQUEST();
outgoingRequest.hAdapterHandle = Request.hAdapterHandle;
outgoingRequest.EthPacket.Buffer = outgoingPacket;
// put the modified packet on the network card for sending
Ndisapi.SendPacketToAdapter(hNdisapi, ref outgoingRequest);
// free outgoing memory
Marshal.FreeHGlobal(outgoingPacket);
}
Also here is the code I used for the IP checksum calculation, in case that is the problem.
ushort ComputeHeaderIpChecksum(byte[] header, int start, int length)
{
ushort word16;
long sum = 0;
for (int i = start; i < (length + start); i += 2)
{
word16 = (ushort)(((header[i] <<

& 0xFF00) + (header[i + 1] & 0xFF));
sum += (long)word16;
}
while ((sum >> 16) != 0)
{
sum = (sum & 0xFFFF) + (sum >> 16);
}
sum = ~sum;
return (ushort)sum;
}
Any help would be greatly appreciated.