1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.waarp.ftp.core.utils;
19
20 import java.net.Inet4Address;
21 import java.net.Inet6Address;
22 import java.net.InetAddress;
23 import java.net.InetSocketAddress;
24 import java.net.UnknownHostException;
25 import java.util.Iterator;
26 import java.util.Timer;
27
28 import io.netty.channel.Channel;
29
30 import org.slf4j.LoggerFactory;
31 import org.waarp.common.crypto.ssl.WaarpSslUtility;
32 import org.waarp.common.logging.WaarpLogger;
33 import org.waarp.common.logging.WaarpLoggerFactory;
34 import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
35 import org.waarp.ftp.core.config.FtpConfiguration;
36
37 import ch.qos.logback.classic.LoggerContext;
38
39
40
41
42
43
44
45 public class FtpChannelUtils implements Runnable {
46
47
48
49 private static final WaarpLogger logger = WaarpLoggerFactory
50 .getLogger(FtpChannelUtils.class);
51
52
53
54
55
56
57
58 public static InetAddress getRemoteInetAddress(Channel channel) {
59 InetSocketAddress socketAddress = (InetSocketAddress) channel.remoteAddress();
60 if (socketAddress == null) {
61 socketAddress = new InetSocketAddress(20);
62 }
63 return socketAddress.getAddress();
64 }
65
66
67
68
69
70
71
72 public static InetAddress getLocalInetAddress(Channel channel) {
73 InetSocketAddress socketAddress = (InetSocketAddress) channel.localAddress();
74 return socketAddress.getAddress();
75 }
76
77
78
79
80
81
82
83 public static InetSocketAddress getRemoteInetSocketAddress(Channel channel) {
84 return (InetSocketAddress) channel.remoteAddress();
85 }
86
87
88
89
90
91
92
93 public static InetSocketAddress getLocalInetSocketAddress(Channel channel) {
94 return (InetSocketAddress) channel.localAddress();
95 }
96
97
98
99
100
101
102
103 public static InetSocketAddress getInetSocketAddress(String arg) {
104 String[] elements = arg.split(",");
105 if (elements.length != 6) {
106 return null;
107 }
108 byte[] address = new byte[4];
109 int[] iElements = new int[6];
110 for (int i = 0; i < 6; i++) {
111 try {
112 iElements[i] = Integer.parseInt(elements[i]);
113 } catch (NumberFormatException e) {
114 return null;
115 }
116 if (iElements[i] < 0 || iElements[i] > 255) {
117 return null;
118 }
119 }
120 for (int i = 0; i < 4; i++) {
121 address[i] = (byte) iElements[i];
122 }
123 int port = iElements[4] << 8 | iElements[5];
124 InetAddress inetAddress;
125 try {
126 inetAddress = InetAddress.getByAddress(address);
127 } catch (UnknownHostException e) {
128 return null;
129 }
130 return new InetSocketAddress(inetAddress, port);
131 }
132
133
134
135
136
137
138
139
140 public static String getAddress(String address, int port) {
141 return address.replace('.', ',') + ',' +
142 (port >> 8) + ',' + (port & 0xFF);
143 }
144
145
146
147
148
149
150
151 public static String getAddress(InetSocketAddress address) {
152 InetAddress servAddr = address.getAddress();
153 int servPort = address.getPort();
154 return servAddr.getHostAddress().replace('.', ',') + ',' +
155 (servPort >> 8) + ',' + (servPort & 0xFF);
156 }
157
158
159
160
161
162
163
164 public static InetSocketAddress get2428InetSocketAddress(String arg) {
165
166
167 if (arg == null || arg.length() == 0) {
168
169 return null;
170 }
171 String delim = arg.substring(0, 1);
172 String[] infos = arg.split("\\" + delim);
173 if (infos.length != 3 && infos.length != 4) {
174
175 logger.error("Bad address format: " + infos.length);
176 return null;
177 }
178 int start = 0;
179 if (infos.length == 4) {
180 start = 1;
181 }
182 boolean isIPV4 = true;
183 if (infos[start].equals("1")) {
184 isIPV4 = true;
185 } else if (infos[start].equals("2")) {
186 isIPV4 = false;
187 } else {
188
189 logger.error("Bad 1 or 2 format in address: " + infos[start]);
190 return null;
191 }
192 start++;
193 InetAddress inetAddress;
194 if (isIPV4) {
195
196 try {
197 inetAddress = (Inet4Address) InetAddress.getByName(infos[start]);
198 } catch (UnknownHostException e) {
199 logger.error("Bad IPV4 format", e);
200 return null;
201 }
202 } else {
203
204 try {
205 inetAddress = (Inet6Address) InetAddress.getByName(infos[start]);
206 } catch (UnknownHostException e) {
207 logger.error("Bad IPV6 format", e);
208 return null;
209 }
210 }
211 start++;
212 int port = 0;
213 try {
214 port = Integer.parseInt(infos[start]);
215 } catch (NumberFormatException e) {
216 logger.error("Bad port number format: " + infos[start]);
217 return null;
218 }
219 return new InetSocketAddress(inetAddress, port);
220 }
221
222
223
224
225
226
227
228 public static String get2428Address(InetSocketAddress address) {
229 InetAddress servAddr = address.getAddress();
230 int servPort = address.getPort();
231 StringBuilder builder = new StringBuilder();
232 String hostaddress = servAddr.getHostAddress();
233 builder.append('|');
234 if (hostaddress.contains(":")) {
235 builder.append('2');
236 } else {
237 builder.append('1');
238 }
239 builder.append('|').append(hostaddress).append('|').append(servPort).append('|');
240 return builder.toString();
241 }
242
243
244
245
246
247
248
249 static int terminateCommandChannels(final FtpConfiguration configuration) {
250 int result = configuration.getFtpInternalConfiguration()
251 .getCommandChannelGroup().size();
252 configuration.getFtpInternalConfiguration().getCommandChannelGroup()
253 .close();
254 return result;
255 }
256
257
258
259
260
261
262
263 private static int terminateDataChannels(final FtpConfiguration configuration) {
264 int result = configuration.getFtpInternalConfiguration()
265 .getDataChannelGroup().size();
266 configuration.getFtpInternalConfiguration().getDataChannelGroup()
267 .close();
268 return result;
269 }
270
271
272
273
274
275
276
277 public static int nbCommandChannels(FtpConfiguration configuration) {
278 return configuration.getFtpInternalConfiguration()
279 .getCommandChannelGroup().size();
280 }
281
282
283
284
285
286
287
288 public static int nbDataChannels(FtpConfiguration configuration) {
289 return configuration.getFtpInternalConfiguration()
290 .getDataChannelGroup().size();
291 }
292
293
294
295
296
297
298
299 public static int validCommandChannels(FtpConfiguration configuration) {
300 int result = 0;
301 Channel channel = null;
302 Iterator<Channel> iterator = configuration
303 .getFtpInternalConfiguration().getCommandChannelGroup()
304 .iterator();
305 while (iterator.hasNext()) {
306 channel = iterator.next();
307 if (channel.parent() != null) {
308
309 if (channel.isActive()) {
310
311 result++;
312 } else {
313 WaarpSslUtility.closingSslChannel(channel);
314 }
315 } else {
316
317 result++;
318 }
319 }
320 return result;
321 }
322
323
324
325
326
327
328 protected static void exit(FtpConfiguration configuration) {
329 configuration.setShutdown(true);
330 long delay = configuration.getTIMEOUTCON() / 2;
331 logger.warn("Exit: Give a delay of " + delay + " ms");
332 configuration.inShutdownProcess();
333 try {
334 Thread.sleep(delay);
335 } catch (InterruptedException e) {
336 }
337 Timer timer = new Timer(true);
338 FtpTimerTask timerTask = new FtpTimerTask(FtpTimerTask.TIMER_CONTROL);
339 timerTask.setConfiguration(configuration);
340 timer.schedule(timerTask, configuration.getTIMEOUTCON() / 4);
341 configuration.getFtpInternalConfiguration()
342 .getGlobalTrafficShapingHandler().release();
343 configuration.releaseResources();
344 logger.info("Exit Shutdown Data");
345 terminateDataChannels(configuration);
346 logger.warn("Exit end of Data Shutdown");
347 }
348
349
350
351
352
353
354 public static void teminateServer(FtpConfiguration configuration) {
355 FtpShutdownHook.configuration = configuration;
356 FtpShutdownHook.terminate(false);
357 }
358
359
360
361
362
363
364
365 public static void addCommandChannel(Channel channel,
366 FtpConfiguration configuration) {
367
368 configuration.getFtpInternalConfiguration().getCommandChannelGroup()
369 .add(channel);
370 }
371
372
373
374
375
376
377
378 public static void addDataChannel(Channel channel,
379 FtpConfiguration configuration) {
380
381 configuration.getFtpInternalConfiguration().getDataChannelGroup().add(
382 channel);
383 }
384
385
386
387
388 private FtpConfiguration configuration;
389
390 public FtpChannelUtils(FtpConfiguration configuration) {
391 this.configuration = configuration;
392 }
393
394 @Override
395 public void run() {
396 exit(configuration);
397 }
398
399 public static void stopLogger() {
400 if (WaarpLoggerFactory.getDefaultFactory() instanceof WaarpSlf4JLoggerFactory) {
401 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
402 lc.stop();
403 }
404 }
405
406 }