LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 11-11-2010, 07:58 AM   #1
arrow0815
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
Question about create a socket(PF_PACKET, SOCK_DGRAM, htons(xx))


Hi everyone, I'm trying to create a PF_PACKET socket and send a frame to another host, I know the destination's mac address
and interface index, and I want to use my own protocol no., so I defined a PROTONO as 0x2323, here's the code of sender
Code:
int main(int argc, char **argv)
{
	int					sockfd;
	//struct sockaddr_in	servaddr;
	struct sockaddr_ll servaddr, bindaddr;

	bzero(&servaddr, sizeof(servaddr));

        //destination's ifindex and addr is known
	servaddr.sll_family = AF_PACKET;
	servaddr.sll_ifindex = 4;
	servaddr.sll_halen = 6;
	servaddr.sll_addr[0] = 0x00;
	servaddr.sll_addr[1] = 0x0C;
	servaddr.sll_addr[2] = 0x29;
	servaddr.sll_addr[3] = 0xDF;
	servaddr.sll_addr[4] = 0x27;
	servaddr.sll_addr[5] = 0xAC;
	servaddr.sll_addr[6] = 0x00;
	servaddr.sll_addr[7] = 0x00;

	sockfd = Socket(PF_PACKET, SOCK_DGRAM, htons(MYPTNO));

	char buffer[512];

	sprintf(buffer, "---toBeSent---\n");
	sendto(sockfd, buffer, strlen(buffer), 0, (SA*)&servaddr, sizeof(servaddr));

	exit(0);
}
and at the receiver side,I want to get frames to each interface, so I use 0 as the sll_ifindex number.
Code:
#define MYPTNO 0x2323
int main(int argc, char **argv)
{
	int					sockfd;
	struct sockaddr_ll	servaddr, cliaddr;

	sockfd = Socket(PF_PACKET, SOCK_DGRAM, htons(MYPTNO));

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sll_family = AF_PACKET;
	servaddr.sll_protocol = htons(MYPTNO);
	servaddr.sll_ifindex = 0;//bind to every interface

	Bind(sockfd, (SA *) &servaddr, sizeof(servaddr));

	char buff[512];
	int n = 0;
	int length;
	n = recvfrom(sockfd, buff, 512, 0, (SA*)&cliaddr, &length);

	buff[n] = 0;

	printf("%s\n", buff);

	printf("%s\n", Sock_ntop((SA*)&cliaddr, length));
	return 0;
}
But these doesn't work, need I bind the receiver's socket? could anybody help me figure out what's going wrong here? THankS!
 
  


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
Question about create a socket(PF_PACKET, SOCK_DGRAM, htons(xx)) arrow0815 Linux - Networking 1 11-13-2010 04:44 PM
Bind a PF_PACKET socket to a specific port number gr1980 Programming 1 10-27-2010 10:57 PM
PF_PACKET socket receive problem faeze kalantari Programming 2 09-06-2010 01:36 AM
Please Help me : Sendto: Invalid argument PF_PACKET SOCK_DGRAM htons(ETH_P_IP) tuxtuxtux Linux - Networking 3 03-24-2010 06:08 AM
PF_PACKET socket bind() to loopback interface (lo) cwpetersen Linux - Networking 0 03-17-2009 12:02 PM

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

All times are GMT -5. The time now is 10:07 PM.

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