LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2008, 05:21 AM   #1
homePBX
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
Unhappy Raw socket programming


we have been trying to send raw ethernet packet from the computer to a circuit board on which linux 2.4 kernel is loaded ..

the code on the computer is :

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
#include <netinet/ether.h>

#define ETHERTYPE_LEN 2
#define MAC_ADDR_LEN 6
#define BUFFER_LEN 320

typedef unsigned char MacAddress[MAC_ADDR_LEN];
extern int errno;

int main()
{
int sockFd = 0, retValue = 0;
char buffer[BUFFER_LEN], dummyBuf[50];
struct sockaddr_ll destAddr;
short int etherTypeT = htons(0x8200);
MacAddress localMac = {0x00, 0xB0, 0xD0, 0x4C, 0xD5, 0xC6};
//MacAddress localMac = {0x00, 0x17, 0xB3, 0x00, 0x00, 0x00};
MacAddress destMac = {0x00, 0x17, 0xB3, 0x00, 0x00, 0x00};

//MacAddress destMac= {0x00, 0xB0, 0xD0, 0x4C, 0xD5, 0xC6};
//MacAddress destMac = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
memset(&destAddr, 0, sizeof(struct sockaddr_ll));
memset(buffer, 0, BUFFER_LEN);

if((sockFd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) {
printf("ERROR! socket() call failed (Error No: %d \"%s\").\n", errno, strerror(errno));
exit(1);
}
printf("Socket creation success.\n");

destAddr.sll_family =PF_PACKET;
destAddr.sll_protocol = htons(ETH_P_ALL);
destAddr.sll_halen = 6;
destAddr.sll_ifindex = 2;
memcpy(&(destAddr.sll_addr), destMac, MAC_ADDR_LEN);

/* Ethernet Header Construction */
memcpy(buffer, destMac, MAC_ADDR_LEN);
memcpy((buffer+MAC_ADDR_LEN), localMac, MAC_ADDR_LEN);
memcpy((buffer+(2*MAC_ADDR_LEN)), &(etherTypeT), sizeof(etherTypeT));

/* Add some data */
memset(dummyBuf, 0xa0, sizeof(dummyBuf));
memcpy((buffer+ETHERTYPE_LEN+(2*MAC_ADDR_LEN)), dummyBuf, 200);
int i;
for(i=0;i<10;i++)
{
printf("\n frame %d\n",i);
if((retValue = sendto(sockFd, buffer, 214, 0, (struct sockaddr *)&(destAddr), sizeof(struct sockaddr_ll))) < 0)
{

printf("ERROR! sendto() call failed (Error No: %d \"%s\").\n", errno, strerror(errno));
exit(1);
}
printf("Send success (%d).\n", retValue);
sleep(2);
}

return(0);
}


the code on our board :

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <string.h>

int main(void)
{

int i = 0;
int s; /*socketdescriptor*/
int frameCount = 1;
int MAX_FRAMES = 1000;

s = socket(AF_PACKET, SOCK_RAW, ntohs(ETH_P_ALL));
if (s == -1) { printf("ERROR BINDING SOCKET...\n"); exit(0); }

unsigned char* buffer = (unsigned char*)malloc(ETH_FRAME_LEN);
memset(buffer, '\0', ETH_FRAME_LEN);
/*Buffer for ethernet frame*/
int length = 0; /*length of the received frame*/

while(frameCount <= MAX_FRAMES){

length = recvfrom(s, buffer, ETH_FRAME_LEN, 0, (struct sockaddr *) 0, (int *) 0);
if (length == -1)
{
printf("Error while receiving ethernet frame...\n");
return 0;
}
else {

printf("Frame %d (hex)\n\n", frameCount);
for(i=0; i<length; i++)
{
printf("%.2x ", buffer[i]);
}
printf("\n\n");

printf("Frame %d (char)\n\n", frameCount);
for(i=0; i<length; i++)
{
printf("%c ", buffer[i]);
}
printf("\n\n");

frameCount++;
memset(buffer, '\0', ETH_FRAME_LEN);

}

}

close(s);

}

the packets are going from computer to computer and also from the board to the computer ... but vice versa is not happening.. the received length on the board is coming 0 ..
we are not able to figure out why our codes are giving us problem ... someone plz help us !!
 
Old 04-10-2008, 09:51 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,596

Rep: Reputation: 80
firewall problem?
 
Old 04-10-2008, 01:16 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Also, raw socket mode requires root privileges.
--- rod.
 
Old 04-14-2008, 12:50 AM   #4
homePBX
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by theNbomr View Post
Also, raw socket mode requires root privileges.
--- rod.


Hey,
Thanks but that doesn't seem to be the problem because we have the root privileges.
Also we are getting the ping packets and we are able to send these raw packets.
Any other suggestions?
 
Old 04-14-2008, 08:44 PM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Some code I've used has a note that the SOCK_RAW parameter was not working for me (and I never studied it to find out why), but the working code uses 'AF_INET, SOCK_PACKET' as the first two args to the socket() function. It may or may not be relevant, but this code uses a main loop that calls select() to detect incoming traffic (various sockets), and then uses read() to fetch the data.
Nothing I see in your code jumps out at me (but it would have made it a lot easier to read if it had been posted in [CODE] tags).

Is there any other network code in your board that is fully functional?

--- rod.

Last edited by theNbomr; 04-14-2008 at 08:46 PM.
 
Old 05-02-2008, 07:51 AM   #6
homePBX
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
HEY!
There is no other network code on the board . I think the problem is at a much lower level than the application code. Because the code or even tcpdump does not even show the incoming ping packets. It just prints all the outgoing ping packets. Is there any problem with linux 2.4 that we are using. And can you help as to how should we go about it.
 
Old 05-02-2008, 12:25 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by homePBX View Post
HEY!
There is no other network code on the board .
You did say that you could send from the board to a computer, and that ping works at some level. Ergo, you've got networking. What is the output of /sbin/ifconfig? What is the output of netstat -i?
--- rod.
 
  


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
Raw socket programming with C arabindav Programming 17 06-09-2011 11:58 AM
raw socket/ip packet help shouup Programming 14 04-24-2006 04:54 PM
Help with raw socket programming tuxfood Programming 2 07-25-2005 01:17 PM
Using Raw Socket for UDP myself_rajat Linux - Networking 0 05-17-2004 03:35 AM
Socket Raw linuxanswer Programming 1 04-01-2004 09:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:51 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