Add IPv6 to C# ZeroTier.Sockets, Add C# selftest, Misc C API improvements
This commit is contained in:
10
build.ps1
10
build.ps1
@@ -230,4 +230,14 @@ function Clean
|
||||
{
|
||||
rm cache -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
rm dist -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
#rm pkg\nuget\bindings\ZeroTier.Sockets\*.cs -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
#rm pkg\nuget\bindings\ZeroTier.Sockets\bin -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
#rm pkg\nuget\bindings\ZeroTier.Sockets\obj -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
#rm pkg\nuget\ZeroTier.Sockets\contentFiles -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
rm pkg\nuget\ZeroTier.Sockets\lib -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
rm pkg\nuget\ZeroTier.Sockets\bin -Recurse -Force -Confirm:$false -ErrorAction:'silentlycontinue'
|
||||
rm 'pkg\nuget\*' -Recurse -Include *.pdb
|
||||
rm 'pkg\nuget\*' -Recurse -Include *.cs
|
||||
rm 'pkg\nuget\*' -Recurse -Include *.dll
|
||||
rm 'pkg\nuget\*' -Recurse -Include LICENSE.*
|
||||
}
|
||||
|
||||
34
build.sh
34
build.sh
@@ -310,6 +310,7 @@ host()
|
||||
if [[ $1 = *"docs"* ]]; then
|
||||
# Generate documentation
|
||||
cd docs/c && doxygen
|
||||
cp -f *.png html/
|
||||
exit 0
|
||||
fi
|
||||
# -DZTS_ENABLE_CENTRAL_API=0
|
||||
@@ -547,15 +548,42 @@ test-c()
|
||||
rm -rf $TARGET_BUILD_DIR
|
||||
# Build selftest
|
||||
test $1
|
||||
# Ports can be anything we want since they aren't system ports
|
||||
port4=8080
|
||||
port6=8081
|
||||
# Start Alice as server
|
||||
"$BIN_OUTPUT_DIR/selftest-c-api" $alice_path $testnet $port4 $port6 &
|
||||
# Start Bob as client
|
||||
"$BIN_OUTPUT_DIR/selftest-c-api" $bob_path $testnet $port4 $alice_ip4 $port6 $alice_ip6 &
|
||||
}
|
||||
|
||||
# Test C# API
|
||||
test-cs()
|
||||
{
|
||||
if [[ -z "${alice_path}" ]]; then
|
||||
echo "Please set necessary environment variables for test"
|
||||
exit 0
|
||||
fi
|
||||
ARTIFACT="pinvoke"
|
||||
# Default to debug so asserts aren't optimized out
|
||||
BUILD_TYPE=${1:-debug}
|
||||
TARGET_BUILD_DIR=$DEFAULT_HOST_BIN_OUTPUT_DIR-$ARTIFACT-$BUILD_TYPE
|
||||
LIB_OUTPUT_DIR=$TARGET_BUILD_DIR/lib
|
||||
BIN_OUTPUT_DIR=$TARGET_BUILD_DIR/bin
|
||||
rm -rf $TARGET_BUILD_DIR
|
||||
# Build C shared library exporting C# symbols
|
||||
host-pinvoke $1
|
||||
# TODO: This should eventually be converted to a proper dotnet project
|
||||
# Build C# managed API library
|
||||
csc -target:library -out:$LIB_OUTPUT_DIR/ZeroTier.Sockets.dll src/bindings/csharp/*.cs
|
||||
# Build selftest
|
||||
mkdir -p $BIN_OUTPUT_DIR
|
||||
csc -out:$BIN_OUTPUT_DIR/selftest.exe -reference:$LIB_OUTPUT_DIR/ZeroTier.Sockets.dll test/selftest.cs
|
||||
# Copy shared libraries into bin directory so they can be discovered by dlopen
|
||||
cp $LIB_OUTPUT_DIR/* $BIN_OUTPUT_DIR
|
||||
# Start Alice as server
|
||||
mono --debug "$BIN_OUTPUT_DIR/selftest.exe" $alice_path $testnet $port4 $port6 &
|
||||
# Start Bob as client
|
||||
mono --debug "$BIN_OUTPUT_DIR/selftest.exe" $bob_path $testnet $port4 $alice_ip4 $port6 $alice_ip6 &
|
||||
}
|
||||
|
||||
# Recursive deep clean
|
||||
clean()
|
||||
{
|
||||
|
||||
@@ -139,7 +139,8 @@ public class ExampleApp {
|
||||
}
|
||||
catch (ZeroTier.Sockets.SocketException e)
|
||||
{
|
||||
Console.WriteLine("ServiveErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode);
|
||||
Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}",
|
||||
e.ServiceErrorCode, e.SocketErrorCode);
|
||||
}
|
||||
if (bytesRec > 0) {
|
||||
Console.WriteLine("Bytes received: {0}", bytesRec);
|
||||
@@ -163,7 +164,8 @@ public class ExampleApp {
|
||||
|
||||
} catch (ZeroTier.Sockets.SocketException e) {
|
||||
Console.WriteLine(e);
|
||||
Console.WriteLine("ServiveErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode);
|
||||
Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}",
|
||||
e.ServiceErrorCode, e.SocketErrorCode);
|
||||
}
|
||||
|
||||
Console.WriteLine("\nPress ENTER to continue...");
|
||||
@@ -212,7 +214,7 @@ public class ExampleApp {
|
||||
Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
|
||||
} catch (ZeroTier.Sockets.SocketException e) {
|
||||
Console.WriteLine(e);
|
||||
Console.WriteLine("ServiveErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode);
|
||||
Console.WriteLine("ServiceErrorCode={0} SocketErrorCode={1}", e.ServiceErrorCode, e.SocketErrorCode);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine( e.ToString());
|
||||
|
||||
@@ -1519,14 +1519,29 @@ ZTS_API int ZTCALL zts_connect(
|
||||
/**
|
||||
* @brief Connect a socket to a remote host
|
||||
*
|
||||
* This convenience function exists because ZeroTier uses transport-triggered
|
||||
* links. This means that links between peers do not exist until peers try to
|
||||
* talk to each other. This can be a problem during connection procedures since
|
||||
* some of the initial packets are lost. To alleviate the need to try
|
||||
* `zts_connect` many times, this function will keep re-trying for you, even if
|
||||
* no known routes exist. However, if the socket is set to `non-blocking` mode
|
||||
* it will behave identically to `zts_connect` and return immediately upon
|
||||
* failure.
|
||||
*
|
||||
* @param fd Socket file descriptor
|
||||
* @param family Address family: `ZTS_AF_INET` or `ZTS_AF_INET6`
|
||||
* @param ipstr Human-readable IP string
|
||||
* @param port Port
|
||||
* @return `ZTS_ERR_OK` if successful, `ZTS_ERR_SERVICE` if the node
|
||||
* experiences a problem, `ZTS_ERR_ARG` if invalid arg. Sets `zts_errno`
|
||||
* @param timeout_ms (Approximate) amount of time in milliseconds before
|
||||
* connection attempt is aborted. Will block for `30 seconds` if timeout is
|
||||
* set to `0`.
|
||||
*
|
||||
* @return `ZTS_ERR_OK` if successful, `ZTS_ERR_SOCKET` if the function times
|
||||
* out with no connection made, `ZTS_ERR_SERVICE` if the node experiences a
|
||||
* problem, `ZTS_ERR_ARG` if invalid arg. Sets `zts_errno`
|
||||
*/
|
||||
ZTS_API int ZTCALL zts_connect_easy(int fd, int family, char *ipstr, int port);
|
||||
ZTS_API int ZTCALL zts_connect_easy(
|
||||
int fd, int family, char *ipstr, int port, int timeout_ms);
|
||||
|
||||
/**
|
||||
* @brief Bind a socket to a local address
|
||||
@@ -1574,6 +1589,19 @@ ZTS_API int ZTCALL zts_listen(int fd, int backlog);
|
||||
ZTS_API int ZTCALL zts_accept(
|
||||
int fd, struct zts_sockaddr *addr, zts_socklen_t *addrlen);
|
||||
|
||||
/**
|
||||
* @brief Accept an incoming connection
|
||||
*
|
||||
* @param fd Socket file descriptor
|
||||
* @param remoteIpStr Buffer that will receive remote host IP string
|
||||
* @param len Size of buffer that will receive remote host IP string
|
||||
* Must be set to `ZTS_INET6_ADDRSTRLEN`
|
||||
* @param port Port number of the newly connected remote host (value-result)
|
||||
* @return New file descriptor if successful, `ZTS_ERR_SERVICE` if the node
|
||||
* experiences a problem, `ZTS_ERR_ARG` if invalid arg. Sets `zts_errno`
|
||||
*/
|
||||
ZTS_API int ZTCALL zts_accept_easy(int fd, char *remoteIpStr, int len, int *port);
|
||||
|
||||
// Socket level option number
|
||||
#define ZTS_SOL_SOCKET 0x0fff
|
||||
// Socket options
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
[ZeroTier](https://www.zerotier.com) SDK
|
||||
[ZeroTier](https://www.zerotier.com)
|
||||
=====
|
||||
|
||||
Connect physical devices, virtual devices, and application instances as if everything is on a single LAN.
|
||||
Securely connect application instances, physical devices, and virtual devices as if everything is on a single LAN. ZeroTier brings your network into user-space. No root, and no host configuration requirements.
|
||||
|
||||
ZeroTier brings your network into user-space. We've paired our network hypervisor core with a TCP/UDP/IP stack [(lwIP)](https://en.wikipedia.org/wiki/LwIP) to provide your application with an exclusive and private virtual network interface. All traffic on this interface is end-to-end encrypted between each peer and we provide an easy-to-use socket interface similar to [Berkeley Sockets](https://en.wikipedia.org/wiki/Berkeley_sockets). Since we aren't using the kernel's IP stack that means no drivers, no root, and no host configuration requirements.
|
||||
We've paired our network hyper-visor core with a TCP/UDP/IP stack [(lwIP)](https://en.wikipedia.org/wiki/LwIP) to provide your application with an exclusive and private virtual network interface. All traffic on this interface is end-to-end encrypted between each peer and we provide an easy-to-use socket interface similar to [Berkeley Sockets](https://en.wikipedia.org/wiki/Berkeley_sockets).
|
||||
|
||||
- Website: https://www.zerotier.com/
|
||||
- ZeroTier Manual: https://www.zerotier.com/manual/
|
||||
@@ -11,19 +11,20 @@ ZeroTier brings your network into user-space. We've paired our network hyperviso
|
||||
- SDK Repo: https://github.com/zerotier/libzt
|
||||
- Forum: https://discuss.zerotier.com
|
||||
|
||||
## 1.3.3-alpha.2 Release Notes
|
||||
## 1.3.4 Release Notes
|
||||
|
||||
### New namespace structure:
|
||||
- `ZeroTier.Core` (API to control a ZeroTier Node)
|
||||
- `class ZeroTier.Core.Node`
|
||||
- `class ZeroTier.Core.Event`
|
||||
- `ZeroTier.Sockets`
|
||||
- `class ZeroTier.Sockets.Socket`
|
||||
- `class ZeroTier.Sockets.SocketException`
|
||||
- `ZeroTier.Central` (upcoming)
|
||||
### Added:
|
||||
- IPv6 Support
|
||||
- `Socket.ReceiveTimeout`
|
||||
- `Socket.SendTimeout`
|
||||
- `Socket.ConnectTimeout`
|
||||
- `Socket.SendBufferSize`
|
||||
- `Socket.ReceiveBufferSize`
|
||||
- `Socket.Ttl`
|
||||
- `Socket.LingerState`
|
||||
- `Socket.KeepAlive`
|
||||
- `Socket.NoDelay`
|
||||
- `Socket.Blocking`
|
||||
|
||||
### Added to Socket API
|
||||
- `Socket.ReceiveTimeout`, `Socket.SendTimeout`, etc.
|
||||
|
||||
### Bugs
|
||||
- Fixed memory leak caused by unmanaged resources not being released.
|
||||
### Bugfixes:
|
||||
- Minor C API fixes
|
||||
|
||||
@@ -66,22 +66,44 @@ int zts_connect(int fd, const struct zts_sockaddr *addr, zts_socklen_t addrlen)
|
||||
return lwip_connect(fd, (sockaddr*)addr, addrlen);
|
||||
}
|
||||
|
||||
int zts_connect_easy(int fd, int family, char *ipstr, int port) {
|
||||
int zts_connect_easy(int fd, int family, char *ipstr, int port, int timeout_ms) {
|
||||
if (timeout_ms < 0) {
|
||||
return ZTS_ERR_ARG;
|
||||
}
|
||||
if (timeout_ms == 0) {
|
||||
timeout_ms = 30000; // Default
|
||||
}
|
||||
int div = 4; // Must be > 0, Four connection attempts per second
|
||||
int n_tries = (timeout_ms / 1000) * div;
|
||||
int connect_delay = 1000 / div;
|
||||
int err = ZTS_ERR_SOCKET;
|
||||
|
||||
zts_socklen_t addrlen = 0;
|
||||
struct zts_sockaddr_storage ss;
|
||||
struct zts_sockaddr *sa = NULL;
|
||||
|
||||
if (family == ZTS_AF_INET) {
|
||||
struct zts_sockaddr_in in4;
|
||||
zts_socklen_t addrlen = sizeof(in4);
|
||||
addrlen = sizeof(ss);
|
||||
ipstr2sockaddr(
|
||||
family, ipstr, port, (struct zts_sockaddr *)&in4, &addrlen);
|
||||
struct zts_sockaddr *sa = (struct zts_sockaddr *)&in4;
|
||||
return zts_connect(fd, sa, addrlen);
|
||||
family, ipstr, port, (struct zts_sockaddr *)&ss, &addrlen);
|
||||
sa = (struct zts_sockaddr *)&ss;
|
||||
}
|
||||
if (family == ZTS_AF_INET6) {
|
||||
struct zts_sockaddr_in6 in6;
|
||||
zts_socklen_t addrlen = sizeof(in6);
|
||||
addrlen = sizeof(ss);
|
||||
ipstr2sockaddr(
|
||||
family, ipstr, port, (struct zts_sockaddr *)&in6, &addrlen);
|
||||
struct zts_sockaddr *sa = (struct zts_sockaddr *)&in6;
|
||||
return zts_connect(fd, sa, addrlen);
|
||||
family, ipstr, port, (struct zts_sockaddr *)&ss, &addrlen);
|
||||
sa = (struct zts_sockaddr *)&ss;
|
||||
}
|
||||
if (addrlen > 0 && sa != NULL) {
|
||||
if (zts_get_blocking(fd)) {
|
||||
do {
|
||||
err = zts_connect(fd, sa, addrlen);
|
||||
zts_delay_ms(connect_delay);
|
||||
n_tries--;
|
||||
}
|
||||
while ((err < 0) && (zts_errno != 0) && (n_tries > 0));
|
||||
}
|
||||
return err;
|
||||
}
|
||||
return ZTS_ERR_ARG;
|
||||
}
|
||||
@@ -137,6 +159,34 @@ int zts_accept(int fd, struct zts_sockaddr *addr, zts_socklen_t *addrlen)
|
||||
return lwip_accept(fd, (sockaddr*)addr, (socklen_t*)addrlen);
|
||||
}
|
||||
|
||||
int zts_accept_easy(int fd, char *remoteIpStr, int len, int *port)
|
||||
{
|
||||
if (len != ZTS_INET6_ADDRSTRLEN) {
|
||||
return ZTS_ERR_ARG;
|
||||
}
|
||||
char ipstr[ZTS_INET6_ADDRSTRLEN];
|
||||
memset(ipstr, 0, ZTS_INET6_ADDRSTRLEN);
|
||||
|
||||
zts_sockaddr_storage ss;
|
||||
zts_socklen_t addrlen = sizeof(ss);
|
||||
|
||||
int acc_fd = zts_accept(fd, (zts_sockaddr*)&ss, (zts_socklen_t*)&addrlen);
|
||||
struct zts_sockaddr *sa = (struct zts_sockaddr *)&ss;
|
||||
if (sa->sa_family == ZTS_AF_INET) {
|
||||
struct zts_sockaddr_in *in4 = (struct zts_sockaddr_in*)sa;
|
||||
zts_inet_ntop(ZTS_AF_INET, &(in4->sin_addr),
|
||||
remoteIpStr, ZTS_INET_ADDRSTRLEN);
|
||||
*port = ntohs(in4->sin_port);
|
||||
}
|
||||
if (sa->sa_family == ZTS_AF_INET6) {
|
||||
struct zts_sockaddr_in6 *in6 = (struct zts_sockaddr_in6*)sa;
|
||||
zts_inet_ntop(ZTS_AF_INET6, &(in6->sin6_addr),
|
||||
remoteIpStr, ZTS_INET6_ADDRSTRLEN);
|
||||
*port = ntohs(in6->sin6_port);
|
||||
}
|
||||
return acc_fd;
|
||||
}
|
||||
|
||||
int zts_setsockopt(
|
||||
int fd, int level, int optname, const void *optval,zts_socklen_t optlen)
|
||||
{
|
||||
|
||||
@@ -35,6 +35,8 @@ namespace ZeroTier.Core
|
||||
static ulong _nodeId = 0x0;
|
||||
static bool _isOnline = false;
|
||||
static bool _joinedAtLeastOneNetwork = false;
|
||||
static bool _has_ipv4_routes = false;
|
||||
static bool _has_ipv6_routes = false;
|
||||
static bool _hasBeenFreed = false;
|
||||
string _configFilePath;
|
||||
ushort _servicePort;
|
||||
@@ -94,12 +96,22 @@ namespace ZeroTier.Core
|
||||
newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_ACCESS_DENIED");
|
||||
}
|
||||
if (msg.eventCode == Constants.EVENT_NETWORK_READY_IP4) {
|
||||
_joinedAtLeastOneNetwork = true;
|
||||
zts_network_details unmanagedDetails =
|
||||
(zts_network_details)Marshal.PtrToStructure(msg.network, typeof(zts_network_details));
|
||||
newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_READY_IP4");
|
||||
newEvent.networkDetails = new NetworkDetails();
|
||||
newEvent.networkDetails.networkId = unmanagedDetails.nwid;
|
||||
_joinedAtLeastOneNetwork = true;
|
||||
_has_ipv4_routes = true;
|
||||
}
|
||||
if (msg.eventCode == Constants.EVENT_NETWORK_READY_IP6) {
|
||||
_joinedAtLeastOneNetwork = true;
|
||||
zts_network_details unmanagedDetails =
|
||||
(zts_network_details)Marshal.PtrToStructure(msg.network, typeof(zts_network_details));
|
||||
newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_READY_IP6");
|
||||
newEvent.networkDetails = new NetworkDetails();
|
||||
newEvent.networkDetails.networkId = unmanagedDetails.nwid;
|
||||
_joinedAtLeastOneNetwork = true;
|
||||
_has_ipv6_routes = true;
|
||||
}
|
||||
if (msg.eventCode == Constants.EVENT_NETWORK_DOWN) {
|
||||
newEvent = new ZeroTier.Core.Event(msg.eventCode,"EVENT_NETWORK_DOWN");
|
||||
@@ -261,7 +273,7 @@ namespace ZeroTier.Core
|
||||
_nodeId = 0x0;
|
||||
return zts_restart();
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Requests to join a ZeroTier network. Remember to authorize your node/device.
|
||||
/// </summary>
|
||||
@@ -300,6 +312,17 @@ namespace ZeroTier.Core
|
||||
return _joinedAtLeastOneNetwork;
|
||||
}
|
||||
|
||||
|
||||
public bool HasIPv4Routes
|
||||
{
|
||||
get { return _has_ipv4_routes; }
|
||||
}
|
||||
|
||||
public bool HasIPv6Routes
|
||||
{
|
||||
get { return _has_ipv6_routes; }
|
||||
}
|
||||
|
||||
public ulong NodeId
|
||||
{
|
||||
get {
|
||||
|
||||
@@ -48,6 +48,8 @@ namespace ZeroTier.Sockets
|
||||
bool _isBound;
|
||||
bool _isConnected;
|
||||
|
||||
int _connectTimeout = 30000;
|
||||
|
||||
AddressFamily _socketFamily;
|
||||
SocketType _socketType;
|
||||
ProtocolType _socketProtocol;
|
||||
@@ -140,50 +142,26 @@ namespace ZeroTier.Sockets
|
||||
throw new ArgumentNullException("remoteEndPoint");
|
||||
}
|
||||
int err = Constants.ERR_OK;
|
||||
int addrlen = 0;
|
||||
IntPtr remoteAddrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_sockaddr)));
|
||||
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
zts_sockaddr_in sa = new zts_sockaddr_in();
|
||||
addrlen = Marshal.SizeOf(typeof(zts_sockaddr_in));
|
||||
switch (remoteEndPoint.AddressFamily)
|
||||
{
|
||||
case AddressFamily.InterNetwork:
|
||||
sa.sin_family = (byte)Constants.AF_INET;
|
||||
break;
|
||||
case AddressFamily.InterNetworkV6:
|
||||
sa.sin_family = (byte)Constants.AF_INET6;
|
||||
break;
|
||||
case AddressFamily.Unknown:
|
||||
sa.sin_family = (byte)Constants.AF_UNSPEC;
|
||||
break;
|
||||
}
|
||||
sa.sin_port = (short)IPAddress.HostToNetworkOrder((ushort)remoteEndPoint.Port);
|
||||
sa.sin_addr = remoteEndPoint.Address.GetAddressBytes();
|
||||
sa.sin_len = (byte)addrlen; // lwIP-specific
|
||||
|
||||
Marshal.StructureToPtr(sa, remoteAddrPtr, false);
|
||||
//zts_sockaddr sAddr = (zts_sockaddr)Marshal.PtrToStructure(remoteAddrPtr, typeof(zts_sockaddr));
|
||||
err = zts_connect(_fd, remoteAddrPtr, (byte)addrlen);
|
||||
|
||||
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork) {
|
||||
err = zts_connect_easy(
|
||||
_fd,
|
||||
Constants.AF_INET,
|
||||
remoteEndPoint.Address.ToString(),
|
||||
(ushort)remoteEndPoint.Port,
|
||||
_connectTimeout);
|
||||
}
|
||||
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
/*
|
||||
socketAddress.iSockaddrLength = Marshal.SizeOf(typeof(sockaddr_in6));
|
||||
socketAddress.lpSockAddr = CriticalAllocHandle.FromSize(socketAddress.iSockaddrLength);
|
||||
sockaddr_in6 sa = new sockaddr_in6();
|
||||
sa.sin6_family = (short)AddressFamily.InterNetworkV6;
|
||||
sa.sin6_port = (ushort)endpoint.Port;
|
||||
sa.sin6_addr = endpoint.Address.GetAddressBytes();
|
||||
sa.sin6_scope_id = (uint)endpoint.Address.ScopeId;
|
||||
Marshal.StructureToPtr(sa, (IntPtr)socketAddress.lpSockAddr, false);
|
||||
*/
|
||||
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetworkV6) {
|
||||
Console.WriteLine("going to connect to: " + remoteEndPoint.ToString());
|
||||
err = zts_connect_easy(
|
||||
_fd,
|
||||
Constants.AF_INET6,
|
||||
remoteEndPoint.Address.ToString(),
|
||||
(ushort)remoteEndPoint.Port,
|
||||
_connectTimeout);
|
||||
}
|
||||
if (err < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo);
|
||||
}
|
||||
Marshal.FreeHGlobal(remoteAddrPtr);
|
||||
_remoteEndPoint = remoteEndPoint;
|
||||
_isConnected = true;
|
||||
}
|
||||
@@ -201,48 +179,24 @@ namespace ZeroTier.Sockets
|
||||
throw new ArgumentNullException("localEndPoint");
|
||||
}
|
||||
int err = Constants.ERR_OK;
|
||||
int addrlen = 0;
|
||||
IntPtr localAddrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_sockaddr)));
|
||||
if (localEndPoint.AddressFamily == AddressFamily.InterNetwork)
|
||||
{
|
||||
zts_sockaddr_in sa = new zts_sockaddr_in();
|
||||
addrlen = Marshal.SizeOf(typeof(zts_sockaddr_in));
|
||||
switch (localEndPoint.AddressFamily)
|
||||
{
|
||||
case AddressFamily.InterNetwork:
|
||||
sa.sin_family = (byte)Constants.AF_INET;
|
||||
break;
|
||||
case AddressFamily.InterNetworkV6:
|
||||
sa.sin_family = (byte)Constants.AF_INET6;
|
||||
break;
|
||||
case AddressFamily.Unknown:
|
||||
sa.sin_family = (byte)Constants.AF_UNSPEC;
|
||||
break;
|
||||
}
|
||||
sa.sin_port = (short)IPAddress.HostToNetworkOrder((ushort)localEndPoint.Port);
|
||||
sa.sin_addr = localEndPoint.Address.GetAddressBytes();
|
||||
sa.sin_len = (byte)addrlen; // lwIP-specific
|
||||
|
||||
Marshal.StructureToPtr(sa, localAddrPtr, false);
|
||||
err = zts_bind(_fd, localAddrPtr, (byte)addrlen);
|
||||
if (localEndPoint.AddressFamily == AddressFamily.InterNetwork) {
|
||||
err = zts_bind_easy(
|
||||
_fd,
|
||||
Constants.AF_INET,
|
||||
"0.0.0.0",
|
||||
(ushort)localEndPoint.Port);
|
||||
}
|
||||
if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
|
||||
{
|
||||
/*
|
||||
socketAddress.iSockaddrLength = Marshal.SizeOf(typeof(sockaddr_in6));
|
||||
socketAddress.lpSockAddr = CriticalAllocHandle.FromSize(socketAddress.iSockaddrLength);
|
||||
sockaddr_in6 sa = new sockaddr_in6();
|
||||
sa.sin6_family = (short)AddressFamily.InterNetworkV6;
|
||||
sa.sin6_port = (ushort)endpoint.Port;
|
||||
sa.sin6_addr = endpoint.Address.GetAddressBytes();
|
||||
sa.sin6_scope_id = (uint)endpoint.Address.ScopeId;
|
||||
Marshal.StructureToPtr(sa, (IntPtr)socketAddress.lpSockAddr, false);
|
||||
*/
|
||||
if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6) {
|
||||
// Todo: detect IPAddress.IPv6Any
|
||||
err = zts_bind_easy(
|
||||
_fd,
|
||||
Constants.AF_INET6,
|
||||
"::",
|
||||
(ushort)localEndPoint.Port);
|
||||
}
|
||||
if (err < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException((int)err);
|
||||
}
|
||||
Marshal.FreeHGlobal(localAddrPtr);
|
||||
_localEndPoint = localEndPoint;
|
||||
_isBound = true;
|
||||
}
|
||||
@@ -274,29 +228,22 @@ namespace ZeroTier.Sockets
|
||||
throw new ZeroTier.Sockets.SocketException((int)Constants.ERR_SOCKET);
|
||||
}
|
||||
if (_isListening == false) {
|
||||
throw new InvalidOperationException("Socket is not in a listening state. Call Listen() first");
|
||||
throw new InvalidOperationException(
|
||||
"Socket is not in a listening state. Call Listen() first");
|
||||
}
|
||||
// TODO: Rewrite -- Check for memory leaks
|
||||
// Inform zts_accept of the size of the available address buffer
|
||||
int addrlen = Marshal.SizeOf(typeof(zts_sockaddr_in));
|
||||
IntPtr addrlenPtr = GCHandle.Alloc(addrlen, GCHandleType.Pinned).AddrOfPinnedObject();
|
||||
// Allocate space for address buffer and provide pointer to zts_accept
|
||||
zts_sockaddr_in in4 = new zts_sockaddr_in();
|
||||
IntPtr remoteAddrPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_sockaddr_in)));
|
||||
Marshal.StructureToPtr(in4, remoteAddrPtr, false);
|
||||
|
||||
int err = zts_accept(_fd, remoteAddrPtr, addrlenPtr);
|
||||
if (err < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo);
|
||||
}
|
||||
in4 = (zts_sockaddr_in)Marshal.PtrToStructure(remoteAddrPtr, typeof(zts_sockaddr_in));
|
||||
// Convert sockaddr contents to IPEndPoint
|
||||
IPAddress ipAddress = new IPAddress(in4.sin_addr);
|
||||
IPEndPoint clientEndPoint = new IPEndPoint(ipAddress, IPAddress.NetworkToHostOrder(((ushort)in4.sin_port)));
|
||||
IntPtr lpBuffer = Marshal.AllocHGlobal(ZeroTier.Constants.INET6_ADDRSTRLEN);
|
||||
int port = 0;
|
||||
int accepted_fd = zts_accept_easy(
|
||||
_fd, lpBuffer, ZeroTier.Constants.INET6_ADDRSTRLEN, ref port);
|
||||
// Convert buffer to managed string
|
||||
string str = Marshal.PtrToStringAnsi(lpBuffer);
|
||||
Marshal.FreeHGlobal(lpBuffer);
|
||||
lpBuffer = IntPtr.Zero;
|
||||
IPEndPoint clientEndPoint = new IPEndPoint(IPAddress.Parse(str), port);
|
||||
Console.WriteLine("clientEndPoint = " + clientEndPoint.ToString());
|
||||
// Create new socket by providing file descriptor returned from zts_accept call.
|
||||
Socket clientSocket = new Socket(
|
||||
err, _socketFamily, _socketType, _socketProtocol, _localEndPoint, clientEndPoint);
|
||||
Marshal.FreeHGlobal(remoteAddrPtr);
|
||||
accepted_fd, _socketFamily, _socketType, _socketProtocol, _localEndPoint, clientEndPoint);
|
||||
return clientSocket;
|
||||
}
|
||||
|
||||
@@ -332,28 +279,8 @@ namespace ZeroTier.Sockets
|
||||
|
||||
public bool Blocking
|
||||
{
|
||||
get {
|
||||
return _isBlocking;
|
||||
}
|
||||
set {
|
||||
if (_isClosed) {
|
||||
throw new ObjectDisposedException("Socket has been closed");
|
||||
}
|
||||
int opts = 0;
|
||||
if ((opts = zts_fcntl(_fd, (int)(ZeroTier.Constants.F_GETFL), 0)) < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException(opts, ZeroTier.Core.Node.ErrNo);
|
||||
}
|
||||
if (value) { // Blocking
|
||||
opts = opts & (~(ZeroTier.Constants.O_NONBLOCK));
|
||||
}
|
||||
if (!value) { // Non-Blocking
|
||||
opts = opts | (int)(ZeroTier.Constants.O_NONBLOCK);
|
||||
}
|
||||
if ((opts = zts_fcntl(_fd, ZeroTier.Constants.F_SETFL, (int)opts)) < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException(opts, ZeroTier.Core.Node.ErrNo);
|
||||
}
|
||||
_isBlocking = value;
|
||||
}
|
||||
get { return Convert.ToBoolean(zts_get_blocking(_fd)); }
|
||||
set { zts_set_blocking(_fd, Convert.ToInt32(value)); }
|
||||
}
|
||||
|
||||
public bool Poll(int microSeconds, System.Net.Sockets.SelectMode mode)
|
||||
@@ -384,13 +311,16 @@ namespace ZeroTier.Sockets
|
||||
poll_set = (zts_pollfd)Marshal.PtrToStructure(poll_fd_ptr, typeof(zts_pollfd));
|
||||
if (result != 0) {
|
||||
if (mode == SelectMode.SelectRead) {
|
||||
result = Convert.ToInt32(((byte)poll_set.revents & (byte)ZeroTier.Constants.POLLIN) != 0);
|
||||
result = Convert.ToInt32(((byte)poll_set.revents
|
||||
& (byte)ZeroTier.Constants.POLLIN) != 0);
|
||||
}
|
||||
if (mode == SelectMode.SelectWrite) {
|
||||
result = Convert.ToInt32(((byte)poll_set.revents & (byte)ZeroTier.Constants.POLLOUT) != 0);
|
||||
result = Convert.ToInt32(((byte)poll_set.revents
|
||||
& (byte)ZeroTier.Constants.POLLOUT) != 0);
|
||||
}
|
||||
if (mode == SelectMode.SelectError) {
|
||||
result = Convert.ToInt32(((poll_set.revents & (byte)ZeroTier.Constants.POLLERR) != 0) ||
|
||||
result = Convert.ToInt32(((poll_set.revents
|
||||
& (byte)ZeroTier.Constants.POLLERR) != 0) ||
|
||||
((poll_set.revents & (byte)ZeroTier.Constants.POLLNVAL) != 0));
|
||||
}
|
||||
}
|
||||
@@ -430,66 +360,67 @@ namespace ZeroTier.Sockets
|
||||
return zts_recv(_fd, bufferPtr, (uint)Buffer.ByteLength(buffer), (int)flags);
|
||||
}
|
||||
|
||||
private void _set_timeout(int timeout_ms, int optname)
|
||||
{
|
||||
zts_timeval tv = new zts_timeval();
|
||||
// Convert milliseconds to timeval struct
|
||||
tv.tv_sec = timeout_ms / 1000;
|
||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||
IntPtr tv_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_timeval)));
|
||||
Marshal.StructureToPtr(tv, tv_ptr, false);
|
||||
ushort option_size = (ushort)Marshal.SizeOf(typeof(zts_sockaddr_in));
|
||||
int err = 0;
|
||||
if ((err = zts_setsockopt(_fd, ZeroTier.Constants.SOL_SOCKET,
|
||||
ZeroTier.Constants.SO_RCVTIMEO, tv_ptr, option_size)) < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo);
|
||||
}
|
||||
Marshal.FreeHGlobal(tv_ptr);
|
||||
}
|
||||
|
||||
private int _get_timeout(int optname)
|
||||
{
|
||||
zts_timeval tv = new zts_timeval();
|
||||
IntPtr tv_ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(zts_timeval)));
|
||||
Marshal.StructureToPtr(tv, tv_ptr, false);
|
||||
ushort optlen = (ushort)Marshal.SizeOf(typeof(zts_timeval));
|
||||
GCHandle optlen_gc_handle = GCHandle.Alloc(optlen, GCHandleType.Pinned);
|
||||
IntPtr optlen_ptr = optlen_gc_handle.AddrOfPinnedObject();
|
||||
int err = 0;
|
||||
if ((err = zts_getsockopt(_fd, ZeroTier.Constants.SOL_SOCKET,
|
||||
ZeroTier.Constants.SO_RCVTIMEO, tv_ptr, optlen_ptr)) < 0) {
|
||||
throw new ZeroTier.Sockets.SocketException(err, ZeroTier.Core.Node.ErrNo);
|
||||
}
|
||||
tv = (zts_timeval)Marshal.PtrToStructure(tv_ptr, typeof(zts_timeval));
|
||||
optlen_gc_handle.Free();
|
||||
Marshal.FreeHGlobal(tv_ptr);
|
||||
// Convert timeval struct to milliseconds
|
||||
return (int)((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
|
||||
}
|
||||
|
||||
public int ReceiveTimeout
|
||||
{
|
||||
get { return _get_timeout(ZeroTier.Constants.SO_RCVTIMEO); }
|
||||
set { _set_timeout(value, ZeroTier.Constants.SO_RCVTIMEO); }
|
||||
get { return zts_get_recv_timeout(_fd); }
|
||||
// TODO: microseconds
|
||||
set { zts_set_recv_timeout(_fd, value, 0); }
|
||||
}
|
||||
|
||||
public int SendTimeout
|
||||
{
|
||||
get { return _get_timeout(ZeroTier.Constants.SO_SNDTIMEO); }
|
||||
set { _set_timeout(value, ZeroTier.Constants.SO_SNDTIMEO); }
|
||||
get { return zts_get_send_timeout(_fd); }
|
||||
// TODO: microseconds
|
||||
set { zts_set_send_timeout(_fd, value, 0); }
|
||||
}
|
||||
|
||||
/* TODO
|
||||
public int ReceiveBufferSize { get; set; }
|
||||
public int ConnectTimeout
|
||||
{
|
||||
get { return _connectTimeout; }
|
||||
set { _connectTimeout = value;}
|
||||
}
|
||||
|
||||
public int SendBufferSize { get; set; }
|
||||
public int ReceiveBufferSize
|
||||
{
|
||||
get { return zts_get_recv_buf_size(_fd); }
|
||||
set { zts_set_recv_buf_size(_fd, value); }
|
||||
}
|
||||
|
||||
public short Ttl { get; set; }
|
||||
public int SendBufferSize
|
||||
{
|
||||
get { return zts_get_send_buf_size(_fd); }
|
||||
set { zts_set_send_buf_size(_fd, value); }
|
||||
}
|
||||
|
||||
public LingerOption LingerState { get; set; }
|
||||
public short Ttl
|
||||
{
|
||||
get { return Convert.ToInt16(zts_get_ttl(_fd)); }
|
||||
set { zts_set_ttl(_fd, value); }
|
||||
}
|
||||
|
||||
public bool NoDelay { get; set; }
|
||||
*/
|
||||
public LingerOption LingerState
|
||||
{
|
||||
get {
|
||||
LingerOption lo = new LingerOption(
|
||||
Convert.ToBoolean(zts_get_linger_enabled(_fd)), zts_get_linger_value(_fd));
|
||||
return lo;
|
||||
}
|
||||
set {
|
||||
zts_set_linger(_fd, Convert.ToInt32(value.Enabled), value.LingerTime);
|
||||
}
|
||||
}
|
||||
|
||||
public bool NoDelay
|
||||
{
|
||||
get { return Convert.ToBoolean(zts_get_no_delay(_fd)); }
|
||||
set { zts_set_no_delay(_fd, Convert.ToInt32(value)); }
|
||||
}
|
||||
|
||||
public bool KeepAlive
|
||||
{
|
||||
get { return Convert.ToBoolean(zts_get_keepalive(_fd)); }
|
||||
set { zts_set_keepalive(_fd, Convert.ToInt32(value)); }
|
||||
}
|
||||
|
||||
public bool Connected { get { return _isConnected; } }
|
||||
|
||||
@@ -526,15 +457,24 @@ namespace ZeroTier.Sockets
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_connect")]
|
||||
static extern int zts_connect(int arg1, IntPtr arg2, ushort arg3);
|
||||
|
||||
[DllImport("libzt", CharSet=CharSet.Ansi, EntryPoint="CSharp_zts_connect_easy")]
|
||||
static extern int zts_connect_easy(int arg1, int arg2, string arg3, ushort arg4, int arg5);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_bind")]
|
||||
static extern int zts_bind(int arg1, IntPtr arg2, ushort arg3);
|
||||
|
||||
[DllImport("libzt", CharSet=CharSet.Ansi, EntryPoint="CSharp_zts_bind_easy")]
|
||||
static extern int zts_bind_easy(int arg1, int arg2, string arg3, ushort arg4);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_listen")]
|
||||
static extern int zts_listen(int arg1, int arg2);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_accept")]
|
||||
static extern int zts_accept(int arg1, IntPtr arg2, IntPtr arg3);
|
||||
|
||||
[DllImport("libzt", CharSet=CharSet.Ansi, EntryPoint="CSharp_zts_accept_easy")]
|
||||
static extern int zts_accept_easy(int arg1, IntPtr remoteAddrStr, int arg2, ref int arg3);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_setsockopt")]
|
||||
static extern int zts_setsockopt(int arg1, int arg2, int arg3, IntPtr arg4, ushort arg5);
|
||||
|
||||
@@ -592,6 +532,69 @@ namespace ZeroTier.Sockets
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_shutdown")]
|
||||
static extern int zts_shutdown(int arg1, int arg2);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_no_delay")]
|
||||
static extern int zts_set_no_delay(int fd, int enabled);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_no_delay")]
|
||||
static extern int zts_get_no_delay(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_linger")]
|
||||
static extern int zts_set_linger(int fd, int enabled, int value);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_linger_enabled")]
|
||||
static extern int zts_get_linger_enabled(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_linger_value")]
|
||||
static extern int zts_get_linger_value(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_reuse_addr")]
|
||||
static extern int zts_set_reuse_addr(int fd, int enabled);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_reuse_addr")]
|
||||
static extern int zts_get_reuse_addr(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_recv_timeout")]
|
||||
static extern int zts_set_recv_timeout(int fd, int seconds, int microseconds);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_recv_timeout")]
|
||||
static extern int zts_get_recv_timeout(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_send_timeout")]
|
||||
static extern int zts_set_send_timeout(int fd, int seconds, int microseconds);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_send_timeout")]
|
||||
static extern int zts_get_send_timeout(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_send_buf_size")]
|
||||
static extern int zts_set_send_buf_size(int fd, int size);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_send_buf_size")]
|
||||
static extern int zts_get_send_buf_size(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_recv_buf_size")]
|
||||
static extern int zts_set_recv_buf_size(int fd, int size);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_recv_buf_size")]
|
||||
static extern int zts_get_recv_buf_size(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_ttl")]
|
||||
static extern int zts_set_ttl(int fd, int ttl);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_ttl")]
|
||||
static extern int zts_get_ttl(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_blocking")]
|
||||
static extern int zts_set_blocking(int fd, int enabled);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_blocking")]
|
||||
static extern int zts_get_blocking(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_set_keepalive")]
|
||||
static extern int zts_set_keepalive(int fd, int enabled);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_get_keepalive")]
|
||||
static extern int zts_get_keepalive(int fd);
|
||||
|
||||
[DllImport("libzt", EntryPoint="CSharp_zts_add_dns_nameserver")]
|
||||
static extern int zts_add_dns_nameserver(IntPtr arg1);
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -426,16 +426,6 @@ void test_service()
|
||||
assert(res == ZTS_ERR_OK);
|
||||
assert(res == 0);
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
// Test *_easy API //
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
//zts_connect_easy(0, ZTS_AF_INET, "192.168.7.9", 7878);
|
||||
//zts_connect_easy(0, ZTS_AF_INET6, "FCC5:205E:4FF5:5311:DFF0::1", 7878);
|
||||
//res = zts_bind_easy(0, ZTS_AF_INET6, "::", 8080);
|
||||
//fprintf(stderr, "res=%d, zts_errno=%d\n", res, zts_errno);
|
||||
//zts_delay_ms(60000);
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
// Test DNS client functionality //
|
||||
//----------------------------------------------------------------------------//
|
||||
@@ -486,7 +476,8 @@ void test_service()
|
||||
// Server //
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
#define MAX_CONNECT_TIME 60
|
||||
#define MAX_CONNECT_TIME 60 // outer re-attempt loop
|
||||
#define CONNECT_TIMEOUT 30 // zts_connect_easy, ms
|
||||
#define BUFLEN 128
|
||||
char *msg = "welcome to the machine";
|
||||
|
||||
@@ -549,6 +540,13 @@ void start_server_app(uint16_t port4, uint16_t port6)
|
||||
zts_close(acc4);
|
||||
assert(err == ZTS_ERR_OK && zts_errno == 0);
|
||||
|
||||
assert(bytes_sent == bytes_read);
|
||||
if (bytes_sent == bytes_read) {
|
||||
fprintf(stderr, "server4: Test OK\n");
|
||||
} else {
|
||||
fprintf(stderr, "server4: Test FAIL\n");
|
||||
}
|
||||
|
||||
//
|
||||
// IPv6 test
|
||||
//
|
||||
@@ -600,6 +598,13 @@ void start_server_app(uint16_t port4, uint16_t port6)
|
||||
assert(err == ZTS_ERR_OK && zts_errno == 0);
|
||||
int s = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0);
|
||||
assert(("s != ZTS_ERR_SERVICE, not shut down", s == ZTS_ERR_SERVICE));
|
||||
|
||||
assert(bytes_sent == bytes_read);
|
||||
if (bytes_sent == bytes_read) {
|
||||
fprintf(stderr, "server6: Test OK\n");
|
||||
} else {
|
||||
fprintf(stderr, "server6: Test FAIL\n");
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
@@ -633,7 +638,7 @@ void start_client_app(char *ip4, uint16_t port4, char *ip6, uint16_t port6)
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
do {
|
||||
fprintf(stderr, "client4: connecting to: %s:%d\n", ip4, port4);
|
||||
err = zts_connect_easy(s4, ZTS_AF_INET, ip4, port4);
|
||||
err = zts_connect_easy(s4, ZTS_AF_INET, ip4, port4, CONNECT_TIMEOUT);
|
||||
zts_delay_ms(500);
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
time_diff = (now.tv_sec - start.tv_sec);
|
||||
@@ -657,6 +662,13 @@ void start_client_app(char *ip4, uint16_t port4, char *ip6, uint16_t port6)
|
||||
zts_close(s4);
|
||||
assert(err == ZTS_ERR_OK && zts_errno == 0);
|
||||
|
||||
assert(bytes_sent == bytes_read);
|
||||
if (bytes_sent == bytes_read) {
|
||||
fprintf(stderr, "client4: Test OK\n");
|
||||
} else {
|
||||
fprintf(stderr, "client4: Test FAIL\n");
|
||||
}
|
||||
|
||||
//
|
||||
// IPv6 test
|
||||
//
|
||||
@@ -671,7 +683,7 @@ void start_client_app(char *ip4, uint16_t port4, char *ip6, uint16_t port6)
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
do {
|
||||
fprintf(stderr, "client6: connecting to: %s:%d\n", ip6, port6);
|
||||
err = zts_connect_easy(s6, ZTS_AF_INET6, ip6, port6);
|
||||
err = zts_connect_easy(s6, ZTS_AF_INET6, ip6, port6, CONNECT_TIMEOUT);
|
||||
zts_delay_ms(500);
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
time_diff = (now.tv_sec - start.tv_sec);
|
||||
@@ -699,6 +711,13 @@ void start_client_app(char *ip4, uint16_t port4, char *ip6, uint16_t port6)
|
||||
assert(err == ZTS_ERR_OK && zts_errno == 0);
|
||||
int s = zts_socket(ZTS_AF_INET, ZTS_SOCK_STREAM, 0);
|
||||
assert(("s != ZTS_ERR_SERVICE, not shut down", s == ZTS_ERR_SERVICE));
|
||||
|
||||
assert(bytes_sent == bytes_read);
|
||||
if (bytes_sent == bytes_read) {
|
||||
fprintf(stderr, "client6: Test OK\n");
|
||||
} else {
|
||||
fprintf(stderr, "client6: Test FAIL\n");
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------//
|
||||
|
||||
Reference in New Issue
Block a user