Esempio n. 1
0
/* print_dg - print an ICMP datagram */
static void print_dg( char *dg, int len )
{
	struct ip *ip;
	struct icmp *icmp;
	struct hostent *hp;
	char *hname;
	int hl;
	static char *redirect_code[] =
	{
		"network", "host",
		"type-of-service and network", "type-of-service and host"
	};
	static char *timexceed_code[] =
	{
		"transit", "reassembly"
	};
	static char *param_code[] =
	{
		"IP header bad", "Required option missing"
	};

	ip = ( struct ip * )dg;
	if ( ip->ip_v != 4 )
	{
		error( 0, 0, "IP datagram not version 4\n" );
		return;
	}
	hl = ip->ip_hl << 2;		/* IP header length in bytes */
	if ( len < hl + ICMP_MINLEN )
	{
		error( 0, 0, "short datagram (%d bytes) from %s\n",
			len, inet_ntoa( ip->ip_src ) );
		return;
	}
	hp = gethostbyaddr( ( char * )&ip->ip_src, 4, AF_INET );
	if ( hp == NULL )
		hname = "";
	else
		hname = hp->h_name;
	icmp = ( struct icmp * )( dg + hl );  /* ICMP packet */
	printf( "ICMP %s (%d) from %s (%s)\n",
		get_type( icmp->icmp_type ),
		icmp->icmp_type, hname, inet_ntoa( ip->ip_src ) );
	if ( icmp->icmp_type == ICMP_UNREACH )
		print_unreachable( icmp );
	else if ( icmp->icmp_type == ICMP_REDIRECT )
		printf( "\tRedirect for %s\n", icmp->icmp_code <= 3 ?
			redirect_code[ icmp->icmp_code ] : "Illegal code" );
	else if ( icmp->icmp_type == ICMP_TIMXCEED )
		printf( "\tTTL == 0 during %s\n", icmp->icmp_code <= 1 ?
			timexceed_code[ icmp->icmp_code ] : "Illegal code" );
/*@.bp*/
	else if ( icmp->icmp_type == ICMP_PARAMPROB )
		printf( "\t%s\n", icmp->icmp_code <= 1 ?
			param_code[ icmp->icmp_code ] : "Illegal code" );
}
Esempio n. 2
0
void print_dg(char* dg, int len)
{
    struct ip *ip;
    struct icmp *icmp;
    struct hostent *hp;
    char *hname;
    int hl;
    char ip_src[16];
    //struct sockaddr_in src_addr;    
    //memset(&src_addr, 0, sizeof(struct sockaddr_in));
    
    static char *redirect_code[] = 
    {
        "netowrk", "host", 
        "type-of-service and network", 
        "type-of-service and host"
    };
    static char *timexceed_code[] =
    {
        "transit", "reassembly"
    };
    static char *param_code[] = 
    {
        "IP header bad", "Required option missing"
    };
    
    ip = (struct ip*)dg;
    if (ip->ip_v != 4){
        printf("IP datagram not version 4.\n");
        return;
    }
    hl = ip->ip_hl << 2; //IP header length in bytes
    
    //printf("ip->ip_src %d\n", ip->ip_src);
    inet_ntop(AF_INET, &ip->ip_src, ip_src, sizeof(ip_src));
    //printf("ip_src %s\n", ip_src);

    if (len < hl + ICMP_MINLEN){
        printf("short datagram (%d bytes) from %s.\n", len, ip_src);
        return;
    }
    // hp = gethostbyaddr((char*)&ip->ip_src, 4, AF_INET);
    // if (hp == NULL){
        // hname = "";
    // }
    // else {
        // hname = hp->h_name;
    // }
    icmp = (struct icmp*)(dg + hl); //ICMP packet
    printf("ICMP %s (icmp_type:%d) from %s\n", get_type(icmp->icmp_type), icmp->icmp_type, ip_src);

    if (icmp->icmp_type == ICMP_UNREACH){
        print_unreachable(icmp, ip_src);
    }
    else if (icmp->icmp_type == ICMP_REDIRECT){
        printf("\tRedirect for %s\n", icmp->icmp_code <= 3 \
            ? redirect_code[icmp->icmp_code] : "Illegal code");
    }
    else if (icmp->icmp_type == ICMP_TIMXCEED){
        printf("\tTTL == 0 during %s\n", icmp->icmp_code <= 1 \
            ? timexceed_code[icmp->icmp_code] : "Illegal code");
    }
    else if (icmp->icmp_type == ICMP_PARAMPROB){
        printf("\t%s\n", icmp->icmp_code <= 1 \
            ? param_code[icmp->icmp_code] : "Illegal code");
    }
}