ccharhexmemcpyuint8t

How to add a uint8_t array into a char*


I have a char *data that is a Datagram to represent the packet I want to send in but I need to insert on that an uint8_t array.

// Datagram to represent the packet
char datagram[4096], source_ip[32], *data, *pseudogram;

// zero out the packet buffer
memset(datagram, 0, 4096);

// IP header
struct iphdr *iph = (struct iphdr *)datagram;

// UDP header
struct udphdr *udph = (struct udphdr *)(datagram + sizeof(struct ip));

// Data part
data = datagram + sizeof(struct iphdr) + sizeof(struct udphdr);
uint8_t packet_bytes[] = { 0xff, 0xff, 0x81, 0x01, 0x01 };
memcpy(data, packet_bytes, 5);

Doing this allows me to insert what I need and it works but the problem is that I have and uint8_t array with 0x00 in the middle making this harder than I thought because the 0x00 hex also means a termination of an array, how can I make it to work with this array instead ?

char packet_bytes[] = {
  0xff, 0xff, 0x81, 0x00, 0x00, 0x00, 0x00, 0x30,
  0x13, 0x43, 0x00, 0x01, 0x01, 0x01, 0x02, 0x00,
  0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,
  0x06, 0x00, 0x00, 0x10, 0x01, 0x01, 0x00, 0x63,
  0x02, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x05,
  0x00, 0x00, 0x00, 0x0c, 0x00, 0x09, 0x04, 0x00,
  0x0a, 0x00, 0x03, 0x01, 0x00, 0x11, 0x77, 0x25
};

Solution

  • the problem is that I have and uint8_t array with 0x00 in the middle making this harder than I thought because the 0x00 hex also means a termination of an array

    There is no such thing as "termination of an array" in C. There is null termination of character arrays used as strings, but that isn't applicable here. So the memcpy part will work just fine.

    You have some other problems however:

    The only reliable way to do this is either to disable struct padding and then memcpy in/out of the struct. Or alternatively write serialization/deserialization routines accessing one struct member at a time, moving it to/from the raw data.