Advertisement
Guest User

Rate Limiting Tunnel

a guest
Dec 4th, 2023
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include <net/if.h>
  8. #include <linux/if_tun.h>
  9. #include <arpa/inet.h>
  10. #include <pthread.h>
  11.  
  12. #define BUFSIZE 2000
  13.  
  14. int make_tun(char const *dev) {
  15.     struct ifreq ifr;
  16.     int fd, err;
  17.  
  18.     if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
  19.         perror("Opening /dev/net/tun");
  20.         exit(EXIT_FAILURE);
  21.     }
  22.  
  23.     memset(&ifr, 0, sizeof(ifr));
  24.     ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  25.  
  26.     if (*dev)
  27.         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  28.  
  29.     if ((err = ioctl(fd, TUNSETIFF, (void *)&ifr)) < 0) {
  30.         perror("ioctl(TUNSETIFF)");
  31.         close(fd);
  32.         exit(err);
  33.     }
  34.  
  35.     return fd;
  36. }
  37.  
  38. int setup_tun(char const *ns, char const *dev, char const *ip) {
  39.     char command[200];
  40.     char const *cmds[] = {
  41.         "ip netns add %s",
  42.         "ip netns exec %s ip link set dev lo up",
  43.         "ip link set netns %s dev %s",
  44.         "ip netns exec %s ip addr add dev %s %s",
  45.         "ip netns exec %s ip link set dev %s up",        
  46.         0
  47.     };
  48.     char const * const *cmd;
  49.     int fd = make_tun(dev);
  50.     for (cmd=cmds; *cmd; ++cmd) {
  51.         snprintf(command, sizeof(command), *cmd, ns, dev, ip);
  52.         int ret = system(command);
  53.         printf("'%s' returned %d\n", command, ret);
  54.     }
  55.     return fd;
  56. }
  57.  
  58. void forward_traffic(int src_fd, int dest_fd) {
  59.     char buf[BUFSIZE];
  60.     ssize_t nbytes = 0;
  61.    
  62.     while (nbytes >= 0) {
  63.         nbytes = read(src_fd, buf, sizeof(buf));
  64.  
  65.         if (nbytes >= 0) {
  66.             usleep(nbytes);
  67.             nbytes = write(dest_fd, buf, nbytes);
  68.         }
  69.     }
  70.     perror("Read/write TUN device");
  71.     exit(EXIT_FAILURE);
  72. }
  73.  
  74. void *thread_function(void *arg) {
  75.     int *fds = (int *)arg;
  76.  
  77.     forward_traffic(fds[0], fds[1]);
  78.  
  79.     return NULL;
  80. }
  81.  
  82. int main() {
  83.     int tun_fd1, tun_fd2;
  84.     pthread_t tid1, tid2;
  85.     int fds1[2], fds2[2];
  86.  
  87.     tun_fd1 = setup_tun("test1", "tun0", "10.0.0.1/24");
  88.     tun_fd2 = setup_tun("test2", "tun1", "10.0.0.2/24");
  89.  
  90.     fds1[0] = tun_fd1;
  91.     fds1[1] = tun_fd2;
  92.  
  93.     fds2[0] = tun_fd2;
  94.     fds2[1] = tun_fd1;
  95.  
  96.     pthread_create(&tid1, NULL, thread_function, (void *)fds1);
  97.     pthread_create(&tid2, NULL, thread_function, (void *)fds2);
  98.  
  99.     pthread_join(tid1, NULL);
  100.     pthread_join(tid2, NULL);
  101.  
  102.     close(tun_fd1);
  103.     close(tun_fd2);
  104.  
  105.     return EXIT_SUCCESS;
  106. }
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement