LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-22-2009, 02:07 PM   #1
ThirtySixBelow
Member
 
Registered: Sep 2004
Posts: 48

Rep: Reputation: 15
Communication using RAW sockets


I'm trying to create a simple program that sends and receives ethernet frames. I'm using ubuntu 9.04 and tried to follow the raw manpage on the ubuntu website but it doesn't seem to be working properly. There are so many resources that give different ideas and I feel like I'm just messing around trying to find the correct combination of code. All I want to do is send [dst_mac_addr|src_mac_addr|type|data] ... it can't be that difficult. The code below is what I'm working with and it currently fails on send. You can notice some different ideas I've tried from the commented out code. Any help would be amazing.

Code:
#include <sys/socket.h>
#include <netinet/in.h>
//#include <netdb.h>
//#include <arpa/inet.h>
//#include <sys/param.h>
#include <netpacket/packet.h>
#include <net/ethernet.h>
//#include <linux/if_packet.h>
//#include <linux/if_ether.h>
//#include <linux/if_arp.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

int main (int argc, char*argv[]) {
	int i,j;
	struct timeval start,stop;
	unsigned char* buffer = (unsigned char*)malloc(ETH_FRAME_LEN);
	unsigned char* etherhead = buffer;
	unsigned char* data = buffer + 14;
	struct ethhdr *eh = (struct ethhdr *)etherhead;
	struct sockaddr_in servaddr;
	
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(80);
	servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	
	int s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
	if(s == -1) {
		printf("socket failed\n");
		exit(0);
	}	
	if(setsockopt(s,SOL_SOCKET,SO_BINDTODEVICE,"eth0",4) == -1) {
		printf("sockopt failed\n");
		exit(0);
	}

	int send_result = 0;
	int length = 0;

	unsigned char src_mac[6] = {0x00,0x21,0x97,0x45,0x6b,0x3b};
	unsigned char dest_mac[6] = {0x00,0x0a,0x35,0x00,0x01,0x02};
//
//	struct sockaddr_ll socket_address;
//	socket_address.sll_family = AF_PACKET;
//	socket_address.sll_protocol = 0;
//	socket_address.sll_ifindex = if_nametoindex("eth0");
//	socket_address.sll_hatype = 0;
//	socket_address.sll_pkttype = 0;
//	socket_address.sll_halen = ETH_ALEN;
//
//	/*MAC - begin*/
//	socket_address.sll_addr[0]  = 0x00;		
//	socket_address.sll_addr[1]  = 0x21;		
//	socket_address.sll_addr[2]  = 0x97;
//	socket_address.sll_addr[3]  = 0x45;
//	socket_address.sll_addr[4]  = 0x6b;
//	socket_address.sll_addr[5]  = 0x3b;
//	/*MAC - end*/
//	socket_address.sll_addr[6]  = 0x00;/*not used*/
//	socket_address.sll_addr[7]  = 0x00;/*not used*/
//

	/*set the frame header*/
	memcpy((void*)buffer, (void*)dest_mac, ETH_ALEN);
	memcpy((void*)(buffer+ETH_ALEN), (void*)src_mac, ETH_ALEN);
	eh->h_proto = 0x00;
	/*fill the frame with some data*/
	for (j = 0; j < 1500; j++) {
		data[j] = (unsigned char)((int) (255.0*rand()/(RAND_MAX+1.0)));
	}

	//if(bind(s,(struct sockaddr *)&socket_address, sizeof(socket_address))<0){
	//	printf("binding error\n");
	//	exit(0);
	//}

	//start = clock();
	gettimeofday(&start,0);
	for(i=0;i<100;i++){
		/*send the packet*/
		send_result = sendto(s, buffer, ETH_FRAME_LEN, 0, 
	      		(struct sockaddr*)&servaddr, sizeof(servaddr));
		if (send_result == -1) { printf("send error\n");return 1;}

		length = recvfrom(s, buffer, ETH_FRAME_LEN, 0, NULL, NULL);
		if (length == -1) { printf("rcv error\n");return 1; }
	}
	//stop = clock();
	gettimeofday(&stop,0);
	printf("%ld\n",stop.tv_usec-start.tv_usec);
	return 0;
}
 
Old 12-23-2009, 02:52 PM   #2
kanver
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Rep: Reputation: 1
How does it fail? Does the program crash? If so, what's the error message? "xy doesn't work, what to do?" never induced any useful answer.
 
Old 12-23-2009, 04:00 PM   #3
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
When running under strace I found this message:

Code:
sendto(3, "\0\n5\0\1\2\0!\227Ek;\0\0\326d\307\313\3502U\303F\215y\240]\202\362\351\242\266"..., 1514, 0, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EMSGSIZE (Message too long)
...maybe it's relevant

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
Raw Sockets and IPv6 SaraiKhan Programming 1 11-27-2008 07:07 AM
raw sockets in c wee-face Programming 1 02-01-2007 09:35 AM
Raw Sockets Srikanth0210 Programming 2 12-05-2005 03:22 AM
raw sockets and C wrongman Programming 3 05-04-2004 02:17 PM
raw sockets BashTin Programming 1 06-07-2003 06:34 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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