socks
Class Socks5DatagramSocket

java.lang.Object
  |
  +--java.net.DatagramSocket
        |
        +--socks.Socks5DatagramSocket

public class Socks5DatagramSocket
extends java.net.DatagramSocket

Datagram socket to interract through the firewall.
Can be used same way as the normal DatagramSocket. One should be carefull though with the datagram sizes used, as additional data is present in both incomming and outgoing datagrams.

SOCKS5 protocol allows to send host address as either:

As with other Socks equivalents, direct addresses are handled transparently, that is data will be send directly when required by the proxy settings.

NOTE:
Unlike other SOCKS Sockets, it does not support proxy chaining, and will throw an exception if proxy has a chain proxy attached. The reason for that is not my laziness, but rather the restrictions of the SOCKSv5 protocol. Basicaly SOCKSv5 proxy server, needs to know from which host:port datagrams will be send for association, and returns address to which datagrams should be send by the client, but it does not inform client from which host:port it is going to send datagrams, in fact there is even no guarantee they will be send at all and from the same address each time.


Constructor Summary
Socks5DatagramSocket()
          Construct Datagram socket for communication over SOCKS5 proxy server.
Socks5DatagramSocket(int port)
          Construct Datagram socket for communication over SOCKS5 proxy server.
Socks5DatagramSocket(int port, java.net.InetAddress ip)
          Construct Datagram socket for communication over SOCKS5 proxy server.
Socks5DatagramSocket(Proxy p, int port, java.net.InetAddress ip)
          Constructs datagram socket for communication over specified proxy.
 
Method Summary
 void close()
          Closes datagram socket, and proxy connection.
 java.net.InetAddress getLocalAddress()
          Address assigned by the proxy, to which datagrams are send for relay.
 int getLocalPort()
          Returns port assigned by the proxy, to which datagrams are relayed.
 boolean isProxyAlive(int timeout)
          This method checks wether proxy still runs udp forwarding service for this socket.
 void receive(java.net.DatagramPacket dp)
          Receives udp packet.
 void send(java.net.DatagramPacket dp)
          Sends the Datagram either through the proxy or directly depending on current proxy settings and destination address.
 void send(java.net.DatagramPacket dp, java.lang.String host)
          This method allows to send datagram packets with address type DOMAINNAME.
 
Methods inherited from class java.net.DatagramSocket
connect, disconnect, getInetAddress, getPort, getReceiveBufferSize, getSendBufferSize, getSoTimeout, setReceiveBufferSize, setSendBufferSize, setSoTimeout
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Socks5DatagramSocket

public Socks5DatagramSocket()
                     throws SocksException,
                            java.io.IOException
Construct Datagram socket for communication over SOCKS5 proxy server. This constructor uses default proxy, the one set with Proxy.setDefaultProxy() method. If default proxy is not set or it is set to version4 proxy, which does not support datagram forwarding, throws SocksException.

Socks5DatagramSocket

public Socks5DatagramSocket(int port)
                     throws SocksException,
                            java.io.IOException
Construct Datagram socket for communication over SOCKS5 proxy server. And binds it to the specified local port. This constructor uses default proxy, the one set with Proxy.setDefaultProxy() method. If default proxy is not set or it is set to version4 proxy, which does not support datagram forwarding, throws SocksException.

Socks5DatagramSocket

public Socks5DatagramSocket(int port,
                            java.net.InetAddress ip)
                     throws SocksException,
                            java.io.IOException
Construct Datagram socket for communication over SOCKS5 proxy server. And binds it to the specified local port and address. This constructor uses default proxy, the one set with Proxy.setDefaultProxy() method. If default proxy is not set or it is set to version4 proxy, which does not support datagram forwarding, throws SocksException.

Socks5DatagramSocket

public Socks5DatagramSocket(Proxy p,
                            int port,
                            java.net.InetAddress ip)
                     throws SocksException,
                            java.io.IOException
