Returning Multiple Unsigned Char data from one function to another

Viewed 57

I am trying to return unsigned characters from one function to another, but am unable to, since I am new to C and it's data structures are still a mystery to me. Here is the function that is calling the function....

void print_ethernet_data(const u_char * Buffer, int Size)
{
    unsigned char destination, src;
    unsigned short qtype;
    get_ethernet_header(Buffer , Size, &destination, &src, &qtype); //The function that is supposed to return the values
    printf("|-Dest : %u \n", destination);
    printf("|-Protocol            : %u \n",qtype);
}

This is the description of the function get_internet_header:

void get_ethernet_header(const u_char *Buffer, int Size, unsigned char* destination, unsigned char* src, unsigned short* qtype)
{
    struct ether_header *eth = (struct ether_header *)Buffer;

    printf( "\n");
    printf( "Ethernet Header\n");
    printf( "   |-Destination Address : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X \n", eth->dhost[0] , eth->dhost[1] , eth->dhost[2] , eth->dhost[3] , eth->dhost[4] , eth->dhost[5] );
    printf( "   |-Source Address      : %.2X:%.2X:%.2X:%.2X:%.2X:%.2X \n", eth->shost[0] , eth->shost[1] , eth->shost[2] , eth->shost[3] , eth->shost[4] , eth->shost[5] );
    printf( "   |-Protocol            : %u \n",(unsigned short)eth->type);
    *destination = eth->dhost;
    *src = eth->shost;
    *qtype = (unsigned short)eth->type;
}

But for some reason unknown to me, I am unable to do the same as this is what it prints for me.

Ethernet Header
   |-Destination Address : 58:49:3B:38:B5:11 
   |-Source Address      : E4:FC:82:FD:32:C1 
   |-Protocol            : 8 
|-Dest : 134 //I suppose that this is the address the pointer points to
|-Protocol            : 8 //This matches the Protocol printed above

Is it because I'm returning it the wrong way or because the formatting where I am printing the data is wrong? Note that the Protocol is printed the right way, just what I wanted. But the destination mac does not.

I read various answers on StackOverflow but couldn't make it work. I'd be greatful if somebody could help me out with the problem. Thanks in advance.

P.S: I dont think the description of struct ether_header is required, but if it is, then I can edit it later.

EDIT-1:

struct ether_header {
        unsigned char dhost[ETHER_ADDR_LEN];    // Destination host address
        unsigned char shost[ETHER_ADDR_LEN];    // Source host address
        unsigned short type;                    // IP? ARP? RARP? etc
};
1 Answers

The shost and dhost members of the structure are arrays (or possibly pointers). Arrays will decay to pointers to their first element, i.e. using plain eth->dhost is the same as &eth->dhost[0].

A pointer is usually 32 or 64 bits wide on modern computers, while a char is typically only 8 bits wide. In other words, it's really impossible to store a pointer in a char.

What you need to do is to "return" the pointer instead of a single byte. To do this define destination and src as pointers:

unsigned char *destination, *src;

And update the function arguments to pointer to pointers:

void get_ethernet_header(const u_char *Buffer, int Size,
                         unsigned char** destination,
                         unsigned char** src, unsigned short* qtype)

Finally remember to print it the correct way (like you do inside the get_ethernet_header function). The rest can stay the same.


Another and possible safer solution, is to use arrays instead, and copy the data. Then you don't have to rely on the "buffer" containing the structure being alive and valid.

Related