OSDN Git Service

UDPのBroadCast制限の追加、付随修正
authornyatla <nyatla@47198e57-cb75-475f-84c4-a814cd6f29e0>
Mon, 10 Jun 2013 08:08:07 +0000 (08:08 +0000)
committernyatla <nyatla@47198e57-cb75-475f-84c4-a814cd6f29e0>
Mon, 10 Jun 2013 08:08:07 +0000 (08:08 +0000)
hostname周りの修正

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

lib/src/flash/NyLPC_cMiMicConfiglation.h
lib/src/net/NyLPC_cNetConfig.c
lib/src/net/NyLPC_cNetConfig.h
lib/src/net/dhcp/NyLPC_cDhcpClient.c
lib/src/net/httpd/NyLPC_cHttpdThread.h
lib/src/net/httpd/mod/NyLPC_cModMiMicSetting.c
lib/src/uip/NyLPC_cUdpSocket.c
lib/src/uip/NyLPC_cUdpSocket.h
projects/example/sample.net.dhcp/src/sketch.c

index 9295c2d..8f39f9e 100644 (file)
@@ -6,6 +6,7 @@
  */\r
 \r
 #include "NyLPC_stdlib.h"\r
+#include "NyLPC_net.h"\r
 #ifndef NYLPC_CCONFIGLATIONSTORAGE_H_\r
 #define NYLPC_CCONFIGLATIONSTORAGE_H_\r
 \r
