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 public TcpUdpBridgeClient(String remoteTcpHost, String remoteUdpHost, int remoteTcpPort, int remoteUdpPort) { 049 this.remoteTcpHost = remoteTcpHost; 050 this.remoteUdpHost = remoteUdpHost; 051 this.remoteTcpPort = remoteTcpPort; 052 this.remoteUdpPort = remoteUdpPort; 053 054 try { 055 localTcpSocket = new Socket(remoteTcpHost, remoteTcpPort); 056 localUdpSocket = new DatagramSocket(0); 057 localUdpPort = localUdpSocket.getLocalPort(); 058 LOGGER.fine("UDP: " + localUdpSocket.getLocalPort()); 059 } 060 catch (IOException e) { 061 LOGGER.log(Level.WARNING, "exception", e); 062 } 063 startBridge(); 064 } 065 066 public void startBridge() { 067 068 069 final Thread process = new Thread(new Runnable() { 070 071 @Override 072 public void run() { 073 try { 074 OutputStream out = localTcpSocket.getOutputStream(); 075 076 while (true) { 077 078 byte[] b = new byte[500]; 079 DatagramPacket p = new DatagramPacket(b, 500); 080 081 localUdpSocket.receive(p); 082 if (p.getLength() == 0) continue; 083 084 LOGGER.fine("UDP Client Received and Sending to TCP Server:" + new String(p.getData(), 0, p.getLength(), "UTF-8")); 085 086 out.write(p.getData(), 0, p.getLength()); 087 out.flush(); 088 LOGGER.fine("Client Flush"); 089 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 InputStream in = localTcpSocket.getInputStream(); 107 InetAddress remoteHost = InetAddress.getByName(remoteUdpHost); 108 process.start(); 109 110 while (true) { 111 byte[] b = new byte[500]; 112 113 int s = in.read(b); 114 // if (s == -1) continue; 115 116 LOGGER.fine("TCP Client:" + new String(b, 0, s, "UTF-8")); 117 118 DatagramPacket udpPacket = new DatagramPacket(b, s); 119 120 udpPacket.setAddress(remoteHost); 121 udpPacket.setPort(remoteUdpPort); 122 123 localUdpSocket.send(udpPacket); 124 125 } 126 127 } 128 catch (IOException e) { 129 LOGGER.log(Level.WARNING, "exception", e); 130 } 131 } 132 133 }).start(); 134 } 135 136 public Socket getLocalTcpSocket() { 137 return localTcpSocket; 138 } 139 140 public DatagramSocket getLocalUdpSocket() { 141 return localUdpSocket; 142 } 143}