This is Webservice SDK for mbed. LPCXpresso1769/LPC1768/FRDM-K64F/LPC4088

Dependents:   MbedFileServer_1768MiniDK2 RedWireBridge IssueDebug_gcc MiMicRemoteMCU-for-Mbed ... more

libMiMic(MiMic library for mbed)は、WebService機能を提供するSDKです。 mbedでWebAPIに対応したネットワークデバイスを簡単に作ることが出来ます。

libMiMicはMiMic projectで開発しています。MiMic projectについてはこちらをご覧ください。 http://nyatla.jp/mimic/wp/

構成

libMiMicはmbedRTOS上で動作し、ユーザアプリケーションにAPIを提供します。コアAPIはC言語で記述されていますが、使用頻度の高いものについてはmbed向けのC++APIが準備されています。

/media/uploads/nyatla/libmimic-sdk.png

※libMiMicはmbedの標準イーサネットドライバをしようしていません。

標準イーサネットドライバと同時に使用することはできません。

  • MiMicIP - IPv4スタックです。レテンシとメモリ消費量を抑えたuipベースのライブラリです。
  • ARP/ICMP/UDP/TCP - 基礎的なソケットAPIを提供します。APIは独自です。
  • HTTP/1.1 Server - HTTP/1.1に対応したサーバです。マルチセッション・Chunked・持続性接続に対応しています。
  • HTTP Modules - HTTP/1.1の機能モジュールです。以下のモジュールがあります。
    • ROM file provider - ROMに格納したファイルイメージを公開します。
    • File system provider - mbedファイルシステムを公開します。
    • Onchip configuration - プログラムフラッシュを利用して設定を保存します。
    • MiMicVM processor - RPCリクエスト(MiMicVM)を処理します。
    • FileUpload - ファイルアップロードを受け取ります。
    • URL decoder - HTTPリクエストを解析します。
    • UPnP handler -UPnPメッセージを処理します。
    • WebSocket - Websocketサーバです。
  • mDNS - マルチキャストDNSサービスです。
  • UPnP - UPnP/1.0の機能を提供します。UPnP handlerと協調して動作します。(現在はデバイス探索(SSDP)・デスクリプション(Description)のみ実装してあります。)
  • DHCP/APIPA - ゼロコンフィギュレーション用のモジュールです。
  • HTTP/1.1 Client
  • mbed C++ class library - mbed向けのC++CPIです。C言語のものより簡単です。

対応機種

  • mbed(mbed LPC1768)
  • LPCXpresso1769

プログラム

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

サンプル

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

チュートリアル

English

libMiMic(MiMic library for mbed) is SDK which provides Webservice functions. It can be created networking device easily using mbed.

See more MiMic information, See MiMic project website. http://nyatla.jp/mimic/wp/

Structure

libMiMic run on mbed RTOS and provides networking API to user application. This library has C++ class API for the mbed, and low-level C language API.

/media/uploads/nyatla/libmimic-sdk.png

For WebService general, it can be written in a simple C + + API.

libMiMic does not have the standard Ethernet driver of mbed. It is not possible that will be used with the standard Ethernet driver.

  • MiMicIP - IPv4 protocol stack. This is based uip which is reduced memory and latency.
  • ARP / ICMP / UDP / TCP - Those are provide basic IP protocols.
  • HTTP/1.1 Server - The Http server compatible HTTP/1.1. It supports multi-session, chunked transport, persistent connection.
  • HTTP Modules - There are addon-module for HTTP server. The following modules.
    • ROM file module - Publish the file images in ROM.
    • File system module - Publish thefiles in mbed file system.
    • Onchip configuration module - To save the network settings to the program flash via REST.
    • MiMicVM module - To handle the (MiMicVM) RPC request.
    • FileUpload module - Accept a file via HTTP POST.
    • URL dedoce module - A versatility URL decoder.
    • UPnP handle module - To handle UPnP messages.
    • UPnP - This provides UPnP/1.0 device functions. It works together with UPnP handler.
    • Websocket - websocket (version13) server
  • mDNS Service - DNS-SD protocol server.
  • UPnP - This provides UPnP/1.0 device functions which works with UPnP handler. (You have been implemented (SSDP) ? description only (Description) device search now.) It is a module zero configuration for - DHCP / APIPA. mbed C + + class library - C of mbed for + + is the CPI. It is simple than that of the C language.
  • DHCP/APIPA - It support zero-cpnfigulation.
  • mbed C++ class library. Almost APIs for Web applications are available.
  • HTTP/1.1 Client

Supported target

  • mbed(mbed LPC1768)
  • LPCXpresso1769

Application

Import programMiMicRemoteMCU-for-Mbed

MiMic RemoteMCU for mbed. This program provides MCU control API over REST API. It can control MCU from Javascript,PHP or any HTTP rest client directly. And, The application has self development environment.

Import programMbedFileServer

The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Sample

Import programMiMicSimpleHttpd

This is a simplest HTTP server made ​​of libMiMic. It will echo back a request path.

Import programUPnPBasicDevice

Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.

Import programWebSocketSample

MiMicSDK Websocket module sample program.

Import programHttpClientSamlpe

A http client sample program.

Import programTcpSocketClientSamlpe

MiMicSDK Tcp client socket sample program.

Import programUdpSocketSamlpe

Udp socket sample program. This program will send back the received packet.

Tutorial

Files at this revision

API Documentation at this revision

Comitter:
nyatla
Date:
Sat Oct 25 14:35:24 2014 +0000
Parent:
97:6ca5900a2d68
Child:
99:875f75e7e63e
Commit message:
bugfix; mDNS problem

