Reputation: 1021
I have to copy Mac address stored as an array with a pointer reference to another. I want to perform a swap but, i dont know if i am copying right.
uint8_t * custom_mac_store;
memcpy(custom_mac_store, ehdr->ether_shost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, custom_mac_store, sizeof(ehdr->ether_shost));
ehdr->ether_shost refers to a Mac address which is 48 bits long and stored in 6 arrays as 8 bits.
struct sr_ethernet_hdr
{
#ifndef ETHER_ADDR_LEN
#define ETHER_ADDR_LEN 6
#endif
uint8_t ether_dhost[ETHER_ADDR_LEN]; /* destination ethernet address */
uint8_t ether_shost[ETHER_ADDR_LEN]; /* source ethernet address */
uint16_t ether_type; /* packet type ID */
} __attribute__ ((packed)) ;
Thanks in advance!!!
Upvotes: 1
Views: 2066
Reputation: 3049
custom_mac_store is a pointer, not an array, you should allocate the memory doing the following:
uint8_t * custom_mac_store = malloc(sizeof(ehdr->ether_shost) * sizeof(uint8_t));
Then do the memcpy
This way you can erase or overwrite ehdr->ether_shost but you already copied your array to the new array.
@alk You are right you need to delete the memory
uint8_t * custom_mac_store = malloc(sizeof(ehdr->ether_shost) * sizeof(uint8_t));
memcpy(custom_mac_store, ehdr->ether_shost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, custom_mac_store, sizeof(ehdr->ether_shost));
free(custom_mac_store); //< Destroys everything in the array
custom_mac_store = NULL; //< NULL must be defined in a macro preferanly DEFINE NULL 0
or you could use c++
uint8_t * custom_mac_store = new uint8_t[sizeof(ehdr->ether_shost)];
std::copy ( ehdr->ether_shost, ehdr->ether_shost+sizeof(ehdr->ether_shost), custom_mac_store );
std::copy ( ehdr->ether_dhost, ehdr->ether_dhost+sizeof(ehdr->ether_shost), ehdr->ether_shost );
std::copy ( custom_mac_store, custom_mac_store+sizeof(ehdr->ether_shost), ehdr->ether_dhost );
delete[] custom_mac_store; //< Destroys everything in the array
or you could use the stack instead of the heap (this should be faster, just don't break the stack)
const std::size_t size_custom_mac_store = 48000;
uint8_t custom_mac_store[size_custom_mac_store]; //< The size must be either a const or a macro
std::copy ( ehdr->ether_shost, ehdr->ether_shost+sizeof(ehdr->ether_shost), custom_mac_store );
std::copy ( ehdr->ether_dhost, ehdr->ether_dhost+sizeof(ehdr->ether_shost), ehdr->ether_shost );
std::copy ( custom_mac_store, custom_mac_store+sizeof(ehdr->ether_shost), ehdr->ether_dhost );
delete[] custom_mac_store; //< Destroys everything in the array
Good luck.
PS: Love memory management, it's the only way
Upvotes: 2
Reputation: 170
It might work if some memory was allocated for your temporary buffer custom_mac_store
.
Like that it's just an uninitialized pointer waiting to cause a crash.
You can either allocate explicitly some memory to it, or use directly a 6 byte array instead.
Upvotes: 2
Reputation: 12688
You can define ethernet length as 6
then making use of a temporary array, just perform memcpy
as below,
#define ETH_ALEN 6
uint8_t tmp[ETH_ALEN];
memcpy(tmp, eth->ether_shost, ETH_ALEN);
memcpy(eth->ether_shost, eth->ether_dhost, ETH_ALEN);
memcpy(eth->ether_dhost, tmp, ETH_ALEN);
Upvotes: 2