@@ -22,7 +23,7 @@ struct NyLPC_TMiMicConfigulation{
     /** ROM焼検出用。0xFFFFFFFFを書く */\r
     NyLPC_TUInt32   fast_boot;\r
     /** ホスト名*/\r
-    NyLPC_TChar     hostname[32];\r
+    NyLPC_TChar     hostname[NyLPC_TcNetConfig_HOSTNAME_LEN];\r
     /** MACアドレスの下位4bit*/\r
     NyLPC_TUInt32   mac_00_01_02_03;\r
     /** MACアドレスの上位2bit*/\r
index 5c514e8..1400e20 100644 (file)
@@ -104,7 +104,7 @@ static void update(NyLPC_TcNetConfig_t* i_inst,const struct NyLPC_TMiMicConfigul
        ip.v=NyLPC_htonl(pdata->ipv4_addr_net);\r
        mask.v=NyLPC_htonl(pdata->ipv4_mask_net);\r
        drt.v=NyLPC_htonl(pdata->ipv4_drut_net);\r
-       strcpy(i_inst->hostname,"MiMic");\r
+       strcpy(i_inst->hostname,pdata->hostname);\r
        NyLPC_cIPv4Config_initialzeForEthernet(&i_inst->super,&ea,ETHERNET_FRAME_LEN);\r
        NyLPC_cIPv4Config_setDefaultRoute(&i_inst->super,&drt);\r
        NyLPC_cIPv4Config_setIp(&i_inst->super,&ip,&mask);\r
index 8850e50..737fcf3 100644 (file)
@@ -92,17 +92,17 @@ void NyLPC_cNetConfig_initialize(NyLPC_TcNetConfig_t* i_inst,NyLPC_TBool i_is_fa
  */\r
 #define NyLPC_cNetConfig_finalize(i_inst);\r
 \r
-#define NyLPC_cNetConfig_setIpAddr(i_inst,ip1,ip2,ip3,ip4) NyLPC_TIPv4Addr_set(&((i_inst)->interface_setting.ethernet.ip_addr),(ip1),(ip2),(ip3),(ip4));\r
+#define NyLPC_cNetConfig_setIpAddr(i_inst,ip1,ip2,ip3,ip4) NyLPC_TIPv4Addr_set(&((i_inst)->super.ip_addr),(ip1),(ip2),(ip3),(ip4));\r
 \r
 /**\r
  * Set IPv4 network mask value to instance.\r
  */\r
-#define NyLPC_cNetConfig_setNetMask(i_inst,ip1,ip2,ip3,ip4) NyLPC_TIPv4Addr_set(&((i_inst)->interface_setting.ethernet.netmask),(ip1),(ip2),(ip3),(ip4));\r
+#define NyLPC_cNetConfig_setNetMask(i_inst,ip1,ip2,ip3,ip4) NyLPC_TIPv4Addr_set(&((i_inst)->super.netmask),(ip1),(ip2),(ip3),(ip4));\r
 \r
 /**\r
  * Set IPv4 default gateway address to instance.\r
  */\r
-#define NyLPC_cNetConfig_setGateway(i_inst,ip1,ip2,ip3,ip4) NyLPC_TIPv4Addr_set(&((i_inst)->interface_setting.ethernet.dr_addr),(ip1),(ip2),(ip3),(ip4));\r
+#define NyLPC_cNetConfig_setGateway(i_inst,ip1,ip2,ip3,ip4) NyLPC_TIPv4Addr_set(&((i_inst)->super.dr_addr),(ip1),(ip2),(ip3),(ip4));\r
 \r
 /**\r
  * Set ethernet mac address to instance.\r
index c3cf2e6..7e0598b 100644 (file)
@@ -195,6 +195,7 @@ NyLPC_TBool NyLPC_cDhcpClient_initialize(NyLPC_TcDhcpClient_t* i_inst)
        if(!NyLPC_cUdpSocket_initialize(&(i_inst->super),68,NULL,0)){\r
                return NyLPC_TBool_FALSE;\r
        }\r
+       NyLPC_cUdpSocket_setBroadcast(&(i_inst->super));\r
        NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->super),onPacket);\r
        return NyLPC_TBool_TRUE;\r
 }\r
index fe17911..ce95be9 100644 (file)
@@ -21,7 +21,7 @@ extern "C" {
 #endif\r
 \r
 /** Httpdセッションスレッドのスタックサイズ*/\r
-#define NyLPC_TcHttpdThread_SIZE_OF_THREAD_STACK 1024+128\r
+#define NyLPC_TcHttpdThread_SIZE_OF_THREAD_STACK 1024\r
 \r
 typedef struct NyLPC_TcHttpdThread NyLPC_TcHttpdThread_t;\r
 /**\r
index 62cbb27..d31dace 100644 (file)
@@ -48,15 +48,30 @@ struct TModMiMicSettingRequest
        /** 文字列のパーサ*/\r
        NyLPC_TcMiMicDbCompiler_t _binparser;\r
        union{\r
-               struct{\r
-                       /**pパラメータ。最大長さは16。\r
-                        * 詳細はNyLPC_TcModMiMicSetting_tを参照\r
+               /**\r
+                * スタックサイズ削減のための構造体。tmpは受信処理に使用。\r
+                * memimgはFlashへ書き込むときに使用。\r
+                */\r
+               union{\r
+                       /**\r
+                        * 受信用構造体。host_name,param_buf[6]までのデータは、memimgのfast_boot以降のデータ構造と位置をあわせてください。\r
+                        * param_buf[7]以降については値変換必須\r
+                        * tmpにデータを作成後にmemimgへ整形して書きこむかんじ。\r
+                        */\r
+                       struct{\r
+                               NyLPC_TUInt16 param_len;\r
+                               NyLPC_TUInt16 host_len;\r
+                               NyLPC_TChar host_name[NyLPC_TcNetConfig_HOSTNAME_LEN];\r
+                               /**pパラメータ。最大長さは16。\r
+                                * 詳細はNyLPC_TcModMiMicSetting_tを参照\r
+                                */\r
+                               NyLPC_TUInt32 param_buf[SIZE_OF_SETUP_PARAM];\r
+                               NyLPC_TUInt32 cval;//コマンド値\r
+                       }tmp;\r
+                       /**\r
+                        * 書き込み用構造体\r
                         */\r
-                       NyLPC_TUInt32 param_buf[SIZE_OF_SETUP_PARAM];\r
-                       NyLPC_TChar host_name[NyLPC_TcNetConfig_HOSTNAME_LEN];\r
-                       NyLPC_TUInt16 param_len;\r
-                       NyLPC_TUInt16 host_len;\r
-                       NyLPC_TUInt32 cval;//コマンド値\r
+                       struct NyLPC_TMiMicConfigulation memimg;\r
                }setup;\r
                struct{\r
                        /**\r
@@ -139,9 +154,9 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha
                switch(out->_content_id)\r
                {\r
                case CONTENT_ID_SETUP:\r
-                       out->content.setup.param_len=0;//クエリが無い場合の初期値\r
-                       out->content.setup.host_len =0;//クエリが無い場合の初期値\r
-                       out->content.setup.cval=QVAL_C_UNKNOWN;\r
+                       out->content.setup.tmp.param_len=0;//クエリが無い場合の初期値\r
+                       out->content.setup.tmp.host_len =0;//クエリが無い場合の初期値\r
+                       out->content.setup.tmp.cval=QVAL_C_UNKNOWN;\r
                        break;\r
                default:\r
                        break;\r
@@ -168,14 +183,14 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha
                        switch(out->_qery_name_id){\r
                        case QNAME_ID_P:\r
                                out->_astate=ST_PARSE_QUERY_VALUE_P;//MIMICBCのDBパラメータパーサを借用。\r
-                               out->content.setup.param_len=0;\r
+                               out->content.setup.tmp.param_len=0;\r
                                break;\r
                        case QNAME_ID_C:\r
                                out->_astate=ST_PARSE_QUERY_VALUE_C;\r
                                break;\r
                        case QNAME_ID_HOST:\r
                                out->_astate=ST_PARSE_QUERY_VALUE_HOST;//_host_nameに蓄積\r
-                               out->content.setup.host_len=0;\r
+                               out->content.setup.tmp.host_len=0;\r
                                break;\r
                        default:\r
                                out->_astate=ST_PARSE_QUERY_VALUE;\r
@@ -197,15 +212,15 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha
                                if(!isalnum(i_c) && i_c!='_'){\r
                                        NyLPC_OnErrorGoto(ERROR);\r
                                }\r
-                               out->content.setup.host_name[out->content.setup.host_len++]=i_c;\r
-                               if(out->content.setup.host_len>=NyLPC_TcNetConfig_HOSTNAME_LEN){\r
+                               out->content.setup.tmp.host_name[out->content.setup.tmp.host_len++]=i_c;\r
+                               if(out->content.setup.tmp.host_len>=NyLPC_TcNetConfig_HOSTNAME_LEN){\r
                                        //長すぎ\r
                                        NyLPC_OnErrorGoto(ERROR);\r
                                }\r
                                return NyLPC_TBool_TRUE;\r
                        }\r
                        //クエリ値解析完了\r
-                       out->content.setup.host_name[out->content.setup.host_len]='\0';\r
+                       out->content.setup.tmp.host_name[out->content.setup.tmp.host_len]='\0';\r
                        out->_astate=ST_PARSE_QUERY_NAME;\r
                        return NyLPC_TBool_TRUE;\r
                case ST_PARSE_QUERY_VALUE_C:\r
@@ -216,9 +231,9 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha
                                return NyLPC_TBool_TRUE;\r
                        }\r
                        if(NyLPC_cStr_isEqual(&out->_tstr,"get")){\r
-                               out->content.setup.cval=QVAL_C_GET;\r
+                               out->content.setup.tmp.cval=QVAL_C_GET;\r
                        }else if(NyLPC_cStr_isEqual(&out->_tstr,"update")){\r
-                               out->content.setup.cval=QVAL_C_UPDATE;\r
+                               out->content.setup.tmp.cval=QVAL_C_UPDATE;\r
                        }else{\r
                                NyLPC_OnErrorGoto(ERROR);\r
                        }\r
@@ -227,16 +242,16 @@ static NyLPC_TBool urlHandler(NyLPC_TcHttpBasicHeaderParser_t* i_inst,NyLPC_TCha
                        return NyLPC_TBool_TRUE;\r
                case ST_PARSE_QUERY_VALUE_P:\r
                        if(i_c!='\0' && i_c!='&'){\r
-                               if(out->content.setup.param_len>=SIZE_OF_SETUP_PARAM)\r
+                               if(out->content.setup.tmp.param_len>=SIZE_OF_SETUP_PARAM)\r
                                {\r
                                        NyLPC_OnErrorGoto(ERROR);\r
                                }\r
-                               switch(NyLPC_cMiMicDbCompiler_compileFragment2(&(out->_binparser),i_c,out->content.setup.param_buf+out->content.setup.param_len))\r
+                               switch(NyLPC_cMiMicDbCompiler_compileFragment2(&(out->_binparser),i_c,out->content.setup.tmp.param_buf+out->content.setup.tmp.param_len))\r
                                {\r
                                case NyLPC_TcMiMicDbCompiler_RET_CONTINUE:\r
                                        break;\r
                                case NyLPC_TcMiMicDbCompiler_RET_OK:\r
-                                       out->content.setup.param_len++;\r
+                                       out->content.setup.tmp.param_len++;\r
                                        break;\r
                                case NyLPC_TcMiMicDbCompiler_RET_ERROR:\r
                                default:\r
@@ -369,13 +384,12 @@ static void setup_proc(NyLPC_TcHttpdConnection_t* i_connection,struct TModMiMicS
 {\r
        NyLPC_TBool ret;\r
        const struct NyLPC_TMiMicConfigulation* config;\r
-       struct NyLPC_TMiMicConfigulation cfg_image;\r
        const NyLPC_TcNetConfig_t* currebt_cfg;\r
        NyLPC_Assert(\r
                (NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_GET)||\r
                (NyLPC_cHttpdConnection_getMethod(i_connection)==NyLPC_THttpMethodType_HEAD));\r
 \r
-       switch(i_req->content.setup.cval){\r
+       switch(i_req->content.setup.tmp.cval){\r
        case QVAL_C_GET:\r
                if(!NyLPC_cHttpdUtils_sendJsonHeader(i_connection)){\r
                        NyLPC_OnErrorGoto(Error);\r
@@ -453,29 +467,31 @@ static void setup_proc(NyLPC_TcHttpdConnection_t* i_connection,struct TModMiMicS
                break;\r
        case QVAL_C_UPDATE:\r
                //check parameter length\r
-               if(i_req->content.setup.param_len!=SIZE_OF_SETUP_PARAM || i_req->content.setup.host_len<1)\r
+               if(i_req->content.setup.tmp.param_len!=SIZE_OF_SETUP_PARAM || i_req->content.setup.tmp.host_len<1)\r
                {\r
                        NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);\r
                }else{\r
                        //パラメータ→ROMイメージ変換\r
-                       cfg_image.fast_boot=0xffffffff;\r
-                       cfg_image.mac_00_01_02_03=(i_req->content.setup.param_buf[0]);\r
-                       cfg_image.mac_04_05_xx_xx=(i_req->content.setup.param_buf[1]&0xffff0000);\r
-                       cfg_image.ipv4_flags     =i_req->content.setup.param_buf[2];\r
-                       cfg_image.ipv4_addr_net  =i_req->content.setup.param_buf[3];\r
-                       cfg_image.ipv4_mask_net  =i_req->content.setup.param_buf[4];\r
-                       cfg_image.ipv4_drut_net  =i_req->content.setup.param_buf[5];\r
-                       cfg_image.srv_flags =i_req->content.setup.param_buf[6];\r
-                       cfg_image.http_port =(NyLPC_TUInt16)(i_req->content.setup.param_buf[7]>>16);\r
-                       strcpy(cfg_image.hostname,i_req->content.setup.host_name);\r
+                       i_req->content.setup.memimg.fast_boot=0xffffffff;\r
+//                     ここの部分は受信時にデータ位置を合わせてあるのでコピー不要。\r
+//                     cfg_image.mac_00_01_02_03=(i_req->content.setup.param_buf[0]);\r
+//                     cfg_image.mac_04_05_xx_xx=(i_req->content.setup.param_buf[1]&0xffff0000);\r
+//                     cfg_image.ipv4_flags     =i_req->content.setup.param_buf[2];\r
+//                     cfg_image.ipv4_addr_net  =i_req->content.setup.param_buf[3];\r
+//                     cfg_image.ipv4_mask_net  =i_req->content.setup.param_buf[4];\r
+//                     cfg_image.ipv4_drut_net  =i_req->content.setup.param_buf[5];\r
+//                     cfg_image.srv_flags =i_req->content.setup.param_buf[6];\r
+//                     strcpy(cfg_image.hostname,i_req->content.setup.host_name);\r
+                       i_req->content.setup.memimg.http_port =(NyLPC_TUInt16)(i_req->content.setup.tmp.param_buf[7]>>16);\r
+                       i_req->content.setup.memimg.padding=0xffff;\r
                        //一応確認。\r
-                       if(cfg_image.http_port==0){\r
+                       if(i_req->content.setup.memimg.http_port==0){\r
                                NyLPC_cHttpdUtils_sendErrorResponse(i_connection,500);\r
                        }else{\r
                                //FreeRTOSの停止\r
                                NyLPC_cIsr_enterCritical();\r
                                //Flashへの書き込み\r
-                               ret=NyLPC_cMiMicConfiglation_updateConfigulation(&cfg_image);\r
+                               ret=NyLPC_cMiMicConfiglation_updateConfigulation(&i_req->content.setup.memimg);\r
                                //FreeRTOSの復帰\r
                                NyLPC_cIsr_exitCritical();\r
                                if(!ret){\r
index 516ed0d..b48fbdb 100644 (file)
 #include "NyLPC_cIPv4Payload_protected.h"\r
 #include "NyLPC_cUipService_protected.h"\r
 \r
+/**\r
+ * フラグ値\r
+ */\r
+#define NyLPC_cUdpSocket_FLAG_BROADCAST 0x01\r
+\r
 #define lockResource(i_inst) NyLPC_cMutex_lock(((i_inst)->_smutex))\r
 #define unlockResource(i_inst) NyLPC_cMutex_unlock(((i_inst)->_smutex))\r
 \r
@@ -70,6 +75,7 @@ NyLPC_TBool NyLPC_cUdpSocket_initialize(NyLPC_TcUdpSocket_t* i_inst,NyLPC_TUInt1
        i_inst->_smutex=NyLPC_cIPv4_getSockMutex(&(srv->_tcpv4));\r
        i_inst->uip_udp_conn.lport=NyLPC_htons(i_port);\r
        i_inst->uip_udp_conn.mcastaddr=NyLPC_TIPv4Addr_ZERO;\r
+       i_inst->uip_udp_conn.flags=0x00;\r
        i_inst->_handler=NULL;\r
 \r
        NyLPC_cFifoBuffer_initialize(&(i_inst->rxbuf),i_rbuf,i_rbuf_len);\r
@@ -94,6 +100,10 @@ void NyLPC_cUdpSocket_joinMulticast(NyLPC_TcUdpSocket_t* i_inst,const struct NyL
 {\r
        i_inst->uip_udp_conn.mcastaddr=*i_addr;\r
 }\r
+void NyLPC_cUdpSocket_setBroadcast(NyLPC_TcUdpSocket_t* i_inst)\r
+{\r
+       i_inst->uip_udp_conn.flags|=NyLPC_cUdpSocket_FLAG_BROADCAST;\r
+}\r
 \r
 \r
 \r
@@ -110,7 +120,12 @@ NyLPC_TBool NyLPC_cUdpSocket_parseRx(
        NyLPC_TUInt16 tmp16;\r
        struct NyLPC_TIPv4RxInfo dheader;\r
        void* data_offset;\r
-\r
+       //ブロードキャストの場合、フラグを確認\r
+       if(NyLPC_TIPv4Addr_isEqual(&(o_ipp->header->destipaddr),&NyLPC_TIPv4Addr_BROADCAST)){\r
+               if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){\r
+                       goto DROP;\r
+               }\r
+       }\r
        //パラメータの計算\r
        tmp16=NyLPC_TUdpHeader_getHeaderLength(o_ipp->payload.tcp);\r
        //UDPペイロードの長さは、IPパケットの長さ-(IPヘッダ+UDPヘッダ)\r
@@ -244,6 +259,12 @@ NyLPC_TBool NyLPC_cUdpSocket_psend(NyLPC_TcUdpSocket_t* i_inst,const struct NyLP
 {\r
        void* buf;\r
        NyLPC_TcIPv4Payload_t ipp;\r
+       //ブロードキャストの場合、フラグを確認\r
+       if(NyLPC_TIPv4Addr_isEqual(i_addr,&NyLPC_TIPv4Addr_BROADCAST)){\r
+               if(!NyLPC_TUInt8_isBitOn(i_inst->uip_udp_conn.flags,NyLPC_cUdpSocket_FLAG_BROADCAST)){\r
+                       return NyLPC_TBool_FALSE;\r
+               }\r
+       }\r
 \r
        //先頭ポインタは、i_buf-sizeof(SIZE_OF_IPv4_TCPIP_HEADER)固定\r
        buf=(NyLPC_TUInt8*)i_buf_ptr-SIZE_OF_IPv4_UDPIP_HEADER;\r
index 530430c..8c6134c 100644 (file)
@@ -16,6 +16,7 @@
 extern "C" {\r
 #endif /* __cplusplus */\r
 \r
+\r
 typedef struct NyLPC_TcUdpSocket NyLPC_TcUdpSocket_t;\r
 /**\r
  * 受信情報を格納する構造体\r
@@ -37,7 +38,8 @@ struct uip_udp_conn{
        /** マルチキャスとアドレス(ZEROで無効)*/\r
        struct NyLPC_TIPv4Addr mcastaddr;\r
        NyLPC_TUInt16 lport;        /**< The local port number in network byte order. */\r
-//     NyLPC_TUInt8  ttl;          /** 今はとりあえずデフォルトのみ*/\r
+       NyLPC_TUInt8  flags;            /**フラグ*/\r
+       NyLPC_TUInt8  padding;          /***/\r
 };\r
 \r
 /**\r
@@ -79,6 +81,11 @@ void NyLPC_cUdpSocket_finalize(NyLPC_TcUdpSocket_t* i_inst);
  */\r
 void NyLPC_cUdpSocket_joinMulticast(NyLPC_TcUdpSocket_t* i_inst,const struct NyLPC_TIPv4Addr* i_addr);\r
 \r
+/**\r
+ * ブロードキャストに参加する。\r
+ */\r
+void NyLPC_cUdpSocket_setBroadcast(NyLPC_TcUdpSocket_t* i_inst);\r
+\r
 \r
 /**\r
  * この関数は、ソケットの受信バッファの読み取り位置と、読み出せるデータサイズを返却します。\r
index 3521acb..95d9b37 100644 (file)
@@ -23,7 +23,7 @@ void loop(void)
        //デフォルト設定の呼び出し\r
        NyLPC_cNetConfig_initialize(&c2,NyLPC_TBool_TRUE);\r
        //DHCP\r
-       NyLPC_cNet_requestAddrDhcp(&net,&(c2.interface_setting.ethernet),3);\r
+       NyLPC_cNet_requestAddrDhcp(&net,&(c2.super),3);\r
        NyLPC_cNet_start(&net,&c2);\r
        //受信待ち\r
        for(;;){\r