#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
*/\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
--- /dev/null
+#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
--- /dev/null
+/*********************************************************************************\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
* ネットワークを更新します。\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
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
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
* 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
*\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