xbmc
SMBWSDiscoveryListener.h
1 /*
2  * Copyright (C) 2021- Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "threads/CriticalSection.h"
12 #include "threads/Thread.h"
13 
14 #include <string>
15 #include <vector>
16 
17 #include <netinet/in.h>
18 
19 namespace WSDiscovery
20 {
21 struct wsd_req_info;
22 }
23 
24 namespace WSDiscovery
25 {
27 {
28 public:
31 
32  void Start();
33  void Stop();
34 
35 protected:
36  // CThread
37  void Process() override;
38 
39 private:
40  struct Command
41  {
42  struct sockaddr_in address;
43  std::string commandMsg;
44  };
45 
46  /*
47  * Dispatch UDP command from command Queue
48  * return (bool) true if message dispatched
49  */
50  bool DispatchCommand();
51 
52  /*
53  * Build SOAPXML command and add to command Queue
54  * in (string) action type
55  * in (string) extra data field (currently used solely for resolve addresses)
56  * return (void)
57  */
58  void AddCommand(const std::string& message, const std::string& extraparameter = "");
59 
60  /*
61  * Process received broadcast messages and add accepted items to
62  * in (string) SOAPXML Message
63  * return (void)
64  */
65  void ParseBuffer(const std::string& buffer);
66 
67  /*
68  * Generates an XML SOAP message given a particular action type
69  * in (string) action type
70  * in/out (string) created message
71  * in (string) extra data field (currently used for resolve addresses)
72  * return (bool) true if full message crafted
73  */
74  bool buildSoapMessage(const std::string& action,
75  std::string& msg,
76  const std::string& extraparameter);
77 
78  // Closes socket and handles setting state for WS-Discovery
79  void Cleanup(bool aborted);
80 
81  /*
82  * Use unicast Get to request computer name
83  * in/out (wsd_req_info&) host info to be updated
84  * return (void)
85  */
86  void UnicastGet(wsd_req_info& info);
87 
88 private:
89  template<std::size_t SIZE>
90  const std::string wsd_tag_find(const std::string& xml,
91  const std::array<std::pair<std::string, std::string>, SIZE>& tag);
92 
93  // Debug print WSD packet data
94  void PrintWSDInfo(const WSDiscovery::wsd_req_info& info);
95 
96  // compare WSD entry address
97  bool equalsAddress(const WSDiscovery::wsd_req_info& lhs,
98  const WSDiscovery::wsd_req_info& rhs) const;
99 
100  // Socket FD for send/recv
101  int fd;
102 
103  std::vector<Command> m_commandbuffer;
104 
105  CCriticalSection crit_commandqueue;
106  CCriticalSection crit_wsdqueue;
107 
108  std::vector<WSDiscovery::wsd_req_info> m_vecWSDInfo;
109 
110  // GUID that should remain constant for an instance
111  const std::string wsd_instance_address;
112 
113  // Number of sends for UDP messages
114  const int retries = 4;
115 
116  // Max udp packet size (+ UDP header + IP header overhead = 65535)
117  const int UDPBUFFSIZE = 65507;
118 
119  // Port for unicast/multicast WSD traffic
120  const int wsdUDP = 3702;
121 
122  // ipv4 multicast group WSD - https://specs.xmlsoap.org/ws/2005/04/discovery/ws-discovery.pdf
123  const char* WDSIPv4MultiGroup = "239.255.255.250";
124 
125  // ToDo: ipv6 broadcast address
126  // const char* WDSIPv6MultiGroup = "FF02::C"
127 };
128 } // namespace WSDiscovery
Definition: Thread.h:44
Definition: IWSDiscovery.h:13
Definition: SMBWSDiscoveryListener.h:26
Definition: SMBWSDiscovery.h:30