1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef OSP_IMPL_INTERNAL_SERVICES_H_
6 #define OSP_IMPL_INTERNAL_SERVICES_H_
7 
8 #include <memory>
9 #include <vector>
10 
11 #include "osp/impl/mdns_platform_service.h"
12 #include "osp/impl/mdns_responder_service.h"
13 #include "osp/impl/quic/quic_connection_factory.h"
14 #include "osp/impl/service_listener_impl.h"
15 #include "osp/impl/service_publisher_impl.h"
16 #include "osp/public/mdns_service_listener_factory.h"
17 #include "osp/public/mdns_service_publisher_factory.h"
18 #include "osp/public/protocol_connection_client.h"
19 #include "osp/public/protocol_connection_server.h"
20 #include "platform/api/network_interface.h"
21 #include "platform/api/time.h"
22 #include "platform/api/udp_socket.h"
23 #include "platform/base/ip_address.h"
24 #include "platform/base/macros.h"
25 
26 namespace openscreen {
27 
28 class TaskRunner;
29 
30 namespace osp {
31 
32 // Factory for ServiceListener and ServicePublisher instances; owns internal
33 // objects needed to instantiate them such as MdnsResponderService and runs an
34 // event loop.
35 // TODO(btolsch): This may be renamed and/or split up once QUIC code lands and
36 // this use case is more concrete.
37 class InternalServices : UdpSocket::Client {
38  public:
39   static std::unique_ptr<ServiceListener> CreateListener(
40       const MdnsServiceListenerConfig& config,
41       ServiceListener::Observer* observer,
42       TaskRunner* task_runner);
43   static std::unique_ptr<ServicePublisher> CreatePublisher(
44       const ServicePublisher::Config& config,
45       ServicePublisher::Observer* observer,
46       TaskRunner* task_runner);
47 
48   // UdpSocket::Client overrides.
49   void OnError(UdpSocket* socket, Error error) override;
50   void OnSendError(UdpSocket* socket, Error error) override;
51   void OnRead(UdpSocket* socket, ErrorOr<UdpPacket> packet) override;
52 
53  private:
54   class InternalPlatformLinkage final : public MdnsPlatformService {
55    public:
56     explicit InternalPlatformLinkage(InternalServices* parent);
57     ~InternalPlatformLinkage() override;
58 
59     std::vector<BoundInterface> RegisterInterfaces(
60         const std::vector<NetworkInterfaceIndex>& allowlist) override;
61     void DeregisterInterfaces(
62         const std::vector<BoundInterface>& registered_interfaces) override;
63 
64    private:
65     InternalServices* const parent_;
66     std::vector<std::unique_ptr<UdpSocket>> open_sockets_;
67   };
68 
69   // The TaskRunner provided here should live for the duration of this
70   // InternalService object's lifetime.
71   InternalServices(ClockNowFunctionPtr now_function, TaskRunner* task_runner);
72   ~InternalServices() override;
73 
74   void RegisterMdnsSocket(UdpSocket* socket);
75   void DeregisterMdnsSocket(UdpSocket* socket);
76 
77   static InternalServices* ReferenceSingleton(TaskRunner* task_runner);
78   static void DereferenceSingleton(void* instance);
79 
80   MdnsResponderService mdns_service_;
81 
82   TaskRunner* const task_runner_;
83 
84   OSP_DISALLOW_COPY_AND_ASSIGN(InternalServices);
85 };
86 
87 }  // namespace osp
88 }  // namespace openscreen
89 
90 #endif  // OSP_IMPL_INTERNAL_SERVICES_H_
91