Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <arpa/inet.h>
- #include <linux/if_arp.h>
- #include <linux/if_ether.h>
- #include <linux/if_packet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #define ETH_P_CUSTOM 0x8888
- char *PkttypeNames[] = {
- "To us",
- "To all",
- "To group",
- "To someone else",
- "Originated by us",
- };
- int main(int argc, char** argv) {
- int sfd, i;
- ssize_t len;
- char* frame;
- char* fdata;
- //char* pkthost;
- struct ethhdr* fhead;
- struct ifreq ifr;
- struct sockaddr_ll sall;
- struct sockaddr_ll sender;
- 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;
- socklen_t addr_len = sizeof(sender);
- len = recvfrom(sfd, frame, ETH_FRAME_LEN, 0, (struct sockaddr*)&sender, &addr_len);
- printf("Rozmiar: [%dB] Nadawca: %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("Odbiorca: %02x:%02x:%02x:%02x:%02x:%02x |\n",
- 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("Typ pakietu: %d [%s]\n", sender.sll_pkttype, PkttypeNames[sender.sll_pkttype]);
- printf("EtherType : %d\n", sender.sll_protocol);
- printf("Dane: %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