524N/A/*
2362N/A * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
524N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
524N/A *
524N/A * This code is free software; you can redistribute it and/or modify it
524N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
524N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
524N/A *
524N/A * This code is distributed in the hope that it will be useful, but WITHOUT
524N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
524N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
524N/A * version 2 for more details (a copy is included in the LICENSE file that
524N/A * accompanied this code).
524N/A *
524N/A * You should have received a copy of the GNU General Public License version
524N/A * 2 along with this work; if not, write to the Free Software Foundation,
524N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
524N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
524N/A */
524N/A
524N/A#include <stdio.h>
524N/A#ifdef _WIN32
524N/A#include <winsock2.h>
524N/A#include <ws2tcpip.h>
524N/A#else
4632N/A#include <sys/types.h>
524N/A#include <sys/socket.h>
524N/A#include <netinet/in.h>
524N/A#include <netinet/tcp.h>
524N/A#endif
524N/A
524N/A/**
524N/A * Generates sun.nio.ch.SocketOptionRegistry, a class that maps Java-level
524N/A * socket options to the platform specific level and option.
524N/A */
524N/A
524N/Astatic void out(char* s) {
524N/A printf("%s\n", s);
524N/A}
524N/A
524N/Astatic void emit(const char *name, char * family, int level, int optname) {
524N/A printf(" map.put(new RegistryKey(%s, %s),", name, family);
524N/A printf(" new OptionKey(%d, %d));\n", level, optname);
524N/A}
524N/A
524N/Astatic void emit_unspec(const char *name, int level, int optname) {
524N/A emit(name, "Net.UNSPEC", level, optname);
524N/A}
524N/A
524N/Astatic void emit_inet(const char *name, int level, int optname) {
524N/A emit(name, "StandardProtocolFamily.INET", level, optname);
524N/A}
524N/A
524N/Astatic void emit_inet6(const char *name, int level, int optname) {
524N/A emit(name, "StandardProtocolFamily.INET6", level, optname);
524N/A}
524N/A
524N/Aint main(int argc, const char* argv[]) {
524N/A out("// AUTOMATICALLY GENERATED FILE - DO NOT EDIT ");
524N/A out("package sun.nio.ch; ");
524N/A out("import java.net.SocketOption; ");
4216N/A out("import java.net.StandardSocketOptions; ");
524N/A out("import java.net.ProtocolFamily; ");
524N/A out("import java.net.StandardProtocolFamily; ");
524N/A out("import java.util.Map; ");
524N/A out("import java.util.HashMap; ");
524N/A out("class SocketOptionRegistry { ");
524N/A out(" private SocketOptionRegistry() { } ");
524N/A out(" private static class RegistryKey { ");
893N/A out(" private final SocketOption<?> name; ");
524N/A out(" private final ProtocolFamily family; ");
4216N/A out(" RegistryKey(SocketOption<?> name, ProtocolFamily family) { ");
524N/A out(" this.name = name; ");
524N/A out(" this.family = family; ");
524N/A out(" } ");
524N/A out(" public int hashCode() { ");
524N/A out(" return name.hashCode() + family.hashCode(); ");
524N/A out(" } ");
524N/A out(" public boolean equals(Object ob) { ");
524N/A out(" if (ob == null) return false; ");
524N/A out(" if (!(ob instanceof RegistryKey)) return false; ");
524N/A out(" RegistryKey other = (RegistryKey)ob; ");
524N/A out(" if (this.name != other.name) return false; ");
524N/A out(" if (this.family != other.family) return false; ");
524N/A out(" return true; ");
524N/A out(" } ");
524N/A out(" } ");
524N/A out(" private static class LazyInitialization { ");
524N/A out(" static final Map<RegistryKey,OptionKey> options = options(); ");
524N/A out(" private static Map<RegistryKey,OptionKey> options() { ");
524N/A out(" Map<RegistryKey,OptionKey> map = ");
524N/A out(" new HashMap<RegistryKey,OptionKey>(); ");
524N/A
4216N/A emit_unspec("StandardSocketOptions.SO_BROADCAST", SOL_SOCKET, SO_BROADCAST);
4216N/A emit_unspec("StandardSocketOptions.SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE);
4216N/A emit_unspec("StandardSocketOptions.SO_LINGER", SOL_SOCKET, SO_LINGER);
4216N/A emit_unspec("StandardSocketOptions.SO_SNDBUF", SOL_SOCKET, SO_SNDBUF);
4216N/A emit_unspec("StandardSocketOptions.SO_RCVBUF", SOL_SOCKET, SO_RCVBUF);
4216N/A emit_unspec("StandardSocketOptions.SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR);
4216N/A emit_unspec("StandardSocketOptions.TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY);
524N/A
4216N/A emit_inet("StandardSocketOptions.IP_TOS", IPPROTO_IP, IP_TOS);
4216N/A emit_inet("StandardSocketOptions.IP_MULTICAST_IF", IPPROTO_IP, IP_MULTICAST_IF);
4216N/A emit_inet("StandardSocketOptions.IP_MULTICAST_TTL", IPPROTO_IP, IP_MULTICAST_TTL);
4216N/A emit_inet("StandardSocketOptions.IP_MULTICAST_LOOP", IPPROTO_IP, IP_MULTICAST_LOOP);
524N/A
524N/A#ifdef AF_INET6
4216N/A emit_inet6("StandardSocketOptions.IP_MULTICAST_IF", IPPROTO_IPV6, IPV6_MULTICAST_IF);
4216N/A emit_inet6("StandardSocketOptions.IP_MULTICAST_TTL", IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
4216N/A emit_inet6("StandardSocketOptions.IP_MULTICAST_LOOP", IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
524N/A#endif
524N/A
524N/A emit_unspec("ExtendedSocketOption.SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE);
524N/A
524N/A out(" return map; ");
524N/A out(" } ");
524N/A out(" } ");
893N/A out(" public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) { ");
524N/A out(" RegistryKey key = new RegistryKey(name, family); ");
524N/A out(" return LazyInitialization.options.get(key); ");
524N/A out(" } ");
524N/A out("} ");
524N/A
524N/A return 0;
524N/A}