Changed in this revision

core/net/mdns/NyLPC_cMDnsServer.c Show annotated file Show diff for this revision Revisions of this file
--- a/core/net/mdns/NyLPC_cMDnsServer.c	Sun Oct 19 08:44:52 2014 +0000
+++ b/core/net/mdns/NyLPC_cMDnsServer.c	Sat Oct 25 14:35:24 2014 +0000
@@ -61,10 +61,13 @@
 
 struct NyLPC_TDnsQuestion
 {
-    const char* qname;
+    const char* buf;        //Questionパケットの先頭
+    NyLPC_TUInt16 buf_len;  //パケット長さ
     NyLPC_TUInt16 qtype;
     NyLPC_TUInt16 qclass;
+    NyLPC_TInt16 qname_pos;//Qnameの開始位置
 };
+
 #define NyLPC_TDnsQuestion_QTYPR_A      1
 #define NyLPC_TDnsQuestion_QTYPR_NS     2
 #define NyLPC_TDnsQuestion_QTYPR_CNAME  5
@@ -81,46 +84,175 @@
 /**************************************************
  * TLabelCache
  **************************************************/
-
-struct TLabelCache
+/**
+ * nameフィールドの文字列圧縮を解除して圧縮後のテキストポインタを返します。
+ */
+static const char* getExtractNamePos(const char* i_packet_buf, const char* i_spos)
 {
-    NyLPC_TUInt16 idx;
-    const char* str;
-};
-static void TLabelCache_reset(struct TLabelCache* i_struct)
+    NyLPC_TUInt8 limit=0;
+    const char* s = i_spos;// question->buf + pos;//クエリの解析位置
+    for (;;){
+        switch (*(const NyLPC_TUInt8*)s){
+        case 0x00:
+            //queryが先に終了に到達するのはおかしい。
+            return NULL;
+        case 0xc0:
+            s = i_packet_buf + *((const NyLPC_TUInt8*)s + 1);//参照先にジャンプ
+            if (i_spos<=s){
+                //後方参照ならエラー
+                return NULL;
+            }
+            limit++;
+            if (limit > 32){
+                return NULL;
+            }
+            continue;
+        default:
+            break;
+        }
+        break;
+    }
+    return s;
+}
+
+/**
+* 展開しない圧縮文字列の長さを返す
+*/
+NyLPC_TInt16 sizeofCompressName(const char* i_str)
 {
-    i_struct->idx=0;
-    i_struct->str=NULL;
+    NyLPC_TInt16 l = 0;
+    NyLPC_TUInt8 c;
+    for (;;){
+        c = (NyLPC_TUInt8)*(i_str + l);
+        switch (c){
+        case 0xc0:
+            return l + 2;
+        case 0x00:
+            return l + 1;
+        }
+        l += c + 1;
+    }
 }
