Prev: Unix scripting
Next: sed
From: Rainer Temme on
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
From: Barry Margolin on
In article <oiFDg.1492$J27.743(a)reader1.news.jippii.net>,
ibaloubi <bobofibodibo(a)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(a)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 ***
From: ibaloubi on
Rainer Temme wrote:

> 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));

thank's, that did the trick.

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

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