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 */
017package org.jivesoftware.smackx.jingleold.nat;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.io.OutputStream;
022import java.net.DatagramPacket;
023import java.net.DatagramSocket;
024import java.net.InetAddress;
025import java.net.Socket;
026import java.util.logging.Level;
027import java.util.logging.Logger;
028
029/**
030 * A Simple and Experimental Bridge.
031 * It Creates a TCP Socket That Connects to another TCP Socket Listener and forwards every packets received to an UDP Listener.
032 * And forwards every packets received in UDP Socket, to the TCP Server
033 */
034public class TcpUdpBridgeClient {
035
036    private static final Logger LOGGER = Logger.getLogger(TcpUdpBridgeClient.class.getName());
037
038    private String remoteTcpHost = null;
039    private String remoteUdpHost = null;
040    private int remoteTcpPort = -1;
041    private int remoteUdpPort = -1;
042    private int localUdpPort = -1;
043
044    private DatagramSocket localUdpSocket;
045    private Socket localTcpSocket;
046
047    public TcpUdpBridgeClient(String remoteTcpHost, String remoteUdpHost, int remoteTcpPort, int remoteUdpPort) {
048        this.remoteTcpHost = remoteTcpHost;
049        this.remoteUdpHost = remoteUdpHost;
050        this.remoteTcpPort = remoteTcpPort;
051        this.remoteUdpPort = remoteUdpPort;
052
053        try {
054            localTcpSocket = new Socket(remoteTcpHost, remoteTcpPort);
055            localUdpSocket = new DatagramSocket(0);
056            localUdpPort = localUdpSocket.getLocalPort();
057            LOGGER.fine("UDP: " + localUdpSocket.getLocalPort());
058        }
059        catch (IOException e) {
060            LOGGER.log(Level.WARNING, "exception", e);
061        }
062        startBridge();
063    }
064
065    public void startBridge() {
066
067
068        final Thread process = new Thread(new Runnable() {
069
070            @Override
071            public void run() {
072                try {
073                    OutputStream out = localTcpSocket.getOutputStream();
074
075                    while (true) {
076
077                        byte[] b = new byte[500];
078                        DatagramPacket p = new DatagramPacket(b, 500);
079
080                        localUdpSocket.receive(p);
081                        if (p.getLength() == 0) continue;
082
083                        LOGGER.fine("UDP Client Received and Sending to TCP Server:" + new String(p.getData(), 0, p.getLength(), "UTF-8"));
084
085                        out.write(p.getData(), 0, p.getLength());
086                        out.flush();
087                        LOGGER.fine("Client Flush");
088
089                    }
090
091                }
092                catch (IOException e) {
093                    LOGGER.log(Level.WARNING, "exception", e);
094                }
095            }
096
097        });
098
099        new Thread(new Runnable() {
100
101            @Override
102            public void run() {
103                try {
104
105                    InputStream in = localTcpSocket.getInputStream();
106                    InetAddress remoteHost = InetAddress.getByName(remoteUdpHost);
107                    process.start();
108
109                    while (true) {
110                        byte[] b = new byte[500];
111
112                        int s = in.read(b);
113                        // if (s == -1) continue;
114
115                        LOGGER.fine("TCP Client:" + new String(b, 0, s, "UTF-8"));
116
117                        DatagramPacket udpPacket = new DatagramPacket(b, s);
118
119                        udpPacket.setAddress(remoteHost);
120                        udpPacket.setPort(remoteUdpPort);
121
122                        localUdpSocket.send(udpPacket);
123
124                    }
125
126                }
127                catch (IOException e) {
128                    LOGGER.log(Level.WARNING, "exception", e);
129                }
130            }
131
132        }).start();
133    }
134
135    public Socket getLocalTcpSocket() {
136        return localTcpSocket;
137    }
138
139    public DatagramSocket getLocalUdpSocket() {
140        return localUdpSocket;
141    }
142}