View Javadoc
1   /*
2    * junixsocket
3    *
4    * Copyright 2009-2024 Christian Kohlschütter
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.newsclub.net.unix.vsock;
19  
20  import java.io.IOException;
21  import java.net.Socket;
22  import java.net.SocketException;
23  
24  import org.newsclub.net.unix.AFSocketFactory;
25  import org.newsclub.net.unix.AFVSOCKSocketAddress;
26  
27  import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
28  
29  /**
30   * The base for a SocketFactory that connects to VSOCK sockets.
31   */
32  public abstract class AFVSOCKSocketFactory extends AFSocketFactory<AFVSOCKSocketAddress> {
33    /**
34     * Creates a {@link AFVSOCKSocketFactory}.
35     */
36    protected AFVSOCKSocketFactory() {
37      super(AFVSOCKSocketAddress.class);
38    }
39  
40    @Override
41    public final Socket createSocket() throws SocketException {
42      return configure(AFVSOCKSocket.newInstance(this));
43    }
44  
45    @Override
46    protected final AFVSOCKSocket connectTo(AFVSOCKSocketAddress addr) throws IOException {
47      return configure(AFVSOCKSocket.connectTo(addr));
48    }
49  
50    /**
51     * Performs some optional configuration on a newly created socket.
52     *
53     * @param sock The socket.
54     * @return The very socket.
55     * @throws SocketException on error.
56     */
57    protected AFVSOCKSocket configure(AFVSOCKSocket sock) throws SocketException {
58      return sock;
59    }
60  
61    /**
62     * Always connects sockets to the given VSOCK type and instance.
63     *
64     * @author Christian Kohlschütter
65     */
66    public static final class FixedAddress extends AFVSOCKSocketFactory {
67      private final AFVSOCKSocketAddress addr;
68  
69      /**
70       * Creates an {@link AFVSOCKSocketFactory} that always uses the given VSOCK address.
71       *
72       * @param port The VSOCK port.
73       * @param cid The VSOCK CID.
74       * @throws SocketException on internal error.
75       */
76      public FixedAddress(int port, int cid) throws SocketException {
77        this(AFVSOCKSocketAddress.ofPortAndCID(port, cid));
78      }
79  
80      /**
81       * Creates an {@link AFVSOCKSocketFactory} that always uses the given VSOCK address.
82       *
83       * @param addr The address.
84       */
85      @SuppressFBWarnings("EI_EXPOSE_REP2")
86      public FixedAddress(AFVSOCKSocketAddress addr) {
87        super();
88        this.addr = addr;
89      }
90  
91      @SuppressFBWarnings("EI_EXPOSE_REP")
92      @Override
93      public AFVSOCKSocketAddress addressFromHost(String host, int javaPort) throws SocketException {
94        return addr;
95      }
96    }
97  }