Handle correctly TCP framing header for both local and relayed TCP socket. Also check that we read framing length before passing up to applications.

This commit is contained in:
seb 2011-10-13 11:36:57 +00:00
parent 338409c769
commit b6213785aa
5 changed files with 129 additions and 16 deletions

View File

@ -756,14 +756,7 @@ public class DelegatingSocket
outputStream = getOutputStream();
}
int len = p.getLength();
int off = p.getOffset();
byte data[] = new byte[len + 2];
data[0] = (byte) ((len >> 8) & 0xff);
data[1] = (byte) (len & 0xff);
System.arraycopy(p.getData(), off, data, 2, len);
outputStream.write(data, 0, len + 2);
data = null;
outputStream.write(p.getData(), p.getOffset(), p.getLength());
}
/**
@ -802,9 +795,22 @@ public class DelegatingSocket
inputStream = this.getInputStream();
}
inputStream.skip(2);
len = inputStream.read(data);
if (len > 0)
short desiredLength = (short)
(((inputStream.read() << 8) & 0xff) | (inputStream.read() & 0xff));
int readLen = 0;
int offset = 0;
while(readLen < desiredLength)
{
len = inputStream.read(data, offset, desiredLength - offset);
if(len == -1)
throw new SocketException("read failed");
offset += len;
readLen += len;
}
if (readLen == desiredLength)
{
p.setData(data);
p.setLength(len);

View File

@ -319,8 +319,8 @@ public class GoogleRelayedCandidateSocket
{
DatagramPacket p = new DatagramPacket(
b,
off + 2,
len - 2,
off,
len,
target);
send(p);

View File

@ -61,6 +61,11 @@ public class MultiplexingSocket
*/
private final TCPInputStream inputStream = new TCPInputStream();
/**
* Custom <tt>OutputStream</tt> for this <tt>Socket</tt>.
*/
private TCPOutputStream outputStream = null;
/**
* The <tt>Object</tt> which synchronizes the access to {@link #inReceive}.
*/
@ -512,4 +517,18 @@ public class MultiplexingSocket
{
return inputStream;
}
/**
* {@inheritDoc}
*/
public OutputStream getOutputStream()
throws IOException
{
if(outputStream == null)
{
outputStream = new TCPOutputStream(super.getOutputStream());
}
return outputStream;
}
}

View File

@ -181,10 +181,12 @@ public class TCPInputStream
public int read()
throws IOException
{
//getNextPacket();
getNextPacket();
int ret = currentPacket[currentPacketOffset];
currentPacketOffset++;
currentPacketLength--;
// TODO Auto-generated method stub
return 0;
return ret;
}
/**

View File

@ -0,0 +1,86 @@
/*
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
* Maintained by the SIP Communicator community (http://sip-communicator.org).
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.ice4j.socket;
import java.io.*;
/**
* TCP output stream for TCP socket. It is used to multiplex sockets and keep
* the <tt>OutputStream</tt> interface to users.
*
* @author Sebastien Vincent
*/
public class TCPOutputStream
extends OutputStream
{
/**
* Original <tt>OutputStream</tt> that this class wraps.
*/
private final OutputStream outputStream;
/**
* Initializes a new <tt>TCPOutputStream</tt>.
*
* @param outputStream original <tt>OutputStream</tt>
*/
public TCPOutputStream(OutputStream outputStream)
{
this.outputStream = outputStream;
}
/**
* {@inheritDoc}
*/
@Override
public void close()
throws IOException
{
}
/**
* {@inheritDoc}
*/
@Override
public void flush()
throws IOException
{
}
/**
* {@inheritDoc}
*/
@Override
public void write(byte[] b)
throws IOException
{
write(b, 0, b.length);
}
/**
* {@inheritDoc}
*/
@Override
public void write(byte[] b, int off, int len)
throws IOException
{
byte data[] = new byte[len + 2];
data[0] = (byte)((len >> 8) & 0xff);
data[1] = (byte)(len & 0xff);
System.arraycopy(b, off, data, 2, len);
outputStream.write(data, 0, len + 2);
}
/**
* {@inheritDoc}
*/
@Override
public void write(int arg0) throws IOException
{
// TODO Auto-generated method stub
}
}