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

Fork of libMiMic by Ryo Iizuka

Files at this revision

API Documentation at this revision

Comitter:
nyatla
Date:
Thu Oct 30 14:03:31 2014 +0000
Parent:
102:41a5e92e0a8e
Child:
104:4f4781ecdc79
Commit message:
bugfix; mDNS

Changed in this revision

core/net/NyLPC_cNetConfig.h Show annotated file Show diff for this revision Revisions of this file
core/net/mdns/NyLPC_cMDnsServer.c Show annotated file Show diff for this revision Revisions of this file
--- a/core/net/NyLPC_cNetConfig.h	Tue Oct 28 12:38:10 2014 +0000
+++ b/core/net/NyLPC_cNetConfig.h	Thu Oct 30 14:03:31 2014 +0000
@@ -69,7 +69,7 @@
         NyLPC_TUInt16 padding;
     }services;
 
-    /** インタフェイス層の設定*/
+    /** インタフェイス層の設定 */
 };
 #define NyLPC_TcNetConfig_IPV4_FLAG_MODE_MASK   0x00000003
 #define NyLPC_TcNetConfig_IPV4_FLAG_MODE_MANUAL 0x00000000
--- a/core/net/mdns/NyLPC_cMDnsServer.c	Tue Oct 28 12:38:10 2014 +0000
+++ b/core/net/mdns/NyLPC_cMDnsServer.c	Thu Oct 30 14:03:31 2014 +0000
@@ -35,7 +35,7 @@
  * mDNSのポート番号
  */
 #define MDNS_MCAST_PORT         5353
-static const struct NyLPC_TIPv4Addr MDNS_MCAST_IPADDR=NyLPC_TIPv4Addr_pack(224,0,0,251);
+static const struct NyLPC_TIPv4Addr MDNS_MCAST_IPADDR = NyLPC_TIPv4Addr_pack(224, 0, 0, 251);
 #define TIMEOUT_IN_MS       1000
 #define NyLPC_TcMDns_TTL (120)    //120
 
@@ -86,14 +86,14 @@
 
 
 /**************************************************
- * TLabelCache
- **************************************************/
+* TLabelCache
+**************************************************/
 /**
  * nameフィールドの文字列圧縮を解除して圧縮後のテキストポインタを返します。
  */
 static const char* getExtractNamePos(const char* i_packet_buf, const char* i_spos)
 {
-    NyLPC_TUInt8 limit=0;
+    NyLPC_TUInt8 limit = 0;
     const char* s = i_spos;// question->buf + pos;//クエリの解析位置
     for (;;){
         switch (*(const NyLPC_TUInt8*)s){
@@ -102,7 +102,7 @@
             return NULL;
         case 0xc0:
             s = i_packet_buf + *((const NyLPC_TUInt8*)s + 1);//参照先にジャンプ
-            if (i_spos<=s){
+            if (i_spos <= s){
                 //後方参照ならエラー
                 return NULL;
             }
@@ -120,8 +120,8 @@
 }
 
 /**
-* 展開しない圧縮文字列の長さを返す
-*/
+ * 展開しない圧縮文字列の長さを返す
+ */
 static NyLPC_TInt16 sizeofCompressName(const char* i_str)
 {
     NyLPC_TInt16 l = 0;
@@ -138,9 +138,9 @@
     }
 }
 /**
-* @return
-* 圧縮するとtrue i_nameは2文字に圧縮される。
-*/
+ * @return
+ * 圧縮するとtrue i_nameは2文字に圧縮される。
+ */
 static NyLPC_TBool compressNameB(char* i_packet, NyLPC_TUInt16 i_spos, NyLPC_TUInt16 i_name_pos)
 {
     NyLPC_TUInt16 ret;
@@ -158,7 +158,7 @@
         }
         for (;;){
             //検索位置のドメインを遡る
-            if (*s == *(i_packet+i_name_pos)){
+            if (*s == *(i_packet + i_name_pos)){
                 //先頭一致
                 p = i_packet + i_name_pos;
                 s2 = getExtractNamePos(i_packet, s);
@@ -178,7 +178,7 @@
                     s2 += (*(NyLPC_TUInt8*)s2) + 1;
                     if (*p == 0 && *s2 == 0){
                         //charで扱ってるクライアントのコケ防止
-                        if(ret>0xff){
+                        if (ret>0xff){
                             return 0;
                         }
                         *(i_packet + i_name_pos + 0) = 0xc0;
@@ -195,12 +195,12 @@
     }
 }
 /**
-* 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)
+ * 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 (;;){
@@ -213,46 +213,35 @@
             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 += sizeofCompressName(i_packet + s);
+        s += 2 + 2 + 4 + 2;
+        if (i_name_start <= s){
+            //圧縮対象文字列に到達
+            return 0;
         }
-        s += NyLPC_ntohs(*((NyLPC_TUInt16*)(i_packet+s - 2)));
+        //          //データは圧縮ポインタにしない方がいいらしい。
+        //          if (compressNameB(i_packet, s, i_name_pos)){
+        //              return i_name_pos + 2;
+        //          }
+        s += NyLPC_ntohs(*((NyLPC_TUInt16*)(i_packet + s - 2)));
     }
 }
 /**
-* @return
-* 新しいi_packetの長さ
-*/
+ * @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);
+        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){
+        if (*(i_packet + s) == 0){
             //圧縮対象文字列に到達
             return (i_name_len + i_name_pos);
         }
@@ -312,7 +301,8 @@
             return NyLPC_TBool_FALSE;
         }
         s += (*s) + 1;
-    }else{
+    }
+    else{
         s = question->buf + (NyLPC_TUInt8)question->qname_pos;//クエリの解析位置
     }
     p = i_protocol;
@@ -356,7 +346,7 @@
             qlen++;
             break;
         case 0xc0:
-            qlen+=2;
+            qlen += 2;
             break;
         default:
             qlen++;
@@ -365,8 +355,8 @@
         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)));
