PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/src/share/native/sun/nio/ch/genSocketOptionRegistry.c

#
C | 130 lines | 88 code | 14 blank | 28 comment | 0 complexity | b326c4f9c4c61959a5f48e49819582d6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  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. #include <stdio.h>
  26. #ifdef _WIN32
  27. #include <winsock2.h>
  28. #include <ws2tcpip.h>
  29. #else
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <netinet/tcp.h>
  34. #endif
  35. /**
  36. * Generates sun.nio.ch.SocketOptionRegistry, a class that maps Java-level
  37. * socket options to the platform specific level and option.
  38. */
  39. static void out(char* s) {
  40. printf("%s\n", s);
  41. }
  42. static void emit(const char *name, char * family, int level, int optname) {
  43. printf(" map.put(new RegistryKey(%s, %s),", name, family);
  44. printf(" new OptionKey(%d, %d));\n", level, optname);
  45. }
  46. static void emit_unspec(const char *name, int level, int optname) {
  47. emit(name, "Net.UNSPEC", level, optname);
  48. }
  49. static void emit_inet(const char *name, int level, int optname) {
  50. emit(name, "StandardProtocolFamily.INET", level, optname);
  51. }
  52. static void emit_inet6(const char *name, int level, int optname) {
  53. emit(name, "StandardProtocolFamily.INET6", level, optname);
  54. }
  55. int main(int argc, const char* argv[]) {
  56. out("// AUTOMATICALLY GENERATED FILE - DO NOT EDIT ");
  57. out("package sun.nio.ch; ");
  58. out("import java.net.SocketOption; ");
  59. out("import java.net.StandardSocketOptions; ");
  60. out("import java.net.ProtocolFamily; ");
  61. out("import java.net.StandardProtocolFamily; ");
  62. out("import java.util.Map; ");
  63. out("import java.util.HashMap; ");
  64. out("class SocketOptionRegistry { ");
  65. out(" private SocketOptionRegistry() { } ");
  66. out(" private static class RegistryKey { ");
  67. out(" private final SocketOption<?> name; ");
  68. out(" private final ProtocolFamily family; ");
  69. out(" RegistryKey(SocketOption<?> name, ProtocolFamily family) { ");
  70. out(" this.name = name; ");
  71. out(" this.family = family; ");
  72. out(" } ");
  73. out(" public int hashCode() { ");
  74. out(" return name.hashCode() + family.hashCode(); ");
  75. out(" } ");
  76. out(" public boolean equals(Object ob) { ");
  77. out(" if (ob == null) return false; ");
  78. out(" if (!(ob instanceof RegistryKey)) return false; ");
  79. out(" RegistryKey other = (RegistryKey)ob; ");
  80. out(" if (this.name != other.name) return false; ");
  81. out(" if (this.family != other.family) return false; ");
  82. out(" return true; ");
  83. out(" } ");
  84. out(" } ");
  85. out(" private static class LazyInitialization { ");
  86. out(" static final Map<RegistryKey,OptionKey> options = options(); ");
  87. out(" private static Map<RegistryKey,OptionKey> options() { ");
  88. out(" Map<RegistryKey,OptionKey> map = ");
  89. out(" new HashMap<RegistryKey,OptionKey>(); ");
  90. emit_unspec("StandardSocketOptions.SO_BROADCAST", SOL_SOCKET, SO_BROADCAST);
  91. emit_unspec("StandardSocketOptions.SO_KEEPALIVE", SOL_SOCKET, SO_KEEPALIVE);
  92. emit_unspec("StandardSocketOptions.SO_LINGER", SOL_SOCKET, SO_LINGER);
  93. emit_unspec("StandardSocketOptions.SO_SNDBUF", SOL_SOCKET, SO_SNDBUF);
  94. emit_unspec("StandardSocketOptions.SO_RCVBUF", SOL_SOCKET, SO_RCVBUF);
  95. emit_unspec("StandardSocketOptions.SO_REUSEADDR", SOL_SOCKET, SO_REUSEADDR);
  96. emit_unspec("StandardSocketOptions.TCP_NODELAY", IPPROTO_TCP, TCP_NODELAY);
  97. emit_inet("StandardSocketOptions.IP_TOS", IPPROTO_IP, IP_TOS);
  98. emit_inet("StandardSocketOptions.IP_MULTICAST_IF", IPPROTO_IP, IP_MULTICAST_IF);
  99. emit_inet("StandardSocketOptions.IP_MULTICAST_TTL", IPPROTO_IP, IP_MULTICAST_TTL);
  100. emit_inet("StandardSocketOptions.IP_MULTICAST_LOOP", IPPROTO_IP, IP_MULTICAST_LOOP);
  101. #ifdef AF_INET6
  102. emit_inet6("StandardSocketOptions.IP_MULTICAST_IF", IPPROTO_IPV6, IPV6_MULTICAST_IF);
  103. emit_inet6("StandardSocketOptions.IP_MULTICAST_TTL", IPPROTO_IPV6, IPV6_MULTICAST_HOPS);
  104. emit_inet6("StandardSocketOptions.IP_MULTICAST_LOOP", IPPROTO_IPV6, IPV6_MULTICAST_LOOP);
  105. #endif
  106. emit_unspec("ExtendedSocketOption.SO_OOBINLINE", SOL_SOCKET, SO_OOBINLINE);
  107. out(" return map; ");
  108. out(" } ");
  109. out(" } ");
  110. out(" public static OptionKey findOption(SocketOption<?> name, ProtocolFamily family) { ");
  111. out(" RegistryKey key = new RegistryKey(name, family); ");
  112. out(" return LazyInitialization.options.get(key); ");
  113. out(" } ");
  114. out("} ");
  115. return 0;
  116. }