Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
* [asking] recvfrom function is not blocking.. please help me..
@ 2002-07-21 14:38 Gijung Kim
  2002-07-21 20:53 ` Ivan Skytte Jørgensen
  0 siblings, 1 reply; 2+ messages in thread
From: Gijung Kim @ 2002-07-21 14:38 UTC (permalink / raw)


Hi all,
I'm making ping program but the following source works well on linux
but not compaq true64.
the problem is that the recvfrom function is not blocking.
Please see the following source and tell me the problem and solution
if anyone know.
Please help me asap.

#include <netinet/ip_icmp.h>
#include <netinet/ip.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#define BUFSIZE 1024

int nsent = 1;
pid_t pid;
int sockfd;
char tmp[16];
char sbuf[BUFSIZE], rbuf[BUFSIZE];
struct sockaddr *sendaddr, *recvaddr; 
struct hostent *he;

void send_echo(void);
void recv_echo(void);
unsigned short cksum(unsigned short *, int);
void print_result();
void sig_alarm(int);
	


int main(int argc, char *argv[])
{
	
	struct sockaddr_in saddr;
	pid = getpid();
	
	if (signal(SIGALRM, sig_alarm) == SIG_ERR)
		perror("Can't catch SIGALRM");

       	if (argc != 2) 
		perror("Usage: ping <IP Adress>");
	
	
	if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) {
		perror("socket error");
		exit(1);
	}


	he = gethostbyname(argv[1]);
	printf("PING %s (%s) : 56 data bytes\n", argv[1],
		       inet_ntop(he->h_addrtype, *(he->h_addr_list), tmp, 16));

	bzero(&saddr, sizeof(struct sockaddr_in));
	saddr.sin_family = AF_INET;
	inet_pton(AF_INET, tmp, &saddr.sin_addr);
	
	sendaddr = (struct sockaddr *)&saddr;		
	recv_echo();
	
}

void sig_alarm(int signo)
{
	send_echo();
	nsent++;
	alarm(1);
	return;
}

void send_echo(void)
{
	struct icmp *icmp;
	int pktlen = 8 + 56;
	int i;

	icmp = (struct icmp *)sbuf;
	icmp->icmp_type = ICMP_ECHO;
	icmp->icmp_code = 0;
	icmp->icmp_id = pid;
	icmp->icmp_seq = nsent;
	gettimeofday((struct timeval *)icmp->icmp_data, NULL);

	icmp->icmp_cksum = 0;
	icmp->icmp_cksum = cksum((unsigned short *) icmp, pktlen);

	sendto(sockfd, sbuf, pktlen, 0, sendaddr, sizeof(struct sockaddr));

}
	
void recv_echo(void)
{
	struct timeval *rtime;
	unsigned int pktlen;
	
	sig_alarm(SIGALRM);
	while(1) {
////////////////////////////////////////////////////////////////////////
// it doesnt block                                                    
    //
////////////////////////////////////////////////////////////////////////
		pktlen = recvfrom(sockfd, rbuf, sizeof(rbuf), 0, 
				recvaddr, (socklen_t *)sizeof(struct sockaddr));
		if (pktlen < 0) 
			perror("recvfrom error");

		gettimeofday(rtime, NULL);
		print_result(rbuf, pktlen, rtime);
	}

}

void print_result(char *pkt, size_t pktlen, struct timeval *rtime)
{
	int iplen, icmplen;
	float rtt;
	struct ip *ip;
	struct icmp *icmp;
	struct timeval *stime;
	
	ip = (struct ip *)pkt;
	iplen = ip->ip_hl << 2;

	icmp = (struct icmp *)(pkt + iplen);
	icmplen = pktlen - iplen;
	if (icmp->icmp_type == ICMP_ECHOREPLY) {
		stime = (struct timeval *)icmp->icmp_data;

		if ((rtime->tv_usec -= stime->tv_usec) < 0) {
			--rtime->tv_sec;
			rtime->tv_usec += 1000000;
		}
		
		rtime->tv_sec -= stime->tv_sec;
		rtt = rtime->tv_sec * 1000.0 + rtime->tv_usec / 1000.0;
		printf("%d bytes from %s : seq=%u, ttl=%d, rtt=%.3f ms\n",
			icmplen, inet_ntop(he->h_addrtype, *(he->h_addr_list), tmp, 16),
		       	icmp->icmp_seq, ip->ip_ttl, rtt);
	}
	else  
		printf("It's not a ICMP_ECHOREPLY packet\n");

}

 


unsigned short cksum(unsigned short *addr, int pktlen)
{
	int nleft = pktlen;
	int sum = 0;
	unsigned short *w = addr;
	unsigned short answer = 0;

	while (nleft > 1) {
		sum += *w++;
		nleft -= 2;
	}

	if (nleft == 1) {
		*(unsigned char *)(&answer) = *(unsigned char *)w;
		sum += answer;
	}

	sum = (sum >> 16) + (sum & 0xffff);
	sum += (sum >> 16);
	answer = ~sum;
	return (answer);
}


Result
1.
O/S: OSF1 websvr.websvr V5.1 1885 alpha

-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
-1 bytes from 218.144.64.21 : seq=0, ttl=0, rtt=1027259498496.000 ms
                                :
                                :

2.
O/S: Linux eos.eosoft.co.kr 2.4.13-1hl #1 2001. 11. 04. (&#51068;)
04:04:58 KST i686 unknown

[root@eos Ping]# ./ping yahoo.com
PING yahoo.com (66.218.71.198) : 56 data bytes
64 bytes from 66.218.71.198 : seq=1, ttl=245, rtt=197.549 ms
64 bytes from 66.218.71.198 : seq=2, ttl=245, rtt=196.446 ms
64 bytes from 66.218.71.198 : seq=3, ttl=245, rtt=197.567 ms
64 bytes from 66.218.71.198 : seq=4, ttl=245, rtt=196.427 ms
64 bytes from 66.218.71.198 : seq=5, ttl=245, rtt=195.914 ms
64 bytes from 66.218.71.198 : seq=6, ttl=245, rtt=196.885 ms


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-07-21 20:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-07-21 14:38 [asking] recvfrom function is not blocking.. please help me Gijung Kim
2002-07-21 20:53 ` Ivan Skytte Jørgensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox