C 对UDP数据包解码的建议
我正在开发一个C应用程序来与一个实际的控制系统设备进行通信。该设备使用定义良好的协议结构。例如,考虑设备在请求时作为UDP包发送的一个结构:-< /P>C 对UDP数据包解码的建议,c,sockets,udp,C,Sockets,Udp,我正在开发一个C应用程序来与一个实际的控制系统设备进行通信。该设备使用定义良好的协议结构。例如,考虑设备在请求时作为UDP包发送的一个结构:-< /P> typedef struct poll_request_s { uint16 poll_number; /* poll number mirrored from the * poll request */ uint16
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\u list\t
是一个包含各种属性的结构,该列表中的每个属性都由一个标识符编号标识,该标识符编号为uint16
(16位整数)。简言之,协议的工作原理如下:-
- 你需要一些数据
- 您以属性列表的形式获取数据
- 属性列表中的每个属性都有对象标识符
- 使用此对象标识符解析每个属性(转换为主机字节顺序)
- 属性本身可能包含更多属性列表。(属性起始)
atAttribute\u 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\u data
只是一个用于保存列表中所有属性的占位符。事实上,必须将此attrs_数据
强制转换到另一个名为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 */
}
现在,为了迭代和解析此结构中的属性,我正在使用此算法(下面的伪代码):
现在,我的问题是:
- 尽管如此,我已经在上述算法中展示了
方法,但实际上,我使用了20到30个HASH_TABLE
。因为C在IF-ELSE
中没有stdlib
哈希表。协议中大约有600个结构,我不想编写600
。对于根据这些结构的if-else
解析这些结构,您给出了哪些建议和方法属性\u id
- 另一个问题是我定义的结构中的编译器填充。我的所有结构都是用数据容器的
字段定义的。现在,当我收到消息时,几乎每个属性都包含灵活数组
,但是这个length
不能用于length
我的解析结构,因为编译器可能会神奇地添加一些填充字节,而我会缺少字节。为了安全起见,我通常使用malloc..ing
大约malloc..ing
字节。事实上,在我看来,这是一种糟糕的内存管理实践。对这个问题有什么建议吗长度+300
malloc..ing
解析接收到的消息的结构是迄今为止我遇到的最大问题。我想要一些内存效率高、速度快的方法
另外,如果你已经做过这样的项目,你愿意分享你的方法吗?有什么建议或意见可以让我走上正确的方向吗?我希望设计简单,而不会使事情变得不必要的复杂。我强烈建议您不要使用C
结构来定义网络协议<代码>C结构布局取决于:
使用XDR或其他可以提供标准格式的东西。您应该能够准确定义XDR中的内容,并自动完成所有编码和解码。谢谢您的回答!这听起来很有趣,但我实际上需要过滤这些数据并执行一些逻辑操作和GUI更新?XDR似乎是表示数据的唯一方式?我可以用它来控制实时设备吗?任何关于XDR的链接或更多信息都会有所帮助。XDR是外部数据表示。它基本上是一种编译成C代码的接口定义语言(IDL),您可以编写自己的扩展。您可以使用它来流式传输,或者在内存中使用它。它不做任何其他事情。它是SUN RPC的一部分。据我所知,它内置于大多数Unix和Linux中。尝试
man rpcgen
。如果协议是由设备定义的,您可能无法将其映射到XDR,但快速查看,我看不到任何不映射的内容。我应该注意,也可以手动完成,将自己的flatten()/unflatten()函数写入memcpy(),将结构中的每个值都写入字节数组,反之亦然(如果您想指定,也可以选择使用endian交换)。这有点繁琐,但确实会使您的网络算法独立于上面列出的编译器gotchas EJP。由于无意义地使用typecast而向您发送坏想法
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]);
}