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

📄 utils.c

📁 altera epxa1的例子程序
💻 C
📖 第 1 页 / 共 2 页
字号:
*/
            break;
    }
}


// EnumType
typedef struct
{
    int   id;
    char  ids[0x40];
} EnumType;

char * xgetstr(EnumType *str, int value);
char * xgetstr(EnumType *str, int value)
{
    int i;
    for (i=1; i<=str[0].id; i++)
    {
        if (str[i].id == value)
        {
            return str[i].ids;
        }
    }
    return str[0].ids;
}


EnumType arp_op[] =
{
    { 0x2,      "UNKNOWN_ARP_OPERATION" },
    { 0x0001,   "ARP_REQUEST "   },
    { 0x0002,   "ARP_RESPONSE"  },
};

void decode_arp(arp_type *pktp)
{
    printf("ARP HEADER\n");
    printf("hardware     : %04x",bitrev16(((USHORT *)pktp->hardware_type)[0]));
    printf("                 protocol     : %04x\n",bitrev16(((USHORT *)pktp->protocol_type)[0]));
    printf("hlen         : %02x",pktp->hlen);
    printf("                   plen         : %02x\n",pktp->plen);
    printf("operation    : %04x %s\n",bitrev16(((USHORT *)pktp->operation)[0]), xgetstr(arp_op,bitrev16(((USHORT *)pktp->operation)[0])));
    printf("sender ha    : %02x:%02x:%02x:%02x:%02x:%02x",pktp->sender_ha[0],pktp->sender_ha[1],pktp->sender_ha[2],pktp->sender_ha[3],pktp->sender_ha[4],pktp->sender_ha[5]);
    printf("    sender ip    : %d.%d.%d.%d\n",pktp->sender_ip[0],pktp->sender_ip[1],pktp->sender_ip[2],pktp->sender_ip[3]);
    printf("target ha    : %02x:%02x:%02x:%02x:%02x:%02x",pktp->target_ha[0],pktp->target_ha[1],pktp->target_ha[2],pktp->target_ha[3],pktp->target_ha[4],pktp->target_ha[5]);
    printf("    target ip    : %d.%d.%d.%d\n",pktp->target_ip[0],pktp->target_ip[1],pktp->target_ip[2],pktp->target_ip[3]);
}

EnumType ip_prot[] =
{
    { 0xb,      "??? " },
    { 1,        "ICMP" },
    { 2,        "IGMP" },
    { 3,        "GGP " },
    { 6,        "TCP " },
    { 8,        "EGP " },
    { 12,       "PUP " },
    { 17,       "UDP " },
    { 20,       "NMP " },
    { 22,       "XNS " },
    { 27,       "RDP " },
    { 66,       "RVD " },
};
void decode_ip(ip_type *pktp)
{
    printf("IP HEADER\n");
    printf("vers         : %x",(pktp->vers_hlen >> 4) & 0x0f);
    printf("             hlen        : %x",pktp->vers_hlen & 0x0f);
    printf("              type       : %02x\n",pktp->service_type);
    printf("length       : %04x",bitrev16(((USHORT *)pktp->len)[0]));
    printf("          id          : %04x",bitrev16(((USHORT *)pktp->identification)[0]));
    printf("           foffset    : %04x\n",bitrev16(((USHORT *)pktp->flags_frag_offset)[0]));
    printf("time to live : %02x",pktp->time_to_live);
    printf("            protocol    : %02x %s",pktp->protocol, xgetstr(ip_prot,pktp->protocol));
    printf("        checksum   : %04x\n",bitrev16(((USHORT *)pktp->header_checksum)[0]));
    printf("source ip    : %d.%d.%d.%d",pktp->src_ip_addr[0],pktp->src_ip_addr[1],pktp->src_ip_addr[2],pktp->src_ip_addr[3]);
    printf("  dest.  ip   : %d.%d.%d.%d\n",pktp->dst_ip_addr[0],pktp->dst_ip_addr[1],pktp->dst_ip_addr[2],pktp->dst_ip_addr[3]);
}

EnumType icmp_htype[] =
{
    { 0x2,      "UNKNOWN_ICMP_TYPE" },
    { 0,        "ICMP_ECHO_RESPONSE" },
    { 8,        "ICMP_ECHO_REQUEST" },
};
void decode_icmp(icmp_type *pktp)
{
    printf("ICMP HEADER\n");
    printf("type         : %02x %s\n",pktp->type, xgetstr(icmp_htype, pktp->type));
    printf("code         : %02x",pktp->code);
    printf("            checksum    : %04x\n",bitrev16(((USHORT *)pktp->checksum)[0]));
    printf("identifier   : %04x",bitrev16(((USHORT *)pktp->identifier)[0]));
    printf("          sequence    : %04x\n",bitrev16(((USHORT *)pktp->sequence)[0]));
}

