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