1 /*
2  * Copyright (C) 2010 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 #include "jni.h"
18 #include <sys/socket.h>
19 #include "ScopedByteBufferArray.h"
20 
21 // Convert from sockaddr_storage to Inet4Address (AF_INET) or Inet6Address (AF_INET6).
22 // If 'port' is non-NULL and the address family includes a notion
23 // of port number, *port will be set to the port number.
24 jobject sockaddrToInetAddress(JNIEnv* env, const sockaddr_storage& ss, int* port);
25 
26 // Convert from InetAddress to sockaddr_storage. An Inet6Address will be converted to an
27 // AF_INET6 sockaddr_in6. An Inet4Address will be converted to an IPv4-mapped AF_INET6
28 // sockaddr_in6. This is what you want if you're about to perform an operation on a socket,
29 // since all our sockets are AF_INET6.
30 bool inetAddressToSockaddr(JNIEnv* env, jobject inetAddress, int port,
31                            sockaddr_storage& ss, socklen_t& sa_len);
32 
33 // Convert from InetAddress to sockaddr_storage. An Inet6Address will be converted to an
34 // AF_INET6 sockaddr_in6. An Inet4Address will be converted to a sockaddr_in. This is probably
35 // only useful for getnameinfo(2), where we'll be presenting the result to the user and the
36 // user may actually care whether the original address was pure IPv4 or an IPv4-mapped IPv6
37 // address, and for the MCAST_JOIN_GROUP, MCAST_LEAVE_GROUP, and other multicast socket
38 // options.
39 bool inetAddressToSockaddrVerbatim(JNIEnv* env, jobject inetAddress, int port,
40                                    sockaddr_storage& ss, socklen_t& sa_len);
41 
42 // Convert from StructMsghdr to msghdr,
43 // set all fields except msg_name which would be set outside since IPv4 fallback handled outside
44 bool msghdrJavaToC(JNIEnv* env, jobject structMsghdr, struct msghdr& mhdr,
45                    ScopedByteBufferArray& scopedBufArray);
46 
47 // Convert from msghdr to StructMsghdr,
48 // msg_iov/msg_control need to be set since they are output parameters;
49 bool msghdrCToJava(JNIEnv* env, jobject structMsghdr, struct msghdr& mhdr,
50                    ScopedByteBufferArray& scopedBufArray);
51 
52 // Changes 'fd' to be blocking/non-blocking. Returns false and sets errno on failure.
53 // @Deprecated - use IoUtils.setBlocking
54 bool setBlocking(int fd, bool blocking);
55