LinuxQuestions.org
Help answer threads with 0 replies.
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


Closed Thread
  Search this Thread
Old 11-10-2010, 08:08 PM   #1
arrow0815
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
Question 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:
#define MYPTNO 0x2323
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, could anybody help me figure out what's going wrong here? THankS!
 
Old 11-13-2010, 04:44 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
 
  


Closed Thread



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
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
skb buffers free while working with PF_PACKET socket igalch Linux - Kernel 2 04-06-2010 09:45 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 > Linux Forums > Linux - Networking

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