summaryrefslogtreecommitdiff
path: root/common/device/com/android/net/module/util/netlink/StructNdOptRdnss.java
blob: 6dee0c496bbf0b951a3d9da822c90716a728a086 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.net.module.util.netlink;

import static com.android.net.module.util.NetworkStackConstants.IPV6_ADDR_LEN;

import android.util.Log;

import androidx.annotation.NonNull;

import com.android.net.module.util.Struct;
import com.android.net.module.util.structs.RdnssOption;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * The Recursive DNS Server Option. RFC 8106.
 *
 * 0                   1                   2                   3
 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |     Type      |     Length    |           Reserved            |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                           Lifetime                            |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 * |                                                               |
 * :            Addresses of IPv6 Recursive DNS Servers            :
 * |                                                               |
 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */
public class StructNdOptRdnss extends NdOption {
    private static final String TAG = StructNdOptRdnss.class.getSimpleName();
    public static final int TYPE = 25;
    // Length in 8-byte units, only if one IPv6 address included.
    public static final byte MIN_OPTION_LEN = 3;

    public final RdnssOption header;
    @NonNull
    public final Inet6Address[] servers;

    public StructNdOptRdnss(@NonNull final Inet6Address[] servers, long lifetime) {
        super((byte) TYPE, servers.length * 2 + 1);

        Objects.requireNonNull(servers, "Recursive DNS Servers address array must not be null");
        if (servers.length == 0) {
            throw new IllegalArgumentException("DNS server address array must not be empty");
        }

        this.header = new RdnssOption((byte) TYPE, (byte) (servers.length * 2 + 1),
                (short) 0 /* reserved */, lifetime);
        this.servers = servers.clone();
    }

    /**
     * Parses an RDNSS option from a {@link ByteBuffer}.
     *
     * @param buf The buffer from which to parse the option. The buffer's byte order must be
     *            {@link java.nio.ByteOrder#BIG_ENDIAN}.
     * @return the parsed option, or {@code null} if the option could not be parsed successfully.
     */
    public static StructNdOptRdnss parse(@NonNull ByteBuffer buf) {
        if (buf == null || buf.remaining() < MIN_OPTION_LEN * 8) return null;
        try {
            final RdnssOption header = Struct.parse(RdnssOption.class, buf);
            if (header.type != TYPE) {
                throw new IllegalArgumentException("Invalid type " + header.type);
            }
            if (header.length < MIN_OPTION_LEN || (header.length % 2 == 0)) {
                throw new IllegalArgumentException("Invalid length " + header.length);
            }

            final int numOfDnses = (header.length - 1) / 2;
            final Inet6Address[] servers = new Inet6Address[numOfDnses];
            for (int i = 0; i < numOfDnses; i++) {
                byte[] rawAddress = new byte[IPV6_ADDR_LEN];
                buf.get(rawAddress);
                servers[i] = (Inet6Address) InetAddress.getByAddress(rawAddress);
            }
            return new StructNdOptRdnss(servers, header.lifetime);
        } catch (IllegalArgumentException | BufferUnderflowException | UnknownHostException e) {
            // Not great, but better than throwing an exception that might crash the caller.
            // Convention in this package is that null indicates that the option was truncated
            // or malformed, so callers must already handle it.
            Log.d(TAG, "Invalid RDNSS option: " + e);
            return null;
        }
    }

    protected void writeToByteBuffer(ByteBuffer buf) {
        header.writeToByteBuffer(buf);
        for (int i = 0; i < servers.length; i++) {
            buf.put(servers[i].getAddress());
        }
    }

    /** Outputs the wire format of the option to a new big-endian ByteBuffer. */
    public ByteBuffer toByteBuffer() {
        final ByteBuffer buf = ByteBuffer.allocate(Struct.getSize(RdnssOption.class)
                + servers.length * IPV6_ADDR_LEN);
        writeToByteBuffer(buf);
        buf.flip();
        return buf;
    }

    @Override
    @NonNull
    public String toString() {
        final StringJoiner sj = new StringJoiner(",", "[", "]");
        for (int i = 0; i < servers.length; i++) {
            sj.add(servers[i].getHostAddress());
        }
        return String.format("NdOptRdnss(%s,servers:%s)", header.toString(), sj.toString());
    }
}