1/*
2 * Copyright (c) 2008, 2020, Oracle and/or its affiliates. All rights reserved.
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.  Oracle designates this
9 * particular file as subject to the "Classpath" exception as provided
10 * by Oracle in the LICENSE file that accompanied this code.
11 *
12 * This code is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 * version 2 for more details (a copy is included in the LICENSE file that
16 * accompanied this code).
17 *
18 * You should have received a copy of the GNU General Public License version
19 * 2 along with this work; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23 * or visit www.oracle.com if you need additional information or have any
24 * questions.
25 *
26 */
27@@END_COPYRIGHT@@
28
29#include <stdio.h>
30#ifdef _WIN32
31#include <winsock2.h>
32#include <ws2tcpip.h>
33#else
34#include <sys/types.h>
35#include <sys/socket.h>
36#include <netinet/in.h>
37#include <netinet/tcp.h>
38#endif
39
40/* Defines SO_REUSEPORT */
41#if !defined(SO_REUSEPORT)
42#ifdef _WIN32
43#define SO_REUSEPORT 0
44#elif defined(__linux__)
45#define SO_REUSEPORT 15
46#elif defined(AIX) || defined(MACOSX)
47#define SO_REUSEPORT 0x0200
48#else
49#define SO_REUSEPORT 0
50#endif
51#endif
52
53/* To be able to name the Java constants the same as the C constants without
54   having the preprocessor rewrite those identifiers, add PREFIX_ to all
55   identifiers matching a C constant. The PREFIX_ is filtered out in the
56   makefile. */
57
58@@START_HERE@@
59
60package sun.nio.ch;
61import java.net.SocketOption;
62import java.net.StandardSocketOptions;
63import java.net.ProtocolFamily;
64import java.net.StandardProtocolFamily;
65import java.util.Map;
66import java.util.HashMap;
67class SocketOptionRegistry {
68
69    private SocketOptionRegistry() { }
70
71    private static class RegistryKey {
72        private final SocketOption<?> name;
73        private final ProtocolFamily family;
74        RegistryKey(SocketOption<?> name, ProtocolFamily family) {
75            this.name = name;
76            this.family = family;
77        }
78        public int hashCode() {
79            return name.hashCode() + family.hashCode();
80        }
81        public boolean equals(Object ob) {
82            if (ob == null) return false;
83            if (!(ob instanceof RegistryKey)) return false;
84            RegistryKey other = (RegistryKey)ob;
85            if (this.name != other.name) return false;
86            if (this.family != other.family) return false;
87            return true;
88        }
89    }
90
91    private static class LazyInitialization {
92
93        static final Map<RegistryKey,OptionKey> options = options();
94
95        private static Map<RegistryKey,OptionKey> options() {
96            Map<RegistryKey,OptionKey> map =
97                new HashMap<RegistryKey,OptionKey>();
98            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_BROADCAST,
99                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_BROADCAST));
100            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_KEEPALIVE,
101                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_KEEPALIVE));
102            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_LINGER,
103                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_LINGER));
104            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_SNDBUF,
105                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_SNDBUF));
106            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_RCVBUF,
107                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_RCVBUF));
108            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEADDR,
109                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_REUSEADDR));
110            map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEPORT,
111                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_REUSEPORT));
112            // IPPROTO_TCP is 6
113            map.put(new RegistryKey(StandardSocketOptions.PREFIX_TCP_NODELAY,
114                Net.UNSPEC), new OptionKey(6, TCP_NODELAY));
115
116            // IPPROTO_IP is 0
117            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS,
118                StandardProtocolFamily.INET), new OptionKey(0, IP_TOS));
119            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF,
120                StandardProtocolFamily.INET), new OptionKey(0, IP_MULTICAST_IF));
121            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL,
122                StandardProtocolFamily.INET), new OptionKey(0, IP_MULTICAST_TTL));
123            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP,
124                StandardProtocolFamily.INET), new OptionKey(0, IP_MULTICAST_LOOP));
125
126#ifdef AF_INET6
127            // IPPROTO_IPV6 is 41
128            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS,
129                StandardProtocolFamily.INET6), new OptionKey(41, IPV6_TCLASS));
130            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF,
131                StandardProtocolFamily.INET6), new OptionKey(41, IPV6_MULTICAST_IF));
132            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL,
133                StandardProtocolFamily.INET6), new OptionKey(41, IPV6_MULTICAST_HOPS));
134            map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP,
135                StandardProtocolFamily.INET6), new OptionKey(41, IPV6_MULTICAST_LOOP));
136#endif
137
138            map.put(new RegistryKey(ExtendedSocketOption.PREFIX_SO_OOBINLINE,
139                Net.UNSPEC), new OptionKey(SOL_SOCKET, SO_OOBINLINE));
140            return map;
141        }
142    }
143
144    public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) {
145        RegistryKey key = new RegistryKey(name, family);
146        return LazyInitialization.options.get(key);
147    }
148}
149