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

Committer:
nyatla
Date:
Sat Oct 25 14:35:24 2014 +0000
Revision:
98:6284ce9a0476
Parent:
97:6ca5900a2d68
Child:
101:5022828ace54
bugfix; mDNS problem

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nyatla 37:fc4b4fd6a649 1 /*********************************************************************************
nyatla 37:fc4b4fd6a649 2 * PROJECT: MiMic
nyatla 37:fc4b4fd6a649 3 * --------------------------------------------------------------------------------
nyatla 37:fc4b4fd6a649 4 *
nyatla 37:fc4b4fd6a649 5 * This file is part of MiMic
nyatla 37:fc4b4fd6a649 6 * Copyright (C)2011 Ryo Iizuka
nyatla 37:fc4b4fd6a649 7 *
nyatla 37:fc4b4fd6a649 8 * MiMic is free software: you can redistribute it and/or modify
nyatla 37:fc4b4fd6a649 9 * it under the terms of the GNU Lesser General Public License as published
nyatla 37:fc4b4fd6a649 10 * by the Free Software Foundation, either version 3 of the License, or
nyatla 37:fc4b4fd6a649 11 * (at your option) any later version.
nyatla 37:fc4b4fd6a649 12 *
nyatla 37:fc4b4fd6a649 13 * This program is distributed in the hope that it will be useful,
nyatla 37:fc4b4fd6a649 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nyatla 37:fc4b4fd6a649 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nyatla 37:fc4b4fd6a649 16 * GNU General Public License for more details.
nyatla 37:fc4b4fd6a649 17 *
nyatla 37:fc4b4fd6a649 18 * You should have received a copy of the GNU Lesser General Public License
nyatla 37:fc4b4fd6a649 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
nyatla 37:fc4b4fd6a649 20 *
nyatla 37:fc4b4fd6a649 21 * For further information please contact.
nyatla 37:fc4b4fd6a649 22 * http://nyatla.jp/
nyatla 37:fc4b4fd6a649 23 * <airmail(at)ebony.plala.or.jp> or <nyatla(at)nyatla.jp>
nyatla 37:fc4b4fd6a649 24 *
nyatla 37:fc4b4fd6a649 25 *********************************************************************************/
nyatla 37:fc4b4fd6a649 26 #include "NyLPC_cMDnsServer.h"
nyatla 37:fc4b4fd6a649 27 #include "NyLPC_uipService.h"
nyatla 37:fc4b4fd6a649 28 #include "NyLPC_http.h"
nyatla 37:fc4b4fd6a649 29 #include "NyLPC_utils.h"
nyatla 37:fc4b4fd6a649 30 #include <stdio.h>
nyatla 37:fc4b4fd6a649 31 #include <string.h>
nyatla 37:fc4b4fd6a649 32
nyatla 37:fc4b4fd6a649 33
nyatla 37:fc4b4fd6a649 34 /**
nyatla 37:fc4b4fd6a649 35 * mDNSのポート番号
nyatla 37:fc4b4fd6a649 36 */
nyatla 37:fc4b4fd6a649 37 #define MDNS_MCAST_PORT 5353
nyatla 37:fc4b4fd6a649 38 static const struct NyLPC_TIPv4Addr MDNS_MCAST_IPADDR=NyLPC_TIPv4Addr_pack(224,0,0,251);
nyatla 37:fc4b4fd6a649 39 #define TIMEOUT_IN_MS 1000
nyatla 96:e6a0db86988e 40 #define NyLPC_TcMDns_TTL (30*60) //30min
nyatla 37:fc4b4fd6a649 41
nyatla 37:fc4b4fd6a649 42
nyatla 37:fc4b4fd6a649 43 struct NyLPC_TDnsHeader
nyatla 37:fc4b4fd6a649 44 {
nyatla 37:fc4b4fd6a649 45 NyLPC_TUInt16 id;
nyatla 37:fc4b4fd6a649 46 NyLPC_TUInt16 flag;
nyatla 37:fc4b4fd6a649 47 NyLPC_TUInt16 qd;
nyatla 37:fc4b4fd6a649 48 NyLPC_TUInt16 an;
nyatla 37:fc4b4fd6a649 49 NyLPC_TUInt16 ns;
nyatla 37:fc4b4fd6a649 50 NyLPC_TUInt16 ar;
nyatla 37:fc4b4fd6a649 51 }PACK_STRUCT_END;
nyatla 37:fc4b4fd6a649 52
nyatla 37:fc4b4fd6a649 53 #define NyLPC_TDnsHeader_FLAG_MASK_QR 0x8000
nyatla 37:fc4b4fd6a649 54 #define NyLPC_TDnsHeader_FLAG_MASK_OPCODE 0x7800
nyatla 37:fc4b4fd6a649 55 #define NyLPC_TDnsHeader_FLAG_MASK_AA 0x0400
nyatla 37:fc4b4fd6a649 56 #define NyLPC_TDnsHeader_FLAG_MASK_TC 0x0200
nyatla 37:fc4b4fd6a649 57 #define NyLPC_TDnsHeader_FLAG_MASK_RD 0x0100
nyatla 37:fc4b4fd6a649 58 #define NyLPC_TDnsHeader_FLAG_MASK_RA 0x0080
nyatla 37:fc4b4fd6a649 59 #define NyLPC_TDnsHeader_FLAG_MASK_Z 0x0070
nyatla 37:fc4b4fd6a649 60 #define NyLPC_TDnsHeader_FLAG_MASK_RECODE 0x000F
nyatla 37:fc4b4fd6a649 61
nyatla 37:fc4b4fd6a649 62 struct NyLPC_TDnsQuestion
nyatla 37:fc4b4fd6a649 63 {
nyatla 98:6284ce9a0476 64 const char* buf; //Questionパケットの先頭
nyatla 98:6284ce9a0476 65 NyLPC_TUInt16 buf_len; //パケット長さ
nyatla 37:fc4b4fd6a649 66 NyLPC_TUInt16 qtype;
nyatla 37:fc4b4fd6a649 67 NyLPC_TUInt16 qclass;
nyatla 98:6284ce9a0476 68 NyLPC_TInt16 qname_pos;//Qnameの開始位置
nyatla 37:fc4b4fd6a649 69 };
nyatla 98:6284ce9a0476 70
nyatla 37:fc4b4fd6a649 71 #define NyLPC_TDnsQuestion_QTYPR_A 1
nyatla 37:fc4b4fd6a649 72 #define NyLPC_TDnsQuestion_QTYPR_NS 2
nyatla 37:fc4b4fd6a649 73 #define NyLPC_TDnsQuestion_QTYPR_CNAME 5
nyatla 37:fc4b4fd6a649 74 #define NyLPC_TDnsQuestion_QTYPR_SOA 6
nyatla 37:fc4b4fd6a649 75 #define NyLPC_TDnsQuestion_QTYPR_PTR 12
nyatla 37:fc4b4fd6a649 76 #define NyLPC_TDnsQuestion_QTYPR_MX 15
nyatla 37:fc4b4fd6a649 77 #define NyLPC_TDnsQuestion_QTYPR_TXT 16
nyatla 37:fc4b4fd6a649 78 #define NyLPC_TDnsQuestion_QTYPR_ANY 255
nyatla 37:fc4b4fd6a649 79 #define NyLPC_TDnsQuestion_QCLASS_IN 1
nyatla 37:fc4b4fd6a649 80 #define NyLPC_TDnsQuestion_QCLASS_CH 3
nyatla 37:fc4b4fd6a649 81 #define NyLPC_TDnsQuestion_QCLASS_HS 4
nyatla 37:fc4b4fd6a649 82 #define NyLPC_TDnsQuestion_QTYPR_SRV 33
nyatla 37:fc4b4fd6a649 83
nyatla 37:fc4b4fd6a649 84 /**************************************************
nyatla 37:fc4b4fd6a649 85 * TLabelCache
nyatla 37:fc4b4fd6a649 86 **************************************************/
nyatla 98:6284ce9a0476 87 /**
nyatla 98:6284ce9a0476 88 * nameフィールドの文字列圧縮を解除して圧縮後のテキストポインタを返します。
nyatla 98:6284ce9a0476 89 */
nyatla 98:6284ce9a0476 90 static const char* getExtractNamePos(const char* i_packet_buf, const char* i_spos)
nyatla 37:fc4b4fd6a649 91 {
nyatla 98:6284ce9a0476 92 NyLPC_TUInt8 limit=0;
nyatla 98:6284ce9a0476 93 const char* s = i_spos;// question->buf + pos;//クエリの解析位置
nyatla 98:6284ce9a0476 94 for (;;){
nyatla 98:6284ce9a0476 95 switch (*(const NyLPC_TUInt8*)s){
nyatla 98:6284ce9a0476 96 case 0x00:
nyatla 98:6284ce9a0476 97 //queryが先に終了に到達するのはおかしい。
nyatla 98:6284ce9a0476 98 return NULL;
nyatla 98:6284ce9a0476 99 case 0xc0:
nyatla 98:6284ce9a0476 100 s = i_packet_buf + *((const NyLPC_TUInt8*)s + 1);//参照先にジャンプ
nyatla 98:6284ce9a0476 101 if (i_spos<=s){
nyatla 98:6284ce9a0476 102 //後方参照ならエラー
nyatla 98:6284ce9a0476 103 return NULL;
nyatla 98:6284ce9a0476 104 }
nyatla 98:6284ce9a0476 105 limit++;
nyatla 98:6284ce9a0476 106 if (limit > 32){
nyatla 98:6284ce9a0476 107 return NULL;
nyatla 98:6284ce9a0476 108 }
nyatla 98:6284ce9a0476 109 continue;
nyatla 98:6284ce9a0476 110 default:
nyatla 98:6284ce9a0476 111 break;
nyatla 98:6284ce9a0476 112 }
nyatla 98:6284ce9a0476 113 break;
nyatla 98:6284ce9a0476 114 }
nyatla 98:6284ce9a0476 115 return s;
nyatla 98:6284ce9a0476 116 }
nyatla 98:6284ce9a0476 117
nyatla 98:6284ce9a0476 118 /**
nyatla 98:6284ce9a0476 119 * 展開しない圧縮文字列の長さを返す
nyatla 98:6284ce9a0476 120 */
nyatla 98:6284ce9a0476 121 NyLPC_TInt16 sizeofCompressName(const char* i_str)
nyatla 37:fc4b4fd6a649 122 {
nyatla 98:6284ce9a0476 123 NyLPC_TInt16 l = 0;
nyatla 98:6284ce9a0476 124 NyLPC_TUInt8 c;
nyatla 98:6284ce9a0476 125 for (;;){
nyatla 98:6284ce9a0476 126 c = (NyLPC_TUInt8)*(i_str + l);
nyatla 98:6284ce9a0476 127 switch (c){
nyatla 98:6284ce9a0476 128 case 0xc0:
nyatla 98:6284ce9a0476 129 return l + 2;
nyatla 98:6284ce9a0476 130 case 0x00:
nyatla 98:6284ce9a0476 131 return l + 1;
nyatla 98:6284ce9a0476 132 }
nyatla 98:6284ce9a0476 133 l += c + 1;
nyatla 98:6284ce9a0476 134 }
nyatla 37:fc4b4fd6a649 135 }
nyatla 98:6284ce9a0476 136 /**
nyatla 98:6284ce9a0476 137 * @return
nyatla 98:6284ce9a0476 138 * 圧縮するとtrue i_nameは2文字に圧縮される。
nyatla 98:6284ce9a0476 139 */
nyatla 98:6284ce9a0476 140 static NyLPC_TBool compressNameB(char* i_packet, NyLPC_TUInt16 i_spos, NyLPC_TUInt16 i_name_pos)
nyatla 37:fc4b4fd6a649 141 {
nyatla 98:6284ce9a0476 142 NyLPC_TUInt16 ret;
nyatla 98:6284ce9a0476 143 const char* p; //プロトコル文字列の解析開始位置
nyatla 98:6284ce9a0476 144 const char* s = i_packet + i_spos; //クエリの解析位置
nyatla 98:6284ce9a0476 145 const char* s2;
nyatla 98:6284ce9a0476 146
nyatla 98:6284ce9a0476 147 //Protocol
nyatla 98:6284ce9a0476 148 for (;;){
nyatla 98:6284ce9a0476 149 //0xc0参照の解決
nyatla 98:6284ce9a0476 150 s = getExtractNamePos(i_packet, s);
nyatla 98:6284ce9a0476 151 if (s == NULL){
nyatla 98:6284ce9a0476 152 //一致しない
nyatla 98:6284ce9a0476 153 return 0;
nyatla 98:6284ce9a0476 154 }
nyatla 98:6284ce9a0476 155 for (;;){
nyatla 98:6284ce9a0476 156 //検索位置のドメインを遡る
nyatla 98:6284ce9a0476 157 if (*s == *(i_packet+i_name_pos)){
nyatla 98:6284ce9a0476 158 //先頭一致
nyatla 98:6284ce9a0476 159 p = i_packet + i_name_pos;
nyatla 98:6284ce9a0476 160 s2 = getExtractNamePos(i_packet, s);
nyatla 98:6284ce9a0476 161 ret = (NyLPC_TUInt16)(s2 - i_packet);
nyatla 98:6284ce9a0476 162 for (;;){
nyatla 98:6284ce9a0476 163 //一致判定
nyatla 98:6284ce9a0476 164 if (s2 == NULL){
nyatla 98:6284ce9a0476 165 break;
nyatla 98:6284ce9a0476 166 }
nyatla 98:6284ce9a0476 167
nyatla 98:6284ce9a0476 168 if (memcmp(p, s2, (*(NyLPC_TUInt8*)s2 + 1)) != 0){
nyatla 98:6284ce9a0476 169 //不一致
nyatla 98:6284ce9a0476 170 break;
nyatla 98:6284ce9a0476 171 }
nyatla 98:6284ce9a0476 172 //検出位置の移動
nyatla 98:6284ce9a0476 173 p += (*(NyLPC_TUInt8*)s2) + 1;
nyatla 98:6284ce9a0476 174 s2 += (*(NyLPC_TUInt8*)s2) + 1;
nyatla 98:6284ce9a0476 175 if (*p == 0 && *s2 == 0){
nyatla 98:6284ce9a0476 176 //charで扱ってるクライアントのコケ防止
nyatla 98:6284ce9a0476 177 if(ret>0xff){
nyatla 98:6284ce9a0476 178 return 0;
nyatla 98:6284ce9a0476 179 }
nyatla 98:6284ce9a0476 180 *(i_packet + i_name_pos + 0) = 0xc0;
nyatla 98:6284ce9a0476 181 *(i_packet + i_name_pos + 1) = (NyLPC_TUInt8)ret;
nyatla 98:6284ce9a0476 182 return ret;
nyatla 98:6284ce9a0476 183 }
nyatla 98:6284ce9a0476 184 s2 = getExtractNamePos(i_packet, s2);
nyatla 37:fc4b4fd6a649 185 }
nyatla 98:6284ce9a0476 186 }
nyatla 98:6284ce9a0476 187 //一致しない->検索パターンの次のフラグメントを調べる
nyatla 98:6284ce9a0476 188 s += (*(NyLPC_TUInt8*)s) + 1;
nyatla 98:6284ce9a0476 189 break;
nyatla 98:6284ce9a0476 190 }
nyatla 37:fc4b4fd6a649 191 }
nyatla 98:6284ce9a0476 192 }
nyatla 98:6284ce9a0476 193 /**
nyatla 98:6284ce9a0476 194 * mDNSパケットからi_nameを検索する。
nyatla 98:6284ce9a0476 195 * i_targetはi_packetに含まれ、NULL終端されていること
nyatla 98:6284ce9a0476 196 * @return 0 圧縮失敗/その他:圧縮後のパケットサイズ
nyatla 98:6284ce9a0476 197 *
nyatla 98:6284ce9a0476 198 */
nyatla 98:6284ce9a0476 199 static NyLPC_TUInt16 compressNameA(char* i_packet, NyLPC_TUInt16 i_name_start,NyLPC_TUInt16 i_name_pos)
nyatla 98:6284ce9a0476 200 {
nyatla 98:6284ce9a0476 201 NyLPC_TUInt16 s = 12;
nyatla 98:6284ce9a0476 202 for (;;){
nyatla 98:6284ce9a0476 203 if (i_name_start <= s){
nyatla 98:6284ce9a0476 204 //圧縮対象文字列に到達
nyatla 98:6284ce9a0476 205 return 0;
nyatla 98:6284ce9a0476 206 }
nyatla 98:6284ce9a0476 207 //i_targetとAnswer文字列が等しいか確認
nyatla 98:6284ce9a0476 208 if (compressNameB(i_packet, s, i_name_pos)){
nyatla 98:6284ce9a0476 209 return i_name_pos + 2;
nyatla 98:6284ce9a0476 210 }
nyatla 98:6284ce9a0476 211 //
nyatla 98:6284ce9a0476 212 s += sizeofCompressName(i_packet+s);
nyatla 98:6284ce9a0476 213 //CLASS PTR/SRVの場合のみデータセクションも探す
nyatla 98:6284ce9a0476 214 switch (NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet+s))){
nyatla 98:6284ce9a0476 215 case NyLPC_TDnsQuestion_QTYPR_SRV:
nyatla 98:6284ce9a0476 216 s += 2+2+2;//PTRよりちょっと多い
nyatla 98:6284ce9a0476 217 case NyLPC_TDnsQuestion_QTYPR_PTR:
nyatla 98:6284ce9a0476 218 s += 2 + 2 + 4 + 2;
nyatla 98:6284ce9a0476 219 if (i_name_start <= s){
nyatla 98:6284ce9a0476 220 //圧縮対象文字列に到達
nyatla 98:6284ce9a0476 221 return 0;
nyatla 98:6284ce9a0476 222 }
nyatla 98:6284ce9a0476 223 // //データは圧縮ポインタにしない方がいいらしい。
nyatla 98:6284ce9a0476 224 // if (compressNameB(i_packet, s, i_name_pos)){
nyatla 98:6284ce9a0476 225 // return i_name_pos + 2;
nyatla 98:6284ce9a0476 226 // }
nyatla 98:6284ce9a0476 227 break;
nyatla 98:6284ce9a0476 228 default:
nyatla 98:6284ce9a0476 229 //ヘッダとデータサイズぶんだけ移動して終わり
nyatla 98:6284ce9a0476 230 s += 2 + 2 + 4 + 2;
nyatla 98:6284ce9a0476 231 break;
nyatla 98:6284ce9a0476 232 }
nyatla 98:6284ce9a0476 233 s += NyLPC_ntohs(*((NyLPC_TUInt16*)(i_packet+s - 2)));
nyatla 98:6284ce9a0476 234 }
nyatla 98:6284ce9a0476 235 }
nyatla 98:6284ce9a0476 236 /**
nyatla 98:6284ce9a0476 237 * @return
nyatla 98:6284ce9a0476 238 * 新しいi_packetの長さ
nyatla 98:6284ce9a0476 239 */
nyatla 98:6284ce9a0476 240 static NyLPC_TUInt16 compressName(char* i_packet, NyLPC_TUInt16 i_name_pos, NyLPC_TUInt16 i_name_len)
nyatla 98:6284ce9a0476 241 {
nyatla 98:6284ce9a0476 242 NyLPC_TUInt16 p;
nyatla 98:6284ce9a0476 243 NyLPC_TUInt16 s = i_name_pos;
nyatla 98:6284ce9a0476 244 for (;;){
nyatla 98:6284ce9a0476 245 p = compressNameA(i_packet, i_name_pos,s);
nyatla 98:6284ce9a0476 246 //i_targetとAnswer文字列が等しいか確認
nyatla 98:6284ce9a0476 247 if (p != 0){
nyatla 98:6284ce9a0476 248 return p;
nyatla 98:6284ce9a0476 249 }
nyatla 98:6284ce9a0476 250 s += (NyLPC_TUInt16)*(i_packet + s) + 1;
nyatla 98:6284ce9a0476 251 if (*(i_packet+s) == 0){
nyatla 98:6284ce9a0476 252 //圧縮対象文字列に到達
nyatla 98:6284ce9a0476 253 return (i_name_len + i_name_pos);
nyatla 98:6284ce9a0476 254 }
nyatla 98:6284ce9a0476 255 }
nyatla 37:fc4b4fd6a649 256 }
nyatla 37:fc4b4fd6a649 257
nyatla 37:fc4b4fd6a649 258
nyatla 37:fc4b4fd6a649 259
nyatla 37:fc4b4fd6a649 260
nyatla 37:fc4b4fd6a649 261
nyatla 37:fc4b4fd6a649 262 /**
nyatla 37:fc4b4fd6a649 263 * 受領可能なQuestionか確認する
nyatla 37:fc4b4fd6a649 264 * @return
nyatla 37:fc4b4fd6a649 265 * 受領可能なQuestionの数
nyatla 37:fc4b4fd6a649 266 *
nyatla 37:fc4b4fd6a649 267 */
nyatla 98:6284ce9a0476 268 static NyLPC_TUInt16 getNumberOfQuestion(const void* i_packet, NyLPC_TUInt16 i_len)
nyatla 37:fc4b4fd6a649 269 {
nyatla 98:6284ce9a0476 270 struct NyLPC_TDnsHeader* ptr = (struct NyLPC_TDnsHeader*)i_packet;
nyatla 37:fc4b4fd6a649 271 NyLPC_TUInt16 t;
nyatla 98:6284ce9a0476 272 if (i_len<sizeof(struct NyLPC_TDnsHeader)){
nyatla 37:fc4b4fd6a649 273 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 274 }
nyatla 37:fc4b4fd6a649 275 //questrionの確認
nyatla 37:fc4b4fd6a649 276 //QR==0 && op==0 && tc=0
nyatla 98:6284ce9a0476 277 t = NyLPC_ntohs(ptr->flag);
nyatla 98:6284ce9a0476 278 if (((t & NyLPC_TDnsHeader_FLAG_MASK_QR) != 0) &&
nyatla 98:6284ce9a0476 279 ((t & NyLPC_TDnsHeader_FLAG_MASK_OPCODE) != 0) &&
nyatla 98:6284ce9a0476 280 ((t & NyLPC_TDnsHeader_FLAG_MASK_TC) != 0))
nyatla 37:fc4b4fd6a649 281 {
nyatla 37:fc4b4fd6a649 282 //this is response
nyatla 37:fc4b4fd6a649 283 return 0;
nyatla 37:fc4b4fd6a649 284 }
nyatla 37:fc4b4fd6a649 285 return NyLPC_ntohs(ptr->qd);
nyatla 37:fc4b4fd6a649 286 }
nyatla 37:fc4b4fd6a649 287
nyatla 98:6284ce9a0476 288
nyatla 98:6284ce9a0476 289 /**
nyatla 98:6284ce9a0476 290 * [i_name].[i_protocol].localをquestionと比較します。i_nameは省略ができます。
nyatla 98:6284ce9a0476 291 * @return 等しい場合true
nyatla 98:6284ce9a0476 292 */
nyatla 98:6284ce9a0476 293 static NyLPC_TBool NyLPC_TDnsQuestion_isEqualName(const struct NyLPC_TDnsQuestion* question, const char* i_name, const char* i_protocol)
nyatla 37:fc4b4fd6a649 294 {
nyatla 98:6284ce9a0476 295 NyLPC_TUInt8 tmp;
nyatla 98:6284ce9a0476 296 const char* p; //プロトコル文字列の解析開始位置
nyatla 98:6284ce9a0476 297 const char* s = question->buf + (NyLPC_TUInt8)question->qname_pos; //クエリの解析位置
nyatla 98:6284ce9a0476 298
nyatla 98:6284ce9a0476 299 //Domain
nyatla 98:6284ce9a0476 300 if (i_name != NULL){
nyatla 98:6284ce9a0476 301 //0xc0参照の解決
nyatla 98:6284ce9a0476 302 s = getExtractNamePos(question->buf, s);
nyatla 98:6284ce9a0476 303 if (s == NULL){
nyatla 98:6284ce9a0476 304 return NyLPC_TBool_FALSE;
nyatla 98:6284ce9a0476 305 }
nyatla 98:6284ce9a0476 306 tmp = (NyLPC_TUInt8)strlen(i_name);
nyatla 98:6284ce9a0476 307 if (tmp != *s || memcmp(s + 1, i_name, tmp) != 0){
nyatla 98:6284ce9a0476 308 return NyLPC_TBool_FALSE;
nyatla 98:6284ce9a0476 309 }
nyatla 98:6284ce9a0476 310 s += (*s) + 1;
nyatla 98:6284ce9a0476 311 }else{
nyatla 98:6284ce9a0476 312 s = question->buf + (NyLPC_TUInt8)question->qname_pos;//クエリの解析位置
nyatla 98:6284ce9a0476 313 }
nyatla 98:6284ce9a0476 314 p = i_protocol;
nyatla 98:6284ce9a0476 315 //Protocol
nyatla 98:6284ce9a0476 316 for (;;){
nyatla 98:6284ce9a0476 317 //0xc0参照の解決
nyatla 98:6284ce9a0476 318 s = getExtractNamePos(question->buf, s);
nyatla 98:6284ce9a0476 319 if (s == NULL){
nyatla 98:6284ce9a0476 320 return NyLPC_TBool_FALSE;
nyatla 98:6284ce9a0476 321 }
nyatla 98:6284ce9a0476 322 //SRVの末端到達
nyatla 98:6284ce9a0476 323 if (*p == 0){
nyatla 98:6284ce9a0476 324 if (question->buf + question->buf_len<s + 7 + 4){
nyatla 98:6284ce9a0476 325 return NyLPC_TBool_FALSE;
nyatla 98:6284ce9a0476 326 }
nyatla 98:6284ce9a0476 327 return (memcmp("\5local\0", s, 7) == 0);
nyatla 98:6284ce9a0476 328 }
nyatla 98:6284ce9a0476 329 //有効サイズなら一致検出
nyatla 98:6284ce9a0476 330 if (question->buf + question->buf_len<s + 1 + (NyLPC_TUInt8)*s + 4){
nyatla 98:6284ce9a0476 331 return NyLPC_TBool_FALSE;
nyatla 98:6284ce9a0476 332 }
nyatla 98:6284ce9a0476 333 if (memcmp(p, s + 1, (NyLPC_TUInt8)*s) != 0){
nyatla 98:6284ce9a0476 334 //不一致
nyatla 98:6284ce9a0476 335 return NyLPC_TBool_FALSE;
nyatla 98:6284ce9a0476 336 }
nyatla 98:6284ce9a0476 337 //検出位置の移動
nyatla 98:6284ce9a0476 338 p += (*s) + 1;
nyatla 98:6284ce9a0476 339 s += (*s) + 1;
nyatla 98:6284ce9a0476 340 }
nyatla 98:6284ce9a0476 341 }
nyatla 98:6284ce9a0476 342
nyatla 98:6284ce9a0476 343
nyatla 98:6284ce9a0476 344 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)
nyatla 98:6284ce9a0476 345 {
nyatla 37:fc4b4fd6a649 346 NyLPC_TUInt16 i;
nyatla 98:6284ce9a0476 347 //解析開始位置を計算
nyatla 98:6284ce9a0476 348 NyLPC_TUInt16 qlen = 0;
nyatla 98:6284ce9a0476 349 for (i = i_parse_start; i<i_packet_len - 4; i++){
nyatla 98:6284ce9a0476 350 switch ((NyLPC_TUInt8)(*(i_packet + i))){
nyatla 98:6284ce9a0476 351 case 0x00:
nyatla 98:6284ce9a0476 352 qlen++;
nyatla 98:6284ce9a0476 353 break;
nyatla 98:6284ce9a0476 354 case 0xc0:
nyatla 98:6284ce9a0476 355 qlen+=2;
nyatla 98:6284ce9a0476 356 break;
nyatla 98:6284ce9a0476 357 default:
nyatla 98:6284ce9a0476 358 qlen++;
nyatla 98:6284ce9a0476 359 continue;
nyatla 37:fc4b4fd6a649 360 }
nyatla 98:6284ce9a0476 361 o_val->buf = i_packet;
nyatla 98:6284ce9a0476 362 o_val->buf_len = i_packet_len;
nyatla 98:6284ce9a0476 363 o_val->qname_pos = i_parse_start;
nyatla 98:6284ce9a0476 364 o_val->qtype = NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet+i_parse_start + qlen));
nyatla 98:6284ce9a0476 365 o_val->qclass = NyLPC_ntohs(*(NyLPC_TUInt16*)(i_packet+i_parse_start + qlen + sizeof(NyLPC_TUInt16)));
nyatla 98:6284ce9a0476 366 return qlen + 4;
nyatla 37:fc4b4fd6a649 367 }
nyatla 37:fc4b4fd6a649 368 return 0;
nyatla 37:fc4b4fd6a649 369 }
nyatla 37:fc4b4fd6a649 370
nyatla 37:fc4b4fd6a649 371 /**
nyatla 37:fc4b4fd6a649 372 * DNSレコードのPRTフィールドとDNSラベル文字列を比較する。
nyatla 37:fc4b4fd6a649 373 */
nyatla 98:6284ce9a0476 374 static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchPtrIdx(const struct NyLPC_TDnsRecord* i_struct,const struct NyLPC_TDnsQuestion* question)
nyatla 37:fc4b4fd6a649 375 {
nyatla 37:fc4b4fd6a649 376 NyLPC_TInt16 i;
nyatla 37:fc4b4fd6a649 377 for(i=0;i<i_struct->num_of_srv;i++){
nyatla 98:6284ce9a0476 378 if(NyLPC_TDnsQuestion_isEqualName(question,NULL,i_struct->srv[i].protocol)){
nyatla 37:fc4b4fd6a649 379 return i;
nyatla 37:fc4b4fd6a649 380 }
nyatla 37:fc4b4fd6a649 381 }
nyatla 37:fc4b4fd6a649 382 return -1;
nyatla 37:fc4b4fd6a649 383 }
nyatla 98:6284ce9a0476 384 static NyLPC_TInt16 NyLPC_TDnsRecord_getMatchSrvIdx(const struct NyLPC_TDnsRecord* i_struct,const struct NyLPC_TDnsQuestion* question)
nyatla 37:fc4b4fd6a649 385 {
nyatla 37:fc4b4fd6a649 386 NyLPC_TInt16 i;
nyatla 37:fc4b4fd6a649 387 for(i=0;i<i_struct->num_of_srv;i++){
nyatla 98:6284ce9a0476 388 if(NyLPC_TDnsQuestion_isEqualName(question,i_struct->name,i_struct->srv[i].protocol)){
nyatla 37:fc4b4fd6a649 389 return i;
nyatla 37:fc4b4fd6a649 390 }
nyatla 37:fc4b4fd6a649 391 }
nyatla 37:fc4b4fd6a649 392 return -1;
nyatla 37:fc4b4fd6a649 393 }
nyatla 98:6284ce9a0476 394
nyatla 37:fc4b4fd6a649 395
nyatla 37:fc4b4fd6a649 396
nyatla 37:fc4b4fd6a649 397 /**
nyatla 37:fc4b4fd6a649 398 * '.'区切り文字列をDNS形式の[n]text[n]text\0へ変換する。
nyatla 37:fc4b4fd6a649 399 * @return
nyatla 37:fc4b4fd6a649 400 * 変換後のデータブロックの長さin byte
nyatla 37:fc4b4fd6a649 401 * 終端の\0の長さを含みます。
nyatla 37:fc4b4fd6a649 402 */
nyatla 37:fc4b4fd6a649 403 static NyLPC_TInt16 str2label(NyLPC_TChar* buf,const NyLPC_TChar* name)
nyatla 37:fc4b4fd6a649 404 {
nyatla 37:fc4b4fd6a649 405 //proto文字列の変換
nyatla 37:fc4b4fd6a649 406 NyLPC_TChar* lp;
nyatla 37:fc4b4fd6a649 407 const NyLPC_TChar* n=name;
nyatla 37:fc4b4fd6a649 408 NyLPC_TChar* b=buf;
nyatla 37:fc4b4fd6a649 409 while(*n!='\0'){
nyatla 37:fc4b4fd6a649 410 lp=b;
nyatla 37:fc4b4fd6a649 411 b++;
nyatla 37:fc4b4fd6a649 412 for(;strchr(".\0",*n)==NULL;){
nyatla 37:fc4b4fd6a649 413 *b=*n;
nyatla 37:fc4b4fd6a649 414 b++;
nyatla 37:fc4b4fd6a649 415 n++;
nyatla 37:fc4b4fd6a649 416 }
nyatla 37:fc4b4fd6a649 417 *lp=(char)(b-lp-1);
nyatla 37:fc4b4fd6a649 418 if(*n!='\0'){
nyatla 37:fc4b4fd6a649 419 n++;
nyatla 37:fc4b4fd6a649 420 }
nyatla 37:fc4b4fd6a649 421 }
nyatla 37:fc4b4fd6a649 422 *b='\0';
nyatla 37:fc4b4fd6a649 423 b++;
nyatla 37:fc4b4fd6a649 424 return b-buf;
nyatla 37:fc4b4fd6a649 425 }
nyatla 37:fc4b4fd6a649 426
nyatla 37:fc4b4fd6a649 427 /**
nyatla 37:fc4b4fd6a649 428 * ResourceHeaderのライタ
nyatla 37:fc4b4fd6a649 429 */
nyatla 98:6284ce9a0476 430 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)
nyatla 37:fc4b4fd6a649 431 {
nyatla 37:fc4b4fd6a649 432 NyLPC_TInt16 s;
nyatla 37:fc4b4fd6a649 433 NyLPC_TInt16 l=1+(NyLPC_TInt16)strlen(i_name)+1+5+1;
nyatla 98:6284ce9a0476 434 if(buflen<i_spos+l+4+4){
nyatla 37:fc4b4fd6a649 435 return 0;
nyatla 37:fc4b4fd6a649 436 }
nyatla 98:6284ce9a0476 437 s=str2label(i_packet+i_spos,i_name)-1;
nyatla 98:6284ce9a0476 438 s+=str2label(i_packet+i_spos+s,"local");
nyatla 98:6284ce9a0476 439 //レコード圧縮
nyatla 98:6284ce9a0476 440 l=compressName(i_packet,i_spos,s);
nyatla 98:6284ce9a0476 441 (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(i_type);
nyatla 98:6284ce9a0476 442 (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(i_class);
nyatla 98:6284ce9a0476 443 (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
nyatla 37:fc4b4fd6a649 444 return l+2+2+4;
nyatla 37:fc4b4fd6a649 445 }
nyatla 37:fc4b4fd6a649 446
nyatla 98:6284ce9a0476 447 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)
nyatla 37:fc4b4fd6a649 448 {
nyatla 98:6284ce9a0476 449 NyLPC_TInt16 s;
nyatla 37:fc4b4fd6a649 450 NyLPC_TInt16 l=(NyLPC_TInt16)(1+strlen(i_recode->name)+1+strlen(i_recode->srv[i_sid].protocol)+1+5+1);
nyatla 98:6284ce9a0476 451 if(buflen<i_spos+l+2+2+4){
nyatla 37:fc4b4fd6a649 452 return 0;
nyatla 37:fc4b4fd6a649 453 }
nyatla 98:6284ce9a0476 454 s=str2label(i_packet+i_spos,i_recode->name)-1;
nyatla 98:6284ce9a0476 455 s+=str2label(i_packet+i_spos+s,i_recode->srv[i_sid].protocol)-1;
nyatla 98:6284ce9a0476 456 s+=str2label(i_packet+i_spos+s,"local");
nyatla 98:6284ce9a0476 457 l=compressName(i_packet,i_spos,s);//圧縮
nyatla 98:6284ce9a0476 458 (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_HTONS(i_type);
nyatla 98:6284ce9a0476 459 (*(NyLPC_TUInt16*)(i_packet+l+2))=NyLPC_HTONS(i_class);
nyatla 98:6284ce9a0476 460 (*(NyLPC_TUInt32*)(i_packet+l+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
nyatla 37:fc4b4fd6a649 461 return l+2+2+4;
nyatla 37:fc4b4fd6a649 462 }
nyatla 37:fc4b4fd6a649 463
nyatla 98:6284ce9a0476 464 /**
nyatla 98:6284ce9a0476 465 * パケットヘッダを書き込みます。
nyatla 98:6284ce9a0476 466 * @return パケットサイズ
nyatla 98:6284ce9a0476 467 */
nyatla 98:6284ce9a0476 468 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)
nyatla 37:fc4b4fd6a649 469 {
nyatla 98:6284ce9a0476 470 struct NyLPC_TDnsHeader* p=(struct NyLPC_TDnsHeader*)i_packet;
nyatla 37:fc4b4fd6a649 471 if(i_in_dns_header!=NULL){
nyatla 37:fc4b4fd6a649 472 memcpy(p,i_in_dns_header,sizeof(struct NyLPC_TDnsHeader));
nyatla 37:fc4b4fd6a649 473 p->flag=p->flag | NyLPC_HTONS(NyLPC_TDnsHeader_FLAG_MASK_QR|NyLPC_TDnsHeader_FLAG_MASK_AA);
nyatla 37:fc4b4fd6a649 474 p->flag=p->flag & NyLPC_HTONS(~(NyLPC_TDnsHeader_FLAG_MASK_RECODE|NyLPC_TDnsHeader_FLAG_MASK_TC|NyLPC_TDnsHeader_FLAG_MASK_RA));
nyatla 37:fc4b4fd6a649 475 }else{
nyatla 37:fc4b4fd6a649 476 p->flag=0;
nyatla 37:fc4b4fd6a649 477 p->id=0;
nyatla 37:fc4b4fd6a649 478 }
nyatla 37:fc4b4fd6a649 479 p->qd=0;
nyatla 37:fc4b4fd6a649 480 p->an=NyLPC_HTONS(i_an_count);
nyatla 37:fc4b4fd6a649 481 p->ns=NyLPC_HTONS(i_ns_count);
nyatla 37:fc4b4fd6a649 482 p->ar=NyLPC_HTONS(i_ar_count);
nyatla 37:fc4b4fd6a649 483 return sizeof(struct NyLPC_TDnsHeader);
nyatla 37:fc4b4fd6a649 484 }
nyatla 98:6284ce9a0476 485 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)
nyatla 37:fc4b4fd6a649 486 {
nyatla 98:6284ce9a0476 487 NyLPC_TInt16 s;
nyatla 37:fc4b4fd6a649 488 //AnswerはAレコードのみ
nyatla 37:fc4b4fd6a649 489 //A record header
nyatla 98:6284ce9a0476 490 s=writeResourceHeader(i_packet,i_spos,obuflen,a_rec,NyLPC_TDnsQuestion_QTYPR_A,NyLPC_TDnsQuestion_QCLASS_IN);
nyatla 98:6284ce9a0476 491 if(s==0 || obuflen<s+6){
nyatla 37:fc4b4fd6a649 492 return 0;
nyatla 37:fc4b4fd6a649 493 }
nyatla 37:fc4b4fd6a649 494 //Aレコードを書く
nyatla 37:fc4b4fd6a649 495 //IPADDR
nyatla 98:6284ce9a0476 496 (*(NyLPC_TUInt16*)(i_packet+s))=NyLPC_HTONS(4);
nyatla 98:6284ce9a0476 497 (*(NyLPC_TUInt32*)(i_packet+s+2))=ip->v;
nyatla 98:6284ce9a0476 498 return s+6;
nyatla 37:fc4b4fd6a649 499 }
nyatla 98:6284ce9a0476 500
nyatla 98:6284ce9a0476 501
nyatla 98:6284ce9a0476 502 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)
nyatla 37:fc4b4fd6a649 503 {
nyatla 37:fc4b4fd6a649 504 NyLPC_TInt16 l,s;
nyatla 37:fc4b4fd6a649 505 //Header
nyatla 98:6284ce9a0476 506 // s=(NyLPC_TInt16)*(i_question->buf+i_question->qname_pos);
nyatla 37:fc4b4fd6a649 507 //Headerの長さチェック
nyatla 98:6284ce9a0476 508 if(obuflen<i_spos+30+2+2+4){
nyatla 37:fc4b4fd6a649 509 return 0;
nyatla 37:fc4b4fd6a649 510 }
nyatla 37:fc4b4fd6a649 511 //Header書込み
nyatla 98:6284ce9a0476 512 memcpy(i_packet+i_spos,"\x09_services\x07_dns-sd\x04_udp\x05local\x00",30);
nyatla 98:6284ce9a0476 513 s=compressName(i_packet,i_spos,30);
nyatla 98:6284ce9a0476 514 (*(NyLPC_TUInt16*)(i_packet+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR);
nyatla 98:6284ce9a0476 515 (*(NyLPC_TUInt16*)(i_packet+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN);
nyatla 98:6284ce9a0476 516 (*(NyLPC_TUInt32*)(i_packet+s+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
nyatla 37:fc4b4fd6a649 517 l=s+2+2+4;
nyatla 37:fc4b4fd6a649 518
nyatla 37:fc4b4fd6a649 519 //Resourceの書込み
nyatla 37:fc4b4fd6a649 520 s=(NyLPC_TInt16)(1+strlen(i_srvlec->protocol)+1+5+1);//逆引き文字列の長さ(デリミタ×3+1)
nyatla 37:fc4b4fd6a649 521 if(obuflen<s+l+2){
nyatla 37:fc4b4fd6a649 522 return 0;
nyatla 37:fc4b4fd6a649 523 }
nyatla 98:6284ce9a0476 524 (*(NyLPC_TUInt16*)(i_packet+l))=NyLPC_ntohs(s);
nyatla 37:fc4b4fd6a649 525 l+=2;
nyatla 98:6284ce9a0476 526 s=str2label(i_packet+l,i_srvlec->protocol)-1;
nyatla 98:6284ce9a0476 527 s+=str2label(i_packet+l+s,"local");
nyatla 98:6284ce9a0476 528 s=compressName(i_packet,l,s);//圧縮
nyatla 98:6284ce9a0476 529 return s;
nyatla 37:fc4b4fd6a649 530 }
nyatla 98:6284ce9a0476 531 static NyLPC_TInt16 writePtrRecord(const struct NyLPC_TDnsRecord* i_recode,NyLPC_TInt16 i_sid,char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
nyatla 37:fc4b4fd6a649 532 {
nyatla 37:fc4b4fd6a649 533 NyLPC_TInt16 l,s;
nyatla 37:fc4b4fd6a649 534 NyLPC_TUInt16* rlen;
nyatla 37:fc4b4fd6a649 535 //Header:開始文字数(1)+プレフィクス(n)+終端(1)+local(5)+1
nyatla 37:fc4b4fd6a649 536 s=(NyLPC_TInt16)(1+strlen(i_recode->srv[i_sid].protocol)+1+5+1);
nyatla 37:fc4b4fd6a649 537 //Headerの長さチェック
nyatla 98:6284ce9a0476 538 if(obuflen<i_spos+s+2+2+4){
nyatla 37:fc4b4fd6a649 539 return 0;
nyatla 37:fc4b4fd6a649 540 }
nyatla 37:fc4b4fd6a649 541 //Header書込み
nyatla 98:6284ce9a0476 542 s =str2label(i_packet+i_spos,i_recode->srv[i_sid].protocol)-1;
nyatla 98:6284ce9a0476 543 s+=str2label(i_packet+i_spos+s,"local");
nyatla 98:6284ce9a0476 544 s=compressName(i_packet,i_spos,s);
nyatla 98:6284ce9a0476 545 (*(NyLPC_TUInt16*)(i_packet+s))=NyLPC_HTONS(NyLPC_TDnsQuestion_QTYPR_PTR);
nyatla 98:6284ce9a0476 546 (*(NyLPC_TUInt16*)(i_packet+s+2))=NyLPC_HTONS(NyLPC_TDnsQuestion_QCLASS_IN);
nyatla 98:6284ce9a0476 547 (*(NyLPC_TUInt32*)(i_packet+s+4))=NyLPC_HTONL(NyLPC_TcMDns_TTL);
nyatla 37:fc4b4fd6a649 548 l=s+2+2+4;
nyatla 37:fc4b4fd6a649 549
nyatla 37:fc4b4fd6a649 550 //Resourceの書込み
nyatla 37:fc4b4fd6a649 551 s=1+strlen(i_recode->name)+1+strlen(i_recode->srv[i_sid].protocol)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1)
nyatla 37:fc4b4fd6a649 552 if(obuflen<s+l+2){
nyatla 37:fc4b4fd6a649 553 return 0;
nyatla 37:fc4b4fd6a649 554 }
nyatla 98:6284ce9a0476 555 rlen=(NyLPC_TUInt16*)(i_packet+l);
nyatla 37:fc4b4fd6a649 556 l+=2;
nyatla 98:6284ce9a0476 557 s =str2label(i_packet+l,i_recode->name)-1;
nyatla 98:6284ce9a0476 558 s+=str2label(i_packet+l+s,i_recode->srv[i_sid].protocol)-1;
nyatla 98:6284ce9a0476 559 s+=str2label(i_packet+l+s,"local");
nyatla 98:6284ce9a0476 560 s=compressName(i_packet,l,s);//圧縮
nyatla 98:6284ce9a0476 561 (*rlen)=NyLPC_ntohs(s-l);
nyatla 98:6284ce9a0476 562 return s;
nyatla 37:fc4b4fd6a649 563 }
nyatla 37:fc4b4fd6a649 564
nyatla 98:6284ce9a0476 565 static NyLPC_TInt16 writeSRVRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* i_packet,NyLPC_TUInt16 i_spos,NyLPC_TInt16 obuflen)
nyatla 37:fc4b4fd6a649 566 {
nyatla 37:fc4b4fd6a649 567 NyLPC_TInt16 l,s;
nyatla 37:fc4b4fd6a649 568 NyLPC_TUInt16* rlen;
nyatla 37:fc4b4fd6a649 569
nyatla 37:fc4b4fd6a649 570 //SRV Record
nyatla 98:6284ce9a0476 571 s=writeSrvResourceHeader(i_packet,i_spos,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_SRV,NyLPC_TDnsQuestion_QCLASS_IN);
nyatla 37:fc4b4fd6a649 572 if(s==0){
nyatla 37:fc4b4fd6a649 573 return 0;
nyatla 37:fc4b4fd6a649 574 }
nyatla 37:fc4b4fd6a649 575
nyatla 37:fc4b4fd6a649 576 l=1+strlen(i_inst->_ref_record->a)+1+5+1;//逆引き文字列の長さ(デリミタ×3+1)
nyatla 98:6284ce9a0476 577 if(obuflen<s+8+l){
nyatla 37:fc4b4fd6a649 578 return 0;
nyatla 37:fc4b4fd6a649 579 }
nyatla 37:fc4b4fd6a649 580 //IPADDR
nyatla 98:6284ce9a0476 581 rlen=(NyLPC_TUInt16*)(i_packet+s);
nyatla 98:6284ce9a0476 582 (*(NyLPC_TUInt16*)(i_packet+s+2))=NyLPC_HTONS(0);//Priority
nyatla 98:6284ce9a0476 583 (*(NyLPC_TUInt16*)(i_packet+s+4))=NyLPC_HTONS(0);//Weight
nyatla 98:6284ce9a0476 584 (*(NyLPC_TUInt16*)(i_packet+s+6))=NyLPC_HTONS(i_inst->_ref_record->srv[i_sid].port);//PORT
nyatla 37:fc4b4fd6a649 585 l=4*2+s;
nyatla 98:6284ce9a0476 586 s=str2label(i_packet+l,i_inst->_ref_record->a)-1;
nyatla 98:6284ce9a0476 587 s+=str2label(i_packet+l+s,"local");
nyatla 98:6284ce9a0476 588 s=compressName(i_packet,l,s);//圧縮
nyatla 98:6284ce9a0476 589 (*rlen)=NyLPC_HTONS(2+2+2+(s-l));
nyatla 98:6284ce9a0476 590 return s;
nyatla 37:fc4b4fd6a649 591 }
nyatla 98:6284ce9a0476 592 static NyLPC_TInt16 writeTXTRecord(NyLPC_TcMDnsServer_t* i_inst,NyLPC_TInt16 i_sid,char* i_packet,NyLPC_TInt16 i_spos,NyLPC_TInt16 obuflen)
nyatla 37:fc4b4fd6a649 593 {
nyatla 37:fc4b4fd6a649 594 NyLPC_TInt16 ret;
nyatla 37:fc4b4fd6a649 595 NyLPC_TInt16 l;
nyatla 37:fc4b4fd6a649 596 //Answer
nyatla 98:6284ce9a0476 597 ret=writeSrvResourceHeader(i_packet,i_spos,obuflen,i_inst->_ref_record,i_sid,NyLPC_TDnsQuestion_QTYPR_TXT,NyLPC_TDnsQuestion_QCLASS_IN);
nyatla 37:fc4b4fd6a649 598 if(ret==0){
nyatla 37:fc4b4fd6a649 599 return 0;
nyatla 37:fc4b4fd6a649 600 }
nyatla 37:fc4b4fd6a649 601 //name.proto.localを返す。
nyatla 98:6284ce9a0476 602 if(obuflen<ret+2){
nyatla 37:fc4b4fd6a649 603 return 0;
nyatla 37:fc4b4fd6a649 604 }
nyatla 98:6284ce9a0476 605 (*(NyLPC_TUInt16*)(i_packet+ret))=NyLPC_ntohs(0);
nyatla 37:fc4b4fd6a649 606 //proto.name.local.
nyatla 37:fc4b4fd6a649 607 l=ret+2;
nyatla 37:fc4b4fd6a649 608 return l;
nyatla 37:fc4b4fd6a649 609 }
nyatla 37:fc4b4fd6a649 610
nyatla 37:fc4b4fd6a649 611
nyatla 37:fc4b4fd6a649 612
nyatla 37:fc4b4fd6a649 613
nyatla 37:fc4b4fd6a649 614
nyatla 37:fc4b4fd6a649 615
nyatla 37:fc4b4fd6a649 616 static void sendAnnounse(NyLPC_TcMDnsServer_t* i_inst)
nyatla 37:fc4b4fd6a649 617 {
nyatla 37:fc4b4fd6a649 618 char* obuf;
nyatla 37:fc4b4fd6a649 619 NyLPC_TUInt16 obuflen;
nyatla 98:6284ce9a0476 620 NyLPC_TUInt16 l;
nyatla 37:fc4b4fd6a649 621 int i2;
nyatla 37:fc4b4fd6a649 622 for(i2=0;i2<i_inst->_ref_record->num_of_srv;i2++){
nyatla 37:fc4b4fd6a649 623 //Bufferの取得
nyatla 37:fc4b4fd6a649 624 obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,TIMEOUT_IN_MS);
nyatla 37:fc4b4fd6a649 625 if(obuf==NULL){
nyatla 37:fc4b4fd6a649 626 return;
nyatla 37:fc4b4fd6a649 627 }
nyatla 37:fc4b4fd6a649 628 l=setResponseHeader(obuf,NULL,1,0,3);
nyatla 98:6284ce9a0476 629 l=writePtrRecord(i_inst->_ref_record,i2,obuf,l,obuflen);
nyatla 98:6284ce9a0476 630 if(l<=0){
nyatla 37:fc4b4fd6a649 631 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 632 }
nyatla 98:6284ce9a0476 633 l=writeSRVRecord(i_inst,i2,obuf,l,obuflen);
nyatla 98:6284ce9a0476 634 if(l<=0){
nyatla 37:fc4b4fd6a649 635 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 636 }
nyatla 98:6284ce9a0476 637 l=writeTXTRecord(i_inst,i2,obuf,l,obuflen);
nyatla 98:6284ce9a0476 638 if(l<=0){
nyatla 37:fc4b4fd6a649 639 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 640 }
nyatla 37:fc4b4fd6a649 641 //Aレコード
nyatla 98:6284ce9a0476 642 l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
nyatla 98:6284ce9a0476 643 if(l<=0){
nyatla 37:fc4b4fd6a649 644 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 645 }
nyatla 37:fc4b4fd6a649 646 if(!NyLPC_cUdpSocket_psend(&(i_inst->_super),&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){
nyatla 37:fc4b4fd6a649 647 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 648 }
nyatla 37:fc4b4fd6a649 649 }
nyatla 37:fc4b4fd6a649 650 return;
nyatla 37:fc4b4fd6a649 651 ERROR:
nyatla 37:fc4b4fd6a649 652 NyLPC_cUdpSocket_releaseSendBuf(&(i_inst->_super),obuf);
nyatla 37:fc4b4fd6a649 653 return;
nyatla 37:fc4b4fd6a649 654 }
nyatla 37:fc4b4fd6a649 655
nyatla 37:fc4b4fd6a649 656
nyatla 98:6284ce9a0476 657
nyatla 37:fc4b4fd6a649 658 static void sendReply2(NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsHeader* i_dns_header,const struct NyLPC_TDnsQuestion* q)
nyatla 37:fc4b4fd6a649 659 {
nyatla 37:fc4b4fd6a649 660 NyLPC_TInt16 ptr_recode;
nyatla 37:fc4b4fd6a649 661 NyLPC_TInt16 i2;
nyatla 37:fc4b4fd6a649 662 char* obuf;
nyatla 37:fc4b4fd6a649 663 NyLPC_TUInt16 obuflen;
nyatla 98:6284ce9a0476 664 NyLPC_TUInt16 l;
nyatla 37:fc4b4fd6a649 665 //パケットヘッダの生成
nyatla 37:fc4b4fd6a649 666 switch(q->qtype){
nyatla 37:fc4b4fd6a649 667 case NyLPC_TDnsQuestion_QTYPR_SRV:
nyatla 37:fc4b4fd6a649 668 //SRV,A record
nyatla 98:6284ce9a0476 669 ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q);
nyatla 37:fc4b4fd6a649 670 if(ptr_recode<0){
nyatla 37:fc4b4fd6a649 671 goto DROP;
nyatla 37:fc4b4fd6a649 672 }
nyatla 37:fc4b4fd6a649 673 //Bufferの取得
nyatla 37:fc4b4fd6a649 674 obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
nyatla 37:fc4b4fd6a649 675 if(obuf==NULL){
nyatla 37:fc4b4fd6a649 676 goto DROP;
nyatla 37:fc4b4fd6a649 677 }
nyatla 37:fc4b4fd6a649 678 l=setResponseHeader(obuf,i_dns_header,1,0,2);
nyatla 98:6284ce9a0476 679 l=writeSRVRecord(i_inst,ptr_recode,obuf,l,obuflen);
nyatla 98:6284ce9a0476 680 if(l<=0){
nyatla 37:fc4b4fd6a649 681 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 682 }
nyatla 98:6284ce9a0476 683 l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
nyatla 98:6284ce9a0476 684 if(l<=0){
nyatla 37:fc4b4fd6a649 685 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 686 }
nyatla 37:fc4b4fd6a649 687 //Aレコード
nyatla 98:6284ce9a0476 688 l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
nyatla 98:6284ce9a0476 689 if(l<=0){
nyatla 37:fc4b4fd6a649 690 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 691 }
nyatla 37:fc4b4fd6a649 692 break;
nyatla 37:fc4b4fd6a649 693 case NyLPC_TDnsQuestion_QTYPR_A:
nyatla 37:fc4b4fd6a649 694 //自分宛?(name.local)
nyatla 98:6284ce9a0476 695 if(!NyLPC_TDnsQuestion_isEqualName(q,i_inst->_ref_record->a,"")){
nyatla 37:fc4b4fd6a649 696 goto DROP;
nyatla 37:fc4b4fd6a649 697 }
nyatla 37:fc4b4fd6a649 698 //Bufferの取得
nyatla 37:fc4b4fd6a649 699 obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
nyatla 37:fc4b4fd6a649 700 if(obuf==NULL){
nyatla 37:fc4b4fd6a649 701 goto DROP;
nyatla 37:fc4b4fd6a649 702 }
nyatla 37:fc4b4fd6a649 703 //Headerのコピー
nyatla 37:fc4b4fd6a649 704 l=setResponseHeader(obuf,i_dns_header,1,0,0);
nyatla 37:fc4b4fd6a649 705 //Aレコードのみ
nyatla 98:6284ce9a0476 706 l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
nyatla 98:6284ce9a0476 707 if(l<=0){
nyatla 37:fc4b4fd6a649 708 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 709 }
nyatla 37:fc4b4fd6a649 710 break;
nyatla 37:fc4b4fd6a649 711 case NyLPC_TDnsQuestion_QTYPR_PTR:
nyatla 98:6284ce9a0476 712 //_service._dns-sd._udpかどうか
nyatla 98:6284ce9a0476 713 if(NyLPC_TDnsQuestion_isEqualName(q,NULL,"_services._dns-sd._udp")){
nyatla 37:fc4b4fd6a649 714 //Bufferの取得
nyatla 37:fc4b4fd6a649 715 obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
nyatla 37:fc4b4fd6a649 716 if(obuf==NULL){
nyatla 37:fc4b4fd6a649 717 goto DROP;
nyatla 37:fc4b4fd6a649 718 }
nyatla 37:fc4b4fd6a649 719 l=setResponseHeader(obuf,i_dns_header,i_inst->_ref_record->num_of_srv,0,0);
nyatla 37:fc4b4fd6a649 720 for(i2=0;i2<i_inst->_ref_record->num_of_srv;i2++){
nyatla 98:6284ce9a0476 721 l=writeSdPtrRecord(q,&(i_inst->_ref_record->srv[i2]),obuf,l,obuflen);
nyatla 98:6284ce9a0476 722 if(l<=0){
nyatla 37:fc4b4fd6a649 723 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 724 }
nyatla 37:fc4b4fd6a649 725 }
nyatla 37:fc4b4fd6a649 726 }else{
nyatla 37:fc4b4fd6a649 727 //自分宛?(proto.local)
nyatla 98:6284ce9a0476 728 ptr_recode=NyLPC_TDnsRecord_getMatchPtrIdx(i_inst->_ref_record,q);
nyatla 37:fc4b4fd6a649 729 if(ptr_recode<0){
nyatla 37:fc4b4fd6a649 730 goto DROP;
nyatla 37:fc4b4fd6a649 731 }
nyatla 37:fc4b4fd6a649 732 //Bufferの取得
nyatla 37:fc4b4fd6a649 733 obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
nyatla 37:fc4b4fd6a649 734 if(obuf==NULL){
nyatla 37:fc4b4fd6a649 735 goto DROP;
nyatla 37:fc4b4fd6a649 736 }
nyatla 37:fc4b4fd6a649 737 l=setResponseHeader(obuf,i_dns_header,1,0,3);
nyatla 98:6284ce9a0476 738 l=writePtrRecord(i_inst->_ref_record,ptr_recode,obuf,l,obuflen);
nyatla 98:6284ce9a0476 739 if(l<=0){
nyatla 37:fc4b4fd6a649 740 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 741 }
nyatla 98:6284ce9a0476 742 l=writeSRVRecord(i_inst,ptr_recode,obuf,l,obuflen);
nyatla 98:6284ce9a0476 743 if(l<=0){
nyatla 37:fc4b4fd6a649 744 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 745 }
nyatla 98:6284ce9a0476 746 l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
nyatla 98:6284ce9a0476 747 if(l<=0){
nyatla 37:fc4b4fd6a649 748 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 749 }
nyatla 37:fc4b4fd6a649 750 //Aレコード
nyatla 98:6284ce9a0476 751 l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
nyatla 98:6284ce9a0476 752 if(l<=0){
nyatla 37:fc4b4fd6a649 753 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 754 }
nyatla 37:fc4b4fd6a649 755 }
nyatla 37:fc4b4fd6a649 756 break;
nyatla 37:fc4b4fd6a649 757 case NyLPC_TDnsQuestion_QTYPR_TXT:
nyatla 37:fc4b4fd6a649 758 //自分宛?(proto.local)
nyatla 98:6284ce9a0476 759 ptr_recode=NyLPC_TDnsRecord_getMatchSrvIdx(i_inst->_ref_record,q);
nyatla 37:fc4b4fd6a649 760 if(ptr_recode<0){
nyatla 37:fc4b4fd6a649 761 goto DROP;
nyatla 37:fc4b4fd6a649 762 }
nyatla 37:fc4b4fd6a649 763 //Bufferの取得
nyatla 37:fc4b4fd6a649 764 obuf=NyLPC_cUdpSocket_allocSendBuf(&(i_inst->_super),512,&obuflen,0);
nyatla 37:fc4b4fd6a649 765 l=setResponseHeader(obuf,i_dns_header,1,0,1);
nyatla 98:6284ce9a0476 766 l=writeTXTRecord(i_inst,ptr_recode,obuf,l,obuflen);
nyatla 98:6284ce9a0476 767 if(l<=0){
nyatla 37:fc4b4fd6a649 768 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 769 }
nyatla 98:6284ce9a0476 770 l=writeARecord(obuf,l,obuflen,i_inst->_ref_record->a,&(i_inst->_super.uip_udp_conn.lipaddr));
nyatla 98:6284ce9a0476 771 if(l<=0){
nyatla 37:fc4b4fd6a649 772 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 773 }
nyatla 37:fc4b4fd6a649 774 break;
nyatla 37:fc4b4fd6a649 775 default:
nyatla 37:fc4b4fd6a649 776 goto DROP;
nyatla 37:fc4b4fd6a649 777 }
nyatla 37:fc4b4fd6a649 778 if(!NyLPC_cUdpSocket_psend(&(i_inst->_super),&MDNS_MCAST_IPADDR,MDNS_MCAST_PORT,obuf,l)){
nyatla 37:fc4b4fd6a649 779 NyLPC_OnErrorGoto(ERROR);
nyatla 37:fc4b4fd6a649 780 }
nyatla 37:fc4b4fd6a649 781 return;
nyatla 37:fc4b4fd6a649 782 ERROR:
nyatla 37:fc4b4fd6a649 783 NyLPC_cUdpSocket_releaseSendBuf(&(i_inst->_super),obuf);
nyatla 37:fc4b4fd6a649 784 DROP:
nyatla 37:fc4b4fd6a649 785 return;
nyatla 37:fc4b4fd6a649 786 }
nyatla 37:fc4b4fd6a649 787 #define CTRL_FLAG_INIT 0x00
nyatla 37:fc4b4fd6a649 788 #define CTRL_FLAG_STARTED 0x80
nyatla 37:fc4b4fd6a649 789 #define CTRL_FLAG_STOP_REQUESTED 0x40
nyatla 37:fc4b4fd6a649 790 #define CTRL_FLAG_PROCESS_PACKET 0x20 //パケット処理中の間1
nyatla 37:fc4b4fd6a649 791
nyatla 37:fc4b4fd6a649 792 static NyLPC_TBool onPacket(NyLPC_TcUdpSocket_t* i_inst,const void* i_buf,const struct NyLPC_TIPv4RxInfo* i_info)
nyatla 37:fc4b4fd6a649 793 {
nyatla 98:6284ce9a0476 794 NyLPC_TUInt16 in_len;
nyatla 37:fc4b4fd6a649 795 NyLPC_TUInt16 num_of_query;
nyatla 37:fc4b4fd6a649 796 struct NyLPC_TDnsQuestion q;
nyatla 37:fc4b4fd6a649 797 NyLPC_TUInt16 s;
nyatla 37:fc4b4fd6a649 798 NyLPC_TInt16 i;
nyatla 37:fc4b4fd6a649 799
nyatla 37:fc4b4fd6a649 800 if(i_info->peer_port!=MDNS_MCAST_PORT || !NyLPC_TIPv4Addr_isEqual(&MDNS_MCAST_IPADDR,&i_info->ip)){
nyatla 37:fc4b4fd6a649 801 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 802 }
nyatla 37:fc4b4fd6a649 803
nyatla 98:6284ce9a0476 804 num_of_query=getNumberOfQuestion(i_buf,i_info->size);
nyatla 37:fc4b4fd6a649 805 if(num_of_query==0){
nyatla 37:fc4b4fd6a649 806 goto DROP;
nyatla 37:fc4b4fd6a649 807 }
nyatla 98:6284ce9a0476 808 in_len=sizeof(struct NyLPC_TDnsHeader);
nyatla 37:fc4b4fd6a649 809 for(i=0;i<num_of_query;i++){
nyatla 37:fc4b4fd6a649 810 //Queryのパース
nyatla 98:6284ce9a0476 811
nyatla 98:6284ce9a0476 812 s=NyLPC_TDnsQuestion_parse(i_buf,i_info->size,in_len,&q);
nyatla 37:fc4b4fd6a649 813 if(s==0){
nyatla 37:fc4b4fd6a649 814 goto DROP;
nyatla 37:fc4b4fd6a649 815 }
nyatla 98:6284ce9a0476 816 in_len+=s;
nyatla 37:fc4b4fd6a649 817 sendReply2((NyLPC_TcMDnsServer_t*)i_inst,(const struct NyLPC_TDnsHeader*)i_buf,&q);
nyatla 37:fc4b4fd6a649 818 }
nyatla 37:fc4b4fd6a649 819 //パケット処理終了
nyatla 37:fc4b4fd6a649 820 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 821 DROP:
nyatla 37:fc4b4fd6a649 822 return NyLPC_TBool_FALSE;
nyatla 37:fc4b4fd6a649 823 }
nyatla 37:fc4b4fd6a649 824
nyatla 37:fc4b4fd6a649 825 static void onPeriodic(NyLPC_TcUdpSocket_t* i_inst)
nyatla 37:fc4b4fd6a649 826 {
nyatla 37:fc4b4fd6a649 827
nyatla 37:fc4b4fd6a649 828 if(NyLPC_cStopwatch_isExpired(&((NyLPC_TcMDnsServer_t*)i_inst)->_periodic_sw)){
nyatla 37:fc4b4fd6a649 829 //アナウンス
nyatla 37:fc4b4fd6a649 830 sendAnnounse(((NyLPC_TcMDnsServer_t*)i_inst));
nyatla 37:fc4b4fd6a649 831 //TTL(msec)*1000*80%
nyatla 96:e6a0db86988e 832 NyLPC_cStopwatch_startExpire((&((NyLPC_TcMDnsServer_t*)i_inst)->_periodic_sw),NyLPC_TcMDns_TTL*1000/2);
nyatla 37:fc4b4fd6a649 833 }
nyatla 37:fc4b4fd6a649 834 }
nyatla 37:fc4b4fd6a649 835
nyatla 37:fc4b4fd6a649 836 NyLPC_TBool NyLPC_cMDnsServer_initialize(
nyatla 48:00d211aac2ec 837 NyLPC_TcMDnsServer_t* i_inst,const struct NyLPC_TDnsRecord* i_ref_record)
nyatla 37:fc4b4fd6a649 838 {
nyatla 37:fc4b4fd6a649 839 NyLPC_cStopwatch_initialize(&(i_inst->_periodic_sw));
nyatla 37:fc4b4fd6a649 840 NyLPC_cStopwatch_startExpire(&(i_inst->_periodic_sw),1000);
nyatla 37:fc4b4fd6a649 841 NyLPC_cUdpSocket_initialize(&(i_inst->_super),MDNS_MCAST_PORT,NULL,0);
nyatla 37:fc4b4fd6a649 842 NyLPC_cUdpSocket_setOnRxHandler(&(i_inst->_super),onPacket);
nyatla 48:00d211aac2ec 843 NyLPC_cUdpSocket_setOnPeriodicHandler(&(i_inst->_super),onPeriodic);
nyatla 37:fc4b4fd6a649 844 NyLPC_cUdpSocket_joinMulticast(&(i_inst->_super),&MDNS_MCAST_IPADDR);
nyatla 37:fc4b4fd6a649 845 i_inst->_ref_record=i_ref_record;
nyatla 37:fc4b4fd6a649 846 return NyLPC_TBool_TRUE;
nyatla 37:fc4b4fd6a649 847 }
nyatla 37:fc4b4fd6a649 848 void NyLPC_cMDnsServer_finalize(
nyatla 37:fc4b4fd6a649 849 NyLPC_TcMDnsServer_t* i_inst)
nyatla 37:fc4b4fd6a649 850 {
nyatla 37:fc4b4fd6a649 851 NyLPC_cUdpSocket_finalize(&(i_inst->_super));
nyatla 37:fc4b4fd6a649 852 NyLPC_cStopwatch_finalize(&(i_inst->_periodic_sw));
nyatla 37:fc4b4fd6a649 853 }
nyatla 37:fc4b4fd6a649 854
nyatla 37:fc4b4fd6a649 855
nyatla 37:fc4b4fd6a649 856