Esempio n. 1
0
        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);
            }
        }