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.tipc;
19  
20  import java.io.FileDescriptor;
21  import java.io.IOException;
22  import java.math.BigInteger;
23  import java.net.Socket;
24  import java.net.SocketException;
25  
26  import org.newsclub.net.unix.AFSocket;
27  import org.newsclub.net.unix.AFSocketCapability;
28  import org.newsclub.net.unix.AFSocketFactory;
29  import org.newsclub.net.unix.AFTIPCSocketAddress;
30  import org.newsclub.net.unix.AFTIPCSocketImplExtensions;
31  
32  /**
33   * Implementation of an {@code AF_TIPC} socket.
34   *
35   * @author Christian Kohlschütter
36   */
37  public final class AFTIPCSocket extends AFSocket<AFTIPCSocketAddress> implements
38      AFTIPCSocketExtensions {
39    private static AFTIPCSocketImplExtensions staticExtensions = null;
40  
41    AFTIPCSocket(FileDescriptor fdObj, AFSocketFactory<AFTIPCSocketAddress> factory)
42        throws SocketException {
43      super(new AFTIPCSocketImpl(fdObj), factory);
44    }
45  
46    private static synchronized AFTIPCSocketImplExtensions getStaticImplExtensions()
47        throws IOException {
48      if (staticExtensions == null) {
49        try (AFTIPCSocket socket = new AFTIPCSocket(null, null)) {
50          staticExtensions = (AFTIPCSocketImplExtensions) socket.getImplExtensions();
51        }
52      }
53      return staticExtensions;
54    }
55  
56    /**
57     * Returns <code>true</code> iff {@link AFTIPCSocket}s (sockets of type "AF_TIPC") are supported
58     * by the current Java VM and the kernel.
59     *
60     * To support {@link AFTIPCSocket}s, a custom JNI library must be loaded that is supplied with
61     * <em>junixsocket</em>, and the system must support AF_TIPC sockets.
62     *
63     * This call is equivalent to checking {@link AFSocket#isSupported()} and
64     * {@link AFSocket#supports(AFSocketCapability)} with {@link AFSocketCapability#CAPABILITY_TIPC}.
65     *
66     * @return {@code true} iff supported.
67     */
68    public static boolean isSupported() {
69      return AFSocket.isSupported() && AFSocket.supports(AFSocketCapability.CAPABILITY_TIPC);
70    }
71  
72    @Override
73    protected AFTIPCSocketChannel newChannel() {
74      return new AFTIPCSocketChannel(this);
75    }
76  
77    /**
78     * Creates a new, unbound {@link AFSocket}.
79     *
80     * This "default" implementation is a bit "lenient" with respect to the specification.
81     *
82     * In particular, we ignore calls to {@link Socket#getTcpNoDelay()} and
83     * {@link Socket#setTcpNoDelay(boolean)}.
84     *
85     * @return A new, unbound socket.
86     * @throws IOException if the operation fails.
87     */
88    public static AFTIPCSocket newInstance() throws IOException {
89      return (AFTIPCSocket) AFSocket.newInstance(AFTIPCSocket::new, (AFTIPCSocketFactory) null);
90    }
91  
92    static AFTIPCSocket newInstance(AFTIPCSocketFactory factory) throws SocketException {
93      return (AFTIPCSocket) AFSocket.newInstance(AFTIPCSocket::new, factory);
94    }
95  
96    /**
97     * Creates a new, unbound, "strict" {@link AFSocket}.
98     *
99     * This call uses an implementation that tries to be closer to the specification than
100    * {@link #newInstance()}, at least for some cases.
101    *
102    * @return A new, unbound socket.
103    * @throws IOException if the operation fails.
104    */
105   public static AFTIPCSocket newStrictInstance() throws IOException {
106     return (AFTIPCSocket) AFSocket.newInstance(AFTIPCSocket::new, (AFTIPCSocketFactory) null);
107   }
108 
109   /**
110    * Creates a new {@link AFSocket} and connects it to the given {@link AFTIPCSocketAddress}.
111    *
112    * @param addr The address to connect to.
113    * @return A new, connected socket.
114    * @throws IOException if the operation fails.
115    */
116   public static AFTIPCSocket connectTo(AFTIPCSocketAddress addr) throws IOException {
117     return (AFTIPCSocket) AFSocket.connectTo(AFTIPCSocket::new, addr);
118   }
119 
120   @Override
121   public AFTIPCSocketChannel getChannel() {
122     return (AFTIPCSocketChannel) super.getChannel();
123   }
124 
125   /**
126    * Very basic self-test function.
127    *
128    * Prints "supported" and "capabilities" status to System.out.
129    *
130    * @param args ignored.
131    */
132   public static void main(String[] args) {
133     // If you want to run this directly from within Eclipse, see
134     // org.newsclub.net.unix.tipc.SocketTest#testMain.
135     System.out.print(AFTIPCSocket.class.getName() + ".isSupported(): ");
136     System.out.flush();
137     System.out.println(AFTIPCSocket.isSupported());
138   }
139 
140   @Override
141   public AFTIPCErrInfo getErrInfo() {
142     return AFTIPCErrInfo.of(((AFTIPCSocketImplExtensions) getImplExtensions()).getTIPCErrInfo());
143   }
144 
145   @Override
146   public AFTIPCDestName getDestName() {
147     return AFTIPCDestName.of(((AFTIPCSocketImplExtensions) getImplExtensions()).getTIPCDestName());
148   }
149 
150   /**
151    * Retrieves the 16-byte node ID given a node hash.
152    *
153    * @param peerId The node hash.
154    * @return The node ID, or {@code  null} if unsupported.
155    * @throws IOException on error.
156    */
157   public static byte[] getNodeIdentity(int peerId) throws IOException {
158     return getStaticImplExtensions().getTIPCNodeId(peerId);
159   }
160 
161   /**
162    * Retrieves the TIPC node identity given the node hash of the given address.
163    *
164    * @param address The address.
165    * @return The node identity, or {@code  null} if unsupported.
166    * @throws IOException on error.
167    */
168   public byte[] getNodeIdentity(AFTIPCSocketAddress address) throws IOException {
169     return AFTIPCSocket.getNodeIdentity(address.getTIPCNodeHash());
170   }
171 
172   /**
173    * Retrieves the node ID given a node hash, as a hexadecimal string.
174    *
175    * @param peerId The node hash.
176    * @return The node ID, or {@code  null} if unsupported.
177    * @throws IOException on error.
178    */
179   public static String getNodeIdHexString(int peerId) throws IOException {
180     byte[] id = getNodeIdentity(peerId);
181     return id == null ? null : new BigInteger(1, id).toString(16);
182   }
183 
184   /**
185    * Retrieves the link name given a node hash and a bearer ID.
186    *
187    * @param peerId The node hash.
188    * @param bearerId The bearer Id.
189    * @return The link name, or {@code  null} if unsupported.
190    * @throws IOException on error.
191    */
192   public static String getLinkName(int peerId, int bearerId) throws IOException {
193     return getStaticImplExtensions().getTIPCLinkName(peerId, bearerId);
194   }
195 }