1 /*
   2  * Copyright (c) 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 jdk.internal.net.rdma;
  43 import java.net.SocketOption;
  44 import java.net.StandardSocketOptions;
  45 import java.net.ProtocolFamily;
  46 import java.util.Map;
  47 import java.util.HashMap;
  48 
  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_SNDBUF,
  81                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_SNDBUF));
  82             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_RCVBUF,
  83                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_RCVBUF));
  84             map.put(new RegistryKey(StandardSocketOptions.PREFIX_SO_REUSEADDR,
  85                 RdmaNet.UNSPEC), new RdmaOptionKey(SOL_SOCKET, SO_REUSEADDR));
  86             // IPPROTO_TCP is 6
  87             map.put(new RegistryKey(StandardSocketOptions.PREFIX_TCP_NODELAY,
  88                 RdmaNet.UNSPEC), new RdmaOptionKey(6, TCP_NODELAY));
  89             return map;
  90         }
  91     }
  92 
  93     public static RdmaOptionKey findOption(SocketOption<?> name,
  94                                            ProtocolFamily family) {
  95         RegistryKey key = new RegistryKey(name, family);
  96         return LazyInitialization.options.get(key);
  97     }
  98 }