1

現在、c# で DHCPMessage クラスを作成しています。

RFC はこちらから入手できます: http://www.faqs.org/rfcs/rfc2131.html

擬似

public object DHCPMessage
{
    bool[8] op;
    bool[8] htype;
    bool[8] hlen;
    bool[8] hops;
    bool[32] xid;
    bool[16] secs;
    bool[16] flags;
    bool[32] ciaddr;
    bool[32] yiaddr;
    bool[32] siaddr;
    bool[32] giaddr;
    bool[128] chaddr;
    bool[512] sname;
    bool[1024] file;
    bool[] options;
}

各フィールドが固定長のビット配列であると想像すると、次のようになります。

  1. 最も汎用性の高い
  2. ベスト プラクティス

これをクラスとして表現する方法???

または..これをどのように書きますか?:)

4

3 に答える 3

11

まず、BitArrayクラスを試すことができます。ここで車輪を再発明する必要はありません。

あまりにも多くのスペース/メモリを占有することを心配している場合でも、心配しないでください。適切なサイズに初期化するだけです:

BitArray op = new BitArray(8);

(上記は8ビットを保持し、1バイトを取る必要があります)

于 2010-04-19T17:00:46.950 に答える
4

You are on the wrong track with this, it isn't a bit vector. The message is defined in "octets", better known as "bytes". An equivalent C# declaration that you can use with Marshal.PtrToStructure is:

    [StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
    struct DHCPMessage {
        public byte op;
        public byte htype;
        public byte hlen;
        public byte hops;
        public uint xid;
        public ushort secs;
        public ushort flags;
        public uint ciaddr;
        public uint yiaddr;
        public uint siaddr;
        public uint giaddr;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst=16)]
        public byte[] chaddr;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]
        public string sname;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]
        public string file;
    }

You'll need to handle the variable length options field separately.

于 2010-04-19T17:39:57.777 に答える
2

これらのいくつかにビット配列を使用してもよろしいですか?たとえば、8ビットの場合はbyte、32ビットの場合はint、「sname」などのnullで終了する文字列にマップする部分にはバイト配列を使用できます。次に、単純なビット演算子(&、|)を使用して、ビットをチェック/操作できます。

TCPヘッダーを構造体に変換する際に行ったいくつかの投稿があります。これにはエンディアンなども含まれます。

http://taylorza.blogspot.com/2010/04/archive-structure-from-binary-data.html http://taylorza.blogspot.com/2010/04/archive-binary-data-from-structure.html

これらはかなり古いので、迷子にならないように古いブログから移行しました。

于 2010-04-19T17:14:13.963 に答える