Advertisement
danielhilst

sendethframe.c

May 13th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. /**
  2.  * @brief Sends an ethernet frame
  3.  */
  4.  
  5. #include <arpa/inet.h>
  6. #include <linux/if_packet.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <sys/ioctl.h>
  11. #include <sys/socket.h>
  12. #include <net/if.h>
  13. #include <netinet/ether.h>
  14. #include <linux/ip.h>
  15.  
  16. #define pexit(x)                                \
  17.         do {                                    \
  18.                 perror(x);                      \
  19.                 exit(EXIT_FAILURE);             \
  20.         } while (0)
  21.  
  22. static char buf[BUFSIZ];
  23.  
  24. void parse_mac(char *hwstr,  uint8_t mac[6])
  25. {
  26.         char *ptr;
  27.         int i;
  28.        
  29.         for (i = 0, ptr = hwstr; i < 6; i++) {
  30.                 mac[i] = strtol(ptr, NULL, 16);
  31.                 ptr = strchr(ptr, ':');
  32.                 if (!ptr)
  33.                         return;
  34.                 ptr++;
  35.         }
  36.                
  37. }
  38.  
  39. void print_mac(uint8_t mac[6])
  40. {
  41.         int i;
  42.        
  43.         printf("MAC: ");
  44.         for (i = 0; i < 6; i++)
  45.                 printf("%02X:", mac[i]);
  46.  
  47.         putchar('\n');
  48. }
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.         int sock;
  53.         struct ifreq ifr;
  54.         struct ether_header *eh = (struct ether_header *) buf;
  55.         struct sockaddr_ll sll;
  56.         int len = 0;
  57.         int i = 0;
  58.         int ifidx;
  59.         int n;
  60.  
  61.         if (argc != 4) {
  62.                 fprintf(stderr, "Usage: %s SRCIF DSTHW DATA\n", argv[0]);
  63.                 exit(EXIT_FAILURE);
  64.         }
  65.  
  66.         if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1)
  67.                 pexit("socket");
  68.  
  69.         memset(&ifr, '\0', sizeof(ifr));
  70.         strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
  71.         if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0)
  72.                 pexit("SIOCGIFINDEX");
  73.         printf("Source index: %d\n", ifr.ifr_ifindex);
  74.         ifidx = ifr.ifr_ifindex;
  75.  
  76.         if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0)
  77.                 pexit("SIOCGIFHWADDR");
  78.  
  79.         /* Set Destination and Source */
  80.         memcpy(eh->ether_shost, ifr.ifr_hwaddr.sa_data, 6);
  81.         printf("Source ");
  82.         print_mac(eh->ether_shost);
  83.  
  84.         parse_mac(argv[2], eh->ether_dhost);
  85.         printf("Destination ");
  86.         print_mac(eh->ether_dhost);
  87.         eh->ether_type = htons(ETH_P_ALL);
  88.  
  89.         len += sizeof(struct ether_header);
  90.  
  91.         strncpy(&buf[len], argv[3], strlen(argv[3]));
  92.         len += strlen(argv[3]);
  93.  
  94.         printf("LENGHT: %d\n", len);
  95.  
  96.         memset(&sll, '\0', sizeof(sll));
  97.         sll.sll_ifindex = ifidx;
  98.         sll.sll_halen = ETH_ALEN;
  99.         sll.sll_family = AF_PACKET;
  100.         parse_mac(argv[2], (uint8_t *)&sll.sll_addr);
  101.  
  102.         len += sizeof(struct ether_header);
  103.  
  104.         if (sendto(sock, buf, ETH_ALEN, 0, (struct sockaddr *)&sll, sizeof(sll)) < 0)
  105.                 perror("send");
  106.  
  107.         if ((n = recvfrom(sock, buf, BUFSIZ, 0, NULL, NULL)) < 0)
  108.                 perror("recvfrom");
  109.  
  110.         printf("Received %d bytes from %02x:%02x:%02x:%02x:%02x:%02x\n",
  111.                n,
  112.                eh->ether_dhost[0],
  113.                eh->ether_dhost[1],
  114.                eh->ether_dhost[2],
  115.                eh->ether_dhost[3],
  116.                eh->ether_dhost[4],
  117.                eh->ether_dhost[5]);
  118.  
  119.         return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement