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 import sun.nio.ch.ExtendedSocketOption;
  50 
  51 class RdmaSocketOptionRegistry {
  52 
  53     private RdmaSocketOptionRegistry() { }
  54 
  55     private static class RegistryKey {
  56         private final SocketOption<?> name;
  57         private final ProtocolFamily family;
  58         RegistryKey(SocketOption<?> name, ProtocolFamily family) {
  59             this.name = name;
  60             this.family = family;
  61         }
  62         public int hashCode() {
  63             return name.hashCode() + family.hashCode();
  64         }
  65         public boolean equals(Object ob) {
  66             if (ob == null) return false;
  67             if (!(ob instanceof RegistryKey)) return false;
  68             RegistryKey other = (RegistryKey)ob;
  69             if (this.name != other.name) return false;
  70             if (this.family != other.family) return false;
  71             return true;
  72         }
  73     }
  74 
  75     private static class LazyInitialization {
  76 
  77         static final Map<RegistryKey,RdmaOptionKey> options = options();
  78 
  79         private static Map<RegistryKey,RdmaOptionKey> options() {
  80             Map<RegistryKey,RdmaOptionKey> map =
  81                 new HashMap<RegistryKey,RdmaOptionKey>();
  82             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_LINGER,
  83                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_LINGER));
  84             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_SNDBUF,
  85                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_SNDBUF));
  86             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_RCVBUF,
  87                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_RCVBUF));
  88             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEADDR,
  89                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_REUSEADDR));
  90             // IPPROTO_TCP is 6
  91             map.put(new RegistryKey(StandardSocketOptions.PREFIX_TCP_NODELAY,
  92                 RdmaNet.UNSPEC), new RdmaOptionKey(6, TCP_NODELAY));
  93             return map;
  94         }
  95     }
  96 
  97     public static RdmaOptionKey findOption(SocketOption<?> name, ProtocolFamily family) {
  98         RegistryKey key = new RegistryKey(name, family);
  99         return LazyInitialization.options.get(key);
 100     }
 101 }