コード例 #1
0
        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());
        }
コード例 #2
0
ファイル: Message.cs プロジェクト: TrabacchinLuigi/DHCPServer
        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);
        }