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 */
036@SuppressWarnings("UnusedVariable")
037public class TcpUdpBridgeServer {
038
039    private static final Logger LOGGER = Logger.getLogger(TcpUdpBridgeServer.class.getName());
040
041    private String remoteTcpHost = null;
042    private String remoteUdpHost = null;
043    private int remoteTcpPort = -1;
044    private int remoteUdpPort = -1;
045    private int localUdpPort = -1;
046
047    private DatagramSocket localUdpSocket;
048    private Socket localTcpSocket;
049    private ServerSocket serverTcpSocket;
050
051    public TcpUdpBridgeServer(String remoteTcpHost, String remoteUdpHost, int remoteTcpPort, int remoteUdpPort) {
052        this.remoteTcpHost = remoteTcpHost;
053        this.remoteUdpHost = remoteUdpHost;
054        this.remoteTcpPort = remoteTcpPort;
055        this.remoteUdpPort = remoteUdpPort;
056
057        try {
058            serverTcpSocket = new ServerSocket(remoteTcpPort);
059            localUdpSocket = new DatagramSocket(0);
060            localUdpPort = localUdpSocket.getLocalPort();
061            LOGGER.fine("UDP: " + localUdpSocket.getLocalPort());
062        }
063        catch (IOException e) {
064            LOGGER.log(Level.WARNING, "exception", e);
065        }
066        startBridge();
067    }
068
069    public void startBridge() {
070
071        final Thread process = new Thread(new Runnable() {
072
073            @Override
074            public void run() {
075                try {
076                    OutputStream out = localTcpSocket.getOutputStream();
077
078                    while (true) {
079
080                        byte[] b = new byte[500];
081                        DatagramPacket p = new DatagramPacket(b, 500);
082
083                        localUdpSocket.receive(p);
084                        if (p.getLength() == 0) continue;
085
086                        LOGGER.fine("UDP Server Received and Sending to TCP Client:" + new String(p.getData(), 0, p.getLength(), "UTF-8"));
087
088                        out.write(p.getData(), 0, p.getLength());
089                        out.flush();
090                        LOGGER.fine("Server Flush");
091                    }
092
093                }
094                catch (IOException e) {
095                    LOGGER.log(Level.WARNING, "exception", e);
096                }
097            }
098
099        });
100
101        new Thread(new Runnable() {
102
103            @Override
104            public void run() {
105                try {
106
107                    localTcpSocket = serverTcpSocket.accept();
108                    process.start();
109                    InputStream in = localTcpSocket.getInputStream();
110                    InetAddress remoteHost = InetAddress.getByName(remoteUdpHost);
111
112                    while (true) {
113                        byte[] b = new byte[500];
114
115                        int s = in.read(b);
116                        // if (s == -1) continue;
117
118                        LOGGER.fine("TCP Server:" + new String(b, 0, s, "UTF-8"));
119
120                        DatagramPacket udpPacket = new DatagramPacket(b, s);
121
122                        udpPacket.setAddress(remoteHost);
123                        udpPacket.setPort(remoteUdpPort);
124
125                        localUdpSocket.send(udpPacket);
126
127                    }
128
129                }
130                catch (IOException e) {
131                    LOGGER.log(Level.WARNING, "exception", e);
132                }
133            }
134
135        }).start();
136    }
137
138    public Socket getLocalTcpSocket() {
139        return localTcpSocket;
140    }
141
142    public DatagramSocket getLocalUdpSocket() {
143        return localUdpSocket;
144    }
145}