OSDN Git Service

add mbed.html
[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  * IPヘッダを送信パケット用に設定する。\r
267  * ipid16にはコールされるたびに新しい値を設定する。\r
268  * ipcecksumには0を設定する。\r
269  * この関数は、パケットサイズ,ローカルIP/リモートIPの設定はしない。\r
270  */\r
271 void NyLPC_TIPv4Header_writeTxIpHeader(\r
272         struct NyLPC_TIPv4Header* i_struct,\r
273         NyLPC_TUInt8 i_proto);\r
274 /**********************************************************************\r
275  *\r
276  * struct NyLPC_TIPv6Header\r
277  *\r
278  **********************************************************************/\r
279 \r
280 \r
281 struct NyLPC_TIPv6Header\r
282 {\r
283         /* IPv6 header. */\r
284         NyLPC_TUInt8 vtc;\r
285         NyLPC_TUInt8 tcflow;\r
286         NyLPC_TUInt16 flow;\r
287         NyLPC_TUInt8 len16;\r
288         NyLPC_TUInt8 proto, ttl;\r
289         uip_ip6addr_t srcipaddr;\r
290         uip_ip6addr_t destipaddr;\r
291 }PACK_STRUCT_END;\r
292 \r
293 void NyLPC_TIPv6Header_setSendHeader(\r
294         struct NyLPC_TIPv6Header* i_iph,\r
295         uip_ip6addr_t i_src,\r
296         uip_ip6addr_t i_dest,\r
297         NyLPC_TUInt8 i_proto,\r
298         NyLPC_TUInt8 i_ttl,\r
299         NyLPC_TUInt16 i_len);\r
300 \r
301 NyLPC_TUInt16 NyLPC_TIPv6Header_makeTcpChecksum(\r
302         struct NyLPC_TIPv6Header* i_iph,\r
303         NyLPC_TUInt16 i_len);\r
304 \r
305 #define NyLPC_TIPv6Header_isDestAddrEqual(i_struct,i_addr) (memcmp((i_struct)->destipaddr,(i_addr),sizeof(uip_ip6addr_t)))\r
306 #define NyLPC_TIPv6Header_isSrcAddrEqual(i_struct,i_addr) (memcmp(i_struct)->srcipaddr,(i_addr),sizeof(uip_ip6addr_t)))\r
307 /**********************************************************************\r
308  *\r
309  * struct NyLPC_TTcpHeader\r
310  *\r
311  **********************************************************************/\r
312 #define TCP_OPT_END     0   /* End of TCP options list */\r
313 #define TCP_OPT_NOOP    1   /* "No-operation" TCP option */\r
314 #define TCP_OPT_MSS     2   /* Maximum segment size TCP option */\r
315 #define TCP_OPT_MSS_LEN 4   /* Length of TCP MSS option. */\r
316 \r
317 /**\r
318  * TCP/IPヘッダのメモリマップ構造体\r
319  * マルチバイトの値は、全てネットワークオーダーです。
320  */\r
321 struct NyLPC_TTcpHeader\r
322 {\r
323         //TCP header.\r
324         NyLPC_TUInt16 srcport;\r
325         NyLPC_TUInt16 destport;\r
326         NyLPC_TUInt32 seqno32;\r
327         NyLPC_TUInt32 ackno32;\r
328         NyLPC_TUInt8 tcpoffset;\r
329         NyLPC_TUInt8 flags;\r
330         NyLPC_TUInt16 wnd16;\r
331         NyLPC_TUInt16 tcpchksum;\r
332         NyLPC_TUInt8 urgp[2];\r
333 //      NyLPC_TUInt8 optdata[4];\r
334 } PACK_STRUCT_END;\r
335 \r
336 \r
337 NyLPC_TUInt8* NyLPC_TTcpHeader_getTcpOptFragmentPtr(\r
338         const struct NyLPC_TTcpHeader* i_struct,\r
339         NyLPC_TUInt8 i_opt_id);\r
340 \r
341 NyLPC_TBool NyLPC_TTcpHeader_getTcpMmsOpt(\r
342         const struct NyLPC_TTcpHeader* i_struct,NyLPC_TUInt16* o_val);\r
343 \r
344 /**\r
345  * この関数は、TCPヘッダの長さを返します。ヘッダの長さは、i_structのフィールドから計算します。\r
346  * @param i_struct\r
347  * 構造体のアドレス。
348  */\r
349 NyLPC_TUInt16 NyLPC_TTcpHeader_getHeaderLength(const struct NyLPC_TTcpHeader* i_struct);\r
350 \r
351 /**\r
352  * この関数は、指定したアドレスに、mmsオプション値を書き込みます。
353  */\r
354 void NyLPC_TTcpHeader_setMmsOpt(NyLPC_TUInt8* i_opt_addr,NyLPC_TUInt16 i_mms);\r
355 \r
356 \r
357 /**********************************************************************\r
358  *\r
359  * struct NyLPC_TUdpHeader\r
360  *\r
361  **********************************************************************/\r
362 \r
363 /**\r
364  * UDP/IPヘッダのメモリマップ構造体\r
365  */\r
366 struct NyLPC_TUdpHeader\r
367 {\r
368         NyLPC_TUInt16 srcport;\r
369         NyLPC_TUInt16 destport;\r
370         NyLPC_TUInt16 udplen;\r
371         NyLPC_TUInt16 udpchksum;\r
372 } PACK_STRUCT_END;\r
373 \r
374 /**\r
375  * UDPヘッダの長さを返す。\r
376  */\r
377 #define NyLPC_TUdpHeader_getHeaderLength(i_struct) (8)\r
378 \r
379 /**********************************************************************\r
380  *\r
381  * struct NyLPC_TIcmpipHeader\r
382  *\r
383  **********************************************************************/\r
384 \r
385 \r
386 struct NyLPC_TIcmpHeader\r
387 {\r
388         /* ICMP (echo) header. */\r
389         NyLPC_TUInt8 type, icode;\r
390         NyLPC_TUInt16 icmpchksum;\r
391         #if !UIP_CONF_IPV6\r
392                 NyLPC_TUInt16 id, seqno;\r
393         #else /* !UIP_CONF_IPV6 */\r
394                 NyLPC_TUInt8 flags, reserved1, reserved2, reserved3;\r
395                 NyLPC_TUInt8 icmp6data[16];\r
396                 NyLPC_TUInt8 options[1];\r
397         #endif /* !UIP_CONF_IPV6 */\r
398 } PACK_STRUCT_END;\r
399 \r
400 /**********************************************************************\r
401  *\r
402  * struct NyLPC_TIcmpipHeader\r
403  *\r
404  **********************************************************************/\r
405 \r
406 struct NyLPC_TArpHeader\r
407 {\r
408   NyLPC_TUInt16 hwtype;\r
409   NyLPC_TUInt16 protocol;\r
410   NyLPC_TUInt8 hwlen;\r
411   NyLPC_TUInt8 protolen;\r
412   NyLPC_TUInt16 opcode;\r
413   struct NyLPC_TEthAddr shwaddr;\r
414   struct NyLPC_TIPv4Addr sipaddr;\r
415   struct NyLPC_TEthAddr dhwaddr;\r
416   struct NyLPC_TIPv4Addr dipaddr;\r
417 } PACK_STRUCT_END;\r
418 \r
419 \r
420 /**\r
421  * i_req_addrを問い合わせるARP_REQUESTを生成します。\r
422  */\r
423 void NyLPC_TArpHeader_setArpRequest(\r
424         struct NyLPC_TArpHeader* i_struct,\r
425         const struct NyLPC_TIPv4Addr i_saddr,\r
426         const struct NyLPC_TEthAddr* i_srceth,\r
427         const struct NyLPC_TIPv4Addr* i_req_addr);\r
428 \r
429 \r
430 typedef struct NyLPC_TcEthernetIIPayload NyLPC_TcEthernetIIPayload_t;\r
431 \r
432 \r
433 \r
434 \r
435 /**********************************************************************\r
436  *\r
437  * NyLPC_TIPv6Payload\r
438  *\r
439  **********************************************************************/\r
440 \r
441 \r
442 struct NyLPC_TIPv6Payload\r
443 {\r
444         struct NyLPC_TIPv6Header* header;\r
445         union{\r
446                 void* rawbuf;\r
447                 void* tcp;\r
448                 void* udp;\r
449                 void* icmp6;\r
450         }payload;\r
451 };\r
452 \r
453 struct NyLPC_TcEthernetIIPayload\r
454 {\r
455         struct NyLPC_TEthernetIIHeader* header;\r
456         union{\r
457                 void* rawbuf;\r
458                 struct NyLPC_TArpHeader* arp;\r
459                 struct NyLPC_TIPv4Payload* ipv4;\r
460                 struct NyLPC_TIPv6Payload* ipv6;\r
461         }payload;\r
462         NyLPC_TUInt16 len;\r
463 };\r
464 \r
465 \r
466 \r
467 \r
468 \r
469 #ifdef __cplusplus\r
470 }\r
471 #endif /* __cplusplus */\r
472 \r
473 \r
474 #endif\r