NetHooks.java revision 12745:f068a4ffddd2
1/*
2 * Copyright (c) 2009, 2010, 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
26package sun.net;
27
28import java.net.InetAddress;
29import java.io.FileDescriptor;
30import java.io.IOException;
31import java.security.AccessController;
32import java.security.PrivilegedAction;
33import sun.security.action.GetPropertyAction;
34
35/**
36 * Defines static methods to be invoked prior to binding or connecting TCP sockets.
37 */
38
39public final class NetHooks {
40
41    /**
42     * A provider with hooks to allow sockets be converted prior to binding or
43     * connecting a TCP socket.
44     *
45     * <p> Concrete implementations of this class should define a zero-argument
46     * constructor and implement the abstract methods specified below.
47     */
48    public abstract static class Provider {
49        /**
50         * Initializes a new instance of this class.
51         */
52        protected Provider() {}
53
54        /**
55         * Invoked prior to binding a TCP socket.
56         */
57        public abstract void implBeforeTcpBind(FileDescriptor fdObj,
58                                               InetAddress address,
59                                               int port)
60            throws IOException;
61
62        /**
63         * Invoked prior to connecting an unbound TCP socket.
64         */
65        public abstract void implBeforeTcpConnect(FileDescriptor fdObj,
66                                                 InetAddress address,
67                                                 int port)
68            throws IOException;
69    }
70
71    /**
72     * For now, we load the SDP provider on Solaris. In the future this may
73     * be changed to use the ServiceLoader facility to allow the deployment of
74     * other providers.
75     */
76    private static final Provider provider = new sun.net.sdp.SdpProvider();
77
78    /**
79     * Invoke prior to binding a TCP socket.
80     */
81    public static void beforeTcpBind(FileDescriptor fdObj,
82                                     InetAddress address,
83                                     int port)
84        throws IOException
85    {
86        provider.implBeforeTcpBind(fdObj, address, port);
87    }
88
89    /**
90     * Invoke prior to connecting an unbound TCP socket.
91     */
92    public static void beforeTcpConnect(FileDescriptor fdObj,
93                                        InetAddress address,
94                                        int port)
95        throws IOException
96    {
97        provider.implBeforeTcpConnect(fdObj, address, port);
98    }
99}
100