1 /*
2  * Copyright (C) 2021 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 package com.android.server.connectivity.mdns;
18 
19 import static java.nio.charset.StandardCharsets.UTF_8;
20 
21 import java.net.InetAddress;
22 import java.net.InetSocketAddress;
23 import java.net.UnknownHostException;
24 import java.nio.charset.Charset;
25 
26 /** mDNS-related constants. */
27 public final class MdnsConstants {
28     public static final int MDNS_PORT = 5353;
29     // Flags word format is:
30     // 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
31     // QR [ Opcode  ] AA TC RD RA  Z AD CD [  Rcode  ]
32     // See http://www.networksorcery.com/enp/protocol/dns.htm
33     // For responses, QR bit should be 1, AA - CD bits should be ignored, and all other bits
34     // should be 0.
35     public static final int FLAGS_QUERY = 0x0000;
36     public static final int FLAGS_RESPONSE_MASK = 0xF80F;
37     public static final int FLAGS_RESPONSE = 0x8000;
38     public static final int FLAG_TRUNCATED = 0x0200;
39     public static final int QCLASS_INTERNET = 0x0001;
40     public static final int QCLASS_UNICAST = 0x8000;
41     public static final int NO_PACKET = 0;
42     public static final String SUBTYPE_LABEL = "_sub";
43     public static final String SUBTYPE_PREFIX = "_";
44     private static final String MDNS_IPV4_HOST_ADDRESS = "224.0.0.251";
45     private static final String MDNS_IPV6_HOST_ADDRESS = "FF02::FB";
46     public static final InetSocketAddress IPV6_SOCKET_ADDR = new InetSocketAddress(
47             getMdnsIPv6Address(), MDNS_PORT);
48     public static final InetSocketAddress IPV4_SOCKET_ADDR = new InetSocketAddress(
49             getMdnsIPv4Address(), MDNS_PORT);
50     private static InetAddress mdnsAddress;
MdnsConstants()51     private MdnsConstants() {
52     }
53 
getMdnsIPv4Address()54     public static InetAddress getMdnsIPv4Address() {
55         synchronized (MdnsConstants.class) {
56             InetAddress addr = null;
57             try {
58                 addr = InetAddress.getByName(MDNS_IPV4_HOST_ADDRESS);
59             } catch (UnknownHostException e) {
60                 /* won't happen */
61             }
62             mdnsAddress = addr;
63             return mdnsAddress;
64         }
65     }
66 
getMdnsIPv6Address()67     public static InetAddress getMdnsIPv6Address() {
68         synchronized (MdnsConstants.class) {
69             InetAddress addr = null;
70             try {
71                 addr = InetAddress.getByName(MDNS_IPV6_HOST_ADDRESS);
72             } catch (UnknownHostException e) {
73                 /* won't happen */
74             }
75             mdnsAddress = addr;
76             return mdnsAddress;
77         }
78     }
79 
getUtf8Charset()80     public static Charset getUtf8Charset() {
81         return UTF_8;
82     }
83 }
84