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