+        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;
@@ -375,21 +365,21 @@
 /**
  * DNSレコードのPRTフィールドとDNSラベル文字列を比較する。
  */
-static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchPtrIdx(const struct NyLPC_TDnsRecord* i_struct,const struct NyLPC_TDnsQuestion* 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(NyLPC_TDnsQuestion_isEqualName(question,NULL,i_struct->srv[i].protocol)){
+    for (i = 0; i<i_struct->num_of_srv; i++){
+        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 struct NyLPC_TDnsQuestion* question)
+static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchSrvIdx(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(NyLPC_TDnsQuestion_isEqualName(question,i_struct->name,i_struct->srv[i].protocol)){
+    for (i = 0; i<i_struct->num_of_srv; i++){
+        if (NyLPC_TDnsQuestion_isEqualName(question, i_struct->name, i_struct->srv[i].protocol)){
             return i;
         }
     }
@@ -404,358 +394,394 @@
  * 変換後のデータブロックの長さin byte
  * 終端の\0の長さを含みます。
  */
-static NyLPC_TInt16 str2label(NyLPC_TChar* buf,const NyLPC_TChar* name)
+static NyLPC_TInt16 str2label(NyLPC_TChar* buf, const NyLPC_TChar* name)
 {
     //proto文字列の変換
     NyLPC_TChar* lp;
-    const NyLPC_TChar* n=name;
-    NyLPC_TChar* b=buf;
-    while(*n!='\0'){
-        lp=b;
+    const NyLPC_TChar* n = name;
+    NyLPC_TChar* b = buf;
+    while (*n != '\0'){
+        lp = b;
         b++;
-        for(;strchr(".\0",*n)==NULL;){
-            *b=*n;
+        for (; strchr(".\0", *n) == NULL;){
+            *b = *n;
             b++;
             n++;
         }
-        *lp=(char)(b-lp-1);
-        if(*n!='\0'){
+        *lp = (char)(b - lp - 1);
+        if (*n != '\0'){
             n++;
         }
     }
-    *b='\0';
+    *b = '\0';
     b++;
-    return b-buf;
+    return (NyLPC_TInt16)((int)b - (int)buf);
 }
 /**
  * query文字列をパケットに追記します。
  * @return 出力したバイト数
  */
-static NyLPC_TInt16 query2label(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* i_query)
+static NyLPC_TInt16 query2label(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query)
 {
     const char* n;
-    NyLPC_TInt16 s=0;
-    for(;;){
-        n=getExtractNamePos(i_query,i_query+s);
-        if(n==NULL){
-            *(i_packet+i_spos+s)=0;
-            s++;
+    NyLPC_TInt16 s = i_query->qname_pos;
+    NyLPC_TInt16 l = 0;//出力した文字数
+    for (;;){
+        n = getExtractNamePos(i_query->buf, i_query->buf + s);
+        if (n == NULL){
+            *(i_packet + i_spos + l) = 0;
+            l++;
             break;
-        }else{
-            memcpy(i_packet+i_spos+s,i_query+s,((NyLPC_TUInt8)*n)+1);
-            s+=((NyLPC_TUInt8)*n)+1;
-            if(obuflen<i_spos+s+1+4+4){
+        }
+        else{
+            memcpy(i_packet + i_spos + l, n, ((NyLPC_TUInt8)*n) + 1);
+            l += ((NyLPC_TUInt8)*n) + 1;
+            s = (NyLPC_TInt16)((int)n - (int)i_query->buf + ((NyLPC_TUInt8)*n) + 1);
+            if (obuflen<i_spos + l + 1){
                 return 0;
             }
         }
     }
-    return s;
+    return l;
 }
 
 
-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)
+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(buflen<i_spos+l+2+2+4){
+    NyLPC_TInt16 l = (NyLPC_TInt16)(1 + strlen(i_recode->name) + 1 + strlen(i_recode->srv[i_sid].protocol) + 1 + 5 + 1);
+    if (buflen<i_spos + l + 2 + 2 + 4){
         return 0;
     }
-    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;
+    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;
 }
 
 /**
- * パケットヘッダを書き込みます。
- * @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)
+* パケットヘッダを書き込みます。
+* @return パケットサイズ
+*/
+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*)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);
-        p->flag=p->flag & NyLPC_HTONS(~(NyLPC_TDnsHeader_FLAG_MASK_RECODE|NyLPC_TDnsHeader_FLAG_MASK_TC|NyLPC_TDnsHeader_FLAG_MASK_RA));
-    }else{
-        p->flag=0;
-        p->id=0;
+    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);
+        p->flag = p->flag & NyLPC_HTONS(~(NyLPC_TDnsHeader_FLAG_MASK_RECODE | NyLPC_TDnsHeader_FLAG_MASK_TC | NyLPC_TDnsHeader_FLAG_MASK_RA));
     }
-    p->qd=0;
-    p->an=NyLPC_HTONS(i_an_count);
-    p->ns=NyLPC_HTONS(i_ns_count);
-    p->ar=NyLPC_HTONS(i_ar_count);
+    else{
+        p->flag = 0;
+        p->id = 0;
+    }
+    p->qd = 0;
+    p->an = NyLPC_HTONS(i_an_count);
+    p->ns = NyLPC_HTONS(i_ns_count);
+    p->ar = NyLPC_HTONS(i_ar_count);
     return sizeof(struct NyLPC_TDnsHeader);
 }
 /**
- * ドメイン名からAレコードレスポンスを書きだす。
- */
-inline static NyLPC_TInt16 writeARecord(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* i_name,const struct NyLPC_TIPv4Addr* ip)
+* ドメイン名からAレコードレスポンスを書きだす。
+*/
+static NyLPC_TInt16 writeARecord(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const NyLPC_TChar* i_name, const struct NyLPC_TIPv4Addr* ip)
 {
     NyLPC_TInt16 s;
     //AnswerはAレコードのみ
-    NyLPC_TInt16 l=1+(NyLPC_TInt16)strlen(i_name)+1+5+1;
-    if(obuflen<i_spos+l+4+4){
+    NyLPC_TInt16 l = 1 + (NyLPC_TInt16)strlen(i_name) + 1 + 5 + 1;
+    if (obuflen<i_spos + l + 4 + 4){
         return 0;
     }
-    s=str2label(i_packet+i_spos,i_name)-1;
-    s+=str2label(i_packet+i_spos+s,"local");
+    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(NyLPC_TDnsQuestion_QTYPR_A);
-    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
-    l+=2+2+4;
+    l = compressName(i_packet, i_spos, s);
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_A);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    l += 2 + 2 + 4;
     //A record header
-    if(obuflen<l+6){
+    if (obuflen<l + 6){
         return 0;
     }
     //Aレコードを書く
     //IPADDR
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(4);
-    (*(NyLPC_TUInt32*)(i_packet+l+2))=ip->v;
-    return l+6;
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(4);
+    (*(NyLPC_TUInt32*)(i_packet + l + 2)) = ip->v;
+    return l + 6;
 }
 
 /**
- * AレコードクエリからAレコードレスポンスを書きだす。
- */
-static NyLPC_TInt16 writeARecordByQuery(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* i_query,const struct NyLPC_TIPv4Addr* ip)
+* AレコードクエリからAレコードレスポンスを書きだす。
+*/
+static NyLPC_TInt16 writeARecordByQuery(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query, const struct NyLPC_TIPv4Addr* ip)
 {
     NyLPC_TInt16 s;
     //AnswerはAレコードのみ
     NyLPC_TInt16 l;
-    s=query2label(i_packet,i_spos,obuflen,i_query);
-    if(s==0){
+    s = query2label(i_packet, i_spos, obuflen, i_query);
+    if (s == 0){
         return 0;
     }
 
     //レコード圧縮
-    l=compressName(i_packet,i_spos,s);
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_A);
-    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
-    l+=2+2+4;
+    l = compressName(i_packet, i_spos, s);
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_A);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    l += 2 + 2 + 4;
     //A record header
-    if(obuflen<l+6){
+    if (obuflen<l + 6){
         return 0;
     }
     //Aレコードを書く
     //IPADDR
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(4);
-    (*(NyLPC_TUInt32*)(i_packet+l+2))=ip->v;
-    return l+6;
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(4);
+    (*(NyLPC_TUInt32*)(i_packet + l + 2)) = ip->v;
+    return l + 6;
 }
 
 /**
- * AレコードクエリからAレコードレスポンスを書きだす。
- */
-static NyLPC_TInt16 writeAAAARecordByQuery(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* i_query,const struct NyLPC_TIPv4Addr* ip)
+* AレコードクエリからAレコードレスポンスを書きだす。
+*/
+static NyLPC_TInt16 writeAAAARecordByQuery(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query, const struct NyLPC_TIPv4Addr* ip)
 {
     NyLPC_TInt16 s;
     //AnswerはAレコードのみ
     NyLPC_TInt16 l;
-    s=query2label(i_packet,i_spos,obuflen,i_query);
-    if(s==0){
+    s = query2label(i_packet, i_spos, obuflen, i_query);
+    if (s == 0){
         return 0;
     }
 
     //レコード圧縮
-    l=compressName(i_packet,i_spos,s);
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_AAAA);
-    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
-    l+=2+2+4;
+    l = compressName(i_packet, i_spos, s);
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_AAAA);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    l += 2 + 2 + 4;
     //A record header
-    if(obuflen<l+2+16){
+    if (obuflen<l + 2 + 16){
+        return 0;
+    }
+    //AAAAレコードを書く
+    //IPADDR
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(16);
+    memset(i_packet + l + 2, 0, 10);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2 + 10)) = 0xffff;
+    (*(NyLPC_TUInt32*)(i_packet + l + 2 + 12)) = ip->v;
+    return l + 2 + 16;
+}
+/**
+* AレコードクエリからAレコードレスポンスを書きだす。
+*/
+static NyLPC_TInt16 writeAAAARecord(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const NyLPC_TChar* i_name, const struct NyLPC_TIPv4Addr* ip)
+{
+    NyLPC_TInt16 s;
+    //AnswerはAレコードのみ
+    NyLPC_TInt16 l = 1 + (NyLPC_TInt16)strlen(i_name) + 1 + 5 + 1;
+    if (obuflen<i_spos + l + 4 + 4){
+        return 0;
+    }
+    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(NyLPC_TDnsQuestion_QTYPR_AAAA);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    l += 2 + 2 + 4;
+    //A record header
+    if (obuflen<l + 2 + 16){
         return 0;
     }
     //AAAAレコードを書く
     //IPADDR
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(16);
-    memset(i_packet+l+2,0,10);
-    (*(NyLPC_TUInt16*)(i_packet+l+2+10))=0xffff;
-    (*(NyLPC_TUInt32*)(i_packet+l+2+12))=ip->v;
-    return l+2+16;
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(16);
+    memset(i_packet + l + 2, 0, 10);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2 + 10)) = 0xffff;
+    (*(NyLPC_TUInt32*)(i_packet + l + 2 + 12)) = ip->v;
+    return l + 2 + 16;
 }
 /**
- * NSECレコードレスポンスを書きだす。
- * IPv6わからんし。
- */
-static NyLPC_TInt16 writeNSECRecord(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* i_name)
+* NSECレコードレスポンスを書きだす。
+* IPv6わからんし。
+*/
+static NyLPC_TInt16 writeNSECRecord(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const NyLPC_TChar* i_name)
 {
     NyLPC_TInt16 s;
     //AnswerはAレコードのみ
-    NyLPC_TInt16 l=1+(NyLPC_TInt16)strlen(i_name)+1+5+1;
-    if(obuflen<i_spos+l+4+4){
+    NyLPC_TInt16 l = 1 + (NyLPC_TInt16)strlen(i_name) + 1 + 5 + 1;
+    if (obuflen<i_spos + l + 4 + 4){
         return 0;
-    }    
-    s=str2label(i_packet+i_spos,i_name)-1;
-    s+=str2label(i_packet+i_spos+s,"local");
+    }
+    s = str2label(i_packet + i_spos, i_name) - 1;
+    s += str2label(i_packet + i_spos + s, "local");
 
     //レコード圧縮
-    l=i_spos+s;//compressName(i_packet,i_spos,s);
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_NSEC);
-    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
-    l+=2+2+4;
+    l = i_spos + s;//compressName(i_packet,i_spos,s);
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_NSEC);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    l += 2 + 2 + 4;
     //A record header
