Solved

ntohl and htonl

Posted on 2009-05-08
6
918 Views
Last Modified: 2012-05-06
In my assignment I am going to send structs over the UDP datagram.. the spec says that everything is in network byte order..

one example of the struct is the following.

So before sending the struct to the other machine, which is a 64 bit machine... what should I do? Should I call htonl?? as I've tried that and the other machine receives the status of the structs differently
typedef struct AARP_Packet {

    u_char			version;		// AARP_VERSION

    u_char			operation;		// See below

    AIP_Addr		aip;		// AIP address

    struct in_addr	ip;			// IP address

    in_port_t		port;		// Port

    u_short			status;			// See below

} AARP_Packet;
 
 

AARP_Packet* temp = malloc(sizeof(AARP_Packet));

Open in new window

0
Comment
Question by:kuntilanak
  • 3
  • 2
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 24336096
>> what should I do? Should I call htonl??

Correct.

>> as I've tried that and the other machine receives the status of the structs differently

The other side will need to do ntohl to convert it back from network byte order to host byte order.


A few extra thoughts :

(a) did you make sure that u_short is a 32 bit unsigned integer type on both platforms ? How about all other used types - do they have the same sizes on both platforms ?
(b) did you make sure that there is no struct padding added ?
0
 

Author Comment

by:kuntilanak
ID: 24336178
how can I make sure the u_short is a 32 bit unsigned integer type??

yes, I think the struct is not padded.. AIP_Addr here is just an u_short, it's just another name for it.. so say when I want to send a struct with AIP_Addr of 2, I should do the following:

Is the code below correct?

I've attached a picture on how I laid out the struct


temp->aip = ntohl(2);

Open in new window

pic.JPG
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24336307
>> how can I make sure the u_short is a 32 bit unsigned integer type??

Did you define it to be a 32bit unsigned integer type ?
How is it defined ?


>> yes, I think the struct is not padded..

If you're not sure, you can't rely on it. The compiler is free to add padding if it thinks that's useful. Some compilers have switches to force it not to add padding.


>> Is the code below correct?

All depends on your struct layout, and the element sizes.


Can you post what the output of this code is for both platforms :
#include <stddef.h>
 

fprintf(stdout, "sizeof(u_char) = %u\n", sizeof(u_char));

fprintf(stdout, "sizeof(AIP_Addr) = %u\n", sizeof(AIP_Addr));

fprintf(stdout, "sizeof(struct in_addr) = %u\n", sizeof(struct in_addr));

fprintf(stdout, "sizeof(in_port_t) = %u\n", sizeof(in_port_t));

fprintf(stdout, "sizeof(u_short) = %u\n", sizeof(u_short));

fprintf(stdout, "sizeof(AARP_Packet) = %u\n", sizeof(AARP_Packet));
 

fprintf(stdout, "offsetof(AARP_Packet, version) = %u\n", offsetof(AARP_Packet, version));

fprintf(stdout, "offsetof(AARP_Packet, operation) = %u\n", offsetof(AARP_Packet, operation));

fprintf(stdout, "offsetof(AARP_Packet, aip) = %u\n", offsetof(AARP_Packet, aip));

fprintf(stdout, "offsetof(AARP_Packet, ip) = %u\n", offsetof(AARP_Packet, ip));

fprintf(stdout, "offsetof(AARP_Packet, port) = %u\n", offsetof(AARP_Packet, port));

fprintf(stdout, "offsetof(AARP_Packet, status) = %u\n", offsetof(AARP_Packet, status));

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:kuntilanak
ID: 24336357
This is what I got on the 32 bit machine and I can't test it on the 64 bit machine, but it can always be assumed to parse it correctly as it's given as a part of the assignment in binary...
sizeof(u_char) = 1

sizeof(AIP_Addr) = 2

sizeof(struct in_addr) = 4

sizeof(in_port_t) = 2

sizeof(u_short) = 2

sizeof(AARP_Packet) = 12

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24336423
>> but it can always be assumed to parse it correctly as it's given as a part of the assignment in binary...

Ok.

Then you'll need to show a bit more code :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24338401
Just as an observation, which I'll let I8 elaborate on if he so chooses (and this isn't mean to contridict anything said so far), generally the safest way to send compound objects across heterogeneous networks is to serialise them first. This removes doubt over things such as packing or data type sizes. It is; however, more work since you need to define a serialization protocol.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now