OSDN Git Service

add mbed.html
[mimic/MiMicSDK.git] / lib / src / netif / mimicip / NyLPC_cIPv4IComp.c
1 /*********************************************************************************
2  * PROJECT: MiMic
3  * --------------------------------------------------------------------------------
4  *
5  * This file is part of MiMic
6  * Copyright (C)2011 Ryo Iizuka
7  *
8  * MiMic is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  *
21  * For further information please contact.
22  *  http://nyatla.jp/
23  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
24  *
25  *
26  * Parts of this file were leveraged from uIP:
27  *
28  * Copyright (c) 2001-2003, Adam Dunkels.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. The name of the author may not be used to endorse or promote
40  *    products derived from this software without specific prior
41  *    written permission.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
44  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
52  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
53  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54  */
55 #include "NyLPC_cIPv4IComp_protected.h"
56 #include "NyLPC_cMiMicIpNetIf_protected.h"
57 #define ICMP_ECHO_REPLY 0
58 #define ICMP_ECHO       8
59
60
61
62 NyLPC_TBool NyLPC_cIPv4IComp_initialize(
63     NyLPC_TcIPv4IComp_t* i_inst,
64     const NyLPC_TcIPv4Config_t* i_ref_config)
65 {
66     i_inst->_ref_config=i_ref_config;
67     return NyLPC_TBool_TRUE;
68 }
69 void NyLPC_cIPv4IComp_finalize(
70     NyLPC_TcIPv4IComp_t* i_inst)
71 {
72     return;
73 }
74 /**
75  * ヘッダブロックの後ろにあるIPペイロードのアドレスを返します。
76  */
77 #define NyLPC_TIPv4Header_getHeaderSize(i) (((i)->vhl & 0x0f)*4)
78
79 void* NyLPC_cIPv4IComp_rx(
80     const NyLPC_TcIPv4IComp_t* i_inst,
81     const NyLPC_TcIPv4Payload_t* i_ipp)
82 {
83     NyLPC_TUInt16 tx_size;
84     struct NyLPC_TIPv4Header* tx;
85     struct NyLPC_TIcmpHeader* payload;
86     const struct NyLPC_TIPv4Addr* my_ip=&(i_inst->_ref_config->ip_addr);
87
88     if (NyLPC_TIPv4Addr_isEqual(&(i_inst->_ref_config->ip_addr),&NyLPC_TIPv4Addr_ZERO))
89     {
90         /* If we are configured to use ping IP address configuration and
91          hasn't been assigned an IP address yet, we accept all ICMP
92          packets. */
93     } else {
94         /* Check if the packet is destined for our IP address. */
95         if (!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),my_ip))
96         {
97             return NyLPC_TBool_FALSE;
98         }
99     }
100     // ICMP echo (i.e., ping) processing. This is simple, we only change the ICMP type from ECHO to ECHO_REPLY and adjust the ICMP checksum before we return the packet.
101     if (i_ipp->payload.icmp->type != ICMP_ECHO)
102     {
103         return NyLPC_TBool_FALSE;
104     }
105     //返送パケットの取得
106     tx=(struct NyLPC_TIPv4Header*)NyLPC_cMiMicIpNetIf_allocTxBuf(NyLPC_NTOHS(i_ipp->header->len16),&tx_size);
107     if(tx==NULL){
108         return NyLPC_TBool_FALSE;
109     }
110     //パケットサイズのチェック
111     if(tx_size<NyLPC_NTOHS(i_ipp->header->len16)){
112         NyLPC_cMiMicIpNetIf_releaseTxBuf(tx);
113         return NyLPC_TBool_FALSE;
114     }
115     //返送パケットの構築
116     ;
117
118     //複製
119     memcpy(tx,i_ipp->header,NyLPC_NTOHS(i_ipp->header->len16));
120
121     //ペイロードの編集
122     payload=(struct NyLPC_TIcmpHeader*)(((NyLPC_TUInt8*)tx)+NyLPC_TIPv4Header_getHeaderSize(tx));
123     payload->type = ICMP_ECHO_REPLY;
124     //update checksum
125     if (payload->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) {
126         payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1;
127     } else {
128         payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8);
129     }
130     //IPヘッダの編集
131     tx->destipaddr=tx->srcipaddr;
132     tx->srcipaddr=*my_ip;
133 /*
134
135     //チェックサムの再計算
136     i_ipp->payload.icmp->type = ICMP_ECHO_REPLY;
137     if (i_ipp->payload.icmp->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) {
138         i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1;
139     } else {
140         i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8);
141     }
142     //OUT/INアドレスの反転
143     i_ipp->header->destipaddr=i_ipp->header->srcipaddr;
144     i_ipp->header->srcipaddr=*my_ip;
145 */
146     return tx;
147
148 }
149