summaryrefslogtreecommitdiffstats
path: root/xbmc/network/mdns/ZeroconfBrowserMDNS.h
blob: 4ef3aac658266eab60b89efdb8544aca74e784fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 *  Copyright (C) 2012-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "network/ZeroconfBrowser.h"
#include "threads/CriticalSection.h"
#include "threads/Thread.h"

#include <map>
#include <memory>
#include <utility>
#include <vector>

#include <dns_sd.h>

//platform specific implementation of  zeroconfbrowser interface using native os x APIs
class CZeroconfBrowserMDNS : public CZeroconfBrowser
{
public:
  CZeroconfBrowserMDNS();
  ~CZeroconfBrowserMDNS();

private:
  ///implementation if CZeroconfBrowser interface
  ///@{
  virtual bool doAddServiceType(const std::string& fcr_service_type);
  virtual bool doRemoveServiceType(const std::string& fcr_service_type);

  virtual std::vector<CZeroconfBrowser::ZeroconfService> doGetFoundServices();
  virtual bool doResolveService(CZeroconfBrowser::ZeroconfService& fr_service, double f_timeout);
  ///@}

  /// browser callback
  static void DNSSD_API BrowserCallback(DNSServiceRef browser,
                                        DNSServiceFlags flags,
                                        uint32_t interfaceIndex,
                                        DNSServiceErrorType errorCode,
                                        const char *serviceName,
                                        const char *regtype,
                                        const char *replyDomain,
                                        void *context);
  /// GetAddrInfo callback
  static void DNSSD_API GetAddrInfoCallback(DNSServiceRef                    sdRef,
                                            DNSServiceFlags                  flags,
                                            uint32_t                         interfaceIndex,
                                            DNSServiceErrorType              errorCode,
                                            const char                       *hostname,
                                            const struct sockaddr            *address,
                                            uint32_t                         ttl,
                                            void                             *context
                                            );

  /// resolve callback
  static void DNSSD_API ResolveCallback(DNSServiceRef                       sdRef,
                                        DNSServiceFlags                     flags,
                                        uint32_t                            interfaceIndex,
                                        DNSServiceErrorType                 errorCode,
                                        const char                          *fullname,
                                        const char                          *hosttarget,
                                        uint16_t                            port,        /* In network byte order */
                                        uint16_t                            txtLen,
                                        const unsigned char                 *txtRecord,
                                        void                                *context
                                        );

  /// adds the service to list of found services
  void addDiscoveredService(DNSServiceRef browser, CZeroconfBrowser::ZeroconfService const& fcr_service);
  /// removes the service from list of found services
  void removeDiscoveredService(DNSServiceRef browser, CZeroconfBrowser::ZeroconfService const& fcr_service);
  // win32: process replies from the bonjour daemon
  void ProcessResults();

  //shared variables (with guard)
  CCriticalSection m_data_guard;
  // tBrowserMap maps service types the corresponding browser
  typedef std::map<std::string, DNSServiceRef> tBrowserMap;
  tBrowserMap m_service_browsers;
  //tDiscoveredServicesMap maps browsers to their discovered services + a ref-count for each service
  //ref-count is needed, because a service might pop up more than once, if there's more than one network-iface
  typedef std::map<DNSServiceRef, std::vector<std::pair<ZeroconfService, unsigned int> > > tDiscoveredServicesMap;
  tDiscoveredServicesMap m_discovered_services;
  DNSServiceRef m_browser;
  CZeroconfBrowser::ZeroconfService m_resolving_service;
  CEvent m_resolved_event;
  CEvent m_addrinfo_event;
};