OSDN Git Service

Apipaクラスの追加
authornyatla <nyatla@47198e57-cb75-475f-84c4-a814cd6f29e0>
Tue, 4 Jun 2013 08:22:24 +0000 (08:22 +0000)
committernyatla <nyatla@47198e57-cb75-475f-84c4-a814cd6f29e0>
Tue, 4 Jun 2013 08:22:24 +0000 (08:22 +0000)
git-svn-id: http://svn.osdn.jp/svnroot/mimic/trunk@255 47198e57-cb75-475f-84c4-a814cd6f29e0

lib/src/include/NyLPC_net.h
lib/src/net/NyLPC_cNet.c
lib/src/net/apipa/NyLPC_cApipa.c [new file with mode: 0644]
lib/src/net/apipa/NyLPC_cApipa.h [new file with mode: 0644]
lib/src/net/dhcp/NyLPC_cDhcpClient.c
lib/src/net/dhcp/NyLPC_cDhcpClient.h
lib/src/uip/NyLPC_cUipService.c
lib/src/uip/NyLPC_cUipService_protected.h

index 1f73a6c..34021e4 100644 (file)
@@ -32,6 +32,7 @@
 #include "../net/NyLPC_cNet.h"\r
 \r
 #include "../net/mdns/NyLPC_cMDnsServer.h"\r
+#include "../net/apipa/NyLPC_cApipa.h"\r
 \r
 #include "../net/httpd/NyLPC_cHttpdConnection.h"\r
 #include "../net/httpd/NyLPC_cHttpd.h"\r
index 50b71eb..2a11e68 100644 (file)
@@ -72,25 +72,31 @@ void NyLPC_cNet_stop(NyLPC_TcNet_t* i_inst)
  */\r
 NyLPC_TBool NyLPC_cNet_requestAddrDhcp(NyLPC_TcNet_t* i_net,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat)\r
 {\r
-       NyLPC_TInt16 i;\r
        NyLPC_TBool ret;\r
        NyLPC_TcDhcpClient_t sock;\r
-       NyLPC_TcNetConfig_t c2;\r
-       //工場出荷時設定でリセットしてIPを0に\r
-       NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE);\r
-       NyLPC_cNetConfig_setIpAddr(&c2,0,0,0,0);\r
-       NyLPC_cNetConfig_setGateway(&c2,0,0,0,0);\r
        //netを開始\r
        NyLPC_cDhcpClient_initialize(&sock);\r
-       NyLPC_cNet_start(i_net,&c2);\r
-       for(i=i_repeat-1;i>=0;i--){\r
-               ret=NyLPC_cDhcpClient_dhcpRequest(&sock,i_cfg);\r
-               if(ret){\r
-                       break;\r
-               }\r
-       }\r
-       NyLPC_cNet_stop(i_net);\r
+       ret=NyLPC_cDhcpClient_requestAddr(&sock,i_cfg,i_repeat);\r
        NyLPC_cDhcpClient_finalize(&sock);\r
-       NyLPC_cNetConfig_finalize(&c2);\r
+       return ret;\r
+}\r
+/**\r
+ * NyLPC_TcIPv4Config_tをAPIPAで更新します。\r
+ * この関数をコールする時は、サービスは停止中でなければなりません。\r
+ * @param i_cfg\r
+ * 更新するi_cfg構造体。\r
+ * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。\r
+ * 更新されるフィールドは、ip,netmast,default_rootの3つです。\r
+ * @return\r
+ * 更新に成功した場合TRUE\r
+ */\r
+NyLPC_TBool NyLPC_cNet_requestAddrApipa(NyLPC_TcNet_t* i_net,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat)\r
+{\r
+       NyLPC_TBool ret;\r
+       NyLPC_TcApipa_t sock;\r
+       //netを開始\r
+       NyLPC_cApipa_initialize(&sock);\r
+       ret=NyLPC_cApipa_requestAddr(&sock,i_cfg,i_repeat);\r
+       NyLPC_cApipa_finalize(&sock);\r
        return ret;\r
 }\r
