CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    26

    Resolved 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?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: typcasting a struct

    Quote Originally Posted by vladic256 View Post
    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

  3. #3
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: typcasting a struct

    Quote Originally Posted by VictorN View Post
    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

  4. #4
    Join Date
    Mar 2009
    Posts
    26

    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.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: typcasting a struct

    Quote Originally Posted by ZuK View Post
    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

  6. #6
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  7. #7
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: typcasting a struct

    Quote Originally Posted by vladic256 View Post
    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.

  9. #9
    Join Date
    Jan 2009
    Posts
    596

    Re: typcasting a struct

    Quote Originally Posted by S_M_A View Post
    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:
    Code:
    int array_name[];
    Last edited by Peter_B; May 8th, 2012 at 04:20 PM.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    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
  •  





Click Here to Expand Forum to Full Width

Featured