字节排成一条直线 [英] Byte Allignment
问题描述
我面临的一个问题位在编写网络软件。当我尝试发送或接收包含8个字节的下一个发送或接收的结构以某种方式影响了数据类型的结构。我想到了一些东西,但首先我想确认一件事之前,我进入调试。
我是一个64位x-86系统上使用32位的Ubuntu 11.04(愚蠢的我)。这是否有什么关系字节对齐的问题?
我开发的控制器与开放式流量开关进行通信。该协议OpenFlow技术定义了一组规格在此基础上开关建成。问题是,当我尝试沟通与交换机一切顺利,直到我发送或接收包含一个64位的日期类型(uint64_t中)一个结构。了用于发送和接收功能的具体结构是
estruct ofp_header {
uint8_t有版本; / * OFP_VERSION。 * /
uint8_t有型; / *其中一个OFPT_常量。 * /
uint16_t长度; / *长度包括本ofp_header。 * /
uint32_t的XID;与此相关的数据包/ *事务ID。
答复使用相同的ID是在请求
以方便配对。 * /};
断言(的sizeof(结构ofp_header)== 8);/ *切换功能。 * /
结构ofp_switch_features {
结构ofp_header头;
uint64_t中datapath_id; / *数据路径的唯一ID。低48位用于MAC地址,而高位的16位是实施者定义。 * /
uint32_t的n_buffers; / *最大数据包缓冲一次。 * /
uint8_t有n_tables; / *由数据通路支持的表数。 * /
uint8_t有垫[3]; / *对齐到64位。 * //* 特征。 * / / *位图的支持ofp_capabilities。 * /
uint32_t的能力; / *支持ofp_action_typeS的位图。 * /
uint32_t的行动;/ *端口信息。* /
结构ofp_phy_port端口[0]; / *端口定义。端口的数量是从标头中的长度字段推断。 * /
};
断言(的sizeof(结构ofp_switch_features)== 32);
问题是,当我使用通讯有不到64位一切顺利的数据类型的任何其他结构。当我收到回复功能,它显示了正确的价值观,但在那之后,如果我收到任何其他结构这表明垃圾值。即使我收到的特点再次回复我得到垃圾值。总之,如果在code中的任何一点上,我收到的功能要求或在具有64位的数据类型,接下来的结构收到垃圾值的规格定义的任何其他结构。在code用于发送和接收功能的要求如下:
//////功能请求和应答////////////ofp_header features_req;features_req.version = OFP_VERSION;
features_req.type = OFPT_FEATURES_REQUEST;
features_req.length = htons(sizeof的features_req);
features_req.xid = htonl(rcv_hello.xid);
如果(发送(连接,与放大器; features_req,sizeof的(features_req),0)== - 1){
的printf(错误在发送邮件\\ n);
出口(-1);
}
输出(功能发送REQ \\ n!);ofp_switch_features features_rep = {0};如果(的recv(连接,与放大器; features_rep,sizeof的(features_rep),0)== - 1){
的printf(错误接收邮件\\ n);
出口(-1);
}的printf(消息类型:%d个\\ N,features_rep.header.type);
的printf(版本:%d个\\ N,features_rep.header.version);
的printf(消息长度数:%d \\ n,还有ntohs(features_rep.header.length));
的printf(XID数:%d \\ n,ntohl(features_rep.header.xid));
的printf(缓冲区数:%d \\ n,ntohl(features_rep.n_buffers));
的printf(表数:%d \\ n,features_rep.n_tables);
谢谢,
阿卜杜拉
- 您转换成结构字符数组发送他们面前 - 这是调用的系列化的
- 使用功能
htons
等家庭,以确保整数网络顺序发送。节省了各种机器的endians麻烦 - 其中recieving结束读取的字节数和重建结构。
这将确保你不会有任何麻烦的。
I am facing a bit of issue in writing a network software. When I try to send or receive a struct that contains a data type of 8 bytes the next sent or received struct is somehow affected. I have a few things in mind but first I wanted to confirm one thing before I get into debugging. I am using 32-bit Ubuntu 11.04 (silly me) on a 64-bit x-86 system. Does this has anything to do with the byte alignment problems ?
I am developing a controller to communicate with the Open Flow switch. The openflow protocol defines a set of specs based on which switches are built. The problem is when I try to communicate with the switch everything goes fine until I send or receive a struct that contains a 64 bit date type (uint64_t). The specific structs that are used for sending and receiving features are
estruct ofp_header {
uint8_t version; /* OFP_VERSION. */
uint8_t type; /* One of the OFPT_ constants. */
uint16_t length; /* Length including this ofp_header. */
uint32_t xid; /* Transaction id associated with this packet.
Replies use the same id as was in the request
to facilitate pairing. */};
assert(sizeof(struct ofp_header) == 8);
/* Switch features. */
struct ofp_switch_features {
struct ofp_header header;
uint64_t datapath_id; /* Datapath unique ID. The lower 48-bits are for a MAC address, while the upper 16-bits are implementer-defined. */
uint32_t n_buffers; /* Max packets buffered at once. */
uint8_t n_tables; /* Number of tables supported by datapath. */
uint8_t pad[3]; /* Align to 64-bits. */
/* Features. */ /* Bitmap of support "ofp_capabilities". */
uint32_t capabilities; /* Bitmap of supported "ofp_action_type"s. */
uint32_t actions;
/* Port info.*/
struct ofp_phy_port ports[0]; /* Port definitions. The number of ports is inferred from the length field in the header. */
};
assert(sizeof(struct ofp_switch_features) == 32);
The problem is when I communicate using any other structs that have data types less than 64-bit everything goes fine. When I receive features reply it shows the right values but after that if i receive any other struct it shows garbage values. Even if i receive features reply again i get garbage values. In short if at any point of code I receive features request or any other struct defined in the specs that has a data type of 64-bit the next structs receive garbage values. The code used for sending and receiving features request is as follows
////// features request and reply ////////////
ofp_header features_req;
features_req.version=OFP_VERSION;
features_req.type=OFPT_FEATURES_REQUEST;
features_req.length= htons(sizeof features_req);
features_req.xid = htonl(rcv_hello.xid);
if (send(connected, &features_req, sizeof(features_req), 0)==-1) {
printf("Error in sending message\n");
exit(-1);
}
printf("features req sent!\n");
ofp_switch_features features_rep={0};
if (recv(connected, &features_rep, sizeof(features_rep), 0)==-1) {
printf("Error in receiving message\n");
exit(-1);
}
printf("message type : %d\n",features_rep.header.type);
printf("version : %d\n",features_rep.header.version);
printf("message length: %d\n",ntohs(features_rep.header.length));
printf("xid : %d\n",ntohl(features_rep.header.xid));
printf("buffers: %d\n",ntohl(features_rep.n_buffers));
printf("tables: %d\n",features_rep.n_tables);
thanks, Abdullah
- Convert your struct into an array of characters before sending them - this is call serialisation
- Use the family of functions
htons
etc to ensure that integers are sent in network order. Saves hassle on the endians of the various machines - One the recieving end read the bytes and reconstruct the struct.
This will ensure that you will not have any hassle at all.
这篇关于字节排成一条直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!