我正在开发一个 C 应用程序来与真实控制系统设备之一进行通信。该设备使用定义明确的协议(protocol)结构。例如,考虑设备在请求时作为 UDP 数据包发送的结构之一:-
typedef struct poll_request_s {
uint16 poll_number; /* poll number mirrored from the
* poll request */
uint16 length; /* length of the message */
/* till above it was all header section this is ACTUAL DATA */
attribute_list_t attr_list; /* this attribute list contains
* various attributes */
} poll_request_t
现在,attribute_list_t
是一个封装了各种属性的结构,该列表中的每个属性都由一个标识符号来标识,该标识符号为uint16
(16位整数)。所以,简而言之,协议(protocol)的工作原理如下:-
- 您请求一些数据。
- 您以属性列表的形式获取数据。
- 属性列表中的每个属性都有对象标识符。
- 您可以使用此对象标识符解析每个属性(转换为主机字节顺序)。
- 属性本身可能包含更多属性列表。 (属性-起始)
这个atrtribute_list_t
结构如下:-
typdef struct attribute_list_s {
uint16 length; /* length of attribute list */
uint16 count; /* number of attributes in this list */
uint8 attrs_data[]; /* another container to hold attributes' data */
} attribute_list_t
现在,attrs_data
只是一个占位符,用于保存列表中的所有属性。事实上,这个 attrs_data
必须转换为另一个名为 ava_type
的结构才能读取属性信息。
typdef struct ava_type_s {
uint16 attr_id; /* type of attribute */
uint16 length; /* length of this attribute
*(this length of single attribute not whole list*/
uint8 data[]; /* another flexible array to hold data for this
* attribute type */
}
现在,为了迭代和解析此结构中的属性,我当前正在使用此算法(下面的伪代码):
uint8* packet = recv_packet(SOCKET);
/* this is used as packet iterator pointer */
unit8* packet_ptr = packet;
parsed_packet_t parsed_packet = malloc(SOME_SIZE);
.
. /* do header un-packing */
.
/* dont need attribute_list length so skip this 2 bytes */
parsed_packet += 2;
/* parsed packet do nee count of attributes */
parsed_packet.attribute_list->count = NTOHS(packet_ptr);
packed_ptr += 2; /* skip count */
/* now packet_ptr is pointer to attr_list */
offset = 0, i = 0;
for(i = 0 to attr_list->count) {
/* cast the attributes' data to ava_type */
packet_ptr += offset;
/* parse object identifier */
parsed_packet.attribute_list->data[i++].object_id = NTOHS(packet_ptr);
packet_ptr += 2; /* skip 2 bytes */
/* next offset would be attribute length of this packet */
attribute_length += 2 + NTOHS(packet_ptr);
packet_ptr += 2;
/* now we are pointer to actual data of i(th) attribute */
/* I've made this parser structure and hash table to
* retrieve the parser for particular attr_id */
parser* p = HASH_TABLE(ava_type->attr_id);
/* parser has function point for network order to host
* order and visa-versa */
p->ntoh(ava_type, parsed_packet.attribute_list->data[i]);
}
现在,我的问题是:
- 尽管如此,我在上述算法中展示了
HASH_TABLE
方法,但实际上,我使用了 20 到 30 个IF-ELSE
。由于 C 在stdlib
中没有哈希表
。协议(protocol)中有大约600个结构,我不想写600个if-else
。对于根据attribute_id
解析这些结构,您给出了哪些建议和方法。 - 另一个问题是编译器在我定义的结构中填充。我的所有结构都是用数据容器的
灵活数组
字段定义的。现在,当我收到消息时,它们包含几乎每个属性的length
,但这个length
不能用于malloc..ing
我的解析的结构,因为编译器可能会神奇地添加一些填充字节,而我会缺少字节。出于安全目的,我通常malloc..ing
大约lenght + 300
字节。事实上,在我看来,这看起来是糟糕的内存管理实践。对于这个问题有什么建议吗?
malloc..ing
解析接收到的消息的结构是迄今为止对我来说最大的问题。我想要一些内存高效且快速的方法来做到这一点?
另外,如果您已经做过此类项目,能否分享一下您的方法?有什么建议或意见可以让我朝着正确的方向前进吗?我想要简单的设计,不要让事情变得不必要的复杂化。
最佳答案
我强烈建议您不要使用C
结构来定义网络协议(protocol)。 C
结构布局依赖于:
- 硬件
- 编译器
- 编译器版本
- 源代码
- 源代码中嵌入的#pragmas(如果有)
- 编译源代码时生效的编译器选项。
使用 XDR 或其他能为您提供标准格式的东西。您应该能够准确定义 XDR 中现在的内容,并自动为您完成所有编码和解码。
关于c - 关于解码UDP数据包的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10826411/