OSDN Git Service

85ff27a68a74c1d20ce77802f3c4a7aa7a015f95
[mimic/MiMicSDK.git] / lib / src / uip / NyLPC_uip.h
1 /*********************************************************************************\r
2  * PROJECT: MiMic\r
3  * --------------------------------------------------------------------------------\r
4  *\r
5  * This file is part of MiMic\r
6  * Copyright (C)2011 Ryo Iizuka\r
7  *\r
8  * MiMic is free software: you can redistribute it and/or modify\r
9  * it under the terms of the GNU Lesser General Public License as published\r
10  * by the Free Software Foundation, either version 3 of the License, or\r
11  * (at your option) any later version.\r
12  *\r
13  * This program is distributed in the hope that it will be useful,\r
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
16  * GNU General Public License for more details.\r
17  *\r
18  * You should have received a copy of the GNU Lesser General Public License\r
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
20  *\r
21  * For further information please contact.\r
22  *      http://nyatla.jp/\r
23  *      <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>\r
24  *\r
25  *\r
26  * Parts of this file were leveraged from uIP:\r
27  *\r
28  * Copyright (c) 2001-2003, Adam Dunkels.\r
29  * All rights reserved.\r
30  *\r
31  * Redistribution and use in source and binary forms, with or without\r
32  * modification, are permitted provided that the following conditions\r
33  * are met:\r
34  * 1. Redistributions of source code must retain the above copyright\r
35  *    notice, this list of conditions and the following disclaimer.\r
36  * 2. Redistributions in binary form must reproduce the above copyright\r
37  *    notice, this list of conditions and the following disclaimer in the\r
38  *    documentation and/or other materials provided with the distribution.\r
39  * 3. The name of the author may not be used to endorse or promote\r
40  *    products derived from this software without specific prior\r
41  *    written permission.\r
42  *\r
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS\r
44  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r
45  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY\r
47  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE\r
49  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
50  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,\r
51  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
52  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
53  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
54  */\r
55 #ifndef NyLPC_uip_h\r
56 #define NyLPC_uip_h\r
57 #include "../include/NyLPC_config.h"\r
58 #include "../include/NyLPC_stdlib.h"\r
59 #include "NyLPC_uip_ethernet.h"\r
60 \r
61 #ifdef __cplusplus\r
62 extern "C" {\r
63 #endif /* __cplusplus */\r
64 \r
65 #define UIP_PROTO_ICMP  1\r
66 #define UIP_PROTO_TCP   6\r
67 #define UIP_PROTO_UDP   17\r
68 #define UIP_PROTO_ICMP6 58\r
69 \r
70 #define ARP_REQUEST 1\r
71 #define ARP_REPLY   2\r
72 #define ARP_HWTYPE_ETH 1\r
73 \r
74 \r
75 #ifndef PACK_STRUCT_END\r
76         #define PACK_STRUCT_END __attribute((packed))\r
77 #endif\r
78 \r
79 /**********************************************************************\r
80  *\r
81  *\r
82  *\r
83  **********************************************************************/\r
84 \r
85 /**\r
86  * IPアドレスを格納します。\r
87  * IPアドレスは、ネットワークオーダーで設定します。\r
88  */\r
89 struct NyLPC_TIPv4Addr\r
90 {\r
91         NyLPC_TUInt32 v;\r
92 }PACK_STRUCT_END;\r
93 extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ZERO;\r
94 extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL;\r
95 extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST;\r
96 extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST;\r
97 extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK;\r
98 extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK;\r
99 \r
100 /**\r
101  * addr1とaddr2が全く同じであるかをテストします。\r
102  * \hideinitializer\r
103  */\r
104 #define NyLPC_TIPv4Addr_isEqual(v1,v2) ((v1)->v==(v2)->v)\r
105 /**\r
106  * addr1とaddr2をmaskでマスクした結果を比較します。\r
107  * \hideinitializer
108  */\r
109 #define NyLPC_TIPv4Addr_isEqualWithMask(addr1, addr2, mask) ((((addr1)->v) & ((mask)->v))==(((addr2)->v) & ((mask)->v)))\r
110 \r
111 /**\r
112  * 変数にIP v4アドレスをセットします。\r
113  * 次のように使います。\r
114  \code\r
115  NyLPC_TIPv4Addr ip;\r
116  NyLPC_TIpv4Addr_set(&ip,1,2,3,4);\r
117  \endcode\r
118  */\r
119 #define NyLPC_TIPv4Addr_set(s,a0,a1,a2,a3) (s)->v=NyLPC_htonl((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&((NyLPC_TUInt32)(a3))))\r
120 #define NyLPC_TIPv4Addr_pack(a0,a1,a2,a3) {NyLPC_HTONL((0xff000000&(((NyLPC_TUInt32)(a0))<<24))|(0x00ff0000&(((NyLPC_TUInt32)(a1))<<16))|(0x0000ff00&(((NyLPC_TUInt32)(a2))<<8))|(0x000000ff&((NyLPC_TUInt32)(a3))))}\r
121 \r
122 /**\r
123  * IPアドレスを文字列に変換して返します。\r
124  */\r
125 NyLPC_TInt16 NyLPC_TIPv4Addr_toString(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TChar* i_buf);\r
126 \r
127 \r
128 \r
129 \r
130 \r
131 struct uip_ip6addr\r
132 {\r
133         NyLPC_TUInt16 v[8];\r
134 }PACK_STRUCT_END;\r
135 \r
136 typedef struct uip_ip6addr uip_ip6addr_t;\r
137 \r
138 NyLPC_TUInt16 NyLPC_uip_chksum(NyLPC_TUInt16 sum, const NyLPC_TUInt8 *data, NyLPC_TUInt16 len);\r
139 \r
140 \r
141 \r
142 \r
143 /**\r
144  * TTL\r
145  */\r
146 #define UIP_DEFAULT_IP_TTL 64\r
147 \r
148 \r
149 \r
150 /**\r
151  * The maximum number of times a segment should be retransmitted\r
152  * before the connection should be aborted.\r
153  *\r
154  * This should not be changed.\r
155  */\r
156 #define UIP_MAXRTX      8\r
157 \r
158 \r
159 \r
160 \r
161 \r
162 #   if UIP_BYTE_ORDER == NyLPC_ENDIAN_BIG\r
163 #       define NyLPC_HTONS(n) (n)\r
164 #               define NyLPC_htons(n) (n)\r
165 #               define NyLPC_ntohs(n) (n)\r
166 #               define NyLPC_htonl(n) (n)\r
167 #               define NyLPC_ntohl(n) (n)\r
168 #               define NyLPC_HTONS(n) (n)\r
169 #               define NyLPC_NTOHS(n) (n)\r
170 #   else\r
171 #               define NyLPC_htonl(n) NyLPC_TUInt32_bswap(n)\r
172 #               define NyLPC_ntohl(n) NyLPC_TUInt32_bswap(n)\r
173 #               define NyLPC_htons(n) NyLPC_TUInt16_bswap(n)\r
174 #               define NyLPC_ntohs(n) NyLPC_TUInt16_bswap(n)\r
175 #               define NyLPC_HTONS(n) NyLPC_TUInt16_BSWAP(n)\r
176 #               define NyLPC_NTOHS(n) NyLPC_TUInt16_BSWAP(n)\r
177 #               define NyLPC_NTOHL(n) NyLPC_TUInt32_BSWAP(n)\r
178 #               define NyLPC_HTONL(n) NyLPC_TUInt32_BSWAP(n)\r
179 #endif /* NyLPC_HTONS */\r
180 \r
181 \r
182 \r
183 \r
184 /**********************************************************************\r
185  *\r
186  * struct NyLPC_TEthernetIIHeader\r
187  *\r
188  **********************************************************************/\r
189 \r
190 #define NyLPC_TEthernetIIHeader_TYPE_IP         0x0800\r
191 #define NyLPC_TEthernetIIHeader_TYPE_ARP        0x0806\r
192 #define NyLPC_TEthernetIIHeader_TYPE_IPV6       0x86DD\r
193 //#define UIP_ETHTYPE_IP  0x0800\r
194 //#define UIP_ETHTYPE_ARP 0x0806\r
195 //#define UIP_ETHTYPE_IP6 0x86dd\r
196 \r
197 struct NyLPC_TEthernetIIHeader\r
198 {\r
199         struct NyLPC_TEthAddr dest;\r
200         struct NyLPC_TEthAddr src;\r
201         NyLPC_TUInt16 type;\r
202 }PACK_STRUCT_END;\r
203 \r
204 \r
205 NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setArpTx(\r
206         struct NyLPC_TEthernetIIHeader* i_struct,\r
207         const struct NyLPC_TEthAddr* i_my_eth_addr);\r
208 \r
209 NyLPC_TUInt16 NyLPC_TEthernetIIHeader_setIPv4Tx(\r
210         struct NyLPC_TEthernetIIHeader* i_eth,\r
211         const struct NyLPC_TEthAddr* i_src_eth_addr,\r
212         const struct NyLPC_TEthAddr* i_dest_eth_addr);\r
213 /**********************************************************************\r
214  *\r
215  * struct NyLPC_TIPv4Header\r
216  *\r
217  **********************************************************************/\r
218 \r
219 \r
220 /**\r
221  * IPパケットヘッダのメモリマップ構造体\r
222  * 値はすべてネットワークオーダーです。
223  */\r
224 struct NyLPC_TIPv4Header\r
225 {\r
226         NyLPC_TUInt8 vhl;\r
227         NyLPC_TUInt8 tos;\r
228         NyLPC_TUInt16 len16;\r
229         NyLPC_TUInt16 ipid16;\r
230         NyLPC_TUInt16 ipoffset;\r
231         NyLPC_TUInt8 ttl;\r
232         NyLPC_TUInt8 proto;\r
233         NyLPC_TUInt16 ipchksum;\r
234         struct NyLPC_TIPv4Addr srcipaddr;\r
235         struct NyLPC_TIPv4Addr destipaddr;\r
236 }PACK_STRUCT_END;\r
237 \r
238 \r
239 \r
240 \r
241 NyLPC_TBool NyLPC_TIPv4Header_isCorrectIpCheckSum(\r
242         const struct NyLPC_TIPv4Header* ip_header);\r
243 \r
244 NyLPC_TBool NyLPC_cIPv4Packet_isCorrectTcpCheckSum(\r
245         const struct NyLPC_TIPv4Header* ip_header);\r
246 \r
247 NyLPC_TUInt16 NyLPC_TIPv4Header_makeIpChecksum(const struct NyLPC_TIPv4Header* ip_header);\r
248 \r
249 \r
250 NyLPC_TUInt16 NyLPC_TIPv4Header_makeTcpChecksum(\r
251         const struct NyLPC_TIPv4Header* i_iph);\r
252 \r
253 #define NyLPC_TIPv4Header_isDestAddrEqual(i_struct,i_addr) ((i_struct)->destipaddr==(i_addr))\r
254 #define NyLPC_TIPv4Header_isSrcAddrEqual(i_struct,i_addr) ((i_struct)->srcipaddr==(i_addr))\r
255 \r
256 /**\r
257  * IPヘッダの長さを返す。\r
258  */\r
259 #define NyLPC_TIPv4Header_getHeaderLength(i_iph) ((i_iph->vhl & 0x0f)*4)\r
260 /**\r
261  * IPパケット全体の長さを返す。
262  */\r
263 #define NyLPC_TIPv4Header_getPacketLength(i_iph) (NyLPC_ntohs((i_iph)->len16))\r
264 \r
265 /**********************************************************************\r
266  *\r
267  * struct NyLPC_TIPv6Header\r
268  *\r
269  **********************************************************************/\r
270 \r
271 \r
272 struct NyLPC_TIPv6Header\r
273 {\r
274         /* IPv6 header. */\r
275         NyLPC_TUInt8 vtc;\r
276         NyLPC_TUInt8 tcflow;\r
277         NyLPC_TUInt16 flow;\r
278         NyLPC_TUInt8 len16;\r
279         NyLPC_TUInt8 proto, ttl;\r
280         uip_ip6addr_t srcipaddr;\r
281         uip_ip6addr_t destipaddr;\r
282 }PACK_STRUCT_END;\r
283 \r
284 void NyLPC_TIPv6Header_setSendHeader(\r
285         struct NyLPC_TIPv6Header* i_iph,\r
286         uip_ip6addr_t i_src,\r
287         uip_ip6addr_t i_dest,\r
288         NyLPC_TUInt8 i_proto,\r
289         NyLPC_TUInt8 i_ttl,\r
290         NyLPC_TUInt16 i_len);\r
291 \r
292 NyLPC_TUInt16 NyLPC_TIPv6Header_makeTcpChecksum(\r
293         struct NyLPC_TIPv6Header* i_iph,\r
294         NyLPC_TUInt16 i_len);\r
295 \r
296 #define NyLPC_TIPv6Header_isDestAddrEqual(i_struct,i_addr) (memcmp((i_struct)->destipaddr,(i_addr),sizeof(uip_ip6addr_t)))\r
297 #define NyLPC_TIPv6Header_isSrcAddrEqual(i_struct,i_addr) (memcmp(i_struct)->srcipaddr,(i_addr),sizeof(uip_ip6addr_t)))\r
298 /**********************************************************************\r
299  *\r
300  * struct NyLPC_TTcpHeader\r
301  *\r
302  **********************************************************************/\r
303 #define TCP_OPT_END     0   /* End of TCP options list */\r
304 #define TCP_OPT_NOOP    1   /* "No-operation" TCP option */\r
305 #define TCP_OPT_MSS     2   /* Maximum segment size TCP option */\r
306 #define TCP_OPT_MSS_LEN 4   /* Length of TCP MSS option. */\r
307 \r
308 /**\r
309  * TCP/IPヘッダのメモリマップ構造体\r
310  * マルチバイトの値は、全てネットワークオーダーです。
311  */\r
312 struct NyLPC_TTcpHeader\r
313 {\r
314         //TCP header.\r
315         NyLPC_TUInt16 srcport;\r
316         NyLPC_TUInt16 destport;\r
317         NyLPC_TUInt32 seqno32;\r
318         NyLPC_TUInt32 ackno32;\r
319         NyLPC_TUInt8 tcpoffset;\r
320         NyLPC_TUInt8 flags;\r
321         NyLPC_TUInt16 wnd16;\r
322         NyLPC_TUInt16 tcpchksum;\r
323         NyLPC_TUInt8 urgp[2];\r
324 //      NyLPC_TUInt8 optdata[4];\r
325 } PACK_STRUCT_END;\r
326 \r
327 \r
328 NyLPC_TUInt8* NyLPC_TTcpHeader_getTcpOptFragmentPtr(\r
329         const struct NyLPC_TTcpHeader* i_struct,\r
330         NyLPC_TUInt8 i_opt_id);\r
331 \r
332 NyLPC_TBool NyLPC_TTcpHeader_getTcpMmsOpt(\r
333         const struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt16* o_val);\r
334 \r
335 /**\r
336  * この関数は、TCPヘッダの長さを返します。ヘッダの長さは、i_structのフィールドから計算します。\r
337  * @param i_struct\r
338  * 構造体のアドレス。
339  */\r
340 NyLPC_TUInt16 NyLPC_TTcpHeader_getHeaderLength(const struct NyLPC_TTcpHeader* i_struct);\r
341 \r
342 /**\r
343  * この関数は、指定したアドレスに、mmsオプション値を書き込みます。
344  */\r
345 void NyLPC_TTcpHeader_setMmsOpt(NyLPC_TUInt8* i_opt_addr,NyLPC_TUInt16 i_mms);\r
346 \r
347 \r
348 /**********************************************************************\r
349  *\r
350  * struct NyLPC_TUdpHeader\r
351  *\r
352  **********************************************************************/\r
353 \r
354 /**\r
355  * UDP/IPヘッダのメモリマップ構造体\r
356  */\r
357 struct NyLPC_TUdpHeader\r
358 {\r
359         NyLPC_TUInt16 srcport;\r
360         NyLPC_TUInt16 destport;\r
361         NyLPC_TUInt16 udplen;\r
362         NyLPC_TUInt16 udpchksum;\r
363 } PACK_STRUCT_END;\r
364 \r
365 /**\r
366  * UDPヘッダの長さを返す。\r
367  */\r
368 #define NyLPC_TUdpHeader_getHeaderLength(i_struct) (8)\r
369 \r
370 /**********************************************************************\r
371  *\r
372  * struct NyLPC_TIcmpipHeader\r
373  *\r
374  **********************************************************************/\r
375 \r
376 \r
377 struct NyLPC_TIcmpHeader\r
378 {\r
379         /* ICMP (echo) header. */\r
380         NyLPC_TUInt8 type, icode;\r
381         NyLPC_TUInt16 icmpchksum;\r
382         #if !UIP_CONF_IPV6\r
383                 NyLPC_TUInt16 id, seqno;\r
384         #else /* !UIP_CONF_IPV6 */\r
385                 NyLPC_TUInt8 flags, reserved1, reserved2, reserved3;\r
386                 NyLPC_TUInt8 icmp6data[16];\r
387                 NyLPC_TUInt8 options[1];\r
388         #endif /* !UIP_CONF_IPV6 */\r
389 } PACK_STRUCT_END;\r
390 \r
391 /**********************************************************************\r
392  *\r
393  * struct NyLPC_TIcmpipHeader\r
394  *\r
395  **********************************************************************/\r
396 \r
397 struct NyLPC_TArpHeader\r
398 {\r
399   NyLPC_TUInt16 hwtype;\r
400   NyLPC_TUInt16 protocol;\r
401   NyLPC_TUInt8 hwlen;\r
402   NyLPC_TUInt8 protolen;\r
403   NyLPC_TUInt16 opcode;\r
404   struct NyLPC_TEthAddr shwaddr;\r
405   struct NyLPC_TIPv4Addr sipaddr;\r
406   struct NyLPC_TEthAddr dhwaddr;\r
407   struct NyLPC_TIPv4Addr dipaddr;\r
408 } PACK_STRUCT_END;\r
409 \r
410 \r
411 /**\r
412  * i_req_addrを問い合わせるARP_REQUESTを生成します。\r
413  */\r
414 void NyLPC_TArpHeader_setArpRequest(\r
415         struct NyLPC_TArpHeader* i_struct,\r
416         const struct NyLPC_TIPv4Addr i_saddr,\r
417         const struct NyLPC_TEthAddr* i_srceth,\r
418         const struct NyLPC_TIPv4Addr* i_req_addr);\r
419 \r
420 \r
421 typedef struct NyLPC_TcEthernetIIPayload NyLPC_TcEthernetIIPayload_t;\r
422 \r
423 \r
424 \r
425 \r
426 /**********************************************************************\r
427  *\r
428  * NyLPC_TIPv6Payload\r
429  *\r
430  **********************************************************************/\r
431 \r
432 \r
433 struct NyLPC_TIPv6Payload\r
434 {\r
435         struct NyLPC_TIPv6Header* header;\r
436         union{\r
437                 void* rawbuf;\r
438                 void* tcp;\r
439                 void* udp;\r
440                 void* icmp6;\r
441         }payload;\r
442 };\r
443 \r
444 struct NyLPC_TcEthernetIIPayload\r
445 {\r
446         struct NyLPC_TEthernetIIHeader* header;\r
447         union{\r
448                 void* rawbuf;\r
449                 struct NyLPC_TArpHeader* arp;\r
450                 struct NyLPC_TIPv4Payload* ipv4;\r
451                 struct NyLPC_TIPv6Payload* ipv6;\r
452         }payload;\r
453         NyLPC_TUInt16 len;\r
454 };\r
455 \r
456 \r
457 \r
458 \r
459 \r
460 #ifdef __cplusplus\r
461 }\r
462 #endif /* __cplusplus */\r
463 \r
464 \r
465 #endif\r