Unix Programming - error--bad network address

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > April 2006 > error--bad network address





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 error--bad network address
xoinki

2006-04-27, 7:55 am

hi all,

after running this code in redhat 9 linux system, i am getting "bad
network address" error.
this program sends a datagram on port 89, to a system specified by IP
address..(./a.out IP address) the datagram sent is not a standard OSPF
packet, but it resembles ICMP structure..
but this code runs if we are sending the datagram only once rather than
every 3 seconds.

please help..

#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include<signal.h>

#define PACKETSIZE 64

void ping(struct sockaddr_in *addr);

struct icmphdr1
{
u_int8_t type; /* message type */
u_int8_t code; /* type sub-code */
u_int16_t checksum;
union
{
struct
{
u_int16_t id;
u_int16_t sequence;
} echo; /* echo datagram */
u_int32_t gateway; /* gateway address */
struct
{
u_int16_t __unused;
u_int16_t mtu;
} frag; /* path mtu discovery */
} un;
};

struct packet
{
struct icmphdr1 hdr;
char msg[PACKETSIZE-sizeof(struct icmphdr1)];
};

int pid=-1;
struct protoent *proto=NULL;

/*sending a packet every 10 seconds*/

void send1(struct sockaddr_in *addr)
{
alarm(3);
ping(addr);
}

/*--------------------------------------------------------------------*/
/*--- checksum - standard 1s complement checksum
---*/
/*--------------------------------------------------------------------*/
unsigned short checksum(void *b, int len)
{ unsigned short *buf = b;
unsigned int sum=0;
unsigned short result;

for ( sum = 0; len > 1; len -= 2 )
sum += *buf++;
if ( len == 1 )
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}

/*--------------------------------------------------------------------*/
/*--- display - present echo info
---*/
/*--------------------------------------------------------------------*/
void display(void *buf, int bytes)
{ int i;
struct iphdr *ip = buf;
struct icmphdr1 *icmp = buf+ip->ihl*4;

printf("----------------\n");
/* for ( i = 0; i < bytes; i++ )
{
if ( !(i & 15) ) printf("\n X: ", i);
printf(" X ", ((unsigned char*)buf)[i]);
}*/
printf("\n");
printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ",
ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
ip->ttl, inet_ntoa(ip->saddr));
printf("dst=%s\n", inet_ntoa(ip->daddr));
if ( icmp->un.echo.id == pid )
{
printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n",
icmp->type, icmp->code, ntohs(icmp->checksum),
icmp->un.echo.id, icmp->un.echo.sequence);
}
}

/*--------------------------------------------------------------------*/
/*--- listener - separate process to listen for and collect
messages--*/
/*--------------------------------------------------------------------*/
void listener(void)
{ int sd;
struct sockaddr_in addr;
unsigned char buf[1024];

sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
if ( sd < 0 )
{
perror("socket");
exit(0);
}
for (;;)
{ int bytes, len=sizeof(addr);

bzero(buf, sizeof(buf));
bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr,
&len);
if ( bytes > 0 )
{
display(buf, bytes);
printf("yess\n");
}
else
perror("recvfrom");
}
exit(0);
}

/*--------------------------------------------------------------------*/
/*--- ping - Create message and send it.
---*/
/*--------------------------------------------------------------------*/
void ping(struct sockaddr_in *addr)
{ const int val=255;
int i, sd, cnt=1;
struct packet pckt;
struct sockaddr_in r_addr;

sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
if ( sd < 0 )
{
perror("socket");
return;
}
if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
perror("Set TTL option");
if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
perror("Request nonblocking I/O");
//for (;;)
{ int len=sizeof(r_addr);

printf("Msg #%d\n", cnt);
if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr,
&len) > 0 )
printf("***Got message!***\n");
bzero(&pckt, sizeof(pckt));
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = pid;
for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
pckt.msg[i] = i+'0';
pckt.msg[i] = 0;
pckt.hdr.un.echo.sequence = cnt++;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
if ( sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)addr,
sizeof(*addr)) <= 0 )
perror("sendto");
sleep(1);
}
}

