org.tinyradius.util
Class RadiusServer

java.lang.Object
  extended by org.tinyradius.util.RadiusServer
Direct Known Subclasses:
org.tinyradius.proxy.RadiusProxy

public abstract class RadiusServer
extends java.lang.Object

Implements a simple Radius server. This class must be subclassed to provide an implementation for getSharedSecret() and getUserPassword(). If the server supports accounting, it must override accountingRequestReceived().


Constructor Summary
RadiusServer()
           
 
Method Summary
 RadiusPacket accessRequestReceived(AccessRequest accessRequest, java.net.InetSocketAddress client)
          Constructs an answer for an Access-Request packet.
 RadiusPacket accountingRequestReceived(AccountingRequest accountingRequest, java.net.InetSocketAddress client)
          Constructs an answer for an Accounting-Request packet.
protected  void copyProxyState(RadiusPacket request, RadiusPacket answer)
          Copies all Proxy-State attributes from the request packet to the response packet.
 int getAcctPort()
          Returns the acct port the server will listen on.
protected  java.net.DatagramSocket getAcctSocket()
          Returns a socket bound to the acct port.
 int getAuthPort()
          Returns the auth port the server will listen on.
protected  java.net.DatagramSocket getAuthSocket()
          Returns a socket bound to the auth port.
 long getDuplicateInterval()
          Returns the duplicate interval in ms.
 java.net.InetAddress getListenAddress()
          Returns the IP address the server listens on.
abstract  java.lang.String getSharedSecret(java.net.InetSocketAddress client)
          Returns the shared secret used to communicate with the client with the passed IP address or null if the client is not allowed at this server.
 int getSocketTimeout()
          Returns the socket timeout (ms).
abstract  java.lang.String getUserPassword(java.lang.String userName)
          Returns the password of the passed user.
protected  RadiusPacket handlePacket(java.net.InetSocketAddress localAddress, java.net.InetSocketAddress remoteAddress, RadiusPacket request, java.lang.String sharedSecret)
          Handles the received Radius packet and constructs a response.
protected  boolean isPacketDuplicate(RadiusPacket packet, java.net.InetSocketAddress address)
          Checks whether the passed packet is a duplicate.
protected  void listen(java.net.DatagramSocket s)
          Listens on the passed socket, blocks until stop() is called.
protected  void listenAcct()
          Listens on the acct port (blocks the current thread).
protected  void listenAuth()
          Listens on the auth port (blocks the current thread).
protected  java.net.DatagramPacket makeDatagramPacket(RadiusPacket packet, java.lang.String secret, java.net.InetAddress address, int port, RadiusPacket request)
          Creates a Radius response datagram packet from a RadiusPacket to be send.
protected  RadiusPacket makeRadiusPacket(java.net.DatagramPacket packet, java.lang.String sharedSecret)
          Creates a RadiusPacket for a Radius request from a received datagram packet.
 void setAcctPort(int acctPort)
          Sets the acct port the server will listen on.
 void setAuthPort(int authPort)
          Sets the auth port the server will listen on.
 void setDuplicateInterval(long duplicateInterval)
          Sets the duplicate interval in ms.
 void setListenAddress(java.net.InetAddress listenAddress)
          Sets the address the server listens on.
 void setSocketTimeout(int socketTimeout)
          Sets the socket timeout.
 void start(boolean listenAuth, boolean listenAcct)
          Starts the Radius server.
 void stop()
          Stops the server and closes the sockets.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RadiusServer

public RadiusServer()
Method Detail

getSharedSecret

public abstract java.lang.String getSharedSecret(java.net.InetSocketAddress client)
Returns the shared secret used to communicate with the client with the passed IP address or null if the client is not allowed at this server.

Parameters:
client - IP address and port number of client
Returns:
shared secret or null

getUserPassword

public abstract java.lang.String getUserPassword(java.lang.String userName)
Returns the password of the passed user. Either this method or accessRequestReceived() should be overriden.

Parameters:
userName - user name
Returns:
plain-text password or null if user unknown

accessRequestReceived

public RadiusPacket accessRequestReceived(AccessRequest accessRequest,
                                          java.net.InetSocketAddress client)
                                   throws RadiusException
Constructs an answer for an Access-Request packet. Either this method or isUserAuthenticated should be overriden.

Parameters:
accessRequest - Radius request packet
client - address of Radius client
Returns:
response packet or null if no packet shall be sent
Throws:
RadiusException - malformed request packet; if this exception is thrown, no answer will be sent

accountingRequestReceived

public RadiusPacket accountingRequestReceived(AccountingRequest accountingRequest,
                                              java.net.InetSocketAddress client)
                                       throws RadiusException
Constructs an answer for an Accounting-Request packet. This method should be overriden if accounting is supported.

