Auto-configure tun interface
authorMatt Corallo <git@bluematt.me>
Wed, 16 Mar 2022 04:40:48 +0000 (04:40 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 16 Mar 2022 04:40:48 +0000 (04:40 +0000)
main.cpp

index 09802cee9c6984d44ffed2c68532f83d03f12052..eff9b46a8f14ec4c43aebf23a167620004d51bb7 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -29,11 +29,13 @@ int getrandom(void* buf, size_t len, unsigned int flags) {
 }
 #endif
 
+#define PACKET_READ_SIZE 1500
 
-static int tun_alloc(char *dev, int queues, int *fds)
+static int tun_alloc(char *dev, const char* local_ip, const char* remote_ip, int queues, int *fds)
 {
        struct ifreq ifr;
        int fd, err, i;
+       char buf[1024];
 
        if (!dev)
                return -1;
@@ -58,6 +60,23 @@ static int tun_alloc(char *dev, int queues, int *fds)
                }
                fds[i] = fd;
        }
+
+       sprintf(buf, "ip link set %s mtu %d", dev, PACKET_READ_SIZE);
+       err = system(buf);
+       if (err) goto err;
+
+       sprintf(buf, "ip addr add %s/32 dev %s", local_ip, dev);
+       err = system(buf);
+       if (err) goto err;
+
+       sprintf(buf, "ip link set %s up", dev);
+       err = system(buf);
+       if (err) goto err;
+
+       sprintf(buf, "ip route add %s/32 dev %s", remote_ip, dev);
+       err = system(buf);
+       if (err) goto err;
+
        return 0;
 err:
        for (--i; i >= 0; i--)
@@ -585,7 +604,7 @@ int main(int argc, char* argv[]) {
        // Create tun and bind to sockets...
        //
 
-       if (tun_alloc(tun_name, TUN_IF_COUNT, fd) != 0) {
+       if (tun_alloc(tun_name, argv[3], argv[2], TUN_IF_COUNT, fd) != 0) {
                fprintf(stderr, "Failed to alloc tun if\n");
                return -1;
        }