Kevin Cernekee | 95d4ae9 | 2016-06-19 10:26:29 -0700 | [diff] [blame^] | 1 | // Copyright 2016 The Chromium OS 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 ARC_NETWORKD_MULTICAST_FORWARDER_H_ |
| 6 | #define ARC_NETWORKD_MULTICAST_FORWARDER_H_ |
| 7 | |
| 8 | #include <netinet/ip.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <time.h> |
| 11 | |
| 12 | #include <deque> |
| 13 | #include <memory> |
| 14 | #include <string> |
| 15 | |
| 16 | #include <base/macros.h> |
| 17 | #include <base/memory/weak_ptr.h> |
| 18 | #include <base/message_loop/message_loop.h> |
| 19 | |
| 20 | #include "arc-networkd/multicast_socket.h" |
| 21 | |
| 22 | using MessageLoopForIO = base::MessageLoopForIO; |
| 23 | |
| 24 | namespace arc_networkd { |
| 25 | |
| 26 | // Listens on a well-known port and forwards multicast messages between |
| 27 | // network interfaces. Handles stateless mDNS messages (src port and |
| 28 | // dst port are both 5353) and stateful mDNS/SSDP messages (src port |
| 29 | // is random, so the forwarder needs to keep a table of open sessions). |
| 30 | class MulticastForwarder : public MessageLoopForIO::Watcher { |
| 31 | public: |
| 32 | MulticastForwarder() {} |
| 33 | virtual ~MulticastForwarder() {} |
| 34 | |
| 35 | // Start forwarding multicast packets between the container's P2P link |
| 36 | // |int_ifname| and the external LAN interface |lan_ifname|. This |
| 37 | // only forwards traffic on multicast address |mcast_addr| and UDP |
| 38 | // port |port|. If |allow_stateless| is true, packets with |
| 39 | // src_port == dst_port == |port| are always passed to the other |
| 40 | // interface without creating a state table entry. If it is false, |
| 41 | // sessions must be initiated from |int_ifname| and will always |
| 42 | // create a state table entry; "unsolicited" traffic from |
| 43 | // |lan_ifname| will be silently discarded. |
| 44 | bool Start(const std::string& int_ifname, |
| 45 | const std::string& lan_ifname, |
| 46 | const std::string& mcast_addr, |
| 47 | unsigned short port, |
| 48 | bool allow_stateless); |
| 49 | |
| 50 | // MessageLoopForIO::Watcher overrides. |
| 51 | void OnFileCanReadWithoutBlocking(int fd) override; |
| 52 | void OnFileCanWriteWithoutBlocking(int fd) override {} |
| 53 | |
| 54 | protected: |
| 55 | void CleanupTask(); |
| 56 | |
| 57 | std::string int_ifname_; |
| 58 | std::string lan_ifname_; |
| 59 | struct in_addr mcast_addr_; |
| 60 | unsigned int port_; |
| 61 | bool allow_stateless_; |
| 62 | |
| 63 | std::unique_ptr<MulticastSocket> int_socket_; |
| 64 | std::unique_ptr<MulticastSocket> lan_socket_; |
| 65 | std::deque<std::unique_ptr<MulticastSocket>> temp_sockets_; |
| 66 | |
| 67 | base::WeakPtrFactory<MulticastForwarder> weak_factory_{this}; |
| 68 | |
| 69 | private: |
| 70 | DISALLOW_COPY_AND_ASSIGN(MulticastForwarder); |
| 71 | }; |
| 72 | |
| 73 | } // namespace arc_networkd |
| 74 | |
| 75 | #endif // ARC_NETWORKD_MULTICAST_FORWARDER_H_ |