5.5.1 - Binding to Sctp header

The Sctp header was introduced in version 1.4 (1.4.r1370 to be specific). The protocol header uses a slightly different method for breaking down Sctp data. Sctp protocol is made up of a fixed-length header followed by 1 or more "Chunks". Most of the chunks are control information carriers about the Sctp connection itself with the esception of "data-chunk".

The jNetPcap breaks down the Scp packets in the following ways. The fixed-length Sctp header followed by one or more Sctp-Chunk headers. The important thing to note is that these are not "sub-headers" but top-level headers.

Here is an example of a Sctp packet. The output is acquired using the following command on a Sctp packet: System.out.println(packet.getState().toDebugString()) which will printout the state information about the packet and all its headers:

JPacket.State#029   : [         Protocol(ID/Flag) | Start | Prefix | Header | Gap | Payload | Postfix ]
JPacket.State#029[0]: [         ETHERNET( 1/0800) |     0 |      0 |     14 |   0 |     356 |       0 ]
JPacket.State#029[1]: [              IP4( 2/0800) |    14 |      0 |     20 |   0 |     336 |       0 ]
JPacket.State#029[2]: [             SCTP(32/0800) |    34 |      0 |     12 |   0 |     324 |       0 ]
JPacket.State#029[3]: [             DATA(33/0800) |    46 |      0 |     16 |   0 |     308 |       0 ]
JPacket.State#029[4]: [             HTTP(13/0800) |    62 |      0 |    305 |   0 |       3 |       0 ]
JPacket.State#029[5]: [        WEB_IMAGE(15/0800) |   367 |      0 |      3 |   0 |       0 |       0 ]

Please notice that SCTP and DATA (Sctp chunk DATA) are separate top-level headers. Http header is bound to the "DATA" Header but in a special way. It had to go back 1 more header in the header-state stack to check the port numbers and verify that Sctp port number 80 was used. If that condition is met then the Binding object or method can return true and it will be placed after the "DATA" header.

Here is an example of a functioning binding in a custom protocol:

private final static JThreadLocal<Sctp> local = new JThreadLocal<Sctp>(Sctp.class);

@Bind(to=SctpData.class)
public static boolean bindToSctpData(JPacket packet, SctpData data) {
	final Sctp sctp = local.get();
	return packet.hasHeader(sctp) && (sctp.destination() == 3868 || sctp.source() == 3868);
}

The above code is used inside another header definition (Diameter class) that extends JHeader. Please note the following important key-points about the binding.

First, the binding is bound to SctpData header, not Sctp. However, inside the binding method, a lookup is made into Sctp header to check the port numbers. jNetPcap provides a utility JThreadLocal class that makes it easy to create thread-local safe headers. A thread-local instance of the Sctp is acquired, a check into the packet is made if the header exists and then the port numbers are checked. The presence of SctpData is a given, since our binding method is bound to it.