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

📄 ip.h

📁 altera epxa1的例子程序
💻 H
字号:
#ifndef _IP_
#define _IP_

#include "winsock.h"

#ifndef UCHAR
#define UCHAR  unsigned char
#endif
#ifndef USHORT
#define USHORT unsigned short
#endif
#ifndef ULONG
#define ULONG  unsigned long
#endif


#define ICMPMAXBUFLEN   1500

#define MAX_SOCKETS     10
#define MAX_PACKET_LEN  1514
#define TCP_PSEUDO_LEN  12
#define MAC_HEADER_LEN  0x0e
#define IP_HEADER_LEN   0x14
#define ICMP_HEADER_LEN 0x8
#define TCP_HEADER_LEN  0x14
#define TCP_OPTION_LEN  0x01
#define UDP_HEADER_LEN  0x08

// Ethernet types
#define UNKNOWN_PACKET_TYPE 0xfffe
#define IP_TYPE             0x0800
#define ARP_TYPE            0x0806
#define NETWARE_TYPE        0x8137
#define TYPE_II             0xffff

// IP protocols
#define UNKNOWN_IP_PROTOCOL 0xfffd
#define ICMP_PROTOCOL   1
#define IGMP_PROTOCOL   2
#define GGP_PROTOCOL    3
#define TCP_PROTOCOL    6
#define PUP_PROTOCOL    12
#define UDP_PROTOCOL    17

// ICMP types
#define UNKNOWN_ICMP_TYPE   0xfffc
#define ICMP_ECHO_REQUEST   0x08
#define ICMP_ECHO_RESPONSE  0x00

// TCP ports
#define UNKNOWN_TCP_PORT 0xfffb
#define ECHO_PORT       0x0700
#define DISCARD_PORT    0x0900
#define SYSTAT_PORT     0x0b00
#define DAYTIME_PORT    0x0d00
#define NETSTAT_PORT    0x0f00
#define QOTD_PORT       0x1100
#define CHARGEN_PORT    0x1300
#define FTP_PORT        0x1500
#define TELNET_PORT     0x1700
#define SMTP_PORT       0x1900
#define TIME_PORT       0x2500
#define NAMESERVER_PORT 0x2a00
#define WHOIS_PORT      0x2b00
#define DOMAIN_PORT     0x3500
#define MTP_PORT        0x3900
#define HTTP_PORT       0x5000
#define RJE_PORT       (77 << 8)
#define FINGER_PORT    (79 << 8)
// ...

// ARP operations
#define UNKNOWN_ARP_OPERATION 0xfffa
#define ARP_REQUEST         0x0100
#define ARP_RESPONSE        0x0200

// TCP flags
#define URG 0x20
#define ACK 0x10
#define PSH 0x08
#define RST 0x04
#define SYN 0x02
#define FIN 0x01

// TCP states
#define FREE        0
#define CLOSED      1
#define LISTEN      2
#define SYN_SENT    3
#define SYN_RCVD    4
#define ESTABLISHED 5
#define FIN_WAIT_1  6
#define FIN_WAIT_2  7
#define CLOSING     8
#define TIME_WAIT   9
#define CLOSE_WAIT  10
#define LAST_ACK    11

// TCP commands
#define QUIT        0xf000
#define IDLE        0xf100
#define CONNECT     0xf200
#define cLISTEN     0xf300
#define SEND        0xf400
#define CLOSE       0xf500
#define RESET       0xf600

#define DATAAVAIL   0x0080


typedef struct
{
    unsigned char ipaddr[4];
    unsigned char macaddr[6];
}arp_table_type;

typedef struct
{
    unsigned char dst_addr[6];
    unsigned char src_addr[6];
    unsigned char type[2];
}mac_type;

typedef struct
{
    unsigned char hardware_type[2];
    unsigned char protocol_type[2];
    unsigned char hlen;
    unsigned char plen;
    unsigned char operation[2];
    unsigned char sender_ha[6];
    unsigned char sender_ip[4];
    unsigned char target_ha[6];
    unsigned char target_ip[4];
}arp_type;

typedef struct
{
    unsigned char vers_hlen;
    unsigned char service_type;
    unsigned char len[2];
    unsigned char identification[2];
    unsigned char flags_frag_offset[2];
    unsigned char time_to_live;
    unsigned char protocol;
    unsigned char header_checksum[2];
    unsigned char src_ip_addr[4];
    unsigned char dst_ip_addr[4];
}ip_type;

typedef struct
{
    unsigned char type;
    unsigned char code;
    unsigned char checksum[2];
    unsigned char identifier[2];
    unsigned char sequence[2];
    unsigned char data[ICMPMAXBUFLEN];
}icmp_type;

typedef struct
{
    unsigned char src_port[2];
    unsigned char dst_port[2];
    unsigned char sequence[4];
    unsigned char acknowledgment[4];
    unsigned char data_offset;
    unsigned char flags;
    unsigned char window[2];
    unsigned char checksum[2];
    unsigned char urgentp[2];
    unsigned char data[1500];
}tcp_type;

typedef struct
{
    unsigned char src_port[2];
    unsigned char dst_port[2];
    unsigned char length[2];
    unsigned char checksum[2];
    unsigned char data[1500];
}udp_type;

typedef struct
{
    unsigned char data[1500];
}raw_type;


typedef struct
{
    union
    {
        unsigned char buffer[MAX_PACKET_LEN];
        struct
        {
            mac_type    mac;
            union
            {
                arp_type    arp;
                struct
                {
                    ip_type     ip;
                    union
                    {
                        icmp_type   icmp;
                        tcp_type    tcp;
                        udp_type    udp;
                        raw_type    raw;
                    }x;
                }x;
            }x;
        }x;
    }x;
} pkt_type;
#define buffer  x.buffer
#define mac     x.x.mac
#define arp     x.x.x.arp
#define ip      x.x.x.x.ip
#define icmp    x.x.x.x.x.icmp
#define tcp     x.x.x.x.x.tcp
#define udp     x.x.x.x.x.udp
#define raw     x.x.x.x.x.raw

typedef struct
{
    unsigned char src_ip_addr[4];
    unsigned char dst_ip_addr[4];
    unsigned char zero;
    unsigned char protocol;
    unsigned char tcp_len[2];
} tcp_pseudo_type;

typedef struct
    {
        USHORT  state;
        USHORT  data_available;
        UCHAR   type;
        UCHAR   protocol;
        UCHAR   blocking;
        UCHAR   srcmac[6];
        UCHAR   dstmac[6];
        UCHAR   srcip[4];
        UCHAR   dstip[4];
        USHORT  srcport;
        USHORT  dstport;
        ULONG   sequence;
        ULONG   acknowledge;
        USHORT  flags;
        UCHAR   *datap;
        USHORT  datalen;
    }tcb_type;


int ip_send(SOCKET skt, pkt_type *pktp, USHORT len);
int ip_receive(pkt_type *pktp, USHORT iplen1);

int udp_receive(SOCKET skt, pkt_type *pktp, USHORT udplen);
int raw_receive(SOCKET skt, pkt_type *pktp, USHORT rawlen);

USHORT checksumTCP(UCHAR * header, UCHAR * buf, USHORT len);
int host2ip(UCHAR *host, UCHAR *ipaddr);
int ip2host(UCHAR *host, UCHAR *ipaddr);
int route(UCHAR *gateip, UCHAR *dstip);

extern UCHAR bcastip[4];
extern int verbose;

#endif

⌨️ 快捷键说明

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