Use LAN name from Avahi instead of assuming it's the D-Bus machine id.

This way p2p works as intended with any LAN name and also works if
it's changed on the fly via D-Bus using e.g. the gdbus command like
this:

 # gdbus call --system --dest org.freedesktop.Avahi --object-path / \
     --method org.freedesktop.Avahi.Server.SetHostName some-new-lan-name

Notably, this change allows us to delete all the code dealing with
obtaining the D-Bus machine-id.

BUG=chromium:245758
TEST=Unit tests pass + Changed LAN name (via the gdbus command) while
    avahi-daemon and p2p-server was running and checked (via the
    p2p-client command) that other devices could still see files
    exported by the devices, under the new name.

Change-Id: Idb1d47ff5feadd0e51eb0be39e906aa3ad659952
Reviewed-on: https://chromium-review.googlesource.com/171480
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: David Zeuthen <zeuthen@chromium.org>
Tested-by: David Zeuthen <zeuthen@chromium.org>
diff --git a/client/service_finder.cc b/client/service_finder.cc
index 5f4b9ff..f88ba0c 100644
--- a/client/service_finder.cc
+++ b/client/service_finder.cc
@@ -287,9 +287,7 @@
 }
 
 bool ServiceFinderAvahi::IsOwnService(const char *name) {
-  // Here we rely on the implementation detail that the DNS-SD name
-  // used is the D-Bus machine-id.
-  return g_strcmp0(name, p2p::util::GetDBusMachineId()) == 0;
+  return g_strcmp0(name, avahi_client_get_host_name(client_)) == 0;
 }
 
 static string ToString(AvahiBrowserEvent event) {
diff --git a/common/util.cc b/common/util.cc
index fef9293..e338f09 100644
--- a/common/util.cc
+++ b/common/util.cc
@@ -66,52 +66,6 @@
   logging::SetLogMessageHandler(SyslogFunc);
 }
 
-// The encoding of the D-Bus machine-id plus a NUL terminator is 33
-// bytes. See http://dbus.freedesktop.org/doc/dbus-specification.html
-const size_t kDBusMachineIdPlusNulSize = 33;
-
-// Reads the D-Bus machine-id into |buf| and ensures that it's
-// NUL-terminated. It is a programming error to pass a |buf|
-// that is not at least |kDBusMachineIdPlusNulSize| bytes long.
-static void ReadMachineId(char *buf) {
-  size_t num_read = 0;
-  FILE* f = NULL;
-
-  // NUL-terminate ahead of time.
-  buf[kDBusMachineIdPlusNulSize - 1] = '\0';
-
-  f = fopen("/var/lib/dbus/machine-id", "r");
-  if (f == NULL) {
-    LOG(ERROR) << "Error opening /var/lib/dbus/machine-id: " << strerror(errno);
-    return;
-  }
-
-  // The machine-id file may include a newline so only request 32 bytes.
-  num_read = fread(buf, 1, kDBusMachineIdPlusNulSize - 1, f);
-  if (num_read != kDBusMachineIdPlusNulSize - 1) {
-    LOG(ERROR) << "Error reading from /var/lib/dbus/machine-id, num_read="
-               << num_read;
-    fclose(f);
-    return;
-  }
-
-  VLOG(1) << "Read machine-id " << buf;
-
-  fclose(f);
-}
-
-const char* GetDBusMachineId() {
-  static char machine_id[kDBusMachineIdPlusNulSize] = { 0 };
-
-  if (machine_id[0] == '\0') {
-    G_STATIC_ASSERT(sizeof machine_id == kDBusMachineIdPlusNulSize);
-    ReadMachineId(machine_id);
-  }
-
-  return const_cast<const char *>(machine_id);
-}
-
-
 bool IsXAttrSupported(const base::FilePath& dir_path) {
   char *path = strdup(dir_path.Append("xattr_test_XXXXXX").value().c_str());
 
diff --git a/common/util.h b/common/util.h
index 30973ac..e95a9fa 100644
--- a/common/util.h
+++ b/common/util.h
@@ -17,12 +17,6 @@
 // |program_name| and, if |include_pid| is true, the process id.
 void SetupSyslog(const char* program_name, bool include_pid);
 
-// Gets the D-Bus machine id.
-//
-// This is not thread safe and blocks the calling thread the first
-// time it is called.
-const char* GetDBusMachineId();
-
 // Checks if xattr is supported in the directory specified by
 // |dir_path| which must be writable. Returns true if the feature is
 // supported, false if not or if an error occured.
diff --git a/server/service_publisher.cc b/server/service_publisher.cc
index a061f1e..17a9094 100644
--- a/server/service_publisher.cc
+++ b/server/service_publisher.cc
@@ -68,9 +68,9 @@
   // The TCP port of the HTTP server.
   uint16_t http_port_;
 
-  // The D-Bus/systemd machine-id. This is used as the identifier
-  // of the DNS-SD service being exported via mDNS.
-  string machine_id_;
+  // The LAN name currently used by Avahi. This is used as the
+  // identifier of the DNS-SD service being exported via mDNS.
+  string lan_name_;
 
   // Object used for integrating Avahi with the GLib mainloop.
   AvahiGLibPoll* poll_;
@@ -179,7 +179,7 @@
                                               AVAHI_IF_UNSPEC,
                                               AVAHI_PROTO_UNSPEC,
                                               (AvahiPublishFlags) 0,
-                                              machine_id_.c_str(),
+                                              lan_name_.c_str(),
                                               "_cros_p2p._tcp",
                                               /* service type */
                                               NULL,       /* domain */
@@ -203,7 +203,7 @@
                                                 AVAHI_IF_UNSPEC,
                                                 AVAHI_PROTO_UNSPEC,
                                                 (AvahiPublishFlags) 0,
-                                                machine_id_.c_str(),
+                                                lan_name_.c_str(),
                                                 "_cros_p2p._tcp",
                                                 /* service type */
                                                 NULL, /* domain */
@@ -226,7 +226,15 @@
 
   VLOG(1) << "OnAvahiChanged, state=" << state;
   if (state == AVAHI_CLIENT_S_RUNNING) {
-    VLOG(1) << "Server running, publishing services";
+    // Free the existing group, if there is one. This can happen if
+    // e.g. the LAN name used by Avahi changes.
+    if (publisher->group_ != NULL) {
+      avahi_entry_group_free(publisher->group_);
+      publisher->group_ = NULL;
+    }
+    publisher->lan_name_ = string(avahi_client_get_host_name(client));
+    VLOG(1) << "Server running, publishing services using LAN name '"
+            << publisher->lan_name_ << "'";
     publisher->Publish(false);
   }
 }
@@ -234,8 +242,6 @@
 bool ServicePublisherAvahi::Init() {
   int error;
 
-  machine_id_ = p2p::util::GetDBusMachineId();
-
   poll_ = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
   client_ = avahi_client_new(avahi_glib_poll_get(poll_),
                              (AvahiClientFlags) 0,