Member
Registered: Jul 2014
Posts: 65
Rep:
|
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
|