473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Alignment issues in network programming

2
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

Expand|Select|Wrap|Line Numbers
  1. /// features req 
  2. struct ofp_header {
  3.     uint8_t version;    /* OFP_VERSION. */
  4.     uint8_t type;       /* One of the OFPT_ constants. */
  5.     uint16_t length;    /* Length including this ofp_header. */
  6.     uint32_t xid;       /* Transaction id associated with this packet.
  7.                            Replies use the same id as was in the request
  8.                            to facilitate pairing. */
  9. };
  10. assert(sizeof(struct ofp_header) == 8); 
  11.  
  12.  
  13. /* Switch features. */
  14. struct ofp_switch_features {
  15. struct ofp_header header;
  16. uint64_t datapath_id; /* Datapath unique ID. The lower 48-bits are for a MAC address, while the upper 16-bits are implementer-defined. */
  17. uint32_t n_buffers; /* Max packets buffered at once. */
  18. uint8_t n_tables; /* Number of tables supported by datapath. */
  19. uint8_t pad[3]; /* Align to 64-bits. */
  20.  
  21. /* Features. */ /* Bitmap of support "ofp_capabilities". */
  22. uint32_t capabilities; /* Bitmap of supported "ofp_action_type"s. */
  23. uint32_t actions; 
  24.  
  25. /* Port info.*/
  26. struct ofp_phy_port ports[0];  /* Port definitions. The number of ports is inferred from the length field in the header. */
  27. };
  28. 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

Expand|Select|Wrap|Line Numbers
  1. ////// features request and reply  ////////////
  2.  
  3. ofp_header features_req;
  4.  
  5. features_req.version=OFP_VERSION;
  6. features_req.type=OFPT_FEATURES_REQUEST;
  7. features_req.length= htons(sizeof features_req);
  8. features_req.xid = htonl(rcv_hello.xid);
  9.  
  10.  
  11. if (send(connected, &features_req, sizeof(features_req), 0)==-1) {
  12. printf("Error in sending message\n");
  13. exit(-1);
  14. }
  15. printf("features req sent!\n");
  16.  
  17.  
  18.  
  19. ofp_switch_features features_rep={0};
  20.  
  21. if (recv(connected, &features_rep, sizeof(features_rep), 0)==-1) {
  22. printf("Error in receiving message\n");
  23. exit(-1);
  24. }
  25.  
  26. printf("message type : %d\n",features_rep.header.type);
  27. printf("version : %d\n",features_rep.header.version);
  28. printf("message length: %d\n",ntohs(features_rep.header.length));
  29. printf("xid : %d\n",ntohl(features_rep.header.xid));
  30. printf("buffers: %d\n",ntohl(features_rep.n_buffers));
  31. printf("tables: %d\n",features_rep.n_tables);
thanks,
Abdullah
Nov 10 '11 #1
0 1332

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Tom Plunket | last post by:
Hey all- A friend of mine has built this interesting (to me) bit of hardware that's intended to communicate with a PC over ethernet. The hardware has an OS written in C, and has some PC-side...
3
by: Jay | last post by:
Hi, I implemeneted an FTP client and server long time back using Java. I found sockets porgramming in java quite useful and easy to handle. I now wanted to implement a similar program using C++....
6
by: Eric | last post by:
Does anyone know of any GOOD network programming book(s) that are C# based, as well as any online tutorials and forums on network programming? Thanks
7
by: Brian | last post by:
Can anyone help me to understand how to make VB .NET transmit text entered in a text box through a serial port while providing the user with the ability to select baud rate, parity, data bits, stop...
1
by: Thr | last post by:
Hi, I read some books about network programming.. Still, I have some questions: 1. let's assume that we have http server written using nonblocking i/o and select(). How is it possible to...
10
by: Lionel B | last post by:
Greetings, I have some code that is to read unformatted data from disc and interpret it as blocks of unsigned integers. In an attempt to achieve efficiency (it is pretty essential for my...
1
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but...
2
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
On Sep 22, 10:45 am, Nick Keighley <nick_keighley_nos...@hotmail.com> wrote: Only problem is that then entire frame in memory might have strange alignment.
8
by: Stephen Horne | last post by:
I understand that the next C++ standard will have features to handle the alignment of data types. This is good, but a bit late for me! I've been using some template trickery to handle alignment...
3
by: Nyris | last post by:
There's a problem with my companies website. For the employees that have larger screens the website's alignment is off only on a few things. Also there are spaces in the dropdown navigation between...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.