void icmp_in(struct pkbuf *pkb) { struct ip *iphdr = pkb2ip(pkb); struct icmp *icmphdr = ip2icmp(iphdr); int icmplen, type; /* sanity check */ icmplen = ipdlen(iphdr); icmpdbg("%d bytes", icmplen); if (icmplen < ICMP_HRD_SZ) { icmpdbg("icmp header is too small"); goto drop_pkb; } if (icmp_chksum((unsigned short *)icmphdr, icmplen) != 0) { icmpdbg("icmp checksum is error"); goto drop_pkb; } type = icmphdr->icmp_type; if (type > ICMP_T_MAXNUM) { icmpdbg("unknown icmp type %d code %d", type, icmphdr->icmp_code); goto drop_pkb; } /* handle icmp type */ icmp_table[type].handler(&icmp_table[type], pkb); return; drop_pkb: free_pkb(pkb); }
static void recv_packet(void) { struct pkbuf *pkb; struct ip *iphdr; struct icmp *icmphdr; while (!finite || recv > 0) { pkb = _recv(sock); if (!pkb) break; iphdr = pkb2ip(pkb); icmphdr = ip2icmp(iphdr); if (iphdr->ip_pro == IP_P_ICMP && _ntohs(icmphdr->icmp_id) == id && icmphdr->icmp_type == ICMP_T_ECHORLY) { recv--; printf("%d bytes from " IPFMT ": icmp_seq=%d ttl=%d\n", ipdlen(iphdr), ipfmt(iphdr->ip_src), _ntohs(icmphdr->icmp_seq), iphdr->ip_ttl); precv++; } free_pkb(pkb); } }