|
| 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 | + |
| 60 | +const char *pre = |
| 61 | +"package sun.nio.ch;\n" |
| 62 | +"import java.net.SocketOption;\n" |
| 63 | +"import java.net.StandardSocketOptions;\n" |
| 64 | +"import java.net.ProtocolFamily;\n" |
| 65 | +"import java.net.StandardProtocolFamily;\n" |
| 66 | +"import java.util.Map;\n" |
| 67 | +"import java.util.HashMap;\n" |
| 68 | +"class SocketOptionRegistry {\n" |
| 69 | +"\n" |
| 70 | +" private SocketOptionRegistry() { }\n" |
| 71 | +"\n" |
| 72 | +" private static class RegistryKey {\n" |
| 73 | +" private final SocketOption<?> name;\n" |
| 74 | +" private final ProtocolFamily family;\n" |
| 75 | +" RegistryKey(SocketOption<?> name, ProtocolFamily family) {\n" |
| 76 | +" this.name = name;\n" |
| 77 | +" this.family = family;\n" |
| 78 | +" }\n" |
| 79 | +" public int hashCode() {\n" |
| 80 | +" return name.hashCode() + family.hashCode();\n" |
| 81 | +" }\n" |
| 82 | +" public boolean equals(Object ob) {\n" |
| 83 | +" if (ob == null) return false;\n" |
| 84 | +" if (!(ob instanceof RegistryKey)) return false;\n" |
| 85 | +" RegistryKey other = (RegistryKey)ob;\n" |
| 86 | +" if (this.name != other.name) return false;\n" |
| 87 | +" if (this.family != other.family) return false;\n" |
| 88 | +" return true;\n" |
| 89 | +" }\n" |
| 90 | +" }\n" |
| 91 | +"\n" |
| 92 | +" private static class LazyInitialization {\n" |
| 93 | +"\n" |
| 94 | +" static final Map<RegistryKey,OptionKey> options = options();\n" |
| 95 | +"\n" |
| 96 | +" private static Map<RegistryKey,OptionKey> options() {\n" |
| 97 | +" Map<RegistryKey,OptionKey> map =\n" |
| 98 | +" new HashMap<RegistryKey,OptionKey>();\n"; |
| 99 | + |
| 100 | +const char *post = |
| 101 | +"\n" |
| 102 | +" return map;\n" |
| 103 | +" }\n" |
| 104 | +" }\n" |
| 105 | +"\n" |
| 106 | +" public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) {\n" |
| 107 | +" RegistryKey key = new RegistryKey(name, family);\n" |
| 108 | +" return LazyInitialization.options.get(key);\n" |
| 109 | +" }\n" |
| 110 | +"}\n"; |
| 111 | + |
| 112 | +int main(void) { |
| 113 | + printf("%s\n", pre); |
| 114 | + |
| 115 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_BROADCAST, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_BROADCAST); |
| 116 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_KEEPALIVE, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_KEEPALIVE); |
| 117 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_LINGER, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_LINGER); |
| 118 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_SNDBUF, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_SNDBUF); |
| 119 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_RCVBUF, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_RCVBUF); |
| 120 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEADDR, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_REUSEADDR); |
| 121 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEPORT, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_REUSEPORT); |
| 122 | + |
| 123 | + printf("// IPPROTO_TCP is 6\n"); |
| 124 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_TCP_NODELAY, Net.UNSPEC), new OptionKey(6, %d));\n", TCP_NODELAY); |
| 125 | + |
| 126 | + printf("// IPPROTO_IP is 0\n"); |
| 127 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_TOS); |
| 128 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_MULTICAST_IF); |
| 129 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_MULTICAST_TTL); |
| 130 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP, StandardProtocolFamily.INET), new OptionKey(0, %d));\n", IP_MULTICAST_LOOP); |
| 131 | + |
| 132 | +#ifdef AF_INET6 |
| 133 | + printf("// IPPROTO_IPV6 is 41\n"); |
| 134 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_TCLASS); |
| 135 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_MULTICAST_IF); |
| 136 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_MULTICAST_HOPS); |
| 137 | + printf("map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP, StandardProtocolFamily.INET6), new OptionKey(41, %d));\n", IPV6_MULTICAST_LOOP); |
| 138 | +#endif |
| 139 | + |
| 140 | + printf("map.put(new RegistryKey(ExtendedSocketOption.PREFIX_SO_OOBINLINE, Net.UNSPEC), new OptionKey(SOL_SOCKET, %d));\n", SO_OOBINLINE); |
| 141 | + |
| 142 | + printf("%s\n", post); |
| 143 | + return 0; |
| 144 | +} |
0 commit comments