Constructs datagram socket for communication over specified proxy. And binds it to the given local address and port. Address of null and port of 0, signify any availabale port/address. Might throw SocksException, if:
  1. Given version of proxy does not support UDP_ASSOCIATE.
  2. Proxy can't be reached.
  3. Authorization fails.
  4. Proxy does not want to perform udp forwarding, for any reason.
Might throw IOException if binding dtagram socket to given address/port fails. See java.net.DatagramSocket for more details.
Method Detail

send

public void send(java.net.DatagramPacket dp)
          throws java.io.IOException
Sends the Datagram either through the proxy or directly depending on current proxy settings and destination address.
NOTE: DatagramPacket size should be at least 10 bytes less than the systems limit.

See documentation on java.net.DatagramSocket for full details on how to use this method.

Overrides:
send in class java.net.DatagramSocket
Parameters:
dp - Datagram to send.
Throws:
java.io.IOException - If error happens with I/O.

send

public void send(java.net.DatagramPacket dp,
                 java.lang.String host)
          throws java.io.IOException
This method allows to send datagram packets with address type DOMAINNAME. SOCKS5 allows to specify host as names rather than ip addresses.Using this method one can send udp datagrams through the proxy, without having to know the ip address of the destination host.

If proxy specified for that socket has an option resolveAddrLocally set to true host will be resolved, and the datagram will be send with address type IPV4, if resolve fails, UnknownHostException is thrown.

Parameters:
dp - Datagram to send, it should contain valid port and data
host - Host name to which datagram should be send.
Throws:
java.io.IOException - If error happens with I/O, or the host can't be resolved when proxy settings say that hosts should be resolved locally.
See Also:
Socks5Proxy.resolveAddrLocally(boolean)

receive

public void receive(java.net.DatagramPacket dp)
             throws java.io.IOException
Receives udp packet. If packet have arrived from the proxy relay server, it is processed and address and port of the packet are set to the address and port of sending host.
If the packet arrived from anywhere else it is not changed.
NOTE: DatagramPacket size should be at least 10 bytes bigger than the largest packet you expect (this is for IPV4 addresses). For hostnames and IPV6 it is even more.
Overrides:
receive in class java.net.DatagramSocket
Parameters:
dp - Datagram in which all relevent information will be copied.

getLocalPort

public int getLocalPort()
Returns port assigned by the proxy, to which datagrams are relayed. It is not the same port to which other party should send datagrams.
Overrides:
getLocalPort in class java.net.DatagramSocket
Returns:
Port assigned by socks server to which datagrams are send for association.

getLocalAddress

public java.net.InetAddress getLocalAddress()
Address assigned by the proxy, to which datagrams are send for relay. It is not necesseraly the same address, to which other party should send datagrams.
Overrides:
getLocalAddress in class java.net.DatagramSocket
Returns:
Address to which datagrams are send for association.

close

public void close()
Closes datagram socket, and proxy connection.
Overrides:
close in class java.net.DatagramSocket

isProxyAlive

public boolean isProxyAlive(int timeout)
This method checks wether proxy still runs udp forwarding service for this socket.

This methods checks wether the primary connection to proxy server is active. If it is, chances are that proxy continues to forward datagrams being send from this socket. If it was closed, most likely datagrams are no longer being forwarded by the server.

Proxy might decide to stop forwarding datagrams, in which case it should close primary connection. This method allows to check, wether this have been done.

You can specify timeout for which we should be checking EOF condition on the primary connection. Timeout is in milliseconds. Specifying 0 as timeout implies infinity, in which case method will block, until connection to proxy is closed or an error happens, and then return false.

One possible scenario is to call isProxyactive(0) in separate thread, and once it returned notify other threads about this event.

Parameters:
timeout - For how long this method should block, before returning.
Returns:
true if connection to proxy is active, false if eof or error condition have been encountered on the connection.