-static NyLPC_TInt16 TLabelCache_compress(struct TLabelCache* i_struct,NyLPC_TChar* i_src)
+/**
+* @return
+* 圧縮するとtrue i_nameは2文字に圧縮される。
+*/
+static NyLPC_TBool compressNameB(char* i_packet, NyLPC_TUInt16 i_spos, NyLPC_TUInt16 i_name_pos)
 {
-    NyLPC_TInt16 s;
-    NyLPC_TInt16 k;
-    const NyLPC_TChar* q;
-    const NyLPC_TChar* d;
-    //初めてのインデクスは保存
-    if(i_struct->idx==0){
-        i_struct->idx=12;
-        i_struct->str=i_src;
-    }else{
-        d=i_struct->str;
-        do{
-            q=i_src;
-            do{
-                if(strcmp(q,d)==0){
-                    //一致,インデクスを計算
-                    s=(NyLPC_TInt16)(q-i_src);
-                    k=(NyLPC_TInt16)(d-i_struct->str);
-                    //新しい長さを返す
-                    *((NyLPC_TInt16*)(i_src+s))=NyLPC_HTONS(0xC000|(i_struct->idx+k));
-                    return s+2;
+    NyLPC_TUInt16 ret;
+    const char* p;  //プロトコル文字列の解析開始位置
+    const char* s = i_packet + i_spos;  //クエリの解析位置
+    const char* s2;
+
+    //Protocol
+    for (;;){
+        //0xc0参照の解決
+        s = getExtractNamePos(i_packet, s);
+        if (s == NULL){
+            //一致しない
+            return 0;
+        }
+        for (;;){
+            //検索位置のドメインを遡る
+            if (*s == *(i_packet+i_name_pos)){
+                //先頭一致
+                p = i_packet + i_name_pos;
+                s2 = getExtractNamePos(i_packet, s);
+                ret = (NyLPC_TUInt16)(s2 - i_packet);
+                for (;;){
+                    //一致判定
+                    if (s2 == NULL){
+                        break;
+                    }
+
+                    if (memcmp(p, s2, (*(NyLPC_TUInt8*)s2 + 1)) != 0){
+                        //不一致
+                        break;
+                    }
+                    //検出位置の移動
+                    p += (*(NyLPC_TUInt8*)s2) + 1;
+                    s2 += (*(NyLPC_TUInt8*)s2) + 1;
+                    if (*p == 0 && *s2 == 0){
+                        //charで扱ってるクライアントのコケ防止
+                        if(ret>0xff){
+                            return 0;
+                        }
+                        *(i_packet + i_name_pos + 0) = 0xc0;
+                        *(i_packet + i_name_pos + 1) = (NyLPC_TUInt8)ret;
+                        return ret;
+                    }
+                    s2 = getExtractNamePos(i_packet, s2);
                 }
-                q+=*q+1;
-            }while(*q!=0);
-            d+=*d+1;
-        }while(*d!=0);
+            }
+            //一致しない->検索パターンの次のフラグメントを調べる
+            s += (*(NyLPC_TUInt8*)s) + 1;
+            break;
+        }
     }
-    return (NyLPC_TInt16)(strlen(i_src)+1);
+}
+/**
+* mDNSパケットからi_nameを検索する。
+* i_targetはi_packetに含まれ、NULL終端されていること
+* @return 0 圧縮失敗/その他:圧縮後のパケットサイズ
+*
+*/
+static NyLPC_TUInt16 compressNameA(char* i_packet, NyLPC_TUInt16 i_name_start,NyLPC_TUInt16 i_name_pos)
+{
+    NyLPC_TUInt16 s = 12;
+    for (;;){
+        if (i_name_start <= s){
+            //圧縮対象文字列に到達
+            return 0;
+        }
+        //i_targetとAnswer文字列が等しいか確認
+        if (compressNameB(i_packet, s, i_name_pos)){
+            return i_name_pos + 2;
+        }
+        //
+        s += sizeofCompressName(i_packet+s);
+        //CLASS PTR/SRVの場合のみデータセクションも探す
+        switch (NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet+s))){
+        case NyLPC_TDnsQuestion_QTYPR_SRV:
+            s += 2+2+2;//PTRよりちょっと多い
+        case NyLPC_TDnsQuestion_QTYPR_PTR:
+            s += 2 + 2 + 4 + 2;
+            if (i_name_start <= s){
+                //圧縮対象文字列に到達
+                return 0;
+            }
+//          //データは圧縮ポインタにしない方がいいらしい。
+//          if (compressNameB(i_packet, s, i_name_pos)){
+//              return i_name_pos + 2;
+//          }
+            break;
+        default:
+            //ヘッダとデータサイズぶんだけ移動して終わり
+            s += 2 + 2 + 4 + 2;
+            break;
+        }
+        s += NyLPC_ntohs(*((NyLPC_TUInt16*)(i_packet+s - 2)));
+    }
+}
+/**
+* @return
+* 新しいi_packetの長さ
+*/
+static NyLPC_TUInt16 compressName(char* i_packet, NyLPC_TUInt16 i_name_pos, NyLPC_TUInt16 i_name_len)
+{
+    NyLPC_TUInt16 p;
+    NyLPC_TUInt16 s = i_name_pos;
+    for (;;){
+        p = compressNameA(i_packet, i_name_pos,s);
+        //i_targetとAnswer文字列が等しいか確認
+        if (p != 0){
+            return p;
+        }
+        s += (NyLPC_TUInt16)*(i_packet + s) + 1;
+        if (*(i_packet+s) == 0){
+            //圧縮対象文字列に到達
+            return (i_name_len + i_name_pos);
+        }
+    }
 }
 
 
@@ -133,19 +265,19 @@
  * 受領可能なQuestionの数
  *
  */
