This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NyLPC_cIPv4IComp.c Source File

NyLPC_cIPv4IComp.c

00001 /*********************************************************************************
00002  * PROJECT: MiMic
00003  * --------------------------------------------------------------------------------
00004  *
00005  * This file is part of MiMic
00006  * Copyright (C)2011 Ryo Iizuka
00007  *
00008  * MiMic is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License as published
00010  * by the Free Software Foundation, either version 3 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public License
00019  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  * For further information please contact.
00022  *  http://nyatla.jp/
00023  *  <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
00024  *
00025  *
00026  * Parts of this file were leveraged from uIP:
00027  *
00028  * Copyright (c) 2001-2003, Adam Dunkels.
00029  * All rights reserved.
00030  *
00031  * Redistribution and use in source and binary forms, with or without
00032  * modification, are permitted provided that the following conditions
00033  * are met:
00034  * 1. Redistributions of source code must retain the above copyright
00035  *    notice, this list of conditions and the following disclaimer.
00036  * 2. Redistributions in binary form must reproduce the above copyright
00037  *    notice, this list of conditions and the following disclaimer in the
00038  *    documentation and/or other materials provided with the distribution.
00039  * 3. The name of the author may not be used to endorse or promote
00040  *    products derived from this software without specific prior
00041  *    written permission.
00042  *
00043  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
00044  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00045  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00046  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00047  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00048  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00049  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00050  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00052  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00053  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00054  */
00055 #include "NyLPC_cIPv4IComp_protected.h"
00056 #include "NyLPC_cMiMicIpNetIf_protected.h"
00057 #define ICMP_ECHO_REPLY 0
00058 #define ICMP_ECHO       8
00059 
00060 
00061 
00062 NyLPC_TBool NyLPC_cIPv4IComp_initialize(
00063     NyLPC_TcIPv4IComp_t* i_inst,
00064     const NyLPC_TcIPv4Config_t* i_ref_config)
00065 {
00066     i_inst->_ref_config=i_ref_config;
00067     return NyLPC_TBool_TRUE;
00068 }
00069 void NyLPC_cIPv4IComp_finalize(
00070     NyLPC_TcIPv4IComp_t* i_inst)
00071 {
00072     return;
00073 }
00074 /**
00075  * ヘッダブロックの後ろにあるIPペイロードのアドレスを返します。
00076  */
00077 #define NyLPC_TIPv4Header_getHeaderSize(i) (((i)->vhl & 0x0f)*4)
00078 
00079 void* NyLPC_cIPv4IComp_rx(
00080     const NyLPC_TcIPv4IComp_t* i_inst,
00081     const NyLPC_TcIPv4Payload_t* i_ipp)
00082 {
00083     NyLPC_TUInt16 tx_size;
00084     struct NyLPC_TIPv4Header* tx;
00085     struct NyLPC_TIcmpHeader* payload;
00086     const struct NyLPC_TIPv4Addr* my_ip=&(i_inst->_ref_config->ip_addr);
00087 
00088     if (NyLPC_TIPv4Addr_isEqual(&(i_inst->_ref_config->ip_addr),&NyLPC_TIPv4Addr_ZERO))
00089     {
00090         /* If we are configured to use ping IP address configuration and
00091          hasn't been assigned an IP address yet, we accept all ICMP
00092          packets. */
00093     } else {
00094         /* Check if the packet is destined for our IP address. */
00095         if (!NyLPC_TIPv4Addr_isEqual(&(i_ipp->header->destipaddr),my_ip))
00096         {
00097             return NyLPC_TBool_FALSE;
00098         }
00099     }
00100     // 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.
00101     if (i_ipp->payload.icmp->type != ICMP_ECHO)
00102     {
00103         return NyLPC_TBool_FALSE;
00104     }
00105     //返送パケットの取得
00106     tx=(struct NyLPC_TIPv4Header*)NyLPC_cMiMicIpNetIf_allocTxBuf(NyLPC_NTOHS(i_ipp->header->len16),&tx_size);
00107     if(tx==NULL){
00108         return NyLPC_TBool_FALSE;
00109     }
00110     //パケットサイズのチェック
00111     if(tx_size<NyLPC_NTOHS(i_ipp->header->len16)){
00112         NyLPC_cMiMicIpNetIf_releaseTxBuf(tx);
00113         return NyLPC_TBool_FALSE;
00114     }
00115     //返送パケットの構築
00116     ;
00117 
00118     //複製
00119     memcpy(tx,i_ipp->header,NyLPC_NTOHS(i_ipp->header->len16));
00120 
00121     //ペイロードの編集
00122     payload=(struct NyLPC_TIcmpHeader*)(((NyLPC_TUInt8*)tx)+NyLPC_TIPv4Header_getHeaderSize(tx));
00123     payload->type = ICMP_ECHO_REPLY;
00124     //update checksum
00125     if (payload->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) {
00126         payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1;
00127     } else {
00128         payload->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8);
00129     }
00130     //IPヘッダの編集
00131     tx->destipaddr=tx->srcipaddr;
00132     tx->srcipaddr=*my_ip;
00133 /*
00134 
00135     //チェックサムの再計算
00136     i_ipp->payload.icmp->type = ICMP_ECHO_REPLY;
00137     if (i_ipp->payload.icmp->icmpchksum >= NyLPC_HTONS(0xffff - (ICMP_ECHO << 8))) {
00138         i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8) + 1;
00139     } else {
00140         i_ipp->payload.icmp->icmpchksum += NyLPC_HTONS(ICMP_ECHO << 8);
00141     }
00142     //OUT/INアドレスの反転
00143     i_ipp->header->destipaddr=i_ipp->header->srcipaddr;
00144     i_ipp->header->srcipaddr=*my_ip;
00145 */
00146     return tx;
00147 
00148 }
00149