Skip to content

Instantly share code, notes, and snippets.

@reflechant
Last active June 27, 2017 10:59
Show Gist options
  • Save reflechant/1dd7b4e71c5016ec5f6a2eb6757e6298 to your computer and use it in GitHub Desktop.
Save reflechant/1dd7b4e71c5016ec5f6a2eb6757e6298 to your computer and use it in GitHub Desktop.
Receiver MAC level packets from network
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <inttypes.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
// #include <netpacket/packet.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <sys/socket.h>
#define BUF_SIZE 1024*1024 //3221225472ULL // 3GB
int main(int argc, const char **argv) {
int s, bound, ifindex;
uint64_t i, j, wr_offset;
//uint8_t *buf;
void *buf;
ssize_t nbytes;
struct sockaddr_ll addr;
socklen_t len;
if (argc < 2) {
fprintf(stderr, "Укажите имя сетевого интерфейса для прослушивания (например, eth0)\n");
exit(EXIT_FAILURE);
}
ifindex = if_nametoindex(argv[1]);
if (ifindex == 0) {
fprintf(stderr, "Не найден сетевой интерфейс %s\n", argv[1]);
exit(EXIT_FAILURE);
}
// s = socket(AF_PACKET,SOCK_RAW,0xAAAA);
s = socket(AF_PACKET,SOCK_RAW,htons(ETH_P_ALL));
//s = socket(AF_PACKET,SOCK_DGRAM,0);
//s = socket(AF_PACKET,SOCK_RDM,0);
if (s == -1) {
perror("Ошибка при создании сокета");
exit(EXIT_FAILURE);
}
/*len = sizeof(addr);
getsockname(s, (struct sockaddr *) &addr, &len);
printf("%X\n", addr.sll_family);
printf("%X\n", addr.sll_protocol);
printf("%X\n", addr.sll_ifindex);
printf("%X\n", addr.sll_hatype);
printf("%X\n", addr.sll_pkttype);
printf("%X\n", addr.sll_halen);
printf("%s\n", addr.sll_addr);*/
setsockopt(s, SOL_SOCKET, PACKET_RX_RING, (void *) &req, sizeof(req));
memset(&addr, 0, sizeof(struct sockaddr_ll)); // обнуляем структуру
addr.sll_family = AF_PACKET;
// addr.sll_protocol = htons(ETH_P_ALL);
addr.sll_ifindex = ifindex;
// addr.sll_hatype = 1; // ARP hardware type, 1 for Ethernet
addr.sll_pkttype = PACKET_HOST; // to us
addr.sll_halen = ETH_ALEN;
memset(addr.sll_addr, 0, sizeof(addr.sll_addr)); // обнуляем физический адрес
addr.sll_addr[0] = 0xb0; // address of the sender
addr.sll_addr[1] = 0xb1;
addr.sll_addr[2] = 0xb2;
addr.sll_addr[3] = 0xb3;
addr.sll_addr[4] = 0xb4;
addr.sll_addr[5] = 0xb5;
len = sizeof(addr);
bound = bind(s, (const struct sockaddr *) &addr, len);
if (bound == -1) {
perror("Ошибка при привязке сокета к интерфейсу");
exit(EXIT_FAILURE);
}
//MMAP
//buf = (uint8_t *) malloc ( sizeof(uint8_t) * BUF_SIZE );
buf = (void*) malloc( BUF_SIZE );
// buf = (void*) malloc( ETH_FRAME_LEN );
fprintf(stderr, "Интерфейс %s прослушивается\n", argv[1]);
wr_offset = 0;
while(wr_offset < (BUF_SIZE - ETH_FRAME_LEN) ) {
nbytes = recv(s, buf+wr_offset, ETH_FRAME_LEN, 0);
if (nbytes == -1) {
perror("Ошибка при получении данных из сокета");
exit(EXIT_FAILURE);
}
fprintf(stderr, "\rПринято %" PRIu64 " байт", wr_offset);
wr_offset += nbytes;
}
printf("\n");
//fprintf(stderr, "Принято %X байт", wr_offset);
for (i=0; i<wr_offset; i+=32) {
for (j=i; j<i+32; j++)
printf("%02X",((unsigned char *)buf)[j] );
printf("\n");
}
printf("\n");
exit(EXIT_SUCCESS);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment