Modules/IPStack - Add structure for propagating ICMP errors
[tpg/acess2.git] / KernelLand / Modules / IPStack / ipstack.h
1 /*
2  * Acess2 IP Stack
3  * - Common Header
4  */
5 #ifndef _IPSTACK_H_
6 #define _IPSTACK_H_
7
8 #include <acess.h>
9 #include <vfs.h>
10
11 typedef union uIPv4     tIPv4;
12 typedef union uIPv6     tIPv6;
13 typedef struct sMacAddr tMacAddr;
14 typedef struct sAdapter tAdapter;
15 typedef struct sInterface       tInterface;
16 typedef struct sSocketFile      tSocketFile;
17
18 typedef enum eIPErrorMode
19 {
20         IPERR_MISC,
21         IPERR_HOST_UNREACHABLE,
22         IPERR_PORT_UNREACHABLE,
23 } tIPErrorMode;
24
25 // NOTE: Non-const to allow reuse of Rx buffer for prepping Tx
26 typedef void    tIPRxCallback(tInterface *Interface, void *Address, int Length, void *Buffer);
27 typedef void    tIPErrorCallback(tInterface *Interface, tIPErrorMode mode, const void *Address, int Length, const void *Buffer);
28
29 enum eInterfaceTypes {
30         AF_NULL,
31         AF_INET4 = 4,   // tIPv4
32         AF_INET6 = 6    // tIPv6
33 };
34
35 union uIPv4 {
36         Uint32  L;
37         Uint8   B[4];
38 } __attribute__((packed));
39
40 union uIPv6 {
41         Uint16  W[8];
42         Uint32  L[4];
43         Uint8   B[16];
44 } __attribute__((packed));
45
46 struct sMacAddr {
47         Uint8   B[6];
48 } __attribute__((packed));
49
50 /**
51  * \brief Route definition structure
52  */
53 typedef struct sRoute {
54         struct sRoute   *Next;
55         
56         tVFS_Node       Node;   //!< Node for route manipulation
57         
58         tInterface      *Interface;     //!< Interface for this route
59          int    AddressType;    //!< 0: Invalid, 4: IPv4, 6: IPv4
60         void    *Network;       //!< Network - Pointer to tIPv4/tIPv6/... at end of structure
61          int    SubnetBits;     //!< Number of bits in \a Network that are valid
62         void    *NextHop;       //!< Next Hop address - Pointer to tIPv4/tIPv6/... at end of structure
63          int    Metric; //!< Route priority
64 }       tRoute;
65
66 struct sInterface {
67         struct sInterface       *Next;  //!< Next interface in list
68         
69         tVFS_Node       Node;   //!< Node to use the interface
70         
71         tAdapter        *Adapter;       //!< Adapter the interface is associated with
72          int    TimeoutDelay;   //!< Time in miliseconds before a packet times out
73          int    Type;   //!< Interface type, see ::eInterfaceTypes
74         
75         void    *Address;       //!< IP address (stored after the Name)
76          int    SubnetBits;     //!< Number of bits that denote the address network
77         
78         tRoute  Route;  //!< Interface route
79         
80         char    Name[];
81 };
82
83 #if 0
84 /**
85  * \brief Represents a network adapter
86  */
87 struct sAdapter {
88         struct sAdapter *Next;
89         
90          int    DeviceFD;       //!< File descriptor of the device
91          int    NRef;   //!< Number of times it's been referenced
92         
93         tMacAddr        MacAddr;        //!< Physical address of the adapter
94          int    DeviceLen;      //!< Device name length
95         char    Device[];       //!< Device name
96 };
97 #endif
98
99 /**
100  * \brief Describes a socket file definition
101  */
102 struct sSocketFile
103 {
104         struct sSocketFile      *Next;
105         const char      *Name;
106         
107         tVFS_Node       *(*Init)(tInterface *Interface);
108 };
109
110 static const tMacAddr cMAC_BROADCAST = {{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
111 static const tMacAddr cMAC_ZERO = {{0x00,0x00,0x00,0x00,0x00,0x00}};
112
113 #define MAC_SET(t,v)    memcpy(&(t),&(v),sizeof(tMacAddr))
114 #define IP4_SET(t,v)    (t).L = (v).L;
115 #define IP6_SET(t,v)    memcpy(&(t),&(v),sizeof(tIPv6))
116
117 #define MAC_EQU(a,b)    (memcmp(&(a),&(b),sizeof(tMacAddr))==0)
118 #define IP4_EQU(a,b)    ((a).L==(b).L)
119 #define IP6_EQU(a,b)    (memcmp(&(a),&(b),sizeof(tIPv6))==0)
120
121 // === FUNCTIONS ===
122 #define htonb(v)        (v)
123 #define htons(v)        BigEndian16(v)
124 #define htonl(v)        BigEndian32(v)
125 #define ntonb(v)        (v)
126 #define ntohs(v)        BigEndian16(v)
127 #define ntohl(v)        BigEndian32(v)
128
129 extern int      IPStack_AddFile(tSocketFile *File);
130 extern int      IPStack_GetAddressSize(int AddressType);
131 extern int      IPStack_CompareAddress(int AddressType, const void *Address1, const void *Address2, int CheckBits);
132 extern bool     IPStack_AddressIsBroadcast(int AddrType, const void *Addr, int SubnetBits);
133 extern const char       *IPStack_PrintAddress(int AddressType, const void *Address);
134
135 extern tRoute   *IPStack_FindRoute(int AddressType, tInterface *Interface, const void *Address);
136
137 #endif

UCC git Repository :: git.ucc.asn.au