1 /* 2 * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdio.h> 27 #ifdef _WIN32 28 #include <winsock2.h> 29 #include <ws2tcpip.h> 30 #else 31 #include <sys/types.h> 32 #include <sys/socket.h> 33 #include <netinet/in.h> 34 #include <netinet/tcp.h> 35 #endif 36 37 /** 38 * Generates sun.nio.ch.SocketOptionRegistry, a class that maps Java-level 39 * socket options to the platform specific level and option. 40 */ 41 42 static void out(char* s) { 43 printf("%s\n", s); 44 } 45 46 static void emit(const char *name, char * family, int level, int optname) { 47 printf(" map.put(new RegistryKey(%s, %s),", name, family); 48 printf(" new OptionKey(%d, %d));\n", level, optname); 49 } 50 51 static void emit_unspec(const char *name, int level, int optname) { 52 emit(name, "Net.UNSPEC", level, optname); 53 } 54 55 static void emit_inet(const char *name, int level, int optname) { 56 emit(name, "StandardProtocolFamily.INET", level, optname); 57 } 58 59 static void emit_inet6(const char *name, int level, int optname) { 60 emit(name, "StandardProtocolFamily.INET6", level, optname); 61 } 62 63 int main(int argc, const char* argv[]) { 64 out("// AUTOMATICALLY GENERATED FILE - DO NOT EDIT "); 65 out("package sun.nio.ch; "); 66 out("import java.net.SocketOption; "); 67 out("import java.net.StandardSocketOptions; "); 68 out("import java.net.ProtocolFamily; "); 69 out("import java.net.StandardProtocolFamily; "); 70 out("import java.util.Map; "); 71 out("import java.util.HashMap; "); 72 out("class SocketOptionRegistry { "); 73 out(" private SocketOptionRegistry() { } "); 74 out(" private static class RegistryKey { "); 75 out(" private final SocketOption<?> name; "); 76 out(" private final ProtocolFamily family; "); 77 out(" RegistryKey(SocketOption<?> name, ProtocolFamily family) { "); 78 out(" this.name = name; "); 79 out(" this.family = family; "); 80 out(" } "); 81 out(" public int hashCode() { "); 82 out(" return name.hashCode() + family.hashCode(); "); 83 out(" } "); 84 out(" public boolean equals(Object ob) { "); 85 out(" if (ob == null) return false; "); 86 out(" if (!(ob instanceof RegistryKey)) return false; "); 87 out(" RegistryKey other = (RegistryKey)ob; "); 88 out(" if (this.name != other.name) return false; "); 89 out(" if (this.family != other.family) return false; "); 90 out(" return true; "); 91 out(" } "); 92 out(" } "); 93 out(" private static class LazyInitialization { "); 94 out(" static final Map<RegistryKey,OptionKey> options = options(); "); 95 out(" private static Map<RegistryKey,OptionKey> options() { "); 96 out(" Map<RegistryKey,OptionKey> map = "); 97 out(" new HashMap<RegistryKey,OptionKey>(); "); 98 99 emit_unspec("StandardSocketOptions.SO_BROADCAST", SOL_SOCKET, SO_BROADCAST); 100 emit_unspec("StandardSocketOptions.SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE); 101 emit_unspec("StandardSocketOptions.SO_LINGER", SOL_SOCKET, SO_LINGER); 102 emit_unspec("StandardSocketOptions.SO_SNDBUF", SOL_SOCKET, SO_SNDBUF); 103 emit_unspec("StandardSocketOptions.SO_RCVBUF", SOL_SOCKET, SO_RCVBUF); 104 emit_unspec("StandardSocketOptions.SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR); 105 emit_unspec("StandardSocketOptions.TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY); 106 107 emit_inet("StandardSocketOptions.IP_TOS", IPPROTO_IP, IP_TOS); 108 emit_inet("StandardSocketOptions.IP_MULTICAST_IF", IPPROTO_IP, IP_MULTICAST_IF); 109 emit_inet("StandardSocketOptions.IP_MULTICAST_TTL", IPPROTO_IP, IP_MULTICAST_TTL); 110 emit_inet("StandardSocketOptions.IP_MULTICAST_LOOP", IPPROTO_IP, IP_MULTICAST_LOOP); 111 112 #ifdef AF_INET6 113 emit_inet6("StandardSocketOptions.IP_TOS", IPPROTO_IPV6, IPV6_TCLASS); 114 emit_inet6("StandardSocketOptions.IP_MULTICAST_IF", IPPROTO_IPV6, IPV6_MULTICAST_IF); 115 emit_inet6("StandardSocketOptions.IP_MULTICAST_TTL", IPPROTO_IPV6, IPV6_MULTICAST_HOPS); 116 emit_inet6("StandardSocketOptions.IP_MULTICAST_LOOP", IPPROTO_IPV6, IPV6_MULTICAST_LOOP); 117 #endif 118 119 emit_unspec("ExtendedSocketOption.SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE); 120 121 out(" return map; "); 122 out(" } "); 123 out(" } "); 124 out(" public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) { "); 125 out(" RegistryKey key = new RegistryKey(name, family); "); 126 out(" return LazyInitialization.options.get(key); "); 127 out(" } "); 128 out("} "); 129 130 return 0; 131 }