• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 <optional>
20 #include <string_view>
21 
22 #include "adb_wifi.h"
23 
24 namespace mdns {
25 
26 struct MdnsInstance {
27     std::string instance_name;   // "my name"
28     std::string service_name;    // "_adb-tls-connect"
29     std::string transport_type;  // either "_tcp" or "_udp"
30 
MdnsInstanceMdnsInstance31     MdnsInstance(std::string_view inst, std::string_view serv, std::string_view trans)
32         : instance_name(inst), service_name(serv), transport_type(trans) {}
33 };
34 
35 // This parser is based on https://tools.ietf.org/html/rfc6763#section-4.1 for
36 // structured service instance names, where the whole name is in the format
37 // <Instance>.<Service>.<Domain>.
38 //
39 // In our case, we ignore <Domain> portion of the name, which
40 // we always assume to be ".local", or link-local mDNS.
41 //
42 // The string can be in one of the following forms:
43 //   - <Instance>.<Service>.<Domain>.?
44 //     - e.g. "instance._service._tcp.local" (or "...local.")
45 //   - <Instance>.<Service>.? (must contain either "_tcp" or "_udp" at the end)
46 //     - e.g. "instance._service._tcp" (or "..._tcp.)
47 //   - <Instance> (can contain dots '.')
48 //     - e.g. "myname", "name.", "my.name."
49 //
50 // Returns an MdnsInstance with the appropriate fields filled in (instance name is never empty),
51 // otherwise returns std::nullopt.
52 std::optional<MdnsInstance> mdns_parse_instance_name(std::string_view name);
53 
54 }  // namespace mdns
55