github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/ClientLibrary/example/main.c (about) 1 #include "libpsiphontunnel.h" 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 6 char *read_file(char *filename) { 7 char *buffer = NULL; 8 size_t size = 0; 9 10 FILE *fp = fopen(filename, "r"); 11 12 if (!fp) { 13 return NULL; 14 } 15 16 fseek(fp, 0, SEEK_END); 17 size = ftell(fp); 18 19 rewind(fp); 20 buffer = malloc((size + 1) * sizeof(*buffer)); 21 22 fread(buffer, size, 1, fp); 23 buffer[size] = '\0'; 24 25 return buffer; 26 } 27 28 int main(int argc, char *argv[]) { 29 30 // load config 31 char * const default_config = "psiphon_config"; 32 33 char * config = argv[1]; 34 35 if (!config) { 36 config = default_config; 37 printf("Using default config file: %s\n", default_config); 38 } 39 40 char *psiphon_config = read_file(config); 41 if (!psiphon_config) { 42 printf("Could not find config file: %s\n", config); 43 return 1; 44 } 45 46 // set server list 47 char *server_list = ""; 48 49 // set client platform 50 char * const os = "OSName"; // "Android", "iOS", "Windows", etc. 51 char * const os_version = "OSVersion"; // "4.0.4", "10.3", "10.0.10240", etc. 52 char * const bundle_identifier = "com.example.exampleClientLibraryApp"; 53 char * client_platform = (char *)malloc(sizeof(char) * (strlen(os) + strlen(os_version) + strlen(bundle_identifier) + 4)); // 4 for 3 underscores and null terminating character 54 55 int n = sprintf(client_platform, "%s_%s_%s", os, os_version, bundle_identifier); 56 57 // set network ID 58 char * const network_id = "TEST"; 59 60 // set timeout 61 int32_t timeout = 60; 62 63 struct Parameters params; 64 params.sizeofStruct = sizeof(struct Parameters); 65 params.dataRootDirectory = "."; 66 params.clientPlatform = client_platform; 67 params.networkID = network_id; 68 params.establishTunnelTimeoutSeconds = &timeout; 69 70 // connect 5 times 71 for (int i = 0; i < 5; i++) { 72 // start will return once Psiphon connects or does not connect for timeout seconds 73 char *result = PsiphonTunnelStart(psiphon_config, server_list, ¶ms); 74 75 // print results 76 printf("Result: %s\n", result); 77 78 // The underlying memory of `result` is managed by PsiphonTunnel and is freed in Stop 79 PsiphonTunnelStop(); 80 } 81 82 free(client_platform); 83 client_platform = NULL; 84 free(psiphon_config); 85 psiphon_config = NULL; 86 } 87