xref: /aosp_15_r20/external/openscreen/discovery/dnssd/impl/service_key.cc (revision 3f982cf4871df8771c9d4abe6e9a6f8d829b2736)
1 // Copyright 2019 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 #include "discovery/dnssd/impl/service_key.h"
6 
7 #include "absl/strings/str_join.h"
8 #include "absl/strings/str_split.h"
9 #include "discovery/dnssd/impl/conversion_layer.h"
10 #include "discovery/mdns/mdns_records.h"
11 #include "discovery/mdns/public/mdns_constants.h"
12 
13 namespace openscreen {
14 namespace discovery {
15 
16 // The InstanceKey ctor used below cares about the Instance ID of the
17 // MdnsRecord, while this class doesn't, so it's possible that the InstanceKey
18 // ctor will fail for this reason. That's not a problem though - A failure when
19 // creating an InstanceKey would mean that the record we received was invalid,
20 // so there is no reason to continue processing.
ServiceKey(const MdnsRecord & record)21 ServiceKey::ServiceKey(const MdnsRecord& record) {
22   ErrorOr<ServiceKey> key = TryCreate(record);
23   *this = std::move(key.value());
24 }
25 
ServiceKey(const DomainName & domain)26 ServiceKey::ServiceKey(const DomainName& domain) {
27   ErrorOr<ServiceKey> key = TryCreate(domain);
28   *this = std::move(key.value());
29 }
30 
ServiceKey(absl::string_view service,absl::string_view domain)31 ServiceKey::ServiceKey(absl::string_view service, absl::string_view domain)
32     : service_id_(service.data(), service.size()),
33       domain_id_(domain.data(), domain.size()) {
34   OSP_DCHECK(IsServiceValid(service_id_)) << "invalid service id: " << service;
35   OSP_DCHECK(IsDomainValid(domain_id_)) << "invalid domain id: " << domain;
36 }
37 
38 ServiceKey::ServiceKey(const ServiceKey& other) = default;
39 ServiceKey::ServiceKey(ServiceKey&& other) = default;
40 
41 ServiceKey& ServiceKey::operator=(const ServiceKey& rhs) = default;
42 ServiceKey& ServiceKey::operator=(ServiceKey&& rhs) = default;
43 
GetName() const44 DomainName ServiceKey::GetName() const {
45   std::string service_type = service_id().substr(0, service_id().size() - 5);
46   std::string protocol = service_id().substr(service_id().size() - 4);
47   return DomainName{std::move(service_type), std::move(protocol), domain_id_};
48 }
49 
50 // static
TryCreate(const MdnsRecord & record)51 ErrorOr<ServiceKey> ServiceKey::TryCreate(const MdnsRecord& record) {
52   return TryCreate(GetDomainName(record));
53 }
54 
55 // static
TryCreate(const DomainName & names)56 ErrorOr<ServiceKey> ServiceKey::TryCreate(const DomainName& names) {
57   // Size must be at least 4, because the minimum valid label is of the form
58   // <instance>.<service type>.<protocol>.<domain>
59   if (names.labels().size() < 4) {
60     return Error::Code::kParameterInvalid;
61   }
62 
63   // Skip the InstanceId.
64   auto it = ++names.labels().begin();
65 
66   std::string service_name = *it++;
67   const std::string protocol = *it++;
68   const std::string service_id = service_name.append(".").append(protocol);
69   if (!IsServiceValid(service_id)) {
70     return Error::Code::kParameterInvalid;
71   }
72 
73   const std::string domain_id = absl::StrJoin(it, names.labels().end(), ".");
74   if (!IsDomainValid(domain_id)) {
75     return Error::Code::kParameterInvalid;
76   }
77 
78   return ServiceKey(service_id, domain_id);
79 }
80 
81 }  // namespace discovery
82 }  // namespace openscreen
83