blob: 706c11b91352846491ffa5be4013c9c403d2b670 [file] [log] [blame]
Qingsi Wang09619332018-09-13 05:51:551/*
2 * Copyright 2018 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef RTC_BASE_FAKE_MDNS_RESPONDER_H_
12#define RTC_BASE_FAKE_MDNS_RESPONDER_H_
13
14#include <map>
15#include <memory>
16#include <string>
17
Ali Tofighd3890782022-04-29 12:02:2218#include "absl/strings/string_view.h"
Mirko Bonadei4a427422019-03-01 14:30:4319#include "rtc_base/ip_address.h"
Qingsi Wang09619332018-09-13 05:51:5520#include "rtc_base/mdns_responder_interface.h"
Mirko Bonadei4a427422019-03-01 14:30:4321#include "rtc_base/thread.h"
Qingsi Wang09619332018-09-13 05:51:5522
23namespace webrtc {
24
Niels Möllerc964d802021-03-25 08:31:2625// This class posts tasks on the given `thread` to invoke callbacks. It's the
26// callback's responsibility to be aware of potential destruction of state it
27// depends on, e.g., using WeakPtrFactory or PendingTaskSafetyFlag.
Qingsi Wang7852d292018-10-31 18:17:0728class FakeMdnsResponder : public MdnsResponderInterface {
Qingsi Wang09619332018-09-13 05:51:5529 public:
Qingsi Wang3ea7b832018-11-07 01:51:0230 explicit FakeMdnsResponder(rtc::Thread* thread) : thread_(thread) {}
Qingsi Wang7852d292018-10-31 18:17:0731 ~FakeMdnsResponder() = default;
Qingsi Wang09619332018-09-13 05:51:5532
33 void CreateNameForAddress(const rtc::IPAddress& addr,
34 NameCreatedCallback callback) override {
35 std::string name;
36 if (addr_name_map_.find(addr) != addr_name_map_.end()) {
37 name = addr_name_map_[addr];
38 } else {
39 name = std::to_string(next_available_id_++) + ".local";
40 addr_name_map_[addr] = name;
41 }
Danil Chapovalov5286dcf2022-07-18 15:04:5642 thread_->PostTask([callback, addr, name]() { callback(addr, name); });
Qingsi Wang09619332018-09-13 05:51:5543 }
44 void RemoveNameForAddress(const rtc::IPAddress& addr,
45 NameRemovedCallback callback) override {
46 auto it = addr_name_map_.find(addr);
47 if (it != addr_name_map_.end()) {
48 addr_name_map_.erase(it);
49 }
Qingsi Wang3ea7b832018-11-07 01:51:0250 bool result = it != addr_name_map_.end();
Danil Chapovalov5286dcf2022-07-18 15:04:5651 thread_->PostTask([callback, result]() { callback(result); });
Qingsi Wang09619332018-09-13 05:51:5552 }
53
Ali Tofighd3890782022-04-29 12:02:2254 rtc::IPAddress GetMappedAddressForName(absl::string_view name) const {
Qingsi Wang1dac6d82018-12-12 23:28:4755 for (const auto& addr_name_pair : addr_name_map_) {
56 if (addr_name_pair.second == name) {
57 return addr_name_pair.first;
58 }
59 }
60 return rtc::IPAddress();
61 }
62
Qingsi Wang09619332018-09-13 05:51:5563 private:
64 uint32_t next_available_id_ = 0;
65 std::map<rtc::IPAddress, std::string> addr_name_map_;
Niels Möllerc964d802021-03-25 08:31:2666 rtc::Thread* const thread_;
Qingsi Wang09619332018-09-13 05:51:5567};
68
69} // namespace webrtc
70
71#endif // RTC_BASE_FAKE_MDNS_RESPONDER_H_