AFVSOCKServerSocket.java

  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. import java.io.FileDescriptor;
  20. import java.io.IOException;
  21. import java.net.ServerSocket;
  22. import java.net.SocketException;

  23. import org.newsclub.net.unix.AFServerSocket;
  24. import org.newsclub.net.unix.AFServerSocketChannel;
  25. import org.newsclub.net.unix.AFSocket;
  26. import org.newsclub.net.unix.AFSocketAddress;
  27. import org.newsclub.net.unix.AFSocketImpl;
  28. import org.newsclub.net.unix.AFVSOCKSocketAddress;

  29. /**
  30.  * The server part of an {@code AF_VSOCK} socket.
  31.  *
  32.  * @author Christian Kohlschütter
  33.  */
  34. public final class AFVSOCKServerSocket extends AFServerSocket<AFVSOCKSocketAddress> {
  35.   AFVSOCKServerSocket(FileDescriptor fdObj) throws IOException {
  36.     super(fdObj);
  37.   }

  38.   @Override
  39.   protected AFServerSocketChannel<AFVSOCKSocketAddress> newChannel() {
  40.     return new AFVSOCKServerSocketChannel(this);
  41.   }

  42.   @Override
  43.   public AFVSOCKServerSocketChannel getChannel() {
  44.     return (AFVSOCKServerSocketChannel) super.getChannel();
  45.   }

  46.   /**
  47.    * Returns a new, unbound AF_VSOCK {@link ServerSocket}.
  48.    *
  49.    * @return The new, unbound {@link AFServerSocket}.
  50.    * @throws IOException if the operation fails.
  51.    */
  52.   public static AFVSOCKServerSocket newInstance() throws IOException {
  53.     return (AFVSOCKServerSocket) AFServerSocket.newInstance(AFVSOCKServerSocket::new);
  54.   }

  55.   static AFVSOCKServerSocket newInstance(FileDescriptor fdObj, int localPort, int remotePort)
  56.       throws IOException {
  57.     return (AFVSOCKServerSocket) AFServerSocket.newInstance(AFVSOCKServerSocket::new, fdObj,
  58.         localPort, remotePort);
  59.   }

  60.   /**
  61.    * Returns a new AF_VSOCK {@link ServerSocket} that is bound to the given
  62.    * {@link AFVSOCKSocketAddress}.
  63.    *
  64.    * @param addr The socket file to bind to.
  65.    * @return The new, bound {@link AFServerSocket}.
  66.    * @throws IOException if the operation fails.
  67.    */
  68.   public static AFVSOCKServerSocket bindOn(final AFVSOCKSocketAddress addr) throws IOException {
  69.     return (AFVSOCKServerSocket) AFServerSocket.bindOn(AFVSOCKServerSocket::new, addr);
  70.   }

  71.   /**
  72.    * Returns a new AF_VSOCK {@link ServerSocket} that is bound to the given {@link AFSocketAddress}.
  73.    *
  74.    * @param addr The socket file to bind to.
  75.    * @param deleteOnClose If {@code true}, the socket file (if the address points to a file) will be
  76.    *          deleted upon {@link #close}.
  77.    * @return The new, bound {@link AFServerSocket}.
  78.    * @throws IOException if the operation fails.
  79.    */
  80.   public static AFVSOCKServerSocket bindOn(final AFVSOCKSocketAddress addr, boolean deleteOnClose)
  81.       throws IOException {
  82.     return (AFVSOCKServerSocket) AFServerSocket.bindOn(AFVSOCKServerSocket::new, addr,
  83.         deleteOnClose);
  84.   }

  85.   /**
  86.    * Returns a new, <em>unbound</em> AF_VSOCK {@link ServerSocket} that will always bind to the
  87.    * given address, regardless of any socket address used in a call to <code>bind</code>.
  88.    *
  89.    * @param forceAddr The address to use.
  90.    * @return The new, yet unbound {@link AFServerSocket}.
  91.    * @throws IOException if an exception occurs.
  92.    */
  93.   public static AFVSOCKServerSocket forceBindOn(final AFVSOCKSocketAddress forceAddr)
  94.       throws IOException {
  95.     return (AFVSOCKServerSocket) AFServerSocket.forceBindOn(AFVSOCKServerSocket::new, forceAddr);
  96.   }

  97.   @Override
  98.   protected AFSocketImpl<AFVSOCKSocketAddress> newImpl(FileDescriptor fdObj)
  99.       throws SocketException {
  100.     return new AFVSOCKSocketImpl(fdObj);
  101.   }

  102.   @Override
  103.   protected AFSocket<AFVSOCKSocketAddress> newSocketInstance() throws IOException {
  104.     return AFVSOCKSocket.newInstance();
  105.   }

  106.   @Override
  107.   public AFVSOCKSocket accept() throws IOException {
  108.     return (AFVSOCKSocket) super.accept();
  109.   }
  110. }