Unix Programming - raw sockets and ping problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > raw sockets and ping problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author raw sockets and ping problem
fatso

2004-03-21, 3:33 am

Hi all

I'm trying to build ICMP packets in order to send PING requests.
When I don't add any data to my packet, the remote host answers
correctly, however, when i add some data bytes, the remote host
doesn't answer back.

I don't undersannd what happens, and in the two cases (data or not)
the checksums are correct as for Ethereal. Could anyone help me ?

This the function which builds the packets :


[..]
/* Various headers's sizes */
#define TAILLE_ETHER sizeof(struct ethhdr)
#define TAILLE_IP sizeof(struct iphdr)
#define TAILLE_ICMP sizeof(struct icmphdr)
#define TAILLE_DATA 20

#define TAILLE_HDR TAILLE_ETHER + TAILLE_IP + TAILLE_ICMP
#define TAILLE_TOTALE TAILLE_HDR + TAILLE_DATA

/* target's data */
#define CIBLE "xxx.xxx.xxx.xxx.xxx"
#define MAC_CIBLE "XX.XX.XX.XX.XX.XX"

/* Number of packets to send */
#define NB_PACKET 2

struct toto
{
char adresse_ip[15]; /* IP adress */
char adresse_mc[17]; /* MAC adress */
};

[..]

void *forge_packet (struct toto *gars)
{
char *packet = NULL;

struct ether_addr *amac_cible = NULL,
*amac_desti = NULL;
struct ethhdr *ether_hdr = NULL;
struct iphdr *ip_hdr = NULL;
struct icmphdr *icmp_hdr = NULL;
struct in_addr src, dst;
char *data = NULL;
int j = 0;

/* Packet's definition */
packet = (char *) malloc (TAILLE_TOTALE * sizeof (char));
ether_hdr = (struct ethhdr *) (packet);
ip_hdr = (struct iphdr *) (packet + TAILLE_ETHER);
icmp_hdr = (struct icmphdr *) (packet + TAILLE_ETHER + TAILLE_IP);
data = (char *) (packet + TAILLE_HDR);

/* from ip adress (char[15]) to IP (struct in_addr) */
inet_aton (CIBLE , &src);
inet_aton (gars->adresse_ip, &dst);

/* from MAC adress (char[17]) to MAC (int[6])
* then we copy this array in an unsigned char[6] one */

amac_cible = ether_aton (MAC_CIBLE);
for (j = 0; j < ETH_ALEN; j++)
{
ether_hdr->h_source[j] =
(unsigned char) (amac_cible->ether_addr_octet[j]);

}

amac_desti = ether_aton (gars->adresse_mc);
for (j = 0; j < ETH_ALEN; j++)
{
ether_hdr->h_dest[j] =
(unsigned char) (amac_desti->ether_addr_octet[j]);
}

/* We choose IP protocol for our packet */
ether_hdr->h_proto = htons (ETH_P_IP);

/* On remplit l'entete IP du packet */
ip_hdr->saddr = src.s_addr;
ip_hdr->daddr = dst.s_addr;
ip_hdr->version = (unsigned int) 4;
ip_hdr->ttl = (u_int8_t) 128;
ip_hdr->protocol = (u_int8_t) IPPROTO_ICMP;
ip_hdr->id = (u_int16_t) rand ();
ip_hdr->frag_off = (u_int16_t) 0;
ip_hdr->tos = (u_int8_t) 0;
ip_hdr->tot_len = (u_int16_t) htons (TAILLE_TOTALE);
ip_hdr->ihl = (unsigned int) 5;
ip_hdr->check = (u_int16_t) 0;

/* ICMP Header */
icmp_hdr->type = ICMP_ECHO;
icmp_hdr->code = 0;
icmp_hdr->checksum = 0;
icmp_hdr->un.echo.id = getpid ();
icmp_hdr->un.echo.sequence = 0;

/* We add random data ... */
for (j = TAILLE_HDR; j < TAILLE_TOTALE; j++)
{
packet[j] =
(unsigned char) ((int) (255.0 * rand () / (RAND_MAX + 1.0)));
}

/* checksums */
icmp_hdr->checksum = (u_int16_t) cksum ((unsigned short
*)(icmp_hdr),
TAILLE_ICMP + TAILLE_DATA);
ip_hdr->check = (u_int16_t) cksum ((unsigned short *)(ip_hdr),
TAILLE_IP);

/* we send packets */
send_packet (packet);

return 0;
}
[..]

Thanks
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com