Cleanup
This commit is contained in:
@@ -48,35 +48,22 @@
|
||||
|
||||
#define STACK_LWIP 1
|
||||
#define STACK_PICO 0
|
||||
#define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code)
|
||||
|
||||
/* The following three quantities are related and govern how incoming frames are fed into the
|
||||
network stack's core:
|
||||
|
||||
Every LWIP_GUARDED_BUF_CHECK_INTERVAL milliseconds, a callback will be called from the core and
|
||||
will input a maximum of LWIP_FRAMES_HANDLED_PER_CORE_CALL frames before returning control back
|
||||
to the core. Meanwhile, incoming frames from the ZeroTier wire will be allocated and their
|
||||
pointers will be cached in the receive frame buffer of the size LWIP_MAX_GUARDED_RX_BUF_SZ to
|
||||
await the next callback from the core */
|
||||
|
||||
#define LWIP_GUARDED_BUF_CHECK_INTERVAL 50 // in ms
|
||||
#define LWIP_MAX_GUARDED_RX_BUF_SZ 1024 // number of frame pointers that can be cached waiting for receipt into core
|
||||
#define LWIP_FRAMES_HANDLED_PER_CORE_CALL 16
|
||||
#define NO_STACK 0 // for layer-2 only (this will omit all userspace network stack code)
|
||||
|
||||
/* sanity checks for userspace network stack and socket API layer choices
|
||||
|
||||
EX.
|
||||
zts_socket()
|
||||
1. ) ZT_VIRTUAL_SOCKET? -> virt_socket() --- Choose this if the default socket layer isn't doing what you need
|
||||
STACK_LWIP? -> raw lwip_ API
|
||||
STACK_PICO? -> raw pico_ API
|
||||
otherStack? -> raw API
|
||||
1. ) ZT_VIRTUAL_SOCKET? -> virt_socket() --- Choose this if the default socket layer isn't doing what you need
|
||||
STACK_LWIP? -> raw lwip_ API
|
||||
STACK_PICO? -> raw pico_ API
|
||||
otherStack? -> raw API
|
||||
|
||||
2.) ZT_LWIP_SEQ_SOCKET? (default) -> lwip_socket() --- currently provides greatest safety and performance
|
||||
3.) ZT_PICO_BSD_SOCKET? -> pico_ socket API
|
||||
otherStack? -> other_stack_socket()
|
||||
2.) ZT_LWIP_SEQ_SOCKET? (default) -> lwip_socket() --- currently provides greatest safety and performance
|
||||
3.) ZT_PICO_BSD_SOCKET? -> pico_ socket API
|
||||
otherStack? -> other_stack_socket()
|
||||
|
||||
Default is: STACK_LWIP=1 ZT_LWIP_SEQ_SOCKET=1
|
||||
Default is: STACK_LWIP=1 ZT_LWIP_SEQ_SOCKET=1
|
||||
|
||||
*/
|
||||
|
||||
@@ -100,48 +87,44 @@ await the next callback from the core */
|
||||
#endif
|
||||
|
||||
#if STACK_LWIP==1
|
||||
#undef STACK_PICO
|
||||
#undef NO_STACK
|
||||
#undef STACK_PICO
|
||||
#undef NO_STACK
|
||||
#endif
|
||||
#if STACK_PICO==1
|
||||
#undef STACK_LWIP
|
||||
#undef NO_STACK
|
||||
#undef STACK_LWIP
|
||||
#undef NO_STACK
|
||||
#endif
|
||||
#if NO_STACK==1
|
||||
#undef STACK_LWIP
|
||||
#undef STACK_PICO
|
||||
#undef STACK_LWIP
|
||||
#undef STACK_PICO
|
||||
#endif
|
||||
#if ZT_VIRTUAL_SOCKET==1
|
||||
#undef ZT_LWIP_SEQ_SOCKET
|
||||
#undef ZT_PICO_BSD_SOCKET
|
||||
#undef ZT_LWIP_SEQ_SOCKET
|
||||
#undef ZT_PICO_BSD_SOCKET
|
||||
#endif
|
||||
#if ZT_LWIP_SEQ_SOCKET==1
|
||||
#undef ZT_VIRTUAL_SOCKET
|
||||
#undef ZT_PICO_BSD_SOCKET
|
||||
#undef ZT_VIRTUAL_SOCKET
|
||||
#undef ZT_PICO_BSD_SOCKET
|
||||
#endif
|
||||
#if ZT_PICO_BSD_SOCKET==1
|
||||
#undef ZT_VIRTUAL_SOCKET
|
||||
#undef ZT_LWIP_SEQ_SOCKET
|
||||
#undef ZT_VIRTUAL_SOCKET
|
||||
#undef ZT_LWIP_SEQ_SOCKET
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Maximum MTU size for ZeroTier
|
||||
*/
|
||||
#define ZT_MAX_MTU 10000
|
||||
#define ZT_MAX_MTU 10000
|
||||
|
||||
/**
|
||||
* How fast service states are re-checked (in milliseconds)
|
||||
*/
|
||||
#define ZTO_WRAPPER_CHECK_INTERVAL 100
|
||||
#define ZTO_WRAPPER_CHECK_INTERVAL 100
|
||||
|
||||
/**
|
||||
* Length of buffer required to hold a ztAddress/nodeID
|
||||
*/
|
||||
#define ZTO_ID_LEN 16
|
||||
|
||||
//#if !defined(__WIN32__)
|
||||
//typedef unsigned int socklen_t;
|
||||
//#endif
|
||||
#define ZTO_ID_LEN 16
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <BaseTsd.h>
|
||||
@@ -151,8 +134,8 @@ typedef SSIZE_T ssize_t;
|
||||
/****************************************************************************/
|
||||
/* For SOCK_RAW support, it will initially be modeled after linux's API, so */
|
||||
/* below are the various things we need to define in order to make this API */
|
||||
/* work on other platforms. Mayber later down the road we will customize */
|
||||
/* this for each different platform. Maybe. */
|
||||
/* work on other platforms. Mayber later down the road we will customize */
|
||||
/* this for each different platform. Maybe. */
|
||||
/****************************************************************************/
|
||||
|
||||
#if !defined(__linux__)
|
||||
@@ -162,115 +145,67 @@ typedef SSIZE_T ssize_t;
|
||||
// Normally defined in linux/if_packet.h, defined here so we can offer a linux-like
|
||||
// raw socket API on non-linux platforms
|
||||
struct sockaddr_ll {
|
||||
unsigned short sll_family; /* Always AF_PACKET */
|
||||
unsigned short sll_protocol; /* Physical layer protocol */
|
||||
int sll_ifindex; /* Interface number */
|
||||
unsigned short sll_hatype; /* ARP hardware type */
|
||||
unsigned char sll_pkttype; /* Packet type */
|
||||
unsigned char sll_halen; /* Length of address */
|
||||
unsigned char sll_addr[8]; /* Physical layer address */
|
||||
unsigned short sll_family; /* Always AF_PACKET */
|
||||
unsigned short sll_protocol; /* Physical layer protocol */
|
||||
int sll_ifindex; /* Interface number */
|
||||
unsigned short sll_hatype; /* ARP hardware type */
|
||||
unsigned char sll_pkttype; /* Packet type */
|
||||
unsigned char sll_halen; /* Length of address */
|
||||
unsigned char sll_addr[8]; /* Physical layer address */
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
// Provide missing optnames for setsockopt() implementations
|
||||
#ifdef _WIN32
|
||||
#ifdef _WIN64
|
||||
#else
|
||||
#endif
|
||||
#elif __APPLE__
|
||||
#define IP_BIND_ADDRESS_NO_PORT 201
|
||||
#define IP_FREEBIND 202
|
||||
#define IP_MTU 203
|
||||
#define IP_MTU_DISCOVER 204
|
||||
#define IP_MULTICAST_ALL 205
|
||||
#define IP_NODEFRAG 206
|
||||
#define IP_RECVORIGDSTADDR 207
|
||||
#define IP_ROUTER_ALERT 208
|
||||
#define IP_TRANSPARENT 209
|
||||
#define TCP_INFO 210
|
||||
#define SO_STYLE 100
|
||||
#define TCP_CORK 101
|
||||
#define TCP_DEFER_ACCEPT 102
|
||||
//#ifndef TCP_KEEPIDLE
|
||||
//#define TCP_KEEPIDLE 103
|
||||
//#endif
|
||||
#define TCP_LINGER2 104
|
||||
#define TCP_QUICKACK 105
|
||||
#define TCP_SYNCNT 106
|
||||
#define TCP_WINDOW_CLAMP 107
|
||||
#define UDP_CORK 108
|
||||
#elif __linux__
|
||||
#define SO_STYLE 100
|
||||
#define UDP_CORK 101
|
||||
#define IP_BIND_ADDRESS_NO_PORT 201
|
||||
#define IP_NODEFRAG 206
|
||||
#elif __unix__
|
||||
#elif defined(_POSIX_VERSION)
|
||||
#else
|
||||
# error "Unknown platform"
|
||||
#endif
|
||||
*/
|
||||
|
||||
/****************************************************************************/
|
||||
/* Legend */
|
||||
/****************************************************************************/
|
||||
|
||||
/*
|
||||
NSLWIP network_stack_lwip
|
||||
NSPICO network_stack_pico
|
||||
NSRXBF network_stack_pico guarded frame buffer RX
|
||||
ZTVIRT zt_virtual_wire
|
||||
APPFDS app_fd
|
||||
VSRXBF app_fd TX buf
|
||||
VSTXBF app_fd RX buf
|
||||
*/
|
||||
|
||||
/****************************************************************************/
|
||||
/* lwIP */
|
||||
/****************************************************************************/
|
||||
|
||||
#define LWIP_NETIF_STATUS_CALLBACK 0
|
||||
|
||||
// For LWIP configuration see: include/lwipopts.h
|
||||
|
||||
#if defined(STACK_LWIP)
|
||||
|
||||
/* The following three quantities are related and govern how incoming frames are fed into the
|
||||
network stack's core:
|
||||
|
||||
Every LWIP_GUARDED_BUF_CHECK_INTERVAL milliseconds, a callback will be called from the core and
|
||||
will input a maximum of LWIP_FRAMES_HANDLED_PER_CORE_CALL frames before returning control back
|
||||
to the core. Meanwhile, incoming frames from the ZeroTier wire will be allocated and their
|
||||
pointers will be cached in the receive frame buffer of the size LWIP_MAX_GUARDED_RX_BUF_SZ to
|
||||
await the next callback from the core */
|
||||
|
||||
#define LWIP_GUARDED_BUF_CHECK_INTERVAL 50 // in ms
|
||||
#define LWIP_MAX_GUARDED_RX_BUF_SZ 1024 // number of frame pointers that can be cached waiting for receipt into core
|
||||
#define LWIP_FRAMES_HANDLED_PER_CORE_CALL 16 // How many frames are handled per call from core
|
||||
|
||||
#define LWIP_NETIF_STATUS_CALLBACK 0
|
||||
|
||||
typedef signed char err_t;
|
||||
|
||||
#define ND6_DISCOVERY_INTERVAL 1000
|
||||
#define ARP_DISCOVERY_INTERVAL ARP_TMR_INTERVAL
|
||||
|
||||
/**
|
||||
Specifies the polling interval and the callback function that should
|
||||
be called to poll the application. The interval is specified in
|
||||
number of TCP coarse grained timer shots, which typically occurs
|
||||
twice a second. An interval of 10 means that the application would
|
||||
be polled every 5 seconds. (only for raw lwIP driver)
|
||||
*/
|
||||
#define LWIP_APPLICATION_POLL_FREQ 2
|
||||
* Specifies the polling interval and the callback function that should
|
||||
* be called to poll the application. The interval is specified in
|
||||
* number of TCP coarse grained timer shots, which typically occurs
|
||||
* twice a second. An interval of 10 means that the application would
|
||||
* be polled every 5 seconds. (only for raw lwIP driver)
|
||||
*/
|
||||
#define LWIP_APPLICATION_POLL_FREQ 2
|
||||
|
||||
/**
|
||||
* TCP timer interval in milliseconds (only for raw lwIP driver)
|
||||
*/
|
||||
#define LWIP_TCP_TIMER_INTERVAL 25
|
||||
#define LWIP_TCP_TIMER_INTERVAL 25
|
||||
|
||||
/**
|
||||
* How often we check VirtualSocket statuses in milliseconds (only for raw lwIP driver)
|
||||
*/
|
||||
#define LWIP_STATUS_TMR_INTERVAL 500
|
||||
#define LWIP_STATUS_TMR_INTERVAL 500
|
||||
|
||||
// #define LWIP_CHKSUM <your_checksum_routine>, See: RFC1071 for inspiration
|
||||
#endif
|
||||
|
||||
/****************************************************************************/
|
||||
/* picoTCP */
|
||||
/****************************************************************************/
|
||||
|
||||
#if defined(STACK_PICO)
|
||||
#endif
|
||||
|
||||
/****************************************************************************/
|
||||
/* Defines */
|
||||
/****************************************************************************/
|
||||
@@ -278,141 +213,140 @@ typedef signed char err_t;
|
||||
/**
|
||||
* Maximum number of sockets that libzt can administer
|
||||
*/
|
||||
#define ZT_MAX_SOCKETS 1024
|
||||
#define ZT_MAX_SOCKETS 1024
|
||||
|
||||
/**
|
||||
* Maximum MTU size for libzt (must be less than or equal to ZT_MAX_MTU)
|
||||
*/
|
||||
#define ZT_SDK_MTU ZT_MAX_MTU
|
||||
#define ZT_SDK_MTU ZT_MAX_MTU
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#define ZT_LEN_SZ 4
|
||||
#define ZT_LEN_SZ 4
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#define ZT_ADDR_SZ 128
|
||||
#define ZT_ADDR_SZ 128
|
||||
|
||||
/**
|
||||
* Size of message buffer for VirtualSockets
|
||||
*/
|
||||
#define ZT_SOCKET_MSG_BUF_SZ ZT_SDK_MTU + ZT_LEN_SZ + ZT_ADDR_SZ
|
||||
#define ZT_SOCKET_MSG_BUF_SZ ZT_SDK_MTU + ZT_LEN_SZ + ZT_ADDR_SZ
|
||||
|
||||
/**
|
||||
* Polling interval (in ms) for file descriptors wrapped in the Phy I/O loop (for raw drivers only)
|
||||
*/
|
||||
#define ZT_PHY_POLL_INTERVAL 5
|
||||
#define ZT_PHY_POLL_INTERVAL 5
|
||||
|
||||
/**
|
||||
* State check interval (in ms) for VirtualSocket state
|
||||
*/
|
||||
#define ZT_ACCEPT_RECHECK_DELAY 50
|
||||
#define ZT_ACCEPT_RECHECK_DELAY 50
|
||||
|
||||
/**
|
||||
* State check interval (in ms) for VirtualSocket state
|
||||
*/
|
||||
#define ZT_CONNECT_RECHECK_DELAY 50
|
||||
#define ZT_CONNECT_RECHECK_DELAY 50
|
||||
|
||||
/**
|
||||
* State check interval (in ms) for VirtualSocket state
|
||||
*/
|
||||
#define ZT_API_CHECK_INTERVAL 50
|
||||
#define ZT_API_CHECK_INTERVAL 50
|
||||
|
||||
/**
|
||||
* Maximum size of guarded RX buffer (for picoTCP raw driver only)
|
||||
*/
|
||||
#define MAX_PICO_FRAME_RX_BUF_SZ ZT_MAX_MTU * 128
|
||||
#define MAX_PICO_FRAME_RX_BUF_SZ ZT_MAX_MTU * 128
|
||||
|
||||
/**
|
||||
* Size of TCP TX buffer for VirtualSockets used in raw network stack drivers
|
||||
*/
|
||||
#define ZT_TCP_TX_BUF_SZ 1024 * 1024 * 128
|
||||
#define ZT_TCP_TX_BUF_SZ 1024 * 1024 * 128
|
||||
|
||||
/**
|
||||
* Size of TCP RX buffer for VirtualSockets used in raw network stack drivers
|
||||
*/
|
||||
#define ZT_TCP_RX_BUF_SZ 1024 * 1024 * 128
|
||||
#define ZT_TCP_RX_BUF_SZ 1024 * 1024 * 128
|
||||
|
||||
/**
|
||||
* Size of UDP TX buffer for VirtualSockets used in raw network stack drivers
|
||||
*/
|
||||
#define ZT_UDP_TX_BUF_SZ ZT_MAX_MTU
|
||||
#define ZT_UDP_TX_BUF_SZ ZT_MAX_MTU
|
||||
|
||||
/**
|
||||
* Size of UDP RX buffer for VirtualSockets used in raw network stack drivers
|
||||
*/
|
||||
#define ZT_UDP_RX_BUF_SZ ZT_MAX_MTU * 10
|
||||
|
||||
// Send and Receive buffer sizes for the network stack
|
||||
// By default picoTCP sets them to 16834, this is good for embedded-scale
|
||||
// stuff but you might want to consider higher values for desktop and mobile
|
||||
// applications.
|
||||
#define ZT_UDP_RX_BUF_SZ ZT_MAX_MTU * 10
|
||||
|
||||
/**
|
||||
*
|
||||
* Send buffer size for the network stack
|
||||
* By default picoTCP sets them to 16834, this is good for embedded-scale
|
||||
* stuff but you might want to consider higher values for desktop and mobile
|
||||
* applications.
|
||||
*/
|
||||
#define ZT_STACK_TCP_SOCKET_TX_SZ ZT_TCP_TX_BUF_SZ
|
||||
#define ZT_STACK_TCP_SOCKET_TX_SZ ZT_TCP_TX_BUF_SZ
|
||||
|
||||
/**
|
||||
*
|
||||
* Receive buffer size for the network stack
|
||||
* By default picoTCP sets them to 16834, this is good for embedded-scale
|
||||
* stuff but you might want to consider higher values for desktop and mobile
|
||||
* applications.
|
||||
*/
|
||||
#define ZT_STACK_TCP_SOCKET_RX_SZ ZT_TCP_RX_BUF_SZ
|
||||
|
||||
// Maximum size we're allowed to read or write from a stack socket
|
||||
// This is put in place because picoTCP seems to fail at higher values.
|
||||
// If you use another stack you can probably bump this up a bit.
|
||||
#define ZT_STACK_TCP_SOCKET_RX_SZ ZT_TCP_RX_BUF_SZ
|
||||
|
||||
/**
|
||||
* Maximum size of write operation to a network stack
|
||||
* Maximum size we're allowed to read or write from a stack socket
|
||||
* This is put in place because picoTCP seems to fail at higher values.
|
||||
* If you use another stack you can probably bump this up a bit.
|
||||
*/
|
||||
#define ZT_STACK_SOCKET_WR_MAX 4096
|
||||
#define ZT_STACK_SOCKET_WR_MAX 4096
|
||||
|
||||
/**
|
||||
* Maximum size of read operation from a network stack
|
||||
*/
|
||||
#define ZT_STACK_SOCKET_RD_MAX 4096*4
|
||||
#define ZT_STACK_SOCKET_RD_MAX 4096*4
|
||||
|
||||
/**
|
||||
* Maximum length of libzt/ZeroTier home path (where keys, and config files are stored)
|
||||
*/
|
||||
#define ZT_HOME_PATH_MAX_LEN 256
|
||||
#define ZT_HOME_PATH_MAX_LEN 256
|
||||
|
||||
/**
|
||||
* Length of human-readable MAC address string
|
||||
*/
|
||||
#define ZT_MAC_ADDRSTRLEN 18
|
||||
#define ZT_MAC_ADDRSTRLEN 18
|
||||
|
||||
/**
|
||||
* Everything is ok
|
||||
*/
|
||||
#define ZT_ERR_OK 0
|
||||
#define ZT_ERR_OK 0
|
||||
|
||||
/**
|
||||
* Value returned during an internal failure at the VirtualSocket/VirtualTap layer
|
||||
*/
|
||||
#define ZT_ERR_GENERAL_FAILURE -88
|
||||
#define ZT_ERR_GENERAL_FAILURE -88
|
||||
|
||||
/**
|
||||
* Whether sockets created will have SO_LINGER set by default
|
||||
*/
|
||||
#define ZT_SOCK_BEHAVIOR_LINGER false
|
||||
#define ZT_SOCK_BEHAVIOR_LINGER false
|
||||
|
||||
/**
|
||||
* Length of time that VirtualSockets should linger (in seconds)
|
||||
*/
|
||||
#define ZT_SOCK_BEHAVIOR_LINGER_TIME 3
|
||||
#define ZT_SOCK_BEHAVIOR_LINGER_TIME 3
|
||||
|
||||
/**
|
||||
* Maximum wait time for socket closure if data is still present in the write queue
|
||||
*/
|
||||
#define ZT_SDK_CLTIME 60
|
||||
#define ZT_SDK_CLTIME 60
|
||||
|
||||
/**
|
||||
* Interval for performing background tasks (such as adding routes) on VirtualTap objects (in seconds)
|
||||
*/
|
||||
#define ZT_HOUSEKEEPING_INTERVAL 1
|
||||
#define ZT_HOUSEKEEPING_INTERVAL 1
|
||||
|
||||
/****************************************************************************/
|
||||
/* Socket API Signatures */
|
||||
|
||||
@@ -40,158 +40,158 @@
|
||||
|
||||
int inet_pton4(const char *src, void *dst)
|
||||
{
|
||||
uint8_t tmp[NS_INADDRSZ], *tp;
|
||||
uint8_t tmp[NS_INADDRSZ], *tp;
|
||||
|
||||
int saw_digit = 0;
|
||||
int octets = 0;
|
||||
*(tp = tmp) = 0;
|
||||
int saw_digit = 0;
|
||||
int octets = 0;
|
||||
*(tp = tmp) = 0;
|
||||
|
||||
int ch;
|
||||
while ((ch = *src++) != '\0')
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
uint32_t n = *tp * 10 + (ch - '0');
|
||||
int ch;
|
||||
while ((ch = *src++) != '\0')
|
||||
{
|
||||
if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
uint32_t n = *tp * 10 + (ch - '0');
|
||||
|
||||
if (saw_digit && *tp == 0)
|
||||
return 0;
|
||||
if (saw_digit && *tp == 0)
|
||||
return 0;
|
||||
|
||||
if (n > 255)
|
||||
return 0;
|
||||
if (n > 255)
|
||||
return 0;
|
||||
|
||||
*tp = n;
|
||||
if (!saw_digit)
|
||||
{
|
||||
if (++octets > 4)
|
||||
return 0;
|
||||
saw_digit = 1;
|
||||
}
|
||||
}
|
||||
else if (ch == '.' && saw_digit)
|
||||
{
|
||||
if (octets == 4)
|
||||
return 0;
|
||||
*++tp = 0;
|
||||
saw_digit = 0;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
if (octets < 4)
|
||||
return 0;
|
||||
*tp = n;
|
||||
if (!saw_digit)
|
||||
{
|
||||
if (++octets > 4)
|
||||
return 0;
|
||||
saw_digit = 1;
|
||||
}
|
||||
}
|
||||
else if (ch == '.' && saw_digit)
|
||||
{
|
||||
if (octets == 4)
|
||||
return 0;
|
||||
*++tp = 0;
|
||||
saw_digit = 0;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
if (octets < 4)
|
||||
return 0;
|
||||
|
||||
memcpy(dst, tmp, NS_INADDRSZ);
|
||||
memcpy(dst, tmp, NS_INADDRSZ);
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int inet_pton6(const char *src, void *dst)
|
||||
{
|
||||
static const char xdigits[] = "0123456789abcdef";
|
||||
uint8_t tmp[NS_IN6ADDRSZ];
|
||||
static const char xdigits[] = "0123456789abcdef";
|
||||
uint8_t tmp[NS_IN6ADDRSZ];
|
||||
|
||||
uint8_t *tp = (uint8_t*) memset(tmp, '\0', NS_IN6ADDRSZ);
|
||||
uint8_t *endp = tp + NS_IN6ADDRSZ;
|
||||
uint8_t *colonp = NULL;
|
||||
uint8_t *tp = (uint8_t*) memset(tmp, '\0', NS_IN6ADDRSZ);
|
||||
uint8_t *endp = tp + NS_IN6ADDRSZ;
|
||||
uint8_t *colonp = NULL;
|
||||
|
||||
/* Leading :: requires some special handling. */
|
||||
if (*src == ':')
|
||||
{
|
||||
if (*++src != ':')
|
||||
return 0;
|
||||
}
|
||||
/* Leading :: requires some special handling. */
|
||||
if (*src == ':')
|
||||
{
|
||||
if (*++src != ':')
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *curtok = src;
|
||||
int saw_xdigit = 0;
|
||||
uint32_t val = 0;
|
||||
int ch;
|
||||
while ((ch = tolower(*src++)) != '\0')
|
||||
{
|
||||
const char *pch = strchr(xdigits, ch);
|
||||
if (pch != NULL)
|
||||
{
|
||||
val <<= 4;
|
||||
val |= (pch - xdigits);
|
||||
if (val > 0xffff)
|
||||
return 0;
|
||||
saw_xdigit = 1;
|
||||
continue;
|
||||
}
|
||||
if (ch == ':')
|
||||
{
|
||||
curtok = src;
|
||||
if (!saw_xdigit)
|
||||
{
|
||||
if (colonp)
|
||||
return 0;
|
||||
colonp = tp;
|
||||
continue;
|
||||
}
|
||||
else if (*src == '\0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (tp + NS_INT16SZ > endp)
|
||||
return 0;
|
||||
*tp++ = (uint8_t) (val >> 8) & 0xff;
|
||||
*tp++ = (uint8_t) val & 0xff;
|
||||
saw_xdigit = 0;
|
||||
val = 0;
|
||||
continue;
|
||||
}
|
||||
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
|
||||
inet_pton4(curtok, (char*) tp) > 0)
|
||||
{
|
||||
tp += NS_INADDRSZ;
|
||||
saw_xdigit = 0;
|
||||
break; /* '\0' was seen by inet_pton4(). */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (saw_xdigit)
|
||||
{
|
||||
if (tp + NS_INT16SZ > endp)
|
||||
return 0;
|
||||
*tp++ = (uint8_t) (val >> 8) & 0xff;
|
||||
*tp++ = (uint8_t) val & 0xff;
|
||||
}
|
||||
if (colonp != NULL)
|
||||
{
|
||||
/*
|
||||
* Since some memmove()'s erroneously fail to handle
|
||||
* overlapping regions, we'll do the shift by hand.
|
||||
*/
|
||||
const int n = tp - colonp;
|
||||
const char *curtok = src;
|
||||
int saw_xdigit = 0;
|
||||
uint32_t val = 0;
|
||||
int ch;
|
||||
while ((ch = tolower(*src++)) != '\0')
|
||||
{
|
||||
const char *pch = strchr(xdigits, ch);
|
||||
if (pch != NULL)
|
||||
{
|
||||
val <<= 4;
|
||||
val |= (pch - xdigits);
|
||||
if (val > 0xffff)
|
||||
return 0;
|
||||
saw_xdigit = 1;
|
||||
continue;
|
||||
}
|
||||
if (ch == ':')
|
||||
{
|
||||
curtok = src;
|
||||
if (!saw_xdigit)
|
||||
{
|
||||
if (colonp)
|
||||
return 0;
|
||||
colonp = tp;
|
||||
continue;
|
||||
}
|
||||
else if (*src == '\0')
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (tp + NS_INT16SZ > endp)
|
||||
return 0;
|
||||
*tp++ = (uint8_t) (val >> 8) & 0xff;
|
||||
*tp++ = (uint8_t) val & 0xff;
|
||||
saw_xdigit = 0;
|
||||
val = 0;
|
||||
continue;
|
||||
}
|
||||
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
|
||||
inet_pton4(curtok, (char*) tp) > 0)
|
||||
{
|
||||
tp += NS_INADDRSZ;
|
||||
saw_xdigit = 0;
|
||||
break; /* '\0' was seen by inet_pton4(). */
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if (saw_xdigit)
|
||||
{
|
||||
if (tp + NS_INT16SZ > endp)
|
||||
return 0;
|
||||
*tp++ = (uint8_t) (val >> 8) & 0xff;
|
||||
*tp++ = (uint8_t) val & 0xff;
|
||||
}
|
||||
if (colonp != NULL)
|
||||
{
|
||||
/*
|
||||
* Since some memmove()'s erroneously fail to handle
|
||||
* overlapping regions, we'll do the shift by hand.
|
||||
*/
|
||||
const int n = tp - colonp;
|
||||
|
||||
if (tp == endp)
|
||||
return 0;
|
||||
if (tp == endp)
|
||||
return 0;
|
||||
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
endp[-i] = colonp[n - i];
|
||||
colonp[n - i] = 0;
|
||||
}
|
||||
tp = endp;
|
||||
}
|
||||
if (tp != endp)
|
||||
return 0;
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
endp[-i] = colonp[n - i];
|
||||
colonp[n - i] = 0;
|
||||
}
|
||||
tp = endp;
|
||||
}
|
||||
if (tp != endp)
|
||||
return 0;
|
||||
|
||||
memcpy(dst, tmp, NS_IN6ADDRSZ);
|
||||
memcpy(dst, tmp, NS_IN6ADDRSZ);
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
switch (af)
|
||||
{
|
||||
case AF_INET:
|
||||
return inet_pton4(src, dst);
|
||||
case AF_INET6:
|
||||
return inet_pton6(src, dst);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
switch (af)
|
||||
{
|
||||
case AF_INET:
|
||||
return inet_pton4(src, dst);
|
||||
case AF_INET6:
|
||||
return inet_pton6(src, dst);
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -254,5 +254,5 @@ char *beautify_eth_proto_nums(int proto)
|
||||
void mac2str(char *macbuf, int len, unsigned char* addr)
|
||||
{
|
||||
snprintf(macbuf, len, "%02x:%02x:%02x:%02x:%02x:%02x",
|
||||
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
||||
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
|
||||
}
|
||||
|
||||
46
src/lwIP.cpp
46
src/lwIP.cpp
@@ -78,7 +78,7 @@ struct netif lwipInterfaces[10];
|
||||
int lwipInterfacesCount = 0;
|
||||
|
||||
ZeroTier::Mutex _rx_input_lock_m;
|
||||
struct pbuf* lwip_frame_rxbuf[MAX_GUARDED_RX_BUF_SZ];
|
||||
struct pbuf* lwip_frame_rxbuf[LWIP_MAX_GUARDED_RX_BUF_SZ];
|
||||
int lwip_frame_rxbuf_tot = 0;
|
||||
|
||||
|
||||
@@ -386,10 +386,6 @@ void lwip_init_interface(void *tapref, const ZeroTier::MAC &mac, const ZeroTier:
|
||||
lwipInterfacesCount++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/****************************************************************************/
|
||||
/* Raw API driver */
|
||||
/****************************************************************************/
|
||||
@@ -1975,35 +1971,35 @@ inline void convert_ip(struct sockaddr_in * addr, struct ip4_addr *addr4);
|
||||
*/
|
||||
inline void convert_ip(struct sockaddr_in *addr, struct ip4_addr *addr4)
|
||||
{
|
||||
struct sockaddr_in *ipv4 = addr;
|
||||
short a = ip4_addr1b(&(ipv4->sin_addr));
|
||||
short b = ip4_addr2b(&(ipv4->sin_addr));
|
||||
short c = ip4_addr3b(&(ipv4->sin_addr));
|
||||
short d = ip4_addr4b(&(ipv4->sin_addr));
|
||||
IP4_ADDR(addr4, a,b,c,d);
|
||||
struct sockaddr_in *ipv4 = addr;
|
||||
short a = ip4_addr1b(&(ipv4->sin_addr));
|
||||
short b = ip4_addr2b(&(ipv4->sin_addr));
|
||||
short c = ip4_addr3b(&(ipv4->sin_addr));
|
||||
short d = ip4_addr4b(&(ipv4->sin_addr));
|
||||
IP4_ADDR(addr4, a,b,c,d);
|
||||
}
|
||||
|
||||
#define IP6_ADDR2(ipaddr, a,b,c,d,e,f,g,h) do { (ipaddr)->addr[0] = ZeroTier::Utils::hton((u32_t)((a & 0xffff) << 16) | (b & 0xffff)); \
|
||||
(ipaddr)->addr[1] = ZeroTier::Utils::hton(((c & 0xffff) << 16) | (d & 0xffff)); \
|
||||
(ipaddr)->addr[2] = ZeroTier::Utils::hton(((e & 0xffff) << 16) | (f & 0xffff)); \
|
||||
(ipaddr)->addr[3] = ZeroTier::Utils::hton(((g & 0xffff) << 16) | (h & 0xffff)); } while(0)
|
||||
(ipaddr)->addr[1] = ZeroTier::Utils::hton(((c & 0xffff) << 16) | (d & 0xffff)); \
|
||||
(ipaddr)->addr[2] = ZeroTier::Utils::hton(((e & 0xffff) << 16) | (f & 0xffff)); \
|
||||
(ipaddr)->addr[3] = ZeroTier::Utils::hton(((g & 0xffff) << 16) | (h & 0xffff)); } while(0)
|
||||
|
||||
/**
|
||||
* Convert from standard IPV6 address structure to an lwIP native structure
|
||||
*/
|
||||
inline void in6_to_ip6(ip6_addr *ba, struct sockaddr_in6 *in6)
|
||||
{
|
||||
uint8_t *ip = &(in6->sin6_addr).s6_addr[0];
|
||||
IP6_ADDR2(ba,
|
||||
(((ip[ 0] & 0xffff) << 8) | ((ip[ 1]) & 0xffff)),
|
||||
(((ip[ 2] & 0xffff) << 8) | ((ip[ 3]) & 0xffff)),
|
||||
(((ip[ 4] & 0xffff) << 8) | ((ip[ 5]) & 0xffff)),
|
||||
(((ip[ 6] & 0xffff) << 8) | ((ip[ 7]) & 0xffff)),
|
||||
(((ip[ 8] & 0xffff) << 8) | ((ip[ 9]) & 0xffff)),
|
||||
(((ip[10] & 0xffff) << 8) | ((ip[11]) & 0xffff)),
|
||||
(((ip[12] & 0xffff) << 8) | ((ip[13]) & 0xffff)),
|
||||
(((ip[14] & 0xffff) << 8) | ((ip[15]) & 0xffff))
|
||||
);
|
||||
uint8_t *ip = &(in6->sin6_addr).s6_addr[0];
|
||||
IP6_ADDR2(ba,
|
||||
(((ip[ 0] & 0xffff) << 8) | ((ip[ 1]) & 0xffff)),
|
||||
(((ip[ 2] & 0xffff) << 8) | ((ip[ 3]) & 0xffff)),
|
||||
(((ip[ 4] & 0xffff) << 8) | ((ip[ 5]) & 0xffff)),
|
||||
(((ip[ 6] & 0xffff) << 8) | ((ip[ 7]) & 0xffff)),
|
||||
(((ip[ 8] & 0xffff) << 8) | ((ip[ 9]) & 0xffff)),
|
||||
(((ip[10] & 0xffff) << 8) | ((ip[11]) & 0xffff)),
|
||||
(((ip[12] & 0xffff) << 8) | ((ip[13]) & 0xffff)),
|
||||
(((ip[14] & 0xffff) << 8) | ((ip[15]) & 0xffff))
|
||||
);
|
||||
}
|
||||
|
||||
#endif // ZT_VIRTUAL_SOCKET
|
||||
|
||||
Reference in New Issue
Block a user