Prev: Unix scripting
Next: sed
From: ibaloubi on
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
From: Rainer Temme on
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
From: ibaloubi on
Rainer Temme wrote:

> 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
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?

From: Rainer Temme on
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
From: ibaloubi on
Rainer Temme wrote:

> 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
<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.

 |  Next  |  Last
Pages: 1 2
Prev: Unix scripting
Next: sed