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); } }
/// <summary> /// 認証実行 /// </summary> /// <returns>true 認証成功, false 認証失敗</returns> private bool auth() { IPAddress[] addrList = Dns.GetHostAddresses(Dns.GetHostName()); IPAddress addr = null; // 一番最初に見つかったIPv4アドレスを送信元とする // TODO: いろいろ変なので調整が必要 foreach (IPAddress ipa in addrList) { if (ipa.AddressFamily == AddressFamily.InterNetwork) { addr = ipa; break; } } IPEndPoint local = new IPEndPoint(addr, (new Random((int)DateTime.Now.ToBinary())).Next(49152, 65535)); // TODO: 定数があれば使う UdpClient udp = new UdpClient(local); udp.Send(request.GetBuffer(), request.Length, serverHost, int.Parse(serverPort)); // TODO: int.Parse() は安全だっけ? IPEndPoint remote = null; byte[] rbuf = udp.Receive(ref remote); udp.Close(); reply = new RadiusPacket(rbuf); if (request.PacketIdentifier == reply.PacketIdentifier) { if (reply.Code == RadiusPacket.CODE.ACCESS_ACCEPT) { return(true); } } return(false); }