Unix Programming - icmp socket

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2006 > icmp socket





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 icmp socket
ibaloubi

2006-08-13, 7:26 am

Having some problem with a socket, for icmp.

here is the code:
<CODE>
struct protoent *proto;
struct sockaddr_in from;
struct sockaddr_in whereto;
hostent * he = gethostbyname(host);
if ( !he )
{
std::cout << "Failed to resolve host " << host << std::endl;
}
memcpy(&whereto.sin_addr.s_addr, he->h_addr_list[0], 4);
bzero((char *)&whereto, sizeof(struct sockaddr) );
if ((proto = getprotobyname("icmp")) == NULL)
{
fprintf(stderr, "icmp: unknown protocol\n");
exit(1);
}
if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0)
{
std::cout << "ingen tillgång" << std::endl;
perror("ping: socket");
exit(2);
}
</CODE>
it exits when the socketis created, "operation not permitted."
does icmp need to be compiled into the kernel? is the above code correct?
thank's
does icmp need to be enab
Rainer Temme

2006-08-13, 7:26 am

ibaloubi wrote:
> here is the code:


> struct protoent *proto;
> struct sockaddr_in from;
> struct sockaddr_in whereto;
> hostent * he = gethostbyname(host);
> if ( !he )
> {
> std::cout << "Failed to resolve host " << host << std::endl;
> }


> memcpy(&whereto.sin_addr.s_addr, he->h_addr_list[0], 4);
> bzero((char *)&whereto, sizeof(struct sockaddr) );


You might want to bzero first, and then fill out
whereto.sin_addr.s_addr! (The way you're doing it now,
will leave the complete structure zeroed).

> if ((proto = getprotobyname("icmp")) == NULL)
> {
> fprintf(stderr, "icmp: unknown protocol\n");
> exit(1);
> }
> if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto)) < 0)
> {
> std::cout << "ingen tillgång" << std::endl;
> perror("ping: socket");
> exit(2);
> }


> it exits when the socketis created, "operation not permitted."
> does icmp need to be compiled into the kernel? is the above code correct?


"operation not pemitted" doesn't mean it is not implemented in the
kernel. It means the process executing the code doesn't have
suficcient permissions to create a raw-socket. (Your process might
need root priviledges on your platform in order to sucessfully create
this type of socket).

Rainer
ibaloubi

2006-08-13, 1:23 pm

Rainer Temme wrote:

> ibaloubi wrote:
>
>
>
> You might want to bzero first, and then fill out
> whereto.sin_addr.s_addr! (The way you're doing it now,
> will leave the complete structure zeroed).
>
>
>
> "operation not pemitted" doesn't mean it is not implemented in the
> kernel. It means the process executing the code doesn't have
> suficcient permissions to create a raw-socket. (Your process might
> need root priviledges on your platform in order to sucessfully create
> this type of socket).
>
> Rainer

Thank's rainer for pointing the bzero out, didn't see it.
Furhter problems:
can now create socket, but get "permission denied" attempting to send with
sendto. What could be the reason for this behaviour?

Rainer Temme

2006-08-13, 1:23 pm

ibaloubi wrote:
> Furhter problems:
> can now create socket, but get "permission denied" attempting to send with
> sendto. What could be the reason for this behaviour?


Show the code behind socket() as well.

Rainer
ibaloubi

2006-08-13, 1:23 pm

Rainer Temme wrote:

> ibaloubi wrote:
>
> Show the code behind socket() as well.
>
> Rainer

<code>
bool pinger( const char * host)
{
ntransmitted = 1;
struct protoent *proto;
struct sockaddr_in from;
struct sockaddr_in whereto;
hostent * he = gethostbyname(host);
if ( !he )
{
std::cout << "Failed to resolve host " << host << std::endl;
}
bzero((char *)&whereto, sizeof(struct sockaddr) );
whereto.sin_family = AF_INET;
whereto.sin_addr.s_addr = inet_addr(he->h_addr_list[0]);
if ((proto = getprotobyname("icmp")) == NULL)
{
fprintf(stderr, "icmp: unknown protocol\n");
exit(1);
}
if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto) ) < 0)
{
perror("ping: socket");
exit(2);
}
static u_char outpack[MAXPACKET];
register struct icmp *icp = (struct icmp *) outpack;
int i, cc;
register struct timeval *tp = (struct timeval *) &outpack[8];
register u_char *datap = &outpack[8+sizeof(struct timeval)];
icp->icmp_type = ICMP_ECHO;
icp->icmp_code = 0;
icp->icmp_cksum = 0;
icp->icmp_seq = ntransmitted;
icp->icmp_id = ident; /* ID */
cc = datalen+8; /* skips ICMP portion */

