コード例 #1
0
ファイル: xsh_ping.c プロジェクト: BridgeNY/purdue
/*------------------------------------------------------------------------
 * xsh_ping - shell command to ping a remote host
 *------------------------------------------------------------------------
 */
shellcmd xsh_ping(int nargs, char *args[])
{
	uint32	ipaddr;			/* IP address in binary		*/
	int32	retval;			/* return value			*/
	int32	icmpid;			/* ICMP ID to use		*/
	static	int32	seq = 0;	/* sequence number		*/
	char	buf[56];		/* buffer of chars		*/
	int32	i;			/* index into buffer		*/
	int32	nextval;		/* next value to use		*/

	/* For argument '--help', emit help about the 'ping' command	*/

	if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) {
		printf("Use: %s  address\n\n", args[0]);
		printf("Description:\n");
		printf("\tUse ICMP Echo to ping a remote host\n");
		printf("Options:\n");
		printf("\t--help\t display this help and exit\n");
		printf("\taddress\t an IP address in dotted decimal\n");
		return 0;
	}

	/* Check for valid number of arguments */

	if (nargs != 2) {
		fprintf(stderr, "%s: invalid arguments\n", args[0]);
		fprintf(stderr, "Try '%s --help' for more information\n",
				args[0]);
		return 1;
	}

	/* convert argument to binary */

	retval = dot2ip(args[1], &ipaddr);
	if ((int32)retval == SYSERR) {
		fprintf(stderr, "%s: invalid IP address\n", args[0]);
		return 1;
	}

	/* Register to receive an ICMP Echo Reply */

	icmpid = icmp_register(ipaddr);
	if (icmpid == SYSERR) {
		fprintf(stderr, "%s: ICMP registration fails\n", args[0]);
		return 1;
	}

	/* Fill the buffer with values - start with low-order byte of	*/
	/*	the sequence number and increment			*/

	nextval = seq;
	for (i = 0; i<sizeof(buf); i++) {
		buf[i] = 0xff & nextval++;
	}

	/* Send an ICMP Echo Request */

	retval = icmp_send(ipaddr, ICMP_ECHOREQST, icmpid, seq++, buf,
				sizeof(buf));
	if (retval == SYSERR) {
		fprintf(stderr, "%s: cannot send a ping\n", args[0]);
		icmp_release(icmpid);
		return 1;
	}

	/* Read a reply */

	retval = icmp_recv(icmpid, buf, sizeof(buf), 3000);
	icmp_release(icmpid);
	if (retval == TIMEOUT) {
		fprintf(stderr, "%s: no response from %s\n\n", args[0],
					args[1]);
		return 1;
	}
	fprintf(stderr, "host %s is alive\n", args[1]);
	return 0;
}
コード例 #2
0
ファイル: ping.c プロジェクト: suborb/zsock
void Ping()
{
    char	buffer[80];
    char	buffer2[20];
    u16_t	i;
    u16_t	sequence;
    u32_t	ident;
    u32_t	timeout;
    ipaddr_t  addr;
    
    printf("Enter hostname/IP to ping\n");

    fgets_cons(buffer,79);
    if ( (addr=resolve_i(buffer) ) == 0L ) {
	printf("\nUnknown host\n");
	return;
    }

    

    memset(pingstat,0,sizeof(pingstat));
    
    ident = sequence = 0;
    received = pingav = pingmin = pingmax = 0;
    
    if ( icmp_register(ping_handler) == NULL ) {
	printf("\nCan't register handler..\n");
	return;
    }
    
    printf("\nPING %s (%s): 56 data bytes\n",buffer,inet_ntoa_i(addr,buffer2));


    timeout = set_timeout(50);
    for (;;) {
	if (  chk_timeout(timeout) ) {
	    timeout = set_timeout(100);
	    if ( ping_send(addr,&ident) ) 
		ident++;
	    else
		break;
	} 
#ifdef __Z88__
	if (getk() == 27 ) break;
#else
	if ( ident == 4 )
	    break;
#endif
	BUSYINT();
   }


    icmp_deregister();
    /* Just to test the resolver.... */
    if (reverse_addr_lookup_i(addr,buffer) == 0 )
	inet_ntoa_i(addr,buffer);
    printf("\n--- %s ping statistics ---\n",buffer);
    printf("%lu packets transmitted, %d packets received, %d%% packet loss\n",ident,received, 100-((received*100)/ident)  );
    printf("round-trip time min/avg/max= %d/%d/%d ms\n",pingmin,(pingav*10)/received,pingmax);
    return;
}