-static NyLPC_TUInt16 getNumberOfQuestion(const void* i_packet,NyLPC_TUInt16 i_len)
+static NyLPC_TUInt16 getNumberOfQuestion(const void* i_packet, NyLPC_TUInt16 i_len)
 {
-    struct NyLPC_TDnsHeader* ptr=(struct NyLPC_TDnsHeader*)i_packet;
+    struct NyLPC_TDnsHeader* ptr = (struct NyLPC_TDnsHeader*)i_packet;
     NyLPC_TUInt16 t;
-    if(i_len<12){
+    if (i_len<sizeof(struct NyLPC_TDnsHeader)){
         return NyLPC_TBool_FALSE;
     }
     //questrionの確認
     //QR==0 && op==0 && tc=0
-    t=NyLPC_ntohs(ptr->flag);
-    if( ((t & NyLPC_TDnsHeader_FLAG_MASK_QR)!=0) &&
-        ((t & NyLPC_TDnsHeader_FLAG_MASK_OPCODE)!=0) &&
-        ((t & NyLPC_TDnsHeader_FLAG_MASK_TC)!=0))
+    t = NyLPC_ntohs(ptr->flag);
+    if (((t & NyLPC_TDnsHeader_FLAG_MASK_QR) != 0) &&
+        ((t & NyLPC_TDnsHeader_FLAG_MASK_OPCODE) != 0) &&
+        ((t & NyLPC_TDnsHeader_FLAG_MASK_TC) != 0))
     {
         //this is response
         return 0;
@@ -153,97 +285,113 @@
     return NyLPC_ntohs(ptr->qd);
 }
 
-static NyLPC_TInt16 NyLPC_TDnsQuestion_parse(const void* i_qpacket,NyLPC_TUInt16 i_len,struct NyLPC_TDnsQuestion* o_val)
+
+/**
+ * [i_name].[i_protocol].localをquestionと比較します。i_nameは省略ができます。
+ * @return 等しい場合true
+ */
+static NyLPC_TBool NyLPC_TDnsQuestion_isEqualName(const struct NyLPC_TDnsQuestion* question, const char* i_name, const char* i_protocol)
 {
-    NyLPC_TUInt16 l;
+    NyLPC_TUInt8 tmp;
+    const char* p;                      //プロトコル文字列の解析開始位置
+    const char* s = question->buf + (NyLPC_TUInt8)question->qname_pos; //クエリの解析位置
+
+    //Domain
+    if (i_name != NULL){
+        //0xc0参照の解決
+        s = getExtractNamePos(question->buf, s);
+        if (s == NULL){
+            return NyLPC_TBool_FALSE;
+        }
+        tmp = (NyLPC_TUInt8)strlen(i_name);
+        if (tmp != *s || memcmp(s + 1, i_name, tmp) != 0){
+            return NyLPC_TBool_FALSE;
+        }
+        s += (*s) + 1;
+    }else{
+        s = question->buf + (NyLPC_TUInt8)question->qname_pos;//クエリの解析位置
+    }
+    p = i_protocol;
+    //Protocol
+    for (;;){
+        //0xc0参照の解決
+        s = getExtractNamePos(question->buf, s);
+        if (s == NULL){
+            return NyLPC_TBool_FALSE;
+        }
+        //SRVの末端到達
+        if (*p == 0){
+            if (question->buf + question->buf_len<s + 7 + 4){
+                return NyLPC_TBool_FALSE;
+            }
+            return (memcmp("\5local\0", s, 7) == 0);
+        }
+        //有効サイズなら一致検出
+        if (question->buf + question->buf_len<s + 1 + (NyLPC_TUInt8)*s + 4){
+            return NyLPC_TBool_FALSE;
+        }
+        if (memcmp(p, s + 1, (NyLPC_TUInt8)*s) != 0){
+            //不一致
+            return NyLPC_TBool_FALSE;
+        }
+        //検出位置の移動
+        p += (*s) + 1;
+        s += (*s) + 1;
+    }
+}
+
+
+static NyLPC_TUInt16 NyLPC_TDnsQuestion_parse(const char* i_packet, NyLPC_TUInt16 i_packet_len, NyLPC_TInt16 i_parse_start, struct NyLPC_TDnsQuestion* o_val)
+{
     NyLPC_TUInt16 i;
-    const char* p=(const char*)i_qpacket;
-    //QNameのパース'0終端'
-    l=i_len-4-1;//スキャン長はパケットサイズ-5まで
-    for(i=0;i<l;i++){
-        if(*(p+i)==0){
-            l=i+1;//NULL終端を加味した文字列長
-            o_val->qname=p;
-            p+=l;
-            o_val->qtype=NyLPC_ntohs(*(NyLPC_TUInt16*)p);
-            o_val->qclass=NyLPC_ntohs(*(NyLPC_TUInt16*)(p+sizeof(NyLPC_TUInt16)));
-            //OK
-            return l+4;
+    //解析開始位置を計算
+    NyLPC_TUInt16 qlen = 0;
+    for (i = i_parse_start; i<i_packet_len - 4; i++){
+        switch ((NyLPC_TUInt8)(*(i_packet + i))){
+        case 0x00:
+            qlen++;
+            break;
+        case 0xc0:
+            qlen+=2;
+            break;
+        default:
+            qlen++;
+            continue;
         }
+        o_val->buf = i_packet;
+        o_val->buf_len = i_packet_len;
+        o_val->qname_pos = i_parse_start;
+        o_val->qtype = NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet+i_parse_start + qlen));
+        o_val->qclass = NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet+i_parse_start + qlen + sizeof(NyLPC_TUInt16)));
+        return qlen + 4;
     }
     return 0;
 }
 
 /**
- * .区切りラベル文字列とDNSラベル文字列を比較する。
- * @param record
- * .区切りのドメイン名
- * @param question
- * QuestionName文字列
- */
-inline static NyLPC_TBool isEqualName(const char* record,const char* question)
-{
-    char s;
-    const char* q=question;
-    const char* m=record;
-    for(;;){
-        if(*m=='\0'){
-            //questionは最後のフラグメントが残るはず
-            return memcmp(q,"\5local\0",7)==0;
-        }
-        s=*q;
-        switch(*q)
-        {
-        case 0x00:
-            return NyLPC_TBool_FALSE;
-        case 0xc0:
-            return NyLPC_TBool_FALSE;//offset
-        default:
-            q++;
-            if(strncmp(m,q,s)!=0){
-                return NyLPC_TBool_FALSE;
-            }
-            m+=s;
-            q+=s;
-        }
-        m++;
-    }
-}
-/**
  * DNSレコードのPRTフィールドとDNSラベル文字列を比較する。
  */
