From 9a1dc6c89980c92375ece0e2d3c067249b1a17fd Mon Sep 17 00:00:00 2001 From: nyatla Date: Mon, 3 Jun 2013 11:01:10 +0000 Subject: [PATCH] =?utf8?q?APIPA=E3=81=AE=E7=82=BA=E3=81=AEAPI=E8=BF=BD?= =?utf8?q?=E5=8A=A0=201=E5=9B=9E=E7=9B=AE=E3=81=AEUDP=E9=80=81=E4=BF=A1?= =?utf8?q?=E3=81=AB=E5=BF=85=E3=81=9A=E5=A4=B1=E6=95=97=E3=81=99=E3=82=8B?= =?utf8?q?=E4=B8=8D=E5=85=B7=E5=90=88=E3=81=AE=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.osdn.jp/svnroot/mimic/trunk@253 47198e57-cb75-475f-84c4-a814cd6f29e0 --- lib/src/driver/uip/IEthernetDevice.h | 2 ++ lib/src/net/NyLPC_cNet.c | 25 ++++++++++++++++ lib/src/net/NyLPC_cNet.h | 25 ++++++++++++++++ lib/src/net/NyLPC_cNetConfig.c | 25 ++++++++++++++++ lib/src/net/NyLPC_cNetConfig.h | 25 ++++++++++++++++ lib/src/net/mdns/NyLPC_cMDnsServer.c | 29 ++++++++++++++++--- lib/src/uip/NyLPC_cIPv4Arp.c | 9 +----- lib/src/uip/NyLPC_cIPv4Config.c | 5 +++- lib/src/uip/NyLPC_cIPv4Config.h | 5 ++++ lib/src/uip/NyLPC_cUdpSocket.c | 6 +++- lib/src/uip/NyLPC_cUipService.c | 48 ++++++++++++++++++++++++++++--- lib/src/uip/NyLPC_cUipService_protected.h | 20 ++++++++++++- lib/src/uip/NyLPC_uip.c | 2 +- lib/src/uip/NyLPC_uip.h | 5 ++-- 14 files changed, 209 insertions(+), 22 deletions(-) diff --git a/lib/src/driver/uip/IEthernetDevice.h b/lib/src/driver/uip/IEthernetDevice.h index 7445e46..18d50e7 100644 --- a/lib/src/driver/uip/IEthernetDevice.h +++ b/lib/src/driver/uip/IEthernetDevice.h @@ -75,6 +75,8 @@ typedef void (*NyLPC_TiEthernetDevice_releaseTxBuf)(struct NyLPC_TTxBufferHeader * allocTxBufで得たメモリか、初期化したNyLPC_TTxBufferHeaderメモリブロックを指定する。 * 送信が終わるまでの間、メモリを開放してはならない。 * #外部で確保したメモリについては、利用不能なケースがあるかもしれない。現在のMiMicでは、使用できることを前提としている。 + * @oaram i_size + * i_bufの後ろに連結されているデータメモリの長さ */ typedef void (*NyLPC_TiEthernetDevice_sendTxEthFrame)(struct NyLPC_TTxBufferHeader* i_buf,unsigned short i_size); /** diff --git a/lib/src/net/NyLPC_cNet.c b/lib/src/net/NyLPC_cNet.c index c12ce09..50b71eb 100644 --- a/lib/src/net/NyLPC_cNet.c +++ b/lib/src/net/NyLPC_cNet.c @@ -1,3 +1,28 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ #include "NyLPC_cNet.h" #include "dhcp/NyLPC_cDhcpClient.h" diff --git a/lib/src/net/NyLPC_cNet.h b/lib/src/net/NyLPC_cNet.h index 79dc663..4d2993c 100644 --- a/lib/src/net/NyLPC_cNet.h +++ b/lib/src/net/NyLPC_cNet.h @@ -1,3 +1,28 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ #ifndef NYLPC_CNET_H_ #define NYLPC_CNET_H_ diff --git a/lib/src/net/NyLPC_cNetConfig.c b/lib/src/net/NyLPC_cNetConfig.c index 6a302e5..f9ac38f 100644 --- a/lib/src/net/NyLPC_cNetConfig.c +++ b/lib/src/net/NyLPC_cNetConfig.c @@ -1,3 +1,28 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ #include "NyLPC_cNetConfig.h" #include "../flash/NyLPC_cMiMicConfiglation.h" diff --git a/lib/src/net/NyLPC_cNetConfig.h b/lib/src/net/NyLPC_cNetConfig.h index 8918a7c..27080d5 100644 --- a/lib/src/net/NyLPC_cNetConfig.h +++ b/lib/src/net/NyLPC_cNetConfig.h @@ -1,3 +1,28 @@ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ #ifndef NYLPC_CNETCONFIG_H_ #define NYLPC_CNETCONFIG_H_ diff --git a/lib/src/net/mdns/NyLPC_cMDnsServer.c b/lib/src/net/mdns/NyLPC_cMDnsServer.c index 584bb87..79c3471 100644 --- a/lib/src/net/mdns/NyLPC_cMDnsServer.c +++ b/lib/src/net/mdns/NyLPC_cMDnsServer.c @@ -1,7 +1,28 @@ -/** - * httpdサーバのサンプルです。 - * ROMにある定義済みファイルの入出力と、クロスドメインRESTが確認できます。 - */ +/********************************************************************************* + * PROJECT: MiMic + * -------------------------------------------------------------------------------- + * + * This file is part of MiMic + * Copyright (C)2011 Ryo Iizuka + * + * MiMic is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * For further information please contact. + * http://nyatla.jp/ + * or + * + *********************************************************************************/ #include "NyLPC_cMDnsServer.h" #include "NyLPC_uipService.h" #include "NyLPC_http.h" diff --git a/lib/src/uip/NyLPC_cIPv4Arp.c b/lib/src/uip/NyLPC_cIPv4Arp.c index f4ea891..4659a94 100644 --- a/lib/src/uip/NyLPC_cIPv4Arp.c +++ b/lib/src/uip/NyLPC_cIPv4Arp.c @@ -234,18 +234,11 @@ NyLPC_TBool NyLPC_cIPv4Arp_incomingArp(NyLPC_TcIPv4Arp_t* i_inst,struct NyLPC_TA const struct NyLPC_TEthAddr* NyLPC_cIPv4Arp_IPv4toEthAddr(NyLPC_TcIPv4Arp_t* i_inst,const struct NyLPC_TIPv4Addr i_ip_addr) { int i; - struct NyLPC_TIPv4Addr ip; struct NyLPC_TArpTableItem *tabptr; - //LocalIPでなければ、デフォルトゲートウェイのアドレスに設定。 - if (!NyLPC_cIPv4Config_isLocalIP(i_inst->_cfg, &i_ip_addr)){ - ip = i_inst->_cfg->dr_addr; - } else { - ip = i_ip_addr; - } //ARPテーブルから検索 for (i = NyLPC_TcIPv4Arp_ARPTAB_SIZE - 1; i >= 0; i--) { tabptr = &i_inst->arp_table[i]; - if (NyLPC_TIPv4Addr_isEqual(&ip,&(tabptr->ipaddr))) { + if (NyLPC_TIPv4Addr_isEqual(&i_ip_addr,&(tabptr->ipaddr))) { return &tabptr->ethaddr; } } diff --git a/lib/src/uip/NyLPC_cIPv4Config.c b/lib/src/uip/NyLPC_cIPv4Config.c index 87d444b..71c2e67 100644 --- a/lib/src/uip/NyLPC_cIPv4Config.c +++ b/lib/src/uip/NyLPC_cIPv4Config.c @@ -41,7 +41,10 @@ void NyLPC_cIPv4Config_initialzeForEthernet(NyLPC_TcIPv4Config_t* i_inst,const s i_inst->default_mss=i_ether_frame_len-(UIP_ETHERHEADER_LEN+UIP_TCPH_LEN + UIP_IPH_LEN); return; } - +void NyLPC_cIPv4Config_initialzeCopy(NyLPC_TcIPv4Config_t* i_inst,const NyLPC_TcIPv4Config_t* i_src) +{ + memcpy(i_inst,i_src,sizeof(NyLPC_TcIPv4Config_t)); +} /** * See header file. */ diff --git a/lib/src/uip/NyLPC_cIPv4Config.h b/lib/src/uip/NyLPC_cIPv4Config.h index e3b50b6..b9424c1 100644 --- a/lib/src/uip/NyLPC_cIPv4Config.h +++ b/lib/src/uip/NyLPC_cIPv4Config.h @@ -82,6 +82,11 @@ struct NyLPC_TcIPv4Config */ void NyLPC_cIPv4Config_initialzeForEthernet(NyLPC_TcIPv4Config_t* i_inst,const struct NyLPC_TEthAddr* i_ether_addr,NyLPC_TUInt16 i_ether_frame_len); +/** + * コピーコンストラクタ + */ +void NyLPC_cIPv4Config_initialzeCopy(NyLPC_TcIPv4Config_t* i_inst,const NyLPC_TcIPv4Config_t* i_src); + /** * デストラクタです。インスタンスを破棄して、確保している動的リソースを元に戻します。 diff --git a/lib/src/uip/NyLPC_cUdpSocket.c b/lib/src/uip/NyLPC_cUdpSocket.c index 8293930..dfc2d40 100644 --- a/lib/src/uip/NyLPC_cUdpSocket.c +++ b/lib/src/uip/NyLPC_cUdpSocket.c @@ -257,7 +257,11 @@ NyLPC_TBool NyLPC_cUdpSocket_psend(NyLPC_TcUdpSocket_t* i_inst,const struct NyLP NyLPC_cIPv4Payload_setUdpTxHeaderByConnection(&ipp,&(i_inst->uip_udp_conn),i_addr,i_port); NyLPC_cIPv4Payload_closeUdpTxPacket(&ipp); unlockResource(i_inst); - // @bug 初めての相手に対して1回目の送信は必ず失敗する。どうする? + // @bug 初めての相手に対してなら1度ARPを送っておく + if(!NyLPC_cUipService_hasArpInfo(i_addr)){ + NyLPC_cUipService_sendArpRequest(i_addr); + NyLPC_cThread_sleep(30); + } NyLPC_cUipService_sendIPv4Tx(buf); NyLPC_cUipService_releaseTxBuf(buf); NyLPC_cIPv4Payload_finalize(&ipp); diff --git a/lib/src/uip/NyLPC_cUipService.c b/lib/src/uip/NyLPC_cUipService.c index bc09fec..c051d36 100644 --- a/lib/src/uip/NyLPC_cUipService.c +++ b/lib/src/uip/NyLPC_cUipService.c @@ -266,6 +266,8 @@ static void emacIsrHandler(unsigned long i_status) #define PERIODIC_TIMER (1*200) #define ARP_TIMER (60*1000*10) + + /** * 操作キューを確認して、タスクのステータスをアップデートします。 * 高速化のため、Proc-Callerを使用していません。複雑なタスク操作をするときには、書き換えてください。 @@ -435,8 +437,40 @@ void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload) NyLPC_cMutex_unlock(&(inst->_mutex)); return; } +/** + * 指定したIPアドレスに対してARPRequestを送信します。 + */ +void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr) +{ + void* buf; + struct TEthPacket* ethbuf; + NyLPC_TUInt16 s; + NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; + NyLPC_TUInt16 tx_len; + do{ + buf=NyLPC_cUipService_allocTxBuf(64,&s); + NyLPC_cThread_sleep(10); + }while(buf==NULL); + //IPパケットのアドレスをイーサネットパケットアドレスまで後退させる。 + ethbuf=(struct TEthPacket*)(((struct NyLPC_TEthernetIIHeader*)buf)-1); + //ARPパケットを作る。 + NyLPC_TArpHeader_setArpRequest(&(ethbuf->data.arp),inst->_ref_config->ip_addr,&(inst->_ref_config->eth_mac),*i_addr); + tx_len=NyLPC_TEthernetIIHeader_setArpTx(&(ethbuf->header),&(inst->_ref_config->eth_mac)); + //送信 + NyLPC_cMutex_lock(&(inst->_mutex)); + inst->_ethif->sendTxEthFrame(((struct NyLPC_TTxBufferHeader*)ethbuf)-1,tx_len); + NyLPC_cMutex_unlock(&(inst->_mutex)); + return; +} - +/** + * ARPテーブルに指定したIPがあるかを返します。 + */ +NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr) +{ + NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; + return NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,*i_addr)!=NULL; +} /** @@ -445,14 +479,17 @@ void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload) * @param i_hint * 取得したいメモリサイズを指定します。(このサイズは、イーサネットヘッダのサイズを含みません。) * このサイズよりも小さなサイズが割り当てられることがあります。 + * @param o_size + * イーサネットヘッダを除いたペイロード部分の長さ * @return - * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL + * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL + * 返されるメモリはブロックの[TEthPacket][payload]の構造で、[payload]のアドレスが返されます。 */ void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size) { - //排他処理をして、メモリを取得する。 NyLPC_TcUipService_t* inst=_NyLPC_TcUipService_inst; struct NyLPC_TTxBufferHeader* ethbuf; + //排他処理をして、メモリを取得する。 NyLPC_cMutex_lock(&(inst->_mutex)); ethbuf=(struct NyLPC_TTxBufferHeader*)inst->_ethif->allocTxBuf(i_hint+sizeof(struct NyLPC_TEthernetIIHeader),o_size); NyLPC_cMutex_unlock(&(inst->_mutex)); @@ -594,7 +631,10 @@ static NyLPC_TBool sendIPv4Tx(struct NyLPC_TTxBufferHeader* i_eth_buf) ip2MulticastEmacAddr(&(ethbuf->data.ipv4.destipaddr),&emac); eth_dest=&emac; }else{ - eth_dest=NyLPC_cIPv4Arp_IPv4toEthAddr(&inst->_arp,ethbuf->data.ipv4.destipaddr); + //LocalIP以外ならdefaultRootへ変換 + eth_dest=NyLPC_cIPv4Arp_IPv4toEthAddr( + &inst->_arp, + NyLPC_cIPv4Config_isLocalIP(inst->_ref_config, &(ethbuf->data.ipv4.destipaddr))?(ethbuf->data.ipv4.destipaddr):(inst->_ref_config->dr_addr)); //IP->MAC変換をテスト。 if(eth_dest==NULL){ return NyLPC_TBool_FALSE; diff --git a/lib/src/uip/NyLPC_cUipService_protected.h b/lib/src/uip/NyLPC_cUipService_protected.h index ba8b92c..efcf6fd 100644 --- a/lib/src/uip/NyLPC_cUipService_protected.h +++ b/lib/src/uip/NyLPC_cUipService_protected.h @@ -114,10 +114,14 @@ void NyLPC_cUipService_sendIPv4Tx(void* i_eth_payload); /** * 送信ペイロードメモリを返します。 * この関数は、リエントラントを許容します。 - * 取得したメモリは、releaseTxBufで必ず開放してください。 * @param i_hint * 取得したいメモリサイズを指定します。(このサイズは、イーサネットヘッダのサイズを含みません。) * このサイズよりも小さなサイズが割り当てられることがあります。 + * @param o_size + * 取得メモリのイーサネットヘッダを除いたペイロード部分の長さ + * @return + * 成功:IPペイロードのためのメモリブロックを返します。/失敗:NULL + * メモリは、[TEthPacket][payload]の構造で返されます。 */ void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); @@ -126,9 +130,23 @@ void* NyLPC_cUipService_allocTxBuf(NyLPC_TUInt16 i_hint,NyLPC_TUInt16* o_size); */ void* NyLPC_cUipService_releaseTxBuf(void* i_buf); +/** + * 指定したIPアドレスを要求するARPリクエストを発行します。 + */ +void NyLPC_cUipService_sendArpRequest(const struct NyLPC_TIPv4Addr* i_addr); + +/** + * ARPテーブルに指定したIPがあるかを返します。 + */ +NyLPC_TBool NyLPC_cUipService_hasArpInfo(const struct NyLPC_TIPv4Addr* i_addr); + + void NyLPC_cTcpSocket_periodic( NyLPC_TcTcpSocket_t* i_inst); + + + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/lib/src/uip/NyLPC_uip.c b/lib/src/uip/NyLPC_uip.c index 8cf49b0..f60d126 100644 --- a/lib/src/uip/NyLPC_uip.c +++ b/lib/src/uip/NyLPC_uip.c @@ -61,7 +61,7 @@ const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL ={0xffffffff}; const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST = { 0xffffffff }; const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST = NyLPC_TIPv4Addr_pack(224,0,0,0); const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK = NyLPC_TIPv4Addr_pack(224,0,0,0); - +const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK = NyLPC_TIPv4Addr_pack(255,255,0,0); NyLPC_TUInt16 NyLPC_uip_chksum(NyLPC_TUInt16 sum, const NyLPC_TUInt8 *data, NyLPC_TUInt16 len) diff --git a/lib/src/uip/NyLPC_uip.h b/lib/src/uip/NyLPC_uip.h index 1729769..1700134 100644 --- a/lib/src/uip/NyLPC_uip.h +++ b/lib/src/uip/NyLPC_uip.h @@ -95,6 +95,7 @@ extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_ALL; extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_BROADCAST; extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST; extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK; +extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_APIPA_MASK; /** * addr1とaddr2が全く同じであるかをテストします。 @@ -115,8 +116,8 @@ extern const struct NyLPC_TIPv4Addr NyLPC_TIPv4Addr_MULTICAST_MASK; NyLPC_TIpv4Addr_set(&ip,1,2,3,4); \endcode */ -#define NyLPC_TIPv4Addr_set(s,a0,a1,a2,a3) (s)->v=NyLPC_htonl((0xff000000&(a0<<24))|(0x00ff0000&(a1<<16))|(0x0000ff00&(a2<<8))|(0x000000ff&(a3))) -#define NyLPC_TIPv4Addr_pack(a0,a1,a2,a3) {NyLPC_HTONL((0xff000000&(a0<<24))|(0x00ff0000&(a1<<16))|(0x0000ff00&(a2<<8))|(0x000000ff&(a3)))} +#define NyLPC_TIPv4Addr_set(s,a0,a1,a2,a3) (s)->v=NyLPC_htonl((0xff000000&((a0)<<24))|(0x00ff0000&((a1)<<16))|(0x0000ff00&((a2)<<8))|(0x000000ff&(a3))) +#define NyLPC_TIPv4Addr_pack(a0,a1,a2,a3) {NyLPC_HTONL((0xff000000&((a0)<<24))|(0x00ff0000&((a1)<<16))|(0x0000ff00&((a2)<<8))|(0x000000ff&(a3)))} -- 2.11.0