-
May 8th, 2012, 02:23 PM
#1
typcasting a struct
Hello,
Currently am working on a project which creates a fake ip package and transmits that packet to a second copy of the same program with a different address. My goal is is to create a fake TCP struct and transmit that over the ip packet.
as of now for testing my struct is define as this
Code:
struct tcp
{
string message;
};
an ip struct is define as this.
Code:
struct ip_packet
{ byte source_addr[6];
byte dest_addr[6];
short int protocol;
short int length;
byte content[0]; };
where the byte content[0] is where you store the data. Byte is declare like this "typedef unsigned char byte;"
my idea is to piggy back the tcp struct inside the ip packet by using typecast like this
Code:
tcp a;
ip_packet * x;
x->content[0] = (byte)&a;
it compiles just fine, but gives me a warning that states
warning: cast from pointer to integer of different size
when the program runs on and the second copy of the program gets the ip packet I just get a segmentation fault.
on the receiving end of the code I have it like this
Code:
byte b;
tcp rev;
b = p->content[0];
rev = (faketcp &)b;
any idea of how to properly typecast this?
-
May 8th, 2012, 02:42 PM
#2
Re: typcasting a struct
Originally Posted by vladic256
an ip struct is define as this.
Code:
struct tcp
{
string message;
};
Code:
tcp a;
ip_packet * x;
x->content[0] = (byte)&a;
Get rid of tcp struct.
just use something like
Code:
string message;
....
p_packet * x;
x->content = (byte*)message.c_str();
Victor Nijegorodov
-
May 8th, 2012, 02:56 PM
#3
Re: typcasting a struct
Originally Posted by VictorN
Code:
x->content = (byte*)message.c_str();
You can't assign to an array.
Use strcpy() instead.
Code:
strcpy( (char *)x->content, message.c_str());
Kurt
-
May 8th, 2012, 03:01 PM
#4
Re: typcasting a struct
Well the TCP struct was just for testing. My plans is too add more to that struct and be able to transmit the complete struct to the other end of my transmission.
-
May 8th, 2012, 03:07 PM
#5
Re: typcasting a struct
Originally Posted by ZuK
You can't assign to an array.
Use strcpy() instead.
Code:
strcpy( (char *)x->content, message.c_str());
Kurt
Well, before copy you have to allocate a buffer to copy to.
Victor Nijegorodov
-
May 8th, 2012, 03:14 PM
#6
Re: typcasting a struct
Well. I understand that arrays of size 0 are not legal. Some/most C compilers let you get away with this kind of structures just to be able to append some data to this structures.
Usually used this way
Code:
char * content = "some string";
struct ip_packet * packet = malloc( sizeof(struct ip_packet) + strlen(content) +1 );
strcpy(packet->content, content);
packet->length = strlen(content);
Kurt
-
May 8th, 2012, 03:34 PM
#7
Re: typcasting a struct
That way of doing it (ending a struct with a zero length array that is) even has a name that I unfortunately not can remember at the moment...
Last edited by S_M_A; May 8th, 2012 at 03:42 PM.
-
May 8th, 2012, 03:52 PM
#8
Re: typcasting a struct
Originally Posted by vladic256
Well the TCP struct was just for testing. My plans is too add more to that struct and be able to transmit the complete struct to the other end of my transmission.
The key point which you seem to have missed is that you cannot simply send a "flat" struct if it contains a non-POD type such as a std::string. You'll be transmitting the bytes which contain the string internals you don't care about, not the bytes which contain the string data that you do want.
-
May 8th, 2012, 04:16 PM
#9
Re: typcasting a struct
Originally Posted by S_M_A
That way of doing it (ending a struct with a zero length array that is) even has a name that I unfortunately cannot remember at the moment...
In C99 they are called 'flexible array members', and are declared like this:
Last edited by Peter_B; May 8th, 2012 at 04:20 PM.
-
May 8th, 2012, 07:36 PM
#10
Re: typcasting a struct
[QUOTE=vladic256;2066868]as of now for testing my struct is define as this
Code:
struct tcp
{
string message;
};
This won't work as Lindley pointed out. You can't take non-POD objects, break them down into bytes, send those bytes over a socket (or save to a file, or whatever you want to do with these bytes) and magically that same object shows up on the other side of the socket. "Teleportation" won't work with C++ non-POD objects. The only types you can transmit are POD types, i.e. 'C'-compatible types -- int, short, double, float, long, char, etc. and arrays/structs containing these types and only these types.
To create C++ non-POD objects, those objects must be properly constructed. This means the object must be created by having the constructor create the object. What you must do is send the data the string object holds, not the object itself. On the other side, you declare a string, and set the string to the data that is coming in.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 8th, 2012 at 07:42 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|