public override string ToString() { var sb = new StringBuilder(); sb.Append("Message Type (op): ").AppendLine(MessageType.ToString()); sb.Append("Hardware Address Type (htype): ").AppendLine(HardwareAddressType.ToString()); sb.Append("Hardware Address Length (hlen): ").AppendLine(HardwareAddressLength.ToString()); sb.Append("Gateway Hops (hops): ").AppendLine(GatewayHops.ToString()); sb.Append("Transaction Id (xid): ").AppendLine(TransactionId.ToString()); sb.Append("Seconds Elapsed (secs): ").AppendLine(SecondsElapsed.ToString()); sb.Append("Flags (flags): ").AppendLine(Convert.ToString((int)Flags, 2)); foreach (DhcpServerPacketFlags flag in Enum.GetValues(typeof(DhcpServerPacketFlags))) { sb.Append(" "); var mask = Convert.ToString((short)flag, 2).Replace('0', '.'); if (Flags.HasFlag(flag)) { sb.Append(mask).Append(": ").AppendLine(flag.ToString()); } else { sb.Append(mask.Replace('1', '0')).Append(": No ").AppendLine(flag.ToString()); } } sb.Append("Client IP Address (ciaddr): ").AppendLine(ClientIpAddress.ToString()); sb.Append("Your IP Address (yiaddr): ").AppendLine(YourIpAddress.ToString()); sb.Append("Next Server IP Address (siaddr): ").AppendLine(NextServerIpAddress.ToString()); sb.Append("Relay Agent IP Address (giaddr): ").AppendLine(RelayAgentIpAddress.ToString()); sb.Append("Client Hardware Address (chaddr): ").AppendLine(ClientHardwareAddress.ToString()); sb.Append("Server Host Name (sname): ").AppendLine(ServerHostName); sb.Append("File Name (file): ").AppendLine(FileName); sb.Append("Options Magic Cookie: ").AppendLine(OptionsMagicCookie.ToString()); var options = Options.ToList(); if (options.Count == 0) { sb.Append("Options: None"); } else { sb.Append("Options:"); foreach (var option in options) { sb.AppendLine(); sb.Append(" "); sb.Append(((byte)option.Id).ToString("000")); sb.Append(" "); sb.Append(option.Id.ToString()); sb.Append(" ["); sb.Append(option.Type.ToString()); sb.AppendLine("]"); sb.Append(" "); option.DataAsFormatted(sb); } } return(sb.ToString()); }
public DHCPOptionClientId(int optionLength, byte[] buffer, long offset) { IdType = (HardwareAddressType)buffer[offset]; switch (IdType) { case HardwareAddressType.Ethernet: { var ethernetAddress = new byte[6]; Array.Copy(buffer, offset + 1, ethernetAddress, 0, 6); _clientId = new EthernetClientHardwareAddress(ethernetAddress); } break; default: _clientId = new GenericClientHardwareAddress(buffer, offset + 1, optionLength - 1); break; } }
public Lease ReserveByRelayAddress(IPEndPoint remoteRelay, ClientHardwareAddress clientId, UInt32 transactionId) { var pool = GetPoolByIP(remoteRelay.Address); if (pool == null) { System.Diagnostics.Debug.WriteLine("No DHCP pool exists for a network containing the IP address : " + remoteRelay.ToString()); return(null); } System.Diagnostics.Debug.WriteLine("Selected DHCP pool [" + pool.Network.ToString() + "] for request relayed by [" + remoteRelay.ToString() + "]"); var addressPool = AddressPools.Where(x => x.Pool == pool).FirstOrDefault(); if (addressPool == null) { throw new Exception("There is no available address pool for the given DHCP pool"); } System.Diagnostics.Debug.WriteLine("Cleaning DHCP pool - " + pool.Network.ToString()); CleanPool(pool, addressPool); // TODO : Track a counter for pending reservations instead of running large queries { var leasesFromPool = Leases .Where(x => x.Pool == pool ).OrderBy(x => x.Address.ToUInt32() ).ToList(); var incompleteReservations = leasesFromPool.Where(x => x.Acknowledged == null).Count(); if (incompleteReservations >= pool.MaxIncompleteRequests) { System.Diagnostics.Debug.WriteLine("Warning: There are currently " + incompleteReservations.ToString() + " for the pool " + pool.ToString() + " no reservation will be made. Possible security issue."); return(null); } } var nextAvailableAddress = addressPool.ReserveNextAvailableAddress(); if (nextAvailableAddress == null) { System.Diagnostics.Debug.WriteLine("No IP addresses were available in pool " + pool.Network.ToString()); return(null); } var leaseOptions = new LeaseOptions { BootFile = pool.PoolOptions.BootFile, DNSServers = pool.PoolOptions.DNSServers, TFTPServers = pool.PoolOptions.TFTPServers, Hostname = nextAvailableAddress.ToHexString() }; var now = DateTimeOffset.Now; var newLease = new Lease { Address = nextAvailableAddress, Options = leaseOptions, Pool = pool, Requested = now, Expires = now.Add(pool.LeaseDuration), TimesOut = now.Add(pool.RequestTimeOut), Acknowledged = DateTimeOffset.MinValue, Renewed = DateTimeOffset.MinValue, ClientId = clientId.Clone(), TransactionId = transactionId }; Leases.Add(newLease); return(newLease); }
public Boolean TryWriteTo(Byte[] buffer) { try { buffer[0] = (Byte)Operation; buffer[1] = (Byte)HardwareAddressType; buffer[2] = HardwareLength; buffer[3] = Hops; BitConverter.TryWriteBytes(new Span <Byte>(buffer, 4, 4), SessionId); BitConverter.TryWriteBytes(new Span <Byte>(buffer, 8, 2), Seconds); BitConverter.TryWriteBytes(new Span <Byte>(buffer, 10, 2), Flags); ClientIpAddress.TryWriteBytes(new Span <Byte>(buffer, 12, 4), out _); YourIpAddress.TryWriteBytes(new Span <Byte>(buffer, 16, 4), out _); ServerIdentifierAddress.TryWriteBytes(new Span <Byte>(buffer, 20, 4), out _); GatewayOrRelayAddress.TryWriteBytes(new Span <Byte>(buffer, 24, 4), out _); ClientHardwareAddress.GetAddressBytes().CopyTo(new Span <Byte>(buffer, 28, 16)); Encoding.UTF8.GetBytes(ServerHostName).CopyTo(new Span <Byte>(buffer, 44, 64)); Encoding.UTF8.GetBytes(BootFileName).CopyTo(new Span <Byte>(buffer, 108, 128)); BitConverter.TryWriteBytes(new Span <Byte>(buffer, 236, 4), MagicCookie); var index = 240; foreach (var option in Options) { buffer[index] = (Byte)option.Key; Byte dataLength = 0; switch (option.Value) { case IPAddress iPAddress: buffer[index + 1] = dataLength = 4; iPAddress.TryWriteBytes(new Span <Byte>(buffer, index + 2, dataLength), out _); break; case IPAddress[] iPAddresses: buffer[index + 1] = dataLength = (Byte)(4 * iPAddresses.Length); var offset = 0; foreach (var iPAddress in iPAddresses) { iPAddress.TryWriteBytes(new Span <Byte>(buffer, index + 2 + offset, dataLength), out _); offset += 4; } break; case Byte[] bytes: buffer[index + 1] = dataLength = (Byte)bytes.Length; bytes.CopyTo(new Span <Byte>(buffer, index + 2, dataLength)); break; case Byte b: buffer[index + 1] = dataLength = 1; buffer[index + 2] = b; break; case MessageType messageType: buffer[index + 1] = dataLength = 1; buffer[index + 2] = (Byte)messageType; break; case UInt32 uintVal: buffer[index + 1] = dataLength = 4; BitConverter.TryWriteBytes(new Span <Byte>(buffer, index + 2, dataLength), uintVal); break; default: throw new NotSupportedException(); } index += dataLength + 2; } } catch { return(false); } return(true); }
public DHCPOptionClientId(ClientHardwareAddress clientId) { ClientId = clientId; }