/*--------------------------------------------------------------------*/
/*--- main - look up host and start ping processes.
---*/
/*--------------------------------------------------------------------*/
int main(int count, char *strings[])
{ struct hostent *hname;
struct sockaddr_in addr;

if ( count != 2 )
{
printf("usage: %s <addr>\n", strings[0]);
exit(0);
}
if ( count > 1 )
{
pid = getpid();
proto = getprotobyname("OSPFIGP");
hname = gethostbyname(strings[1]);
bzero(&addr, sizeof(addr));
addr.sin_family = hname->h_addrtype;
addr.sin_port = 0;
addr.sin_addr.s_addr = *(long*)hname->h_addr;
signal(SIGALRM,send1);
if ( fork() == 0 )
listener();
else
send1(&addr);
wait(0);
}
else
printf("usage: myping <hostname>\n");
return 0;
}


thanking u in advance..
xoinki

Barry Margolin

2006-04-27, 7:55 am

In article <1144782849.584115.72190@i40g2000cwc.googlegroups.com>,
"xoinki" <xoinki@gmail.com> wrote:

> hi all,
>
> after running this code in redhat 9 linux system, i am getting "bad
> network address" error.
> this program sends a datagram on port 89, to a system specified by IP
> address..(./a.out IP address) the datagram sent is not a standard OSPF
> packet, but it resembles ICMP structure..
> but this code runs if we are sending the datagram only once rather than
> every 3 seconds.


Send1() expects to receive the address to send to as an argument. This
happens when it's called from main(), but not when it's called later as
a signal handler. You should have gotten a compiler error telling you
that send1 is not the proper type argument for signal, because signal
expects void (*)(int) and you supplied void (*)(struct sockaddr_in*).

