/// <summary> /// Decode a datagram packet into this DhcpMessage object. /// </summary> /// <param name="buf">ByteBuffer containing the packet to be decoded</param> public void Decode(ByteBuffer buf) { log.Debug("Decoding DhcpMessage from: " + remoteAddress); if ((buf != null) && buf.hasRemaining()) { DecodeMessageType(buf); if (buf.hasRemaining()) { SetTransactionId(DhcpTransactionId.Decode(buf)); if (log.IsDebugEnabled) { log.Debug("TransactionId=" + transactionId); } if (buf.hasRemaining()) { DecodeOptions(buf); } else { string errmsg = "Failed to decode options: buffer is empty"; log.Error(errmsg); throw new IOException(errmsg); } } else { string errmsg = "Failed to decode transaction id: buffer is empty"; log.Error(errmsg); throw new IOException(errmsg); } } else { string errmsg = "Failed to decode message: buffer is empty"; log.Error(errmsg); throw new IOException(errmsg); } if (log.IsDebugEnabled) { log.Debug("DhcpMessage decoded."); } }
/// <summary> /// Encode this DhcpMessage to wire format for sending. /// </summary> /// <returns>a ByteBuffer containing the encoded DhcpMessage</returns> public ByteBuffer Encode() { if (log.IsDebugEnabled) { log.Debug("Encoding DhcpMessage for: " + remoteAddress); } ByteBuffer buf = ByteBuffer.allocate(1024); buf.put((byte)MessageType); buf.put(DhcpTransactionId.Encode(transactionId)); buf.put(EncodeOptions()); buf.flip(); if (log.IsDebugEnabled) { log.Debug("DhcpMessage encoded."); } return(buf); }