LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-22-2014, 04:20 AM   #1
zerop
Member
 
Registered: Jul 2014
Posts: 59

Rep: Reputation: Disabled
Question set socket option error when set PACKET_TX_RING


return socket invalid option

and

is PACKET_RX_RING the same as PACKET_TX_RING
just by changing PACKET_TX_RING to PACKET_RX_RING?


#include <sys/socket.h>
#include <sys/mman.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
//#include <linux/module.h> // included for all kernel modules
//#include <linux/kernel.h> // included for KERN_INFO
//#include <linux/init.h>
//#include <chrono>
#include <ctime>
//#include <conio.h>
#include <curses.h>
#include <time.h>
//#define ETH_FRAME_LEN 1518
// 14 + 46-1500 + 4
#define ETH_FRAME_LEN 64
//tcpdump -i eth0 host 127.0.0.1
//MODULE_LICENSE("GPL");
//MODULE_AUTHOR("Lakshmanan");
//MODULE_DESCRIPTION("A Simple Hello World module");

int main()
{

int s; /*socketdescriptor*/

s = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (s == -1) { printf("socket error\n\n"); }


//96/96 = 1, 1*1 = 1
tpacket_req req;
req.tp_block_size=96;
req.tp_frame_size=96;
req.tp_block_nr=1;
req.tp_frame_nr=(req.tp_block_size / req.tp_frame_size) * req.tp_block_nr;
if ( (setsockopt(s,
SOL_PACKET,
PACKET_TX_RING,
(char *)&req,
sizeof(req))) != 0 ) {
perror("setsockopt()");
close(s);
return 1;
};
char* map=(char*)mmap(NULL, req.tp_block_size * req.tp_block_nr, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, s, 0);

/*target address*/
struct sockaddr_ll socket_address;

/*buffer for ethernet frame*/
unsigned char* buffer = (unsigned char*)malloc(ETH_FRAME_LEN);

/*pointer to ethenet header*/
unsigned char* etherhead = (unsigned char*)buffer;

/*userdata in ethernet frame*/
unsigned char* data = (unsigned char*)(buffer);

/*another pointer to ethernet header*/
struct ethhdr *eh = (struct ethhdr *)etherhead;

int send_result = 0;

/*our MAC address*/
unsigned char src_mac[6] = {0x10, 0x78, 0xd2, 0xad, 0x90, 0xcb};

/*other host MAC address 10:78:d2:ad:90:cb*/
unsigned char dest_mac[6] = {0x10, 0x78, 0xd2, 0xad, 0x90, 0xcb};

/*prepare sockaddr_ll*/

/*RAW communication*/
socket_address.sll_family = PF_PACKET;
/*we don't use a protocoll above ethernet layer
->just use anything here*/
socket_address.sll_protocol = htons(ETH_P_IP);

/*index of the network device
see full code later how to retrieve it*/
socket_address.sll_ifindex = 2;

/*ARP hardware identifier is ethernet*/
socket_address.sll_hatype = ARPHRD_ETHER;

/*target is another host*/
socket_address.sll_pkttype = PACKET_OTHERHOST;

/*address length*/
socket_address.sll_halen = ETH_ALEN;
/*MAC - begin*/
socket_address.sll_addr[0] = 0x10;
socket_address.sll_addr[1] = 0x78;
socket_address.sll_addr[2] = 0xd2;
socket_address.sll_addr[3] = 0xad;
socket_address.sll_addr[4] = 0x90;
socket_address.sll_addr[5] = 0xcb;
/*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*/
int i = 0;
int j = 0;
//for (j = 46; --j; data[j] = (unsigned char)((int) (255.0*rand()/(RAND_MAX+1.0))));
/*
for (j = 1499; j >=0 ; j--) {
i = 1499-j;
data[i] = (unsigned char)((int) (255.0*rand()/(RAND_MAX+1.0)));
}*/

/*send the packet*/
//send_result = sendto(s, buffer, ETH_FRAME_LEN, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
//if (send_result == -1) {
//printf("send error");
//}
//const clock_t begin_time = clock();

//while((100*( clock() - begin_time )/CLOCKS_PER_SEC) < 15)
while(true)
{
char buff[46] = "\0";
printf("input: ");
scanf("%s", buff);


memcpy(&buffer[14], &buff, 45);
char buff2[46] = "\0";
memcpy(&buff2, &buff, 46);
printf(buff2);
/*send the packet*/
//send_result = sendto(s, buffer, ETH_FRAME_LEN, 0, (struct sockaddr*)&socket_address, sizeof(socket_address));
send_result = sendto(s, buffer, ETH_FRAME_LEN, MSG_DONTWAIT, (struct sockaddr*)&socket_address, sizeof(socket_address));
//ret = sendto(s, buffer, ETH_FRAME_LEN, MSG_DONTWAIT, (struct sockaddr *)(void *) to, sizeof(struct sockaddr_in));
send_result == -1?printf("send error"):0;

if(buff[0] == 'q')
{
close(s);
exit(0);
}
}
printf("goodbye\n");
return 0; // Non-zero return means that the module couldn't be loaded.
}

//46+14 = 60
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Wireshark UDP Packets Socket not set robtard Programming 3 10-27-2010 06:45 PM
[SOLVED] Atheros error "set encode" (AR9285). Can't set an encryption key ansichart Linux - Networking 4 06-04-2010 03:26 PM
Fedora 10 iwconfig error: Error for wireless request "Set Encode" (8B2A) :SET failed mishkind Linux - Laptop and Netbook 2 05-10-2009 03:43 PM
[c] Raw socket, set source ip to 0.0.0.0.0 darell Programming 5 03-19-2009 08:35 PM
How to set SQL to UNIX socket Skunk_Face Linux - General 1 01-19-2004 07:20 AM


All times are GMT -5. The time now is 06:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration