1 /*
   2  * Copyright (c) 2008, 2018, 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 #include <sys/types.h>
  31 #include <sys/socket.h>
  32 #include <netinet/in.h>
  33 #include <netinet/tcp.h>
  34 
  35 /* To be able to name the Java constants the same as the C constants without
  36    having the preprocessor rewrite those identifiers, add PREFIX_ to all
  37    identifiers matching a C constant. The PREFIX_ is filtered out in the
  38    makefile. */
  39 
  40 @@START_HERE@@
  41 
  42 package rdma.ch;
  43 import java.net.SocketOption;
  44 import java.net.StandardSocketOptions;
  45 import java.net.ProtocolFamily;
  46 import java.net.StandardProtocolFamily;
  47 import java.util.Map;
  48 import java.util.HashMap;
  49 class RdmaSocketOptionRegistry {
  50 
  51     private RdmaSocketOptionRegistry() { }
  52 
  53     private static class RegistryKey {
  54         private final SocketOption<?> name;
  55         private final ProtocolFamily family;
  56         RegistryKey(SocketOption<?> name, ProtocolFamily family) {
  57             this.name = name;
  58             this.family = family;
  59         }
  60         public int hashCode() {
  61             return name.hashCode() + family.hashCode();
  62         }
  63         public boolean equals(Object ob) {
  64             if (ob == null) return false;
  65             if (!(ob instanceof RegistryKey)) return false;
  66             RegistryKey other = (RegistryKey)ob;
  67             if (this.name != other.name) return false;
  68             if (this.family != other.family) return false;
  69             return true;
  70         }
  71     }
  72 
  73     private static class LazyInitialization {
  74 
  75         static final Map<RegistryKey,RdmaOptionKey> options = options();
  76 
  77         private static Map<RegistryKey,RdmaOptionKey> options() {
  78             Map<RegistryKey,RdmaOptionKey> map =
  79                 new HashMap<RegistryKey,RdmaOptionKey>();
  80             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_KEEPALIVE,
  81                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_KEEPALIVE));
  82             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_SNDBUF,
  83                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_SNDBUF));
  84             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_RCVBUF,
  85                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_RCVBUF));
  86             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEADDR,
  87                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_REUSEADDR));
  88             // IPPROTO_TCP is 6
  89             map.put(new RegistryKey(StandardSocketOptions.PREFIX_TCP_NODELAY,
  90                 RdmaNet.UNSPEC), new RdmaOptionKey(6, TCP_NODELAY));
  91 
  92 #ifdef AF_INET6
  93             // IPPROTO_IPV6 is 41
  94             map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_TOS,
  95                 StandardProtocolFamily.INET6), new RdmaOptionKey(41, IPV6_TCLASS));
  96             map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_IF,
  97                 StandardProtocolFamily.INET6), new RdmaOptionKey(41, IPV6_MULTICAST_IF));
  98             map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_TTL,
  99                 StandardProtocolFamily.INET6), new RdmaOptionKey(41, IPV6_MULTICAST_HOPS));
 100             map.put(new RegistryKey(StandardSocketOptions.PREFIX_IP_MULTICAST_LOOP,
 101                 StandardProtocolFamily.INET6), new RdmaOptionKey(41, IPV6_MULTICAST_LOOP));
 102 #endif
 103             return map;
 104         }
 105     }
 106 
 107     public static RdmaOptionKey findOption(SocketOption<?> name, ProtocolFamily family) {
 108         RegistryKey key = new RegistryKey(name, family);
 109         return LazyInitialization.options.get(key);
 110     }
 111 }