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

📄 ip_fw_nt.h.svn-base

📁 wipfw 是windows下的网络控制工具
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/* Copyright (c) 2004-2006 Vlad Goncharov, Ruslan Staritsin
 *
 * Redistribution and use in source forms, with and without modification,
 * are permitted provided that this entire comment appears intact.
 *
 * Redistribution in binary form may occur without any restrictions.
 * Obviously, it would be nice if you gave credit where credit is due
 * but requiring it would be too onerous.
 *
 * This software is provided ``AS IS'' without any warranties of any kind.
 */

#ifndef _ip_fw_nt_h_
#define _ip_fw_nt_h_

// [u][_]int{8,16,32,64}_t moved to stdint.h
#include "stdint.h"

typedef char *          caddr_t;        /* core address */


#define __P(protos)     protos          /* full-blown ANSI C */


/* Vlad: <sys/queue> stuff */

#undef SLIST_ENTRY          // avoid conflicts with <ntddk.h>

#include <sys/queue.h>


/*
 * XPG4.2 states that inclusion of <netinet/in.h> must pull these
 * in and that inclusion of <sys/socket.h> must pull in sa_family_t.
 * We put there here because there are other headers that require
 * these types and <sys/socket.h> and <netinet/in.h> will indirectly
 * include <sys/types.h>.  Thus we are compliant without too many hoops.
 */
typedef u_int32_t       in_addr_t;      /* base type for internet address */
typedef u_int16_t       in_port_t;      /* IP port type */
typedef u_int16_t       sa_family_t;    /* sockaddr address family type (make sockaddr_in compatible with winsock one) */
typedef u_int32_t       socklen_t;      /* length type for network syscalls */



/*
 * IP Version 4 Internet address (a structure for historical reasons)
 */
struct in_addr {
        in_addr_t s_addr;
};

/*
 * IP Version 4 socket address.
 */
#pragma pack(1)     // Vlad: this structure compatible with winsock one
struct sockaddr_in {
        sa_family_t sin_family;
        in_port_t   sin_port;
        struct      in_addr sin_addr;
        int8_t      sin_zero[8];
};
#pragma pack()

/*
 * Network types.
 *
 * Internally the system keeps counters in the headers with the bytes
 * swapped so that VAX instructions will work on them.  It reverses
 * the bytes before transmission at each protocol level.  The n_ types
 * represent the types with the bytes in ``high-ender'' order.
 */
typedef u_int16_t n_short;              /* short as received from the net */
typedef u_int32_t n_long;               /* long as received from the net */

typedef u_int32_t n_time;               /* ms since 00:00 GMT, byte rev */



#define LITTLE_ENDIAN   1234
#define BIG_ENDIAN      4321


#define BYTE_ORDER  LITTLE_ENDIAN


#define NBBY    8               /* number of bits in a byte */


struct ifaddr {
        struct  sockaddr *ifa_addr;     /* address of interface */
        struct  ifnet *ifa_ifp;         /* back-pointer to interface */
        TAILQ_ENTRY(ifaddr) ifa_link;   /* queue macro glue */
        // Vlad: ripped
};

TAILQ_HEAD(ifaddrhead, ifaddr); /* instantiation is preserved in the list */

/*
 * Structure defining a network interface.
 *
 * (Would like to call this struct ``if'', but C isn't PL/1.)
 */
struct ifnet {
        // Vlad: structure ripped
        char    if_name[16];            /* name, e.g. ``en'' or ``lo'' */
        short   if_unit;                /* sub-unit for lower level driver */

        struct  ifaddrhead if_addrhead; /* linked list of addresses per if */

        // Vlad: iphlpapi interface index
        unsigned int    if_indx;
};


/*
 * Interface address, Internet version.  One of these structures
 * is allocated for each interface with an Internet address.
 * The ifaddr structure contains the protocol-independent part
 * of the structure and is assumed to be first.
 */
struct in_ifaddr {
        struct  ifaddr ia_ifa;          /* protocol-independent info */
#define ia_ifp          ia_ifa.ifa_ifp
        TAILQ_ENTRY(in_ifaddr) ia_list; /* list of internet addresses */
        struct  sockaddr_in ia_addr;    /* reserve space for interface name */
        // Vlad: ripped
};



TAILQ_HEAD(in_ifaddrhead, in_ifaddr);
extern  struct  in_ifaddrhead in_ifaddr;

/*
 * Macro for finding the interface (ifnet structure) corresponding to one
 * of our IP addresses.
 */
/*
 * Vlad: this macro in ip_fw.c used only to check is this address belongs to localhost (ifp only checked == NULL)
 *       and it's called always inside g_iflist_guard spinlock
 */
#define INADDR_TO_IFP(addr, ifp) \
        /* struct in_addr addr; */ \
        /* struct ifnet *ifp; */ \
{ \
        register struct in_ifaddr *ia; \
\
        for (ia = in_ifaddr.tqh_first; \
            ia != NULL && ia->ia_addr.sin_addr.s_addr != (addr).s_addr; \
            ia = ia->ia_list.tqe_next) \
                 continue; \
        (ifp) = (ia == NULL) ? NULL : ia->ia_ifp; \
}




/*
 * Structure used by kernel to store most
 * addresses.
 */
#pragma pack(1)
struct sockaddr {       // Vlad: this structure is compatible with winsock one
        sa_family_t sa_family;          /* address family */
        char        sa_data[14];        /* actually longer; address value */
};
#pragma pack()

#define AF_INET         2               /* internetwork: UDP, TCP, etc. */


/*
 * Vlad: C99 snprintf returns number of total bytes that should be copied (not bytes actually copied)
 * but in ip_fw.c it looks like snprintf should return strlen(buf)
 * Windows' _snprintf doesn't terminate buffer with zero if size > buf_size
 */