EnumType tcp_port[] =
{
    { 47,       "UNKNOWN    " },
    { 7,        "ECHO       " },
    { 9,        "DISCARD    " },
    { 11,       "SYSTAT     " },
    { 13,       "DAYTIME    " },
    { 15,       "NETSTAT    " },
    { 17,       "QOTD       " },
    { 19,       "CHARGEN    " },
    { 20,       "FTPDATA    " },
    { 21,       "FTP        " },
    { 23,       "TELNET     " },
    { 25,       "SMTP       " },
    { 37,       "TIME       " },
    { 42,       "NAME       " },
    { 43,       "WHOIS      " },
    { 53,       "DOMAIN     " },
    { 57,       "MTP        " },
    { 77,       "RJE        " },
    { 79,       "FINGER     " },
    { 80,       "HTTP       " },
    { 87,       "LINK       " },
    { 95,       "SUPDUP     " },
    {101,       "HOSTNAMES  " },
    {102,       "ISO-TSAP   " },
    {103,       "X400       " },
    {104,       "X400-SND   " },
    {105,       "CSNET-NS   " },
    {109,       "POP        " },
    {110,       "POP3       " },
    {111,       "PORTMAP    " },
    {113,       "AUTH       " },
    {115,       "SFTP       " },
    {117,       "PATH       " },
    {119,       "NNTP       " },
    {139,       "NBSESSION  " },
    {144,       "NEWS       " },
    {158,       "TCPREPO    " },
    {170,       "PRINT-SRV  " },
    {175,       "VMNET      " },
    {400,       "VMNET0     " },
    {512,       "EXEC       " },
    {513,       "LOGIN      " },
    {514,       "SHELL      " },
    {515,       "PRINTER    " },
    {520,       "EFS        " },
    {526,       "TEMP0      " },
    {5900,      "VNC        " },
};
EnumType tcp_flag[] =
{
    { 0x6,      "???" },
    { 0x01,     "FIN" },
    { 0x02,     "SYN" },
    { 0x04,     "RST" },
    { 0x08,     "PSH" },
    { 0x10,     "ACK" },
    { 0x20,     "URG" },
};
void decode_tcp(tcp_type *pktp)
{
    int i;
    printf("TCP HEADER\n");
    printf("src port     : %04x (%04d) %s",bitrev16(((USHORT *)pktp->src_port)[0]),bitrev16(((USHORT *)pktp->src_port)[0]),xgetstr(tcp_port,bitrev16(((USHORT *)pktp->src_port)[0])));
    printf("     dest. port  : %04x (%04d) %s\n",bitrev16(((USHORT *)pktp->dst_port)[0]),bitrev16(((USHORT *)pktp->dst_port)[0]),xgetstr(tcp_port,bitrev16(((USHORT *)pktp->dst_port)[0])));
    printf("sequence     : %08lx",bitrev32(((ULONG *)pktp->sequence)[0]));
    printf("      acknowledge : %08lx\n",bitrev32(((ULONG *)pktp->acknowledgment)[0]));
    printf("offset       : %x",(pktp->data_offset >> 4) & 0x0f);
    printf("             flags       : %02x",pktp->flags);
    for (i=5; i>0; i--)
        if (pktp->flags & (1<<i))
            printf(" %s",xgetstr(tcp_flag,(1<<i)));
    printf("\n");
    printf("window       : %04x",bitrev16(((USHORT *)pktp->window)[0]));
    printf("          checksum    : %04x",bitrev16(((USHORT *)pktp->checksum)[0]));
    printf("           urgent ptr : %04x\n",bitrev16(((USHORT *)pktp->urgentp)[0]));
}
EnumType udp_port[] =
{
    { 27,       "UNKNOWN    " },
    { 7,        "ECHO       " },
    { 9,        "DISCARD    " },
    { 13,       "DAYTIME    " },
    { 17,       "QOTD       " },
    { 19,       "CHARGEN    " },
    { 37,       "TIME       " },
    { 39,       "RLP        " },
    { 42,       "NAME       " },
    { 53,       "DOMAIN     " },
    { 67,       "BOOTP      " },
    { 69,       "TFTP       " },
    {111,       "PORTMAP    " },
    {123,       "NTP        " },
    {137,       "NBNAME     " },
    {138,       "NBDATAGRAM " },
    {153,       "SGMP       " },
    {161,       "SNMP       " },
    {162,       "SNMP-TRAP  " },
    {315,       "LOAD       " },
    {500,       "SYTEK      " },
    {512,       "BIFF       " },
    {513,       "WHO        " },
    {514,       "SYSLOG     " },
    {517,       "TALK       " },
    {518,       "NTALK      " },
    {520,       "ROUTE      " },
    {525,       "TIMED      " },
};
void decode_udp(udp_type *pktp)
{
    printf("UDP HEADER\n");
    printf("src port     : %04x (%d) %s",bitrev16(((USHORT *)pktp->src_port)[0]),bitrev16(((USHORT *)pktp->src_port)[0]),xgetstr(tcp_port,bitrev16(((USHORT *)pktp->src_port)[0])));
    printf("     dest. port  : %04x (%d) % s\n",bitrev16(((USHORT *)pktp->dst_port)[0]),bitrev16(((USHORT *)pktp->dst_port)[0]),xgetstr(udp_port,bitrev16(((USHORT *)pktp->dst_port)[0])));
    printf("length       : %04x",bitrev16(((USHORT *)pktp->length)[0]));
    printf("          checksum    : %04x\n",bitrev16(((USHORT *)pktp->checksum)[0]));
}
void decode_data(raw_type * pktp, int len)
{
    int     i,j;
    UCHAR   asc;
//    USHORT hex;

//    for(i = 0x00; i < len; i+= 0x10)
    for(i = 0x00; i < len; i+= 0x40)
    {
        printf("%04x  : ",i);
//        for(j=i; j<i+0x10; j++)
//        {
//            hex = pktp->data[j];
//            if (j < len)
//                printf("%02x ",hex);
//            else
//                printf("   ");
//        }
//        printf("  ");
//        for(j=i; j<i+0x10; j++)
        for(j=i; j<i+0x40; j++)
        {
            asc = pktp->data[j];
            if ((asc<0x20) || (asc>0x7f) || (j>=len)) asc = 0x20;
            printf("%c",asc);
        }
        printf("\n");
    }
}
























⌨️ 快捷键说明

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