View Javadoc
1   /*
2    * Copyright 2017 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.channel.unix;
17  
18  import io.netty.buffer.ByteBuf;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.InetSocketAddress;
23  import java.net.UnknownHostException;
24  import java.nio.file.Files;
25  import java.nio.file.InvalidPathException;
26  import java.nio.file.LinkOption;
27  import java.nio.file.Path;
28  import java.nio.file.Paths;
29  import java.nio.file.attribute.BasicFileAttributes;
30  
31  import static io.netty.channel.unix.Limits.IOV_MAX;
32  
33  public final class UnixChannelUtil {
34  
35      private UnixChannelUtil() {
36      }
37  
38      /**
39       * Checks if the specified buffer has memory address or is composed of n(n <= IOV_MAX) NIO direct buffers.
40       * (We check this because otherwise we need to make it a new direct buffer.)
41       */
42      public static boolean isBufferCopyNeededForWrite(ByteBuf byteBuf) {
43          return isBufferCopyNeededForWrite(byteBuf, IOV_MAX);
44      }
45  
46      static boolean isBufferCopyNeededForWrite(ByteBuf byteBuf, int iovMax) {
47          return !byteBuf.hasMemoryAddress() && (!byteBuf.isDirect() || byteBuf.nioBufferCount() > iovMax);
48      }
49  
50      /**
51       * Delete the file at {@code path} but only if it is not a regular file, directory or symbolic link (i.e. it is
52       * a socket inode as left behind by a bound domain socket). Symbolic links are never followed.
53       *
54       * @return {@code true} if the file was deleted.
55       */
56      public static boolean deleteDomainSocketFile(String path) {
57          try {
58              Path file = Paths.get(path);
59              BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class,
60                      LinkOption.NOFOLLOW_LINKS);
61              if (!attrs.isOther()) {
62                  return false;
63              }
64              Files.delete(file);
65              return true;
66          } catch (IOException ignore) {
67              return false;
68          } catch (InvalidPathException ignore) {
69              // For example abstract namespace addresses, which have no file system presence.
70              return false;
71          }
72      }
73  
74      public static InetSocketAddress computeRemoteAddr(InetSocketAddress remoteAddr, InetSocketAddress osRemoteAddr) {
75          if (osRemoteAddr != null) {
76              try {
77                  // Only try to construct a new InetSocketAddress if we using java >= 7 as getHostString() does not
78                  // exists in earlier releases and so the retrieval of the hostname could block the EventLoop if a
79                  // reverse lookup would be needed.
80                  return new InetSocketAddress(InetAddress.getByAddress(remoteAddr.getHostString(),
81                          osRemoteAddr.getAddress().getAddress()),
82                          osRemoteAddr.getPort());
83              } catch (UnknownHostException ignore) {
84                  // Should never happen but fallback to osRemoteAddr anyway.
85              }
86              return osRemoteAddr;
87          }
88          return remoteAddr;
89      }
90  }