LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   Raw socket programming (https://www.linuxquestions.org/questions/linux-networking-3/raw-socket-programming-634353/)

homePBX 04-10-2008 05:21 AM

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 !!

Agrouf 04-10-2008 09:51 AM

firewall problem?

theNbomr 04-10-2008 01:16 PM

Also, raw socket mode requires root privileges.
--- rod.

homePBX 04-14-2008 12:50 AM

Quote:

Originally Posted by theNbomr (Post 3117083)
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?

theNbomr 04-14-2008 08:44 PM

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.

homePBX 05-02-2008 07:51 AM

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.

theNbomr 05-02-2008 12:25 PM

Quote:

Originally Posted by homePBX (Post 3139813)
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.


All times are GMT -5. The time now is 01:15 AM.