Protocols


Socket Protocols


The standard Java library provides support for the TCP and UDP protocols through its Socket and DatagramSocket classes - however, it lacks support for the ICMP protocol and for raw sockets in general, which means that it is not possible to use Java to write applications such as ping, traceroute or packet sniffers. The JSocket Wrench project adds support for the missing protocols.

A socket protocol in the JSocket Wrench library is characterized by three properties:

Each property is discussed in turn.

The socket() function call

The socket() function call takes the form socket(family, type, protocol).


The IP_HDRINCL socket option

Most applications will allow the operating system to generate the IP headers for their messages, in which case IP_HDRINCL will be set to FALSE. However, some applications (the classic example is traceroute) will wish to create their own IP headers. (In the case of traceroute, the application will modify the time-to-live segment in the IP header between successive messages). A socket protocol that is to create its own IP headers will have the IP_HDRINCL socket option set to TRUE.


SocketImpl and DatagramSocketImpl subclasses

As mentioned above, the Java library supports the TCP and UDP protocols through the classes Socket and DatagramSocket. However, when subclasses of the abstract classes SocketImpl and DatagramSocketImpl are provided, the default protocol implementations will be overridden. Such subclasses are rarely seen in literature because they are seldom needed and are non-trivial to write. The JSocketWrench library provides three subclasses: GeneralDatagramSocketImpl, GeneralSocketImpl and RawTCPSocketImpl .

Since the library class DatagramSocket always creates sockets of type SOCK_DGRAM, it is appropriate only for the UDP protocol. The subclass GeneralDatagramSocketImpl allows sockets of any type to be created - in particular, raw sockets, which, amongst other things, opens the way to support of the ICMP protocol.

The class GeneralSocketImpl similarly generalizes the Socket class - however, the generalization is of less use here because the calls to the Berkeley C socket library made from the JNI layer that underpins GeneralSocketImpl will return errors if invoked on sockets of any type other than SOCK_STREAM. However, since the class illustrates how a subclass of SocketImpl might be constructed, it is of some illustrative value.

The subclass RawTCPSocketImpl builds a rudimentary TCP implementation on top of raw sockets. The class shows how an asynchronous, stateful protocol could be constructed using the Java thread model and provides a basis for experimentation.


The JSocket Wrench protocols

The following protocols are supported by the JSocketWrench library.

ICMP

JSocket Wrench namesocket typeprotocolIP_HDRINCLDatagramSocketImpl subclass
ICMP SOCK_RAW IPPROTO_ICMP FALSE GeneralDatagramSocketImpl
HdrICMP SOCK_RAW IPPROTO_ICMP TRUE GeneralDatagramSocketImpl

UDP

JSocket Wrench namesocket typeprotocolIP_HDRINCLDatagramSocketImpl subclass
UDP SOCK_DGRAM IPPROTO_IP FALSE GeneralDatagramSocketImpl
RawUDP SOCK_RAW IPPROTO_UDP FALSE GeneralDatagramSocketImpl
RawHdrUDP SOCK_RAW IPPROTO_UDP TRUE GeneralDatagramSocketImpl

TCP

JSocket Wrench Namesocket typeprotocolIP_HDRINCLSocketImpl subclass
TCP SOCK_STREAM IPPROTO_IP FALSE GeneralSocketImpl
RawTCP SOCK_RAW IPPROTO_TCP FALSE RawTCPSocketImpl
RawHdrTCP SOCK_RAW IPPROTO_TCP TRUE RawTCPSocketImpl
RawTCPJ SOCK_RAW IPPROTO_TCPJ FALSE RawTCPSocketImpl
RawHdrTCPJ SOCK_RAW IPPROTO_TCPJ TRUE RawTCPSocketImpl

SourceForge.net Logo