[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(usagi-users 02062) Re: ICMP_ECHO raw socket



Thanks David.
You were right about it, I didn't look it properly.

-- basit

removed student signature :)

On Tue, 7 Jan 2003, David Lamparter wrote:

> Abdul Basit wrote:
> > // -- code snipped
> >
> >        buf = (char*) malloc(sizeof(struct ip)+sizeof(struct icmp));
> >
> >         sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
> >
> >         for (;;) {
> >                 n = recv(sockfd, buf, sizeof(buf),0);
>
> you can't use sizeof() for dynamically allocated buffers. sizeof will
> give you 4, the size of the pointer, not the size of the buffer behind
> the pointer ...
>
> >                 ip = (struct ip *) buf;
> >                 ip_len = ip->ip_hl << 2; // ip header length
> >
> >                 icmp = (struct icmp *) (buf + ip_len);
> >
> >                 printf("n= %d\n",n);
> >                 if (icmp->icmp_type == ICMP_ECHO) {
> >                         printf("ping request arrived\n");
> >                 }
> >         }
> >
> >
> > It seems that the kernel donot pass ICMP_ECHO to raw socket
> > if i use ICMP_ECHOREPLY, it will work,
> >
> looks like the memory you allocate with malloc() is initialized to zero
> ... ICMP_ECHOREPLY is zero ...
>
> replace "sizeof(buf)" with "sizeof(struct ip)+sizeof(struct icmp)" and
> it should work
>
> David Lamparter
>
> -----
> 17yr old high school student
>
>
>