From fa8ec11955fc152c5da68c5da99a6ec70e4618a7 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Wed, 18 Jul 2012 13:38:19 +0000 Subject: [PATCH] Added pseudoTCP --- build.xml | 54 +- src/org/ice4j/pseudotcp/IPseudoTcpNotify.java | 52 + src/org/ice4j/pseudotcp/PseudoTCPBase.java | 2132 +++++++++++++++++ src/org/ice4j/pseudotcp/PseudoTcpSocket.java | 788 ++++++ src/org/ice4j/pseudotcp/PseudoTcpState.java | 38 + src/org/ice4j/pseudotcp/WriteResult.java | 28 + .../ice4j/pseudotcp/util/ByteFifoBuffer.java | 381 +++ .../pseudotcp/MultiThreadSupportTest.java | 75 + .../ice4j/pseudotcp/PseudoTcpStreamTest.java | 139 ++ .../ice4j/pseudotcp/PseudoTcpTestBase.java | 684 ++++++ .../pseudotcp/PseudoTcpTestPingPong.java | 357 +++ .../pseudotcp/PseudoTcpTestRecvWindow.java | 404 ++++ .../pseudotcp/PseudoTcpTestTransfer.java | 445 ++++ .../pseudotcp/util/ByteFifoBufferTest.java | 339 +++ test/test/Ice.java | 2 +- test/test/IcePseudoTcp.java | 488 ++++ 16 files changed, 6392 insertions(+), 14 deletions(-) create mode 100755 src/org/ice4j/pseudotcp/IPseudoTcpNotify.java create mode 100755 src/org/ice4j/pseudotcp/PseudoTCPBase.java create mode 100755 src/org/ice4j/pseudotcp/PseudoTcpSocket.java create mode 100755 src/org/ice4j/pseudotcp/PseudoTcpState.java create mode 100755 src/org/ice4j/pseudotcp/WriteResult.java create mode 100755 src/org/ice4j/pseudotcp/util/ByteFifoBuffer.java create mode 100755 test/org/ice4j/pseudotcp/MultiThreadSupportTest.java create mode 100755 test/org/ice4j/pseudotcp/PseudoTcpStreamTest.java create mode 100755 test/org/ice4j/pseudotcp/PseudoTcpTestBase.java create mode 100755 test/org/ice4j/pseudotcp/PseudoTcpTestPingPong.java create mode 100755 test/org/ice4j/pseudotcp/PseudoTcpTestRecvWindow.java create mode 100755 test/org/ice4j/pseudotcp/PseudoTcpTestTransfer.java create mode 100755 test/org/ice4j/pseudotcp/util/ByteFifoBufferTest.java create mode 100755 test/test/IcePseudoTcp.java diff --git a/build.xml b/build.xml index a9b0b5b..a72149b 100644 --- a/build.xml +++ b/build.xml @@ -145,10 +145,10 @@ for a general build success while this might not be the case.--> + classname="org.apache.tools.ant.launch.Launcher" + classpath="${java.class.path}:${lib}/xalan-2.6.0.jar" + failonerror="true" + output="${junit.html.reports}/output.txt"> @@ -156,36 +156,64 @@ + classname="test.Ice" + classpathref="project.class.path"> + value="lib/logging.properties"/> + + + + + + + + + + + + + + + + + + + + + + + + + classname="test.IceDistributed" + classpathref="project.class.path"> + value="lib/logging.properties"/> + classname="test.IceLite" + classpathref="project.class.path"> + value="lib/logging.properties"/> diff --git a/src/org/ice4j/pseudotcp/IPseudoTcpNotify.java b/src/org/ice4j/pseudotcp/IPseudoTcpNotify.java new file mode 100755 index 0000000..395cf64 --- /dev/null +++ b/src/org/ice4j/pseudotcp/IPseudoTcpNotify.java @@ -0,0 +1,52 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; + +/** + * Notification of tcp events. + * Is implemented by @link(PseudoTcpSocket) to expose stream functionality. + * @author Pawel Domas + */ +interface IPseudoTcpNotify +{ + /** + * Called when tcp enters opened state + * @param tcp + */ + void OnTcpOpen(PseudoTCPBase tcp); + + /** + * Called when any data is available in read buffer + * @param tcp + */ + void OnTcpReadable(PseudoTCPBase tcp); + + /** + * Called when there is free space available in the send buffer + * @param tcp + */ + void OnTcpWriteable(PseudoTCPBase tcp); + + /** + * Called when tcp enters closed state + * @param tcp + * @param e null means no error + */ + void OnTcpClosed(PseudoTCPBase tcp, IOException e); + + /** + * Called when protocol requests packet transfer through the network. + * @param tcp + * @param buffer data + * @param len data length + * @return the result, see {@link WriteResult} description for more info + */ + WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len); +} diff --git a/src/org/ice4j/pseudotcp/PseudoTCPBase.java b/src/org/ice4j/pseudotcp/PseudoTCPBase.java new file mode 100755 index 0000000..dc28cd1 --- /dev/null +++ b/src/org/ice4j/pseudotcp/PseudoTCPBase.java @@ -0,0 +1,2132 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.util.*; +import java.util.logging.*; +import org.ice4j.pseudotcp.util.*; + +/** + * Main protocol logic class. To open connection use Connect() method. Then @link(Recv) and @link(Send) operations may be used for data transfer. + * To operate this class requires implementation of @link(IPseudoTcpNotify) + * Also it must be notified about the time progress. + * + * @author Pawel Domas + */ +public class PseudoTCPBase +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(PseudoTCPBase.class.getName()); + /** + * Keepalive - disabled by default + */ + private static boolean PSEUDO_KEEPALIVE = false; + /** + * Packet maximum levels + */ + static final int[] PACKET_MAXIMUMS = new int[] + { + 65535, // Theoretical maximum, Hyperchannel + 32000, // Nothing + 17914, // 16Mb IBM Token Ring + 8166, // IEEE 802.4 + //4464, // IEEE 802.5 (4Mb max) + 4352, // FDDI + //2048, // Wideband Network + 2002, // IEEE 802.5 (4Mb recommended) + //1536, // Expermental Ethernet Networks + //1500, // Ethernet, Point-to-Point (default) + 1492, // IEEE 802.3 + 1006, // SLIP, ARPANET + //576, // X.25 Networks + //544, // DEC IP Portal + //512, // NETBIOS + 508, // IEEE 802/Source-Rt Bridge, ARCNET + 296, // Point-to-Point (low delay) + //68, // Official minimum + 0, // End of list marker + }; + static final int MAX_PACKET = 65535; + // Note: we removed lowest level because packet overhead was larger! + static final int MIN_PACKET = 296; + static final int IP_HEADER_SIZE = 20; // (+ up to 40 bytes of options?) + static final int ICMP_HEADER_SIZE = 8; + static final int UDP_HEADER_SIZE = 8; + // TODO: Make JINGLE_HEADER_SIZE transparent to this code? + static final int JINGLE_HEADER_SIZE = 64; // when relay framing is in use + // Default size for receive and send buffer. + static final int DEFAULT_RCV_BUF_SIZE = 60 * 1024; + static final int DEFAULT_SND_BUF_SIZE = 90 * 1024; + ////////////////////////////////////////////////////////////////////// + // Global Constants and Functions + ////////////////////////////////////////////////////////////////////// + // + // 0 1 2 3 + // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // 0 | Conversation Number | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // 4 | Sequence Number | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // 8 | Acknowledgment Number | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // | | |U|A|P|R|S|F| | + // 12 | Control | |R|C|S|S|Y|I| Window | + // | | |G|K|H|T|N|N| | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // 16 | Timestamp sending | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // 20 | Timestamp receiving | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // 24 | data | + // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ + // + ////////////////////////////////////////////////////////////////////// + static final long MAX_SEQ = 0xFFFFFFFFL; + static final int HEADER_SIZE = 24; + static final int PACKET_OVERHEAD = HEADER_SIZE + UDP_HEADER_SIZE + IP_HEADER_SIZE + JINGLE_HEADER_SIZE; + static final long MIN_RTO = 250; // 250 ms (RFC1122, Sec 4.2.3.1 "fractions of a second") + static final long DEF_RTO = 3000; // 3 seconds (RFC1122, Sec 4.2.3.1) + static final long MAX_RTO = 60000; // 60 seconds + static final long DEF_ACK_DELAY = 100; // 100 milliseconds + static final short FLAG_CTL = 0x02; + static final short FLAG_RST = 0x04; + static final short CTL_CONNECT = 0; + //static final short CTL_REDIRECT = 1; + static final short CTL_EXTRA = 255; + // TCP options. + /** + * End of list + */ + static final short TCP_OPT_EOL = 0; + /** + * No-op + */ + static final short TCP_OPT_NOOP = 1; + /** + * Maximum segment size + */ + static final short TCP_OPT_MSS = 2; + /** + * Window scale factor + */ + static final short TCP_OPT_WND_SCALE = 3; + //static final short FLAG_FIN = 0x01; static final short FLAG_SYN = 0x02; + //static final short FLAG_ACK = 0x10; + static final int CTRL_BOUND = 0x80000000; + /** + * If there are no pending clocks, wake up every 4 seconds + */ + static final long DEFAULT_TIMEOUT = 4000; + /** + * If the connection is closed, once per minute + */ + static final long CLOSED_TIMEOUT = 60 * 1000; // + /** + * Idle ping interval + */ + static final int IDLE_PING = 20 * 1000; // 20 seconds (note: WinXP SP2 firewall udp timeout is 90 seconds) + /** + * Idle timeout(used if keepalive is enabled) + */ + static final int IDLE_TIMEOUT = 90 * 1000; // 90 seconds; + // TCB data + /** + * Tcp state + */ + PseudoTcpState m_state; + /** + * Conversation number + */ + long m_conv; + boolean m_bReadEnable, m_bWriteEnable, m_bOutgoing; + /** + * Last traffic timestamp + */ + long m_lasttraffic; + /** + * List of incoming segments. Segments store info like stream offset and + * control flags. If segment contains any data it is stored in the receive + * buffer. + */ + List m_rlist = new ArrayList(); + /** + * Last receive timestamp + */ + long m_lastrecv; + /** + * Receive buffer length + */ + int m_rbuf_len; + /** + * The sequence number of the next byte of data that is expected from the + * other device + */ + int m_rcv_nxt; + /** + * Receive window size + */ + int m_rcv_wnd; + /** + * Window scale factor + */ + private short m_rwnd_scale; + /** + * The receive buffer + */ + ByteFifoBuffer m_rbuf; + /** + * Outgoing segments list + */ + List m_slist = new ArrayList(); + /** + * Last send timestamp + */ + long m_lastsend; + /** + * The sequence number of the next byte of data to be sent + */ + long m_snd_nxt; + /** + * The sequence number of the first byte of data that has been sent but not + * yet acknowledged + */ + long m_snd_una; + /** + * The send buffer's size + */ + int m_sbuf_len; + /** + * Send window size + */ + private int m_snd_wnd; + /** + * Send window scale factor + */ + private short m_swnd_scale; + /** + * The send buffer + */ + ByteFifoBuffer m_sbuf; + // Maximum segment size, estimated protocol level, largest segment sent + /** + * + */ + long m_mss; + /** + * + */ + long m_largest; + /** + * + */ + long m_mtu_advise; + /** + * + */ + int m_msslevel; + /** + * Retransmit timer + */ + long m_rto_base; + /** + * Timestamp tracking + */ + long m_ts_recent, m_ts_lastack; + /** + * Round-trip calculation + */ + long m_rx_rttvar, m_rx_srtt, m_rx_rto; + /** + * Congestion avoidance, Fast retransmit/recovery, Delayed ACKs + */ + long m_ssthresh, m_cwnd; + short m_dup_acks; + long m_recover; + long m_t_ack; + // Configuration options + /** + * Use nagling + */ + boolean m_use_nagling; + /* + * Acknowledgment delay + */ + long m_ack_delay; + boolean m_support_wnd_scale; + IPseudoTcpNotify m_notify; + EnShutdown m_shutdown; + /** + * Debug name used to identify peers in log messages + */ + String debugName = ""; + + ////////////////////////////////////////////////////////////////////// + // PseudoTcp + ////////////////////////////////////////////////////////////////////// + /** + * + * @param notify {@link IPseudoTcpNotify} implementation + * @param conv the conversation number used by this instance + */ + public PseudoTCPBase(IPseudoTcpNotify notify, long conv) + { + m_notify = notify; + m_shutdown = EnShutdown.SD_NONE; + m_rbuf_len = DEFAULT_RCV_BUF_SIZE; + m_rbuf = new ByteFifoBuffer(m_rbuf_len); + m_sbuf_len = DEFAULT_SND_BUF_SIZE; + m_sbuf = new ByteFifoBuffer(m_sbuf_len); + // Sanity check on buffer sizes (needed for OnTcpWriteable notification logic) + assert m_rbuf_len + MIN_PACKET < m_sbuf_len; + long now = Now(); + + m_state = PseudoTcpState.TCP_LISTEN; + m_conv = conv; + m_rcv_wnd = m_rbuf_len; + m_rwnd_scale = m_swnd_scale = 0; + m_snd_nxt = 0; + m_snd_wnd = 1; + m_snd_una = m_rcv_nxt = 0; + m_bReadEnable = true; + m_bWriteEnable = false; + m_t_ack = 0; + + m_msslevel = 0; + m_largest = 0; + assert MIN_PACKET > PACKET_OVERHEAD; + m_mss = MIN_PACKET - PACKET_OVERHEAD; + m_mtu_advise = MAX_PACKET; + + m_rto_base = 0; + + m_cwnd = 2 * m_mss; + m_ssthresh = m_rbuf_len; + m_lastrecv = m_lastsend = m_lasttraffic = now; + m_bOutgoing = false; + + m_dup_acks = 0; + m_recover = 0; + + m_ts_recent = m_ts_lastack = 0; + + m_rx_rto = DEF_RTO; + m_rx_srtt = m_rx_rttvar = 0; + + m_use_nagling = true; + m_ack_delay = DEF_ACK_DELAY; + m_support_wnd_scale = true; + } + + /** + * Enqueues connect message and starts connection procedure + * + * @throws IOException if the protocol is not in initial state + */ + public void Connect() throws IOException + { + if (m_state != PseudoTcpState.TCP_LISTEN) + { + //m_error = PseudoTcpError.EINVAL; + throw new IOException("Invalid socket state"); + } + + m_state = PseudoTcpState.TCP_SYN_SENT; + logger.log(Level.FINE, "State: TCP_SYN_SENT", ""); + + queueConnectMessage(); + attemptSend(SendFlags.sfNone); + } + + /** + * Set the MTU value + * + * @param mtu + */ + public void NotifyMTU(int mtu) + { + m_mtu_advise = mtu; + if (m_state == PseudoTcpState.TCP_ESTABLISHED) + { + adjustMTU(); + } + } + + /** + * + * @return current timestamp limited to 32 bits + */ + public static long Now() + { + return System.currentTimeMillis() & 0xFFFFFFFFL; + } + + /** + * Evaluate next interval between @link(GetNextClock) calls. + * It is based on current protocol action timeout + * + * @param now current timestamp + * @return next interval + */ + public long GetNextClock(long now) + { + return clock_check(now); + } + + /** + * This method should be called in time intervals retrieved from @link(GetNextClock) + * + * @param now current timestamp + */ + public void NotifyClock(long now) + { + if (logger.isLoggable(Level.FINEST)) + { + logger.log(Level.FINEST, debugName + " update clock " + now); + } + if (m_state == PseudoTcpState.TCP_CLOSED) + { + return; + } + + now = now & 0xFFFFFFFFL; + + // Check if it's time to retransmit a segment + if (m_rto_base > 0 && (TimeDiff(m_rto_base + m_rx_rto, now) <= 0)) + { + assert m_slist.isEmpty() == false; + // retransmit segments + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, "timeout retransmit (rto: " + m_rx_rto + + ")(rto_base: " + m_rto_base + ") (now: " + now + ") (dup_acks: " + + m_dup_acks + ")"); + } + if (!transmit(m_slist.get(0), now)) + { + closedown(new IOException("Connection aborted")); + return; + } + + long nInFlight = m_snd_nxt - m_snd_una; + m_ssthresh = Math.max(nInFlight / 2, 2 * m_mss); + //Logger.Log(LS_INFO) << "m_ssthresh: " << m_ssthresh << " nInFlight: " << nInFlight << " m_mss: " << m_mss; + m_cwnd = m_mss; + + // Back off retransmit timer. Note: the limit is lower when connecting. + long rto_limit = (m_state.ordinal() < PseudoTcpState.TCP_ESTABLISHED.ordinal()) + ? DEF_RTO : MAX_RTO; + m_rx_rto = Math.min(rto_limit, m_rx_rto * 2); + m_rto_base = now; + } + + // Check if it's time to probe closed windows + if ((getM_snd_wnd() == 0) && (TimeDiff(m_lastsend + m_rx_rto, now) <= 0)) + { + if (TimeDiff(now, m_lastrecv) >= 15000) + { + closedown(new IOException("Connection aborted")); + return; + } + // probe the window + packet(m_snd_nxt - 1, (short) 0, 0, 0); + m_lastsend = now; + + // back off retransmit timer + m_rx_rto = Math.min(MAX_RTO, m_rx_rto * 2); + } + + // Check if it's time to send delayed acks + long timeDiff = TimeDiff(m_t_ack + m_ack_delay, now); + if (m_t_ack > 0 && (timeDiff <= 0)) + { + packet(m_snd_nxt, (short) 0, 0, 0); + } + + if (PSEUDO_KEEPALIVE) // Check for idle timeout + { + if ((m_state == PseudoTcpState.TCP_ESTABLISHED) + && (TimeDiff(m_lastrecv + IDLE_TIMEOUT, now) <= 0)) + { + closedown(new IOException("Connection aborted")); + return; + } + + // Check for ping timeout (to keep udp mapping open) + if ((m_state == PseudoTcpState.TCP_ESTABLISHED) + && (TimeDiff(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3 / 2 : IDLE_PING), now) <= 0)) + { + packet(m_snd_nxt, (short) 0, 0, 0); + } + + } + } + + /** + * Use this method to notify protocol about packets received from the + * network + * + * @param buffer packet's data + * @param len data length + * @return true if packet was successfully parsed + */ + synchronized public boolean NotifyPacket(byte[] buffer, int len) + { + if (len > MAX_PACKET) + { + logger.log(Level.WARNING, debugName + " packet too large"); + return false; + } + return parse(buffer, len); + } + + /** + * Retrieve option's value. See {@link Option} for available options + * + * @param opt option which value will be retrieved + * @return + */ + long GetOption(Option opt) + { + if (opt == Option.OPT_NODELAY) + { + return m_use_nagling ? 0 : 1; + } + else + { + if (opt == Option.OPT_ACKDELAY) + { + return m_ack_delay; + } + else + { + if (opt == Option.OPT_SNDBUF) + { + return m_sbuf_len; + } + else + { + assert opt == Option.OPT_RCVBUF; + return m_rbuf_len; + } + } + } + } + + /** + * Sets {@link Option} value + * + * @param opt option whose value will be set + * @param value the value to be set + */ + void SetOption(Option opt, int value) + { + if (opt == Option.OPT_NODELAY) + { + m_use_nagling = value == 0; + } + else + { + if (opt == Option.OPT_ACKDELAY) + { + m_ack_delay = value; + } + else + { + if (opt == Option.OPT_SNDBUF) + { + assert m_state == PseudoTcpState.TCP_LISTEN; + resizeSendBuffer(value); + } + else + { + assert opt == Option.OPT_RCVBUF; + assert m_state == PseudoTcpState.TCP_LISTEN; + resizeReceiveBuffer(value); + } + } + } + } + + /** + * + * @return congestion window size + */ + long GetCongestionWindow() + { + return m_cwnd; + } + + /** + * + * @return bytes in flight + */ + long GetBytesInFlight() + { + return m_snd_nxt - m_snd_una; + } + + /** + * + * @return bytes buffered, but not sent yet + */ + long GetBytesBufferedNotSent() + { + long buffered_bytes = m_sbuf.GetBuffered(); + return m_snd_una + buffered_bytes - m_snd_nxt; + } + + /** + * + * @return bytes available in receive buffer + */ + int GetAvailable() + { + return m_rbuf.GetBuffered(); + } + + /** + * + * @return space available in the send buffer + */ + int GetAvailableSendBuffer() + { + return m_sbuf.GetWriteRemaining(); + } + + /** + * + * @return round trip time estimate in ms + */ + long GetRoundTripTimeEstimateMs() + { + return m_rx_srtt; + } + + /** + * Reads the data available in receive buffer. This method returns 0 if + * there's no data available at the moment. + * + * @param buffer destination buffer + * @param offset destination buffer's offset + * @param len bytes to be read + * @return byte count actually read + * @throws IOException if the protocol is not in the connected state + */ + public synchronized int Recv(byte[] buffer, int offset, int len) throws IOException + { + if (m_state != PseudoTcpState.TCP_ESTABLISHED) + { + throw new IOException("Socket not connected"); + } + + int read = m_rbuf.Read(buffer, len); + + // If there's no data in |m_rbuf|. + if (read == 0) + { + m_bReadEnable = true; + return 0; + } + assert read != -1; + + int available_space = m_rbuf.GetWriteRemaining(); + if (available_space - m_rcv_wnd >= Math.min(m_rbuf_len / 8, m_mss)) + { + boolean bWasClosed = (m_rcv_wnd == 0); // !?! Not sure about this was closed business + m_rcv_wnd = available_space; + + if (bWasClosed) + { + attemptSend(SendFlags.sfImmediateAck); + } + } + return read; + } + + /** + * + * @param buffer + * @param len + * @return + * @throws IOException + */ + public int Recv(byte[] buffer, int len) throws IOException + { + return Recv(buffer, 0, len); + } + + /** + * + * @param buffer + * @param len + * @return + * @throws IOException + */ + public int Send(byte[] buffer, int len) throws IOException + { + return Send(buffer, 0, len); + } + + /** + * Enqueues data in the send buffer + * + * @param buffer source data buffer + * @param offset offset of the source data buffer + * @param len bytes count to be sent + * @return bytes count written to the send buffer + * @throws IOException if the protocol is not in connected state + */ + public synchronized int Send(byte[] buffer, int offset, int len) + throws IOException + { + if (m_state != PseudoTcpState.TCP_ESTABLISHED) + { + throw new IOException("Socket not connected"); + } + + long available_space; + available_space = m_sbuf.GetWriteRemaining(); + + if (available_space == 0) + { + m_bWriteEnable = true; + return 0; + } + + int written = queue(buffer, offset, len, false); + attemptSend(SendFlags.sfNone); + return written; + } + + /** + * Shuts down the protocol which enters closed state + * + * @param force if true all data received from this moment will be discarded + */ + public void Close(boolean force) + { + logger.log(Level.FINE, debugName + " close (" + force + ")"); + m_shutdown = force ? EnShutdown.SD_FORCEFUL : EnShutdown.SD_GRACEFUL; + if (force) + { + m_state = PseudoTcpState.TCP_CLOSED; + } + } + +// +// Internal Implementation +// + /** + * Enqueues data segment in the send buffer + * + * @param buffer source buffer + * @param offset source buffer's offset + * @param len data length + * @param bCtrl true for control data + * @return written byte count + */ + int queue(byte[] buffer, int offset, int len, boolean bCtrl) + { + int available_space; + available_space = m_sbuf.GetWriteRemaining(); + if (len > available_space) + { + assert !bCtrl; + len = available_space; + } + + // We can concatenate data if the last segment is the same type + // (control v. regular data), and has not been transmitted yet + SSegment back = null; + if (!m_slist.isEmpty()) + { + back = m_slist.get(m_slist.size() - 1); + } + if (back != null && (back.bCtrl == bCtrl) && (back.xmit == 0)) + { + back.len += len; + } + else + { + long snd_buffered; + snd_buffered = m_sbuf.GetBuffered(); + SSegment sseg = new SSegment( + m_snd_una + snd_buffered, + len, + bCtrl); + //m_slist.push_back(sseg); + if (logger.isLoggable(Level.FINEST)) + { + logger.log(Level.FINEST, debugName + " enqueued send segment seq: " + + sseg.seq + " len: " + sseg.len); + } + m_slist.add(sseg); + } + + int written = m_sbuf.Write(buffer, len); + return written; + } + + /** + * Creates a packet starting at offset in the send buffer of + * specified length and sends it with help of @link(IPseudoTcpNotify). + * + * @param seq used sequence number + * @param flags + * @param offset in the send buffer + * @param len length of data from + * @return @link(WriteResult) returned by @link(IPseudoTcpNotify) + */ + WriteResult packet(long seq, short flags, long offset, long len) + { + assert HEADER_SIZE + len <= MAX_PACKET; + + long now = Now(); + + byte[] buffer = new byte[MAX_PACKET]; + long_to_bytes(m_conv, buffer, 0); + long_to_bytes(seq, buffer, 4); + long_to_bytes(m_rcv_nxt, buffer, 8); + buffer[12] = 0; + buffer[13] = (byte) (flags & 0xFF); + short_to_bytes(m_rcv_wnd >> m_rwnd_scale, buffer, 14); + + // Timestamp computations + long_to_bytes(now, buffer, 16); + long_to_bytes(m_ts_recent, buffer, 20); + m_ts_lastack = m_rcv_nxt; + + if (len > 0) + { + int bytes_read = m_sbuf.ReadOffset(buffer, HEADER_SIZE, + (int) len, + (int) offset); + assert bytes_read == len; + } + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, "<-- " + debugName + " " + + ""); + } + WriteResult wres = m_notify.TcpWritePacket(this, + buffer, + (int) len + HEADER_SIZE); + /** + * Note: When len is 0, this is an ACK packet. We don't read the return + * value for those, and thus we won't retry. So go ahead and treat the + * packet as a success (basically simulate as if it were dropped), which + * will prevent our timers from being messed up. + */ + if ((wres != WriteResult.WR_SUCCESS) && (0 != len)) + { + return wres; + } + m_t_ack = 0; + if (len > 0) + { + m_lastsend = now; + } + m_lasttraffic = now; + m_bOutgoing = true; + + return WriteResult.WR_SUCCESS; + } + + /** + * Creates new segment from the data in buffer which is processed + * by the protocol. + * + * @param buffer source buffer + * @param size data length + * @return true if successfully parsed the data + */ + boolean parse(byte[] buffer, int size) + { + if (size < 12) + { + return false; + } + + Segment seg = new Segment(); + seg.conv = bytes_to_long(buffer, 0); + seg.seq = bytes_to_long(buffer, 4); + seg.ack = bytes_to_long(buffer, 8); + seg.flags = buffer[13]; + seg.wnd = bytes_to_short(buffer, 14); + + seg.tsval = bytes_to_long(buffer, 16); + seg.tsecr = bytes_to_long(buffer, 20); + + seg.data = copy_buffer(buffer, HEADER_SIZE, size - HEADER_SIZE); + seg.len = size - HEADER_SIZE; + + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + "--> " + debugName + ""); + } + return process(seg); + } + + /** + * Calculates timeout in ms for current operation + * + * @param now current timestamp in ms + * @return next timeout or -1 in case of an error + */ + long clock_check(long now) + { + if (m_shutdown == EnShutdown.SD_FORCEFUL) + { + return -1; + } + + long nTimeout; + long snd_buffered; + snd_buffered = m_sbuf.GetBuffered(); + if ((m_shutdown == EnShutdown.SD_GRACEFUL) + && ((m_state != PseudoTcpState.TCP_ESTABLISHED) + || ((snd_buffered == 0) && (m_t_ack == 0)))) + { + return -1; + } + + if (m_state == PseudoTcpState.TCP_CLOSED) + { + return CLOSED_TIMEOUT; + } + + nTimeout = DEFAULT_TIMEOUT; + + if (m_t_ack > 0) + { + nTimeout = Math.min(nTimeout, TimeDiff(m_t_ack + m_ack_delay, now)); + } + if (m_rto_base > 0) + { + nTimeout = Math.min(nTimeout, TimeDiff(m_rto_base + m_rx_rto, now)); + } + if (getM_snd_wnd() == 0) + { + nTimeout = Math.min(nTimeout, TimeDiff(m_lastsend + m_rx_rto, now)); + } + if (PSEUDO_KEEPALIVE) + { + if (m_state == PseudoTcpState.TCP_ESTABLISHED) + { + nTimeout = Math.min( + nTimeout, + TimeDiff(m_lasttraffic + (m_bOutgoing ? IDLE_PING * 3 / 2 : IDLE_PING), now)); + } + } + //nTimeout is used on wait methods, so cannot be equal to 0 + return nTimeout <= 0 ? 1 : nTimeout; + } + + /** + * Process given segment + * + * @param seg + * @return false in case of error + */ + boolean process(Segment seg) + { + // If this is the wrong conversation, send a reset!?! (with the correct conversation?) + if (seg.conv != m_conv) + { + //if ((seg.flags & FLAG_RST) == 0) { + // packet(tcb, seg.ack, 0, FLAG_RST, 0, 0); + //} + closedown(new IOException( + debugName + " wrong conversation number, this: " + m_conv + + " remote: " + seg.conv)); + return false; + } + + long now = Now(); + m_lasttraffic = m_lastrecv = now; + m_bOutgoing = false; + + if (m_state == PseudoTcpState.TCP_CLOSED) + { + // !?! send reset? + closedown(new IOException(debugName + " in closed state")); + return false; + } + + // Check if this is a reset segment + if ((seg.flags & FLAG_RST) > 0) + { + //closedown(PseudoTcpError.ECONNRESET); + closedown(new IOException("Connection reset")); + return false; + } + + // Check for control data + boolean bConnect = false; + if ((seg.flags & FLAG_CTL) > 0) + { + if (seg.len == 0) + { + logger.log(Level.SEVERE, debugName + " Missing control code"); + return false; + } + else + { + if (seg.data[0] == CTL_CONNECT) + { + bConnect = true; + + // TCP options are in the remainder of the payload after CTL_CONNECT. + parseOptions(seg.data, 1, seg.len - 1); + + if (m_state == PseudoTcpState.TCP_LISTEN) + { + m_state = PseudoTcpState.TCP_SYN_RECEIVED; + logger.log(Level.FINE, + debugName + " State: TCP_SYN_RECEIVED"); + //m_notify->associate(addr); + queueConnectMessage(); + } + else + { + if (m_state == PseudoTcpState.TCP_SYN_SENT) + { + m_state = PseudoTcpState.TCP_ESTABLISHED; + logger.log(Level.FINE, + debugName + " State: TCP_ESTABLISHED"); + adjustMTU(); + if (m_notify != null) + { + m_notify.OnTcpOpen(this); + } + //notify(evOpen); + } + } + } + else + { + logger.log(Level.SEVERE, + debugName + " Unknown control code: " + seg.data[0]); + return false; + } + } + } + + // Update timestamp + if ((seg.seq <= m_ts_lastack) && (m_ts_lastack < seg.seq + seg.len)) + { + m_ts_recent = seg.tsval; + } + + // Check if this is a valuable ack + if ((seg.ack > m_snd_una) && (seg.ack <= m_snd_nxt)) + { + // Calculate round-trip time + if (seg.tsecr > 0) + { + long rtt = TimeDiff(now, seg.tsecr); + assert rtt >= 0; + if (m_rx_srtt == 0) + { + m_rx_srtt = rtt; + m_rx_rttvar = rtt / 2; + } + else + { + m_rx_rttvar = (3 * m_rx_rttvar + Math.abs(rtt - m_rx_srtt)) / 4; + m_rx_srtt = (7 * m_rx_srtt + rtt) / 8; + } + m_rx_rto = bound(MIN_RTO, m_rx_srtt + + Math.max(1, 4 * m_rx_rttvar), + MAX_RTO); + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, + "rtt: " + rtt + " srtt: " + m_rx_srtt + " rto: " + m_rx_rto); + } + } + + m_snd_wnd = seg.wnd << m_swnd_scale; + //setWindowWithScale(seg.wnd, getM_swnd_scale()); + //setM_snd_wnd(seg.wnd << m_swnd_scale); + + long nAcked = seg.ack - m_snd_una; + m_snd_una = seg.ack; + + m_rto_base = (m_snd_una == m_snd_nxt) ? 0 : now; + + m_sbuf.ConsumeReadData((int) nAcked); + synchronized (ack_notify) + { + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, + debugName + " acked: " + nAcked + + " m_snd_una: " + m_snd_una); + } + ack_notify.notifyAll(); + } + + for (long nFree = nAcked; nFree > 0;) + { + assert !m_slist.isEmpty(); + if (nFree < m_slist.get(0).len) + { + m_slist.get(0).len -= nFree; + nFree = 0; + } + else + { + if (m_slist.get(0).len > m_largest) + { + m_largest = m_slist.get(0).len; + } + nFree -= m_slist.get(0).len; + m_slist.remove(0); + //m_slist.pop_front(); + } + } + + if (m_dup_acks >= 3) + { + if (m_snd_una >= m_recover) + { // NewReno + long nInFlight = m_snd_nxt - m_snd_una; + m_cwnd = Math.min(m_ssthresh, nInFlight + m_mss); // (Fast Retransmit) + logger.log(Level.FINE, "exit recovery"); + m_dup_acks = 0; + } + else + { + logger.log(Level.FINE, "recovery retransmit"); + if (!transmit(m_slist.get(0), now)) + { + //closedown(PseudoTcpError.ECONNABORTED); + closedown(new IOException("Connection aborted")); + return false; + } + m_cwnd += m_mss - Math.min(nAcked, m_cwnd); + } + } + else + { + m_dup_acks = 0; + // Slow start, congestion avoidance + if (m_cwnd < m_ssthresh) + { + m_cwnd += m_mss; + } + else + { + m_cwnd += Math.max(1, m_mss * m_mss / m_cwnd); + } + } + } + else + { + if (seg.ack == m_snd_una) + { + // !?! Note, tcp says don't do this... but otherwise how does a closed window become open? + //setWindowWithScale(seg.wnd, getM_swnd_scale()); + m_snd_wnd = seg.wnd << m_swnd_scale; + //setM_snd_wnd(seg.wnd << m_swnd_scale); + + // Check duplicate acks + if (seg.len > 0) + { + // it's a dup ack, but with a data payload, so don't modify m_dup_acks + } + else + { + if (m_snd_una != m_snd_nxt) + { + m_dup_acks += 1; + if (m_dup_acks == 3) + { // (Fast Retransmit) + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + debugName + " enter recovery"); + logger.log(Level.FINE, + debugName + " recovery retransmit"); + } + if (!transmit(m_slist.get(0), now)) + { + closedown(new IOException("Connection aborted")); + //closedown(PseudoTcpError.ECONNABORTED); + return false; + } + m_recover = m_snd_nxt; + long nInFlight = m_snd_nxt - m_snd_una; + m_ssthresh = Math.max(nInFlight / 2, 2 * m_mss); + //Logger.Log(LS_INFO) << "m_ssthresh: " << m_ssthresh << " nInFlight: " << nInFlight << " m_mss: " << m_mss; + m_cwnd = m_ssthresh + 3 * m_mss; + } + else + { + if (m_dup_acks > 3) + { + m_cwnd += m_mss; + } + } + } + else + { + m_dup_acks = 0; + } + } + } + } + // !?! A bit hacky + if ((m_state == PseudoTcpState.TCP_SYN_RECEIVED) && !bConnect) + { + m_state = PseudoTcpState.TCP_ESTABLISHED; + logger.log(Level.FINE, debugName + " State: TCP_ESTABLISHED"); + adjustMTU(); + if (m_notify != null) + { + m_notify.OnTcpOpen(this); + } + //notify(evOpen); + } + // If we make room in the send queue, notify the user + // The goal it to make sure we always have at least enough data to fill the + // window. We'd like to notify the app when we are halfway to that point. + long kIdealRefillSize = (m_sbuf_len + m_rbuf_len) / 2; + long snd_buffered = m_sbuf.GetBuffered(); + if (m_bWriteEnable && snd_buffered < kIdealRefillSize) + { + m_bWriteEnable = false; + if (m_notify != null) + { + m_notify.OnTcpWriteable(this); + } + //notify(evWrite); + } + // Conditions were acks must be sent: + // 1) Segment is too old (they missed an ACK) (immediately) + // 2) Segment is too new (we missed a segment) (immediately) + // 3) Segment has data (so we need to ACK!) (delayed) + // ... so the only time we don't need to ACK, is an empty segment that points to rcv_nxt! + SendFlags sflags = SendFlags.sfNone; + if (seg.seq != m_rcv_nxt) + { + sflags = SendFlags.sfImmediateAck; // (Fast Recovery) + } + else + { + if (seg.len + != 0) + { + if (m_ack_delay == 0) + { + sflags = SendFlags.sfImmediateAck; + } + else + { + sflags = SendFlags.sfDelayedAck; + } + } + } + + if (sflags == SendFlags.sfImmediateAck) + { + if (seg.seq > m_rcv_nxt) + { + logger.log(Level.FINER, "too new"); + } + else + { + if (seg.seq + seg.len <= m_rcv_nxt) + { + logger.log(Level.FINER, "too old"); + } + } + } + + // Adjust the incoming segment to fit our receive buffer + if (seg.seq < m_rcv_nxt) + { + long nAdjust = m_rcv_nxt - seg.seq; + if (nAdjust < seg.len) + { + seg.seq += nAdjust; + seg.data = ScrollBuffer(seg.data, nAdjust); + seg.len -= nAdjust; + } + else + { + seg.len = 0; + } + } + long available_space = m_rbuf.GetWriteRemaining(); + if ((seg.seq + seg.len - m_rcv_nxt) > available_space) + { + long nAdjust = seg.seq + seg.len - m_rcv_nxt - available_space; + if (nAdjust < seg.len) + { + seg.len -= nAdjust; + } + else + { + seg.len = 0; + } + } + boolean bIgnoreData = ((seg.flags & FLAG_CTL) > 0) || (m_shutdown != EnShutdown.SD_NONE); + boolean bNewData = false; + if (seg.len > 0) + { + if (bIgnoreData) + { + if (seg.seq == m_rcv_nxt) + { + m_rcv_nxt += seg.len; + } + } + else + { + long nOffset = seg.seq - m_rcv_nxt; + + int result = m_rbuf.WriteOffset(seg.data, seg.len, + (int) nOffset); + assert result == seg.len; + + if (seg.seq == m_rcv_nxt) + { + if (logger.isLoggable(Level.FINEST)) + { + logger.log(Level.FINEST, + "Avail space: " + available_space + + " seg.len: " + seg.len); + } + m_rbuf.ConsumeWriteBuffer(seg.len); + m_rcv_nxt += seg.len; + m_rcv_wnd -= seg.len; + bNewData = true; + + + Iterator iter = m_rlist.iterator(); + List toBeRemoved = new ArrayList(); + while (iter.hasNext()) + { + RSegment it = iter.next(); + if (it.seq > m_rcv_nxt) + { + break; + } + if (it.seq + it.len > m_rcv_nxt) + { + sflags = SendFlags.sfImmediateAck; // (Fast Recovery) + long nAdjust = (it.seq + it.len) - m_rcv_nxt; + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + "Recovered " + nAdjust + " bytes (" + + m_rcv_nxt + " -> " + (m_rcv_nxt + nAdjust) + + ")"); + } + m_rbuf.ConsumeWriteBuffer((int) nAdjust); + m_rcv_nxt += nAdjust; + m_rcv_wnd -= nAdjust; + } + toBeRemoved.add(it); + } + m_rlist.removeAll(toBeRemoved); + } + else + { + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + "Saving " + seg.len + " bytes (" + seg.seq + + " -> " + (seg.seq + seg.len) + ")"); + } + RSegment rseg = new RSegment(seg.seq, seg.len); + int insertPos; + for (insertPos = 0; insertPos < m_rlist.size(); insertPos++) + { + RSegment it = m_rlist.get(insertPos); + if (it.seq >= rseg.seq) + { + break; + } + } + m_rlist.add(insertPos, rseg); + } + } + } + + attemptSend(sflags); + // If we have new data, notify the user + if (bNewData && m_bReadEnable) + { + m_bReadEnable = false; + if (m_notify != null) + { + m_notify.OnTcpReadable(this); + } + //notify(evRead); + } + return true; + } + + /** + * Util time method + * + * @param later timestamp in ms + * @param earlier timestamp in ms + * @return difference between later and earlier + */ + private static long TimeDiff(long later, long earlier) + { + return later - earlier; + } + + /** + * Stores 32 bit unsigned int in a buffer at specified offset + * + * @param anUnsignedInt + * @param buf destination buffer + * @param offset destination buffer's offset + */ + private static void long_to_bytes(long anUnsignedInt, byte[] buf, int offset) + { + buf[offset] = (byte) ((anUnsignedInt & 0xFF000000L) >>> 24); + buf[offset + 1] = (byte) ((anUnsignedInt & 0x00FF0000L) >>> 16); + buf[offset + 2] = (byte) ((anUnsignedInt & 0x0000FF00L) >>> 8); + buf[offset + 3] = (byte) ((anUnsignedInt & 0x000000FFL)); + //java.nio.ByteBuffer.wrap(buffer, offset, 4).putInt((int) (m_conv & 0xFFFFFFFFL)); + } + + /** + * Stores 16 bit unsigned int in the buffer at specified offset + * + * @param anUnsignedShort + * @param buf destination buffer + * @param offset destination buffer's offset + */ + private static void short_to_bytes(int anUnsignedShort, byte[] buf, int offset) + { + buf[offset] = (byte) ((anUnsignedShort & 0xFF00) >>> 8); + buf[offset + 1] = (byte) ((anUnsignedShort & 0x00FF)); + //java.nio.ByteBuffer.wrap(buffer, offset, 2).putShort((short) (shrt & 0xFFFF)); + } + + /** + * Reads 32 bit unsigned int from the buffer at specified offset + * + * @param buffer + * @param offset + * @return 32 bit unsigned value + */ + private static long bytes_to_long(byte[] buffer, int offset) + { + int fByte = (0x000000FF & ((int) buffer[offset])); + int sByte = (0x000000FF & ((int) buffer[offset + 1])); + int tByte = (0x000000FF & ((int) buffer[offset + 2])); + int foByte = (0x000000FF & ((int) buffer[offset + 3])); + return ((long) (fByte << 24 + | sByte << 16 + | tByte << 8 + | foByte)) + & 0xFFFFFFFFL; + } + + /** + * Reads 16 bit unsigned int from the buffer at specified offset + * + * @param buffer + * @param offset + * @return 16 bit unsigned int + */ + private static int bytes_to_short(byte[] buffer, int offset) + { + int fByte = (0x000000FF & ((int) buffer[offset])); + int sByte = (0x000000FF & ((int) buffer[offset + 1])); + return ((fByte << 8 + | sByte)) + & 0xFFFF; + } + + /** + * Wrapped system function arrayCopy + * + * @param buffer source buffer + * @param sOffset source buffer offset + * @param len bytes count to be copied + * @return new buffer size of len + */ + private static byte[] copy_buffer(byte[] buffer, int sOffset, int len) + { + byte[] newData = new byte[len]; + System.arraycopy(buffer, sOffset, newData, 0, len); + return newData; + } + + /** + * + * @param lower + * @param middle + * @param upper + * @return + */ + private long bound(long lower, long middle, long upper) + { + return Math.min(Math.max(lower, middle), upper); + } + + private byte[] ScrollBuffer(byte[] data, long nAdjust) + { + //TODO: never been hit so far, to be implemented + throw new UnsupportedOperationException("Not yet implemented"); + } + + /** + * Transmits given segment + * + * @param seg segment to be sent + * @param now current timestamp + * @return false in case of error + */ + boolean transmit(SSegment seg, long now) + { + // Logger.Log(LS_INFO) << "seg->xmit: "<< seg->xmit; + if (seg.xmit >= ((m_state == PseudoTcpState.TCP_ESTABLISHED) ? 15 : 30)) + { + logger.log(Level.FINE, "too many retransmits"); + return false; + } + + long nTransmit = Math.min(seg.len, m_mss); + + while (true) + { + long seq = seg.seq; + short flags = (seg.bCtrl ? FLAG_CTL : 0); + WriteResult wres = packet(seq, + flags, + seg.seq - m_snd_una, + nTransmit); + + if (wres == WriteResult.WR_SUCCESS) + { + break; + } + + if (wres == WriteResult.WR_FAIL) + { + logger.log(Level.WARNING, "packet failed"); + return false; + } + + assert wres == WriteResult.WR_TOO_LARGE; + + while (true) + { + if (PACKET_MAXIMUMS[(m_msslevel + 1)] == 0) + { + logger.log(Level.INFO, "MTU too small"); + return false; + } + // !?! We need to break up all outstanding and pending packets and then retransmit!?! + + m_mss = PACKET_MAXIMUMS[++m_msslevel] - PACKET_OVERHEAD; + m_cwnd = 2 * m_mss; // I added this... haven't researched actual formula + if (m_mss < nTransmit) + { + nTransmit = m_mss; + break; + } + } + if (logger.isLoggable(Level.INFO)) + { + logger.log(Level.INFO, "Adjusting mss to " + m_mss + " bytes"); + } + } + + if (nTransmit < seg.len) + { + if (logger.isLoggable(Level.INFO)) + { + logger.log(Level.INFO, "mss reduced to " + m_mss); + } + SSegment subseg = new SSegment(seg.seq + nTransmit, + seg.len - nTransmit, seg.bCtrl); + //subseg.tstamp = seg->tstamp; + subseg.xmit = seg.xmit; + seg.len = nTransmit; + + //SList::iterator next = seg; + m_slist.add(m_slist.indexOf(seg) + 1, subseg); + } + + if (seg.xmit == 0) + { + m_snd_nxt += seg.len; + } + seg.xmit += 1; + //seg->tstamp = now; + if (m_rto_base == 0) + { + m_rto_base = now; + } + + return true; + } + + /** + * This method checks if it's time to send a packet(ack or retransmit + * anything) + * + * @param sflags + */ + void attemptSend(SendFlags sflags) + { + long now = Now(); + + if (TimeDiff(now, m_lastsend) > m_rx_rto) + { + m_cwnd = m_mss; + } + boolean bFirst = true; + + while (true) + { + long cwnd = m_cwnd; + if ((m_dup_acks == 1) || (m_dup_acks == 2)) + { // Limited Transmit + cwnd += m_dup_acks * m_mss; + } + long nWindow = Math.min(getM_snd_wnd(), cwnd); + long nInFlight = m_snd_nxt - m_snd_una; + long nUseable = (nInFlight < nWindow) ? (nWindow - nInFlight) : 0; + + long snd_buffered = m_sbuf.GetBuffered(); + /* + * System.out.println("is available? buffered: " + snd_buffered + " + * inFlight: " + nInFlight + " m_mss: " + m_mss + " m_snd_wnd: " + + * getM_snd_wnd() + " cwnd: " + cwnd + " nWindow: " + nWindow + " + * nUseable: " + nUseable); + */ + long nAvailable = Math.min(snd_buffered - nInFlight, m_mss); + + if (nAvailable > nUseable) + { + if (nUseable * 4 < nWindow) + { + // RFC 813 - avoid SWS + logger.log(Level.FINER, + "RFC 813 - avoid SWS(nAvailable = 0)"); + nAvailable = 0; + } + else + { + nAvailable = nUseable; + } + } + + if (bFirst) + { + long available_space = m_sbuf.GetWriteRemaining(); + + bFirst = false; + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + "[cwnd: " + m_cwnd + " nWindow: " + nWindow + + " nInFlight: " + nInFlight + " nAvailable: " + nAvailable + + " nQueued: " + snd_buffered + " nEmpty: " + available_space + + " ssthresh: " + m_ssthresh + "]"); + } + } + + if (nAvailable == 0) + { + if (sflags == SendFlags.sfNone) + { + logger.log(Level.FINEST, "nAvailable == 0: quit"); + return; + } + + // If this is an immediate ack, or the second delayed ack + if ((sflags == SendFlags.sfImmediateAck) || (m_t_ack > 0)) + { + packet(m_snd_nxt, (short) 0, 0, 0); + logger.log(Level.FINER, "Immediate ack: "); + } + else + { + m_t_ack = Now(); + logger.log(Level.FINER, "Delayed ack, m_t_ack: " + m_t_ack); + } + return; + } + + // Nagle's algorithm. + // If there is data already in-flight, and we haven't a full segment of + // data ready to send then hold off until we get more to send, or the + // in-flight data is acknowledged. + if (m_use_nagling && (m_snd_nxt > m_snd_una) && (nAvailable < m_mss)) + { + logger.log(Level.FINER, "wait untill more data is acked"); + return; + } + + // Find the next segment to transmit + SSegment seg = null; + Iterator iter = m_slist.iterator(); + do + { + SSegment it = iter.next(); + if (it.xmit == 0) + { + seg = it; + break; + } + } + while (iter.hasNext()); + + assert seg != null; + + // If the segment is too large, break it into two + if (seg.len > nAvailable) + { + logger.log(Level.FINEST, "Break a segment into 2"); + SSegment subseg = new SSegment( + seg.seq + nAvailable, + seg.len - nAvailable, + seg.bCtrl); + seg.len = nAvailable; + //m_slist.insert(++it, subseg); + m_slist.add(m_slist.indexOf(seg) + 1, subseg); + } + if (logger.isLoggable(Level.FINEST)) + { + logger.log(Level.FINEST, + "TRANSMIT SEGMENT seq: " + seg.seq + + " len: " + seg.len); + } + if (!transmit(seg, now)) + { + logger.log(Level.SEVERE, "transmit failed"); + // TODO: consider closing socket + return; + } + + sflags = SendFlags.sfNone; + } + } + + /** + * This metod is called in case of en error. Tcp enters closed state and + * notifies listener about it. + * + * @param e exception to be propagated + */ + void closedown(IOException e) + { + logger.log(Level.FINE, debugName + " State: TCP_CLOSED "); + m_state = PseudoTcpState.TCP_CLOSED; + if (m_notify != null) + { + m_notify.OnTcpClosed(this, e); + } + } + + /** + * Adjusts MTU + */ + void adjustMTU() + { + // Determine our current mss level, so that we can adjust appropriately later + for (m_msslevel = 0; PACKET_MAXIMUMS[(m_msslevel + 1)] > 0; ++m_msslevel) + { + if (PACKET_MAXIMUMS[m_msslevel] <= m_mtu_advise) + { + break; + } + } + m_mss = m_mtu_advise - PACKET_OVERHEAD; + // !?! Should we reset m_largest here? + logger.log(Level.INFO, "Adjusting mss to " + m_mss + " bytes"); + + // Enforce minimums on ssthresh and cwnd + m_ssthresh = Math.max(m_ssthresh, 2 * m_mss); + m_cwnd = Math.max(m_cwnd, m_mss); + } + + /** + * + * @return true if receive buffer is full + */ + boolean isReceiveBufferFull() + { + return m_rbuf.GetWriteRemaining() == 0; + } + + /** + * Disables window scaling. Must be called before the connection is + * established. + */ + void disableWindowScale() + { + m_support_wnd_scale = false; + } + + /** + * Enqueues connect message + */ + void queueConnectMessage() + { + byte[] buff = new byte[4]; + buff[0] = CTL_CONNECT & 0xFF; + if (m_support_wnd_scale) + { + buff[1] = TCP_OPT_WND_SCALE & 0xFF; + buff[2] = 1; + buff[3] = (byte) (m_rwnd_scale & 0xFF); + } + m_snd_wnd = buff.length; + queue(buff, 0, buff.length, true); + } + + /** + * Parse and process option in given buffer/offset/length + * + * @param data source buffer + * @param offset source offset + * @param len byte count + */ + void parseOptions(byte[] data, int offset, int len) + { + List options_specified = new ArrayList(); + + // See http://www.freesoft.org/CIE/Course/Section4/8.htm for + // parsing the options list. + java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(data, offset, len); + while (buf.hasRemaining()) + { + short kind = TCP_OPT_EOL; + short tmp = buf.get(); + if (tmp != -1) + { + kind = tmp; + } + if (kind == TCP_OPT_EOL) + { + // End of option list. + break; + } + else + { + if (kind == TCP_OPT_NOOP) + { + // No op. + continue; + } + } + + // Length of this option. + assert len != 0; + //UNUSED(len); + short opt_len = buf.get(); + + // Content of this option. + if (opt_len <= buf.remaining()) + { + byte[] opt_data = new byte[opt_len]; + buf.get(opt_data); + applyOption(kind, opt_data, opt_len); + } + else + { + logger.log(Level.SEVERE, "Invalid option length received."); + return; + } + options_specified.add(kind); + } + + if (options_specified.indexOf(TCP_OPT_WND_SCALE) == -1)//options_specified.size() - 1) + { + logger.log(Level.WARNING, "Peer doesn't support window scaling"); + if (getM_rwnd_scale() > 0) + { + // Peer doesn't support TCP options and window scaling. + // Revert receive buffer size to default value. + resizeReceiveBuffer(DEFAULT_RCV_BUF_SIZE); + m_swnd_scale = 0; + } + } + } + + /** + * Applies kind of option and it's data + * + * @param kind option type + * @param data option's data buffer + * @param len data length + */ + void applyOption(short kind, byte[] data, long len) + { + if (kind == TCP_OPT_MSS) + { + logger.log( + Level.WARNING, + "Peer specified MSS option which is not supported."); + // TODO: Implement. + } + else + { + if (kind == TCP_OPT_WND_SCALE) + { + // Window scale factor. + // http://www.ietf.org/rfc/rfc1323.txt + if (len != 1) + { + logger.log(Level.SEVERE, "Invalid window scale option received."); + return; + } + applyWindowScaleOption(data[0]); + } + } + } + + /** + * Applies window scale option with given scale_factor + * + * @param scale_factor + */ + void applyWindowScaleOption(short scale_factor) + { + m_swnd_scale = scale_factor; + } + + /** + * Resizes send buffer to new_size + * + * @param new_size + */ + void resizeSendBuffer(int new_size) + { + m_sbuf_len = new_size; + m_sbuf.SetCapacity(new_size); + } + + /** + * Resizes receive buffer to new_size + * + * @param new_size + */ + void resizeReceiveBuffer(int new_size) + { + short scale_factor = 0; + + // Determine the scale factor such that the scaled window size can fit + // in a 16-bit unsigned integer. + while (new_size > 0xFFFF) + { + ++scale_factor; + new_size >>= 1; + } + + // Determine the proper size of the buffer. + new_size <<= scale_factor; + boolean result = m_rbuf.SetCapacity(new_size); + + // Make sure the new buffer is large enough to contain data in the old + // buffer. This should always be true because this method is called either + // before connection is established or when peers are exchanging connect + // messages. + assert result; + m_rbuf_len = new_size; + m_rwnd_scale = scale_factor; + m_ssthresh = new_size; + + int available_space = m_rbuf.GetWriteRemaining(); + m_rcv_wnd = available_space; + } + + /** + * @return send window size + */ + int getM_snd_wnd() + { + return m_snd_wnd; + } + + /** + * + * @return current @link{PseudoTcpState} + */ + public PseudoTcpState getState() + { + return m_state; + } + + /** + * + * @return send buffer's length + */ + int getSendBufferSize() + { + return m_sbuf_len; + } + + /** + * + * @return receive buffer's length + */ + int getRecvBufferSize() + { + return m_rbuf_len; + } + + /** + * @return the receive window scale + */ + public short getM_rwnd_scale() + { + return m_rwnd_scale; + } + + /** + * @return the send window scale + */ + public short getM_swnd_scale() + { + return m_swnd_scale; + } + private final Object ack_notify = new Object(); + + public Object GetAckNotify() + { + return ack_notify; + } +} + +/** + * Class used internally as a structure for receive segments + * + * @author Pawel + */ +class RSegment +{ + public long seq, len; + + public RSegment(long seq, long len) + { + this.seq = seq; + this.len = len; + } +} + +/** + * Class used internally as a structure for send segments + * + * @author Pawel Domas + */ +class SSegment +{ + long seq, len; + //uint32 tstamp; + short xmit; + boolean bCtrl; + + SSegment(long s, long l, boolean c) + { + seq = s; + len = l; + xmit = 0; + bCtrl = c; + } +} + +/** + * Shutdown enum used internally + * + * @author Pawel Domas + */ +enum EnShutdown +{ + /** + * There was no shutdown + */ + SD_NONE, + /** + * There was a graceful shutdown + */ + SD_GRACEFUL, + /** + * There was a forceful shutdown + */ + SD_FORCEFUL +}; + +/** + * Options used in pseudotcp + * + * @author Pawel Domas + */ +enum Option +{ + /** + * Whether to enable Nagle's algorithm (0 == off) + */ + OPT_NODELAY, + /** + * The Delayed ACK timeout (0 == off). + */ + OPT_ACKDELAY, + /** + * Set the receive buffer size, in bytes. + */ + OPT_RCVBUF, + /** + * Set the send buffer size, in bytes. + */ + OPT_SNDBUF, +}; + +/** + * Send flags used internally + * + * @author Pawel Domas + */ +enum SendFlags +{ + sfNone, sfImmediateAck, sfDelayedAck; +} + +/** + * Class used internally as a segment structure + * + * @author Pawel Domas + */ +class Segment +{ + long conv; + long seq; + long ack; + byte flags; + int wnd; + long tsval; + long tsecr; + byte[] data; + int len; +} \ No newline at end of file diff --git a/src/org/ice4j/pseudotcp/PseudoTcpSocket.java b/src/org/ice4j/pseudotcp/PseudoTcpSocket.java new file mode 100755 index 0000000..8ca9345 --- /dev/null +++ b/src/org/ice4j/pseudotcp/PseudoTcpSocket.java @@ -0,0 +1,788 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.net.*; +import java.util.logging.*; + +public class PseudoTcpSocket implements IPseudoTcpNotify +{ + /** + * The logger. + */ + private static final java.util.logging.Logger logger = + java.util.logging.Logger.getLogger(PseudoTCPBase.class.getName()); + /** + * Pseudotcp logic instance + */ + private final PseudoTCPBase pseudoTcp; + /** + * Datagram socket used to handle network operations + */ + private final DatagramSocket socket; + /** + * Current socket address of remote socket that we are connected to + */ + private SocketAddress remoteAddr; + /** + * Receive buffer size used for receiving packets TODO: this should be + * checked with MTU ? + */ + private int DATAGRAM_RCV_BUFFER_SIZE = 70000; + /** + * Monitor object used to block threads on write operation. That is when the + * send buffer is full. + */ + private final Object write_notify = new Object(); + /** + * Monitor object used to block threads on read operation. That is when + * there's no more data available for reading. + */ + private final Object read_notify = new Object(); + /** + * Monitor object used to block thread waiting for change of TCP state. + */ + private final Object state_notify = new Object(); + /** + * Monitor object used by clock thread. Clock thread sleeps for some + * intervals given by pseudotcp logic, but sometimes it is required to + * finish this sleep earlier. In that case notify method of this monitor is + * called. + */ + private final Object clock_notify = new Object(); + /** + * Exception which occurred in pseudotcp logic and must be propagated to + * threads blocked on any operations. + */ + private IOException exception; + + /** + * + * @param conv_id conversation id, must be the same on both sides + * @param sock datagram socket used for network operations + */ + public PseudoTcpSocket(long conv_id, DatagramSocket sock) + { + pseudoTcp = new PseudoTCPBase(this, conv_id); + this.socket = sock; + } + + /** + * This constructor creates DatagramSocket with random port. Should + * be used for clients. + * + * @param conv_id conversation id, must be the same on both sides + * @throws SocketException + */ + public PseudoTcpSocket(long conv_id) + throws SocketException + { + this(conv_id, new DatagramSocket()); + } + + /** + * Binds DatagramSocket to given local_port + * + * @param conv_id conversation id, must be the same on both sides + * @param local_port + * @throws SocketException + */ + public PseudoTcpSocket(long conv_id, int local_port) + throws SocketException + { + this(conv_id, new DatagramSocket(local_port)); + } + + /** + * Creates DatagramSocket for local_ip:local_port + * + * @param conv_id conversation id, must be the same on both sides + * @param local_ip used by DatagramSocket + * @param local_port used by DatagramSocket + * @throws SocketException + * @throws UnknownHostException + */ + public PseudoTcpSocket(long conv_id, String local_ip, int local_port) + throws SocketException, + UnknownHostException + { + this(conv_id, new DatagramSocket(local_port, + InetAddress.getByName(local_ip))); + } + + /** + * Start connection procedure + * + * @param remoteAddress to which this socket connects to + * @param timeout for this operation in ms + * @throws IOException + */ + public void Connect(InetSocketAddress remoteAddress, long timeout) + throws IOException + { + this.remoteAddr = remoteAddress; + StartThreads(); + pseudoTcp.Connect(); + UpdateClock(); + try + { + long elapsed = 0; + //Here the threads is blocked untill we reach TCP_ESTABLISHED state + //There's also check for timeout for that op + synchronized (state_notify) + { + while (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED + && elapsed < timeout) + { + long start = System.currentTimeMillis(); + state_notify.wait(timeout); + long end = System.currentTimeMillis(); + elapsed += end - start; + } + if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED) + { + throw new IOException("Connect timeout"); + } + } + } + catch (InterruptedException ex) + { + Close(); + throw new IOException("Connect aborted"); + } + } + + /** + * Start connection procedure + * + * @param ip destination ip address + * @param port destination port + * @param timeout for this operation in ms + * @throws IOException + */ + public void Connect(String ip, int port, long timeout) + throws IOException + { + //this.remoteAddr = InetAddress.getByName(ip); + //this.remotePort = port; + Connect(new InetSocketAddress(InetAddress.getByName(ip), port), timeout); + } + + /** + * Blocking method waits for connection. + * + * @param timeout for this operation in ms + * @throws IOException If socket gets closed or timeout expires + */ + public void Accept(int timeout) + throws IOException + { + try + { + StartThreads(); + PseudoTcpState state = pseudoTcp.getState(); + if (state == PseudoTcpState.TCP_CLOSED) + { + throw new IOException("Socket closed"); + } + if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED) + { + synchronized (state_notify) + { + state_notify.wait(timeout); + } + } + if (pseudoTcp.getState() != PseudoTcpState.TCP_ESTABLISHED) + { + throw new IOException("Accept timeout"); + } + } + catch (InterruptedException ex) + { + IOException e = new IOException("Accept aborted"); + pseudoTcp.closedown(e); + throw e; + } + } + + /** + * + * @return current TCP state + */ + public PseudoTcpState getState() + { + return pseudoTcp.getState(); + } + + /** + * Interrupts clock thread's wait method to force time update + */ + private void UpdateClock() + { + synchronized (clock_notify) + { + clock_notify.notifyAll(); + } + } + + /** + * Starts all threads required by the socket + */ + private void StartThreads() + { + pseudoTcp.NotifyClock(System.currentTimeMillis()); + receiveThread = new Thread(new Runnable() + { + @Override + public void run() + { + ReceivePackets(); + } + }, "PseudoTcpReceiveThread"); + clockThread = new Thread(new Runnable() + { + @Override + public void run() + { + RunClock(); + } + }, "PseudoTcpClockThread"); + runReceive = true; + runClock = true; + receiveThread.start(); + clockThread.start(); + } + + /** + * Shutdown the socket + * + * @throws IOException + */ + public void Close() throws IOException + { + try + { + pseudoTcp.Close(true); + //System.out.println("ON CLOSE: in flight "+pseudoTcp.GetBytesInFlight()); + //System.out.println("ON CLOSE: buff not sent "+pseudoTcp.GetBytesBufferedNotSent()); + OnTcpClosed(pseudoTcp, null); + socket.close(); + JoinAllThreads(); + //UpdateClock(); + //TODO: closing procedure + //Here the thread should be blocked until TCP + //reaches CLOSED state, but there's no closing procedure + /* + * synchronized(state_notify){ while(pseudoTcp.getState() != + * PseudoTcpState.TCP_CLOSED){ try { state_notify.wait(); } catch + * (InterruptedException ex) { throw new IOException("Close + * connection aborted"); } } } + */ + } + catch (InterruptedException ex) + { + throw new IOException("Closing socket interrupted", ex); + } + + } + + /** + * Implements @link(IPseudoTcpNotify) + * Called when TCP enters connected state. + * + * @param tcp + */ + @Override + public void OnTcpOpen(PseudoTCPBase tcp) + { + logger.log(Level.FINE, "tcp opened"); + //Release threads blocked at state_notify monitor object. + synchronized (state_notify) + { + state_notify.notifyAll(); + } + //TCP is considered writeable at this point + OnTcpWriteable(tcp); + } + + /** + * Implements @link(IPseudoTcpNotify) + * + * @param tcp + */ + @Override + public void OnTcpReadable(PseudoTCPBase tcp) + { + //release all thread blocked at read_notify monitor + synchronized (read_notify) + { + logger.log( + Level.FINER, + "TCP READABLE data available for reading: "); + read_notify.notifyAll(); + } + } + + /** + * Implements @link(IPseudoTcpNotify) + * + * @param tcp + */ + @Override + public void OnTcpWriteable(PseudoTCPBase tcp) + { + + logger.log(Level.FINER, "stream writeable"); + //release all threads blocked at write monitor + synchronized (write_notify) + { + write_notify.notifyAll(); + } + //writeSemaphore.release(1); + logger.log(Level.FINER, "write notified - now !"); + + } + + /** + * Implements @link(IPseudoTcpNotify) + * + * @param tcp + * @param e + */ + @Override + public void OnTcpClosed(PseudoTCPBase tcp, IOException e) + { + if (e != null) + { + e.printStackTrace(); + logger.log(Level.SEVERE, "PseudoTcp closed: " + e); + } + else + { + logger.log(Level.FINE, "PseudoTcp closed"); + } + runReceive = false; + runClock = false; + this.exception = e; + releaseAllLocks(); + } + + /** + * Releases all monitor objects so that the threads will check their "run + * flags" + */ + private void releaseAllLocks() + { + synchronized (read_notify) + { + read_notify.notifyAll(); + } + synchronized (write_notify) + { + write_notify.notifyAll(); + } + synchronized (state_notify) + { + state_notify.notifyAll(); + } + //this interrupt won't work for DatagramSocket read packet operation + //receiveThread.interrupt(); + clockThread.interrupt(); + } + + /** + * Joins all running threads + * + * @throws InterruptedException + */ + private void JoinAllThreads() throws InterruptedException + { + clockThread.join(); + receiveThread.join(); + } + + /** + * Implements @link(IPseudoTcpNotify) + * + * @param tcp + * @param buffer + * @param len + * @return + */ + @Override + public WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len) + { + if (logger.isLoggable(Level.FINEST)) + { + logger.log(Level.FINEST, "write packet to network " + len); + } + try + { + DatagramPacket packet = new DatagramPacket(buffer, len, remoteAddr); + socket.send(packet); + return WriteResult.WR_SUCCESS; + } + catch (IOException ex) + { + logger.log(Level.SEVERE, "TcpWritePacket exception: " + ex); + return WriteResult.WR_FAIL; + } + + } + /** + * Flag which enables packets receive thread + */ + private boolean runReceive = false; + /** + * Thread receiving packets from the network + */ + private Thread receiveThread; + + /** + * Receives packets from the network and passes them to TCP logic class + */ + private void ReceivePackets() + { + byte[] buffer = new byte[DATAGRAM_RCV_BUFFER_SIZE]; + DatagramPacket packet = new DatagramPacket(buffer, DATAGRAM_RCV_BUFFER_SIZE); + while (runReceive) + { + try + { + socket.receive(packet); + //Here is the binding point for remote socket if wasn't + //specified earlier + if (remoteAddr == null) + { + remoteAddr = packet.getSocketAddress(); + } + else + { + if (!packet.getSocketAddress().equals(remoteAddr)) + { + logger.log(Level.WARNING, + "Ignoring packet from " + packet.getAddress() + + ":" + packet.getPort() + " should be: " + remoteAddr); + } + } + synchronized (pseudoTcp) + { + pseudoTcp.NotifyPacket(buffer, packet.getLength()); + //we need to update the clock after new packet is receivied + UpdateClock(); + } + } + catch (IOException ex) + { + //this exception occurs even when the socket + //is closed with the close operation, so we check + //here if this exception is important + if (runReceive) + { + logger.log(Level.SEVERE, + "ReceivePackets exception: " + ex); + pseudoTcp.closedown(ex); + } + break; + } + } + } + /** + * The run flag for clock thread + */ + private boolean runClock = false; + private Thread clockThread; + + /** + * Method runs cyclic notification about time progress for TCP logic class + * It runs in a separate thread + */ + private void RunClock() + { + long sleep; + while (runClock) + { + synchronized (pseudoTcp) + { + pseudoTcp.NotifyClock(System.currentTimeMillis()); + sleep = pseudoTcp.GetNextClock(System.currentTimeMillis()); + } + + //there might be negative interval even if there's no error + if (sleep == -1) + { + releaseAllLocks(); + if (exception != null) + { + logger.log(Level.SEVERE, + "STATE: " + pseudoTcp.getState() + + " ERROR: " + exception.getMessage()); + } + break; + } + synchronized (clock_notify) + { + try + { + logger.log(Level.FINEST, "Clock sleep for " + sleep); + clock_notify.wait(sleep); + } + catch (InterruptedException ex) + { + //interruption means end of task, as in normal operation + //notify will be called + break; + } + } + + } + } + private OutputStream outputstream; + + /** + * Lazy initialization for socket class + * + * @return output socket object + */ + public OutputStream getOutputStream() + { + if (outputstream == null) + { + outputstream = new PseudoTcpOutputStream(); + } + return outputstream; + } + private PseudoTcpInputStream inputStream; + + /** + * Lazy initialization for socket class + * + * @return output socket object + */ + public InputStream getInputStream() + { + if (inputStream == null) + { + inputStream = new PseudoTcpInputStream(); + } + return inputStream; + } + + /** + * This class implements @link(InputStream) * + */ + class PseudoTcpInputStream extends InputStream + { + public PseudoTcpInputStream() + { + } + + @Override + public boolean markSupported() + { + return false; + } + + /** + * There's no end of stream detection at the moment. Method blocks until + * it returns any data or an exception is thrown + * + * @return read byte count + * @throws IOException in case of en error + */ + @Override + public int read() throws IOException + { + byte[] buff = new byte[1]; + int readCount = read(buff, 0, 1); + return readCount == 1 ? buff[0] : -1; + } + + @Override + public int read(byte[] bytes) throws IOException + { + return read(bytes, 0, bytes.length); + } + + /** + * This method blocks until any data is available + * + * @param buffer destination buffer + * @param offset destination buffer's offset + * @param length maximum count of bytes that can be read + * @return byte count actually read + * @throws IOException + */ + @Override + public int read(byte[] buffer, int offset, int length) throws IOException + { + int read; + while (true) + { + logger.log(Level.FINER, "Read Recv"); + read = pseudoTcp.Recv(buffer, offset, length); + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, "Read Recv read count: " + read); + } + if (read > 0) + { + return read; + } + try + { + synchronized (read_notify) + { + logger.log(Level.FINER, "Read wait for data available"); + read_notify.wait(); + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, + "Read notified: " + pseudoTcp.GetAvailable()); + } + } + if (exception != null) + { + throw exception; + } + } + catch (InterruptedException ex) + { + if (exception != null) + { + throw new IOException("Read aborted", exception); + } + else + { + throw new IOException("Read aborted"); + } + } + } + } + + @Override + public int available() throws IOException + { + return pseudoTcp.GetAvailable(); + } + + @Override + public void close() throws IOException + { + } + } + + /** + * Implements @link(OutputStream) + */ + class PseudoTcpOutputStream extends OutputStream + { + @Override + public void write(int b) throws IOException + { + byte[] bytes = new byte[1]; + bytes[0] = (byte) b; + write(b); + } + + @Override + public void write(byte[] bytes) throws IOException + { + write(bytes, 0, bytes.length); + } + + /** + * This method blocks until all data has been written or an exception + * occurs + * + * @param buffer source buffer + * @param offset source buffer's offset + * @param length byte count to be written + * @throws IOException + */ + @Override + public void write(byte[] buffer, int offset, int length) throws IOException + { + int toSend = length; + int sent; + while (toSend > 0) + { + synchronized (pseudoTcp) + { + sent = pseudoTcp.Send(buffer, offset + length - toSend, toSend); + } + if (sent > 0) + { + toSend -= sent; + } + else + { + try + { + logger.log(Level.FINER, "Write wait for notify"); + synchronized (write_notify) + { + write_notify.wait(); + } + logger.log(Level.FINER, + "Write notified, available: " + + pseudoTcp.GetAvailableSendBuffer()); + if (exception != null) + { + throw exception; + } + } + catch (InterruptedException ex) + { + if (exception != null) + { + throw new IOException("Write aborted", exception); + } + else + { + throw new IOException("Write aborted", ex); + } + } + } + } + } + + /** + * This method block until all buffered data has been written + * + * @throws IOException + */ + @Override + public synchronized void flush() throws IOException + { + logger.log(Level.FINE, "Flushing..."); + final Object ackNotify = pseudoTcp.GetAckNotify(); + while (pseudoTcp.GetBytesBufferedNotSent() > 0) + { + synchronized (ackNotify) + { + try + { + ackNotify.wait(); + } + catch (InterruptedException ex) + { + throw new IOException("Flush stream interrupted", ex); + } + } + } + logger.log(Level.FINE, "Flushing completed"); + } + + @Override + public void close() throws IOException + { + } + } +} diff --git a/src/org/ice4j/pseudotcp/PseudoTcpState.java b/src/org/ice4j/pseudotcp/PseudoTcpState.java new file mode 100755 index 0000000..44126d2 --- /dev/null +++ b/src/org/ice4j/pseudotcp/PseudoTcpState.java @@ -0,0 +1,38 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +/** + * TCP states defined for pseudoTCP + * @author Pawel Domas + */ +public enum PseudoTcpState +{ + /** + * Initial state, can accept connection + */ + TCP_LISTEN,// = 0, + /** + * SYN sent to remote peer, wits for SYN + */ + TCP_SYN_SENT,// = 1, + /** + * SYN received from remote peer, sends back SYN + */ + TCP_SYN_RECEIVED,// = 2; + /** + * SYN sent and received - connection established + */ + TCP_ESTABLISHED,// = 3; + /** + * Closed state. In current implementation reached on error + * or explicite by close method with force option + * TODO: closing procedure + */ + TCP_CLOSED// = 4; +}; diff --git a/src/org/ice4j/pseudotcp/WriteResult.java b/src/org/ice4j/pseudotcp/WriteResult.java new file mode 100755 index 0000000..590c506 --- /dev/null +++ b/src/org/ice4j/pseudotcp/WriteResult.java @@ -0,0 +1,28 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +/** + * The result of write packet operations + * @author Pawel Domas + */ +public enum WriteResult +{ + /** + * Packet successfully transmitted + */ + WR_SUCCESS, + /** + * Packet was too large + */ + WR_TOO_LARGE, + /** + * Write failed + */ + WR_FAIL +}; diff --git a/src/org/ice4j/pseudotcp/util/ByteFifoBuffer.java b/src/org/ice4j/pseudotcp/util/ByteFifoBuffer.java new file mode 100755 index 0000000..3eedcfc --- /dev/null +++ b/src/org/ice4j/pseudotcp/util/ByteFifoBuffer.java @@ -0,0 +1,381 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp.util; + +import java.nio.*; + +/** + * First in - first out byte buffer + * + * @author Pawel Domas + */ +public class ByteFifoBuffer +{ + /** + * Backing byte array + */ + private byte[] array; + /** + * Current write position + */ + private int write_pos = 0; + /** + * Stored bytes count + */ + private int buffered = 0; + /** + * Current read position + */ + private int read_pos = 0; + + /** + * Creates buffer of specified size + * + * @param len buffer's size + */ + public ByteFifoBuffer(int len) + { + array = new byte[len]; + } + + /** + * @return buffer's capacity + */ + public int Length() + { + return array.length; + } + + /** + * Method reads count bytes into out_buffer + * Current read position is incremented by count of bytes + * that has been successfully read + * + * @param out_buffer + * @param count + * @return bytes successfully read + */ + public int Read(byte[] out_buffer, int count) + { + count = readLimit(count); + if (count > 0) + { + readOp(out_buffer, 0, count, array, read_pos, array.length); + read_pos = (read_pos + count) % array.length; + buffered -= count; + } + return count; + } + + /** + * Limits desiredReadCount to count that is actually available + * @param desiredReadCount + * @return + */ + private int readLimit(int desiredReadCount) + { + return desiredReadCount > buffered ? buffered : desiredReadCount; + } + + /** + * Utility method used for read operations + * @param outBuffer + * @param dst_buff_offset + * @param count + * @param srcBuffer + * @param read_pos + * @param buff_len + */ + private static void readOp(byte[] outBuffer, int dst_buff_offset, int count, + byte[] srcBuffer, int read_pos, int buff_len) + { + if (read_pos + count <= buff_len) + { + //single operation + System.arraycopy(srcBuffer, read_pos, outBuffer, dst_buff_offset, + count); + } + else + { + //two operations + int tillEndCount = buff_len - read_pos; + System.arraycopy(srcBuffer, read_pos, outBuffer, + dst_buff_offset, tillEndCount); + int fromStartCount = count - tillEndCount; + System.arraycopy(srcBuffer, 0, outBuffer, + dst_buff_offset + tillEndCount, fromStartCount); + } + } + + /** + * + * @return space left in buffer for write + */ + public int GetWriteRemaining() + { + return array.length - buffered; + } + + /** + * + * @return bytes stored in buffer and available for reading + */ + public int GetBuffered() + { + return buffered; + } + + /** + * Writes count of bytes from the buffer + * + * @param buffer + * @param count + * @return bytes successfully written to buffer + */ + public int Write(byte[] buffer, int count) + { + return Write(buffer, 0, count); + } + + /** + * + * @param data source data + * @param offset source buffer's offset + * @param count + * @return byte count actually read + */ + public int Write(byte[] data, int offset, int count) + { + /* + * System.out.println("----write " + this + " " + len + " buffered " + + * GetBuffered() + " buff avail: " + GetWriteRemaining()); + */ + count = writeLimit(count); + writeOp(data, offset, count, array, write_pos, array.length); + write_pos = (write_pos + count) % array.length; + buffered += count; + /* + * System.out.println("----write "+this+" "+len+" buffered + * "+GetBuffered()); for(int i=0; i < len; i++){ + * System.out.println("WDATA: "+data[i]); } + */ + return count; + } + + /** + * Utility method for write operations + * @param inBuffer + * @param inOffset + * @param count + * @param outBuffer + * @param write_pos + * @param buff_len + */ + private static void writeOp(byte[] inBuffer, + int inOffset, + int count, + byte[] outBuffer, + int write_pos, + int buff_len) + { + if ((write_pos + count) <= buff_len) + { + //single op + System.arraycopy(inBuffer, inOffset, outBuffer, write_pos, count); + } + else + { + //till end and from beginning + int tillEndCount; + int fromStartCount; + tillEndCount = buff_len - write_pos; + fromStartCount = count - tillEndCount; + System.arraycopy(inBuffer, inOffset, outBuffer, + write_pos, tillEndCount); + System.arraycopy(inBuffer, inOffset + tillEndCount, + outBuffer, 0, fromStartCount); + } + } + + /** + * Limits desiredWriteCount to what's actually available + * @param desiredWriteCount + * @return + */ + private int writeLimit(int desiredWriteCount) + { + return desiredWriteCount > (array.length - buffered) ? + (array.length - buffered) : desiredWriteCount; + } + + /** + * Checks if new write position is correct + * + * @param newWrPos new write position + */ + private void assertWriteLimit(int newWrPos) + throws IllegalArgumentException + { + int spaceReq; + int availSpace = GetWriteRemaining(); + if (newWrPos < write_pos) + { + spaceReq = newWrPos + (array.length - write_pos); + } + else + { + spaceReq = newWrPos - write_pos; + } + + if (spaceReq > availSpace) + { + throw new IllegalArgumentException(); + } + } + + /** + * Advances current buffer's write position by count bytes + * + * @param count + */ + public void ConsumeWriteBuffer(int count) + throws IllegalArgumentException, + BufferOverflowException + { + if (count > GetWriteRemaining()) + { + throw new BufferOverflowException(); + } + if (count < 0) + { + throw new IllegalArgumentException(); + } + int newPos = (write_pos + count) % array.length; + assertWriteLimit(newPos); + + write_pos = newPos; + buffered += count; + } + + /** + * Sets new buffer's capacity + * + * @param new_size + * @return true if operation is possible to perform, that is if new + * buffered data fits into new buffer + */ + public boolean SetCapacity(int new_size) + { + if (new_size < GetBuffered()) + { + return false; + } + byte[] newBuff = new byte[new_size]; + readOp(newBuff, 0, buffered, array, read_pos, array.length); + this.array = newBuff; + return true; + } + + /** + * Aligns current read position by count + * + * @param count + * @throws BufferUnderflowException if new position exceeds buffered data + * count + */ + public void ConsumeReadData(int count) + throws IllegalArgumentException, + BufferUnderflowException + { + /* + * System.out.println("Consume read " + this + " " + count + " read pos: + * " + read_pos); + */ + if (count > buffered) + { + throw new BufferUnderflowException(); + } + if (count < 0) + { + throw new IllegalArgumentException(); + } + this.read_pos = (read_pos + count) % array.length; + buffered -= count; + } + + /** + * Reads count bytes from buffer without storing new read position + * + * @param dst_buff + * @param dst_buff_offset offset of destination buffer + * @param count bytes to read + * @param offset from current read position + * @return bytes successfully read + */ + public int ReadOffset(byte[] dst_buff, + int dst_buff_offset, + int count, + int offset) + { + //TODO: not sure if should decrease read count or throw an exception + /* + * System.out.println("Read dst offset " + dst_buff_offset + " offset " + * + offset + " len " + count + " " + this); + */ + int read_offset = (this.read_pos + offset) % array.length; + readOp(dst_buff, dst_buff_offset, count, array, read_offset, array.length); + + return count; + } + + /** + * Writes count bytes from data to the buffer without + * affecting buffer's write position + * + * @param data + * @param count + * @param nOffset from buffer's write position + * @return bytes successfully written + */ + public int WriteOffset(byte[] data, int count, int nOffset) + throws BufferOverflowException + { + if (count > GetWriteRemaining()) + { + throw new BufferOverflowException(); + } + if (count < 0) + { + throw new IllegalArgumentException(); + } + int offWritePos = (this.write_pos + nOffset) % array.length; + count = writeLimit(count); + //try{ + assertWriteLimit(offWritePos + count); + //}catch(IllegalArgumentException e){ + // System.out.println("Write offset: " + nOffset + " count: " + count + //+ " wpos: " + write_pos + " buffered: " + buffered + " len: " + length + this); + // throw e; + //} + //buffer.position(writePos + nOffset); + writeOp(data, 0, count, array, offWritePos, array.length); + //buffer.position(writePos); + return count; + } + + public void ResetReadPosition() + { + this.read_pos = 0; + } + + public void ResetWritePosition() + { + this.write_pos = 0; + this.buffered = 0; + } +} diff --git a/test/org/ice4j/pseudotcp/MultiThreadSupportTest.java b/test/org/ice4j/pseudotcp/MultiThreadSupportTest.java new file mode 100755 index 0000000..70ea7e3 --- /dev/null +++ b/test/org/ice4j/pseudotcp/MultiThreadSupportTest.java @@ -0,0 +1,75 @@ +package org.ice4j.pseudotcp; + +import junit.framework.*; +import static org.junit.Assert.*; + +/** + * This class enables multi thread tests where main thread waits in loop + * for specified condition to be met, while others perform some operations. + * For example until the connection is established or closed. + * It also registers default uncaught exception handler to catch exceptions + * from other threads. + * + * Condition checks are passed as IWaitUntilDone interface. + * + * @author Pawel Domas + */ +public class MultiThreadSupportTest extends TestCase +{ + private volatile Throwable testError; + private volatile Thread errorThread; + private final Object testLock = new Object(); + + public MultiThreadSupportTest() + { + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() + { + @Override + public void uncaughtException(Thread t, Throwable e) + { + synchronized (testLock) + { + testError = e; + errorThread = t; + testLock.notifyAll(); + } + } + }); + } + + private long assertWaitInterval = 100; + protected interface IWaitUntilDone + { + public boolean isDone(); + } + + protected boolean assert_wait_until(IWaitUntilDone wait, long timeoutMs) + { + try + { + long start = System.currentTimeMillis(); + while (!wait.isDone() && (System.currentTimeMillis() - start) < timeoutMs) + { + synchronized (testLock) + { + testLock.wait(assertWaitInterval); + if (testError != null) + { + testError.printStackTrace(); + fail("Error in thread: " + errorThread.getName() + " : " + testError.getMessage()); + } + } + } + return wait.isDone(); + } + catch (InterruptedException ex) + { + ex.printStackTrace(); + fail("assert_wait - interrupted"); + //return is unreachable + return false; + } + } + + +} diff --git a/test/org/ice4j/pseudotcp/PseudoTcpStreamTest.java b/test/org/ice4j/pseudotcp/PseudoTcpStreamTest.java new file mode 100755 index 0000000..6076c69 --- /dev/null +++ b/test/org/ice4j/pseudotcp/PseudoTcpStreamTest.java @@ -0,0 +1,139 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.net.*; +import java.util.logging.*; +import static org.junit.Assert.*; +import org.junit.*; + +/** + * + * @author Pawel Domas + */ +public class PseudoTcpStreamTest extends MultiThreadSupportTest +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(PseudoTCPBase.class.getName()); + + public PseudoTcpStreamTest() + { + } + + /** + * Test one-way transfer with @link(PseudoTcpStream) + * + * @throws SocketException + */ + public void testConnectTransferClose() throws SocketException + { + String ip = ""; + final int server_port = 49999; + long conv_id = 0; + final int size = 1000000; + int transferTimeout = 5000; + final PseudoTcpSocket server = new PseudoTcpSocket(conv_id, server_port); + final PseudoTcpSocket client = new PseudoTcpSocket(conv_id); + Thread serverThread = new Thread(new Runnable() + { + @Override + public void run() + { + try + { + server.Accept(300); + int rcvd = 0; + while (rcvd != size) + { + rcvd += server.getInputStream().read(new byte[size]); + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, "Received: " + rcvd); + } + } + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, "Total received: " + rcvd); + } + //server.Close(); + + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + } + }); + Thread clientThread = new Thread(new Runnable() + { + @Override + public void run() + { + try + { + client.Connect("localhost", server_port, 30000); + assertEquals(PseudoTcpState.TCP_ESTABLISHED, client.getState()); + client.getOutputStream().write(new byte[size]); + client.getOutputStream().flush(); + client.Close(); + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + } + }); + serverThread.start(); + clientThread.start(); + try + { + boolean success = assert_wait_until(new IWaitUntilDone() + { + @Override + public boolean isDone() + { + return client.getState() == PseudoTcpState.TCP_CLOSED; + } + }, transferTimeout); + if (success) + { + clientThread.join(); + serverThread.join(); + } + else + { + fail("Transfer timeout"); + } + } + catch (InterruptedException ex) + { + throw new RuntimeException(ex); + } + } + + /** + * Test the timeout on accept method + */ + public void testAccept() + { + try + { + PseudoTcpSocket server = new PseudoTcpSocket(0); + server.Accept(10); + fail("Should throw timeout exception"); + } + catch (IOException ex) + { + //success + } + } +} diff --git a/test/org/ice4j/pseudotcp/PseudoTcpTestBase.java b/test/org/ice4j/pseudotcp/PseudoTcpTestBase.java new file mode 100755 index 0000000..293014d --- /dev/null +++ b/test/org/ice4j/pseudotcp/PseudoTcpTestBase.java @@ -0,0 +1,684 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.util.*; +import java.util.logging.*; +import org.ice4j.pseudotcp.util.*; + +/** + * Base class for other pseduoTCP logic tests. Runs all threads required for the + * protocol to work. There are two instances of pseudoTCP logic being run. Data + * packets between them are passes directly with some loss and/or delay + * introduced optionally. + * + * @author Pawel Domas + */ +public abstract class PseudoTcpTestBase extends MultiThreadSupportTest + implements IPseudoTcpNotify +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(PseudoTCPBase.class.getName()); + /** + * Remote peer TCP logic instance + */ + private final PseudoTCPBase remoteTcp; + /** + * Local peer TCP logic instance + */ + private final PseudoTCPBase localTcp; + private int local_mtu_; + private int remote_mtu_; + /** + * Delay in ms introduced to packets delivery + */ + private int delay_; + /** + * Simulated packets loss in % + */ + private int loss_; + /** + * Stores info about connection state for use by child classes + */ + protected boolean have_connected_; + protected boolean have_disconnected_; + /** + * Timer used to delay packets delivery + */ + private Timer timer = new Timer("Delay timer"); + /** + * Timeout for connect operation in ms + */ + static final int kConnectTimeoutMs = 5000; // ~3 * default RTO of 3000ms + //static final int kMinTransferRate = 1050000; + /** + * Transfer rate used to calculate timeout for transfer operations. This + * timeout counts before the transfer tests will fail. + */ + static final int kMinTransferRate = 1000; + /** + * Transfer blocks size + */ + static final int kBlockSize = 4096; + + public PseudoTcpTestBase() + { + this.remoteTcp = new PseudoTCPBase(this, 1); + //Debug names are usefull to identify peers in log messages + remoteTcp.debugName = "REM"; + this.localTcp = new PseudoTCPBase(this, 1); + localTcp.debugName = "LOC"; + SetLocalMtu(65535); + SetRemoteMtu(65535); + } + + /** + * Creates some random data array + * + * @param size + * @return + */ + static byte[] createDummyData(int size) + { + byte[] dummy = new byte[size]; + Random r = new Random(); + r.nextBytes(dummy); + return dummy; + } + + /** + * Sets the mtu for local peer + * + * @param mtu + */ + void SetLocalMtu(int mtu) + { + localTcp.NotifyMTU(mtu); + local_mtu_ = mtu; + } + + /** + * Sets the mtu for remote peer + * + * @param mtu + */ + void SetRemoteMtu(int mtu) + { + remoteTcp.NotifyMTU(mtu); + remote_mtu_ = mtu; + } + + /** + * Sets the delay introduced to packets delivery between peers + * + * @param delay + */ + void SetDelay(int delay) + { + delay_ = delay; + } + + /** + * Sets loss % of packets transferred between local and remote peers + * + * @param percent + */ + void SetLoss(int percent) + { + loss_ = percent; + } + + /** + * Sets OptNagling for both local and remote peers + * + * @param enable_nagles + */ + void SetOptNagling(boolean enable_nagles) + { + localTcp.SetOption(Option.OPT_NODELAY, enable_nagles ? 0 : 1); + remoteTcp.SetOption(Option.OPT_NODELAY, enable_nagles ? 0 : 1); + + } + + /** + * Sets ack delay option for local and remote peers + * + * @param ack_delay + */ + void SetOptAckDelay(int ack_delay) + { + localTcp.SetOption(Option.OPT_ACKDELAY, ack_delay); + remoteTcp.SetOption(Option.OPT_ACKDELAY, ack_delay); + } + + /** + * Sets send buffer option for local and remote peers + * + * @param size + */ + void SetOptSndBuf(int size) + { + localTcp.SetOption(Option.OPT_SNDBUF, size); + remoteTcp.SetOption(Option.OPT_SNDBUF, size); + } + + /** + * Sets receive buffer size option for remote peer + * + * @param size + */ + void SetRemoteOptRcvBuf(int size) + { + remoteTcp.SetOption(Option.OPT_RCVBUF, size); + } + + /** + * Sets receive buffer size option for local peer + * + * @param size + */ + void SetLocalOptRcvBuf(int size) + { + localTcp.SetOption(Option.OPT_RCVBUF, size); + } + + /** + * Disable window scaling for remote peer + */ + void DisableRemoteWindowScale() + { + remoteTcp.disableWindowScale(); + } + + /** + * Disable window scaling for local peer + */ + void DisableLocalWindowScale() + { + localTcp.disableWindowScale(); + } + + /** + * Starts the connection from local to remote peer + * + * @throws IOException + */ + void Connect() throws IOException + { + localTcp.Connect(); + UpdateLocalClock(); + } + + /** + * Closes the connection + */ + void Close() + { + localTcp.Close(false); + UpdateLocalClock(); + } + + /** + * Catches the event OnTcpOpen on the local peer and marks have_connected + * flag + * + * @param tcp + */ + @Override + public void OnTcpOpen(PseudoTCPBase tcp) + { + if (tcp == localTcp) + { + have_connected_ = true; + OnTcpWriteable(tcp); + } + } + + /** + * Catches OnTcpClosed event on remote peer and marks have_disconnected flag + * + * @param tcp + * @param exc + */ + @Override + public void OnTcpClosed(PseudoTCPBase tcp, IOException exc) + { + assert exc == null; + if (tcp == remoteTcp) + { + have_disconnected_ = true; + } + } + /** + * Randomizer instance used to decide about packet loss + */ + private Random random = new Random(); + + int RandomInt() + { + return random.nextInt(100); + } + + /** + * Send the data from local to remote peer + * + * @param data + * @param len + * @return + * @throws IOException + */ + int LocalSend(byte[] data, int len) throws IOException + { + return localTcp.Send(data, len); + } + + /** + * Receive data as local peer + * + * @param buffer + * @param len + * @return + * @throws IOException + */ + int LocalRecv(byte[] buffer, int len) throws IOException + { + return localTcp.Recv(buffer, len); + } + + /** + * Receive data as remote peer + * + * @param buffer + * @param len + * @return + * @throws IOException + */ + int RemoteRecv(byte[] buffer, int len) throws IOException + { + + return remoteTcp.Recv(buffer, len); + } + + /** + * Sends the data from remote to local peer + * + * @param data + * @param len + * @return + * @throws IOException + */ + int RemoteSend(byte[] data, int len) + throws IOException + { + return remoteTcp.Send(data, len); + } + + /** + * Simulates packet received by local peer + * + * @param data + * @param len + * @throws IOException + */ + private void LocalPacket(byte[] data, int len) + throws IOException + { + localTcp.NotifyPacket(data, len); + UpdateLocalClock(); + } + + /** + * Simulates packet received by remote peer + * + * @param data + * @param len + * @throws IOException + */ + private void RemotePacket(byte[] data, int len) + throws IOException + { + remoteTcp.NotifyPacket(data, len); + UpdateRemoteClock(); + } + + /** + * Creates TimerTask with @link(RemotePacket) action + * + * @param data + * @param len + * @return + */ + private TimerTask getWriteRemotePacketTask(final byte[] data, final int len) + { + return new TimerTask() + { + @Override + public void run() + { + try + { + RemotePacket(data, len); + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + } + }; + } + + /** + * Creates TimerTask with @link(LocalPacket) action + * + * @param data + * @param len + * @return + */ + private TimerTask getWriteLocalPacketTask(final byte[] data, final int len) + { + return new TimerTask() + { + @Override + public void run() + { + try + { + LocalPacket(data, len); + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + } + }; + } + + /** + * Handles passing packets between local and remote peers. Here are taken + * decisions about packets loss and delay. + * + * @param tcp + * @param buffer + * @param len + * @return + */ + @Override + public WriteResult TcpWritePacket(PseudoTCPBase tcp, byte[] buffer, int len) + { + // Randomly drop the desired percentage of packets. + // Also drop packets that are larger than the configured MTU. + if (RandomInt() < loss_) + { + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, "Randomly dropping packet, size=" + len); + } + } + else + { + if (len > Math.min(local_mtu_, remote_mtu_)) + { + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + "Dropping packet that exceeds path MTU, size=" + + len); + } + } + else + { + if (tcp == localTcp) + { + timer.schedule(getWriteRemotePacketTask(buffer, len), delay_); + } + else + { + timer.schedule(getWriteLocalPacketTask(buffer, len), delay_); + } + } + } + return WriteResult.WR_SUCCESS; + } + + /** + * Wakes up local clock thread from wait method causing forced time update + */ + protected void UpdateLocalClock() + { + if (localClockThread != null) + { + synchronized (localClockLock) + { + localClockLock.notifyAll(); + } + } + } + + /** + * Wakes up remote clock thread from wait method causing forced time update + */ + protected void UpdateRemoteClock() + { + if (remoteClockThread != null) + { + synchronized (remoteClockLock) + { + remoteClockLock.notifyAll(); + } + } + } + + /** + * Method handles time update for pseudoTCP logic class + * + * @param tcp + * @param lock + */ + private void UpdateNextClock(final PseudoTCPBase tcp, final Object lock) + { + try + { + + long now = PseudoTCPBase.Now(); + //System.out.println(tcp.debugName + " NOTIFY CLOCK: " + now); + synchronized (tcp) + { + tcp.NotifyClock(now); + } + //UpdateClock(tcp); + long interval; // NOLINT + synchronized (tcp) + { + interval = tcp.GetNextClock(PseudoTCPBase.Now()); + } + //interval = Math.max(interval, 0L); // sometimes interval is < 0 + if (logger.isLoggable(Level.FINEST)) + { + logger.log(Level.FINEST, + tcp.debugName + " CLOCK sleep for " + interval); + } + if (interval < 0) + { + if (interval == -1) + { + interval = 1000; + } + else + { + return; + } + } + synchronized (lock) + { + lock.wait(interval); + } + } + catch (InterruptedException ex) + { + //Logger.getLogger(PseudoTcpTestBase.class.getName()).log(Level.SEVERE, null, ex); + } + } + /** + * Local peer clock thread + */ + private Thread localClockThread; + private final Object localClockLock = new Object(); + /** + * Remote peer clock thread + */ + private Thread remoteClockThread; + private final Object remoteClockLock = new Object(); + /** + * The "run flag" for clock threads + */ + private boolean runClocks = false; + + /** + * Start clock threads + */ + protected void StartClocks() + { + if (localClockThread == null && remoteClockThread == null) + { + runClocks = true; + localClockThread = new Thread(new Runnable() + { + @Override + public void run() + { + while (runClocks) + { + //localTcp.NotifyClock(PseudoTCPBase.Now()); + UpdateNextClock(localTcp, localClockLock); + + } + + } + }, "LocalClockThread"); + remoteClockThread = new Thread(new Runnable() + { + @Override + public void run() + { + while (runClocks) + { + //remoteTcp.NotifyClock(PseudoTCPBase.Now()); + UpdateNextClock(remoteTcp, remoteClockLock); + } + + } + }, "RemoteClockThread"); + localClockThread.start(); + remoteClockThread.start(); + } + else + { + throw new IllegalStateException(); + } + } + + /** + * Stops clock threads + */ + protected void StopClocks() + { + if (localClockThread != null && remoteClockThread != null) + { + try + { + runClocks = false; + localClockThread.interrupt(); + remoteClockThread.interrupt(); + localClockThread.join(); + localClockThread = null; + remoteClockThread.join(); + remoteClockThread = null; + } + catch (InterruptedException ex) + { + ex.printStackTrace(); + } + } + else + { + throw new IllegalStateException(); + } + } + + /** + * This method waits kConnectTimeoutMs miliseconds or until the + * connection has been established between local and remote peers + * + * @param kConnectTimeoutMs + * @return isDone result + */ + protected boolean assert_Connected_wait(int kConnectTimeoutMs) + { + return assert_wait_until(new IWaitUntilDone() + { + @Override + public boolean isDone() + { + return PseudoTcpTestBase.this.have_connected_; + } + }, kConnectTimeoutMs); + } + + /** + * This method waits kTransferTimeoutMs miliseconds or until the + * connection has been closed, which means that the data was transferred + * + * @param kTransferTimeoutMs + * @return isDone result + */ + protected boolean assert_Disconnected_wait(long kTransferTimeoutMs) + { + return assert_wait_until(new IWaitUntilDone() + { + @Override + public boolean isDone() + { + return PseudoTcpTestBase.this.have_disconnected_; + } + }, kTransferTimeoutMs); + } + + /** + * @return the remoteTcp + */ + PseudoTCPBase getRemoteTcp() + { + return remoteTcp; + } + + /** + * @return the localTcp + */ + PseudoTCPBase getLocalTcp() + { + return localTcp; + } + + /** + * Calculates maximum transfer time of size bytes for specified + * transfer rate + * + * @param size + * @param kBps + * @return timeout for transfer in ms(minimum 3000 ms) + */ + public long MaxTransferTime(long size, long kBps) + { + long transferTout = ((size) / kBps) * 8 * 1000; + return transferTout > 3000 ? transferTout : 3000; + } +} diff --git a/test/org/ice4j/pseudotcp/PseudoTcpTestPingPong.java b/test/org/ice4j/pseudotcp/PseudoTcpTestPingPong.java new file mode 100755 index 0000000..d7d9f77 --- /dev/null +++ b/test/org/ice4j/pseudotcp/PseudoTcpTestPingPong.java @@ -0,0 +1,357 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.util.logging.*; +import org.ice4j.pseudotcp.util.*; +import static org.junit.Assert.*; +import org.junit.*; + +/** + * This class implements test for two way transfers + * + * @author Pawel Domas + */ +public class PseudoTcpTestPingPong extends PseudoTcpTestBase +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(PseudoTCPBase.class.getName()); + /** + * The sender + */ + private PseudoTCPBase sender; + /** + * The receiver + */ + private PseudoTCPBase receiver; + /** + * How much data is sent per ping + */ + private int bytesPerSend; + /** + * Iterations count + */ + private int iterationsRemaining; + + public PseudoTcpTestPingPong() + { + } + + public void setBytesPerSend(int bytes_per_send) + { + this.bytesPerSend = bytes_per_send; + } + /** + * The send stream buffer + */ + ByteFifoBuffer send_stream; + /** + * The receive stream buffer + */ + ByteFifoBuffer recv_stream; + + /** + * Performs ping-pong test for iterations with packets of + * size bytes + * + * @param size + * @param iterations + */ + public void TestPingPong(int size, int iterations) + { + long start, end; + iterationsRemaining = iterations; + receiver = getRemoteTcp(); + sender = getLocalTcp(); + // Create some dummy data + byte[] dummy = createDummyData(size); + send_stream = new ByteFifoBuffer(size); + send_stream.Write(dummy, size); + //Prepare the receive stream + recv_stream = new ByteFifoBuffer(size); + //Connect and wait until connected + start = PseudoTCPBase.Now(); + StartClocks(); + try + { + Connect(); + } + catch (IOException ex) + { + ex.printStackTrace(); + fail(ex.getMessage()); + } + //assert Connect() == 0; + assert_Connected_wait(kConnectTimeoutMs); + // Sending will start from OnTcpWriteable and stop when the required + // number of iterations have completed. + assert_Disconnected_wait(kMinTransferRate); + long elapsed = PseudoTCPBase.Now() - start; + StopClocks(); + logger.log(Level.INFO, + "Performed " + iterations + " pings in " + elapsed + " ms"); + } + + /** + * Catches onTcpReadable event for receiver + * + * @param tcp + */ + @Override + public void OnTcpReadable(PseudoTCPBase tcp) + { + assertEquals("Unexpected onTcpReadable", receiver, tcp); + try + { + // Stream bytes to the recv stream as they arrive. + ReadData(); + } + catch (IOException ex) + { + //will be caught by default handler and test will fail + throw new RuntimeException(ex); + } + // If we've received the desired amount of data, rewind things + // and send it back the other way! + int recvd = recv_stream.GetBuffered(); + int required = send_stream.Length(); + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, + "test - receivied: " + recvd + " required: " + required); + } + + if (recvd == required) + { + if (receiver == getLocalTcp() && --iterationsRemaining == 0) + { + Close(); + // TODO: Fake OnTcpClosed() on the receiver for now. + OnTcpClosed(getRemoteTcp(), null); + return; + } + //switches receivier with sender and performs test the other way + PseudoTCPBase tmp = receiver; + receiver = sender; + sender = tmp; + send_stream.ResetReadPosition(); + send_stream.ConsumeWriteBuffer(send_stream.GetWriteRemaining()); + recv_stream.ResetWritePosition(); + OnTcpWriteable(sender); + } + + } + + /** + * Catches the ontcpWriteable event for sender + * + * @param tcp + */ + @Override + public void OnTcpWriteable(PseudoTCPBase tcp) + { + if (tcp != sender) + { + return; + } + // Write bytes from the send stream when we can. + // Shut down when we've sent everything. + logger.log(Level.FINER, "Flow Control Lifted"); + try + { + WriteData(); + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + + } + + /** + * Reads the data in loop until is something available + * + * @throws IOException + */ + private void ReadData() throws IOException + { + byte[] block = new byte[kBlockSize]; + int rcvd = 0; + do + { + rcvd = receiver.Recv(block, block.length); + if (rcvd > 0) + { + recv_stream.Write(block, rcvd); + if (logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, + "Receivied: " + recv_stream.GetBuffered()); + } + } + } + while (rcvd > 0); + } + + /** + * Writes all data to the receiver + * + * @throws IOException + */ + private void WriteData() throws IOException + { + int tosend; + int sent = 0; + byte[] block = new byte[kBlockSize]; + do + { + tosend = bytesPerSend != 0 ? bytesPerSend : block.length; + tosend = send_stream.Read(block, tosend); + if (tosend > 0) + { + sent = sender.Send(block, tosend); + UpdateLocalClock(); + if (sent != -1) + { + if(logger.isLoggable(Level.FINE)) + { + logger.log(Level.FINE, "Sent: " + sent); + } + } + else + { + logger.log(Level.FINE, "Flow controlled"); + } + } + else + { + sent = tosend = 0; + } + } + while (sent > 0); + } + + /** + * + * Ping-pong (request/response) tests + * + */ + /** + * Test sending <= 1x MTU of data in each ping/pong. Should take <10ms. + */ + public void testPingPong1xMtu() + { + //logger.log(Level.INFO, "Test ping - pong 1xMTU"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.TestPingPong(100, 100); + } + + /** + * Test sending 2x-3x MTU of data in each ping/pong. Should take <10ms. + */ + public void testPingPong3xMtu() + { + //logger.log(Level.INFO, "Test ping - pong 3xMTU"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.TestPingPong(400, 100); + } + + /** + * Test sending 1x-2x MTU of data in each ping/pong. Should take ~1s, due to + * interaction between Nagling and Delayed ACK. + */ + public void testPingPong2xMtu() + { + //logger.log(Level.INFO, "Test ping - pong 2xMTU"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.TestPingPong(2000, 5); + } + + /** + * Test sending 1x-2x MTU of data in each ping/pong with Delayed ACK off. + * Should take <10ms. + */ + public void testPingPong2xMtuWithAckDelayOff() + { + //logger.log(Level.INFO, "Test ping - pong 2xMTU ack delay off"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptAckDelay(0); + test.TestPingPong(2000, 100); + } + + /** + * Test sending 1x-2x MTU of data in each ping/pong with Nagling off. Should + * take <10ms. + */ + public void testPingPong2xMtuWithNaglingOff() + { + //logger.log(Level.INFO, "Test ping - pong 2xMTU nagling off"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptNagling(false); + test.TestPingPong(2000, 5); + } + + /** + * Test sending a ping as pair of short (non-full) segments. Should take + * ~1s, due to Delayed ACK interaction with Nagling. + */ + public void testPingPongShortSegments() + { + //logger.log(Level.INFO, "Test ping - pong short segments"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptAckDelay(5000); + test.setBytesPerSend(50); // i.e. two Send calls per payload + test.TestPingPong(100, 5); + } + + /** + * Test sending ping as a pair of short (non-full) segments, with Nagling + * off. Should take <10ms. + */ + public void testPingPongShortSegmentsWithNaglingOff() + { + //logger.log(Level.INFO, "Test ping - pong short segments nagling off"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptNagling(false); + test.setBytesPerSend(50); // i.e. two Send calls per payload + test.TestPingPong(100, 5); + } + + /** + * Test sending <= 1x MTU of data ping/pong, in two segments, no Delayed + * ACK. Should take ~1s. + */ + public void testPingPongShortSegmentsWithAckDelayOff() + { + //logger.log(Level.INFO, "Test ping - pong short segments nagling off"); + PseudoTcpTestPingPong test = new PseudoTcpTestPingPong(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.setBytesPerSend(50); // i.e. two Send calls per payload + test.SetOptAckDelay(0); + test.TestPingPong(100, 5); + } +} diff --git a/test/org/ice4j/pseudotcp/PseudoTcpTestRecvWindow.java b/test/org/ice4j/pseudotcp/PseudoTcpTestRecvWindow.java new file mode 100755 index 0000000..e05a190 --- /dev/null +++ b/test/org/ice4j/pseudotcp/PseudoTcpTestRecvWindow.java @@ -0,0 +1,404 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.util.*; +import java.util.logging.*; +import org.ice4j.pseudotcp.util.*; +import static org.junit.Assert.*; +import org.junit.*; + +/** + * Fill the receiver window until it is full, drain it and then fill it with the + * same amount. This is to test that receiver window contracts and enlarges + * correctly. + * + * @author Pawel Domas + */ +public class PseudoTcpTestRecvWindow extends PseudoTcpTestBase +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(PseudoTCPBase.class.getName()); + /** + * Send data buffer + */ + private ByteFifoBuffer send_stream; + /** + * List which stores stream position after each write cycle + */ + private List send_position; + /** + * Receive data buffer + */ + private ByteFifoBuffer recv_stream; + /** + * List which stores stream position after each read cycle + */ + private List recv_position; + /** + * Times used for write operations scheduling + */ + private Timer writeTimer = new Timer("WriteTimer"); + /** + * Stores data length used for the test. + */ + private int testDataSize; + + public void TestTransfer(int size) + { + testDataSize = size; + long start, end, elapsed; + send_position = new ArrayList(); + recv_position = new ArrayList(); + // Create some dummy data + byte[] dummy = createDummyData(size); + send_stream = new ByteFifoBuffer(size); + send_stream.Write(dummy, size); + //Prepare the receive stream + recv_stream = new ByteFifoBuffer(size); + //Connect and wait until connected + start = PseudoTCPBase.Now(); + StartClocks(); + try + { + Connect(); + } + catch (IOException ex) + { + fail(ex.getMessage()); + } + //assert Connect() == 0; + //TODO: check assert result and fail + // Connect and wait until connected. + assert_Connected_wait(kConnectTimeoutMs); + + ScheduleWriteAction(0); + + long transferTout = MaxTransferTime(dummy.length, kMinTransferRate); + System.out.println("Timeout: " + transferTout); + boolean transfferInTime = assert_Disconnected_wait(transferTout); + elapsed = PseudoTCPBase.Now() - start; + StopClocks(); + int received = recv_stream.GetBuffered(); + assertEquals("Transfer timeout, transferred: " + received + + " required: " + dummy.length + + " elapsed: " + + elapsed + " limit: " + transferTout, + true, transfferInTime); + + assert 2 == send_position.size(); + assert 2 == recv_position.size(); + + int estimated_recv_window = EstimateReceiveWindowSize(); + + // The difference in consecutive send positions should equal the + // receive window size or match very closely. This verifies that receive + // window is open after receiver drained all the data. + int send_position_diff = send_position.get(1) - send_position.get(0); + assertTrue(estimated_recv_window - send_position_diff <= 1024); + + // Receiver drained the receive window twice.(+-2 because of window scaling) + assert ((recv_position.get(1) - 2 * estimated_recv_window) + <= getShadowedBytes(getRemoteScaleFactor())); + } + + /** + * This function calculates amount of bytes witch may introduce error to + * estimation of receive window size caused by scale factor. This is because + * data is being sent until all sent data is available in remote side's + * buffer. But because of scale factor window size 0 is reached earlier than + * expected and some data may still wait for window to open in the send + * queue. + * + * For example: m_rcv_scale == 1 and m_rcv_wnd < 2 then rcv_wnd == 0 (1 byte + * may block) m_rcv_scale == 2 and m_rcv_wnd < 4 then rcv_wnd == 0 (3 bytes + * may block) m_rcv_scale == 3 and m_rcv_wnd < 8 then rcv_wnd == 0 (7 bytes + * may block) and so on... + * + * In normal operation something would read data on remote side causing + * window to expand. + * + * + * + * + + * + * @param scaleFactor + * @return count bytes shadowed by scale actor + */ + static int getShadowedBytes(int scaleFactor) + { + return (int) (Math.pow(2, scaleFactor) - 1); + } + + /** + * Reads all data available at the buffer + * + * @throws IOException + */ + void ReadUntilIOPending() throws IOException + { + byte[] block = new byte[getRemoteTcp().getRecvBufferSize() * 2]; + int position = recv_stream.GetBuffered(); + int rcvd, total = 0; + do + { + rcvd = RemoteRecv(block, block.length); + if (rcvd > 0) + { + recv_stream.Write(block, rcvd); + total += rcvd; + position += rcvd; + } + } + while (rcvd > 0 && total != 0); + recv_position.add(position); + + // Disconnect if we have done two transfers. + if (recv_position.size() == 2) + { + Close(); + OnTcpClosed(getRemoteTcp(), null); + } + else + { + WriteData(); + } + } + + /** + * Schedules write operation with delay given in ms + * + * @param delay + */ + void ScheduleWriteAction(long delay) + { + writeTimer.schedule(new TimerTask() + { + @Override + public void run() + { + try + { + WriteData(); + } + catch (IOException ex) + { + //it will get cought by + //deafult exception handler in PseudoTcpTestBase + throw new RuntimeException(ex); + } + } + }, delay); + } + + /** + * Writes the data + * + * @throws IOException + */ + void WriteData() throws IOException + { + //writeOpCount++; + int tosend; + int sent; + int totalSent = 0; + byte[] block = new byte[getRemoteTcp().getRecvBufferSize() * 2]; + int position = testDataSize - send_stream.GetBuffered(); + synchronized (getLocalTcp()) + { + do + { + tosend = send_stream.ReadOffset(block, 0, block.length, 0); + if (tosend > 0) + { + sent = LocalSend(block, tosend); + UpdateLocalClock(); + if (sent > 0) + { + totalSent += sent; + send_stream.ConsumeReadData(sent); + position += sent; + } + else + { + logger.log(Level.FINE, "Flow Controlled"); + } + } + else + { + sent = tosend = 0; + } + } + while (sent > 0); + //position = send_stream.GetBuffered(); + + } + // Measured with precision according to window scale option used + if (totalSent - getRemoteTcp().GetAvailable() + > getShadowedBytes(getRemoteScaleFactor())) + { + //send buffer was fully filled + //waits until it will be received by remote peer + while (totalSent - getRemoteTcp().GetAvailable() + > getShadowedBytes(getRemoteScaleFactor()) + && !getRemoteTcp().isReceiveBufferFull()) + { + try + { + Thread.sleep(50); + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, + "Waiting... sent: " + totalSent + " avail: " + + getRemoteTcp().GetAvailable() + " buffered not sent: " + + getLocalTcp().GetBytesBufferedNotSent() + + " isFull? " + getRemoteTcp().isReceiveBufferFull()); + } + } + catch (InterruptedException ex) + { + throw new RuntimeException(ex); + } + } + } + send_position.add(position); + writeTimer.schedule(new TimerTask() + { + @Override + public void run() + { + try + { + ReadUntilIOPending(); + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + } + }, 10); + } + + /** + * + * @return estimated receive window size + */ + int EstimateReceiveWindowSize() + { + return recv_position.get(0); + } + + /** + * + * @return estimated send window size + */ + int EstimateSendWindowSize() + { + return send_position.get(0); + } + + @Override + public void OnTcpReadable(PseudoTCPBase tcp) + { + } + + @Override + public void OnTcpWriteable(PseudoTCPBase tcp) + { + } + + void SetLocalOptSndBuf(int len) + { + getLocalTcp().SetOption(Option.OPT_SNDBUF, len); + } + + int getRemoteScaleFactor() + { + return getRemoteTcp().getM_rwnd_scale(); + } + + public void testGetShadowedBytes() + { + assert (PseudoTcpTestRecvWindow.getShadowedBytes(0) == 0); + assert (PseudoTcpTestRecvWindow.getShadowedBytes(1) == 1); + assert (PseudoTcpTestRecvWindow.getShadowedBytes(2) == 3); + assert (PseudoTcpTestRecvWindow.getShadowedBytes(3) == 7); + assert (PseudoTcpTestRecvWindow.getShadowedBytes(4) == 15); + assert (PseudoTcpTestRecvWindow.getShadowedBytes(11) == 2047); + assert (PseudoTcpTestRecvWindow.getShadowedBytes(14) == 16383); + } + + /** + * Test that receive window expands and contract correctly. + */ + public void testReceiveWindow() + { + //logger.log(Level.INFO, "Test receive window"); + PseudoTcpTestRecvWindow test = new PseudoTcpTestRecvWindow(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptNagling(false); + test.SetOptAckDelay(0); + test.TestTransfer(1024 * 1000); + } + + /** + * Test setting send window size to a very small value. + */ + public void testSetVerySmallSendWindowSize() + { + //TODO: finish test + logger.log(Level.INFO, "Test very small receive window"); + PseudoTcpTestRecvWindow test = new PseudoTcpTestRecvWindow(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptNagling(false); + test.SetOptAckDelay(0); + test.SetOptSndBuf(900); + test.TestTransfer(1024 * 1000); + assertEquals(900, test.EstimateSendWindowSize()); + } + + /** + * Test setting receive window size to a value other than default. + */ + public void testSetReceiveWindowSize() + { + //logger.log(Level.INFO, "Test set receive window size"); + PseudoTcpTestRecvWindow test = new PseudoTcpTestRecvWindow(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetOptNagling(false); + test.SetOptAckDelay(0); + int wndSize = 300000; + test.SetRemoteOptRcvBuf(wndSize); + test.SetLocalOptSndBuf(wndSize); + int wndScale = test.getRemoteScaleFactor(); + //logger.log(Level.INFO, "Using scale factor: {0}", wndScale); + test.TestTransfer(1024 * 3000); + //beacuse there may be situations + //when 1 byte may be waiting in send queue + //before + //scaling factor == 1 not allows to determine exact window size (+-1) + assert (wndSize - test.EstimateReceiveWindowSize() + <= PseudoTcpTestRecvWindow.getShadowedBytes(wndScale)); + } + + /* + * Test sending data with mismatched MTUs. We should detect this and reduce + * // our packet size accordingly. // TODO: This doesn't actually work right + * now. The current code // doesn't detect if the MTU is set too high on + * either side. TEST_F(PseudoTcpTest, TestSendWithMismatchedMtus) { + * SetLocalMtu(1500); SetRemoteMtu(1280); TestTransfer(1000000); } + */ +} diff --git a/test/org/ice4j/pseudotcp/PseudoTcpTestTransfer.java b/test/org/ice4j/pseudotcp/PseudoTcpTestTransfer.java new file mode 100755 index 0000000..a38213a --- /dev/null +++ b/test/org/ice4j/pseudotcp/PseudoTcpTestTransfer.java @@ -0,0 +1,445 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp; + +import java.io.*; +import java.util.logging.*; +import org.ice4j.pseudotcp.util.*; +import static org.junit.Assert.*; +import org.junit.*; + +/** + * Implements one way transfer test + * + * @author Pawel Domas + */ +public class PseudoTcpTestTransfer extends PseudoTcpTestBase +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(PseudoTCPBase.class.getName()); + + public PseudoTcpTestTransfer() + { + super(); + } + /** + * The send data + */ + private byte[] sendData; + /** + * Send stream size + */ + private int sendStreamSize; + /** + * Total bytes sent counter + */ + private int totalSent; + /** + * Receive stream + */ + private ByteArrayOutputStream recvStream; + + /** + * Transfers the data of size bytes + * + * @param size + */ + public void TestTransfer(int size) + { + long start, elapsed; + int received; + // Create some dummy data to send + sendData = createDummyData(size); + sendStreamSize = size; + + // Prepare the receive stream. + recvStream = new ByteArrayOutputStream(size); + // Connect and wait until connected. + start = PseudoTCPBase.Now(); + StartClocks(); + try + { + Connect(); + //assertEquals(0, Connect()); + } + catch (IOException ex) + { + fail(ex.getMessage()); + } + + assert_Connected_wait(kConnectTimeoutMs); + // Sending will start from OnTcpWriteable and complete when all data has + // been received. + long transferTout = MaxTransferTime(sendData.length, kMinTransferRate); + boolean transfferInTime = assert_Disconnected_wait(transferTout); + elapsed = PseudoTCPBase.Now() - start; + StopClocks(); + received = recvStream.size(); + assertEquals("Transfer timeout, transferred: " + received + + " required: " + sendData.length + + " elapsed: " + + elapsed + " limit: " + transferTout, + true, transfferInTime); + + // Ensure we closed down OK and we got the right data. + assertEquals(size, received); + byte[] recvdArray = recvStream.toByteArray(); + assertArrayEquals(sendData, recvdArray); + + logger.log(Level.INFO, + "Transferred " + received + " bytes in " + elapsed + + " ms (" + (size * 8 / elapsed) + " Kbps"); + + } + + /** + * Reads all data available at remote peer's buffer + * + * @throws IOException + */ + void ReadData() throws IOException + { + byte[] block = new byte[kBlockSize]; + int rcvd; + do + { + rcvd = RemoteRecv(block, block.length); + UpdateRemoteClock(); + if (rcvd != -1) + { + recvStream.write(block, 0, rcvd); + } + } + while (rcvd > 0); + } + + /** + * Writes the data until there's space available + * + * @return true if there's no more data left to write + * @throws IOException + */ + boolean WriteData() throws IOException + { + int tosend; + int sent; + byte[] block = new byte[kBlockSize]; + do + { + tosend = Math.min(sendStreamSize - totalSent, block.length); + System.arraycopy(sendData, totalSent, block, 0, tosend); + if (tosend > 0) + { + sent = LocalSend(block, tosend); + UpdateLocalClock(); + if (sent != -1) + { + totalSent += sent; + } + else + { + logger.log(Level.FINE, "Flow Controlled"); + } + } + else + { + sent = tosend = 0; + } + } + while (sent > 0); + + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, "Sent: " + totalSent + + " remaining: " + (sendStreamSize - totalSent)); + } + + return tosend == 0; + } + + /** + * Catches TCP readable event for remote peer and reads the data. When total + * read count equals send data size the test is finished. + * + * @param tcp + */ + @Override + public void OnTcpReadable(PseudoTCPBase tcp) + { + if (tcp == getRemoteTcp()) + { + try + { + ReadData(); + // TODO: OnTcpClosed() is currently only notified on error - + // there is no on-the-wire equivalent of TCP FIN. + // So we fake the notification when all the data has been read. + int received, required; + received = recvStream.size(); + required = sendStreamSize; + if (logger.isLoggable(Level.FINER)) + { + logger.log(Level.FINER, "Receivied: " + received + + " required: " + required); + } + if (received == required) + { + OnTcpClosed(getRemoteTcp(), null); + } + } + catch (IOException ex) + { + throw new RuntimeException(ex); + } + } + } + + /** + * Catches on TCP writeable event for local peer. Writes all data and closes + * the stream + * + * @param tcp + */ + @Override + public void OnTcpWriteable(PseudoTCPBase tcp) + { + if (tcp == getLocalTcp()) + { + // Write bytes from the send stream when we can. + // Shut down when we've sent everything. + logger.log(Level.FINER, "Flow Control Lifted"); + try + { + if (WriteData()) + { + Close(); + } + } + catch (IOException ex) + { + ex.printStackTrace(); + fail(ex.getMessage()); + } + } + } + + /** + * Basic end-to-end data transfer tests Test the normal case of sending data + * from one side to the other. + */ + public void testSend() + { + //logger.log(Level.INFO, "Test send"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.TestTransfer(1000000); + } + + /** + * Test sending data with a 50 ms RTT. Transmission should take longer due + * to a slower ramp-up in send rate. + */ + public void testSendWithDelay() + { + //logger.log(Level.INFO, "Test send with delay"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetDelay(50); + test.TestTransfer(1000000); + } + + /** + * Test sending data with packet loss. Transmission should take much longer + * due to send back-off when loss occurs. + */ + public void testSendWithLoss() + { + //logger.log(Level.INFO, "Test send with loss"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetLoss(10); + test.TestTransfer(100000); // less data so test runs faster + } + + /** + * Test sending data with a 50 ms RTT and 10% packet loss. Transmission + * should take much longer due to send back-off and slower detection of + * loss. + */ + public void testSendWithDelayAndLoss() + { + //logger.log(Level.INFO, "Test send with delay and loss"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetDelay(50); + test.SetLoss(10); + test.TestTransfer(100000); // less data so test runs faster + } + + /** + * Test sending data with 10% packet loss and Nagling disabled. Transmission + * should take about the same time as with Nagling enabled. + */ + public void testSendWithLossAndOptNaglingOff() + { + //logger.log(Level.INFO, "Test send with loss and OptNagling off"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetLoss(10); + test.SetOptNagling(false); + test.TestTransfer(100000); // less data so test runs faster + } + + /** + * Test sending data with 10% packet loss and Delayed ACK disabled. + * Transmission should be slightly faster than with it enabled. + */ + public void testSendWithLossAndOptAckDelayOff() + { + //logger.log(Level.INFO, "Test send with loss and OptAckDelay off"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetLoss(10); + test.SetOptAckDelay(0); + test.TestTransfer(100000); + } + + /** + * Test sending data with 50ms delay and Nagling disabled. + */ + public void testSendWithDelayAndOptNaglingOff() + { + //logger.log(Level.INFO, "Test send with delay and OptNagling off"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetDelay(50); + test.SetOptNagling(false); + test.TestTransfer(100000); // less data so test runs faster + } + + /** + * Test sending data with 50ms delay and Delayed ACK disabled. + */ + public void testSendWithDelayAndOptAckDelayOff() + { + //logger.log(Level.INFO, "Test send with delay and OptAckDelay off"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetDelay(50); + test.SetOptAckDelay(0); + test.TestTransfer(100000); // less data so test runs faster + } + + /** + * Test a large receive buffer with a sender that doesn't support scaling. + */ + public void testSendRemoteNoWindowScale() + { + //logger.log(Level.INFO, "Test send - remote no window scale"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetLocalOptRcvBuf(100000); + test.DisableRemoteWindowScale(); + test.TestTransfer(1000000); + } + + /** + * Test a large sender-side receive buffer with a receiver that doesn't + * support scaling. + */ + public void testSendLocalNoWindowScale() + { + //logger.log(Level.INFO, "Test send - local no window scale"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetRemoteOptRcvBuf(100000); + test.DisableLocalWindowScale(); + test.TestTransfer(1000000); + } + + /** + * Test when both sides use window scaling. + */ + public void testSendBothUseWindowScale() + { + //logger.log(Level.INFO, "Test send - both use window scale"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetRemoteOptRcvBuf(100000); + test.SetLocalOptRcvBuf(100000); + test.TestTransfer(1000000); + } + + /** + * Test using a large window scale value. + */ + public void testSendLargeInFlight() + { + //logger.log(Level.INFO, "Test send large in flight"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetRemoteOptRcvBuf(100000); + test.SetLocalOptRcvBuf(100000); + test.SetOptSndBuf(150000); + test.TestTransfer(1000000); + } + + public void testSendBothUseLargeWindowScale() + { + //logger.log(Level.INFO, "Test send both use large window scale"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetRemoteOptRcvBuf(1000000); + test.SetLocalOptRcvBuf(1000000); + test.TestTransfer(10000000); + } + + /** + * Test using a small receive buffer. + */ + public void testSendSmallReceiveBuffer() + { + //logger.log(Level.INFO, "Test send small receive buffer"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetRemoteOptRcvBuf(10000); + test.SetLocalOptRcvBuf(10000); + test.TestTransfer(1000000); + } + + /** + * Test using a very small receive buffer. + */ + public void testSendVerySmallReceiveBuffer() + { + //logger.log(Level.INFO, "Test send very small receive buffer"); + PseudoTcpTestTransfer test = new PseudoTcpTestTransfer(); + test.SetLocalMtu(1500); + test.SetRemoteMtu(1500); + test.SetRemoteOptRcvBuf(100); + test.SetLocalOptRcvBuf(100); + test.TestTransfer(100000); + } +} diff --git a/test/org/ice4j/pseudotcp/util/ByteFifoBufferTest.java b/test/org/ice4j/pseudotcp/util/ByteFifoBufferTest.java new file mode 100755 index 0000000..1b627a9 --- /dev/null +++ b/test/org/ice4j/pseudotcp/util/ByteFifoBufferTest.java @@ -0,0 +1,339 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * Maintained by the Jitsi community (https://jitsi.org). + * + * Distributable under LGPL license. + * See terms of license at gnu.org. + */ +package org.ice4j.pseudotcp.util; + +import junit.framework.*; +import java.nio.*; +import java.util.*; +import static org.junit.Assert.*; +import org.junit.*; + +/** + * + * @author Pawel Domas + */ +public class ByteFifoBufferTest extends TestCase +{ + public ByteFifoBufferTest() + { + } + + /** + * Test of Length method, of class ByteFifoBuffer. + */ + public void testLength() + { + //System.out.println("Length"); + int expResult = 1000; + ByteFifoBuffer instance = new ByteFifoBuffer(expResult); + assertEquals(expResult, instance.Length()); + int wSize = 100; + instance.Write(getWData(wSize), wSize); + int result = instance.Length(); + assertEquals(expResult, result); + } + + /** + * Test of Read method, of class ByteFifoBuffer. + */ + public void testRead() + { + //System.out.println("Read"); + int count = 1024; + byte[] wData = getWData(count); + ByteFifoBuffer instance = new ByteFifoBuffer(count); + instance.Write(wData, count); + + byte[] readBuff = new byte[count]; + int expResult = count; + int result = instance.Read(readBuff, count); + assertEquals(expResult, result); + assertArrayEquals(wData, readBuff); + + } + + /** + * return some random array + * @param count array size + * @return + */ + private byte[] getWData(int count) + { + Random r = new Random(); + byte[] res = new byte[count]; + r.nextBytes(res); + return res; + } + + /** + * Test of GetWriteRemaining method, of class ByteFifoBuffer. + */ + public void testGetWriteRemaining() + { + //System.out.println("GetWriteRemaining"); + int len = 100; + ByteFifoBuffer instance = new ByteFifoBuffer(len); + int expResult = len; + int result = instance.GetWriteRemaining(); + assertEquals(expResult, result); + + int w_size = 23; + byte[] w_data = getWData(w_size); + instance.Write(w_data, w_size); + + expResult = len - w_size; + result = instance.GetWriteRemaining(); + assertEquals(expResult, result); + } + + /** + * Test of GetBuffered method, of class ByteFifoBuffer. + */ + public void testGetBuffered() + { + //System.out.println("GetBuffered"); + int len = 1000; + ByteFifoBuffer instance = new ByteFifoBuffer(len); + int w_len = 100; + byte[] w_data = getWData(w_len); + + instance.Write(w_data, w_len); + + int expResult = w_len; + int result = instance.GetBuffered(); + assertEquals(expResult, result); + int consume = 5; + expResult = w_len + consume; + instance.ConsumeWriteBuffer(consume); + result = instance.GetBuffered(); + assertEquals(expResult, result); + } + + /** + * Test of Write method, of class ByteFifoBuffer. + */ + public void testWrite() + { + //System.out.println("Write"); + int len = 2048; + byte[] data = getWData(len); + ByteFifoBuffer instance = new ByteFifoBuffer(len); + int expResult = len; + int result = instance.Write(data, len); + assertEquals(expResult, result); + + byte[] read = new byte[len]; + int readCount = instance.Read(read, len); + assertEquals(result, readCount); + assertArrayEquals(data, read); + } + + + public void testWriteWithOffset() + { + //System.out.println("Write"); + int len = 2048; + byte[] data = getWData(len); + ByteFifoBuffer instance = new ByteFifoBuffer(len); + int expResult = len/2; + int result = instance.Write(data,0, len/2); + assertEquals(expResult, result); + result = instance.Write(data, len/2, len/2); + assertEquals(expResult, result); + + byte[] read = new byte[len]; + int readCount = instance.Read(read, len); + assertEquals(len, readCount); + assertArrayEquals(data, read); + } + + + /** + * Test of ConsumeWriteBuffer method, of class ByteFifoBuffer. + */ + public void testConsumeWriteBuffer() + { + //System.out.println("ConsumeWriteBuffer"); + int len = 100; + ByteFifoBuffer instance = new ByteFifoBuffer(len); + instance.ConsumeWriteBuffer(len / 2); + instance.ConsumeWriteBuffer(len / 2); + try + { + instance.ConsumeWriteBuffer(1); + fail(); + } + catch (BufferOverflowException e) + { + } + + instance = new ByteFifoBuffer(len); + instance.ConsumeWriteBuffer(95); + instance.ConsumeReadData(40); + instance.ConsumeWriteBuffer(20); + + } + + /** + * Test of SetCapacity method, of class ByteFifoBuffer. + */ + public void testSetCapacity() + { + //System.out.println("SetCapacity"); + int old_size = 100; + int new_size = 200; + ByteFifoBuffer instance = new ByteFifoBuffer(old_size); + boolean expResult = true; + instance.Write(getWData(old_size), old_size); + boolean result = instance.SetCapacity(new_size); + assertEquals(expResult, result); + + expResult = false; + instance.ResetWritePosition(); + instance.Write(getWData(new_size), new_size); + result = instance.SetCapacity(old_size); + assertEquals(expResult, result); + + /* + * int write_size = old_size; instance = new + * ByteFifoBuffer(old_size); byte[] written = getWData(write_size); + * assertEquals(write_size, instance.Write(written, write_size)); + * instance.SetCapacity(new_size); byte[] read = new byte[write_size]; + * instance.Read(read, write_size); assertArrayEquals(written, read); + */ + } + + /** + * Test of ConsumeReadData method, of class ByteFifoBuffer. + */ + public void testConsumeReadData() + { + //System.out.println("ConsumeReadData"); + int lCount = 100; + ByteFifoBuffer instance = new ByteFifoBuffer(lCount); + instance.Write(getWData(lCount), lCount); + instance.ConsumeReadData(lCount / 2); + instance.ConsumeReadData(lCount / 2); + try + { + instance.ConsumeReadData(1); + fail(); + } + catch (BufferUnderflowException e) + { + } + + } + + /** + * Test of ReadOffset method, of class ByteFifoBuffer. + */ + public void testReadOffset() + { + //System.out.println("ReadOffset"); + int dst_buff_offset = 0; + int len = 100; + byte[] src_buff = getWData(len); + byte[] dst_buff = new byte[len]; + int offset = 0; + ByteFifoBuffer instance = new ByteFifoBuffer(len); + int expResult = len; + instance.Write(src_buff, len); + int result = instance.ReadOffset(dst_buff, dst_buff_offset, len, offset); + assertEquals(expResult, result); + assertArrayEquals(dst_buff, src_buff); + + } + + /** + * Test of WriteOffset method, of class ByteFifoBuffer. + */ + public void testWriteOffset() + { + //System.out.println("WriteOffset"); + int len = 200; + int dataLen = 100; + byte[] srcData = getWData(dataLen); + byte[] data = new byte[dataLen]; + int nOffset = 10; + ByteFifoBuffer instance = new ByteFifoBuffer(len); + int result = instance.WriteOffset(srcData, dataLen, nOffset); + int readCount = instance.ReadOffset(data, 0, dataLen, nOffset); + assertEquals(result, readCount); + assertArrayEquals(srcData, data); + + + byte[] halfFilled = new byte[dataLen * 2]; + System.arraycopy(srcData, 0, halfFilled, dataLen, dataLen); + byte[] halfFilledRead = new byte[dataLen * 2]; + instance.ReadOffset(halfFilledRead, dataLen, dataLen, nOffset); + assertArrayEquals(halfFilled, halfFilledRead); + + //case when w_pos+offset exceeds current backing array length + instance = new ByteFifoBuffer(len); + instance.Write(srcData, dataLen); + instance.Write(srcData, dataLen / 2);//current writePos = 150 + instance.Read(data, dataLen);// curretn readPos = 100 + instance.WriteOffset(srcData, dataLen, 50); + + instance = new ByteFifoBuffer(61440); + instance.WriteOffset(getWData(1384), 1384, 31832); + + } + + public void testWriteReadWriteRead() + { + int len = 2000; + ByteFifoBuffer instance = new ByteFifoBuffer(len); + byte[] wrData = getWData(len * 2); + int written = 0; + byte[] readBuff = new byte[wrData.length]; + int read = 0; + do + { + int wrRemaining = instance.GetWriteRemaining(); + if (wrRemaining > 0 && written < wrData.length) + { + int wrCount = instance.WriteOffset(wrData, wrRemaining, 0); + instance.ConsumeWriteBuffer(wrCount); + written += wrCount; + } + int readAvailable = instance.GetBuffered(); + if (readAvailable > 0) + { + int rCount = instance.ReadOffset(readBuff, read, readAvailable, 0); + instance.ConsumeReadData(rCount); + read += rCount; + } + } + while ((read != wrData.length) || (written != wrData.length)); + } + + public void testSomeMultiTest() + { + int Alen = 16; + int Blen = 32; + int Clen = 64; + int Dlen = 256; + int len = Alen + Blen + Clen + Dlen; + ByteFifoBuffer fifo = new ByteFifoBuffer(len); + byte[] A = getWData(Alen); + byte[] B = getWData(Blen); + byte[] C = getWData(Clen); + byte[] D = getWData(Dlen); + byte[] Aread = getWData(Alen); + byte[] Bread = getWData(Blen); + byte[] Cread = getWData(Clen); + byte[] Dread = getWData(Dlen); + + fifo.WriteOffset(A, Alen, 0); + fifo.ConsumeWriteBuffer(Alen); + fifo.ReadOffset(Aread, 0, Alen, 0); + assertArrayEquals(A, Aread); + } +} diff --git a/test/test/Ice.java b/test/test/Ice.java index d6d2714..fedfd7e 100644 --- a/test/test/Ice.java +++ b/test/test/Ice.java @@ -176,7 +176,7 @@ public class Ice * local * @param remotePeer a reference to what we'll pretend to be a remote agent. */ - private static void transferRemoteCandidates(Agent localAgent, + static void transferRemoteCandidates(Agent localAgent, Agent remotePeer) { List streams = localAgent.getStreams(); diff --git a/test/test/IcePseudoTcp.java b/test/test/IcePseudoTcp.java new file mode 100755 index 0000000..4c4aa5e --- /dev/null +++ b/test/test/IcePseudoTcp.java @@ -0,0 +1,488 @@ +/* + * 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 test; + +import java.beans.*; +import java.io.*; +import java.net.*; +import java.util.*; +import java.util.logging.*; +import org.ice4j.*; +import org.ice4j.ice.*; +import org.ice4j.ice.harvest.*; +import org.ice4j.security.*; +import org.ice4j.pseudotcp.*; + +/** + * Sample program which first uses ICE to discover UDP connectivity. After that + * selected cadidates are used by "remote" and "local" pseudoTCP peers to + * transfer some test data. + * + * @author Pawel Domas + */ +public class IcePseudoTcp +{ + /** + * The logger. + */ + private static final Logger logger = + Logger.getLogger(CheckList.class.getName()); + private static long startTime; + /** + * Local job thread variable + */ + private static LocalPseudoTcpJob localJob = null; + /** + * Remote job thread variable + */ + private static RemotePseudoTcpJob remoteJob = null; + /** + * Test data size + */ + private static final int TEST_BYTES_COUNT = 1000000; + /** + * Flag inidcates if STUN should be used + */ + private static final boolean USE_STUN = true; + /** + * Flag inidcates if TURN should be used + */ + private static final boolean USE_TURN = true; + /** + * Monitor object used to wait for remote agent to finish it's job + */ + private static final Object remoteAgentMonitor = new Object(); + /** + * Monitor object used to wait for local agent to finish it's job + */ + private static final Object localAgentMonitor = new Object(); + /** + * Timeout for ICE discovery + */ + private static long agentJobTimeout = 15000; + + protected static Agent createAgent(int pTcpPort) + throws Throwable + { + Agent agent = new Agent(); + // STUN + if (USE_STUN) + { + StunCandidateHarvester stunHarv = new StunCandidateHarvester( + new TransportAddress("sip-communicator.net", + 3478, Transport.UDP)); + StunCandidateHarvester stun6Harv = new StunCandidateHarvester( + new TransportAddress("ipv6.sip-communicator.net", + 3478, Transport.UDP)); + + agent.addCandidateHarvester(stunHarv); + agent.addCandidateHarvester(stun6Harv); + } + // TURN + if (USE_TURN) + { + String[] hostnames = new String[] + { + "130.79.90.150", + "2001:660:4701:1001:230:5ff:fe1a:805f" + }; + int port = 3478; + LongTermCredential longTermCredential = new LongTermCredential( + "guest", "anonymouspower!!"); + + for (String hostname : hostnames) + { + agent.addCandidateHarvester(new TurnCandidateHarvester( + new TransportAddress(hostname, port, + Transport.UDP), longTermCredential)); + } + } + //STREAM + createStream(pTcpPort, "data", agent); + + return agent; + } + + private static IceMediaStream createStream(int pTcpPort, + String streamName, + Agent agent) + throws Throwable + { + IceMediaStream stream = agent.createMediaStream(streamName); + + long startTime = System.currentTimeMillis(); + + //udp component + agent.createComponent( + stream, Transport.UDP, pTcpPort, pTcpPort, pTcpPort + 100); + + long endTime = System.currentTimeMillis(); + logger.log(Level.INFO, + "UDP Component created in " + (endTime - startTime) + " ms"); + startTime = endTime; + + return stream; + } + + private static final class LocalIceProcessingListener + implements PropertyChangeListener + { + /** + * System.exit()s as soon as ICE processing enters a final state. + * + * @param evt the {@link PropertyChangeEvent} containing the old and new + * states of ICE processing. + */ + public void propertyChange(PropertyChangeEvent evt) + { + long processingEndTime = System.currentTimeMillis(); + + Object iceProcessingState = evt.getNewValue(); + + logger.log(Level.INFO, + "Local agent entered the " + iceProcessingState + " state."); + if (iceProcessingState == IceProcessingState.COMPLETED) + { + logger.log(Level.INFO, + "Local - Total ICE processing time: " + + (processingEndTime - startTime) + "ms"); + Agent agent = (Agent) evt.getSource(); + logger.log(Level.INFO, "Local: Create pseudo tcp stream"); + IceMediaStream dataStream = agent.getStream("data"); + Component udpComponent = dataStream.getComponents().get(0); + CandidatePair selectedPair = udpComponent.getSelectedPair(); + if (selectedPair != null) + { + LocalCandidate localCandidate = selectedPair.getLocalCandidate(); + Candidate remoteCandidate = selectedPair.getRemoteCandidate(); + logger.log(Level.INFO, "Local: " + localCandidate); + logger.log(Level.INFO, "Remote: " + remoteCandidate); + try + { + localJob = new LocalPseudoTcpJob( + localCandidate.getTransportAddress()); + } + catch (UnknownHostException ex) + { + logger.log(Level.SEVERE, + "Error while trying to create" + + " local pseudotcp thread " + ex); + } + } + else + { + logger.log(Level.INFO, "Failed to select any candidate pair"); + } + } + else + { + if (iceProcessingState == IceProcessingState.TERMINATED + || iceProcessingState == IceProcessingState.FAILED) + { + /* + * Though the process will be instructed to die, demonstrate + * that Agent instances are to be explicitly prepared for + * garbage collection. + */ + ((Agent) evt.getSource()).free(); + + if (localJob != null + && iceProcessingState == IceProcessingState.TERMINATED) + { + localJob.start(); + synchronized (localAgentMonitor) + { + localAgentMonitor.notifyAll(); + } + } + else + { + System.exit(0); + } + } + } + } + } + + private static final class RemoteIceProcessingListener + implements PropertyChangeListener + { + /** + * System.exit()s as soon as ICE processing enters a final state. + * + * @param evt the {@link PropertyChangeEvent} containing the old and new + * states of ICE processing. + */ + public void propertyChange(PropertyChangeEvent evt) + { + long processingEndTime = System.currentTimeMillis(); + + Object iceProcessingState = evt.getNewValue(); + + logger.log(Level.INFO, + "Remote agent entered the " + iceProcessingState + " state."); + if (iceProcessingState == IceProcessingState.COMPLETED) + { + logger.log(Level.INFO, + "Remote: Total ICE processing time: " + + (processingEndTime - startTime) + " ms"); + Agent agent = (Agent) evt.getSource(); + + logger.log(Level.INFO, "Remote: Create pseudo tcp stream"); + IceMediaStream dataStream = agent.getStream("data"); + Component udpComponent = dataStream.getComponents().get(0); + CandidatePair usedPair = udpComponent.getSelectedPair(); + if (usedPair != null) + { + LocalCandidate localCandidate = usedPair.getLocalCandidate(); + Candidate remoteCandidate = usedPair.getRemoteCandidate(); + logger.log(Level.INFO, + "Remote: Local address " + localCandidate); + logger.log(Level.INFO, + "Remote: Peer address " + remoteCandidate); + try + { + remoteJob = new RemotePseudoTcpJob( + localCandidate.getTransportAddress(), + remoteCandidate.getTransportAddress()); + } + catch (UnknownHostException ex) + { + logger.log(Level.SEVERE, + "Error while trying to create" + + " remote pseudotcp thread " + ex); + } + } + else + { + logger.log(Level.SEVERE, + "Remote: Failed to select any candidate pair"); + } + } + else + { + if (iceProcessingState == IceProcessingState.TERMINATED + || iceProcessingState == IceProcessingState.FAILED) + { + /* + * Though the process will be instructed to die, demonstrate + * that Agent instances are to be explicitly prepared for + * garbage collection. + */ + ((Agent) evt.getSource()).free(); + if (remoteJob != null + && iceProcessingState == IceProcessingState.TERMINATED) + { + remoteJob.start(); + synchronized (remoteAgentMonitor) + { + remoteAgentMonitor.notifyAll(); + } + } + else + { + System.exit(0); + } + } + } + } + } + + public static void main(String[] args) throws Throwable + { + startTime = System.currentTimeMillis(); + + int localPort = 7999; + int remotePort = 6000; + + Agent localAgent = createAgent(localPort); + localAgent.setNominationStrategy( + NominationStrategy.NOMINATE_HIGHEST_PRIO); + Agent remotePeer = + createAgent(remotePort); + + localAgent.addStateChangeListener(new IcePseudoTcp.LocalIceProcessingListener()); + remotePeer.addStateChangeListener(new IcePseudoTcp.RemoteIceProcessingListener()); + + //let them fight ... fights forge character. + localAgent.setControlling(true); + remotePeer.setControlling(false); + + long endTime = System.currentTimeMillis(); + + Ice.transferRemoteCandidates(localAgent, remotePeer); + for (IceMediaStream stream : localAgent.getStreams()) + { + stream.setRemoteUfrag(remotePeer.getLocalUfrag()); + stream.setRemotePassword(remotePeer.getLocalPassword()); + } + + Ice.transferRemoteCandidates(remotePeer, localAgent); + + for (IceMediaStream stream : remotePeer.getStreams()) + { + stream.setRemoteUfrag(localAgent.getLocalUfrag()); + stream.setRemotePassword(localAgent.getLocalPassword()); + } + + logger.log(Level.INFO, "Total candidate gathering time: {0} ms", + (endTime - startTime)); + logger.log(Level.INFO, "LocalAgent: {0}", + localAgent); + + localAgent.startConnectivityEstablishment(); + + //if (START_CONNECTIVITY_ESTABLISHMENT_OF_REMOTE_PEER) + remotePeer.startConnectivityEstablishment(); + + + IceMediaStream dataStream = localAgent.getStream("data"); + + if (dataStream != null) + { + logger.log(Level.INFO, + "Local data clist:" + dataStream.getCheckList()); + } + //wait for one of the agents to complete it's job + synchronized (remoteAgentMonitor) + { + remoteAgentMonitor.wait(agentJobTimeout); + } + if (remoteJob != null) + { + logger.log(Level.FINEST, "Remote thread join started"); + remoteJob.join(); + logger.log(Level.FINEST, "Remote thread joined"); + } + if (localJob != null) + { + logger.log(Level.FINEST, "Local thread join started"); + localJob.join(); + logger.log(Level.FINEST, "Local thread joined"); + } + System.exit(0); + } + + private static class LocalPseudoTcpJob extends Thread implements Runnable + { + private InetSocketAddress transportAddr; + + public LocalPseudoTcpJob(InetSocketAddress transportAddr) + throws UnknownHostException + { + //This is required because of can not bind exception on Linux + InetAddress locIP = InetAddress.getByName( + transportAddr.getAddress().getHostAddress()); + this.transportAddr = new InetSocketAddress(locIP, transportAddr.getPort()); + } + + @Override + public void run() + { + logger.log(Level.FINEST, "Local pseudotcp worker started"); + try + { + logger.log(Level.INFO, + "Local pseudotcp is using: " + transportAddr); + PseudoTcpSocket socket = new PseudoTcpSocket( + 0, new DatagramSocket(transportAddr)); + socket.Accept(5000); + byte[] buffer = new byte[TEST_BYTES_COUNT]; + int read = 0; + while (read != TEST_BYTES_COUNT) + { + read += socket.getInputStream().read(buffer); + logger.log(Level.FINEST, "Local job read: " + read); + } + //TODO: close when all received data is acked + //socket.Close(); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + logger.log(Level.FINEST, "Local pseudotcp worker finished"); + } + } + + private static class RemotePseudoTcpJob extends Thread implements Runnable + { + private InetSocketAddress transportAddr; + private InetSocketAddress peerAddr; + + public RemotePseudoTcpJob(InetSocketAddress transportAddr, + InetSocketAddress peerAddr) + throws UnknownHostException + { + //This is required because of can not bind exception on Linux + //InetAddress locIP = InetAddress.getByName("127.0.0.1"); + InetAddress locIP = InetAddress.getByName( + transportAddr.getAddress().getHostAddress()); + this.transportAddr = new InetSocketAddress(locIP, transportAddr.getPort()); + this.peerAddr = peerAddr; + } + + @Override + public void run() + { + logger.log(Level.FINEST, "Remote pseudotcp worker started"); + try + { + logger.log(Level.INFO, + "Remote pseudotcp is using: " + transportAddr); + PseudoTcpSocket socket = new PseudoTcpSocket( + 0, new DatagramSocket(transportAddr)); + long start, end; + start = System.currentTimeMillis(); + socket.Connect(peerAddr, 5000); + byte[] buffer = new byte[TEST_BYTES_COUNT]; + socket.getOutputStream().write(buffer); + socket.getOutputStream().flush(); + socket.Close(); + end = System.currentTimeMillis(); + logger.log(Level.INFO, + "Transferred " + TEST_BYTES_COUNT + + " bytes in " + ((end - start) / 1000) + " sec"); + } + catch (IOException e) + { + throw new RuntimeException(e); + } + logger.log(Level.FINEST, "Remote pseudotcp worker finished"); + } + } + + /* + * static void TransferWithTcp() { try { final ServerSocket server = new + * ServerSocket(); int testPort = 60000; server.bind(new + * InetSocketAddress(testPort)); final Socket client = new Socket(); new + * Thread(new RunnableImpl(client, server)).start(); long start = + * System.currentTimeMillis(); byte[] serverBuffer = new + * byte[TEST_BYTES_COUNT]; Socket clientAtServer = server.accept(); int + * received = 0; do { received += + * clientAtServer.getInputStream().read(serverBuffer); } while (received < + * serverBuffer.length); long end = System.currentTimeMillis(); + * logger.log(Level.INFO, "Transferred " + received + " bytes in " + ((end - + * start) / 1000) + " sec"); } catch (IOException ex) { + * Logger.getLogger(IcePseudoTcp.class.getName()).log(Level.SEVERE, null, + * ex); } + * + * } + * + * static class RunnableImpl implements Runnable { private final Socket + * client; private final ServerSocket server; + * + * public RunnableImpl(Socket client, ServerSocket server) { this.client = + * client; this.server = server; } + * + * public void run() { try { client.connect(server.getLocalSocketAddress()); + * client.getOutputStream().write(new byte[TEST_BYTES_COUNT]); } catch + * (IOException ex) { + * Logger.getLogger(IcePseudoTcp.class.getName()).log(Level.SEVERE, null, + * ex); } } } + */ +}