From: Matt Corallo Date: Wed, 16 Mar 2022 04:40:48 +0000 (+0000) Subject: Auto-configure tun interface X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=tunudptotcp;a=commitdiff_plain;h=487fcef7148730fec86f39738823689487ad24c3 Auto-configure tun interface --- diff --git a/main.cpp b/main.cpp index 09802ce..eff9b46 100644 --- 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; }