/**
* Listens on the passed socket, blocks until stop() is called.
* @param s socket to listen on
*/
protected void listen(DatagramSocket s) {
DatagramPacket packetIn = new DatagramPacket(new byte[RadiusPacket.MAX_PACKET_LENGTH], RadiusPacket.MAX_PACKET_LENGTH);
while (true) {
try {
// receive packet
try {
logger.trace("about to call socket.receive()");
s.receive(packetIn);
if (logger.isDebugEnabled())
logger.debug("receive buffer size = " + s.getReceiveBufferSize());
} catch (SocketException se) {
if (closing) {
// end thread
logger.info("got closing signal - end listen thread");
return;
} else {
// retry s.receive()
logger.error("SocketException during s.receive() -> retry", se);
continue;
}
}
// check client
InetSocketAddress localAddress = (InetSocketAddress)s.getLocalSocketAddress();
InetSocketAddress remoteAddress = new InetSocketAddress(packetIn.getAddress(), packetIn.getPort());
String secret = getSharedSecret(remoteAddress);
if (secret == null) {
if (logger.isInfoEnabled())
logger.info("ignoring packet from unknown client " + remoteAddress + " received on local address " + localAddress);
continue;
}
// parse packet
RadiusPacket request = makeRadiusPacket(packetIn, secret);
if (logger.isInfoEnabled())
logger.info("received packet from " + remoteAddress + " on local address " + localAddress + ": " + request);
// handle packet
logger.trace("about to call RadiusServer.handlePacket()");
RadiusPacket response = handlePacket(localAddress, remoteAddress, request, secret);
// send response
if (response != null) {
if (logger.isInfoEnabled())
logger.info("send response: " + response);
DatagramPacket packetOut = makeDatagramPacket(response, secret, remoteAddress.getAddress(), packetIn.getPort(), request);
s.send(packetOut);
} else
logger.info("no response sent");
} catch (SocketTimeoutException ste) {
// this is expected behaviour