Advertisement
Guest User

Untitled

a guest
Dec 11th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. #include <errno.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <net/if.h>
  7. #include <netinet/in.h>
  8. #include <netinet/ip.h>
  9. #include <netinet/udp.h>
  10. #include <sys/socket.h>
  11. #include <net/ethernet.h>
  12. #include <linux/if_packet.h>
  13.  
  14. unsigned short csum(void *buffer, unsigned int n)
  15. {
  16.     unsigned short byte;
  17.     short ret;
  18.     register unsigned long sum = 0;
  19.     const uint16_t *buf = buffer;
  20.  
  21.     while (n > 1) {
  22.         sum += *buf++;
  23.         n -= 2;
  24.     }
  25.     if (n == 1) {
  26.         byte = 0;
  27.         *((u_char*)&byte) = *(u_char*)buf;
  28.         sum += byte;
  29.         sum += htons(*(u_char *)buf << 8);
  30.     }
  31.  
  32.     sum = (sum >> 16) + (sum & 0xffff);
  33.     sum = sum + (sum >> 16);
  34.     ret = (unsigned short)~sum;
  35.  
  36.     return ret;
  37. }
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     struct sockaddr_ll saddr, daddr;
  42.     int saddr_size, data_size, bytes_sent;
  43.     struct ethhdr *ehdr;
  44.     struct iphdr *ip;
  45.     struct udphdr *udp;
  46.     unsigned char *payload;
  47.     unsigned char ch;
  48.     unsigned char *buffer;
  49.     int i, j;
  50.  
  51.     int sock_in = socket(AF_PACKET, SOCK_RAW, IPPROTO_UDP);
  52.     int sock_out = socket(AF_PACKET, SOCK_RAW, IPPROTO_UDP);
  53.  
  54.     if (argc != 3) {
  55.         printf("usage: %s <in_eth> <out_eth>\n", argv[0]);
  56.         return 1;
  57.     }
  58.  
  59.     buffer = (unsigned char*)malloc(IP_MAXPACKET * sizeof(unsigned char));
  60.  
  61.     bzero(buffer, IP_MAXPACKET * sizeof(unsigned char));
  62.     bzero(&saddr, sizeof(struct sockaddr_ll));
  63.  
  64.     saddr.sll_family = AF_PACKET;
  65.     saddr.sll_protocol = htons(ETH_P_ALL);
  66.     saddr.sll_ifindex = if_nametoindex(argv[1]);
  67.     if (bind(sock_in, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
  68.         perror("bind failed\n");
  69.         close(sock_in);
  70.     }
  71.  
  72.     bzero(&daddr, sizeof(struct sockaddr_ll));
  73.     daddr.sll_family = AF_PACKET;
  74.     daddr.sll_protocol = htons(ETH_P_ALL);
  75.     daddr.sll_ifindex = if_nametoindex(argv[2]);
  76.     if (bind(sock_out, (struct sockaddr *)&daddr, sizeof(daddr)) < 0) {
  77.         perror("bind failed\n");
  78.         close(sock_out);
  79.     }
  80.     struct ifreq ifr;
  81.     bzero(&ifr, sizeof(ifr));
  82.     snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", argv[2]);
  83.     if (setsockopt(sock_out, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
  84.         perror("bind to eth1");
  85.     }
  86.  
  87.     while (1) {
  88.         saddr_size = sizeof(struct sockaddr);
  89.         data_size = recvfrom(sock_in, buffer, ETH_FRAME_LEN, 0, (struct sockaddr *)&saddr, (socklen_t *)&saddr_size);
  90.         if (data_size > ETH_FRAME_LEN)
  91.             data_size = ETH_FRAME_LEN;
  92.  
  93.         if (data_size < 0) {
  94.             printf("Recvfrom error , failed to get packets\n");
  95.             return 1;
  96.         } else {
  97.             ehdr =  (struct ethhdr *)buffer;
  98.             ip = (struct iphdr *)(ehdr + 1);
  99.             udp = (struct udphdr *)(ip + 1);
  100.             i = 0;
  101.             j = ntohs(udp->len) - sizeof(struct udphdr) - 1;
  102.  
  103.             printf("Received %d bytes, udp->len = %d, udp->uh_ulen = %d\n", data_size, udp->len, udp->uh_ulen);
  104.             printf("%x:%x:%x:%x:%x:%x -> %x:%x:%x:%x:%x:%x\n",
  105.                 ehdr->h_source[0], ehdr->h_source[1], ehdr->h_source[2], ehdr->h_source[3], ehdr->h_source[4], ehdr->h_source[5],
  106.                 ehdr->h_dest[0], ehdr->h_dest[1], ehdr->h_dest[2], ehdr->h_dest[3], ehdr->h_dest[4], ehdr->h_dest[5]);
  107.  
  108.             payload = (unsigned char *)(udp + 1);
  109.             printf("Trying to resend '%s'(%lu, %d) ", payload, ntohs(udp->len) - sizeof(struct udphdr), ntohs(ip->ihl));
  110.  
  111.             while (i < j) {
  112.                 ch = payload[i];
  113.                 payload[i] = payload[j];
  114.                 payload[j] = ch;
  115.                 i++;
  116.                 j--;
  117.             }
  118.  
  119.             udp->check = 0;
  120.             udp->check = csum(udp, sizeof(struct udphdr));
  121.             printf("as '%s'\n", payload);
  122.  
  123.             bytes_sent = sendto(sock_out, buffer, data_size, 0, (struct sockaddr *)&daddr, sizeof(daddr));
  124.             if (bytes_sent < 0) {
  125.                 printf("sendto data_suze = %d\n", data_size+ 0);
  126.                 if (errno == EMSGSIZE)
  127.                     break;
  128.             }
  129.         }
  130.     }
  131.     free(buffer);
  132.     close(sock_in);
  133.     close(sock_out);
  134.  
  135.     return 0;
  136. }
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement