LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 11-11-2010, 04:03 PM   #1
aztroboy
LQ Newbie
 
Registered: Jun 2010
Posts: 19

Rep: Reputation: 0
Unhappy Raw Socket Programming question (my head is going to explode)


Greetings,

This is my main question:

Quote:
In raw socket sniffing: how do I copy data from a structure into a char[] or pointer?
My problem:

I started doing some raw socket programming in OpenWRT (I'm a newbie) for research. It's been a long time since the last time I programmed in C, so here's my newbie question/problem.

I'm receiving packets and using an .h file with the structures to get the information of each 802.11 packet in monitor mode (attached to this post).

My (simple) goal is to get the Tx MAC Address. No luck.

This is my "read the prism header" function:

Code:
ParsePrismHeader(unsigned char *packet,int len)
{
	wlan_header *wlan;ieee_802_11_header *i802;u_int16_t eth_type;
	unsigned char *mac;unsigned char *mac2;

	wlan = (wlan_header *)packet;
	i802 = (ieee_802_11_header *)(packet + wlan->msglen);

	//copy the DESTINATION mac address to a pointer
	getMACaddr((i802)->mac1,mac);
	
	//is this packet for me? -- Is the DES MAC my mac?
	if(strcmp(localMAC,mac)==0)
	{
		//let's print my own mac address (DEST mac)
                printf(" %s ",mac);
		//now copy the Transmitting mac
        	getMACaddr((i802)->mac2,mac2);
	}
}
this is the getMACaddr function:
Code:
getMACaddr(unsigned char *p,unsigned char *macx){
	int s=0;
	char tmp[50]="";

	for( s = 0; s < 6; s++ )
	{
		sprintf(tmp,"%.2X", *p);
		strcat(macx,tmp);
		p++;
	}
		size_t begin=1;
		size_t end=12;

		sprintf(macx,"%s",substring(macx,begin,end));

}
mac, which is the Destination MAC (my mac), does the job and prints it without problem

BUT when I use getMACaddress the second time, it prompts the 'C' character, like this:

Code:
 687F7427B81C  4FD370694
 687F7427B81C 4FD3706940
 687F7427B81C  4FD37069404
 687F7427B81C  4FD370694040
 687F7427B81C  FD3706940404
 687F7427B81C  D37069404040
 687F7427B81C  370694040404
 687F7427B81C  706940404040
 687F7427B81C  069404040404
 687F7427B81C  694040404040
 687F7427B81C  940404040400
the first one is my MAC (it's ok), the second it's NOT :/

now again, HOW do I copy data from a structure TO a char[] or a pointer? why is it copying one and not the other one?


thank you for your indulgence.
Attached Files
File Type: txt rssi.txt (1.3 KB, 1 views)
 
Old 11-11-2010, 04:20 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
Quote:
Originally Posted by aztroboy View Post
... HOW do I copy data from a structure TO a char[] or a pointer?
...

man 3 strcpy
.
 
Old 11-11-2010, 07:38 PM   #3
aztroboy
LQ Newbie
 
Registered: Jun 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Sergei Steshenko View Post
man 3 strcpy
.
I actually did this

Code:
...
	unsigned char *mac2;
...
	getMACaddr((i802)->mac1,mac); // I made this function. It won't work with "mac2" after I use it with "mac"
...
        strcpy(mac2, (unsigned char *)((i802)->mac2) );
        printf(" %s ",mac);
        printf(" %s \n",mac2);
this is what I got:

Code:
 687F7427B81C  ,=VX\
 687F7427B81C  ,=VX\
 687F7427B81C  ,=VX\0
 687F7427B81C  ,=VX\P
 687F7427B81C  ,=VX\`
 687F7427B81C  ,=VX\
 687F7427B81C  ,=VX\
 687F7427B81C  ,=VX\
 687F7427B81C  ,=VX\
 687F7427B81C  ,=VX\0ª
Am I printing it wrong? The second one is a mess. The first on is copied without trouble.
 
Old 11-11-2010, 08:57 PM   #4
aztroboy
LQ Newbie
 
Registered: Jun 2010
Posts: 19

Original Poster
Rep: Reputation: 0
I actually got it, thanks, it worked

What was I doing wrong? printing the string that I strcpy'ed.
 
  


Reply

Tags
mac, packet, raw, socket


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Raw socket programming with C arabindav Programming 17 06-09-2011 11:58 AM
send a file through raw socket using C programming enginius Programming 20 11-29-2010 07:33 AM
raw socket programming bigboss360 Linux - Networking 2 10-17-2009 09:11 AM
Raw socket programming homePBX Linux - Networking 6 05-02-2008 12:25 PM
Help with raw socket programming tuxfood Programming 2 07-25-2005 01:17 PM


All times are GMT -5. The time now is 03:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration