1 /*
2  * Copyright (c) 2016, 2018, 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 
26 package sun.net.ext;
27 
28 import java.io.FileDescriptor;
29 import java.net.SocketException;
30 import java.net.SocketOption;
31 import java.util.Collections;
32 import java.util.Set;
33 import java.util.stream.Collectors;
34 
35 /**
36  * Defines the infrastructure to support extended socket options, beyond those
37  * defined in {@link java.net.StandardSocketOptions}.
38  *
39  * Extended socket options are accessed through the jdk.net API, which is in
40  * the jdk.net module.
41  */
42 public abstract class ExtendedSocketOptions {
43 
44     public static final short SOCK_STREAM = 1;
45     public static final short SOCK_DGRAM = 2;
46 
47     private final Set<SocketOption<?>> options;
48 
49     /** Tells whether or not the option is supported. */
isOptionSupported(SocketOption<?> option)50     public final boolean isOptionSupported(SocketOption<?> option) {
51         return options().contains(option);
52     }
53 
54     /** Return the, possibly empty, set of extended socket options available. */
options()55     public final Set<SocketOption<?>> options() { return options; }
56 
options(short type)57     public static final Set<SocketOption<?>> options(short type) {
58         return getInstance().options0(type);
59     }
60 
options0(short type)61     private Set<SocketOption<?>> options0(short type) {
62         Set<SocketOption<?>> extOptions = null;
63         switch (type) {
64             case SOCK_DGRAM:
65                 extOptions = options.stream()
66                         .filter((option) -> !option.name().startsWith("TCP_"))
67                         .collect(Collectors.toUnmodifiableSet());
68                 break;
69             case SOCK_STREAM:
70                 extOptions = options.stream()
71                         .filter((option) -> !option.name().startsWith("UDP_"))
72                         .collect(Collectors.toUnmodifiableSet());
73                 break;
74             default:
75                 //this will never happen
76                 throw new IllegalArgumentException("Invalid socket option type");
77         }
78         return extOptions;
79     }
80 
81     /** Sets the value of a socket option, for the given socket. */
setOption(FileDescriptor fd, SocketOption<?> option, Object value)82     public abstract void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
83             throws SocketException;
84 
85     /** Returns the value of a socket option, for the given socket. */
getOption(FileDescriptor fd, SocketOption<?> option)86     public abstract Object getOption(FileDescriptor fd, SocketOption<?> option)
87             throws SocketException;
88 
ExtendedSocketOptions(Set<SocketOption<?>> options)89     protected ExtendedSocketOptions(Set<SocketOption<?>> options) {
90         this.options = options;
91     }
92 
93     private static volatile ExtendedSocketOptions instance;
94 
getInstance()95     public static final ExtendedSocketOptions getInstance() { return instance; }
96 
97     /** Registers support for extended socket options. Invoked by the jdk.net module. */
register(ExtendedSocketOptions extOptions)98     public static final void register(ExtendedSocketOptions extOptions) {
99         if (instance != null)
100             throw new InternalError("Attempting to reregister extended options");
101 
102         instance = extOptions;
103     }
104 
105     static {
106         try {
107             // If the class is present, it will be initialized which
108             // triggers registration of the extended socket options.
109             Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions");
110         } catch (ClassNotFoundException e) {
111             // the jdk.net module is not present => no extended socket options
112             instance = new NoExtendedSocketOptions();
113         }
114     }
115 
116     static final class NoExtendedSocketOptions extends ExtendedSocketOptions {
117 
NoExtendedSocketOptions()118         NoExtendedSocketOptions() {
119             super(Collections.<SocketOption<?>>emptySet());
120         }
121 
122         @Override
setOption(FileDescriptor fd, SocketOption<?> option, Object value)123         public void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
124             throws SocketException
125         {
126             throw new UnsupportedOperationException(
127                     "no extended options: " + option.name());
128         }
129 
130         @Override
getOption(FileDescriptor fd, SocketOption<?> option)131         public Object getOption(FileDescriptor fd, SocketOption<?> option)
132             throws SocketException
133         {
134             throw new UnsupportedOperationException(
135                     "no extended options: " + option.name());
136         }
137     }
138 }
139