1 | // DSWifi Project - sgIP Internet Protocol Stack Implementation |
---|
2 | // Copyright (C) 2005-2006 Stephen Stair - sgstair@akkit.org - http://www.akkit.org |
---|
3 | /****************************************************************************** |
---|
4 | DSWifi Lib and test materials are licenced under the MIT open source licence: |
---|
5 | Copyright (c) 2005-2006 Stephen Stair |
---|
6 | |
---|
7 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
8 | this software and associated documentation files (the "Software"), to deal in |
---|
9 | the Software without restriction, including without limitation the rights to |
---|
10 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
---|
11 | of the Software, and to permit persons to whom the Software is furnished to do |
---|
12 | so, subject to the following conditions: |
---|
13 | |
---|
14 | The above copyright notice and this permission notice shall be included in all |
---|
15 | copies or substantial portions of the Software. |
---|
16 | |
---|
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
---|
23 | SOFTWARE. |
---|
24 | ******************************************************************************/ |
---|
25 | |
---|
26 | |
---|
27 | #ifndef SGIP_CONFIG_H |
---|
28 | #define SGIP_CONFIG_H |
---|
29 | |
---|
30 | #include <errno.h> |
---|
31 | |
---|
32 | ////////////////////////////////////////////////////////////////////////// |
---|
33 | // General options - these control the core functionality of the stack. |
---|
34 | |
---|
35 | // SGIP_USEDYNAMICMEMORY: Allows the stack to use memory as it needs it, via malloc()/free() |
---|
36 | // This option is extremely useful in environments where it can be used, as it prevents the |
---|
37 | // overhead of allocating per-connection memory in advance, and allows an unlimited number |
---|
38 | // of connections, provided the memory space. This option requires the implementation of |
---|
39 | // two C functions, "void * sgIP_malloc(int)" and "void sgIP_free(void *)", which behave |
---|
40 | // similarly to the malloc and free functions commonly used in C. |
---|
41 | #define SGIP_USEDYNAMICMEMORY |
---|
42 | |
---|
43 | // SGIP_INTERRUPT_THREADING_MODEL: Provides memory protection in a system that can allow |
---|
44 | // multiple processing "threads" by way of interrupts. This is not required on single |
---|
45 | // threaded systems, and not adequate on multithreaded systems, but provides a way to |
---|
46 | // allow protection against contention on interrupt-driven systems. This option requires |
---|
47 | // the system to implement two C functions "int sgIP_DisableInterrupts()" and additionally |
---|
48 | // "void sgIP_RestoreInterrupts(int)" that takes as a parameter the value returned by |
---|
49 | // sgIP_DisableInterrupts(). Interrupts are disabled upon beginning work with sensitive |
---|
50 | // memory areas or allocation/deallocation of memory, and are restored afterwards. |
---|
51 | #define SGIP_INTERRUPT_THREADING_MODEL |
---|
52 | |
---|
53 | // SGIP_MULTITHREADED_THREADING_MODEL: Standard memory protection for large multithreaded |
---|
54 | // systems, such as operating systems and the like. This kind of memory protection is |
---|
55 | // useful for true multithreaded systems but useless in a single-threaded system and |
---|
56 | // harmful in an interrupt-based multiprocess system. |
---|
57 | //#define SGIP_MULTITHREADED_THREADING_MODEL |
---|
58 | |
---|
59 | #define SGIP_LITTLEENDIAN |
---|
60 | |
---|
61 | ////////////////////////////////////////////////////////////////////////// |
---|
62 | // Temporary memory system settings |
---|
63 | |
---|
64 | // SGIP_MEMBLOCK_DATASIZE: This is the maximum data size contained in a single sgIP_memblock. |
---|
65 | // for best performance ensure this value is larger than any packet that is expected to be |
---|
66 | // received, however, in a memory-tight situation, much smaller values can be used. |
---|
67 | #define SGIP_MEMBLOCK_DATASIZE 1600 |
---|
68 | |
---|
69 | // SGIP_MEMBLOCK_BASENUM: The starting number of memblocks that will be allocated. This is |
---|
70 | // also the total number of memblocks that will be allocated if sgIP is not configured to use |
---|
71 | // dynamic memory allocation. |
---|
72 | #define SGIP_MEMBLOCK_BASENUM 12 |
---|
73 | |
---|
74 | // SGIP_MEMBLOCK_STEPNUM: In the case that all memblocks are full, and dynamic memory is |
---|
75 | // enabled, this many additional memblocks will be allocated in an attempt to satasfy the |
---|
76 | // memory usage demands of the stack. |
---|
77 | #define SGIP_MEMBLOCK_STEPNUM 6 |
---|
78 | |
---|
79 | // SGIP_MEMBLOCK_DYNAMIC_MALLOC_ALL: Who cares what the other memblock defines say, let's |
---|
80 | // Generate all memblocks by mallocing 'em. |
---|
81 | #define SGIP_MEMBLOCK_DYNAMIC_MALLOC_ALL |
---|
82 | |
---|
83 | ////////////////////////////////////////////////////////////////////////// |
---|
84 | // Hardware layer settings |
---|
85 | |
---|
86 | // SGIP_MAXHWADDRLEN: The maximum usable hardware address length. Ethernet is 6 bytes. |
---|
87 | #define SGIP_MAXHWADDRLEN 8 |
---|
88 | |
---|
89 | // SGIP_MAXHWHEADER: The maximum allocated size for hardware headers. |
---|
90 | #define SGIP_MAXHWHEADER 16 |
---|
91 | |
---|
92 | // SGIP_MTU_OVERRIDE: This is the maximum MTU that will be accepted. By default it is being |
---|
93 | // set to 1460 bytes in order to be courteous to Ethernet and it's ridiculously low MTU. |
---|
94 | // This value will allow you to prevent fragmentation of IP packets by not using the full |
---|
95 | // MTU available to your network interface when the IP packet will just be sliced and diced |
---|
96 | // at the next smaller MTU. (the stack will still use HW mtu if it's lower.) |
---|
97 | #define SGIP_MTU_OVERRIDE 1460 |
---|
98 | |
---|
99 | |
---|
100 | ////////////////////////////////////////////////////////////////////////// |
---|
101 | // Connection settings - can be tuned to change memory usage and performance |
---|
102 | |
---|
103 | // SGIP_TCP_STATELESS_LISTEN: Uses a technique to prevent syn-flooding from blocking listen |
---|
104 | // ports by using all the connection blocks/memory. |
---|
105 | #define SGIP_TCP_STATELESS_LISTEN |
---|
106 | |
---|
107 | // SGIP_TCP_STEALTH: Only sends packets in response to connections to active ports. Doing so |
---|
108 | // causes ports to appear not as closed, but as if the deviced does not exist when probing |
---|
109 | // ports that are not in use. |
---|
110 | //#define SGIP_TCP_STEALTH |
---|
111 | |
---|
112 | // SGIP_TCP_TTL: Time-to-live value given to outgoing packets, in the absence of a reason to |
---|
113 | // manually override this value. |
---|
114 | #define SGIP_IP_TTL 128 |
---|
115 | |
---|
116 | // SGIP_TCPRECEIVEBUFFERLENGTH: The size (in bytes) of the receive FIFO in a TCP connection |
---|
117 | #define SGIP_TCP_RECEIVEBUFFERLENGTH 8192 |
---|
118 | |
---|
119 | // SGIP_TCPTRANSMITBUFFERLENGTH: The size (in bytes) of the transmit FIFO in a TCP connection |
---|
120 | #define SGIP_TCP_TRANSMITBUFFERLENGTH 8192 |
---|
121 | |
---|
122 | // SGIP_TCPOOBBUFFERLENGTH: The size (in bytes) of the receive OOB data FIFO in a TCP connection |
---|
123 | #define SGIP_TCP_OOBBUFFERLENGTH 256 |
---|
124 | |
---|
125 | // SGIP_ARP_MAXENTRIES: The maximum number of cached ARP entries - this is defined staticly |
---|
126 | // because it's somewhat impractical to dynamicly allocate memory for such a small structure |
---|
127 | // (at least on most smaller systems) |
---|
128 | #define SGIP_ARP_MAXENTRIES 32 |
---|
129 | |
---|
130 | // SGIP_HUB_MAXHWINTERFACES: The maximum number of hardware interfaces the sgIP hub will |
---|
131 | // connect to. A hardware interface being some port (ethernet, wifi, etc) that will relay |
---|
132 | // packets to the outside world. |
---|
133 | #define SGIP_HUB_MAXHWINTERFACES 1 |
---|
134 | |
---|
135 | // SGIP_HUB_MAXPROTOCOLINTERFACES: The maximum number of protocol interfaces the sgIP hub will |
---|
136 | // connect to. A protocol interface being a software handler for a certain protocol type |
---|
137 | // (such as IP) |
---|
138 | #define SGIP_HUB_MAXPROTOCOLINTERFACES 1 |
---|
139 | |
---|
140 | #define SGIP_TCP_FIRSTOUTGOINGPORT 40000 |
---|
141 | #define SGIP_TCP_LASTOUTGOINGPORT 65000 |
---|
142 | #define SGIP_UDP_FIRSTOUTGOINGPORT 40000 |
---|
143 | #define SGIP_UDP_LASTOUTGOINGPORT 65000 |
---|
144 | |
---|
145 | #define SGIP_TCP_GENTIMEOUTMS 6000 |
---|
146 | #define SGIP_TCP_TRANSMIT_DELAY 25 |
---|
147 | #define SGIP_TCP_TRANSMIT_IMMTHRESH 40 |
---|
148 | #define SGIP_TCP_TIMEMS_2MSL 1000*60*2 |
---|
149 | #define SGIP_TCP_MAXRETRY 7 |
---|
150 | #define SGIP_TCP_MAXSYNS 64 |
---|
151 | #define SGIP_TCP_REACK_THRESH 1000 |
---|
152 | |
---|
153 | #define SGIP_TCP_SYNRETRYMS 250 |
---|
154 | #define SGIP_TCP_GENRETRYMS 500 |
---|
155 | #define SGIP_TCP_BACKOFFMAX 6000 |
---|
156 | |
---|
157 | #define SGIP_SOCKET_MAXSOCKETS 32 |
---|
158 | |
---|
159 | //#define SGIP_SOCKET_DEFAULT_NONBLOCK 1 |
---|
160 | |
---|
161 | |
---|
162 | ////////////////////////////////////////////////////////////////////////// |
---|
163 | // DNS settings |
---|
164 | |
---|
165 | #define SGIP_DNS_MAXRECORDSCACHE 16 |
---|
166 | #define SGIP_DNS_MAXRECORDADDRS 4 |
---|
167 | #define SGIP_DNS_MAXALIASES 4 |
---|
168 | #define SGIP_DNS_TIMEOUTMS 5000 |
---|
169 | #define SGIP_DNS_MAXRETRY 3 |
---|
170 | #define SGIP_DNS_MAXSERVERRETRY 4 |
---|
171 | |
---|
172 | ////////////////////////////////////////////////////////////////////////// |
---|
173 | |
---|
174 | #define SGIP_DHCP_ERRORTIMEOUT 45000 |
---|
175 | #define SGIP_DHCP_RESENDTIMEOUT 3000 |
---|
176 | #define SGIP_DHCP_DEFAULTHOSTNAME "NintendoDS" |
---|
177 | #define SGIP_DHCP_CLASSNAME "sgIP 0.3" |
---|
178 | |
---|
179 | ////////////////////////////////////////////////////////////////////////// |
---|
180 | // Static memory settings - only used if SGIP_USEDYNAMICMEMORY is NOT defined. |
---|
181 | |
---|
182 | // SGIP_TCP_MAXCONNECTIONS: In the case dynamic memory is not used, this value gives the max |
---|
183 | // number of TCP blocks available for inbound/outbound connections via TCP. |
---|
184 | #define SGIP_TCP_MAXCONNECTIONS 10 |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | ////////////////////////////////////////////////////////////////////////// |
---|
190 | // Debugging options |
---|
191 | |
---|
192 | |
---|
193 | // SGIP_DEBUG: Enable debug logging. |
---|
194 | // requires external function "void sgIP_dbgprint(char *, ...);" |
---|
195 | //#define SGIP_DEBUG |
---|
196 | |
---|
197 | #ifdef SGIP_DEBUG |
---|
198 | #define SGIP_DEBUG_MESSAGE(param) sgIP_dbgprint param |
---|
199 | #define SGIP_DEBUG_ERROR(param) sgIP_dbgprint param; while(1); |
---|
200 | #else |
---|
201 | #define SGIP_DEBUG_MESSAGE(param) |
---|
202 | #define SGIP_DEBUG_ERROR(param) |
---|
203 | #endif |
---|
204 | |
---|
205 | |
---|
206 | ////////////////////////////////////////////////////////////////////////// |
---|
207 | // Error handling |
---|
208 | extern int sgIP_errno; |
---|
209 | #define SGIP_ERROR(a) ((errno=(a)), -1) |
---|
210 | #define SGIP_ERROR0(a) ((errno=(a)), 0) |
---|
211 | |
---|
212 | ////////////////////////////////////////////////////////////////////////// |
---|
213 | // Error checking |
---|
214 | |
---|
215 | |
---|
216 | #ifdef SGIP_MULTITHREADED_THREADING_MODEL |
---|
217 | #ifdef SGIP_INTERRUPT_THREADING_MODEL |
---|
218 | #error SGIP_INTERRUPT_THREADING_MODEL and SGIP_MULTITHREADED_THREADING_MODEL cannot be used together! |
---|
219 | #endif |
---|
220 | #endif |
---|
221 | |
---|
222 | |
---|
223 | ////////////////////////////////////////////////////////////////////////// |
---|
224 | // External option-based dependencies |
---|
225 | |
---|
226 | |
---|
227 | #ifdef SGIP_INTERRUPT_THREADING_MODEL |
---|
228 | #ifdef __cplusplus |
---|
229 | extern "C" { |
---|
230 | #endif |
---|
231 | extern int sgIP_DisableInterrupts(void); |
---|
232 | extern void sgIP_RestoreInterrupts(int); |
---|
233 | extern void sgIP_IntrWaitEvent(void); |
---|
234 | #ifdef __cplusplus |
---|
235 | }; |
---|
236 | #endif |
---|
237 | #define SGIP_INTR_PROTECT() \ |
---|
238 | int tIME; \ |
---|
239 | tIME=sgIP_DisableInterrupts() |
---|
240 | #define SGIP_INTR_REPROTECT() \ |
---|
241 | tIME=sgIP_DisableInterrupts() |
---|
242 | #define SGIP_INTR_UNPROTECT() \ |
---|
243 | sgIP_RestoreInterrupts(tIME) |
---|
244 | #define SGIP_WAITEVENT() \ |
---|
245 | sgIP_IntrWaitEvent() |
---|
246 | #else // !SGIP_INTERRUPT_THREADING_MODEL |
---|
247 | #define SGIP_INTR_PROTECT() |
---|
248 | #define SGIP_INTR_REPROTECT() |
---|
249 | #define SGIP_INTR_UNPROTECT() |
---|
250 | #define SGIP_WAITEVENT(); |
---|
251 | #endif // SGIP_INTERRUPT_THREADING_MODEL |
---|
252 | |
---|
253 | #ifdef SGIP_DEBUG |
---|
254 | #ifdef __cplusplus |
---|
255 | extern "C" { |
---|
256 | #endif |
---|
257 | extern void sgIP_dbgprint(char *, ...); |
---|
258 | #ifdef __cplusplus |
---|
259 | }; |
---|
260 | #endif |
---|
261 | #endif // SGIP_DEBUG |
---|
262 | |
---|
263 | #ifdef SGIP_USEDYNAMICMEMORY |
---|
264 | #ifdef __cplusplus |
---|
265 | extern "C" { |
---|
266 | #endif |
---|
267 | extern void * sgIP_malloc(int); |
---|
268 | extern void sgIP_free(void *); |
---|
269 | #ifdef __cplusplus |
---|
270 | }; |
---|
271 | #endif |
---|
272 | #endif // SGIP_USEDYNAMICMEMORY |
---|
273 | |
---|
274 | #endif |
---|