Source code: RadiusTransport.java

index |  71 lines | javadoc ]

package nl.west.aaa.radius;

import nl.west.aaa.*;
import java.net.*;
import java.io.*;

public class RadiusTransport
    extends UDPTransport
    implements TransportProtocol
{

    private AAAUnit receiver;
    
    /**
     * Port number for sending RADIUS messages.
     */
    private static final int RADIUS_PORT=1812;
    
    /**
     * Port number for sending RADIUS accounting messages.
     */
    private static final int RADIUS_ACC_PORT=1813;
    
    private DatagramSocket radiusSocket;
    private DatagramSocket radiusAccountingSocket;
    
    /*
      When RADIUS messages are replied to the source
      and destination ports of the RADIUS messages are reversed
      so message a:40 -> b:1812
      reply     b:1812 - > a:40
    */

    public RadiusTransport()
        throws SocketException
    {
        radiusSocket=new DatagramSocket(RADIUS_PORT);
        radiusAccountingSocket=
                     new DatagramSocket(RADIUS_ACC_PORT);
        addSocketListener(radiusSocket);
        addSocketListener(radiusAccountingSocket);
    }
    
    public void sendMessage(byte[] data,Identifier id)
        throws IOException
    {
        // find out which port and socket to use
        int port=RADIUS_PORT;
        DatagramSocket sock=radiusSocket;
        int code=(int)data[0];
        if( (code==RadiusFormat.MSG_ACCOUNTING_REQUEST) ||
            (code==RadiusFormat.MSG_ACCOUNTING_RESPONSE) )
        {
            port=RADIUS_ACC_PORT;
            sock=radiusAccountingSocket;
        }
        // get the address
        InetAddress addr=InetAddress.getByName(id.getHostname());
        // send it on the socket
        DatagramPacket pack=new DatagramPacket(data,data.length,addr,port);
//** DEBUG STUFF
/*
System.out.println("SEND "+pack);        
System.out.println("SEND -- code="+code+" port="+port+" addr="+addr+" data="+data);
for(int i=0;i<data.length;i++) System.out.print((((int)data[i])&0xff)+"["+(char)data[i]+"],");
System.out.println("");
*/
        sock.send(pack);
    }
    
}


Arthur <arthur@ch.twi.tudelft.nl> http://ch.twi.tudelft.nl/~arthur/
2002-05-27