Advertisement
Guest User

Untitled

a guest
Dec 11th, 2022
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.97 KB | None | 0 0
  1. #include <errno.h>
  2. #include <ev.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/un.h>
  7. #include <unistd.h>
  8. #include <pthread.h>
  9. #include <netinet/in.h>
  10. #include <net/ethernet.h>
  11. #include <netinet/ip.h>
  12. #include <netinet/udp.h>
  13. #include <net/if.h>
  14. #include <linux/if_packet.h>
  15.  
  16. #define MAIN_SOCKET "/tmp/main_socket"
  17. #define THREAD_SOCKET "/tmp/thread_socket"
  18. #define BUF_SZ ETH_FRAME_LEN
  19.  
  20. struct client {
  21.     ev_io io;
  22.     int main_fd;
  23.     int sock_in;
  24.     int sock_out;
  25.     struct client *next;
  26.     struct client *prev;
  27. };
  28.  
  29. struct io_args {
  30.     ev_io io;
  31.     int main_fd;
  32.     int sock_in;
  33.     int sock_out;
  34.     char *if_in;
  35.     char *if_out;
  36. };
  37.  
  38. struct client *clients = NULL;
  39.  
  40. void free_clients()
  41. {
  42.     struct client *client = clients, *tmp;
  43.  
  44.     while (client) {
  45.         tmp = client;
  46.         client = client->next;
  47.         free(tmp);
  48.     }
  49.  
  50.     clients = NULL;
  51. }
  52.  
  53. void add_client(struct client *client)
  54. {
  55.     if (clients == NULL) {
  56.         clients = client;
  57.         clients->next = clients->prev = NULL;
  58.         return;
  59.     }
  60.  
  61.     client->next = clients;
  62.     clients->prev = client;
  63.     client->prev = NULL;
  64.     clients = client;
  65. }
  66.  
  67. int pass_to_main(char *buff)
  68. {
  69.     struct sockaddr_un addr_un;
  70.     socklen_t addrlen = sizeof(struct sockaddr);
  71.     int ret = -1, fd;
  72.  
  73.     if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
  74.         perror("socket");
  75.         goto out;
  76.     }
  77.  
  78.     bzero(&addr_un, sizeof(addr_un));
  79.     addr_un.sun_family = AF_UNIX;
  80.     strcpy(addr_un.sun_path, THREAD_SOCKET);
  81.     unlink(THREAD_SOCKET);
  82.     if (bind(fd, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0) {
  83.         perror("bind");
  84.         goto out;
  85.     }
  86.  
  87.     bzero(&addr_un, sizeof(addr_un));
  88.     addr_un.sun_family = AF_UNIX;
  89.     strcpy(addr_un.sun_path, MAIN_SOCKET);
  90.     if (connect(fd, (struct sockaddr *)&addr_un, sizeof(addr_un)) == -1) {
  91.         perror("connect");
  92.         goto out;
  93.     }
  94.  
  95.     if (send(fd, buff, strlen(buff) + 1, 0) == -1) {
  96.         perror("send");
  97.     }
  98.     recvfrom(fd, buff, BUF_SZ, 0, (struct sockaddr *)&addr_un, &addrlen);
  99.  
  100.     ret = 0;
  101. out:
  102.     return ret;
  103. }
  104.  
  105. uint16_t csum(void *buffer, unsigned int n)
  106. {
  107.     unsigned short byte;
  108.     short ret;
  109.     register unsigned long sum = 0;
  110.     const uint16_t *buf = buffer;
  111.  
  112.     while (n > 1) {
  113.         sum += *buf++;
  114.         n -= 2;
  115.     }
  116.     if (n == 1) {
  117.         byte = 0;
  118.         *((u_char*)&byte) = *(u_char*)buf;
  119.         sum += byte;
  120.         sum += htons(*(u_char *)buf << 8);
  121.     }
  122.  
  123.     sum = (sum >> 16) + (sum & 0xffff);
  124.     sum = sum + (sum >> 16);
  125.     ret = (uint16_t)~sum;
  126.  
  127.     return ret;
  128. }
  129.  
  130. void thread_read_cb(struct ev_loop *loop, struct ev_io *io, int revents)
  131. {
  132.     struct sockaddr_un addr_un;
  133.     struct client *clnt;
  134.     char buffer[ETH_FRAME_LEN];
  135.     ssize_t read;
  136.     struct io_args *args;
  137.     int fd;
  138. printf("inside of %s\n", __func__);
  139.  
  140.     if (EV_ERROR & revents) {
  141.         perror("invalid event");
  142.         return;
  143.     }
  144.     args = (struct io_args*)io;
  145.  
  146.     struct sockaddr_ll saddr, daddr;
  147.     int saddr_size, data_size, bytes_sent;
  148.     struct ethhdr *ehdr;
  149.     struct iphdr *ip;
  150.     struct udphdr *udp;
  151.     unsigned char *payload;
  152.     unsigned char ch;
  153.     int i, j;
  154.         saddr_size = sizeof(struct sockaddr);
  155. //      printf("%s: args->sock_in = %d\n", __func__, args->sock_in);
  156.         read = recvfrom(args->sock_in, buffer, ETH_FRAME_LEN, 0, (struct sockaddr *)&saddr, (socklen_t *)&saddr_size);
  157. //      printf("get some data [%ld]\n", read);
  158.  
  159. //          if (read > ETH_FRAME_LEN)
  160. //              read = ETH_FRAME_LEN;
  161.  
  162.         if (read < 0) {
  163.             perror("recvfrom");
  164.             printf("Recvfrom error , failed to get packets, errno = %d\n", errno);
  165.             return;
  166.         } else if (read > 0) {
  167.             ehdr =  (struct ethhdr *)buffer;
  168.             ip = (struct iphdr *)(ehdr + 1);
  169.             udp = (struct udphdr *)((char *)ip + (ip->ihl << 2));
  170.             payload = (unsigned char *)(udp + 1);
  171. #if 0
  172. //          printf("ehdr->h_proto = %d, ip->protocol = %d\n", ehdr->h_proto, ip->protocol);
  173.  
  174.             if (ntohs(ehdr->h_proto) != ETH_P_IP) {
  175. //              printf("h_proto(%0xX) != ETH_P_IP\n", ehdr->h_proto);
  176.                 return;
  177.             }
  178.             if (ntohs(ip->protocol) != IPPROTO_UDP) {
  179.                 printf("protocol(0x%x) != IPPROTO_UDP\n", ip->protocol);
  180.                 return;
  181.             }
  182. #endif
  183.             uint16_t orig_csum = udp->check;
  184.             udp->check = 0;
  185.             udp->check = csum(udp, sizeof(struct udphdr));
  186.             if (ntohs(orig_csum) != udp->check) {
  187.                 printf("CSUM %x != %x\n", orig_csum, udp->check);
  188.                 udp->check = orig_csum;
  189. //              printf("BAD CSUM!!!\n");
  190. //              return;
  191.             } else
  192.                 printf("CSUM OK-OK-OK!!!\n");
  193.  
  194.             return;
  195.             i = 0;
  196.             j = ntohs(udp->len) - sizeof(struct udphdr) - 1;
  197.  
  198.             printf("Received %ld bytes: '%s', udp->len = %d\n", read, payload, ntohs(udp->len));
  199.             printf("%x:%x:%x:%x:%x:%x -> %x:%x:%x:%x:%x:%x\n",
  200.                 ehdr->h_source[0], ehdr->h_source[1], ehdr->h_source[2], ehdr->h_source[3], ehdr->h_source[4], ehdr->h_source[5],
  201.                 ehdr->h_dest[0], ehdr->h_dest[1], ehdr->h_dest[2], ehdr->h_dest[3], ehdr->h_dest[4], ehdr->h_dest[5]);
  202.  
  203.             printf("Trying to resend '%s'(%lu, %d)\n", payload, udp->len - sizeof(struct udphdr), ntohs(ip->ihl));
  204.  
  205.             printf("[i, j] = [%d, %d]\n", i, j);
  206.             while (i < j) {
  207.                 ch = payload[i];
  208.                 payload[i] = payload[j];
  209.                 payload[j] = ch;
  210.                 i++;
  211.                 j--;
  212.             }
  213.  
  214.             udp->check = 0;
  215.             udp->check = csum(udp, sizeof(struct udphdr));
  216.             printf("as '%s'\n", payload);
  217.  
  218.             bytes_sent = sendto(args->sock_out, buffer, read, 0, (struct sockaddr *)&daddr, sizeof(daddr));
  219.             if (bytes_sent < 0) {
  220.                 if (errno == EMSGSIZE) {
  221.                     perror("sendto");
  222.                     return;
  223.                 }
  224.                 printf("sendto data_size = %ld\n", read+ 0);
  225.             }
  226.         }
  227.  
  228.     if (read == 0)
  229.         return;
  230. /*
  231.     read = recv(io->fd, buffer, BUF_SZ, 0);
  232.  
  233.     if (read < 0) {
  234.         perror("read error");
  235.         return;
  236.     }
  237.  
  238.     if (read == 0) {
  239.         ev_io_stop(loop, io);
  240.         clnt = (struct client*)io;
  241.         if (!clnt->prev) {
  242.             if (clients->next) {
  243.                 clients = clnt->next;
  244.                 clients->next->prev = NULL;
  245.             } else
  246.                 clients = NULL;
  247.         } else if (!clnt->next) {
  248.             clnt->prev->next = NULL;
  249.         } else {
  250.             clnt->prev->next = clnt->next;
  251.             clnt->next->prev = clnt->prev;
  252.         }
  253.  
  254.         free(clnt);
  255.         return;
  256.     } else {
  257.         buffer[read] = '\0';
  258.         if (!strncmp(buffer, "quit", 4)) {
  259.             bzero(&addr_un, sizeof(addr_un));
  260.             addr_un.sun_family = AF_UNIX;
  261.             strcpy(addr_un.sun_path, MAIN_SOCKET);
  262.             fd = socket(AF_UNIX, SOCK_DGRAM, 0);
  263.             if (bind(fd, (struct sockaddr *)&addr_un, sizeof(addr_un)) < 0) {
  264.                 perror("bind");
  265.             }
  266.             if (sendto(fd, "\0", 1, 0, (struct sockaddr *)&addr_un, sizeof(addr_un)) == -1) {
  267.                 perror("thread send");
  268.             }
  269.             ev_io_stop(loop, io);
  270.             ev_break(loop, EVBREAK_ONE);
  271.             ev_loop_destroy(loop);
  272.             free_clients();
  273.             return;
  274.         }
  275.     }
  276. */
  277.     printf("pass_to_main()...\n");
  278.     pass_to_main(buffer);
  279.     printf("send(read == %ld)...\n", read);
  280.     send(io->fd, buffer, read, 0);
  281.     bzero(buffer, read);
  282.     printf("after send()...\n");
  283. }
  284. /*
  285. void thread_accept_cb(struct ev_loop *loop, struct ev_io *io, int revents)
  286. {
  287.     struct sockaddr_in client_addr;
  288.     struct client *client;
  289.     socklen_t client_len;
  290.     int fd;
  291.  
  292.     printf("%s\n", __func__);
  293.     client_len = sizeof(client_addr);
  294.     client = (struct client*)malloc(sizeof(struct client));
  295.     if (!client) {
  296.         perror("malloc");
  297.         return;
  298.     }
  299.  
  300.     client->prev = client->next = NULL;
  301.  
  302.     if (EV_ERROR & revents) {
  303.         perror("invalid event");
  304.         free(client);
  305.         return;
  306.     }
  307.  
  308.     fd = accept(io->fd, (struct sockaddr *)&client_addr, &client_len);
  309.  
  310.     if (fd < 0) {
  311.         perror("accept");
  312.         free(client);
  313.         return;
  314.     }
  315.  
  316.     ev_io_init(&(client->io), thread_read_cb, fd, EV_READ);
  317.     ev_io_start(loop, &(client->io));
  318.     add_client(client);
  319. }
  320. */
  321. void* thread_main(void *arg)
  322. {
  323.     struct ev_loop *loop;
  324.     struct client clnt;
  325.     struct io_args *args;
  326.     struct sockaddr_in addr;
  327.     int opt = 1;
  328.  
  329.     struct sockaddr_ll saddr, daddr;
  330.     int saddr_size, data_size, bytes_sent;
  331.     struct ethhdr *ehdr;
  332.     struct iphdr *ip;
  333.     struct udphdr *udp;
  334.     struct ifreq ifr;
  335.     unsigned char *payload;
  336.     unsigned char ch;
  337.     unsigned char *buffer;
  338.     int i, j;
  339.  
  340.     args = (struct io_args *)arg;
  341.     args->sock_in = socket(AF_PACKET, SOCK_RAW, IPPROTO_UDP);
  342.     if (args->sock_in == -1) {
  343.         perror("socket");
  344.         return NULL;
  345.     }
  346.     args->sock_out = socket(AF_PACKET, SOCK_RAW, IPPROTO_UDP);
  347.     if (args->sock_out == -1) {
  348.         perror("socket");
  349.         return NULL;
  350.     }
  351.     loop = ev_loop_new(0);
  352.  
  353.     buffer = (unsigned char*)malloc(IP_MAXPACKET * sizeof(unsigned char));
  354.  
  355.     bzero(buffer, IP_MAXPACKET * sizeof(unsigned char));
  356.  
  357.     bzero(&saddr, sizeof(struct sockaddr_ll));
  358.     saddr.sll_family = AF_PACKET;
  359.     saddr.sll_protocol = htons(ETH_P_ALL);
  360.     saddr.sll_ifindex = if_nametoindex(args->if_in);
  361.     printf("args->sock_in = %d\n", args->sock_in);
  362.     if (bind(args->sock_in, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
  363.         perror("bind failed");
  364.         close(args->sock_in);
  365.         free(buffer);
  366.         return NULL;
  367.     }
  368.  
  369.     bzero(&daddr, sizeof(struct sockaddr_ll));
  370.     daddr.sll_family = AF_PACKET;
  371.     daddr.sll_protocol = htons(ETH_P_ALL);
  372.     daddr.sll_ifindex = if_nametoindex(args->if_out);
  373.     if (bind(args->sock_out, (struct sockaddr *)&daddr, sizeof(daddr)) < 0) {
  374.         perror("bind failed\n");
  375.         close(args->sock_out);
  376.         free(buffer);
  377.         return NULL;
  378.     }
  379.  
  380.     bzero(&ifr, sizeof(ifr));
  381.     snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", args->if_in);
  382.     if (setsockopt(args->sock_in, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
  383.         perror("bind to eth1");
  384.         free(buffer);
  385.         return NULL;
  386.     }
  387.  
  388.     if (setsockopt(args->sock_in, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
  389.         perror("setsockopt on sock_in");
  390.         free(buffer);
  391.         return NULL;
  392.     }
  393.     bzero(&ifr, sizeof(ifr));
  394.     snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", args->if_out);
  395.     if (setsockopt(args->sock_out, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
  396.         perror("bind to eth1");
  397.         free(buffer);
  398.         return NULL;
  399.     }
  400.  
  401.     if (setsockopt(args->sock_out, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
  402.         perror("setsockopt on sock_out");
  403.         free(buffer);
  404.         return NULL;
  405.     }
  406.  
  407.     printf("INSIDE THREAD\n");
  408.  
  409. //  while (1) {
  410. //  }
  411. /*
  412.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  413.         perror("socket");
  414.         return NULL;
  415.     }
  416.  
  417.     if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
  418.         perror("setsockopt");
  419.     }
  420.  
  421.     args = (struct io_args *)arg;
  422.     clnt.main_fd = args->main_fd;
  423.     bzero(&addr, sizeof(addr));
  424.     addr.sin_family = AF_INET;
  425.     addr.sin_port = htons(args->port);
  426.     addr.sin_addr.s_addr = INADDR_ANY;
  427.  
  428.     if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
  429.         perror("bind");
  430.     }
  431.  
  432.     if (listen(sock, 2) < 0) {
  433.         perror("listen");
  434.         return NULL;
  435.     }
  436. */
  437.  
  438.     printf("args->sock_in = %d\n", args->sock_in);
  439.     clnt.main_fd = args->main_fd;
  440.     clnt.sock_in = args->sock_in;
  441.     clnt.sock_out = args->sock_out;
  442.     ev_io_init(&clnt.io, thread_read_cb, args->sock_in, EV_READ | EV_WRITE);
  443.     ev_io_start(loop, &clnt.io);
  444.  
  445.     ev_loop(loop, 0);
  446.  
  447.     unlink(THREAD_SOCKET);
  448.  
  449.     return  NULL;
  450. }
  451.  
  452. void read_cb(struct ev_loop* loop, __attribute__ ((unused)) struct ev_io* io, int revents)
  453. {
  454.     struct sockaddr_un addr, th_addr;
  455.     socklen_t sock_len = sizeof(th_addr);
  456.     char buffer[BUF_SZ], ch;
  457.     int i, j, fd, ret, len;
  458. printf("inside of %s\n", __func__);
  459.  
  460.     if (EV_ERROR & revents) {
  461.         perror("invalid event");
  462.         return;
  463.     }
  464.  
  465.     if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
  466.         perror("socket");
  467.         goto out;
  468.     }
  469.  
  470.     memset(&addr, 0, sizeof(addr));
  471.     addr.sun_family = AF_UNIX;
  472.     strcpy(addr.sun_path, MAIN_SOCKET);
  473.     unlink(MAIN_SOCKET);
  474.     if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  475.         perror("bind");
  476.         goto out;
  477.     }
  478.     while ((len = recvfrom(fd, buffer, BUF_SZ, 0, (struct sockaddr *)&th_addr, &sock_len)) > 0 &&
  479.             (buffer[0] != '\0')) {
  480.  
  481.         i = 0;
  482.         j = strlen(buffer) - 1;
  483.         if (j < 0)
  484.             return;
  485.  
  486.         if (buffer[j] == '\n')
  487.             j--;
  488.         while (i < j) {
  489.             ch = buffer[i];
  490.             buffer[i] = buffer[j];
  491.             buffer[j] = ch;
  492.             i++;
  493.             j--;
  494.         }
  495.  
  496.         if (strlen(buffer) == 0)
  497.             continue;
  498.  
  499.         ret = sendto(fd, buffer, strlen(buffer) + 1, 0, (struct sockaddr *)&th_addr, sock_len);
  500.         if (ret < 0) {
  501.             perror("sendto");
  502.             break;
  503.         }
  504.     }
  505.  
  506. out:
  507.     if (fd >= 0) {
  508.         close(fd);
  509.     }
  510.     printf("%s: EV_BREAK\n", __func__);
  511.     ev_break(loop, EVBREAK_ONE);
  512. }
  513.  
  514. int main(int argc, char **argv)
  515. {
  516.     struct ev_loop *loop;
  517.     struct sockaddr_un addr;
  518.     struct io_args *io_args;
  519.     pthread_t thread = -1;
  520.     int sock;
  521.     int opt = 1;
  522.  
  523.     if (argc != 3)
  524.         return -1;
  525.  
  526.     if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
  527.         perror("socket");
  528.         return -1;
  529.     }
  530.  
  531.     if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1) {
  532.         perror("setsockopt");
  533.     }
  534.  
  535.     loop = EV_DEFAULT;
  536.     memset(&addr, 0, sizeof(addr));
  537.     addr.sun_family = AF_LOCAL;
  538.     strcpy(addr.sun_path, MAIN_SOCKET);
  539.     unlink(MAIN_SOCKET);
  540.     if (bind(sock, (struct sockaddr*)(&addr), sizeof(addr)) < 0) {
  541.         perror("bind");
  542.         return -1;
  543.     }
  544.  
  545.     listen(sock, 10);
  546.  
  547.     io_args = malloc(sizeof(struct io_args));
  548.     if (!io_args) {
  549.         perror("malloc");
  550.         goto out;
  551.     }
  552.  
  553.     io_args->main_fd = sock;
  554.     io_args->if_in = argv[1];
  555.     io_args->if_out = argv[2];
  556.  
  557.     ev_io_init(&io_args->io, read_cb, sock, EV_READ | EV_WRITE);
  558.     pthread_create(&thread, NULL, &thread_main, io_args);
  559.     ev_io_start(loop, &io_args->io);
  560.     ev_loop(loop, 0);
  561. sleep(5);
  562. out:
  563.     pthread_join(thread, NULL);
  564.     ev_loop_destroy(loop);
  565.     free(io_args);
  566.     unlink(MAIN_SOCKET);
  567.  
  568.     return 0;
  569. }
  570.  
  571.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement