⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipretperro.c

📁 记录IP/TCP/UDP/ICMP网络包日志
💻 C
📖 第 1 页 / 共 3 页
字号:
                                  break;                          case 3: printf( "Redundant Checksum Avoidance.\n");                                  break;                  }                  break;         case 15: i = 2;                  while( i < opt_len) {                         printf( "%-30.30sData byte #%d : %-5d (0x%x)\n", " ", i - 2, ( PERRO_U8) buf[ i], ( PERRO_U8) buf[ i]);                         i++;                  }                  break; } if( tcpol > opt_len) {     printf( "TCP Options padding bytes   :\n");     print_ala_hexdump( buf + opt_len, tcpol - opt_len); }}void print_header_udp( struct perro_udphdr udph){ printf( "******* UDP Header *******\n"); printf( "Source port address (From)  : %d/%s\n"      , htons( udph.source), get_serv_name( udph.source, "udp")); printf( "Destination port address    : %d/%s\n"      , htons( udph.dest  ), get_serv_name( udph.dest  , "udp")); printf( "UDP Length                  : %-5d (0x%x)\n", htons( udph.len   ), htons( udph.len)           ); printf( "UDP Checksum                : %-5d (0x%x)\n", htons( udph.check ), htons( udph.check)         );}void print_header_icmp( struct perro_icmphdr ich){ struct in_addr addrs; char *type; char *code; char temp[ 1024]; int show_id     = 0;  /* Show ICMP id field       */ int show_seq    = 0;  /* Show ICMP sequence field */ int show_gat    = 0;  /* Show ICMP gateway field  */ int show_poi    = 0;  /* Show ICMP pointer field  */ int show_res    = 0;  /* Show ICMP reserved field */ int show_tr     = 0;  /* Used by traceroute ICMP  */ int show_routad = 0;  /* Show router advertisement fields    */ PERRO_U32 unused;     /* Used when a field is unused         */ PERRO_U16 routad;     /* Used to print router advert. fields */ flag_printiph = 0;  printf( "******* ICMP Header *******\n"); sprintf( temp, "%-5d", ich.code); code = temp; switch( ich.type) {         case  1:         case  2:         case  7: type = "Unassigned";                  break;         case  0: type = "Echo Reply";                  show_id  = 1;                  show_seq = 1;                  break;         case  3: type = "Destination Unreachable";                  switch( ich.code) {                          case  0: code = "Network Unreachable"             ; break;                          case  1: code = "Host Unreachable"                ; break;                          case  2: code = "Protocol Unreachable"            ; break;                          case  3: code = "Port Unreachable"                ; break;                          case  4: code = "Fragmentation needed and DF set" ; break;                          case  5: code = "Source Route Failed"             ; break;                          case  6: code = "Destination Network Unknown"     ; break;                          case  7: code = "Destination Host Unknown"        ; break;                          case  8: code = "Source Host Isolated"            ; break;                          case  9: code = "Communication with Destination Network is Administratively Prohibited"; break;                          case 10: code = "Communication with Destination Host is Administratively Prohibited"   ; break;                          case 11: code = "Destination Network Unreachable for Type of Service"       ; break;                          case 12: code = "Destination Host Unreachable for Type of Service"          ; break;                          case 13: code = "Communication Administratively Prohibited, Packet filtered"; break;                          case 14: code = "Host Precedence Violation"  ; break;                          case 15: code = "Precedence cutoff in effect"; break;                          default: code = "Dest Unreachable, Bad Code"; break;                  }                  flag_printiph = 1;                  break;         case  4: type = "Source Quench";                  flag_printiph = 1;                  break;         case  5: type = "Redirect (change route)";                  switch( ich.code) {                          case  0: code = "Redirect Network"                    ; break;                          case  1: code = "Redirect Host"                       ; break;                          case  2: code = "Redirect Type of Service and Network"; break;                          case  3: code = "Redirect Type of Service and Host"   ; break;                          default: code = "Redirect, Bad Code"                  ; break;                  }                  sprintf( temp ,"%s (New addr: 0x%08lx)\n", code, ich.un.gateway);                  code = temp;                  flag_printiph = 1;                  show_gat = 1;                  break;         case  6: type = "Alternate Host Address";                  switch( ich.code) {                          case  0: code = "Alternate Address for Host"; break;                          default: code = "Alt. Host. Addr., Bad Code"; break;                  }                  break;         case  8: type = "Echo Request";                  show_id  = 1;                  show_seq = 1;                  break;         /* See RFC 1256 */         case  9: type = "ICMP Router Advertisement Message";                  show_routad = 1;                  break;         case 10: type = "ICMP Router Solicitation Message";                  show_res = 1;                  break;         case 11: type = "Time Exceeded";                  switch( ich.code) {                          case  0: code = "Time to live exceeded in transit" ; break;                          case  1: code = "Fragment reassembly time exceeded"; break;                          default: code = "Time exceeded, Bad Code"          ; break;                  }                  flag_printiph = 1;                  break;         case 12: type = "Parameter problem";                  switch( ich.code) {                          case  0: code = "Pointer indicates the error"; break;                          case  1: code = "Missing a Required Option"  ; break;                          case  2: code = "Bad Length"                 ; break;                          default: code = "Par. problem, Bad Code"     ; break;                  }                  flag_printiph = 1;                  show_poi      = 1;                  break;         case 13: type = "Timestamp Request";                  show_id  = 1;                  show_seq = 1;                  break;         case 14: type = "Timestamp Reply";                  show_id  = 1;                  show_seq = 1;                  break;         case 15: type = "Information Request";                  show_id  = 1;                  show_seq = 1;                  break;         case 16: type = "Information Reply";                  show_id  = 1;                  show_seq = 1;                  break;         case 17: type = "Address Mask Request";                  show_id  = 1;                  show_seq = 1;                  break;         case 18: type = "Address Mask Reply";                  show_id  = 1;                  show_seq = 1;                  break;         /* See RFC 1393 */         case 30: type = "Traceroute";                  switch( ich.code) {                          case  0: code = "Outbound Packet successfully forwarded"        ; break;                          case  1: code = "No route for Outbound Packet; packet discarded"; break;                          default: code = "Traceroute, Bad Code"                          ; break;                  }                  show_id  = 1; /* show id but not seq     */                  show_tr  = 1; /* user sequence as unused */                  break;         /* See RFC 1475 */         case 31: type = "TP/IX: Conversion Failed";                  switch( ich.code) {                          case  0: code = "Unknown/unspecified error"                 ; break;                          case  1: code = "Don't Convert option present"              ; break;                          case  2: code = "Unknown mandatory option present"          ; break;                          case  3: code = "Known unsupported option present"          ; break;                          case  4: code = "Unsupported transport protocol"            ; break;                          case  5: code = "Overall length exceeded"                   ; break;                          case  6: code = "IP header length exceeded"                 ; break;                          case  7: code = "Transport protocol > 255"                  ; break;                          case  8: code = "Port conversion out of range"              ; break;                          case  9: code = "Transport header length exceeded"          ; break;                          case 10: code = "32 Bit Rollover missing and ACK set"       ; break;                          case 11: code = "Unknown mandatory transport option present"; break;                          default: code = "Conversion Failed, Bad Code"               ; break;                  }                  break;         /* See RFC 1700*/         case 32: type = "Mobile Host Redirect";                  break;         case 33: type = "IPv6 Where-Are-You";                  break;         case 34: type = "IPv6 I-Am-Here";                  break;         case 35: type = "Mobile Registration Request";                  break;         case 36: type = "Mobile Registration Reply";                  break;         /* See RFC 1788 */         case 37: type = "Domain Name Request";                  show_id  = 1;                  show_seq = 1;                  break;         case 38: type = "Domain Name Reply";                  show_id  = 1;                  show_seq = 1;                  break;         default: type = "Reserved";                  break;    } /* end switch( ich.type) */ printf( "Type                        : %s (%d)\n"    , type, ich.type); printf( "Code                        : %s (%d)\n"    , code, ich.code); printf( "CheckSum                    : %-5d (0x%x)\n", htons( ich.checksum), htons( ich.checksum)); if( show_gat) {     addrs.s_addr = ich.un.gateway;     printf( "Gateway                     : %s (%s)\n", inet_ntoa( addrs), resolve_host_name( ich.un.gateway)); } if( show_poi)     printf( "Pointer (error octect)      : %-5d (0x%x)\n", ich.un.pointer, ich.un.pointer); if( show_id)     printf( "Identification              : %-5d (0x%x)\n", htons( ich.un.echo.id), htons( ich.un.echo.id)); if( show_seq)     printf( "Sequence                    : %-5d (0x%x)\n", htons( ich.un.echo.sequence), htons( ich.un.echo.sequence)); /* Used by ICMP Traceroute message,    use field un.echo.sequence as unused,    see RFC 1393 */ if( show_tr)      printf( "Unused                      : %-5d (0x%x)\n", htons( ich.un.echo.sequence), htons( ich.un.echo.sequence)); if( show_res)     printf( "Reserved                    : %ld (0x%lx)\n", htonl( ich.un.reserved), htonl( ich.un.reserved)); if( show_routad) {     routad = ( PERRO_U16) ( htonl( ich.un.routad) & 0xFF000000) >> 24;     printf( "Number of address           : %-5d (0x%x)\n", routad, routad);     routad = ( PERRO_U16) ( htonl( ich.un.routad) & 0x00FF0000) >> 16;     printf( "Address entry size          : %-5d (0x%x)\n", routad, routad);     routad = ( PERRO_U16) ( htonl( ich.un.routad) & 0x0000FFFF);     printf( "Lifetime                    : %-5d (0x%x)\n", routad, routad); } if( show_seq == 0 &&      show_id  == 0 &&     show_gat == 0 &&     show_res == 0 &&     show_tr  == 0 &&     show_routad == 0     ) {     if( show_poi == 0)         unused = htonl( ich.un.unused);     else /* If pointer exists then clear it */         unused = htonl( ich.un.unused) & 0x00FFFFFF;     printf( "Unused field                : %ld (0x%lx)\n", unused, unused); }}void print_data( char *buf, char *pkttype, int size){ if( flag_printiph && flag_verbose) /* Print the IP header again */     print_ipret_icmp_printiph_again( buf); else {     printf( "******* %s Data *******\n", pkttype);     print_ala_hexdump( buf, size); }}void print_ala_hexdump( char *buf, int size){ int hsize = 0; /* Hex size */ int pos   = 0;  for( pos = 0; pos < size; pos += 16) {        printf( "%07X  ", pos);        hsize = (size - pos) > 16 ? 16: size - pos;        print_hex(   buf + pos, hsize);        print_ascii( buf + pos, hsize);        printf( "\n"); }}void print_hex( char *buf, int size){ int j = 0, sp = 1; while( j < size) {        printf( "%02X", (unsigned char) buf[ j]);        j++;        if( sp == 4) {             printf( "  ");             sp = 1;        } else {             printf( " ");             sp++;        } } while( j++ < 16) {        printf( "  ");        if( sp == 4) {             printf( "  ");             sp = 1;        } else {             printf( " ");             sp++;        } }}void print_ascii( char *buf, int size){ int i; for( i = 0; i < size; i++)      printf( "%c", buf[ i] > 31 && buf[ i] < 127 ? buf[ i] : '.'); while( i++ < 16)        printf( " ");}/* * Print the ip header with the data of an ICMP message + 64 bits of data, * some ICMP messages include the original IP header + 64 bits of * the data of the original datagram. */void print_ipret_icmp_printiph_again( char *buf){ struct perro_iphdr iph; /* IP header */ int iphlen; flag_printiph = 0; memcpy( &iph, buf, sizeof( iph)); printf( "******* ICMP data: IP header of the original datagram: *******\n"); print_header_ip( iph);       /* Get IP options size */ iphlen  = ( iph.hlv & 0x0F) << 2; iphlen -= 20; if( iphlen > 0)     print_header_ipoptions( buf + sizeof( iph), iphlen); printf( "******* 64 bits of the original datagram data: *******\n"); print_ala_hexdump( buf + sizeof( iph) + iphlen, 8); printf( "******* End of interpreted ICMP data *******\n");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -