View Javadoc
1   /*
2    * Copyright 2019 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.handler.address;
17  
18  import io.netty.channel.ChannelHandlerContext;
19  import io.netty.channel.ChannelOutboundHandler;
20  import io.netty.channel.ChannelOutboundHandlerAdapter;
21  import io.netty.channel.ChannelPromise;
22  
23  import java.net.NetworkInterface;
24  import java.net.SocketAddress;
25  
26  /**
27   * {@link ChannelOutboundHandler} implementation which allows to dynamically replace the used
28   * {@code remoteAddress} and / or {@code localAddress} when making a connection attempt.
29   * <p>
30   * This can be useful to for example bind to a specific {@link NetworkInterface} based on
31   * the {@code remoteAddress}.
32   */
33  public abstract class DynamicAddressConnectHandler extends ChannelOutboundHandlerAdapter {
34  
35      @Override
36      public final void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
37                                SocketAddress localAddress, ChannelPromise promise) {
38          final SocketAddress remote;
39          final SocketAddress local;
40          try {
41              remote = remoteAddress(remoteAddress, localAddress);
42              local = localAddress(remoteAddress, localAddress);
43          } catch (Exception e) {
44              promise.setFailure(e);
45              return;
46          }
47          ctx.connect(remote, local, promise).addListener(future -> {
48              if (future.isSuccess()) {
49                  // We only remove this handler from the pipeline once the connect was successful as otherwise
50                  // the user may try to connect again.
51                  ctx.pipeline().remove(DynamicAddressConnectHandler.this);
52              }
53          });
54      }
55  
56      /**
57       * Returns the local {@link SocketAddress} to use for
58       * {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress)} based on the original {@code remoteAddress}
59       * and {@code localAddress}.
60       * By default, this method returns the given {@code localAddress}.
61       */
62      protected SocketAddress localAddress(
63              @SuppressWarnings("unused") SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
64          return localAddress;
65      }
66  
67      /**
68       * Returns the remote {@link SocketAddress} to use for
69       * {@link ChannelHandlerContext#connect(SocketAddress, SocketAddress)} based on the original {@code remoteAddress}
70       * and {@code localAddress}.
71       * By default, this method returns the given {@code remoteAddress}.
72       */
73      protected SocketAddress remoteAddress(
74              SocketAddress remoteAddress, @SuppressWarnings("unused") SocketAddress localAddress) throws Exception {
75          return remoteAddress;
76      }
77  }