1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef _MDNSSDLISTENER_H__ 18 #define _MDNSSDLISTENER_H__ 19 20 #include <pthread.h> 21 #include <sysutils/FrameworkListener.h> 22 #include <dns_sd.h> 23 24 #include "NetdCommand.h" 25 26 // callbacks 27 void MDnsSdListenerDiscoverCallback(DNSServiceRef sdRef, DNSServiceFlags flags, 28 uint32_t interfaceIndex, DNSServiceErrorType errorCode, 29 const char *serviceName, const char *regType, const char *replyDomain, 30 void *inContext); 31 32 void MDnsSdListenerRegisterCallback(DNSServiceRef sdRef, DNSServiceFlags flags, 33 DNSServiceErrorType errorCode, const char *serviceName, const char *regType, 34 const char *domain, void *inContext); 35 36 void MDnsSdListenerResolveCallback(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interface, 37 DNSServiceErrorType errorCode, const char *fullname, const char *hosttarget, uint16_t port, 38 uint16_t txtLen, const unsigned char *txtRecord, void *inContext); 39 40 void MDnsSdListenerSetHostnameCallback(DNSServiceRef, DNSServiceFlags flags, 41 DNSServiceErrorType errorCode, const char *hostname, void *inContext); 42 43 void MDnsSdListenerGetAddrInfoCallback(DNSServiceRef sdRef, DNSServiceFlags flags, 44 uint32_t interface, DNSServiceErrorType errorCode, const char *hostname, 45 const struct sockaddr *const sa, uint32_t ttl, void *inContext); 46 47 #define RESCAN "1" 48 49 class MDnsSdListener : public FrameworkListener { 50 public: 51 MDnsSdListener(); ~MDnsSdListener()52 virtual ~MDnsSdListener() {} 53 54 static constexpr const char* SOCKET_NAME = "mdns"; 55 56 class Context { 57 public: 58 MDnsSdListener *mListener; 59 int mRefNumber; 60 Context(int refNumber,MDnsSdListener * m)61 Context(int refNumber, MDnsSdListener *m) { 62 mRefNumber = refNumber; 63 mListener = m; 64 } 65 ~Context()66 ~Context() { 67 } 68 }; 69 70 private: 71 class Monitor { 72 public: 73 Monitor(); ~Monitor()74 virtual ~Monitor() {} 75 DNSServiceRef *allocateServiceRef(int id, Context *c); 76 void startMonitoring(int id); 77 DNSServiceRef *lookupServiceRef(int id); 78 void freeServiceRef(int id); 79 int startService(); 80 int stopService(); 81 void run(); 82 void deallocateServiceRef(DNSServiceRef* ref); 83 84 private: 85 int rescan(); // returns the number of elements in the poll 86 class Element { 87 public: 88 int mId; 89 Element *mNext; 90 DNSServiceRef mRef; 91 Context *mContext; 92 int mReady; Element(int id,Context * context)93 Element(int id, Context *context) 94 : mId(id), mNext(NULL), mContext(context), mReady(0) {} ~Element()95 virtual ~Element() { delete(mContext); } 96 }; 97 Element *mHead; 98 int mLiveCount; 99 struct pollfd *mPollFds; 100 DNSServiceRef **mPollRefs; 101 int mPollSize; 102 int mCtrlSocketPair[2]; 103 pthread_mutex_t mHeadMutex; 104 }; 105 106 class Handler : public NetdCommand { 107 public: 108 Handler(Monitor *m, MDnsSdListener *listener); 109 virtual ~Handler(); 110 int runCommand(SocketClient *c, int argc, char** argv); 111 112 MDnsSdListener *mListener; // needed for broadcast purposes 113 private: 114 void stop(SocketClient *cli, int argc, char **argv, const char *str); 115 116 void discover(SocketClient *cli, const char *iface, const char *regType, 117 const char *domain, const int requestNumber, 118 const int requestFlags); 119 120 void serviceRegister(SocketClient *cli, int requestId, const char *interfaceName, 121 const char *serviceName, const char *serviceType, const char *domain, 122 const char *host, int port, int textLen, void *txtRecord); 123 124 void resolveService(SocketClient *cli, int requestId, 125 const char *interfaceName, const char *serviceName, const char *regType, 126 const char *domain); 127 128 void setHostname(SocketClient *cli, int requestId, const char *hostname); 129 130 void getAddrInfo(SocketClient *cli, int requestId, const char *interfaceName, 131 uint32_t protocol, const char *hostname); 132 133 int ifaceNameToI(const char *iface); 134 const char *iToIfaceName(int i); 135 DNSServiceFlags iToFlags(int i); 136 int flagsToI(DNSServiceFlags flags); 137 Monitor *mMonitor; 138 }; 139 }; 140 141 #endif 142