org.tinyradius.util
Class RadiusClient

java.lang.Object
  extended by org.tinyradius.util.RadiusClient

public class RadiusClient
extends java.lang.Object

This object represents a simple Radius client which communicates with a specified Radius server. You can use a single instance of this object to authenticate or account different users with the same Radius server as long as you authenticate/account one user after the other. This object is thread safe, but only opens a single socket so operations using this socket are synchronized to avoid confusion with the mapping of request and result packets.


Constructor Summary
RadiusClient(RadiusEndpoint client)
          Constructs a Radius client for the given Radius endpoint.
RadiusClient(java.lang.String hostName, java.lang.String sharedSecret)
          Creates a new Radius client object for a special Radius server.
 
Method Summary
 RadiusPacket account(AccountingRequest request)
          Sends an Accounting-Request packet and receives a response packet.
 RadiusPacket authenticate(AccessRequest request)
          Sends an Access-Request packet and receives a response packet.
 boolean authenticate(java.lang.String userName, java.lang.String password)
          Authenticates a user.
 void close()
          Closes the socket of this client.
static RadiusPacket communicate(RadiusEndpoint remoteServer, RadiusPacket request)
          Sends the specified packet to the specified Radius server endpoint.
 RadiusPacket communicate(RadiusPacket request, int port)
          Sends a Radius packet to the server and awaits an answer.
 int getAcctPort()
          Returns the Radius server accounting port.
 int getAuthPort()
          Returns the Radius server auth port.
 java.lang.String getHostName()
          Returns the host name of the Radius server.
 int getRetryCount()
          Returns the retry count for failed transmissions.
 java.lang.String getSharedSecret()
          Returns the secret shared between server and client.
protected  java.net.DatagramSocket getSocket()
          Returns the socket used for the server communication.
 int getSocketTimeout()
          Returns the socket timeout.
protected  java.net.DatagramPacket makeDatagramPacket(RadiusPacket packet, int port)
          Creates a datagram packet from a RadiusPacket to be send.
protected  RadiusPacket makeRadiusPacket(java.net.DatagramPacket packet, RadiusPacket request)
          Creates a RadiusPacket from a received datagram packet.
 void setAcctPort(int acctPort)
          Sets the Radius server accounting port.
 void setAuthPort(int authPort)
          Sets the auth port of the Radius server.
 void setHostName(java.lang.String hostName)
          Sets the host name of the Radius server.
 void setRetryCount(int retryCount)
          Sets the retry count for failed transmissions.
 void setSharedSecret(java.lang.String sharedSecret)
          Sets the secret shared between server and client.
 void setSocketTimeout(int socketTimeout)
          Sets the socket timeout
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RadiusClient

public RadiusClient(java.lang.String hostName,
                    java.lang.String sharedSecret)
Creates a new Radius client object for a special Radius server.

Parameters:
hostName - host name of the Radius server
sharedSecret - shared secret used to secure the communication

RadiusClient

public RadiusClient(RadiusEndpoint client)
Constructs a Radius client for the given Radius endpoint.

Parameters:
client - Radius endpoint
Method Detail

authenticate

public boolean authenticate(java.lang.String userName,
                            java.lang.String password)
                     throws java.io.IOException,
                            RadiusException
Authenticates a user.

Parameters:
userName - user name
password - password
Returns:
true if authentication is successful, false otherwise
Throws:
RadiusException - malformed packet
java.io.IOException - communication error (after getRetryCount() retries)

authenticate

public RadiusPacket authenticate(AccessRequest request)
                          throws java.io.IOException,
                                 RadiusException
Sends an Access-Request packet and receives a response packet.

Parameters:
request - request packet
Returns:
Radius response packet
Throws:
RadiusException - malformed packet
java.io.IOException - communication error (after getRetryCount() retries)

account

public RadiusPacket account(AccountingRequest request)
                     throws java.io.IOException,
                            RadiusException
Sends an Accounting-Request packet and receives a response packet.

Parameters:
request - request packet
Returns:
Radius response packet
Throws:
RadiusException - malformed packet
java.io.IOException - communication error (after getRetryCount() retries)

close

public void close()
Closes the socket of this client.


getAuthPort

public int getAuthPort()
Returns the Radius server auth port.

Returns:
auth port

setAuthPort

public void setAuthPort(int authPort)
Sets the auth port of the Radius server.

Parameters:
authPort - auth port, 1-65535

getHostName

public java.lang.String getHostName()
Returns the host name of the Radius server.

Returns:
host name

setHostName

public void setHostName(java.lang.String hostName)
Sets the host name of the Radius server.

Parameters:
hostName - host name

getRetryCount

public int getRetryCount()
Returns the retry count for failed transmissions.

Returns:
retry count

setRetryCount

public void setRetryCount(int retryCount)
Sets the retry count for failed transmissions.

Parameters:
retryCount - retry count, >0

getSharedSecret

public java.lang.String getSharedSecret()
Returns the secret shared between server and client.

Returns:
shared secret

setSharedSecret

public void setSharedSecret(java.lang.String sharedSecret)
Sets the secret shared between server and client.

Parameters:
sharedSecret - shared secret

getSocketTimeout

public int getSocketTimeout()
Returns the socket timeout.

Returns:
socket timeout, ms

setSocketTimeout

public void setSocketTimeout(int socketTimeout)
                      throws java.net.SocketException
Sets the socket timeout

Parameters:
socketTimeout - timeout, ms, >0
Throws:
java.net.SocketException

setAcctPort

public void setAcctPort(int acctPort)
Sets the Radius server accounting port.

Parameters:
acctPort - acct port, 1-65535

getAcctPort

public int getAcctPort()
Returns the Radius server accounting port.

Returns:
acct port

communicate

public RadiusPacket communicate(RadiusPacket request,
                                int port)
                         throws java.io.IOException,
                                RadiusException
Sends a Radius packet to the server and awaits an answer.

Parameters:
request - packet to be sent
port - server port number
Returns:
response Radius packet
Throws:
RadiusException - malformed packet
java.io.IOException - communication error (after getRetryCount() retries)

communicate

public static RadiusPacket communicate(RadiusEndpoint remoteServer,
                                       RadiusPacket request)
                                throws RadiusException,
                                       java.io.IOException
Sends the specified packet to the specified Radius server endpoint.

Parameters:
remoteServer - Radius endpoint consisting of server address, port number and shared secret
request - Radius packet to be sent
Returns:
received response packet
Throws:
RadiusException - malformed packet
java.io.IOException - error while communication

getSocket

protected java.net.DatagramSocket getSocket()
                                     throws java.net.SocketException
Returns the socket used for the server communication. It is bound to an arbitrary free local port number.

Returns:
local socket
Throws:
java.net.SocketException

makeDatagramPacket

protected java.net.DatagramPacket makeDatagramPacket(RadiusPacket packet,
                                                     int port)
                                              throws java.io.IOException
Creates a datagram packet from a RadiusPacket to be send.

Parameters:
packet - RadiusPacket
port - destination port number
Returns:
new datagram packet
Throws:
java.io.IOException

makeRadiusPacket

protected RadiusPacket makeRadiusPacket(java.net.DatagramPacket packet,
                                        RadiusPacket request)
                                 throws java.io.IOException,
                                        RadiusException
Creates a RadiusPacket from a received datagram packet.

Parameters:
packet - received datagram
request - Radius request packet
Returns:
RadiusPacket object
Throws:
java.io.IOException
RadiusException