001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smackx.jingleold.nat;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.net.DatagramPacket;
024import java.net.DatagramSocket;
025import java.net.InetAddress;
026import java.net.ServerSocket;
027import java.net.Socket;
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031/**
032 * A Simple and Experimental Bridge.
033 * It Creates a TCP Socket Listeners for Connections and forwards every packets received to an UDP Listener.
034 * And forwards every packets received in UDP Socket, to the TCP Client
035 */
036public class TcpUdpBridgeServer {
037
038    private static final Logger LOGGER = Logger.getLogger(TcpUdpBridgeServer.class.getName());
039
040    private String remoteTcpHost = null;
041    private String remoteUdpHost = null;
042    private int remoteTcpPort = -1;
043    private int remoteUdpPort = -1;
044    private int localUdpPort = -1;
045
046    private DatagramSocket localUdpSocket;
047    private Socket localTcpSocket;
048    private ServerSocket serverTcpSocket;
049
050    public TcpUdpBridgeServer(String remoteTcpHost, String remoteUdpHost, int remoteTcpPort, int remoteUdpPort) {
051        this.remoteTcpHost = remoteTcpHost;
052        this.remoteUdpHost = remoteUdpHost;
053        this.remoteTcpPort = remoteTcpPort;
054        this.remoteUdpPort = remoteUdpPort;
055
056        try {
057            serverTcpSocket = new ServerSocket(remoteTcpPort);
058            localUdpSocket = new DatagramSocket(0);
059            localUdpPort = localUdpSocket.getLocalPort();
060            LOGGER.fine("UDP: " + localUdpSocket.getLocalPort());
061        }
062        catch (IOException e) {
063            LOGGER.log(Level.WARNING, "exception", e);
064        }
065        startBridge();
066    }
067
068    public void startBridge() {
069
070        final Thread process = new Thread(new Runnable() {
071
072            @Override
073            public void run() {
074                try {
075                    OutputStream out = localTcpSocket.getOutputStream();
076
077                    while (true) {
078
079                        byte[] b = new byte[500];
080                        DatagramPacket p = new DatagramPacket(b, 500);
081
082                        localUdpSocket.receive(p);
083                        if (p.getLength() == 0) continue;
084
085                        LOGGER.fine("UDP Server Received and Sending to TCP Client:" + new String(p.getData(), 0, p.getLength(), "UTF-8"));
086
087                        out.write(p.getData(), 0, p.getLength());
088                        out.flush();
089                        LOGGER.fine("Server Flush");
090                    }
091
092                }
093                catch (IOException e) {
094                    LOGGER.log(Level.WARNING, "exception", e);
095                }
096            }
097
098        });
099
100        new Thread(new Runnable() {
101
102            @Override
103            public void run() {
104                try {
105
106                    localTcpSocket = serverTcpSocket.accept();
107                    process.start();
108                    InputStream in = localTcpSocket.getInputStream();
109                    InetAddress remoteHost = InetAddress.getByName(remoteUdpHost);
110
111                    while (true) {
112                        byte[] b = new byte[500];
113
114                        int s = in.read(b);
115                        // if (s == -1) continue;
116
117                        LOGGER.fine("TCP Server:" + new String(b, 0, s, "UTF-8"));
118
119                        DatagramPacket udpPacket = new DatagramPacket(b, s);
120
121                        udpPacket.setAddress(remoteHost);
122                        udpPacket.setPort(remoteUdpPort);
123
124                        localUdpSocket.send(udpPacket);
125
126                    }
127
128                }
129                catch (IOException e) {
130                    LOGGER.log(Level.WARNING, "exception", e);
131                }
132            }
133
134        }).start();
135    }
136
137    public Socket getLocalTcpSocket() {
138        return localTcpSocket;
139    }
140
141    public DatagramSocket getLocalUdpSocket() {
142        return localUdpSocket;
143    }
144}