/// <summary> /// Opens a RADIUS client port using the <see cref="RadiusClientSettings" /> passed. /// </summary> /// <param name="settings">The client settings.</param> /// <remarks> /// <note> /// All successful calls to <see cref="Open" /> must eventually be matched /// with a call to <see cref="Close" /> so that system resources will be /// released promptly. /// </note> /// </remarks> public void Open(RadiusClientSettings settings) { using (TimedLock.Lock(this)) { if (isOpen) { throw new RadiusException("RADIUS client port is already open."); } sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.Bind(settings.NetworkBinding); this.sock.SendBufferSize = settings.SocketBuffer; this.sock.ReceiveBufferSize = settings.SocketBuffer; isOpen = true; networkBinding = settings.NetworkBinding; servers = settings.Servers; secret = settings.Secret; retryInterval = settings.RetryInterval; maxTransmissions = settings.MaxTransmissions; realmFormat = settings.RealmFormat; nextID = 0; serverPos = 0; transactions = new AuthTransaction[256]; recvBuf = new byte[TcpConst.MTU]; sourceEP = new IPEndPoint(IPAddress.Any, 0); onReceive = new AsyncCallback(OnReceive); if (networkBinding.Address.Equals(IPAddress.Any)) { nasIPAddress = NetHelper.GetActiveAdapter(); } else { nasIPAddress = networkBinding.Address; } // Initiate reception of the first RADIUS packet. sock.BeginReceiveFrom(recvBuf, 0, recvBuf.Length, SocketFlags.None, ref sourceEP, onReceive, null); } }
/// <summary> /// Starts the server using the settings passed. /// </summary> /// <param name="settings">The server settings.</param> /// <remarks> /// <note> /// All successful calls to <b>Start()</b> must eventually be matched /// with a call to <see cref="Stop" /> so that system resources will be /// released promptly. /// </note> /// </remarks> public void Start(RadiusServerSettings settings) { using (TimedLock.Lock(this)) { if (isRunning) { throw new RadiusException("RADIUS server has already started."); } this.sock = new EnhancedSocket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); this.sock.Bind(settings.NetworkBinding); this.sock.SendBufferSize = settings.SocketBuffer; this.sock.ReceiveBufferSize = settings.SocketBuffer; this.isRunning = true; this.defSecret = settings.DefaultSecret; this.networkBinding = settings.NetworkBinding; this.dnsRefreshInterval = settings.DnsRefreshInterval; this.nextDnsRefresh = SysTime.Now + dnsRefreshInterval; this.bkTimer = new GatedTimer(new TimerCallback(OnBkTask), null, settings.BkTaskInterval, settings.BkTaskInterval); this.recvBuf = new byte[TcpConst.MTU]; this.Devices = settings.Devices; this.onReceive = new AsyncCallback(OnReceive); this.remoteEP = new IPEndPoint(IPAddress.Any, 0); this.realmFormat = settings.RealmFormat; if (networkBinding.Address.Equals(IPAddress.Any)) { actualEndPoint = new IPEndPoint(NetHelper.GetActiveAdapter(), ((IPEndPoint)sock.LocalEndPoint).Port); } else { actualEndPoint = new IPEndPoint(networkBinding.Address, ((IPEndPoint)sock.LocalEndPoint).Port); } this.sock.BeginReceiveFrom(recvBuf, 0, recvBuf.Length, SocketFlags.None, ref remoteEP, onReceive, null); } }