PDA

View Full Version : how to send ICMP packet



eleanor
30th October 2007, 13:11
Hi, I've build ICMP echo request packet:



#include <string.h>

class CIcmp {
public:
CIcmp(char type[8], char code[8], char checksum[8], char identifier[8], char sequenceNumber[8], char *additionalData=0);
~CIcmp();
char* icmpBuildPacket(char *packet);

};





#include "icmp.h"

CIcmp::CIcmp(char type[8], char code[8], char checksum[8], char identifier[8], char sequenceNumber[8], char *additionalData) {
char packet[strlen(type) + strlen(code) + strlen(checksum) + strlen(identifier) + strlen(sequenceNumber) + strlen(additionalData)];

//packet[0] = *type;
strcpy(packet, type);
strcpy(packet, code);
strcpy(packet, checksum);
strcpy(packet, identifier);
strcpy(packet, sequenceNumber);
strcpy(packet, additionalData);

icmpBuildPacket(packet);

}

CIcmp::~CIcmp() {

}

char* CIcmp::icmpBuildPacket(char *packet) {
return packet;
}


Now I want to know, if I did that correctly? (I know I have to calculate the checksum, but let's say the additionalData is always 0 for now)

Well, now I need a way to send this packet over the network to destination --> what would be the best way to do that?

kernel_panic
30th October 2007, 13:48
raw socket.
i think you have to write an own rawsocket class, which handles the socket platform specific.

maybe this helps you:
http://www.c-worker.ch/winsock/ping.cpp
http://www.alhem.net/Sockets/index.html

wysota
30th October 2007, 14:23
If you are using a secure* system, you need super-user priviledges to craft icmp packets and you have to use platform dependent code as already suggested.


*) non-Windows