1 /*
2  * Copyright 2014 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  *   http://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.epoll;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelException;
20 import io.netty.channel.socket.ServerSocketChannel;
21 import io.netty.channel.socket.SocketChannel;
22 import io.netty.util.concurrent.GlobalEventExecutor;
23
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Map;
30 import java.util.concurrent.Executor;
31
32 import static io.netty.channel.epoll.LinuxSocket.newSocketStream;
33
34 /**
35  * {@link SocketChannel} implementation that uses linux EPOLL Edge-Triggered Mode for
36  * maximal performance.
37  */

38 public final class EpollSocketChannel extends AbstractEpollStreamChannel implements SocketChannel {
39
40     private final EpollSocketChannelConfig config;
41
42     private volatile Collection<InetAddress> tcpMd5SigAddresses = Collections.emptyList();
43
44     public EpollSocketChannel() {
45         super(newSocketStream(), false);
46         config = new EpollSocketChannelConfig(this);
47     }
48
49     public EpollSocketChannel(int fd) {
50         super(fd);
51         config = new EpollSocketChannelConfig(this);
52     }
53
54     EpollSocketChannel(LinuxSocket fd, boolean active) {
55         super(fd, active);
56         config = new EpollSocketChannelConfig(this);
57     }
58
59     EpollSocketChannel(Channel parent, LinuxSocket fd, InetSocketAddress remoteAddress) {
60         super(parent, fd, remoteAddress);
61         config = new EpollSocketChannelConfig(this);
62
63         if (parent instanceof EpollServerSocketChannel) {
64             tcpMd5SigAddresses = ((EpollServerSocketChannel) parent).tcpMd5SigAddresses();
65         }
66     }
67
68     /**
69      * Returns the {@code TCP_INFO} for the current socket. See <a href="http://linux.die.net/man/7/tcp">man 7 tcp</a>.
70      */

71     public EpollTcpInfo tcpInfo() {
72         return tcpInfo(new EpollTcpInfo());
73     }
74
75     /**
76      * Updates and returns the {@code TCP_INFO} for the current socket.
77      * See <a href="http://linux.die.net/man/7/tcp">man 7 tcp</a>.
78      */

79     public EpollTcpInfo tcpInfo(EpollTcpInfo info) {
80         try {
81             socket.getTcpInfo(info);
82             return info;
83         } catch (IOException e) {
84             throw new ChannelException(e);
85         }
86     }
87
88     @Override
89     public InetSocketAddress remoteAddress() {
90         return (InetSocketAddress) super.remoteAddress();
91     }
92
93     @Override
94     public InetSocketAddress localAddress() {
95         return (InetSocketAddress) super.localAddress();
96     }
97
98     @Override
99     public EpollSocketChannelConfig config() {
100         return config;
101     }
102
103     @Override
104     public ServerSocketChannel parent() {
105         return (ServerSocketChannel) super.parent();
106     }
107
108     @Override
109     protected AbstractEpollUnsafe newUnsafe() {
110         return new EpollSocketChannelUnsafe();
111     }
112
113     private final class EpollSocketChannelUnsafe extends EpollStreamUnsafe {
114         @Override
115         protected Executor prepareToClose() {
116             try {
117                 // Check isOpen() first as otherwise it will throw a RuntimeException
118                 // when call getSoLinger() as the fd is not valid anymore.
119                 if (isOpen() && config().getSoLinger() > 0) {
120                     // We need to cancel this key of the channel so we may not end up in a eventloop spin
121                     // because we try to read or write until the actual close happens which may be later due
122                     // SO_LINGER handling.
123                     // See https://github.com/netty/netty/issues/4449
124                     ((EpollEventLoop) eventLoop()).remove(EpollSocketChannel.this);
125                     return GlobalEventExecutor.INSTANCE;
126                 }
127             } catch (Throwable ignore) {
128                 // Ignore the error as the underlying channel may be closed in the meantime and so
129                 // getSoLinger() may produce an exception. In this case we just return null.
130                 // See https://github.com/netty/netty/issues/4449
131             }
132             return null;
133         }
134     }
135
136     void setTcpMd5Sig(Map<InetAddress, byte[]> keys) throws IOException {
137         tcpMd5SigAddresses = TcpMd5Util.newTcpMd5Sigs(this, tcpMd5SigAddresses, keys);
138     }
139 }
140