Thread: append data at end of previous memcpy

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    6

    append data at end of previous memcpy

    Here's the issue, I'm building a payload of TLVs to send onto the network. I need to pass multiple structs of the same type into a single header struct. With the below code I can pass one occurence of the tlv struct into the pHeader struct but then when I go to copy additional tlv structs I think I'm just overwriting memory. How do I append additional tlv structs at the end of the previous tlv struct?

    To be simple, how to I memcpy to the end of the previous memcpy?

    Code:
    int main(int argc, char *argv[]) {
    
    	struct packet
    	{
    	u_int8_t version;
    	u_int8_t length;
    	char val[10];
    	u_int8_t *data;
    	};
    	
    	struct packet *pHeader;
    	pHeader = malloc(sizeof(struct packet) + MAX_PACKET_BUFFER_SIZE);
    	pHeader->version = 0x7;
    	strcpy(pHeader->val,"foobar");
    
    	struct tlvs
    	{
    	u_int8_t type;
    	u_int8_t len;
    	char value[MAX_TLV_VALUE];
    	};
    	
    	struct tlvs *tlv;
    	tlv = malloc(sizeof(struct tlvs));
    
    	int temp = 0;
    	int valueSize = 0;
            int i;
    
    if (argc < 2) 
                       printf("Usage: foobar\n");
    else
        for(i=1; i < argc ;i++)
        {
            switch(argv[i][1])
            {
                case 's':               
                    if (i+1 >= argc)
                       printf("Usage: foobar\n");
    					valueSize = strlen(argv[i]);
    					strcpy(tlv->value,argv[++i]);
    					tlv->type = sAttrib;
    					tlv->len = valueSize + 2;
    					temp += valueSize;
    					break;
                case 'h':               
                    if (i+1 >= argc)
                       printf("Usage: foobar\n");
    					valueSize = strlen(argv[i]);
    					strcpy(tlv->value,argv[++i]);
    					tlv->type = hAttrib;
    					tlv->len = valueSize + 2;
    					temp += valueSize;
    					break;
                case 'f':               
                    if (i+1 >= argc)
                       printf("Usage: foobar\n");
    					valueSize = strlen(argv[i]);
    					strcpy(tlv->value,argv[++i]);
    					tlv->type = fAttrib;
    					tlv->len = valueSize + 2;
    					temp += valueSize;
    					break;
                default:
                    printf("Usage: foobar\n");
                    break;
            }
        }
    	
    
    	/* Building header */
    
    	memcpy(&pHeader->data, tlv, sizeof(struct tlvs)); 
    	int pHeader_s = (sizeof(struct packet) + temp);
    	pHeader->length = pHeader_s;
    
    	u_char *payload = malloc(pHeader_s);
    	memset(&payload,0,sizeof(payload));
    	memcpy(&payload,&pHeader,pHeader_s);
    
            /* send (u_char*)payload onto network */
    Please let me know if you need more information.

    Thanks!
    Last edited by metroflex; 08-22-2009 at 08:35 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    tlv can only hold one struct, since you only malloc room for one struct. If you intend to put a bunch of things into the data field of the header, then that's fine if your malloc covers it -- but you'll have to know where the previous one ends to know where to start copying the next one.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    That makes sense. Would something like this work:

    Code:
    #define MAX_TLVS 10;
    int tlvMaxSize = (sizeof(struct tlvs) * MAX_TLVS);
    tlv = malloc(tlvMaxSize);
    
    strcpy(tlv->value,"first");
    strcpy(&tlv->value[tlvMaxSize - sizeof("first")],"second");
    Thanks.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you do
    Code:
    tlv = malloc(tlvMaxSize)
    then you have
    Code:
    tlv[0]
    tlv[1]
    tlv[2]
    .
    .
    .
    tlv[8]
    tlv[9]

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    6
    Right, but if I want it all to be dynamic since I won't know the length of the previous tlv I can't set, for example, the second tlv to tlv[9] because I don't know that the previous tlv ended at tlv[8]. It will be different every time. Make sense?

    Thanks for your responses.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by metroflex View Post
    Right, but if I want it all to be dynamic since I won't know the length of the previous tlv I can't set, for example, the second tlv to tlv[9] because I don't know that the previous tlv ended at tlv[8]. It will be different every time. Make sense?
    Heaven forbid you do something like
    Code:
    previous_write_to_the_header_was_at = 8;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. can't insert data into my B-Tree class structure
    By daluu in forum C++ Programming
    Replies: 0
    Last Post: 12-05-2002, 06:03 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread