@@ -78,7 +78,7 @@ class MSearchSchedule : public Schedule {
7878};
7979
8080/* *
81- * @brief Answer from device to MSearch request by sending a reply
81+ * @brief Answer from device to MSearch request by sending the related replies
8282 * @author Phil Schatzmann
8383 */
8484class MSearchReplySchedule : public Schedule {
@@ -111,8 +111,22 @@ class MSearchReplySchedule : public Schedule {
111111 }
112112
113113 bool isValid () override {
114- // Check if the requester's IP is on the same subnet as defined by
115- // DLNA_DISCOVERY_NETMASK
114+ // check if the search target is valid for this device
115+ if (!isValidSearchTarget (*p_device, search_target.c_str ())){
116+ return false ;
117+ }
118+ return isValidIP ();
119+ }
120+
121+ Str search_target;
122+ DLNADeviceInfo* p_device;
123+ int mx = 0 ;
124+
125+ protected:
126+ int max_age = MAX_AGE;
127+
128+ // / check netmask
129+ bool isValidIP () {
116130 IPAddress netmask = DLNA_DISCOVERY_NETMASK;
117131 IPAddress localIP = p_device->getIPAddress ();
118132 IPAddress peerIP = address.address ;
@@ -133,12 +147,27 @@ class MSearchReplySchedule : public Schedule {
133147 return true ;
134148 }
135149
136- Str search_target;
137- DLNADeviceInfo* p_device;
138- int mx = 0 ;
139-
140- protected:
141- int max_age = MAX_AGE;
150+ // check requested search target
151+ bool isValidSearchTarget (DLNADeviceInfo& device, const char * search_target) {
152+ if (StrView (search_target).equals (" ssdp:all" )) {
153+ return true ;
154+ }
155+ if (StrView (search_target).equals (" upnp:rootdevice" )) {
156+ return true ;
157+ }
158+ if (StrView (search_target).equals (device.getDeviceType ())) {
159+ return true ;
160+ }
161+ // check services
162+ for (auto & service : device.getServices ()) {
163+ if (StrView (search_target).equals (service.service_type )) {
164+ return true ;
165+ }
166+ }
167+ DlnaLogger.log (DlnaLogLevel::Info, " Ignoring M-SEARCH for %s" ,
168+ search_target);
169+ return false ;
170+ }
142171
143172 bool sendReply (IUDPService& udp, DLNADeviceInfo& device, const char * target,
144173 const char * udn) {
0 commit comments