void HandleRequest(object state) { // try to logon the user and create the response var request = (Request)state; int timeout; try { timeout = LogonAndCreateSession(request.UserName, request.Password, request.Address); } catch (OleDbException e) { ServiceApplication.LogEvent(EventLogEntryType.Error, e.Message); return; } var response = new RadiusPacket(timeout < 0 ? PacketCode.AccessReject : PacketCode.AccessAccept); response.Identifier = request.Identifier; if (timeout > 0) { response.Attribute(RadiusAttribute.SessionTimeout).Add(timeout); } response.Attribute(RadiusAttribute.ProxyState).AddRange(request.ProxyStates); response.SignResponse(request.Authenticator, sharedSecred); try { socket.SendTo(response.GetBuffer(), 0, response.Length, SocketFlags.None, request.Client); } catch (ObjectDisposedException) { } catch (SocketException e) { ServiceApplication.LogEvent(EventLogEntryType.Error, e.Message); } }
void Listener() { // create the buffer and start listening var buffer = new byte[0x10000]; while (true) { // receive the next message var endpoint = (EndPoint) new IPEndPoint(IPAddress.Any, 0); var length = socket.ReceiveFrom(buffer, ref endpoint); // if nothing is received, continue listening if (length == 0) { continue; } // parse the packet and retrieve all necessary attributes var request = new Request(); request.Client = (IPEndPoint)endpoint; try { var packet = new RadiusPacket(buffer, length); if (packet.Code != PacketCode.AccessRequest) { continue; } request.Identifier = packet.Identifier; request.Authenticator = packet.Authenticator; if (packet.Attribute(RadiusAttribute.CHAPPassword).Count > 0) { throw new FormatException("CHAP-Password is not supported"); } var userNames = packet.Attribute(RadiusAttribute.UserName); if (userNames.Count != 1) { throw new FormatException("User-Name is not present"); } request.UserName = userNames[0]; if (packet.Attribute(RadiusAttribute.UserPassword).Count != 1) { throw new FormatException("User-Password is not present"); } request.Password = packet.GetUserPassword(sharedSecred); var callerIds = packet.Attribute(RadiusAttribute.CallingStationId); request.Address = callerIds.Count > 0 ? callerIds[0] : null; request.ProxyStates = packet.Attribute(RadiusAttribute.ProxyState).ToArray(); } #if DEBUG catch (FormatException e) { ServiceApplication.LogEvent(EventLogEntryType.Information, e.Message); continue; } #else catch (FormatException) { continue; } #endif // enqueue the request ThreadPool.QueueUserWorkItem(HandleRequest, request); } }