static int
snprintf(char *buf, size_t buf_size, const char *fmt, ...)
{
    int result;
    va_list ap;
    va_start(ap, fmt);
    result = _vsnprintf(buf, buf_size, fmt, ap);
    if (result < 0) {
        buf[buf_size - 1] = '\0';
        result = buf_size - 1;
    }
    va_end(ap);
    return result;
}

/*
 * Vlad: the original inet_ntoa is not thread safe. There's no TLS in kernel mode so I added buffer as 2nd parameter
 * function returns buf
 * buf size is INET_NTOA_BUF_SIZE
 */
char      *inet_ntoa_thread_safe(struct in_addr, char *buf);

#define INET_NTOA_BUF_SIZE      sizeof("255.255.255.255")
#define DECLARE_INET_NTOA_BUF   char inet_ntoa_buf[INET_NTOA_BUF_SIZE]

#define inet_ntoa(in_addr)      inet_ntoa_thread_safe(in_addr, inet_ntoa_buf)


/*
 * Protocols
 */
#define IPPROTO_IP              0               /* dummy for IP */
#define IPPROTO_HOPOPTS         IPPROTO_IP      /* Hop-by-hop option header */
#define IPPROTO_ICMP            1               /* control message protocol */
#define IPPROTO_IGMP            2               /* group mgmt protocol */
#define IPPROTO_GGP             3               /* gateway^2 (deprecated) */
#define IPPROTO_IPIP            4               /* IP inside IP */
#define IPPROTO_IPV4            IPPROTO_IPIP    /* IP inside IP */
#define IPPROTO_TCP             6               /* tcp */
#define IPPROTO_EGP             8               /* exterior gateway protocol */
#define IPPROTO_PUP             12              /* pup */
#define IPPROTO_UDP             17              /* user datagram protocol */
#define IPPROTO_IDP             22              /* xns idp */
#define IPPROTO_TP              29              /* tp-4 w/ class negotiation */
#define IPPROTO_IPV6            41              /* IPv6 in IPv6 */
#define IPPROTO_ROUTING         43              /* Routing header */
#define IPPROTO_FRAGMENT        44              /* Fragmentation/reassembly header */
#define IPPROTO_RSVP            46              /* resource reservation */
#define IPPROTO_GRE             47              /* GRE encap, RFCs 1701/1702 */
#define IPPROTO_ESP             50              /* Encap. Security Payload */
#define IPPROTO_AH              51              /* Authentication header */
#define IPPROTO_MOBILE          55              /* IP Mobility, RFC 2004 */
#define IPPROTO_ICMPV6          58              /* ICMP for IPv6 */
#define IPPROTO_NONE            59              /* No next header */
#define IPPROTO_DSTOPTS         60              /* Destination options header */
#define IPPROTO_EON             80              /* ISO cnlp */
#define IPPROTO_ETHERIP         97              /* Ethernet in IPv4 */
#define IPPROTO_ENCAP           98              /* encapsulation header */
#define IPPROTO_PIM             103             /* Protocol indep. multicast */
#define IPPROTO_IPCOMP          108             /* IP Payload Comp. Protocol */
#define IPPROTO_RAW             255             /* raw IP packet */

#define IPPROTO_MAX             256



/* Note that these macros evaluate their arguments several times.  */
#define __swap16gen(x)                                                  \
    (u_int16_t)(((u_int16_t)(x) & 0xff) << 8 | ((u_int16_t)(x) & 0xff00) >> 8)

#define __swap32gen(x)                                                  \
    (u_int32_t)(((u_int32_t)(x) & 0xff) << 24 |                         \
    ((u_int32_t)(x) & 0xff00) << 8 | ((u_int32_t)(x) & 0xff0000) >> 8 | \
    ((u_int32_t)(x) & 0xff000000) >> 24)

#define __swap64gen(x)                                                  \
        (u_int64_t)(((u_int64_t)(x) & 0xff) << 56) |                    \
            ((u_int64_t)(x) & 0xff00) << 40 |                           \
            ((u_int64_t)(x) & 0xff0000) << 24 |                         \
            ((u_int64_t)(x) & 0xff000000) << 8 |                        \
            ((u_int64_t)(x) & 0xff00000000) >> 8 |                      \
            ((u_int64_t)(x) & 0xff0000000000) >> 24 |                   \
            ((u_int64_t)(x) & 0xff000000000000) >> 40 |                 \
            ((u_int64_t)(x) & 0xff00000000000000) >> 56)


#define swap16 __swap16gen
#define swap32 __swap32gen
#define swap64 __swap64gen


#if BYTE_ORDER == LITTLE_ENDIAN

#define htobe16 swap16
#define htobe32 swap32
#define htobe64 swap64
#define betoh16 swap16
#define betoh32 swap32
#define betoh64 swap64

#define htole16(x) (x)
#define htole32(x) (x)
#define htole64(x) (x)
#define letoh16(x) (x)
#define letoh32(x) (x)
#define letoh64(x) (x)

#define htons htobe16
#define htonl htobe32
#define ntohs betoh16
#define ntohl betoh32

#define NTOHL(x) (x) = ntohl((u_int32_t)(x))
#define NTOHS(x) (x) = ntohs((u_int16_t)(x))
#define HTONL(x) (x) = htonl((u_int32_t)(x))
#define HTONS(x) (x) = htons((u_int16_t)(x))

#else

#define htole16 __swap16
#define htole32 __swap32
#define htole64 __swap64
#define letoh16 __swap16
#define letoh32 __swap32
#define letoh64 __swap64

#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)

⌨️ 快捷键说明

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