blob: e66d45d0fb85c24171124de773a1318c448cb987 [file] [log] [blame]
Erik Kline6193aa32015-01-20 12:28:26 +09001/*
2 * Copyright (C) 2015 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
17package android.net.netlink;
18
19import android.net.netlink.NetlinkConstants;
20import android.system.OsConstants;
21import java.nio.ByteBuffer;
22
23
24/**
25 * struct ndmsg
26 *
27 * see: <linux_src>/include/uapi/linux/neighbour.h
28 *
29 * @hide
30 */
31public class StructNdMsg {
32 // Already aligned.
33 public static final int STRUCT_SIZE = 12;
34
35 // Neighbor Cache Entry States
36 public static final short NUD_INCOMPLETE = 0x01;
37 public static final short NUD_REACHABLE = 0x02;
38 public static final short NUD_STALE = 0x04;
39 public static final short NUD_DELAY = 0x08;
40 public static final short NUD_PROBE = 0x10;
41 public static final short NUD_FAILED = 0x20;
42 public static final short NUD_NOARP = 0x40;
43 public static final short NUD_PERMANENT = 0x80;
44
45 public static String stringForNudState(short nudState) {
46 switch (nudState) {
47 case NUD_INCOMPLETE: return "NUD_INCOMPLETE";
48 case NUD_REACHABLE: return "NUD_REACHABLE";
49 case NUD_STALE: return "NUD_STALE";
50 case NUD_DELAY: return "NUD_DELAY";
51 case NUD_PROBE: return "NUD_PROBE";
52 case NUD_FAILED: return "NUD_FAILED";
53 case NUD_NOARP: return "NUD_NOARP";
54 case NUD_PERMANENT: return "NUD_PERMANENT";
55 default:
56 return "unknown NUD state: " + String.valueOf(nudState);
57 }
58 }
59
60 public static boolean isNudStateConnected(short nudState) {
61 return ((nudState & (NUD_PERMANENT|NUD_NOARP|NUD_REACHABLE)) != 0);
62 }
63
64 // Neighbor Cache Entry Flags
65 public static byte NTF_USE = (byte) 0x01;
66 public static byte NTF_SELF = (byte) 0x02;
67 public static byte NTF_MASTER = (byte) 0x04;
68 public static byte NTF_PROXY = (byte) 0x08;
69 public static byte NTF_ROUTER = (byte) 0x80;
70
71 public static String stringForNudFlags(byte flags) {
72 final StringBuilder sb = new StringBuilder();
73 if ((flags & NTF_USE) != 0) {
74 sb.append("NTF_USE");
75 }
76 if ((flags & NTF_SELF) != 0) {
77 if (sb.length() > 0) { sb.append("|"); }
78 sb.append("NTF_SELF");
79 }
80 if ((flags & NTF_MASTER) != 0) {
81 if (sb.length() > 0) { sb.append("|"); }
82 sb.append("NTF_MASTER");
83 }
84 if ((flags & NTF_PROXY) != 0) {
85 if (sb.length() > 0) { sb.append("|");
86 }
87 sb.append("NTF_PROXY"); }
88 if ((flags & NTF_ROUTER) != 0) {
89 if (sb.length() > 0) { sb.append("|"); }
90 sb.append("NTF_ROUTER");
91 }
92 return sb.toString();
93 }
94
95 private static boolean hasAvailableSpace(ByteBuffer byteBuffer) {
96 return byteBuffer != null && byteBuffer.remaining() >= STRUCT_SIZE;
97 }
98
99 public static StructNdMsg parse(ByteBuffer byteBuffer) {
100 if (!hasAvailableSpace(byteBuffer)) { return null; }
101
102 // The ByteOrder must have already been set by the caller. In most
103 // cases ByteOrder.nativeOrder() is correct, with the possible
104 // exception of usage within unittests.
105 final StructNdMsg struct = new StructNdMsg();
106 struct.ndm_family = byteBuffer.get();
107 final byte pad1 = byteBuffer.get();
108 final short pad2 = byteBuffer.getShort();
109 struct.ndm_ifindex = byteBuffer.getInt();
110 struct.ndm_state = byteBuffer.getShort();
111 struct.ndm_flags = byteBuffer.get();
112 struct.ndm_type = byteBuffer.get();
113 return struct;
114 }
115
116 public byte ndm_family;
117 public int ndm_ifindex;
118 public short ndm_state;
119 public byte ndm_flags;
120 public byte ndm_type;
121
122 public StructNdMsg() {
123 ndm_family = (byte) OsConstants.AF_UNSPEC;
124 }
125
126 public boolean pack(ByteBuffer byteBuffer) {
127 if (!hasAvailableSpace(byteBuffer)) { return false; }
128
129 // The ByteOrder must have already been set by the caller. In most
130 // cases ByteOrder.nativeOrder() is correct, with the exception
131 // of usage within unittests.
132 byteBuffer.put(ndm_family);
133 byteBuffer.put((byte) 0); // pad1
134 byteBuffer.putShort((short) 0); // pad2
135 byteBuffer.putInt(ndm_ifindex);
136 byteBuffer.putShort(ndm_state);
137 byteBuffer.put(ndm_flags);
138 byteBuffer.put(ndm_type);
139 return true;
140 }
141
142 public boolean nudConnected() {
143 return isNudStateConnected(ndm_state);
144 }
145
146 public boolean nudValid() {
147 return (nudConnected() || ((ndm_state & (NUD_PROBE|NUD_STALE|NUD_DELAY)) != 0));
148 }
149
150 @Override
151 public String toString() {
152 final String stateStr = "" + ndm_state + " (" + stringForNudState(ndm_state) + ")";
153 final String flagsStr = "" + ndm_flags + " (" + stringForNudFlags(ndm_flags) + ")";
154 return "StructNdMsg{ "
155 + "family{" + NetlinkConstants.stringForAddressFamily((int) ndm_family) + "}, "
156 + "ifindex{" + ndm_ifindex + "}, "
157 + "state{" + stateStr + "}, "
158 + "flags{" + flagsStr + "}, "
159 + "type{" + ndm_type + "} "
160 + "}";
161 }
162}