-    if(obuflen<l+2+2+6){
+    if (obuflen<l + 2 + 2 + 6){
         return 0;
     }
     //NSECレコードを書く
-    *((NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(2+6);
-    l+=2;
-    *(i_packet+l)=0xc0;
-    *(i_packet+l+1)=(NyLPC_TUInt8)i_spos;
-    l+=2;
-    memcpy(i_packet+l,"\x00\x04\x00\x00\x00\x08",6);
-    return l+6;
+    *((NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(2 + 6);
+    l += 2;
+    *(i_packet + l) = 0xc0;
+    *(i_packet + l + 1) = (NyLPC_TUInt8)i_spos;
+    l += 2;
+    memcpy(i_packet + l, "\x00\x04\x00\x00\x00\x08", 6);
+    return l + 6;
 }
 /**
- * NSECレコードレスポンスを書きだす。
- * IPv6わからんし。
- */
-static NyLPC_TInt16 writeNSECRecordByQuery(char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen,const NyLPC_TChar* i_query)
+* NSECレコードレスポンスを書きだす。
+* IPv6わからんし。
+*/
+static NyLPC_TInt16 writeNSECRecordByQuery(char* i_packet, NyLPC_TInt16 i_spos, NyLPC_TInt16 obuflen, const struct NyLPC_TDnsQuestion* i_query)
 {
     NyLPC_TInt16 s;
     //AnswerはAレコードのみ
     NyLPC_TInt16 l;
-    s=query2label(i_packet,i_spos,obuflen,i_query);
-    if(s==0){
+    s = query2label(i_packet, i_spos, obuflen, i_query);
+    if (s == 0){
         return 0;
     }
 
     //レコード圧縮
-    l=i_spos+s;
-//    l=compressName(i_packet,i_spos,s);
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_NSEC);
-    (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
-    l+=2+2+4;
+    l = i_spos + s;
+    //    l=compressName(i_packet,i_spos,s);
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_NSEC);
+    (*(NyLPC_TUInt16*)(i_packet + l + 2)) = NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    (*(NyLPC_TUInt32*)(i_packet + l + 4)) = NyLPC_HTONL(NyLPC_TcMDns_TTL);
+    l += 2 + 2 + 4;
     //A record header
-    if(obuflen<l+2+2+6){
+    if (obuflen<l + 2 + 2 + 6){
         return 0;
     }
     //NSECレコードを書く
-    *((NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(2+6);
-    l+=2;
-    *(i_packet+l)=0xc0;
-    *(i_packet+l+1)=(NyLPC_TUInt8)i_spos;
-    l+=2;
-    memcpy(i_packet+l,"\x00\x04\x00\x00\x00\x08",6);
-    return l+6;
+    *((NyLPC_TUInt16*)(i_packet + l)) = NyLPC_HTONS(2 + 6);
+    l += 2;
+    *(i_packet + l) = 0xc0;
+    *(i_packet + l + 1) = (NyLPC_TUInt8)i_spos;
+    l += 2;
+    memcpy(i_packet + l, "\x00\x04\x00\x00\x00\x08", 6);
+    return l + 6;
 }
-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)
+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;
+    NyLPC_TInt16 l, s;
     //Header
-//    s=(NyLPC_TInt16)*(i_question->buf+i_question->qname_pos);
+    //    s=(NyLPC_TInt16)*(i_question->buf+i_question->qname_pos);
     //Headerの長さチェック
-    if(obuflen<i_spos+30+2+2+4){
+    if (obuflen<i_spos + 30 + 2 + 2 + 4){
         return 0;
     }
     //Header書込み
-    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;
+    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の書込み
-    s=(NyLPC_TInt16)(1+strlen(i_srvlec->protocol)+1+5+1);//逆引き文字列の長さ(デリミタ×3+1)
-    if(obuflen<s+l+2){
+    s = (NyLPC_TInt16)(1 + strlen(i_srvlec->protocol) + 1 + 5 + 1);//逆引き文字列の長さ(デリミタ×3+1)
+    if (obuflen<s + l + 2){
         return 0;
     }
-    (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_ntohs(s);
-    l+=2;
-    s=str2label(i_packet+l,i_srvlec->protocol)-1;
-    s+=str2label(i_packet+l+s,"local");
-    s=compressName(i_packet,l,s);//圧縮
+    (*(NyLPC_TUInt16*)(i_packet + l)) = NyLPC_ntohs(s);
+    l += 2;
+    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* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
+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_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);
+    s = (NyLPC_TInt16)(1 + strlen(i_recode->srv[i_sid].protocol) + 1 + 5 + 1);
     //Headerの長さチェック
-    if(obuflen<i_spos+s+2+2+4){
+    if (obuflen<i_spos + s + 2 + 2 + 4){
         return 0;
     }
     //Header書込み
-    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;
+    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の書込み
-    s=1+strlen(i_recode->name)+1+strlen(i_recode->srv[i_sid].protocol)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1)
-    if(obuflen<s+l+2){
+    s = (NyLPC_TInt16)(1 + strlen(i_recode->name) + 1 + strlen(i_recode->srv[i_sid].protocol) + 1 + 5 + 1);//逆引き文字列の長さ(デリミタ×3+1)
+    if (obuflen<s + l + 2){
         return 0;
     }
-    rlen=(NyLPC_TUInt16*)(i_packet+l);
-    l+=2;
-    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);
+    rlen = (NyLPC_TUInt16*)(i_packet + l);
+    l += 2;
+    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* i_packet,NyLPC_TUInt16 i_spos,NyLPC_TInt16 obuflen)
+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_TInt16 l, s;
     NyLPC_TUInt16* rlen;
 
     //SRV Record
-    s=writeSrvResourceHeader(i_packet,i_spos,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_SRV,NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    if(s==0){
+    s = writeSrvResourceHeader(i_packet, i_spos, obuflen, i_inst->_ref_record, i_sid, NyLPC_TDnsQuestion_QTYPR_SRV, NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    if (s == 0){
         return 0;
     }
 
-    l=1+strlen(i_inst->_ref_record->a)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1)
-    if(obuflen<s+8+l){
+    l = (NyLPC_TInt16)(1 + strlen(i_inst->_ref_record->a) + 1 + 5 + 1);//逆引き文字列の長さ(デリミタ×3+1)
+    if (obuflen<s + 8 + l){
         return 0;
     }
     //IPADDR
-    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(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));
+    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(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* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
+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(i_packet,i_spos,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_TXT,NyLPC_TDnsQuestion_QCLASS_IN|NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
-    if(ret==0){
+    ret = writeSrvResourceHeader(i_packet, i_spos, obuflen, i_inst->_ref_record, i_sid, NyLPC_TDnsQuestion_QTYPR_TXT, NyLPC_TDnsQuestion_QCLASS_IN | NyLPC_TDnsQuestion_QCLASS_CACHE_FLUSH);
+    if (ret == 0){
         return 0;
     }
     //name.proto.localを返す。
-    if(obuflen<ret+2){
+    if (obuflen<ret + 2){
         return 0;
     }
-    (*(NyLPC_TUInt16*)(i_packet+ret))=NyLPC_ntohs(0);
+    (*(NyLPC_TUInt16*)(i_packet + ret)) = NyLPC_ntohs(0);
     //proto.name.local.
-    l=ret+2;
+    l = ret + 2;
     return l;
 }
 
@@ -776,7 +802,7 @@
         if(obuf==NULL){
             return;
         }
-        l=setResponseHeader(obuf,NULL,1,0,4);
+        l=setResponseHeader(obuf,NULL,1,0,5);
         l=writePtrRecord(i_inst->_ref_record,i2,obuf,l,obuflen);
         if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
@@ -796,6 +822,11 @@
         if(l<=0){
             NyLPC_OnErrorGoto(ERROR);
         }
+        //AAAAレコード
+        l=writeAAAARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
+        if(l<=0){
+            NyLPC_OnErrorGoto(ERROR);
+        }        
         //NSEC
         l=writeNSECRecord(obuf,l,obuflen,i_inst->_ref_record->a);
         if(l<=0){
@@ -813,7 +844,7 @@
 
 
 
-static void sendReply2(NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsHeader* i_dns_header,const struct NyLPC_TDnsQuestion* q)
+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;
@@ -821,164 +852,192 @@
     NyLPC_TUInt16 obuflen;
     NyLPC_TUInt16 l;
     //パケットヘッダの生成
-    switch(q->qtype){
+    switch (q->qtype){
     case NyLPC_TDnsQuestion_QTYPR_SRV:
-        //SRV,A record
-        ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q);
-        if(ptr_recode<0){
+        //
+        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);
-        if(obuf==NULL){
+        obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0);
+        if (obuf == NULL){
             goto DROP;
         }
-        l=setResponseHeader(obuf,i_dns_header,1,0,2);
-        l=writeSRVRecord(i_inst,ptr_recode,obuf,l,obuflen);
-        if(l<=0){
+        //SRV,(TXT,A,AAAA,NSEC)
+        l = setResponseHeader(obuf, i_dns_header, 1, 0, 4);
+        l = writeSRVRecord(i_inst, ptr_recode, obuf, l, obuflen);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
-        if(l<=0){
+        l = writeTXTRecord(i_inst, ptr_recode, obuf, l, obuflen);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
         //Aレコード
-        l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(l<=0){
+        l = writeARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
+            NyLPC_OnErrorGoto(ERROR);
+        }
+        //AAAAレコード
+        l = writeAAAARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
         //NSEC
-        l=writeNSECRecord(obuf,l,obuflen,i_inst->_ref_record->a);
-        if(l<=0){
+        l = writeNSECRecord(obuf, l, obuflen, i_inst->_ref_record->a);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
-        }      
+        }
         break;
     case NyLPC_TDnsQuestion_QTYPR_AAAA:
         //自分宛?(name.local)
-        if(!NyLPC_TDnsQuestion_isEqualName(q,i_inst->_ref_record->a,"")){
+        if (!NyLPC_TDnsQuestion_isEqualName(q, i_inst->_ref_record->a, "")){
             goto DROP;
         }
         //Bufferの取得
-        obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
-        if(obuf==NULL){
+        obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0);
+        if (obuf == NULL){
             goto DROP;
         }
         //Headerのコピー
-        l=setResponseHeader(obuf,i_dns_header,1,0,1);        
+        //AAAA,(A,NSEC)
+        l = setResponseHeader(obuf, i_dns_header, 1, 0, 2);
         //AAAAレコード
-        l=writeAAAARecordByQuery(obuf,l,obuflen,q->buf+q->qname_pos,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(l<=0){
+        l = writeAAAARecordByQuery(obuf, l, obuflen, q, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
+            NyLPC_OnErrorGoto(ERROR);
+        }
+        //Aレコードのみ
+        l = writeARecordByQuery(obuf, l, obuflen, q, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
         //NSEC
-        l=writeNSECRecordByQuery(obuf,l,obuflen,q->buf+q->qname_pos);
-        if(l<=0){
+        l = writeNSECRecordByQuery(obuf, l, obuflen, q);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
         break;
     case NyLPC_TDnsQuestion_QTYPR_A:
         //自分宛?(name.local)
-        if(!NyLPC_TDnsQuestion_isEqualName(q,i_inst->_ref_record->a,"")){
+        if (!NyLPC_TDnsQuestion_isEqualName(q, i_inst->_ref_record->a, "")){
             goto DROP;
         }
         //Bufferの取得
-        obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
-        if(obuf==NULL){
+        obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0);
+        if (obuf == NULL){
             goto DROP;
         }
         //Headerのコピー
-        l=setResponseHeader(obuf,i_dns_header,1,0,1);
-        //Aレコードのみ
-        l=writeARecordByQuery(obuf,l,obuflen,q->buf+q->qname_pos,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(l<=0){
+        l = setResponseHeader(obuf, i_dns_header, 1, 0, 1);
+        //A、(NSEC
+        l = writeARecordByQuery(obuf, l, obuflen, q, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l=writeNSECRecordByQuery(obuf,l,obuflen,q->buf+q->qname_pos);
-        if(l<=0){
+        l = writeNSECRecordByQuery(obuf, l, obuflen, q);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
-        }        
+        }
         break;
     case NyLPC_TDnsQuestion_QTYPR_PTR:
         //_service._dns-sd._udpかどうか
-        if(NyLPC_TDnsQuestion_isEqualName(q,NULL,"_services._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){
+            obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0);
+            if (obuf == NULL){
                 goto DROP;
             }
-            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++){
-                l=writeSdPtrRecord(q,&(i_inst->_ref_record->srv[i2]),obuf,l,obuflen);
-                if(l<=0){
+            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++){
+                l = writeSdPtrRecord(q, &(i_inst->_ref_record->srv[i2]), obuf, l, obuflen);
+                if (l <= 0){
                     NyLPC_OnErrorGoto(ERROR);
                 }
             }
-        }else{
+        }
+        else{
             //自分宛?(proto.local)
-            ptr_recode=NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record,q);
-            if(ptr_recode<0){
+            ptr_recode = NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record, q);
+            if (ptr_recode<0){
                 goto DROP;
             }
             //Bufferの取得
-            obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
-            if(obuf==NULL){
+            obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0);
+            if (obuf == NULL){
                 goto DROP;
             }
-            l=setResponseHeader(obuf,i_dns_header,1,0,4);
-            l=writePtrRecord(i_inst->_ref_record,ptr_recode,obuf,l,obuflen);
-            if(l<=0){
+            l = setResponseHeader(obuf, i_dns_header, 1, 0, 5);
+            l = writePtrRecord(i_inst->_ref_record, ptr_recode, obuf, l, obuflen);
+            if (l <= 0){
                 NyLPC_OnErrorGoto(ERROR);
             }
-            l=writeSRVRecord(i_inst,ptr_recode,obuf,l,obuflen);
-            if(l<=0){
+            //SRV
+            l = writeSRVRecord(i_inst, ptr_recode, obuf, l, obuflen);
+            if (l <= 0){
                 NyLPC_OnErrorGoto(ERROR);
             }
-            l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
-            if(l<=0){
+            //TXT
+            l = writeTXTRecord(i_inst, ptr_recode, obuf, l, obuflen);
+            if (l <= 0){
                 NyLPC_OnErrorGoto(ERROR);
             }
             //Aレコード
-            l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-            if(l<=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=writeNSECRecord(obuf,l,obuflen,i_inst->_ref_record->a);
-            if(l<=0){
+            //AAAAレコード
+            l = writeAAAARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr));
+            if (l <= 0){
+                NyLPC_OnErrorGoto(ERROR);
+            }
+            //NSEC
+            l = writeNSECRecord(obuf, l, obuflen, i_inst->_ref_record->a);
+            if (l <= 0){
                 NyLPC_OnErrorGoto(ERROR);
             }
         }
         break;
     case NyLPC_TDnsQuestion_QTYPR_TXT:
         //自分宛?(proto.local)
-        ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q);
-        if(ptr_recode<0){
+        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,2);
-        l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
-        if(l<=0){
+        obuf = NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super), 512, &obuflen, 0);
+        l = setResponseHeader(obuf, i_dns_header, 1, 0, 3);
+        l = writeTXTRecord(i_inst, ptr_recode, obuf, l, obuflen);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
-        if(l<=0){
+        //A recoad
+        l = writeARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
-        l=writeNSECRecord(obuf,l,obuflen,i_inst->_ref_record->a);
-        if(l<=0){
+        //AAAAレコード
+        l = writeAAAARecord(obuf, l, obuflen, i_inst->_ref_record->a, &(i_inst->_super.uip_udp_conn.lipaddr));
+        if (l <= 0){
+            NyLPC_OnErrorGoto(ERROR);
+        }
+        //NSEC
+        l = writeNSECRecord(obuf, l, obuflen, i_inst->_ref_record->a);
+        if (l <= 0){
             NyLPC_OnErrorGoto(ERROR);
         }
         break;
     default:
         goto DROP;
     }
-    if(!NyLPC_cUdpSocket_psend(&(i_inst->_super),&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){
+    if (!NyLPC_cUdpSocket_psend(&(i_inst->_super), &MDNS_MCAST_IPADDR, MDNS_MCAST_PORT, obuf, l)){
         NyLPC_OnErrorGoto(ERROR);
     }
     return;
 ERROR:
-    NyLPC_cUdpSocket_releaseSendBuf(&(i_inst->_super),obuf);
+    NyLPC_cUdpSocket_releaseSendBuf(&(i_inst->_super), obuf);
 DROP:
     return;
 }