|
![]() |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.savarese.rocksaw.net.RawSocket
public class RawSocket
The RawSocket class provides a strictly utilitarian API for performing I/O with IPv4 and IPv6 raw sockets. The API is currently crude, but functional. It should evolve into something more feature-complete and flexible.
We throw java.io.InterruptedIOException when read/write operations time out because java.net.SocketTimeoutException is present only in J2SE 1.4 and up. By using InterruptedIOException, we allow programmers to use the software with J2SE 1.2 and 1.3.
Socket options should not be set until the socket has been opened.
Important! On most operating systems, you must have root access or administrative privileges to use raw sockets.
Field Summary | |
---|---|
static int |
PF_INET
A protocol family constant for open(int, int) indicating IPv4. |
static int |
PF_INET6
A protocol family constant for open(int, int) indicating IPv6. |
Constructor Summary | |
---|---|
RawSocket()
Creates an uninitialized socket. |
Method Summary | |
---|---|
void |
bind(java.net.InetAddress address)
Binds a local network address to a previously opened raw socket. |
void |
bindDevice(java.lang.String device)
Binds a network device (e.g., eth0) to a previously opened raw socket. |
void |
close()
Closes the socket. |
boolean |
getIPHeaderInclude()
Retrieves the current setting of the IP_HDRINCL option. |
static int |
getProtocolByName(java.lang.String name)
Returns the protocol number corresponding to the given protocol name. |
int |
getReceiveBufferSize()
Retrieves the receive buffer size (SO_RCVBUF). |
int |
getReceiveTimeout()
Retrieves the receive timeout (SO_RCVTIMEO). |
int |
getSendBufferSize()
Retrieves the send buffer size (SO_SNDBUF). |
int |
getSendTimeout()
Retrieves the send timeout (SO_SNDTIMEO). |
void |
getSourceAddressForDestination(java.net.InetAddress destination,
byte[] source)
Returns by out parameter the address of the network interface that will be used to send a packet to the given destination. |
boolean |
getUseSelectTimeout()
Determines whether or not socket send/receive timeouts are emulated by using the POSIX select system function. |
boolean |
isOpen()
Tests if the socket has been opened. |
void |
open(int protocolFamily,
int protocol)
Opens a raw socket. |
int |
read(byte[] data)
Same as read(address, data, 0, data.length, null); |
int |
read(byte[] data,
byte[] address)
Same as read(data, 0, data.length, address); |
int |
read(byte[] data,
int offset,
int length)
Same as read(data, 0, length, null); |
int |
read(byte[] data,
int offset,
int length,
byte[] address)
Reads packet data from the socket. |
void |
setIPHeaderInclude(boolean on)
Sets or unsets the IP_HDRINCL socket option. |
void |
setReceiveBufferSize(int size)
Sets the receive buffer size (SO_RCVBUF). |
void |
setReceiveTimeout(int timeout)
Sets the receive timeout (SO_RCVTIMEO). |
void |
setSendBufferSize(int size)
Sets the send buffer size (SO_SNDBUF). |
void |
setSendTimeout(int timeout)
Sets the send timeout (SO_SNDTIMEO). |
void |
setUseSelectTimeout(boolean useSelect)
Sets whether or not socket send/receive timeouts should be emulated by using the POSIX select function. |
int |
write(java.net.InetAddress address,
byte[] data)
Same as write(address, data, 0, data.length); |
int |
write(java.net.InetAddress address,
byte[] data,
int offset,
int length)
Writes packet data to the socket. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int PF_INET
open(int, int)
indicating IPv4.
This should be moved to another class.
public static final int PF_INET6
open(int, int)
indicating IPv6.
This should be moved to another class.
Constructor Detail |
---|
public RawSocket()
os.name
system
property starts with the string "SunOS",
setUseSelectTimeout(boolean)
is set to true (because Solaris does not
support socket send and receive timeouts), otherwise it is false
by default.
Method Detail |
---|
public boolean isOpen()
public static final int getProtocolByName(java.lang.String name)
Returns the protocol number corresponding to the given protocol name.
For example, getProtocolByName("icmp");
should return 1 and
getProtocolByName("udp");
should return 17. The native system
protocol database is used to look up the protocol numbers.
This method really belongs in another class, probably in vserv-tcpip, but is currently included here as a convenience. It may be moved elsewhere in the 1.0 release API.
public void getSourceAddressForDestination(java.net.InetAddress destination, byte[] source) throws java.io.IOException
Returns by out parameter the address of the network interface that will be used to send a packet to the given destination. This works on Windows only and is necessary in order to compute ICMPv6 checksums.
This method really belongs in another class,, but is currently included here for expediency. It may be moved elsewhere in the 1.0 release API.
destination
- The address of the destination.source
- A byte array in which to store the returned source
address. On platforms other than Microsoft Windows, the array is
left unchanged.
java.io.IOException
- If an I/O error occurs.public void open(int protocolFamily, int protocol) throws java.lang.IllegalStateException, java.io.IOException
protocolFamily
- The protocol family of the socket (e.g.,
PF_INET
or PF_INET6
).protocol
- The protocol within the protocol family. getProtocolByName(java.lang.String)
should be used to obtain protocol numbers.
java.lang.IllegalStateException
- If the object instance is
already open.
java.io.IOException
- If an error occurs while opening the socket.public void bind(java.net.InetAddress address) throws java.lang.IllegalStateException, java.io.IOException
setIPHeaderInclude(true)
has been called).
address
- The address to bind.
java.lang.IllegalStateException
- If the socket has not been opened first.
java.io.IOException
- If the address cannot be bound.public void bindDevice(java.lang.String device) throws java.lang.UnsupportedOperationException, java.lang.IllegalStateException, java.io.IOException
device
- The name of the device to bind (e.g., "eth0").
Passing a zero-length string will remove the current binding.
The loopback interface ("lo") and device aliases (e.g., "eth0:1")
cannot be bound.
java.lang.IllegalStateException
- If the socket has not been opened first.
java.lang.UnsupportedOperationException
- If binding a device
name is not supported on the runtime platform.
java.io.IOException
- If the device cannot be bound.public void close() throws java.io.IOException
java.io.IOException
- If an I/O error occurs.public void setIPHeaderInclude(boolean on) throws java.net.SocketException
on
- True if headers should be included, false if not.
java.net.SocketException
- If the option setting could not be altered.public boolean getIPHeaderInclude() throws java.net.SocketException
java.net.SocketException
- If the option value could not be retrieved.public void setSendBufferSize(int size) throws java.net.SocketException
size
- The size of the send buffer.
java.net.SocketException
- If the option value could not be set.public int getSendBufferSize() throws java.net.SocketException
java.net.SocketException
- If the option value could not be retrieved.public void setReceiveBufferSize(int size) throws java.net.SocketException
size
- The size of the receive buffer.
java.net.SocketException
- If the option value could not be set.public int getReceiveBufferSize() throws java.net.SocketException
java.net.SocketException
- If the option value could not be retrieved.public void setUseSelectTimeout(boolean useSelect)
Sets whether or not socket send/receive timeouts should be
emulated by using the POSIX select
function. Not all
platforms support socket send/receive timeouts and this method
provides a means to reproduce the same effect.
This method is not guaranteed to be retained in the 1.0 API. We may find a better way to provide support for read/write timeouts on all platforms. Technically, it's better to simply use non-blocking I/O rather than rely on socket timeouts, but we have yet to add a non-blocking I/O interface.
useSelect
- true if select
should be used to
implement timeouts, false if not.public boolean getUseSelectTimeout()
Determines whether or not socket send/receive timeouts are
emulated by using the POSIX select
system function.
Not all platforms support socket send/receive timeouts. The
default value is false except for platforms where the os.name
property starts with the string "SunOS".
public void setSendTimeout(int timeout) throws java.net.SocketException
timeout
- The send timeout in milliseconds.
java.net.SocketException
- If the option value could not be set.public int getSendTimeout() throws java.net.SocketException
java.net.SocketException
- If the option value could not be set.public void setReceiveTimeout(int timeout) throws java.net.SocketException
timeout
- The receive timeout in milliseconds.
java.net.SocketException
- If the option value could not be set.public int getReceiveTimeout() throws java.net.SocketException
java.net.SocketException
- If the option value could not be set.public int read(byte[] data, int offset, int length, byte[] address) throws java.lang.IllegalArgumentException, java.io.IOException, java.io.InterruptedIOException
PF_INET
)
packets will be delivered in their entirety, including the IP
header. IPv6 (PF_INET6
) packet data will not include
the IPV6 header.
data
- The buffer in which to store the packet data.offset
- The offset into the buffer where the data should
be stored.length
- The number of bytes to read.address
- A byte array in which to store the source address
of the received packet. It may be null if you don't want to
retrieve the source address. Otherwise, it must be the right
size to store the address (e.g., 4 bytes for an IPv4 address).
java.lang.IllegalArgumentException
- If the offset or lengths are
invalid or if the address parameter is the wrong length.
java.io.IOException
- If an I/O error occurs.
java.io.InterruptedIOException
- If the read operation times out.public int read(byte[] data, int offset, int length) throws java.lang.IllegalArgumentException, java.io.IOException, java.io.InterruptedIOException
read(data, 0, length, null);
java.lang.IllegalArgumentException
java.io.IOException
java.io.InterruptedIOException
public int read(byte[] data, byte[] address) throws java.io.IOException, java.io.InterruptedIOException
read(data, 0, data.length, address);
java.io.IOException
java.io.InterruptedIOException
public int read(byte[] data) throws java.io.IOException, java.io.InterruptedIOException
read(address, data, 0, data.length, null);
java.io.IOException
java.io.InterruptedIOException
public int write(java.net.InetAddress address, byte[] data, int offset, int length) throws java.lang.IllegalArgumentException, java.io.IOException, java.io.InterruptedIOException
PF_INET
) sockets may set the
IP_HDRINCL option with setIPHeaderInclude(boolean)
, in which case the
packet data should include the IP header.
address
- The destination to write to.data
- The buffer from which to copy the packet data.offset
- The offset into the buffer where the data starts.length
- The number of bytes to write.
java.lang.IllegalArgumentException
- If the offset or lengths are invalid.
java.io.IOException
- If an I/O error occurs.
java.io.InterruptedIOException
- If the write operation times out.public int write(java.net.InetAddress address, byte[] data) throws java.io.IOException, java.io.InterruptedIOException
write(address, data, 0, data.length);
java.io.IOException
java.io.InterruptedIOException
|
![]() |
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |