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 */ 034@SuppressWarnings("UnusedVariable") 035public class TcpUdpBridgeClient { 036 037 private static final Logger LOGGER = Logger.getLogger(TcpUdpBridgeClient.class.getName()); 038 039 private String remoteTcpHost = null; 040 private String remoteUdpHost = null; 041 private int remoteTcpPort = -1; 042 private int remoteUdpPort = -1; 043 private int localUdpPort = -1; 044 045 private DatagramSocket localUdpSocket; 046 private Socket localTcpSocket; 047 048 @SuppressWarnings("this-escape") 049 public TcpUdpBridgeClient(String remoteTcpHost, String remoteUdpHost, int remoteTcpPort, int remoteUdpPort) { 050 this.remoteTcpHost = remoteTcpHost; 051 this.remoteUdpHost = remoteUdpHost; 052 this.remoteTcpPort = remoteTcpPort; 053 this.remoteUdpPort = remoteUdpPort; 054 055 try { 056 localTcpSocket = new Socket(remoteTcpHost, remoteTcpPort); 057 localUdpSocket = new DatagramSocket(0); 058 localUdpPort = localUdpSocket.getLocalPort(); 059 LOGGER.fine("UDP: " + localUdpSocket.getLocalPort()); 060 } 061 catch (IOException e) { 062 LOGGER.log(Level.WARNING, "exception", e); 063 } 064 startBridge(); 065 } 066 067 public void startBridge() { 068 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 Client Received and Sending to TCP Server:" + new String(p.getData(), 0, p.getLength(), "UTF-8")); 086 087 out.write(p.getData(), 0, p.getLength()); 088 out.flush(); 089 LOGGER.fine("Client Flush"); 090 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 InputStream in = localTcpSocket.getInputStream(); 108 InetAddress remoteHost = InetAddress.getByName(remoteUdpHost); 109 process.start(); 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 Client:" + 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}