Android-cuttlefish cvd tool
discovered_services.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2025 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <android-base/thread_annotations.h>
20
21#include <cstdint>
22#include <cstring>
23#include <format>
24#include <functional>
25#include <iostream>
26#include <memory>
27#include <mutex>
28#include <optional>
29#include <string>
30#include <tuple>
31#include <unordered_map>
32#include <unordered_set>
33#include <vector>
34
36 uint8_t bytes[4];
37
38 bool operator==(const IPv4Address& other) const {
39 return std::memcmp(bytes, other.bytes, sizeof(bytes)) == 0;
40 }
41};
42
44 uint8_t bytes[16];
45
46 std::string to_string() const {
47 std::string result;
48 result.reserve(39);
49
50 for (size_t i = 0; i < 16; i += 2) {
51 uint16_t word = (static_cast<uint16_t>(bytes[i]) << 8) | bytes[i + 1];
52 result += std::format("{:x}", word);
53
54 if (i < 14) {
55 result += ":";
56 }
57 }
58 return result;
59 }
60
61 // Allow the struct to be used in an unordered_set
62 bool operator<(const IPv6Address& other) const {
63 return std::memcmp(bytes, other.bytes, sizeof(bytes)) < 0;
64 }
65
66 bool operator==(const IPv6Address& other) const {
67 return std::memcmp(bytes, other.bytes, sizeof(bytes)) == 0;
68 }
69};
70
72 std::size_t operator()(const IPv6Address& addr) const {
73 return std::hash<std::string_view>{}(
74 std::string_view(reinterpret_cast<const char*>(addr.bytes), sizeof(addr.bytes)));
75 }
76};
77
79 ServiceInfo() = default;
80
81 ServiceInfo(const std::string& in_instance, const std::string& in_service,
82 const std::string& hostname, const std::optional<IPv4Address>& in_v4_address,
83 const std::unordered_set<IPv6Address, IPv6AddressHash>& in_v6_addresses,
84 const uint16_t in_port, const std::vector<std::vector<uint8_t>>& txt)
85 : instance(in_instance),
86 service(in_service),
88 v4_address(in_v4_address),
89 v6_addresses(in_v6_addresses),
90 port(in_port),
91 attributes(ParseTxt(txt)) {}
92
93 std::string instance;
94 std::string service;
95 std::string hostname;
96 std::optional<IPv4Address> v4_address;
97 std::unordered_set<IPv6Address, IPv6AddressHash> v6_addresses;
98 uint16_t port;
99
100 // Store keys/values from TXT resource record
101 std::unordered_map<std::string, std::string> attributes;
102
103 std::string v4_address_string() const {
104 if (!v4_address.has_value()) {
105 return "";
106 }
107 return std::format("{}.{}.{}.{}", v4_address->bytes[0], v4_address->bytes[1],
108 v4_address->bytes[2], v4_address->bytes[3]);
109 }
110
111 // Parse a key/value from a TXT record. Format expected is "key=value"
112 static std::tuple<bool, std::string, std::string> ParseTxtKeyValue(const std::string& kv);
113 static std::unordered_map<std::string, std::string> ParseTxt(
114 const std::vector<std::vector<uint8_t>>& txt);
115};
116
117std::ostream& operator<<(std::ostream& os, const ServiceInfo& info);
118
123};
124
125namespace mdns {
127 public:
128 void ServiceCreated(const ServiceInfo& service_info);
129
130 // Return true if the provided service_info resulted in an update
131 // of the internal state of DiscoveredServices
132 bool ServiceUpdated(const ServiceInfo& service_info);
133
134 void ServiceDeleted(const ServiceInfo& service_info);
135 std::optional<ServiceInfo> FindInstance(const std::string& service,
136 const std::string& instance);
137 void ForEachServiceNamed(const std::string& service,
138 const std::function<void(const ServiceInfo&)>& callback);
139 void ForAllServices(const std::function<void(const ServiceInfo&)>& callback);
140
141 private:
142 std::mutex services_mutex_;
143 std::unordered_map<std::string, ServiceInfo> services_ GUARDED_BY(services_mutex_);
144};
145
147} // namespace mdns
Definition: discovered_services.h:126
void ForAllServices(const std::function< void(const ServiceInfo &)> &callback)
Definition: discovered_services.cpp:144
void ForEachServiceNamed(const std::string &service, const std::function< void(const ServiceInfo &)> &callback)
Definition: discovered_services.cpp:134
void ServiceCreated(const ServiceInfo &service_info)
Definition: discovered_services.cpp:70
std::unordered_map< std::string, ServiceInfo > services_ GUARDED_BY(services_mutex_)
bool ServiceUpdated(const ServiceInfo &service_info)
Definition: discovered_services.cpp:76
void ServiceDeleted(const ServiceInfo &service_info)
Definition: discovered_services.cpp:118
std::optional< ServiceInfo > FindInstance(const std::string &service, const std::string &instance)
Definition: discovered_services.cpp:124
std::mutex services_mutex_
Definition: discovered_services.h:142
std::ostream & operator<<(std::ostream &os, const ServiceInfo &info)
Definition: discovered_services.cpp:23
ServiceInfoState
Definition: discovered_services.h:119
@ Deleted
Definition: discovered_services.h:122
@ Updated
Definition: discovered_services.h:121
@ Created
Definition: discovered_services.h:120
struct fs_info info
Definition: ext4_utils.cpp:42
EventFormat format
Definition: kernel_log_server.cc:61
Definition: discovered_services.cpp:63
DiscoveredServices discovered_services
Definition: discovered_services.cpp:64
Definition: discovered_services.h:35
uint8_t bytes[4]
Definition: discovered_services.h:36
bool operator==(const IPv4Address &other) const
Definition: discovered_services.h:38
Definition: discovered_services.h:71
std::size_t operator()(const IPv6Address &addr) const
Definition: discovered_services.h:72
Definition: discovered_services.h:43
bool operator<(const IPv6Address &other) const
Definition: discovered_services.h:62
uint8_t bytes[16]
Definition: discovered_services.h:44
bool operator==(const IPv6Address &other) const
Definition: discovered_services.h:66
std::string to_string() const
Definition: discovered_services.h:46
Definition: discovered_services.h:78
std::string v4_address_string() const
Definition: discovered_services.h:103
std::string service
Definition: discovered_services.h:94
static std::unordered_map< std::string, std::string > ParseTxt(const std::vector< std::vector< uint8_t > > &txt)
Definition: discovered_services.cpp:48
ServiceInfo(const std::string &in_instance, const std::string &in_service, const std::string &hostname, const std::optional< IPv4Address > &in_v4_address, const std::unordered_set< IPv6Address, IPv6AddressHash > &in_v6_addresses, const uint16_t in_port, const std::vector< std::vector< uint8_t > > &txt)
Definition: discovered_services.h:81
std::optional< IPv4Address > v4_address
Definition: discovered_services.h:96
uint16_t port
Definition: discovered_services.h:98
ServiceInfo()=default
std::string instance
Definition: discovered_services.h:93
static std::tuple< bool, std::string, std::string > ParseTxtKeyValue(const std::string &kv)
Definition: discovered_services.cpp:29
std::unordered_map< std::string, std::string > attributes
Definition: discovered_services.h:101
std::unordered_set< IPv6Address, IPv6AddressHash > v6_addresses
Definition: discovered_services.h:97
std::string hostname
Definition: discovered_services.h:95