diff --git a/lib/src/net/apipa/NyLPC_cApipa.c b/lib/src/net/apipa/NyLPC_cApipa.c
new file mode 100644 (file)
index 0000000..c180008
--- /dev/null
@@ -0,0 +1,95 @@
+#include "NyLPC_cApipa.h"\r
+#include "NyLPC_uipService.h"\r
+#include "../uip/NyLPC_cUipService_protected.h"\r
+#include <stdio.h>\r
+#include <string.h>\r
+\r
+NyLPC_TcNet_t net;\r
+\r
+\r
+\r
+/**\r
+ * ARPテーブルに指定IPが現れるまで待ちます。\r
+ */\r
+static NyLPC_TBool waitForArpResponse(const struct NyLPC_TIPv4Addr* i_ip,NyLPC_TUInt32 i_wait_in_ms)\r
+{\r
+       NyLPC_TcStopwatch_t sw;\r
+       NyLPC_cStopwatch_initialize(&sw);\r
+       NyLPC_cStopwatch_startExpire(&sw,i_wait_in_ms);\r
+       while(!NyLPC_cStopwatch_isExpired(&sw)){\r
+               NyLPC_cThread_yield();\r
+               if(NyLPC_cUipService_hasArpInfo(i_ip)){\r
+                       return NyLPC_TBool_TRUE;\r
+               }\r
+       }\r
+       NyLPC_cStopwatch_finalize(&sw);\r
+       return NyLPC_TBool_FALSE;\r
+}\r
+static void makeIP(NyLPC_TcApipa_t* i_inst,struct NyLPC_TIPv4Addr* i_ip)\r
+{\r
+//     NyLPC_TIPv4Addr_set(i_ip,192,168,128,206);//for conflict test!\r
+       NyLPC_TIPv4Addr_set(i_ip,169,254,(i_inst->_seed>>8)&0xff,i_inst->_seed & 0xff);\r
+}\r
+static void updateSeed(NyLPC_TcApipa_t* i_inst)\r
+{\r
+       do{\r
+               i_inst->_seed=(391*i_inst->_seed+392);\r
+       }while(((i_inst->_seed & 0xff)==0) || ((i_inst->_seed & 0xff00)==0));\r
+}\r
+\r
+\r
+\r
+void NyLPC_cApipa_initialize(NyLPC_TcApipa_t* i_inst)\r
+{\r
+       i_inst->_seed=0;\r
+}\r
+\r
+\r
+/**\r
+ * この関数はuipを操作します。\r
+ * cNetは停止中である必要があります。\r
+ */\r
+NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat)\r
+{\r
+       int i;\r
+       NyLPC_TcIPv4Config_t cfg;\r
+       struct NyLPC_TIPv4Addr caip;\r
+\r
+       //ゼロコンフィギュレーション用のIPを設定\r
+       NyLPC_cIPv4Config_initialzeCopy(&cfg,i_cfg);\r
+       //seedを更新\r
+       for(i=0;i<6;i++)\r
+       {\r
+               i_inst->_seed+=i_cfg->eth_mac.addr[i];\r
+       }\r
+       NyLPC_cIPv4Config_setDefaultRoute(&cfg,&NyLPC_TIPv4Addr_ZERO);\r
+       for(i=i_repeat-1;i>=0;i--){\r
+               NyLPC_cIPv4Config_setIp(&cfg,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO);\r
+               updateSeed(i_inst);\r
+               makeIP(i_inst,&caip);\r
+               //startInterface\r
+               NyLPC_cUipService_start(&cfg);\r
+               NyLPC_cUipService_sendArpRequest(&caip);\r
+               //テーブル更新待ち\r
+               if(waitForArpResponse(&caip,512+(i_inst->_seed % 256))){\r
+                       NyLPC_cUipService_stop();\r
+                       continue;\r
+               }\r
+               NyLPC_cUipService_stop();\r
+               //IPのコンフリクトテスト\r
+               NyLPC_cIPv4Config_setIp(&cfg,&caip,&NyLPC_TIPv4Addr_APIPA_MASK);\r
+               NyLPC_cUipService_start(&cfg);\r
+               //!ARP送信\r
+               NyLPC_cUipService_sendArpRequest(&caip);\r
+               if(waitForArpResponse(&caip,512+(256-(i_inst->_seed % 256)))){\r
+                       //応答があったらエラー\r
+                       NyLPC_cUipService_stop();\r
+                       continue;\r
+               }\r
+               //OK\r
+               NyLPC_cUipService_stop();\r
+               NyLPC_cIPv4Config_setIp(i_cfg,&cfg.ip_addr,&cfg.netmask);\r
+               return NyLPC_TBool_TRUE;\r
+       }\r
+       return NyLPC_TBool_FALSE;\r
+}\r
diff --git a/lib/src/net/apipa/NyLPC_cApipa.h b/lib/src/net/apipa/NyLPC_cApipa.h
new file mode 100644 (file)
index 0000000..e08264d
--- /dev/null
@@ -0,0 +1,48 @@
+/*********************************************************************************\r
+ * PROJECT: MiMic\r
+ * --------------------------------------------------------------------------------\r
+ *\r
+ * This file is part of MiMic\r
+ * Copyright (C)2011-2013 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_CAPIPA_H_\r
+#define NYLPC_CAPIPA_H_\r
+#include "NyLPC_stdlib.h"\r
+typedef struct NyLPC_cApipa NyLPC_TcApipa_t;\r
+#include "NyLPC_net.h"\r
+\r
+struct NyLPC_cApipa\r
+{\r
+       NyLPC_TUInt32 _seed;\r
+};\r
+void NyLPC_cApipa_initialize(NyLPC_TcApipa_t* i_inst);\r
+\r
+#define NyLPC_cApipa_finalize(i_inst)\r
+\r
+\r
+/**\r
+ * i_cfgにAutoIPのアドレスを取得します。\r
+ * この関数はuipを操作します。uipServiceは停止中である必要があります。\r
+ */\r
+NyLPC_TBool NyLPC_cApipa_requestAddr(NyLPC_TcApipa_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat);\r
+\r
+\r
+#endif /* NYLPC_CAPIPA_H_ */\r
index 534e0f6..c3cf2e6 100644 (file)
@@ -209,7 +209,7 @@ void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst)
  * ネットワークを更新します。\r
  * emac/default_mssを設定したネットワークが必要です。\r
  */\r
-NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_sock,NyLPC_TcIPv4Config_t* i_result)\r
+static NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_sock,NyLPC_TcIPv4Config_t* i_result)\r
 {\r
        char* buf;\r
        NyLPC_TcStopwatch_t sw;\r
@@ -263,7 +263,37 @@ NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_sock,NyLPC_TcI
        return NyLPC_TBool_TRUE;\r
 }\r
 \r
-\r
+/**\r
+ * NyLPC_TcIPv4Config_tをDHCPで更新します。\r
+ * この関数をコールする時は、サービスは停止中でなければなりません。\r
+ * @param i_cfg\r
+ * 更新するi_cfg構造体。\r
+ * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。\r
+ * 更新されるフィールドは、ip,netmast,default_rootの3つです。\r
+ * @return\r
+ * 更新に成功した場合TRUE\r
+ */\r
+NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat)\r
+{\r
+       NyLPC_TInt16 i;\r
+       NyLPC_TBool ret;\r
+       NyLPC_TcIPv4Config_t c2;\r
+       //工場出荷時設定でリセットしてIPを0に\r
+       NyLPC_cIPv4Config_initialzeCopy(&c2,i_cfg);\r
+       NyLPC_cIPv4Config_setIp(&c2,&NyLPC_TIPv4Addr_ZERO,&NyLPC_TIPv4Addr_ZERO);\r
+       NyLPC_cIPv4Config_setDefaultRoute(&c2,&NyLPC_TIPv4Addr_ZERO);\r
+       //netを開始\r
+       NyLPC_cUipService_start(&c2);\r
+       for(i=i_repeat-1;i>=0;i--){\r
+               ret=NyLPC_cDhcpClient_dhcpRequest(i_inst,i_cfg);\r
+               if(ret){\r
+                       break;\r
+               }\r
+       }\r
+       NyLPC_cUipService_stop();\r
+       NyLPC_cIPv4Config_finalize(&c2);\r
+       return ret;\r
+}\r
 \r
 \r
 \r
index abc6645..2d24ad3 100644 (file)
@@ -50,10 +50,16 @@ NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst);
 void NyLPC_cDhcpClient_finalize(NyLPC_TcDhcpClient_t* i_inst);\r
 \r
 /**\r
- * ネットワークを更新します。\r
- * emac/default_mssを設定したネットワークが必要です。\r
+ * NyLPC_TcIPv4Config_tをDHCPで更新します。\r
+ * この関数をコールする時は、サービスは停止中でなければなりません。\r
+ * @param i_cfg\r
+ * 更新するi_cfg構造体。\r
+ * emac,default_mssは設定済である必要があります。他のフィールド値は不定で構いません。\r
+ * 更新されるフィールドは、ip,netmast,default_rootの3つです。\r
+ * @return\r
+ * 更新に成功した場合TRUE\r
  */\r
-NyLPC_TBool NyLPC_cDhcpClient_dhcpRequest(NyLPC_TcDhcpClient_t* i_sock,NyLPC_TcIPv4Config_t* i_result);\r
+NyLPC_TBool NyLPC_cDhcpClient_requestAddr(NyLPC_TcDhcpClient_t* i_inst,NyLPC_TcIPv4Config_t* i_cfg,NyLPC_TInt16 i_repeat);\r
 \r
 \r
 \r
index c051d36..69052f3 100644 (file)
@@ -52,6 +52,7 @@
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
  */\r
+#include "NyLPC_cUipService_protected.h"\r
 #include "NyLPC_cIPv4IComp_protected.h"\r
 #include "NyLPC_cTcpListener_protected.h"\r
 #include "NyLPC_stdlib.h"\r
index efcf6fd..fbdebda 100644 (file)
@@ -24,8 +24,8 @@
  *\r
  *********************************************************************************/\r
 \r
-#ifndef NyLPC_uipService_H\r
-#define NyLPC_uipService_H\r
+#ifndef NyLPC_uipService_protected_H\r
+#define NyLPC_uipService_protected_H\r
 #include "NyLPC_cUipService.h"\r
 #include "../driver/uip/EthDev.h"\r
 #include "../driver/uip/EthDev_LPC17xx.h"\r