1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.newsclub.net.unix.vsock;
19
20 import java.io.File;
21 import java.io.IOException;
22
23 import org.newsclub.net.unix.AFServerSocket;
24 import org.newsclub.net.unix.AFServerSocketConnector;
25 import org.newsclub.net.unix.AFSocketAddress;
26 import org.newsclub.net.unix.AFUNIXSocketAddress;
27 import org.newsclub.net.unix.AFVSOCKSocketAddress;
28 import org.newsclub.net.unix.AddressUnavailableSocketException;
29
30 import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;
31
32
33
34
35
36
37
38
39
40 @SuppressFBWarnings("PATH_TRAVERSAL_IN")
41 public final class AFVSOCKProxyServerSocketConnector implements
42 AFServerSocketConnector<AFVSOCKSocketAddress, AFSocketAddress> {
43 private static final AFServerSocketConnector<AFVSOCKSocketAddress, AFSocketAddress> DIRECT_CONNECTOR =
44 new AFServerSocketConnector<AFVSOCKSocketAddress, AFSocketAddress>() {
45
46 @Override
47 public AFServerSocket<? extends AFSocketAddress> bind(AFVSOCKSocketAddress addr)
48 throws IOException {
49 return addr.newForceBoundServerSocket();
50 }
51 };
52
53 private final String listenAddressPrefix;
54 private final int allowedCID;
55
56 private AFVSOCKProxyServerSocketConnector(String listenAddressPrefix, int allowedCID) {
57 this.listenAddressPrefix = listenAddressPrefix;
58 this.allowedCID = allowedCID;
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72 public static AFServerSocketConnector<AFVSOCKSocketAddress, AFSocketAddress> openFirecrackerStyleConnector(
73 File listenAddressPrefix, int allowedCID) {
74 return new AFVSOCKProxyServerSocketConnector(listenAddressPrefix.getAbsolutePath(), allowedCID);
75 }
76
77
78
79
80
81
82 public static AFServerSocketConnector<AFVSOCKSocketAddress, AFSocketAddress> openDirectConnector() {
83 return DIRECT_CONNECTOR;
84 }
85
86 @Override
87 public AFServerSocket<?> bind(AFVSOCKSocketAddress addr) throws IOException {
88 int cid = addr.getVSOCKCID();
89 if (cid != allowedCID && cid != AFVSOCKSocketAddress.VMADDR_CID_ANY
90 && allowedCID != AFVSOCKSocketAddress.VMADDR_CID_ANY) {
91 throw new AddressUnavailableSocketException("Factory does not cover CID " + cid);
92 }
93
94 return AFUNIXSocketAddress.of(new File(listenAddressPrefix + "_" + addr.getVSOCKPort()))
95 .newForceBoundServerSocket();
96 }
97 }