lokinet/vendor/libtuntap-master/tuntap.cpp

176 lines
4.2 KiB
C++
Raw Normal View History

2018-08-20 19:12:12 +00:00
/*
* Copyright (c) 2012 Tristan Le Guern <tleguern@bouledef.eu>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#if defined Windows
#include <winsock2.h>
#include <ws2tcpip.h>
#include <wspiapi.h>
#if _WIN32_WINNT < 0x0600
extern "C" int
inet_pton(int af, const char *src, void *dst);
extern "C" const char *
inet_ntop(int af, const void *src, char *dst, size_t size);
#endif
2018-08-20 19:12:12 +00:00
#else
#include <arpa/inet.h>
#include <netinet/in.h>
#endif
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
2019-09-01 12:10:49 +00:00
#include <util/logging/logger.hpp>
2018-08-20 19:12:12 +00:00
#include "tuntap.h"
extern "C"
{
struct device *
tuntap_init(void)
{
struct device *dev = NULL;
if((dev = (struct device *)malloc(sizeof(*dev))) == NULL)
return NULL;
dev->obtain_fd = nullptr;
2018-12-14 14:56:10 +00:00
dev->user = nullptr;
2018-08-20 19:12:12 +00:00
(void)memset(dev->if_name, '\0', sizeof(dev->if_name));
dev->tun_fd = TUNFD_INVALID_VALUE;
dev->ctrl_sock = -1;
dev->flags = 0;
2018-12-14 14:56:10 +00:00
__tuntap_log = &tuntap_log_default;
2018-08-20 19:12:12 +00:00
return dev;
}
void
tuntap_destroy(struct device *dev)
{
tuntap_release(dev);
2018-12-14 14:56:10 +00:00
tuntap_sys_destroy(dev);
free(dev);
2018-08-20 19:12:12 +00:00
}
char *
tuntap_get_ifname(struct device *dev)
{
return dev->if_name;
}
int
tuntap_version(void)
{
return TUNTAP_VERSION;
}
int
2018-09-24 16:07:34 +00:00
tuntap_set_ip(struct device *dev, const char *addr, const char *daddr,
int netmask)
2018-08-20 19:12:12 +00:00
{
t_tun_in_addr baddr4;
t_tun_in6_addr baddr6;
uint32_t mask;
int errval;
/* Only accept started device */
if(dev->tun_fd == TUNFD_INVALID_VALUE)
{
llarp::LogInfo("device not started");
return 0;
}
if(addr == NULL)
{
llarp::LogError("Invalid address");
return -1;
}
if(netmask < 0 || netmask > 128)
{
llarp::LogError("Invalid netmask");
return -1;
}
/* Netmask */
mask = ~0;
mask = ~(mask >> netmask);
mask = htonl(mask);
/*
* Destination address parsing: we try IPv4 first and fall back to
* IPv6 if inet_pton return 0
*/
(void)memset(&baddr4, '\0', sizeof(baddr4));
(void)memset(&baddr6, '\0', sizeof(baddr6));
errval = inet_pton(AF_INET, addr, &(baddr4));
if(errval == 1)
{
2018-09-22 11:25:37 +00:00
#ifdef __FreeBSD__
2018-09-23 16:45:23 +00:00
t_tun_in_addr daddr4;
2018-09-22 11:25:37 +00:00
errval = inet_pton(AF_INET, daddr, &(daddr4));
if(errval == 1)
{
2018-10-29 17:20:50 +00:00
return tuntap_sys_set_ipv4_tun(dev, &baddr4, &daddr4, mask, netmask);
2018-09-22 11:25:37 +00:00
}
else
{
llarp::LogError("invalid s4dest address: ", addr);
return -1;
}
#else
(void)daddr;
2018-08-20 19:12:12 +00:00
return tuntap_sys_set_ipv4(dev, &baddr4, mask);
2018-09-22 11:25:37 +00:00
#endif
2018-08-20 19:12:12 +00:00
}
else if(errval == 0)
{
if(inet_pton(AF_INET6, addr, &(baddr6)) == -1)
{
llarp::LogError("invalid ipv6 address: ", addr);
return -1;
}
2019-06-11 16:44:05 +00:00
return tuntap_sys_set_ipv6(dev, &baddr6, netmask);
2018-08-20 19:12:12 +00:00
}
else if(errval == -1)
{
llarp::LogError("invalid address: ", addr);
return -1;
}
/* NOTREACHED */
return -1;
}
}