1 /*
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 
17 package android.system;
18 
19 import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
20 
21 import android.annotation.SystemApi;
22 import android.compat.annotation.UnsupportedAppUsage;
23 
24 import java.net.SocketAddress;
25 import libcore.util.Objects;
26 
27 /**
28  * Packet socket address.
29  *
30  * Corresponds to Linux's {@code struct sockaddr_ll}.
31  *
32  * See <a href="https://man7.org/linux/man-pages/man7/packet.7.html">packet(7)</a>.
33  *
34  * @hide
35  */
36 @SystemApi(client = MODULE_LIBRARIES)
37 @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
38 public final class PacketSocketAddress extends SocketAddress {
39     /**
40      * Protocol. An Ethernet protocol type, e.g., {@link OsConstants#ETH_P_IPV6}.
41      *
42      * @hide
43      */
44     public final int sll_protocol;
45 
46     /**
47      * Interface index.
48      *
49      * @hide
50      */
51     public final int sll_ifindex;
52 
53     /**
54      * ARP hardware type. One of the {@code ARPHRD_*} constants, such as
55      * {@link OsConstants#ARPHRD_ETHER}.
56      *
57      * @hide
58      */
59     public final int sll_hatype;
60 
61     /**
62      * Packet type.
63      *
64      * @hide
65      */
66     public final int sll_pkttype;
67 
68     /**
69      * Hardware address.
70      *
71      * @hide
72      */
73     public final byte[] sll_addr;
74 
75     /**
76      * Constructs a new PacketSocketAddress. Used from native code.
77      *
78      * @hide
79      */
PacketSocketAddress(int sll_protocol, int sll_ifindex, int sll_hatype, int sll_pkttype, byte[] sll_addr)80     public PacketSocketAddress(int sll_protocol, int sll_ifindex, int sll_hatype, int sll_pkttype,
81             byte[] sll_addr) {
82         this.sll_protocol = sll_protocol;
83         this.sll_ifindex = sll_ifindex;
84         this.sll_hatype = sll_hatype;
85         this.sll_pkttype = sll_pkttype;
86         this.sll_addr = sll_addr;
87     }
88 
89     /**
90      * Constructs a new PacketSocketAddress with all the "in" parameters which
91      * correspond to Linux's {@code struct sockaddr_ll}.
92      *
93      * See <a href="https://man7.org/linux/man-pages/man7/packet.7.html">packet(7)</a>.
94      *
95      * @param sll_protocol protocol field in {@code struct sockaddr_ll}
96      * @param sll_ifindex  interface index number field in {@code struct sockaddr_ll}
97      * @param sll_addr     physical-layer address field in {@code struct sockaddr_ll}
98      *
99      * @hide
100      */
101     @SystemApi(client = MODULE_LIBRARIES)
102     @libcore.api.CorePlatformApi(status = libcore.api.CorePlatformApi.Status.STABLE)
PacketSocketAddress(int sll_protocol, int sll_ifindex, byte[] sll_addr)103     public PacketSocketAddress(int sll_protocol, int sll_ifindex, byte[] sll_addr) {
104         this.sll_protocol = sll_protocol;
105         this.sll_ifindex = sll_ifindex;
106         this.sll_hatype = 0;
107         this.sll_pkttype = 0;
108         this.sll_addr = sll_addr;
109     }
110 
111     /**
112      * Legacy constructor. Kept for @UnsupportedAppUsage only.
113      *
114      * @hide
115      */
116     @UnsupportedAppUsage
PacketSocketAddress(short sll_protocol, int sll_ifindex)117     public PacketSocketAddress(short sll_protocol, int sll_ifindex) {
118         this.sll_protocol = sll_protocol;
119         this.sll_ifindex = sll_ifindex;
120         this.sll_hatype = 0;
121         this.sll_pkttype = 0;
122         this.sll_addr = null;
123     }
124 
125     /**
126      * Legacy constructor. Kept for @UnsupportedAppUsage only.
127      *
128      * @hide
129      */
130     @UnsupportedAppUsage
PacketSocketAddress(int sll_ifindex, byte[] sll_addr)131     public PacketSocketAddress(int sll_ifindex, byte[] sll_addr) {
132         this.sll_protocol = 0;
133         this.sll_ifindex = sll_ifindex;
134         this.sll_hatype = 0;
135         this.sll_pkttype = 0;
136         this.sll_addr = sll_addr;
137     }
138 
139     /**
140      * @hide
141      */
142     @Override
toString()143     public String toString() {
144         return Objects.toString(this);
145     }
146 }
147