Jason Jeremy Iman | b610a52 | 2020-03-06 15:36:48 +0900 | [diff] [blame] | 1 | // Copyright 2020 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 | #include <base/bind.h> |
| 6 | #include <base/command_line.h> |
| 7 | #include <base/files/scoped_file.h> |
| 8 | #include <base/macros.h> |
| 9 | #include <brillo/daemons/daemon.h> |
| 10 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 11 | #include "patchpanel/multicast_forwarder.h" |
Jason Jeremy Iman | b610a52 | 2020-03-06 15:36:48 +0900 | [diff] [blame] | 12 | |
| 13 | // Stand-alone daemon to proxy mDNS and SSDP packets between a pair of |
| 14 | // interfaces. Usage: mcastd $physical_ifname $guest_ifname |
| 15 | int main(int argc, char* argv[]) { |
| 16 | base::CommandLine::Init(argc, argv); |
| 17 | base::CommandLine* cl = base::CommandLine::ForCurrentProcess(); |
| 18 | base::CommandLine::StringVector args = cl->GetArgs(); |
| 19 | if (args.size() < 2) { |
| 20 | LOG(ERROR) << "Usage: " << cl->GetProgram().BaseName().value() |
| 21 | << " [physical interface name] [guest interface name]"; |
| 22 | return EXIT_FAILURE; |
| 23 | } |
| 24 | |
| 25 | brillo::Daemon daemon; |
| 26 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 27 | auto mdns_fwd = std::make_unique<patchpanel::MulticastForwarder>( |
| 28 | args[0], patchpanel::kMdnsMcastAddress, |
| 29 | patchpanel::kMdnsMcastAddress6, patchpanel::kMdnsPort); |
Jason Jeremy Iman | b610a52 | 2020-03-06 15:36:48 +0900 | [diff] [blame] | 30 | |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame^] | 31 | auto ssdp_fwd = std::make_unique<patchpanel::MulticastForwarder>( |
| 32 | args[0], patchpanel::kSsdpMcastAddress, |
| 33 | patchpanel::kSsdpMcastAddress6, patchpanel::kSsdpPort); |
Jason Jeremy Iman | b610a52 | 2020-03-06 15:36:48 +0900 | [diff] [blame] | 34 | |
| 35 | // Crostini depends on another daemon (LXD) creating the guest bridge |
| 36 | // interface. This can take a few seconds, so retry if necessary. |
| 37 | bool added_mdns = false, added_ssdp = false; |
| 38 | for (int i = 0; i < 10; i++) { |
| 39 | added_mdns = added_mdns || mdns_fwd->AddGuest(args[1]); |
| 40 | added_ssdp = added_ssdp || ssdp_fwd->AddGuest(args[1]); |
| 41 | if (added_mdns && added_ssdp) |
| 42 | break; |
| 43 | usleep(1000 * 1000 /* 1 second */); |
| 44 | } |
| 45 | if (!added_mdns) |
| 46 | LOG(ERROR) << "mDNS forwarder could not be started on " << args[0] |
| 47 | << " and " << args[1]; |
| 48 | if (!added_ssdp) |
| 49 | LOG(ERROR) << "SSDP forwarder could not be started on " << args[0] |
| 50 | << " and " << args[1]; |
| 51 | if (!added_mdns || !added_ssdp) |
| 52 | return EXIT_FAILURE; |
| 53 | |
| 54 | return daemon.Run(); |
| 55 | } |