2013-02-07 15:19:47 +01:00
|
|
|
/**
|
2014-02-19 10:38:30 +01:00
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
2013-02-07 15:19:47 +01:00
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-02-19 10:38:30 +01:00
|
|
|
|
2014-08-20 00:25:57 +02:00
|
|
|
package org.jivesoftware.smackx.jingleold.nat;
|
2007-03-24 18:54:23 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2008-10-30 22:20:29 +01:00
|
|
|
import java.net.DatagramPacket;
|
|
|
|
import java.net.DatagramSocket;
|
|
|
|
import java.net.InetAddress;
|
|
|
|
import java.net.ServerSocket;
|
|
|
|
import java.net.Socket;
|
2015-03-17 21:19:06 +01:00
|
|
|
import java.util.logging.Level;
|
2014-02-19 10:38:30 +01:00
|
|
|
import java.util.logging.Logger;
|
2007-03-24 18:54:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A Simple and Experimental Bridge.
|
|
|
|
* It Creates a TCP Socket Listeners for Connections and forwards every packets received to an UDP Listener.
|
|
|
|
* And forwards every packets received in UDP Socket, to the TCP Client
|
|
|
|
*/
|
|
|
|
public class TcpUdpBridgeServer {
|
|
|
|
|
2014-02-19 10:38:30 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(TcpUdpBridgeServer.class.getName());
|
2008-10-30 22:20:29 +01:00
|
|
|
|
|
|
|
private String remoteTcpHost = null;
|
2007-03-24 18:54:23 +01:00
|
|
|
private String remoteUdpHost = null;
|
|
|
|
private int remoteTcpPort = -1;
|
|
|
|
private int remoteUdpPort = -1;
|
|
|
|
private int localUdpPort = -1;
|
|
|
|
|
|
|
|
private DatagramSocket localUdpSocket;
|
|
|
|
private Socket localTcpSocket;
|
|
|
|
private ServerSocket serverTcpSocket;
|
|
|
|
|
|
|
|
public TcpUdpBridgeServer(String remoteTcpHost, String remoteUdpHost, int remoteTcpPort, int remoteUdpPort) {
|
|
|
|
this.remoteTcpHost = remoteTcpHost;
|
|
|
|
this.remoteUdpHost = remoteUdpHost;
|
|
|
|
this.remoteTcpPort = remoteTcpPort;
|
|
|
|
this.remoteUdpPort = remoteUdpPort;
|
|
|
|
|
|
|
|
try {
|
|
|
|
serverTcpSocket = new ServerSocket(remoteTcpPort);
|
|
|
|
localUdpSocket = new DatagramSocket(0);
|
|
|
|
localUdpPort = localUdpSocket.getLocalPort();
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("UDP: " + localUdpSocket.getLocalPort());
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
startBridge();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void startBridge() {
|
|
|
|
|
|
|
|
final Thread process = new Thread(new Runnable() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
OutputStream out = localTcpSocket.getOutputStream();
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
2014-08-15 23:16:18 +02:00
|
|
|
byte[] b = new byte[500];
|
2007-03-24 18:54:23 +01:00
|
|
|
DatagramPacket p = new DatagramPacket(b, 500);
|
|
|
|
|
|
|
|
localUdpSocket.receive(p);
|
|
|
|
if (p.getLength() == 0) continue;
|
|
|
|
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("UDP Server Received and Sending to TCP Client:" + new String(p.getData(), 0, p.getLength(), "UTF-8"));
|
2007-03-24 18:54:23 +01:00
|
|
|
|
|
|
|
out.write(p.getData(), 0, p.getLength());
|
|
|
|
out.flush();
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("Server Flush");
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
new Thread(new Runnable() {
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
try {
|
|
|
|
|
|
|
|
localTcpSocket = serverTcpSocket.accept();
|
|
|
|
process.start();
|
|
|
|
InputStream in = localTcpSocket.getInputStream();
|
|
|
|
InetAddress remoteHost = InetAddress.getByName(remoteUdpHost);
|
|
|
|
|
|
|
|
while (true) {
|
2014-08-15 23:16:18 +02:00
|
|
|
byte[] b = new byte[500];
|
2007-03-24 18:54:23 +01:00
|
|
|
|
|
|
|
int s = in.read(b);
|
|
|
|
//if (s == -1) continue;
|
|
|
|
|
2014-02-19 10:38:30 +01:00
|
|
|
LOGGER.fine("TCP Server:" + new String(b, 0, s, "UTF-8"));
|
2007-03-24 18:54:23 +01:00
|
|
|
|
|
|
|
DatagramPacket udpPacket = new DatagramPacket(b, s);
|
|
|
|
|
|
|
|
udpPacket.setAddress(remoteHost);
|
|
|
|
udpPacket.setPort(remoteUdpPort);
|
|
|
|
|
|
|
|
localUdpSocket.send(udpPacket);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (IOException e) {
|
2015-03-17 21:19:06 +01:00
|
|
|
LOGGER.log(Level.WARNING, "exception", e);
|
2007-03-24 18:54:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Socket getLocalTcpSocket() {
|
|
|
|
return localTcpSocket;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DatagramSocket getLocalUdpSocket() {
|
|
|
|
return localUdpSocket;
|
|
|
|
}
|
|
|
|
}
|