-static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchPtrIdx(const struct NyLPC_TDnsRecord* i_struct,const NyLPC_TChar* question)
+static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchPtrIdx(const struct NyLPC_TDnsRecord* i_struct,const struct NyLPC_TDnsQuestion* question)
 {
     NyLPC_TInt16 i;
     for(i=0;i<i_struct->num_of_srv;i++){
-        if(isEqualName(i_struct->srv[i].protocol,question)){
+        if(NyLPC_TDnsQuestion_isEqualName(question,NULL,i_struct->srv[i].protocol)){
             return i;
         }
     }
     return -1;
 }
-static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchSrvIdx(const struct NyLPC_TDnsRecord* i_struct,const NyLPC_TChar* question)
+static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchSrvIdx(const struct NyLPC_TDnsRecord* i_struct,const struct NyLPC_TDnsQuestion* question)
 {
     NyLPC_TInt16 i;
-    NyLPC_TInt16 l=(NyLPC_TInt16)strlen(i_struct->name);
-    //Aレコードをチェック
-    if(l!=question[0] || memcmp(question+1,i_struct->name,l)!=0){
-        return -1;
-    }
     for(i=0;i<i_struct->num_of_srv;i++){
-        if(isEqualName(i_struct->srv[i].protocol,question+l+1)){
+        if(NyLPC_TDnsQuestion_isEqualName(question,i_struct->name,i_struct->srv[i].protocol)){
             return i;
         }
     }
     return -1;
 }
-/**
- * _services._dns-sd._udp.localであるか確認する。
- */
-static NyLPC_TBool NyLPC_TDnsRecord_isServicesDnsSd(const NyLPC_TChar* question)
-{
-    return memcmp(question,"\x09_services\x07_dns-sd\x04_udp\5local\x00",30)==0;
-}
+
 
 
 /**
@@ -279,40 +427,47 @@
 /**
  * ResourceHeaderのライタ
  */
-static NyLPC_TInt16 writeResourceHeader(char* buf,NyLPC_TInt16 len,const char* i_name,NyLPC_TUInt16 i_type,NyLPC_TUInt16 i_class)
+static NyLPC_TInt16 writeResourceHeader(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 buflen,const char* i_name,NyLPC_TUInt16 i_type,NyLPC_TUInt16 i_class)
 {
     NyLPC_TInt16 s;
     NyLPC_TInt16 l=1+(NyLPC_TInt16)strlen(i_name)+1+5+1;
-    if(len<l+4+4){
+    if(buflen<i_spos+l+4+4){
         return 0;
     }
-    s=str2label(buf,i_name);
-    str2label(buf+s-1,"local");
-    (*(NyLPC_TUInt16*)(buf+l))=NyLPC_HTONS(i_type);
-    (*(NyLPC_TUInt16*)(buf+l+2))=NyLPC_HTONS(i_class);
-    (*(NyLPC_TUInt32*)(buf+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    s=str2label(i_packet+i_spos,i_name)-1;
+    s+=str2label(i_packet+i_spos+s,"local");
+    //レコード圧縮
+    l=compressName(i_packet,i_spos,s);
+    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(i_type);
+    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(i_class);
+    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
     return l+2+2+4;
 }
 
-inline static NyLPC_TInt16 writeSrvResourceHeader(char* buf,NyLPC_TInt16 len,const struct NyLPC_TDnsRecord* i_recode,int i_sid,NyLPC_TUInt16 i_type,NyLPC_TUInt16 i_class,struct TLabelCache* i_ca)
+inline static NyLPC_TInt16 writeSrvResourceHeader(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 buflen,const struct NyLPC_TDnsRecord* i_recode,int i_sid,NyLPC_TUInt16 i_type,NyLPC_TUInt16 i_class)
 {
+    NyLPC_TInt16 s;
     NyLPC_TInt16 l=(NyLPC_TInt16)(1+strlen(i_recode->name)+1+strlen(i_recode->srv[i_sid].protocol)+1+5+1);
-    if(len<l+2+2+4){
+    if(buflen<i_spos+l+2+2+4){
         return 0;
     }
-    l=str2label(buf,i_recode->name)-1;
-    l+=str2label(buf+l,i_recode->srv[i_sid].protocol)-1;
-    l+=str2label(buf+l,"local");
-    l=TLabelCache_compress(i_ca,buf);//圧縮
-    (*(NyLPC_TUInt16*)(buf+l))=NyLPC_HTONS(i_type);
-    (*(NyLPC_TUInt16*)(buf+l+2))=NyLPC_HTONS(i_class);
-    (*(NyLPC_TUInt32*)(buf+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    s=str2label(i_packet+i_spos,i_recode->name)-1;
+    s+=str2label(i_packet+i_spos+s,i_recode->srv[i_sid].protocol)-1;
+    s+=str2label(i_packet+i_spos+s,"local");
+    l=compressName(i_packet,i_spos,s);//圧縮
+    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(i_type);
+    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(i_class);
+    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
     return l+2+2+4;
 }
 
-inline static NyLPC_TUInt16 setResponseHeader(char* obuf,const struct NyLPC_TDnsHeader* i_in_dns_header,NyLPC_TUInt16 i_an_count,NyLPC_TUInt16 i_ns_count,NyLPC_TUInt16 i_ar_count)
+/**
+ * パケットヘッダを書き込みます。
+ * @return パケットサイズ
+ */
+inline static NyLPC_TUInt16 setResponseHeader(char* i_packet,const struct NyLPC_TDnsHeader* i_in_dns_header,NyLPC_TUInt16 i_an_count,NyLPC_TUInt16 i_ns_count,NyLPC_TUInt16 i_ar_count)
 {
-    struct NyLPC_TDnsHeader* p=(struct NyLPC_TDnsHeader*)obuf;
+    struct NyLPC_TDnsHeader* p=(struct NyLPC_TDnsHeader*)i_packet;
     if(i_in_dns_header!=NULL){
         memcpy(p,i_in_dns_header,sizeof(struct NyLPC_TDnsHeader));
         p->flag=p->flag | NyLPC_HTONS(NyLPC_TDnsHeader_FLAG_MASK_QR|NyLPC_TDnsHeader_FLAG_MASK_AA);
@@ -327,35 +482,38 @@
     p->ar=NyLPC_HTONS(i_ar_count);
     return sizeof(struct NyLPC_TDnsHeader);
 }
-inline static NyLPC_TInt16 writeARecord(char* obuf,NyLPC_TInt16 obuflen,const NyLPC_TChar* a_rec,const struct NyLPC_TIPv4Addr* ip)
+inline static NyLPC_TInt16 writeARecord(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* a_rec,const struct NyLPC_TIPv4Addr* ip)
 {
-    NyLPC_TInt16 ret;
+    NyLPC_TInt16 s;
     //AnswerはAレコードのみ
     //A record header
-    ret=writeResourceHeader(obuf,obuflen,a_rec,NyLPC_TDnsQuestion_QTYPR_A,NyLPC_TDnsQuestion_QCLASS_IN);
-    if(ret==0 || obuflen-ret<8){
+    s=writeResourceHeader(i_packet,i_spos,obuflen,a_rec,NyLPC_TDnsQuestion_QTYPR_A,NyLPC_TDnsQuestion_QCLASS_IN);
+    if(s==0 || obuflen<s+6){
         return 0;
     }
     //Aレコードを書く
     //IPADDR
-    (*(NyLPC_TUInt16*)(obuf+ret))=NyLPC_HTONS(4);
-    (*(NyLPC_TUInt32*)(obuf+ret+2))=ip->v;
-    return ret+8;
+    (*(NyLPC_TUInt16*)(i_packet+s))=NyLPC_HTONS(4);
+    (*(NyLPC_TUInt32*)(i_packet+s+2))=ip->v;
+    return s+6;
 }
-static NyLPC_TInt16 writeSdPtrRecord(const char* i_qname,const struct NyLPC_TMDnsServiceRecord* i_srvlec,char* obuf,NyLPC_TInt16 obuflen)
+
+
+static NyLPC_TInt16 writeSdPtrRecord(const struct NyLPC_TDnsQuestion* i_question,const struct NyLPC_TMDnsServiceRecord* i_srvlec,char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
 {
     NyLPC_TInt16 l,s;
     //Header
-    s=(NyLPC_TInt16)strlen(i_qname)+1;
+//    s=(NyLPC_TInt16)*(i_question->buf+i_question->qname_pos);
     //Headerの長さチェック
-    if(obuflen<s+2+2+4){
+    if(obuflen<i_spos+30+2+2+4){
         return 0;
     }
     //Header書込み
-    memcpy(obuf,i_qname,s);
-    (*(NyLPC_TUInt16*)(obuf+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR);
-    (*(NyLPC_TUInt16*)(obuf+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN);
-    (*(NyLPC_TUInt32*)(obuf+s+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    memcpy(i_packet+i_spos,"\x09_services\x07_dns-sd\x04_udp\x05local\x00",30);
+    s=compressName(i_packet,i_spos,30);
+    (*(NyLPC_TUInt16*)(i_packet+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR);
+    (*(NyLPC_TUInt16*)(i_packet+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN);
+    (*(NyLPC_TUInt32*)(i_packet+s+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
     l=s+2+2+4;
 
     //Resourceの書込み
@@ -363,29 +521,30 @@
     if(obuflen<s+l+2){
         return 0;
     }
-    (*(NyLPC_TUInt16*)(obuf+l))=NyLPC_ntohs(s);
+    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_ntohs(s);
     l+=2;
-    l+=str2label(obuf+l,i_srvlec->protocol)-1;
-    l+=str2label(obuf+l,"local");
-    return l;
+    s=str2label(i_packet+l,i_srvlec->protocol)-1;
+    s+=str2label(i_packet+l+s,"local");
+    s=compressName(i_packet,l,s);//圧縮
+    return s;
 }
-static NyLPC_TInt16 writePtrRecord(const struct NyLPC_TDnsRecord* i_recode,NyLPC_TInt16 i_sid,char* obuf,NyLPC_TInt16 obuflen,struct TLabelCache* i_ca)
+static NyLPC_TInt16 writePtrRecord(const struct NyLPC_TDnsRecord* i_recode,NyLPC_TInt16 i_sid,char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
 {
     NyLPC_TInt16 l,s;
     NyLPC_TUInt16* rlen;
     //Header:開始文字数(1)+プレフィクス(n)+終端(1)+local(5)+1
     s=(NyLPC_TInt16)(1+strlen(i_recode->srv[i_sid].protocol)+1+5+1);
     //Headerの長さチェック
-    if(obuflen<s+2+2+4){
+    if(obuflen<i_spos+s+2+2+4){
         return 0;
     }
     //Header書込み
-    s=str2label(obuf,i_recode->srv[i_sid].protocol)-1;
-    s+=str2label(obuf+s,"local");
-    s=TLabelCache_compress(i_ca,obuf);
-    (*(NyLPC_TUInt16*)(obuf+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR);
-    (*(NyLPC_TUInt16*)(obuf+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN);
-    (*(NyLPC_TUInt32*)(obuf+s+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    s =str2label(i_packet+i_spos,i_recode->srv[i_sid].protocol)-1;
+    s+=str2label(i_packet+i_spos+s,"local");
+    s=compressName(i_packet,i_spos,s);
+    (*(NyLPC_TUInt16*)(i_packet+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR);
+    (*(NyLPC_TUInt16*)(i_packet+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN);
+    (*(NyLPC_TUInt32*)(i_packet+s+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
     l=s+2+2+4;
 
     //Resourceの書込み
@@ -393,58 +552,57 @@
     if(obuflen<s+l+2){
         return 0;
     }
-    rlen=(NyLPC_TUInt16*)(obuf+l);
+    rlen=(NyLPC_TUInt16*)(i_packet+l);
     l+=2;
-    s=str2label(obuf+l,i_recode->name)-1;
-    s+=str2label(obuf+l+s,i_recode->srv[i_sid].protocol)-1;
-    s+=str2label(obuf+l+s,"local");
-    s=TLabelCache_compress(i_ca,obuf+l);//圧縮
-    (*rlen)=NyLPC_ntohs(s);
-    return l+s;
+    s =str2label(i_packet+l,i_recode->name)-1;
+    s+=str2label(i_packet+l+s,i_recode->srv[i_sid].protocol)-1;
+    s+=str2label(i_packet+l+s,"local");
+    s=compressName(i_packet,l,s);//圧縮
+    (*rlen)=NyLPC_ntohs(s-l);
+    return s;
 }
 
-
-static NyLPC_TInt16 writeSRVRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* obuf,NyLPC_TInt16 obuflen,struct TLabelCache* i_ca)
+static NyLPC_TInt16 writeSRVRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* i_packet,NyLPC_TUInt16 i_spos,NyLPC_TInt16 obuflen)
 {
     NyLPC_TInt16 l,s;
     NyLPC_TUInt16* rlen;
 
     //SRV Record
-    s=writeSrvResourceHeader(obuf,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_SRV,NyLPC_TDnsQuestion_QCLASS_IN,i_ca);
+    s=writeSrvResourceHeader(i_packet,i_spos,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_SRV,NyLPC_TDnsQuestion_QCLASS_IN);
     if(s==0){
         return 0;
     }
 
     l=1+strlen(i_inst->_ref_record->a)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1)
-    if(obuflen-s-l<8){
+    if(obuflen<s+8+l){
         return 0;
     }
     //IPADDR
-    rlen=(NyLPC_TUInt16*)(obuf+s);
-    (*(NyLPC_TUInt16*)(obuf+s+2))=NyLPC_HTONS(0);//Priority
-    (*(NyLPC_TUInt16*)(obuf+s+4))=NyLPC_HTONS(0);//Weight
-    (*(NyLPC_TUInt16*)(obuf+s+6))=NyLPC_HTONS(i_inst->_ref_record->srv[i_sid].port);//PORT
+    rlen=(NyLPC_TUInt16*)(i_packet+s);
+    (*(NyLPC_TUInt16*)(i_packet+s+2))=NyLPC_HTONS(0);//Priority
+    (*(NyLPC_TUInt16*)(i_packet+s+4))=NyLPC_HTONS(0);//Weight
+    (*(NyLPC_TUInt16*)(i_packet+s+6))=NyLPC_HTONS(i_inst->_ref_record->srv[i_sid].port);//PORT
     l=4*2+s;
-    s=str2label(obuf+l,i_inst->_ref_record->a)-1;
-    s+=str2label(obuf+l+s,"local");
-    s=TLabelCache_compress(i_ca,obuf+l);//圧縮
-    (*rlen)=NyLPC_HTONS(2+2+2+s);
-    return l+s;
+    s=str2label(i_packet+l,i_inst->_ref_record->a)-1;
+    s+=str2label(i_packet+l+s,"local");
+    s=compressName(i_packet,l,s);//圧縮
+    (*rlen)=NyLPC_HTONS(2+2+2+(s-l));
+    return s;
 }
-static NyLPC_TInt16 writeTXTRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* obuf,NyLPC_TInt16 obuflen,struct TLabelCache* i_ca)
+static NyLPC_TInt16 writeTXTRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
 {
     NyLPC_TInt16 ret;
     NyLPC_TInt16 l;
     //Answer
-    ret=writeSrvResourceHeader(obuf,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_TXT,NyLPC_TDnsQuestion_QCLASS_IN,i_ca);
+    ret=writeSrvResourceHeader(i_packet,i_spos,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_TXT,NyLPC_TDnsQuestion_QCLASS_IN);
     if(ret==0){
         return 0;
     }
     //name.proto.localを返す。
-    if(obuflen-ret<2){
+    if(obuflen<ret+2){
         return 0;
     }
-    (*(NyLPC_TUInt16*)(obuf+ret))=NyLPC_ntohs(0);
+    (*(NyLPC_TUInt16*)(i_packet+ret))=NyLPC_ntohs(0);
     //proto.name.local.
     l=ret+2;
     return l;
@@ -459,38 +617,32 @@
 {
     char* obuf;
     NyLPC_TUInt16 obuflen;
-    NyLPC_TUInt16 l,s;
+    NyLPC_TUInt16 l;
     int i2;
-    struct TLabelCache cache;
     for(i2=0;i2<i_inst->_ref_record->num_of_srv;i2++){
-        TLabelCache_reset(&cache);
         //Bufferの取得
         obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,TIMEOUT_IN_MS);
         if(obuf==NULL){
             return;
         }
         l=setResponseHeader(obuf,NULL,1,0,3);
-        s=writePtrRecord(i_inst->_ref_record,i2,obuf+l,obuflen-l,&cache);
-        if(s<=0){
+        l=writePtrRecord(i_inst->_ref_record,i2,obuf,l,obuflen);
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
-        s=writeSRVRecord(i_inst,i2,obuf+l,obuflen-l,&cache);
-        if(s<=0){
+        l=writeSRVRecord(i_inst,i2,obuf,l,obuflen);
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
-        s=writeTXTRecord(i_inst,i2,obuf+l,obuflen-l,&cache);
-        if(s<=0){
+        l=writeTXTRecord(i_inst,i2,obuf,l,obuflen);
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
         //Aレコード
-        s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(s<=0){
+        l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
         if(!NyLPC_cUdpSocket_psend(&(i_inst->_super),&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){
             NyLPC_OnErrorGoto(ERROR);
         }
@@ -502,20 +654,19 @@
 }
 
 
+
 static void sendReply2(NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsHeader* i_dns_header,const struct NyLPC_TDnsQuestion* q)
 {
     NyLPC_TInt16 ptr_recode;
     NyLPC_TInt16 i2;
     char* obuf;
     NyLPC_TUInt16 obuflen;
-    NyLPC_TUInt16 l,s;
-    struct TLabelCache cache;
-    TLabelCache_reset(&cache);
+    NyLPC_TUInt16 l;
     //パケットヘッダの生成
     switch(q->qtype){
     case NyLPC_TDnsQuestion_QTYPR_SRV:
         //SRV,A record
-        ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q->qname);
+        ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q);
         if(ptr_recode<0){
             goto DROP;
         }
@@ -525,26 +676,23 @@
             goto DROP;
         }
         l=setResponseHeader(obuf,i_dns_header,1,0,2);
-        s=writeSRVRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache);
-        if(s<=0){
+        l=writeSRVRecord(i_inst,ptr_recode,obuf,l,obuflen);
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
-        s=writeTXTRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache);
-        if(s<=0){
+        l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
         //Aレコード
-        s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(s<=0){
+        l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
         break;
     case NyLPC_TDnsQuestion_QTYPR_A:
         //自分宛?(name.local)
-        if(!isEqualName(i_inst->_ref_record->a,q->qname)){
+        if(!NyLPC_TDnsQuestion_isEqualName(q,i_inst->_ref_record->a,"")){
             goto DROP;
         }
         //Bufferの取得
@@ -555,14 +703,14 @@
         //Headerのコピー
         l=setResponseHeader(obuf,i_dns_header,1,0,0);
         //Aレコードのみ
-        s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(s<=0){
+        l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
         break;
     case NyLPC_TDnsQuestion_QTYPR_PTR:
-        if(NyLPC_TDnsRecord_isServicesDnsSd(q->qname)){
+        //_service._dns-sd._udpかどうか
+        if(NyLPC_TDnsQuestion_isEqualName(q,NULL,"_services._dns-sd._udp")){
             //Bufferの取得
             obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
             if(obuf==NULL){
@@ -570,15 +718,14 @@
             }
             l=setResponseHeader(obuf,i_dns_header,i_inst->_ref_record->num_of_srv,0,0);
             for(i2=0;i2<i_inst->_ref_record->num_of_srv;i2++){
-                s=writeSdPtrRecord(q->qname,&(i_inst->_ref_record->srv[i2]),obuf+l,obuflen-l);
-                if(s<=0){
+                l=writeSdPtrRecord(q,&(i_inst->_ref_record->srv[i2]),obuf,l,obuflen);
+                if(l<=0){
                     NyLPC_OnErrorGoto(ERROR);
                 }
-                l+=s;
             }
         }else{
             //自分宛?(proto.local)
-            ptr_recode=NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record,q->qname);
+            ptr_recode=NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record,q);
             if(ptr_recode<0){
                 goto DROP;
             }
@@ -588,48 +735,42 @@
                 goto DROP;
             }
             l=setResponseHeader(obuf,i_dns_header,1,0,3);
-            s=writePtrRecord(i_inst->_ref_record,ptr_recode,obuf+l,obuflen-l,&cache);
-            if(s<=0){
+            l=writePtrRecord(i_inst->_ref_record,ptr_recode,obuf,l,obuflen);
+            if(l<=0){
                 NyLPC_OnErrorGoto(ERROR);
             }
-            l+=s;
-            s=writeSRVRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache);
-            if(s<=0){
+            l=writeSRVRecord(i_inst,ptr_recode,obuf,l,obuflen);
+            if(l<=0){
                 NyLPC_OnErrorGoto(ERROR);
             }
-            l+=s;
-            s=writeTXTRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache);
-            if(s<=0){
+            l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
+            if(l<=0){
                 NyLPC_OnErrorGoto(ERROR);
             }
-            l+=s;
             //Aレコード
-            s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-            if(s<=0){
+            l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
+            if(l<=0){
                 NyLPC_OnErrorGoto(ERROR);
             }
-            l+=s;
         }
         break;
     case NyLPC_TDnsQuestion_QTYPR_TXT:
         //自分宛?(proto.local)
-        ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q->qname);
+        ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q);
         if(ptr_recode<0){
             goto DROP;
         }
         //Bufferの取得
         obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
         l=setResponseHeader(obuf,i_dns_header,1,0,1);
-        s=writeTXTRecord(i_inst,ptr_recode,obuf+l,obuflen-l,&cache);
-        if(s<=0){
+        l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
-        s=writeARecord(obuf+l,obuflen-l,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(s<=0){
+        l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
+        if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l+=s;
         break;
     default:
         goto DROP;
@@ -650,9 +791,8 @@
 
 static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info)
 {
-    NyLPC_TUInt16 in_len=i_info->size;
+    NyLPC_TUInt16 in_len;
     NyLPC_TUInt16 num_of_query;
-    const char* qptr;
     struct NyLPC_TDnsQuestion q;
     NyLPC_TUInt16 s;
     NyLPC_TInt16 i;
@@ -661,20 +801,19 @@
         return NyLPC_TBool_FALSE;
     }
 
-    num_of_query=getNumberOfQuestion(i_buf,in_len);
+    num_of_query=getNumberOfQuestion(i_buf,i_info->size);
     if(num_of_query==0){
         goto DROP;
     }
-    qptr=(const char*)i_buf+sizeof(struct NyLPC_TDnsHeader);
-    in_len-=sizeof(struct NyLPC_TDnsHeader);
+    in_len=sizeof(struct NyLPC_TDnsHeader);
     for(i=0;i<num_of_query;i++){
         //Queryのパース
-        s=NyLPC_TDnsQuestion_parse(qptr,in_len,&q);
+
+        s=NyLPC_TDnsQuestion_parse(i_buf,i_info->size,in_len,&q);
         if(s==0){
             goto DROP;
         }
-        qptr+=s;
-        in_len-=s;
+        in_len+=s;
         sendReply2((NyLPC_TcMDnsServer_t*)i_inst,(const struct NyLPC_TDnsHeader*)i_buf,&q);
     }
     //パケット処理終了