This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
zhangyang-libzt/tests/zts/zts.udpclient6.c
2017-03-15 13:01:22 -07:00

65 lines
1.7 KiB
C
Executable File

// UDP Client test program (IPV6)
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <string.h>
#include <cstdlib>
#include <fcntl.h>
#include "sdk.h"
#define MAXBUF 65536
#define TESTBUF 1024
int main(int argc, char* argv[])
{
int status, sock, portno, n;
struct addrinfo sainfo, *psinfo;
struct hostent *server;
char buffer[MAXBUF];
struct sockaddr_in6 serv_addr;
if(argc < 3) {
printf("usage: client <addr> <port> <netpath> <nwid>\n");
return 1;
}
/* Starts ZeroTier core service in separate thread, loads user-space TCP/IP stack
and sets up a private AF_UNIX socket between ZeroTier library and your app. Any
subsequent zts_* socket API calls (shown below) are mediated over this hidden AF_UNIX
socket and are spoofed to appear as AF_INET sockets. The implementation of this API
is in src/sockets.c */
zts_init_rpc(argv[3],argv[4]);
sock = zts_socket(AF_INET6, SOCK_DGRAM,0);
portno = atoi(argv[2]);
server = gethostbyname2(argv[1],AF_INET6);
memset((char *) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_family = AF_INET6;
memmove((char *) &serv_addr.sin6_addr.s6_addr, (char *) server->h_addr, server->h_length);
serv_addr.sin6_port = htons(portno);
sprintf(buffer,"Welcome to the machine");
//memset(buffer, 1, TESTBUF);
fcntl(sock, F_SETFL, O_NONBLOCK);
while(1)
{
//usleep(50000);
status = zts_sendto(sock, buffer, strlen(buffer), 0, (const struct sockaddr *)&serv_addr, sizeof(serv_addr));
if(status > 0)
printf("sendto() : %s \t%d\n", buffer, status);
}
close(sock);
zts_stop(); /* Shut down ZT */
return 0;
}