db93328dfa138f7681a467a06cbea1a948009c71
[tpg/acess2.git] / 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 void    (*tIPCallback)(tInterface *Interface, void *Address, int Length, void *Buffer);
19
20 enum eInterfaceTypes {
21         AF_NULL,
22         AF_INET4 = 4,   // tIPv4
23         AF_INET6 = 6    // tIPv6
24 };
25
26 union uIPv4 {
27         Uint32  L;
28         Uint8   B[4];
29 } __attribute__((packed));
30
31 union uIPv6 {
32         Uint16  W[8];
33         Uint32  L[4];
34         Uint8   B[16];
35 } __attribute__((packed));
36
37 struct sMacAddr {
38         Uint8   B[6];
39 } __attribute__((packed));
40
41 struct sInterface {
42         struct sInterface       *Next;  //!< Next interface in list
43         
44         tVFS_Node       Node;   //!< Node to use the interface
45         
46         tAdapter        *Adapter;       //!< Adapter the interface is associated with
47          int    TimeoutDelay;   //!< Time in miliseconds before a packet times out
48          int    Type;   //!< Interface type, see ::eInterfaceTypes
49         
50         void    *Address;       //!< IP address (stored after the Name)
51          int    SubnetBits;     //!< Number of bits that denote the address network
52         
53         char    Name[];
54 };
55
56 /**
57  * \brief Route definition structure
58  */
59 typedef struct sRoute {
60         struct sRoute   *Next;
61         
62         tVFS_Node       Node;   //!< Node for route manipulation
63         
64         tInterface      *Interface;     //!< Interface for this route
65          int    AddressType;    //!< 0: Invalid, 4: IPv4, 6: IPv4
66         void    *Network;       //!< Network - Pointer to tIPv4/tIPv6/... at end of structure
67          int    SubnetBits;     //!< Number of bits in \a Network that are valid
68         void    *NextHop;       //!< Next Hop address - Pointer to tIPv4/tIPv6/... at end of structure
69          int    Metric; //!< Route priority
70 }       tRoute;
71
72 /**
73  * \brief Represents a network adapter
74  */
75 struct sAdapter {
76         struct sAdapter *Next;
77         
78          int    DeviceFD;       //!< File descriptor of the device
79          int    NRef;   //!< Number of times it's been referenced
80         
81         tMacAddr        MacAddr;        //!< Physical address of the adapter
82          int    DeviceLen;      //!< Device name length
83         char    Device[];       //!< Device name
84 };
85
86 /**
87  * \brief Describes a socket file definition
88  */
89 struct sSocketFile
90 {
91         struct sSocketFile      *Next;
92         const char      *Name;
93         
94         tVFS_Node       *(*Init)(tInterface *Interface);
95 };
96
97 static const tMacAddr cMAC_BROADCAST = {{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
98
99 #define MAC_SET(t,v)    memcpy(&(t),&(v),sizeof(tMacAddr))
100 #define IP4_SET(t,v)    (t).L = (v).L;
101 #define IP6_SET(t,v)    memcpy(&(t),&(v),sizeof(tIPv6))
102
103 #define MAC_EQU(a,b)    (memcmp(&(a),&(b),sizeof(tMacAddr))==0)
104 #define IP4_EQU(a,b)    ((a).L==(b).L)
105 #define IP6_EQU(a,b)    (memcmp(&(a),&(b),sizeof(tIPv6))==0)
106
107 // === FUNCTIONS ===
108 #define htonb(v)        (v)
109 #define htons(v)        BigEndian16(v)
110 #define htonl(v)        BigEndian32(v)
111 #define ntonb(v)        (v)
112 #define ntohs(v)        BigEndian16(v)
113 #define ntohl(v)        BigEndian32(v)
114
115 extern int      IPStack_AddFile(tSocketFile *File);
116 extern int      IPStack_GetAddressSize(int AddressType);
117 extern int      IPStack_CompareAddress(int AddressType, void *Address1, void *Address2, int CheckBits);
118
119 #endif

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