OSDN Git Service

APIPAの為のAPI追加
authornyatla <nyatla@47198e57-cb75-475f-84c4-a814cd6f29e0>
Mon, 3 Jun 2013 11:01:10 +0000 (11:01 +0000)
committernyatla <nyatla@47198e57-cb75-475f-84c4-a814cd6f29e0>
Mon, 3 Jun 2013 11:01:10 +0000 (11:01 +0000)
1回目のUDP送信に必ず失敗する不具合の修正

git-svn-id: http://svn.osdn.jp/svnroot/mimic/trunk@253 47198e57-cb75-475f-84c4-a814cd6f29e0

14 files changed:
lib/src/driver/uip/IEthernetDevice.h
lib/src/net/NyLPC_cNet.c
lib/src/net/NyLPC_cNet.h
lib/src/net/NyLPC_cNetConfig.c
lib/src/net/NyLPC_cNetConfig.h
lib/src/net/mdns/NyLPC_cMDnsServer.c
lib/src/uip/NyLPC_cIPv4Arp.c
lib/src/uip/NyLPC_cIPv4Config.c
lib/src/uip/NyLPC_cIPv4Config.h
lib/src/uip/NyLPC_cUdpSocket.c
lib/src/uip/NyLPC_cUipService.c
lib/src/uip/NyLPC_cUipService_protected.h
lib/src/uip/NyLPC_uip.c
lib/src/uip/NyLPC_uip.h

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