Parameters:
accountingRequest - Radius request packet
client - address of Radius client
Returns:
response packet or null if no packet shall be sent
Throws:
RadiusException - malformed request packet; if this exception is thrown, no answer will be sent

start

public void start(boolean listenAuth,
                  boolean listenAcct)
Starts the Radius server.

Parameters:
listenAuth - open auth port?
listenAcct - open acct port?

stop

public void stop()
Stops the server and closes the sockets.


getAuthPort

public int getAuthPort()
Returns the auth port the server will listen on.

Returns:
auth port

setAuthPort

public void setAuthPort(int authPort)
Sets the auth port the server will listen on.

Parameters:
authPort - auth port, 1-65535

getSocketTimeout

public int getSocketTimeout()
Returns the socket timeout (ms).

Returns:
socket timeout

setSocketTimeout

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

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

setAcctPort

public void setAcctPort(int acctPort)
Sets the acct port the server will listen on.

Parameters:
acctPort - acct port 1-65535

getAcctPort

public int getAcctPort()
Returns the acct port the server will listen on.

Returns:
acct port

getDuplicateInterval

public long getDuplicateInterval()
Returns the duplicate interval in ms. A packet is discarded as a duplicate if in the duplicate interval there was another packet with the same identifier originating from the same address.

Returns:
duplicate interval (ms)

setDuplicateInterval

public void setDuplicateInterval(long duplicateInterval)
Sets the duplicate interval in ms. A packet is discarded as a duplicate if in the duplicate interval there was another packet with the same identifier originating from the same address.

Parameters:
duplicateInterval - duplicate interval (ms), >0

getListenAddress

public java.net.InetAddress getListenAddress()
Returns the IP address the server listens on. Returns null if listening on the wildcard address.

Returns:
listen address or null

setListenAddress

public void setListenAddress(java.net.InetAddress listenAddress)
Sets the address the server listens on. Must be called before start(). Defaults to null, meaning listen on every local address (wildcard address).

Parameters:
listenAddress - listen address or null

copyProxyState

protected void copyProxyState(RadiusPacket request,
                              RadiusPacket answer)
Copies all Proxy-State attributes from the request packet to the response packet.

Parameters:
request - request packet
answer - response packet

listenAuth

protected void listenAuth()
                   throws java.net.SocketException
Listens on the auth port (blocks the current thread). Returns when stop() is called.

Throws:
java.net.SocketException
java.lang.InterruptedException

listenAcct

protected void listenAcct()
                   throws java.net.SocketException
Listens on the acct port (blocks the current thread). Returns when stop() is called.

Throws:
java.net.SocketException
java.lang.InterruptedException

listen

protected void listen(java.net.DatagramSocket s)
Listens on the passed socket, blocks until stop() is called.

Parameters:
s - socket to listen on

handlePacket

protected RadiusPacket handlePacket(java.net.InetSocketAddress localAddress,
                                    java.net.InetSocketAddress remoteAddress,
                                    RadiusPacket request,
                                    java.lang.String sharedSecret)
                             throws RadiusException,
                                    java.io.IOException
Handles the received Radius packet and constructs a response.

Parameters:
localAddress - local address the packet was received on
remoteAddress - remote address the packet was sent by
request - the packet
Returns:
response packet or null for no response
Throws:
RadiusException
java.io.IOException

getAuthSocket

protected java.net.DatagramSocket getAuthSocket()
                                         throws java.net.SocketException
Returns a socket bound to the auth port.

Returns:
socket
Throws:
java.net.SocketException

getAcctSocket

protected java.net.DatagramSocket getAcctSocket()
                                         throws java.net.SocketException
Returns a socket bound to the acct port.

Returns:
socket
Throws:
java.net.SocketException

makeDatagramPacket

protected java.net.DatagramPacket makeDatagramPacket(RadiusPacket packet,
                                                     java.lang.String secret,
                                                     java.net.InetAddress address,
                                                     int port,
                                                     RadiusPacket request)
                                              throws java.io.IOException
Creates a Radius response datagram packet from a RadiusPacket to be send.

Parameters:
packet - RadiusPacket
secret - shared secret to encode packet
address - where to send the packet
port - destination port
request - request packet
Returns:
new datagram packet
Throws:
java.io.IOException

makeRadiusPacket

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

Parameters:
packet - received datagram
Returns:
RadiusPacket object
Throws:
RadiusException - malformed packet
java.io.IOException - communication error (after getRetryCount() retries)

isPacketDuplicate

protected boolean isPacketDuplicate(RadiusPacket packet,
                                    java.net.InetSocketAddress address)
Checks whether the passed packet is a duplicate. A packet is duplicate if another packet with the same identifier has been sent from the same host in the last time.

Parameters:
packet - packet in question
address - client address
Returns:
true if it is duplicate