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:
socket()
.
IP_HDRINCL (
which
dictates whether the IP header should be constructed by the operating system -
FALSE - or the application - TRUE)
SocketImpl
and DatagramSocketImpl
used to implement the socket protocol.socket()
function callThe socket()
function call takes the form socket(family, type,
protocol)
.
family
specifies the protocol family, which is always AF_INET
in the JSocketWrench library meaning IPv4. (The newer IPv6 isn't supported).
type
specifies the socket type, which will be SOCK_STREAM
for a stream (TCP) socket, SOCK_DGRAM
for a datagram (UDP) socket
or SOCK_RAW
for a raw socket.
protocol
specifies the protocol to be used, which will be one of:
IPPROTO_IP
- used with non-raw sockets in order to ensure the
appropriate protocol (UDP for SOCK_DGRAM
or TCP for SOCK_STREAM
)
is invoked by the operating system.
IPPROTO_ICMP
IPPROTO_UDP
IPPROTO_TCP
IPPROTO_TCPJ
- an unofficial protocol that has been created for
the library. It is hard to run an alternative TCP implementation on any PC that
already supports TCP, i.e. almost any PC. Suppose a client were to attempt to
connect (i.e. send a SYN signal) to a remote server. The server would respond
with a SYN,ACK signal - but the signal would be intercepted by the main TCP
implementation on the client. The client TCP would not recognize the SYN,ACK
response (it would have known nothing about the original SYN signal), so it
will reset the pending connection with an RST message. To work around the
problem, TCP messages have been embedded within an IP header with the protocol
156, which has been entitled TCP/J - the Transmission Control Protocol for
Java.IP_HDRINCL
socket optionMost 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
subclassesAs 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 following protocols are supported by the JSocketWrench library.
JSocket Wrench name | socket type | protocol | IP_HDRINCL | DatagramSocketImpl subclass |
---|---|---|---|---|
ICMP | SOCK_RAW | IPPROTO_ICMP | FALSE | GeneralDatagramSocketImpl |
HdrICMP | SOCK_RAW | IPPROTO_ICMP | TRUE | GeneralDatagramSocketImpl |
JSocket Wrench name | socket type | protocol | IP_HDRINCL | DatagramSocketImpl subclass |
---|---|---|---|---|
UDP | SOCK_DGRAM | IPPROTO_IP | FALSE | GeneralDatagramSocketImpl |
RawUDP | SOCK_RAW | IPPROTO_UDP | FALSE | GeneralDatagramSocketImpl |
RawHdrUDP | SOCK_RAW | IPPROTO_UDP | TRUE | GeneralDatagramSocketImpl |
DatagramSocket
class so are of illustrative use only.
JSocket Wrench Name | socket type | protocol | IP_HDRINCL | SocketImpl 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 |
Socket
class so is of illustrative use only