-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrosdns_daemon.cc
79 lines (61 loc) · 2.34 KB
/
crosdns_daemon.cc
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
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "crosdns/crosdns_daemon.h"
#include <sysexits.h>
#include <base/logging.h>
#include <chromeos/dbus/service_constants.h>
#include "crosdns/hosts_modifier.h"
namespace {
// Domain for D-Bus error messages.
constexpr char kErrorDomain[] = "crosdns";
// Type for D-Bus error messages.
constexpr char kInternalError[] = "internal_error";
// Path to our modifiable /etc/hosts file, the parent dir is bind mounted
// on top of the dir where the real hosts file is at.
constexpr char kEtcHostsPath[] = "/run/crosdns/hosts";
} // namespace
namespace crosdns {
CrosDnsDaemon::CrosDnsDaemon()
: brillo::DBusServiceDaemon(kCrosDnsServiceName), dbus_adaptor_(this) {}
CrosDnsDaemon::~CrosDnsDaemon() = default;
int CrosDnsDaemon::OnInit() {
int exit_code = DBusServiceDaemon::OnInit();
if (exit_code == EX_OK) {
if (!hosts_modifier_.Init(base::FilePath(kEtcHostsPath))) {
return EX_IOERR;
}
}
return exit_code;
}
void CrosDnsDaemon::RegisterDBusObjectsAsync(
brillo::dbus_utils::AsyncEventSequencer* sequencer) {
dbus_object_ = std::make_unique<brillo::dbus_utils::DBusObject>(
nullptr, bus_, org::chromium::CrosDnsAdaptor::GetObjectPath());
dbus_adaptor_.RegisterWithDBusObject(dbus_object_.get());
dbus_object_->RegisterAsync(
sequencer->GetHandler("CrosDns.RegisterAsync() failed.", true));
}
bool CrosDnsDaemon::SetHostnameIpMapping(brillo::ErrorPtr* err,
const std::string& hostname,
const std::string& ipv4,
const std::string& ipv6) {
std::string err_str;
if (!hosts_modifier_.SetHostnameIpMapping(hostname, ipv4, ipv6, &err_str)) {
*err =
brillo::Error::Create(FROM_HERE, kErrorDomain, kInternalError, err_str);
return false;
}
return true;
}
bool CrosDnsDaemon::RemoveHostnameIpMapping(brillo::ErrorPtr* err,
const std::string& hostname) {
std::string err_str;
if (!hosts_modifier_.RemoveHostnameIpMapping(hostname, &err_str)) {
*err =
brillo::Error::Create(FROM_HERE, kErrorDomain, kInternalError, err_str);
return false;
}
return true;
}
} // namespace crosdns