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  * Netlink socket address.
29  *
30  * Corresponds to Linux's {@code struct sockaddr_nl} from
31  * <a href="https://github.com/torvalds/linux/blob/master/include/uapi/linux/netlink.h">&lt;linux/netlink.h&gt;</a>.
32  *
33  * Netlink socket address descirbes a netlink client is user space or in kernel.
34  * A {@link NetlinkSocketAddress} can be either unicast (only sent to one peer)
35  * or sent to netlink multicast groups ({@code nlGroupsMask} not equal 0).
36  *
37  * Any {@link NetlinkSocketAddress} is described by {@code nlPortId} and {@code nlGroupsMask}.
38  *
39  * {@code nlPortId} is the unicast address of netlink socket. It's always 0
40  * if the destination is in the kernel. For a user-space process,
41  * {@nlPortId} is usually the PID of the process owning the destination
42  * socket. However, {@nlPortId} identifies a netlink socket, not a
43  * process. If a process owns several netlink sockets, then {@nlPortId}
44  * can be equal to the process ID only for at most one socket.
45  *
46  * {@code nlGroupsMask} is a bit mask with every bit representing a netlink
47  * group number. The default value for this field is zero which means that
48  * no multicasts will be received. A socket may multicast messages to any
49  * of the multicast groups by setting {@code nlGroupsMask} to a bit mask of
50  * the groups it wishes to send to.
51  *
52  * @hide
53  */
54 @SystemApi(client = MODULE_LIBRARIES)
55 public final class NetlinkSocketAddress extends SocketAddress {
56     /**
57      * port ID
58      *
59      * @hide
60      */
61     private final int nlPortId;
62 
63     /**
64      * multicast groups mask
65      *
66      * @hide
67      */
68     private final int nlGroupsMask;
69 
70     /**
71      * @hide
72      */
73     // VisibleForTesting
NetlinkSocketAddress()74     public NetlinkSocketAddress() {
75         this(0, 0);
76     }
77     /**
78      * @hide
79      */
80     // VisibleForTesting
NetlinkSocketAddress(int nlPortId)81     public NetlinkSocketAddress(int nlPortId) {
82         this(nlPortId, 0);
83     }
84 
85     /**
86      * Constructs an instance with the given port id and groups mask.
87      *
88      * @param nlPortId     port id
89      * @param nlGroupsMask groups mask
90      *
91      * @hide
92      */
93     @UnsupportedAppUsage
94     @SystemApi(client = MODULE_LIBRARIES)
NetlinkSocketAddress(int nlPortId, int nlGroupsMask)95     public NetlinkSocketAddress(int nlPortId, int nlGroupsMask) {
96         this.nlPortId = nlPortId;
97         this.nlGroupsMask = nlGroupsMask;
98     }
99 
100     /**
101      * Returns this address's port id.
102      *
103      * @return port id
104      *
105      * @hide
106      */
107     @SystemApi(client = MODULE_LIBRARIES)
getPortId()108     public int getPortId() {
109         return nlPortId;
110     }
111 
112     /**
113      * Returns this address's groups multicast mask.
114      *
115      * @return groups mask
116      *
117      * @hide
118      */
119     @SystemApi(client = MODULE_LIBRARIES)
getGroupsMask()120     public int getGroupsMask() {
121         return nlGroupsMask;
122     }
123 
124     /**
125      * @hide
126      */
toString()127     @Override public String toString() {
128       return Objects.toString(this);
129     }
130 }
131