2016-08-03 24 views
0

私はbonjourサービスを通じて2つのiOSアプリ間の通信を実装しようとしています。これはおそらく、App-Storeのレビューでは受け入れられないとは言いません。とにかく店には掲載されません。DNSServiceをループバックインターフェイスに制限する方法はありますか?

サービスのループバックインターフェイスへのBonjourアナウンスを、同じデバイス上の接続のみを許可するように制限しようとしています。次のように私は発見を実施してきたクライアント側で

DNSServiceErrorType err = DNSServiceRegister( 
               &dnsServiceRef, 
               kDNSServiceFlagsDefault, 
               if_nametoindex("lo0"), //limit to loopback interface 
               "myServiceName", 
               "_myservicetype._tcp", 
               NULL, 
               NULL, 
               htons(port), 
               0, 
               NULL, 
               &DNSServiceRegisterCallback, 
               (__bridge void *)(self) 
          ); 

DNSServiceErrorType error = DNSServiceBrowse(&serviceBrowseRef, 
               kDNSServiceFlagsDefault, 
               if_nametoindex("lo0"), //limit discovery to loopback as well 
               "_myservicetype._tcp", 
               NULL, 
               &serviceBrowseReply, 
               (__bridge void *)(self) 
               ); 

テストをして

は、私は次の方法でサービスを発表する低レベルDNSServiceRegisterメソッドを実装しましたiPhoneシミュレータは期待される結果を返しません。この発見により、発表されたサービスは見つかりません。

いくつかの更なる掘削:

はそれがdns-sd -B _myservicetype._tcpを働いていた場合、コマンドラインでチェックしようとしているが、それは何も

は、不思議なことに、次のdns-sd -lo -B _myservicetype._tcp(代わりに、ループバックのすべてのローカルインターフェイスには限界が)期待されるサービスを示していない検出されたが、 interfaceIndexは0です(しかしif_nametoindexはインデックスが1であると言う)DNSServiceBrowseのパラメータとしてif_nametoindex("lo0")の代わりにkDNSServiceInterfaceIndexLocalOnlyの代わりにkDNSServiceInterfaceIndexLocalOnlyを使用すると、アナウンスされたサービスが返されます。 コールバックで報告されたinterfaceIndexは再び0です(コマンドラインツールと同じ動作)。

とにも見知らぬ人

は、この予想される動作(インターフェイスをループバックするために制限することは許されない?)または午前私はサービスの登録中kDNSServiceInterfaceIndexLocalOnlyを使用する場合は、発見されたサービスのInterfaceIndexのは4294967295(UINTオーバーフロー?)であります私はここでインターフェイスインデックスの内部処理に関するいくつかの奇妙なバグを経験していますか?

+0

:宣伝とローカルサービスだけを参照する方法についてのガイダンスに従ってください。 ループバックインターフェイスを介した接続のみを許可するソケットに接続できるようにするには、後で解決されたホスト名を削除して、代わりに 'localhost'を使用します – JanR

答えて

0

lo0を使用する理由は、実際のインターフェイス(マルチキャストループバックをサポートするIP_MULTICAST_LOOP)が登録されているときにmDNSResponderがループバックインターフェイスを使用しないためです。 MacOS XバックエンドのUpdateInterfaceListを参照

DNS SD APIには、if_nametoidex()から返された有効なインデックスではない特殊なインデックス定義があります。通常、インデックスは1から始まり、0と-1を意味するdns_sd.hのドキュメントを参照してください。今私が登録し、ループバックまたは別のローカルインタフェースを使用する場合がありますkDNSServiceInterfaceIndexLocalOnly定数を閲覧してるため

/* 
* Constants for specifying an interface index 
* 
* Specific interface indexes are identified via a 32-bit unsigned integer returned 
* by the if_nametoindex() family of calls. 
* 
* If the client passes 0 for interface index, that means "do the right thing", 
* which (at present) means, "if the name is in an mDNS local multicast domain 
* (e.g. 'local.', '254.169.in-addr.arpa.', '{8,9,A,B}.E.F.ip6.arpa.') then multicast 
* on all applicable interfaces, otherwise send via unicast to the appropriate 
* DNS server." Normally, most clients will use 0 for interface index to 
* automatically get the default sensible behaviour. 
* 
* If the client passes a positive interface index, then for multicast names that 
* indicates to do the operation only on that one interface. For unicast names the 
* interface index is ignored unless kDNSServiceFlagsForceMulticast is also set. 
* 
* If the client passes kDNSServiceInterfaceIndexLocalOnly when registering 
* a service, then that service will be found *only* by other local clients 
* on the same machine that are browsing using kDNSServiceInterfaceIndexLocalOnly 
* or kDNSServiceInterfaceIndexAny. 
* If a client has a 'private' service, accessible only to other processes 
* running on the same machine, this allows the client to advertise that service 
* in a way such that it does not inadvertently appear in service lists on 
* all the other machines on the network. 
* 
* If the client passes kDNSServiceInterfaceIndexLocalOnly when browsing 
* then it will find *all* records registered on that same local machine. 
* Clients explicitly wishing to discover *only* LocalOnly services can 
* accomplish this by inspecting the interfaceIndex of each service reported 
* to their DNSServiceBrowseReply() callback function, and discarding those 
* where the interface index is not kDNSServiceInterfaceIndexLocalOnly. 
* 
* kDNSServiceInterfaceIndexP2P is meaningful only in Browse, QueryRecord, 
* and Resolve operations. It should not be used in other DNSService APIs. 
* 
* - If kDNSServiceInterfaceIndexP2P is passed to DNSServiceBrowse or 
* DNSServiceQueryRecord, it restricts the operation to P2P. 
* 
* - If kDNSServiceInterfaceIndexP2P is passed to DNSServiceResolve, it is 
* mapped internally to kDNSServiceInterfaceIndexAny, because resolving 
* a P2P service may create and/or enable an interface whose index is not 
* known a priori. The resolve callback will indicate the index of the 
* interface via which the service can be accessed. 
* 
* If applications pass kDNSServiceInterfaceIndexAny to DNSServiceBrowse 
* or DNSServiceQueryRecord, they must set the kDNSServiceFlagsIncludeP2P flag 
* to include P2P. In this case, if a service instance or the record being queried 
* is found over P2P, the resulting ADD event will indicate kDNSServiceInterfaceIndexP2P 
* as the interface index. 
*/ 

#define kDNSServiceInterfaceIndexAny 0 
#define kDNSServiceInterfaceIndexLocalOnly ((uint32_t)-1) 
#define kDNSServiceInterfaceIndexUnicast ((uint32_t)-2) 
#define kDNSServiceInterfaceIndexP2P  ((uint32_t)-3)