Eagor's Forums » CEN6520 - Project 2 » Trouble with memcpy
Author Topic: Trouble with memcpy
David Mauk
Posts: 17
  Date Posted: 2005-02-07 16:09

I see to be having trouble with getting my Header to map to the 14 byte buffer field I am using in my fragments. Here is the most recent version of what I am doing. Stuff is getting moved in but I do not think that the correct stuff is getting into the buffer.

char *msg
msg=(char *)malloc(sizeof(HEADER_SIZE));
memcpy(msg, &msg_head->Length, 2); /* Fill header */
memcpy(msg+2, &msg_head->SourceIP,4);
memcpy(msg+6, &msg_head->DestIP, 4);
memcpy(msg+10,&msg_head->ID, 1);
memcpy(msg+11,&msg_head->Offset, 2);
memcpy(msg+13,&msg_head->Flag, 1);


Anyone have any ideas why this might not be working or if it even should?
 
jesse_sweetland
Posts: 14
Date Posted: 2005-02-08 19:27

David,

I saw in another (later?) post that you were 75% complete. Did you get this resolved? If so, what did you find the problem to be (in case one of us gets stuck in the same place)?
David Mauk
Posts: 17
Date Posted: 2005-02-08 20:11

Sorry no I am still stuck on that memcpy thing but the coding is what I am refering to. Also I am trying to get a feel for how everyone is progressing.
jesse_sweetland
Posts: 14
Date Posted: 2005-02-08 21:10

I have yet to test my code, but here are some things to look at:

1. Instead of doing msg + 2, try &msg[2]. I don't know if that makes a difference or not (especially with bytes).

2. I got really twisted around with in_addr_t and struct in_addr. I don't know which you are using for SourceIP and DestIP (or if you used long instead), but I ended up switching everything to struct in_addr and using memcmp to test for equality. Since I was using inet_aton and inet_ntoa a lot I found that using struct in_addr was actually easier in the long run (even though it is a struct and not a scalar type).

I would ask to see your struct definition, but I don't know where the line is as far as "not help[ing] at all with any of the coding aspects"...
David Mauk
Posts: 17
Date Posted: 2005-02-08 21:30

I am using long Jesse. But thanks for the advise if I start using those functions it will be usefull information.

Ok I will answer my own question so that if anyone else's tries to do something like I was this might help them not spend 6 hours trying to fix a problem that does not exist.

Here is what I was trying to do: I was trying to use the value of SourceIP that I memcpy'ied into my header buffer. YOU CAN NOT DO THIS I do not even know why I was trying to but it does not work (stupid stupid). memcpy is "stupid" meaning it will just copy the bytes (which we all know) but in so doing it changes the information from a long,int,etc to a string of bytes. Therefore if you (or I) try to ...oh ... say print the value from that memory location it comes up all garbled because printf is "smart" and says "ok that is a long then? great I will read it like a long" (from top to bottom or bottom to top whichever it is. IT IS THE WRONG WAY) anyway to make a long story really short.


memcpy back from the buffer into some variable of the correct type and then compare,use,change,etc.

I would not feel so bad, if this were not the kind of thing that any C programmer should have known/remebered. But there you have it my stupidity saved forever (or until this forum gets cleaned up) for everone (that comes here) to see and mock my pitiful existence.

:)