// TCP Client test program (IPV4) #include #include #include #include #include #include "sdk.h" int atoi(const char *str); int close(int filedes); #define MSG_SZ 128 int main(int argc , char *argv[]) { if(argc < 3) { printf("usage: client \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]); int sock, port = atoi(argv[2]); struct sockaddr_in server; char server_reply[MSG_SZ]; sock = zts_socket(AF_INET , SOCK_STREAM , 0); if (sock == -1) { printf("could not create socket"); } server.sin_addr.s_addr = inet_addr(argv[1]); server.sin_family = AF_INET; server.sin_port = htons( port ); printf("connecting...\n"); if (zts_connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { perror("connect failed. Error"); return 1; } printf("connected\n"); char *msg = "welcome to the machine!"; while(1) { // TX if(send(sock, msg, strlen(msg), 0) < 0) { printf("send failed"); return 1; } else { printf("TX: %s\n", msg); printf("len = %ld\n", strlen(msg)); int bytes_read = read(sock, server_reply, MSG_SZ); if(bytes_read < 0) printf("\tRX: Nothing\n"); else printf("\tRX = (%d bytes): %s\n", bytes_read, server_reply); } } close(sock); return 0; }