LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-09-2010, 03:35 PM   #1
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Rep: Reputation: 0
Structure Pointing to a Character Array


Hi,

I am currently writing a program using raw sockets. This program is used to send out ARP reply frames.

The problem that I have is that I do not seem to be able to fill out a structure that is pointing to part of a character array.

Code:
struct eth_header *eth;
struct arp_header *arp;
char buf[8];

eth = (struct eth_header *)buf;
arp = (struct arp_header *)(buf+sizeof(struct arp_header));
Whilst I can fill out the variables of eth, I cannot fill out the variables of arp. If I fill them out and then send the contents of the array using sendto, only the first structure (the ethernet header) is sent out. The rest of the data appears to be the original junk data in the array.

I have checked the address of the structure pointer and it is pointing to the right place in the array.

If I write a raw sockets program that uses an IP header struct and TCP header struct I can point these into the array and write to both of them without any issues.

The difference here is that the call to socket is different:

Code:
/* For ethernet */
fd = socket(AF_INET, SOCK_PACKET, htons(ETH_P_ARP));

/* For IP */
fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
If I just create one structure containing all of the varaibles needed this gets around the issue, however, I would like to know what I am doing wrong in the first instance.

Can anyone help?

Cheers
 
Old 08-09-2010, 03:47 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by nikon2k View Post
char buf[8];
You intend that array to hold both an eth_header and an arp_header?

What source did you use for the definitions of those two headers? What are their sizes? Why do you think they will fit in 8 bytes?

Quote:
eth = (struct eth_header *)buf;
arp = (struct arp_header *)(buf+sizeof(struct arp_header));
Why do you add sizeof(struct arp_header) to the buffer address??

You seem to intend that to advance past the eth_header that you put at the start of the buffer.
 
Old 08-09-2010, 04:03 PM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
From the code you posted, you start out with a array of pointers ("buf") and calculate another pointer to the sizeof(struct arp_header) element in that array. So, eth=buf[0] and arp=buf[sizeof(struct arp_header)].

I presume that your code initializes the values of buf[0] and buf[sizeof(struct arp_header)] to some valid memory locations? But I don't understand your question about the contents of buf[1]...buf[7]. Why would you expect that they would have anything in them except the uninitialized values? And, unless sizeof(struct arp_header) is < 8, buf[sizeof(struct arp_header)] is someplace in unallocated memory, so writing to it is somewhat risky.
 
Old 08-09-2010, 05:53 PM   #4
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,

Thanks for the responses.

The code I posted was merely for the purpose of illustrating my issue.

The actual array is 42 bytes, 14 bytes for the ethernet header and 28 bytes for the ARP header.

The intention was to point the ethernet structure to the start of the array and the ARP structure immediately after it in the array.

The only reason that I was doing it this way is because this is how I had seen some TCP raw sockets programs coded and had worked in a couple programs that I subsequently wrote (pretty new to C/programming in general)

Quote:

I presume that your code initializes the values of buf[0] and buf[sizeof(struct arp_header)] to some valid memory locations? But I don't understand your question about the contents of buf[1]...buf[7]. Why would you expect that they would have anything in them except the uninitialized values?
What i was doing was filling in the structures after they are pointed to the array. I also use memset to set the array to all zeroes first.

The reason I thought that the contents of the buffer would not be the uninitialized values is because the memory that the first structure gets in the array does get the values I fill the structure in with.

It is only the memory where the second structure is pointing to that shows the uninitialized values.
 
Old 08-09-2010, 06:02 PM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by nikon2k View Post
It is only the memory where the second structure is pointing to that shows the uninitialized values.
I explained that already.

You used the wrong offset for the second structure, leaving uninitialized memory between the end of the first structure and the beginning of the second.

Quote:
Originally Posted by nikon2k View Post
The actual array is 42 bytes
So you wrote incorrect code that you need help with, but you decided to post different incorrect code.

Last edited by johnsfine; 08-09-2010 at 06:09 PM.
 
Old 08-10-2010, 07:54 AM   #6
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,

Sorry, typo in the sample code that I posted.

It will probably make things a lot clearer if I post the relevant sections of the actual code:

Code:
struct eth_header{

char dst_hw_addr[6];
char src_hw_addr[6];
short int frame_type;

};

struct arp_header{

short int hw_type;
short int proto;
char hw_addr_len;
char pr_addr_len;
short int opcode;
char src_mac[6];
char src_ip[4];
char dst_mac[6];
char dst_ip[4];

};

char buf[42]
struct arp_header *arp;
struct ethernet_header *eth;

eth = (struct ethernet_header *)buf;
arp = (struct arp_header *)(buf+sizeof(struct ethernet header));
Am I correct in saying that in the above code, the ethernet structure is the first 14 bytes of buf and the remaining 28 bytes of buf are the arp structure ?
 
Old 08-10-2010, 08:04 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by nikon2k View Post
typo in the sample code that I posted.
And another typo this time. You're missing the _ in ethernet header in the last line of the code you just posted.

Quote:
It will probably make things a lot clearer if I post the relevant sections of the actual code:
Are you still having a problem with that code? If so, post a larger amount including the part with the problem.

Quote:
Am I correct in saying that in the above code, the ethernet structure is the first 14 bytes of buf and the remaining 28 bytes of buf are the arp structure ?
On any architecture in which a short is two bytes, all that should be correct.

On both x86 and x86_64 a short is two bytes.

Last edited by johnsfine; 08-10-2010 at 08:09 AM.
 
Old 08-10-2010, 03:03 PM   #8
nikon2k
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hi,

Damn typos.

Turns out I was being an idiot and my actual code had the following:

Code:
char *buf[42];
Instead of:

Code:
char buf[42];
Thanks for the replies and help.

Cheers
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to flush a character array Completely Clueless Programming 5 08-03-2010 05:05 AM
structure with character iftiuk Programming 4 06-03-2004 10:09 AM
c++ structure-array question deveraux83 Programming 2 01-01-2004 08:55 AM
C can 't return a character array Linh Programming 5 06-18-2003 04:58 AM
Cannot pass and return a character array Linh Programming 1 06-12-2003 04:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration