Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2019 Michal Kalewski <mkalewski at cs.put.poznan.pl>
- *
- * Compilation: gcc -Wall ./ethrecv.c -o ./ethrecv
- * Usage: ./ethrecv INTERFACE
- * NOTE: This program requires root privileges.
- *
- * Bug reports: https://gitlab.cs.put.poznan.pl/mkalewski/ps-2019/issues
- *
- */
- #include <arpa/inet.h>
- #include <linux/route.h>
- #include <linux/if.h>
- #include <linux/if_arp.h>
- #include <linux/if_ether.h>
- #include <linux/if_packet.h>
- #include <netinet/in.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #define IRI_T_ADDRESS 0
- #define IRI_T_ROUTE 1
- #define ETH_P_CUSTOM 0x8888
- struct ifrtinfo {
- int iri_type;
- char iri_iname[16];
- struct sockaddr_in iri_iaddr;
- struct sockaddr_in iri_rtdst;
- struct sockaddr_in iri_rtmsk;
- struct sockaddr_in iri_rtgip;
- };
- int main(int argc, char** argv) {
- int sfd, i;
- ssize_t len;
- char* frame;
- char* fdata;
- struct ethhdr* fhead;
- struct ifreq ifr;
- struct sockaddr_ll sall;
- sfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_CUSTOM));
- strncpy(ifr.ifr_name, argv[1], IFNAMSIZ);
- ioctl(sfd, SIOCGIFINDEX, &ifr);
- memset(&sall, 0, sizeof(struct sockaddr_ll));
- sall.sll_family = AF_PACKET;
- sall.sll_protocol = htons(ETH_P_CUSTOM);
- sall.sll_ifindex = ifr.ifr_ifindex;
- sall.sll_hatype = ARPHRD_ETHER;
- sall.sll_pkttype = PACKET_HOST;
- sall.sll_halen = ETH_ALEN;
- bind(sfd, (struct sockaddr*) &sall, sizeof(struct sockaddr_ll));
- while(1) {
- frame = malloc(ETH_FRAME_LEN);
- memset(frame, 0, ETH_FRAME_LEN);
- fhead = (struct ethhdr*) frame;
- fdata = frame + ETH_HLEN;
- len = recvfrom(sfd, frame, ETH_FRAME_LEN, 0, NULL, NULL);
- struct ifrtinfo *r;
- r = (struct ifrtinfo *) frame + ETH_HLEN;
- if (r->iri_type = IRI_T_ADDRESS){
- printf("IRI_T_ADDRESS\n");
- // ifsetup
- int sfd1;
- struct ifreq ifr1;
- struct sockaddr_in* sin;
- sfd1 = socket(PF_INET, SOCK_DGRAM, 0);
- strncpy(ifr1.ifr_name, r->iri_iname, strlen(r->iri_iname) + 1);
- sin = (struct sockaddr_in*) &(r -> iri_iaddr);
- // sin = (struct sockaddr_in*) &ifr1.ifr_addr;
- memset(sin, 0, sizeof(struct sockaddr_in));
- sin->sin_family = AF_INET;
- sin->sin_port = 0;
- sin->sin_addr.s_addr = inet_addr(r->iri_iname);
- ioctl(sfd1, SIOCSIFADDR, &ifr1);
- ioctl(sfd1, SIOCGIFFLAGS, &ifr1);
- ifr1.ifr_flags |= IFF_UP | IFF_RUNNING;
- ioctl(sfd1, SIOCSIFFLAGS, &ifr1);
- close(sfd1);
- }else if (r->iri_type = IRI_T_ROUTE){
- // setgw
- printf("IRI_T_ADDRESS\n");
- int sfd1;
- struct rtentry route;
- struct sockaddr_in* addr;
- sfd1 = socket(PF_INET, SOCK_DGRAM, 0);
- memset(&route, 0, sizeof(route));
- addr = (struct sockaddr_in*) &route.rt_gateway;
- addr->sin_family = AF_INET;
- // addr->sin_addr.s_addr = inet_addr(argv[1]);
- addr->sin_addr.s_addr = &(r->iri_rtgip);
- addr = (struct sockaddr_in*) &route.rt_dst;
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = &(r->iri_rtdst);// dac tu .sin_addr.sin_addr
- addr = (struct sockaddr_in*) &route.rt_genmask;
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = &(r->iri_rtmsk);
- route.rt_flags = RTF_UP | RTF_GATEWAY;
- route.rt_metric = 500;
- ioctl(sfd1, SIOCADDRT, &route);
- close(sfd1);
- }
- printf("[%dB] %02x:%02x:%02x:%02x:%02x:%02x -> ", (int)len,
- fhead->h_source[0], fhead->h_source[1], fhead->h_source[2],
- fhead->h_source[3], fhead->h_source[4], fhead->h_source[5]);
- printf("%02x:%02x:%02x:%02x:%02x:%02x | ",
- fhead->h_dest[0], fhead->h_dest[1], fhead->h_dest[2],
- fhead->h_dest[3], fhead->h_dest[4], fhead->h_dest[5]);
- printf("%s\n", fdata);
- for (i = 0; i < len ; i++) {
- printf("%02x ", (unsigned char) frame[i]);
- if ((i + 1) % 16 == 0)
- printf("\n");
- }
- printf("\n\n");
- free(frame);
- }
- close(sfd);
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement