On structure assignment (pack your ETH header structures!)
Program like this :
#include <stdio.h>
#include <stddef.h>
int main()
{
int i, j;
char a[] = { 0x0, 0x1, 0x2, 0x4, 0x5, 0x6, 0x7, 0x8};
typedef struct
{
char c;
short s;
int k;
} s_t;
s_t str;
printf ("offsetof(str, c) = %d\n", offsetof(s_t, c));
printf ("offsetof(str, s) = %d\n", offsetof(s_t, s));
printf ("offsetof(str, k) = %d\n", offsetof(s_t, k));
str = *( (s_t *)a );
printf("str.c = %#x\n", str.c);
printf("str.s = %#x\n", str.s);
printf("str.k = %#x\n", str.k);
return 0;
}
Will give output like this :
drasko@bg:~/stuff$ ./struct_ass
offsetof(str, c) = 0
offsetof(str, s) = 2
offsetof(str, k) = 4
str.c = 0
str.s = 0x402
str.k = 0x8070605
drasko@bg:~/stuff$
If, however, structure was packed, like this :
typedef struct
{
char c;
short s;
int k;
} __attribute__ ((__packed__)) s_t;
Result would be different :
drasko@bg:~/stuff$ ./struct_ass
offsetof(str, c) = 0
offsetof(str, s) = 1
offsetof(str, k) = 3
str.c = 0
str.s = 0x201
str.k = 0x7060504
drasko@bg:~/stuff$
So - advice for casting your ETH packet stream into ETH header-like pkt_hdr struct for easier manipulation of memcers in ETH frame (like src and dst MAC addr and ethtype) :
pack your header struct to kill the padding!
#include <stdio.h>
#include <stddef.h>
int main()
{
int i, j;
char a[] = { 0x0, 0x1, 0x2, 0x4, 0x5, 0x6, 0x7, 0x8};
typedef struct
{
char c;
short s;
int k;
} s_t;
s_t str;
printf ("offsetof(str, c) = %d\n", offsetof(s_t, c));
printf ("offsetof(str, s) = %d\n", offsetof(s_t, s));
printf ("offsetof(str, k) = %d\n", offsetof(s_t, k));
str = *( (s_t *)a );
printf("str.c = %#x\n", str.c);
printf("str.s = %#x\n", str.s);
printf("str.k = %#x\n", str.k);
return 0;
}
Will give output like this :
drasko@bg:~/stuff$ ./struct_ass
offsetof(str, c) = 0
offsetof(str, s) = 2
offsetof(str, k) = 4
str.c = 0
str.s = 0x402
str.k = 0x8070605
drasko@bg:~/stuff$
If, however, structure was packed, like this :
typedef struct
{
char c;
short s;
int k;
} __attribute__ ((__packed__)) s_t;
Result would be different :
drasko@bg:~/stuff$ ./struct_ass
offsetof(str, c) = 0
offsetof(str, s) = 1
offsetof(str, k) = 3
str.c = 0
str.s = 0x201
str.k = 0x7060504
drasko@bg:~/stuff$
So - advice for casting your ETH packet stream into ETH header-like pkt_hdr struct for easier manipulation of memcers in ETH frame (like src and dst MAC addr and ethtype) :
pack your header struct to kill the padding!
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home