>
> please help..
>
> #include <fcntl.h>
> #include <errno.h>
> #include <sys/socket.h>
> #include <resolv.h>
> #include <netdb.h>
> #include <netinet/in.h>
> #include <netinet/ip_icmp.h>
> #include <sys/cdefs.h>
> #include <sys/types.h>
> #include<signal.h>
>
> #define PACKETSIZE 64
>
> void ping(struct sockaddr_in *addr);
>
> struct icmphdr1
> {
> u_int8_t type; /* message type */
> u_int8_t code; /* type sub-code */
> u_int16_t checksum;
> union
> {
> struct
> {
> u_int16_t id;
> u_int16_t sequence;
> } echo; /* echo datagram */
> u_int32_t gateway; /* gateway address */
> struct
> {
> u_int16_t __unused;
> u_int16_t mtu;
> } frag; /* path mtu discovery */
> } un;
> };
>
> struct packet
> {
> struct icmphdr1 hdr;
> char msg[PACKETSIZE-sizeof(struct icmphdr1)];
> };
>
> int pid=-1;
> struct protoent *proto=NULL;
>
> /*sending a packet every 10 seconds*/
>
> void send1(struct sockaddr_in *addr)
> {
> alarm(3);
> ping(addr);
> }
>
> /*--------------------------------------------------------------------*/
> /*--- checksum - standard 1s complement checksum
> ---*/
> /*--------------------------------------------------------------------*/
> unsigned short checksum(void *b, int len)
> { unsigned short *buf = b;
> unsigned int sum=0;
> unsigned short result;
>
> for ( sum = 0; len > 1; len -= 2 )
> sum += *buf++;
> if ( len == 1 )
> sum += *(unsigned char*)buf;
> sum = (sum >> 16) + (sum & 0xFFFF);
> sum += (sum >> 16);
> result = ~sum;
> return result;
> }
>
> /*--------------------------------------------------------------------*/
> /*--- display - present echo info
> ---*/
> /*--------------------------------------------------------------------*/
> void display(void *buf, int bytes)
> { int i;
> struct iphdr *ip = buf;
> struct icmphdr1 *icmp = buf+ip->ihl*4;
>
> printf("----------------\n");
> /* for ( i = 0; i < bytes; i++ )
> {
> if ( !(i & 15) ) printf("\n X: ", i);
> printf(" X ", ((unsigned char*)buf)[i]);
> }*/
> printf("\n");
> printf("IPv%d: hdr-size=%d pkt-size=%d protocol=%d TTL=%d src=%s ",
> ip->version, ip->ihl*4, ntohs(ip->tot_len), ip->protocol,
> ip->ttl, inet_ntoa(ip->saddr));
> printf("dst=%s\n", inet_ntoa(ip->daddr));
> if ( icmp->un.echo.id == pid )
> {
> printf("ICMP: type[%d/%d] checksum[%d] id[%d] seq[%d]\n",
> icmp->type, icmp->code, ntohs(icmp->checksum),
> icmp->un.echo.id, icmp->un.echo.sequence);
> }
> }
>
> /*--------------------------------------------------------------------*/
> /*--- listener - separate process to listen for and collect
> messages--*/
> /*--------------------------------------------------------------------*/
> void listener(void)
> { int sd;
> struct sockaddr_in addr;
> unsigned char buf[1024];
>
> sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
> if ( sd < 0 )
> {
> perror("socket");
> exit(0);
> }
> for (;;)
> { int bytes, len=sizeof(addr);
>
> bzero(buf, sizeof(buf));
> bytes = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&addr,
> &len);
> if ( bytes > 0 )
> {
> display(buf, bytes);
> printf("yess\n");
> }
> else
> perror("recvfrom");
> }
> exit(0);
> }
>
> /*--------------------------------------------------------------------*/
> /*--- ping - Create message and send it.
> ---*/
> /*--------------------------------------------------------------------*/
> void ping(struct sockaddr_in *addr)
> { const int val=255;
> int i, sd, cnt=1;
> struct packet pckt;
> struct sockaddr_in r_addr;
>
> sd = socket(PF_INET, SOCK_RAW, proto->p_proto);
> if ( sd < 0 )
> {
> perror("socket");
> return;
> }
> if ( setsockopt(sd, SOL_IP, IP_TTL, &val, sizeof(val)) != 0)
> perror("Set TTL option");
> if ( fcntl(sd, F_SETFL, O_NONBLOCK) != 0 )
> perror("Request nonblocking I/O");
> //for (;;)
> { int len=sizeof(r_addr);
>
> printf("Msg #%d\n", cnt);
> if ( recvfrom(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&r_addr,
> &len) > 0 )
> printf("***Got message!***\n");
> bzero(&pckt, sizeof(pckt));
> pckt.hdr.type = ICMP_ECHO;
> pckt.hdr.un.echo.id = pid;
> for ( i = 0; i < sizeof(pckt.msg)-1; i++ )
> pckt.msg[i] = i+'0';
> pckt.msg[i] = 0;
> pckt.hdr.un.echo.sequence = cnt++;
> pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
> if ( sendto(sd, &pckt, sizeof(pckt), 0, (struct sockaddr*)addr,
> sizeof(*addr)) <= 0 )
> perror("sendto");
> sleep(1);
> }
> }
>
> /*--------------------------------------------------------------------*/
> /*--- main - look up host and start ping processes.
> ---*/
> /*--------------------------------------------------------------------*/
> int main(int count, char *strings[])
> { struct hostent *hname;
> struct sockaddr_in addr;
>
> if ( count != 2 )
> {
> printf("usage: %s <addr>\n", strings[0]);
> exit(0);
> }
> if ( count > 1 )
> {
> pid = getpid();
> proto = getprotobyname("OSPFIGP");
> hname = gethostbyname(strings[1]);
> bzero(&addr, sizeof(addr));
> addr.sin_family = hname->h_addrtype;
> addr.sin_port = 0;
> addr.sin_addr.s_addr = *(long*)hname->h_addr;
> signal(SIGALRM,send1);
> if ( fork() == 0 )
> listener();
> else
> send1(&addr);
> wait(0);
> }
> else
> printf("usage: myping <hostname>\n");
> return 0;
> }
>
>
> thanking u in advance..
> xoinki


--
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 ***
xoinki

2006-04-27, 7:55 am

hi all,
thank you for the response sir,
now i understood my mistake..
can u suggest a solution for this?
waiting for u r reply..
xoinki

Barry Margolin

2006-04-27, 7:55 am

In article <1144842509.682438.252860@i39g2000cwa.googlegroups.com>,
"xoinki" <xoinki@gmail.com> wrote:

> hi all,
> thank you for the response sir,


Are you referring to me? Please include context; see
<http://cfaj.freeshell.org/google/>

> now i understood my mistake..
> can u suggest a solution for this?


Put the address in a global variable rather than passing it as a
parameter to send1().

--
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 ***
manu

2006-04-27, 7:55 am

thnx sir.. its workin..
now i am identifyin the recieved packets.. to check the type of
datagram sent..
thank u again..
Xoinki

Barry Margolin

2006-04-27, 7:55 am

In article <1144946440.082715.97450@i39g2000cwa.googlegroups.com>,
"manu" <manoharyes@gmail.com> wrote:

> thnx sir.. its workin..


Are you replying to me again? Did you read the Usenet etiquette article
I referred you to?

--
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 ***
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com