Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/linux/vm/ifaddrs/ifaddrs-android.h
48454 views
1
// From https://android.googlesource.com/platform/libcore/+/f3afb1b6a4ace33bd60e5801bdb2fcb2e935d486/luni/src/main/native/ifaddrs-android.h
2
3
/*
4
* Copyright (C) 2009 The Android Open Source Project
5
*
6
* Licensed under the Apache License, Version 2.0 (the "License");
7
* you may not use this file except in compliance with the License.
8
* You may obtain a copy of the License at
9
*
10
* http://www.apache.org/licenses/LICENSE-2.0
11
*
12
* Unless required by applicable law or agreed to in writing, software
13
* distributed under the License is distributed on an "AS IS" BASIS,
14
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
* See the License for the specific language governing permissions and
16
* limitations under the License.
17
*/
18
#ifndef IFADDRS_ANDROID_H_included
19
#define IFADDRS_ANDROID_H_included
20
#include <arpa/inet.h>
21
#include <cstring>
22
#include <errno.h>
23
#include <net/if.h>
24
#include <netinet/in.h>
25
#include <new>
26
#include <sys/ioctl.h>
27
#include <sys/types.h>
28
#include <sys/socket.h>
29
#include <stdio.h>
30
#include <linux/netlink.h>
31
#include <linux/rtnetlink.h>
32
#include "LocalArray.h"
33
#include "ScopedFd.h"
34
// Android (bionic) doesn't have getifaddrs(3)/freeifaddrs(3).
35
// We fake it here, so java_net_NetworkInterface.cpp can use that API
36
// with all the non-portable code being in this file.
37
// Source-compatible subset of the BSD struct.
38
struct ifaddrs {
39
// Pointer to next struct in list, or NULL at end.
40
ifaddrs* ifa_next;
41
// Interface name.
42
char* ifa_name;
43
// Interface flags.
44
unsigned int ifa_flags;
45
// Interface network address.
46
sockaddr* ifa_addr;
47
// Interface netmask.
48
sockaddr* ifa_netmask;
49
ifaddrs(ifaddrs* next)
50
: ifa_next(next), ifa_name(NULL), ifa_flags(0), ifa_addr(NULL), ifa_netmask(NULL)
51
{
52
}
53
~ifaddrs() {
54
delete ifa_next;
55
delete[] ifa_name;
56
delete ifa_addr;
57
delete ifa_netmask;
58
}
59
// Sadly, we can't keep the interface index for portability with BSD.
60
// We'll have to keep the name instead, and re-query the index when
61
// we need it later.
62
bool setNameAndFlagsByIndex(int interfaceIndex) {
63
// Get the name.
64
char buf[IFNAMSIZ];
65
char* name = if_indextoname(interfaceIndex, buf);
66
if (name == NULL) {
67
return false;
68
}
69
ifa_name = new char[strlen(name) + 1];
70
strcpy(ifa_name, name);
71
// Get the flags.
72
ScopedFd fd(socket(AF_INET, SOCK_DGRAM, 0));
73
if (fd.get() == -1) {
74
return false;
75
}
76
ifreq ifr;
77
memset(&ifr, 0, sizeof(ifr));
78
strcpy(ifr.ifr_name, name);
79
int rc = ioctl(fd.get(), SIOCGIFFLAGS, &ifr);
80
if (rc == -1) {
81
return false;
82
}
83
ifa_flags = ifr.ifr_flags;
84
return true;
85
}
86
// Netlink gives us the address family in the header, and the
87
// sockaddr_in or sockaddr_in6 bytes as the payload. We need to
88
// stitch the two bits together into the sockaddr that's part of
89
// our portable interface.
90
void setAddress(int family, void* data, size_t byteCount) {
91
// Set the address proper...
92
sockaddr_storage* ss = new sockaddr_storage;
93
memset(ss, 0, sizeof(*ss));
94
ifa_addr = reinterpret_cast<sockaddr*>(ss);
95
ss->ss_family = family;
96
uint8_t* dst = sockaddrBytes(family, ss);
97
memcpy(dst, data, byteCount);
98
}
99
// Netlink gives us the prefix length as a bit count. We need to turn
100
// that into a BSD-compatible netmask represented by a sockaddr*.
101
void setNetmask(int family, size_t prefixLength) {
102
// ...and work out the netmask from the prefix length.
103
sockaddr_storage* ss = new sockaddr_storage;
104
memset(ss, 0, sizeof(*ss));
105
ifa_netmask = reinterpret_cast<sockaddr*>(ss);
106
ss->ss_family = family;
107
uint8_t* dst = sockaddrBytes(family, ss);
108
memset(dst, 0xff, prefixLength / 8);
109
if ((prefixLength % 8) != 0) {
110
dst[prefixLength/8] = (0xff << (8 - (prefixLength % 8)));
111
}
112
}
113
// Returns a pointer to the first byte in the address data (which is
114
// stored in network byte order).
115
uint8_t* sockaddrBytes(int family, sockaddr_storage* ss) {
116
if (family == AF_INET) {
117
sockaddr_in* ss4 = reinterpret_cast<sockaddr_in*>(ss);
118
return reinterpret_cast<uint8_t*>(&ss4->sin_addr);
119
} else if (family == AF_INET6) {
120
sockaddr_in6* ss6 = reinterpret_cast<sockaddr_in6*>(ss);
121
return reinterpret_cast<uint8_t*>(&ss6->sin6_addr);
122
}
123
return NULL;
124
}
125
private:
126
// Disallow copy and assignment.
127
ifaddrs(const ifaddrs&);
128
void operator=(const ifaddrs&);
129
};
130
// FIXME: use iovec instead.
131
struct addrReq_struct {
132
nlmsghdr netlinkHeader;
133
ifaddrmsg msg;
134
};
135
inline bool sendNetlinkMessage(int fd, const void* data, size_t byteCount) {
136
ssize_t sentByteCount = TEMP_FAILURE_RETRY(send(fd, data, byteCount, 0));
137
return (sentByteCount == static_cast<ssize_t>(byteCount));
138
}
139
inline ssize_t recvNetlinkMessage(int fd, char* buf, size_t byteCount) {
140
return TEMP_FAILURE_RETRY(recv(fd, buf, byteCount, 0));
141
}
142
// Source-compatible with the BSD function.
143
inline int getifaddrs(ifaddrs** result) {
144
// Simplify cleanup for callers.
145
*result = NULL;
146
// Create a netlink socket.
147
ScopedFd fd(socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE));
148
if (fd.get() < 0) {
149
return -1;
150
}
151
// Ask for the address information.
152
addrReq_struct addrRequest;
153
memset(&addrRequest, 0, sizeof(addrRequest));
154
addrRequest.netlinkHeader.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH;
155
addrRequest.netlinkHeader.nlmsg_type = RTM_GETADDR;
156
addrRequest.netlinkHeader.nlmsg_len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(addrRequest)));
157
addrRequest.msg.ifa_family = AF_UNSPEC; // All families.
158
addrRequest.msg.ifa_index = 0; // All interfaces.
159
if (!sendNetlinkMessage(fd.get(), &addrRequest, addrRequest.netlinkHeader.nlmsg_len)) {
160
return -1;
161
}
162
// Read the responses.
163
LocalArray<0> buf(65536); // We don't necessarily have std::vector.
164
ssize_t bytesRead;
165
while ((bytesRead = recvNetlinkMessage(fd.get(), &buf[0], buf.size())) > 0) {
166
nlmsghdr* hdr = reinterpret_cast<nlmsghdr*>(&buf[0]);
167
for (; NLMSG_OK(hdr, (size_t)bytesRead); hdr = NLMSG_NEXT(hdr, bytesRead)) {
168
switch (hdr->nlmsg_type) {
169
case NLMSG_DONE:
170
return 0;
171
case NLMSG_ERROR:
172
return -1;
173
case RTM_NEWADDR:
174
{
175
ifaddrmsg* address = reinterpret_cast<ifaddrmsg*>(NLMSG_DATA(hdr));
176
rtattr* rta = IFA_RTA(address);
177
size_t ifaPayloadLength = IFA_PAYLOAD(hdr);
178
while (RTA_OK(rta, ifaPayloadLength)) {
179
if (rta->rta_type == IFA_LOCAL) {
180
int family = address->ifa_family;
181
if (family == AF_INET || family == AF_INET6) {
182
*result = new ifaddrs(*result);
183
if (!(*result)->setNameAndFlagsByIndex(address->ifa_index)) {
184
return -1;
185
}
186
(*result)->setAddress(family, RTA_DATA(rta), RTA_PAYLOAD(rta));
187
(*result)->setNetmask(family, address->ifa_prefixlen);
188
}
189
}
190
rta = RTA_NEXT(rta, ifaPayloadLength);
191
}
192
}
193
break;
194
}
195
}
196
}
197
// We only get here if recv fails before we see a NLMSG_DONE.
198
return -1;
199
}
200
// Source-compatible with the BSD function.
201
inline void freeifaddrs(ifaddrs* addresses) {
202
delete addresses;
203
}
204
#endif // IFADDRS_ANDROID_H_included
205
206