Examples/KniPong/main.cpp
/**
 * Handle all UDP packets as a packet carying a "ping" string to "pong" to with same string.
 * Handle only packets that are of type: Eth / Ip / Udp / Payload.
 */
Handle all UDP packets as a packet carying a "ping" string to "pong" to with same string.
Handle only packets that are of type: Eth / Ip / Udp / Payload.
Handle all UDP packets as a packet carying a " ping " string to " pong " to with same string . Handle only packets that are of type : Eth / Ip / Udp / Payload .
inline bool processUdp(pcpp::Packet& packet, pcpp::UdpLayer* udpLayer)
{
	pcpp::EthLayer* ethernetLayer = NULL;
	pcpp::IPv4Layer* ipLayer = NULL;
	ethernetLayer = packet.getLayerOfType<pcpp::EthLayer>();
	pcpp::ether_header ethHdr;
	pcpp::ether_header* origEthHdr = ethernetLayer->getEthHeader();
	std::memcpy(&ethHdr, origEthHdr, sizeof(ethHdr));
	std::memcpy(ethHdr.dstMac, origEthHdr->srcMac, sizeof(ethHdr.dstMac));
	std::memcpy(ethHdr.srcMac, origEthHdr->dstMac, sizeof(ethHdr.srcMac));
	std::memcpy(origEthHdr, &ethHdr, sizeof(ethHdr));
	ipLayer = packet.getLayerOfType<pcpp::IPv4Layer>();
	if (ipLayer == NULL) 
		return false;
	pcpp::iphdr ipHdr;
	pcpp::iphdr* origIpHdr = ipLayer->getIPv4Header();
	std::memcpy(&ipHdr, origIpHdr, sizeof(ipHdr));
	if (pcpp::netToHost16(ipHdr.fragmentOffset) & 0x1FFF) 
		return false;
	std::memcpy(&ipHdr.ipSrc, &origIpHdr->ipDst, sizeof(ipHdr.ipSrc));
	std::memcpy(&ipHdr.ipDst, &origIpHdr->ipSrc, sizeof(ipHdr.ipDst));
	ipHdr.ipId = std::rand() & 0xFFFF;
	ipHdr.timeToLive = 64;
	std::memcpy(origIpHdr, &ipHdr, sizeof(ipHdr));
	pcpp::udphdr udpHdr;
	pcpp::udphdr* origUdpHdr = udpLayer->getUdpHeader();
	std::memcpy(&udpHdr, origUdpHdr, sizeof(udpHdr));
	std::memcpy(&udpHdr.portSrc, &origUdpHdr->portDst, sizeof(udpHdr.portSrc));
	std::memcpy(&udpHdr.portDst, &origUdpHdr->portSrc, sizeof(udpHdr.portDst));
	std::memcpy(origUdpHdr, &udpHdr, sizeof(udpHdr));
	packet.computeCalculateFields();
	return true;
}
inline bool processUdp ( pcpp :: Packet & packet , pcpp :: UdpLayer * udpLayer ) { pcpp :: EthLayer * ethernetLayer = NULL ; pcpp :: IPv4Layer * ipLayer = NULL ; ethernetLayer = packet . getLayerOfType < pcpp :: EthLayer > ( ) ; pcpp :: ether_header ethHdr ; pcpp :: ether_header * origEthHdr = ethernetLayer -> getEthHeader ( ) ; std :: memcpy ( & ethHdr , origEthHdr , sizeof ( ethHdr ) ) ; std :: memcpy ( ethHdr . dstMac , origEthHdr -> srcMac , sizeof ( ethHdr . dstMac ) ) ; std :: memcpy ( ethHdr . srcMac , origEthHdr -> dstMac , sizeof ( ethHdr . srcMac ) ) ; std :: memcpy ( origEthHdr , & ethHdr , sizeof ( ethHdr ) ) ; ipLayer = packet . getLayerOfType < pcpp :: IPv4Layer > ( ) ; if ( ipLayer == NULL ) return false ; pcpp :: iphdr ipHdr ; pcpp :: iphdr * origIpHdr = ipLayer -> getIPv4Header ( ) ; std :: memcpy ( & ipHdr , origIpHdr , sizeof ( ipHdr ) ) ; if ( pcpp :: netToHost16 ( ipHdr . fragmentOffset ) & 0x1FFF ) return false ; std :: memcpy ( & ipHdr . ipSrc , & origIpHdr -> ipDst , sizeof ( ipHdr . ipSrc ) ) ; std :: memcpy ( & ipHdr . ipDst , & origIpHdr -> ipSrc , sizeof ( ipHdr . ipDst ) ) ; ipHdr . ipId = std :: rand ( ) & 0xFFFF ; ipHdr . timeToLive = 64 ; std :: memcpy ( origIpHdr , & ipHdr , sizeof ( ipHdr ) ) ; pcpp :: udphdr udpHdr ; pcpp :: udphdr * origUdpHdr = udpLayer -> getUdpHeader ( ) ; std :: memcpy ( & udpHdr , origUdpHdr , sizeof ( udpHdr ) ) ; std :: memcpy ( & udpHdr . portSrc , & origUdpHdr -> portDst , sizeof ( udpHdr . portSrc ) ) ; std :: memcpy ( & udpHdr . portDst , & origUdpHdr -> portSrc , sizeof ( udpHdr . portDst ) ) ; std :: memcpy ( origUdpHdr , & udpHdr , sizeof ( udpHdr ) ) ; packet . computeCalculateFields ( ) ; return true ; }
Handle all UDP packets as a packet carying a "ping" string to "pong" to with same string.
Handle all UDP packets as a packet carying a " ping " string to " pong " to with same string .
// Swap MACs for Ethernet layer // Some invalid packet // Fragmanted packet // Swap src and dst IPs // Randomize IP id // Set by RFC791 // Swap src and dst ports // Calculate checksums of IP and UDP layers // Packet is ready to be sent