if (timing)
gettimeofday( tp, &tz );

for( i=8; i<datalen; i++) /* skip 8 for time */
*datap++ = i;

/* Compute ICMP checksum here */
icp->icmp_cksum = in_cksum( (short unsigned int*)icp, cc );

/* cc = sendto(s, msg, len, flags, to, tolen) */
i = sendto( s, outpack, cc, 0, (const sockaddr*)&whereto, sizeof(struct
sockaddr) );
if( i < 0 || i != cc )
{
if( i<0 )
{
perror("sendto");
return false;
}
printf("ping: wrote %s %d chars, ret=%d\n",
host, cc, i );
fflush(stdout);
}
</code>
something is missing, but I can't figure it out.

It is not an issue with my own system, as the same happens on my dedicated
server.

any advice much appreciated.

Rainer Temme

2006-08-13, 7:29 pm

ibaloubi wrote:

ibaloubi,

Comparing your code with the pinger I wrote a while ago,
I found not overly much important differences ...


> bool pinger( const char * host)
> {
> ntransmitted = 1;
> struct protoent *proto;
> struct sockaddr_in from;
> struct sockaddr_in whereto;
> hostent * he = gethostbyname(host);
> if ( !he )
> {
> std::cout << "Failed to resolve host " << host << std::endl;
> }
> bzero((char *)&whereto, sizeof(struct sockaddr) );
> whereto.sin_family = AF_INET;
> whereto.sin_addr.s_addr = inet_addr(he->h_addr_list[0]);
> if ((proto = getprotobyname("icmp")) == NULL)
> {
> fprintf(stderr, "icmp: unknown protocol\n");
> exit(1);
> }
> if ((s = socket(AF_INET, SOCK_RAW, proto->p_proto) ) < 0)
> {
> perror("ping: socket");
> exit(2);
> }


Set socket to broadcast-on ... one difference that
I saw comparing your code with my code.
{
int rc,on;
on=1;
rc=setsockopt(fd,SOL_SOCKET,SO_BROADCAST
,&on,sizeof(on));
}


> static u_char outpack[MAXPACKET];
> register struct icmp *icp = (struct icmp *) outpack;
> int i, cc;
> register struct timeval *tp = (struct timeval *) &outpack[8];
> register u_char *datap = &outpack[8+sizeof(struct timeval)];
> icp->icmp_type = ICMP_ECHO;
> icp->icmp_code = 0;
> icp->icmp_cksum = 0;
> icp->icmp_seq = ntransmitted;
> icp->icmp_id = ident; /* ID */
> cc = datalen+8; /* skips ICMP portion */


Where was datalen initialized?

>
> if (timing)
> gettimeofday( tp, &tz );
>
> for( i=8; i<datalen; i++) /* skip 8 for time */
> *datap++ = i;
>
> /* Compute ICMP checksum here */
> icp->icmp_cksum = in_cksum( (short unsigned int*)icp, cc );
>
> /* cc = sendto(s, msg, len, flags, to, tolen) */
> i = sendto( s, outpack, cc, 0, (const sockaddr*)&whereto, sizeof(struct
> sockaddr) );


try sizeof(whereto) as last parameter.

> if( i < 0 || i != cc )
> {
> if( i<0 )
> {
> perror("sendto");
> return false;
> }
> printf("ping: wrote %s %d chars, ret=%d\n",
> host, cc, i );
> fflush(stdout);
> }



Rainer
Barry Margolin

2006-08-13, 7:29 pm

In article <oiFDg.1492$J27.743@reader1.news.jippii.net>,
ibaloubi <bobofibodibo@aaakyars.com> wrote:

> can now create socket, but get "permission denied" attempting to send with
> sendto. What could be the reason for this behaviour?


Are you running as root? Only the superuser can use raw sockets.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
ibaloubi

2006-08-14, 1:26 am

Rainer Temme wrote:

> ibaloubi wrote:
>
> ibaloubi,
>
> Comparing your code with the pinger I wrote a while ago,
> I found not overly much important differences ...
>
>
>
> Set socket to broadcast-on ... one difference that
> I saw comparing your code with my code.
> {
> int rc,on;
> on=1;
> rc=setsockopt(fd,SOL_SOCKET,SO_BROADCAST
,&on,sizeof(on));


thank's, that did the trick.

> }
>
>
>
> Where was datalen initialized?
>
>
> try sizeof(whereto) as last parameter.
>
>
>
> Rainer


Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com