int main(int argc, char *argv[]) { /* Initialize networking - Bind to ip 0.0.0.0:PORT */ IP ip; ip.uint32 = 0; DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); manage_keys(dht); printf("Public key: "); uint32_t i; FILE *file; file = fopen("PUBLIC_ID.txt", "w"); for (i = 0; i < 32; i++) { if (dht->c->self_public_key[i] < 16) printf("0"); printf("%hhX", dht->c->self_public_key[i]); fprintf(file, "%hhX", dht->c->self_public_key[i]); } fclose(file); printf("\n"); printf("Port: %u\n", PORT); perror("Initialization."); if (argc > 3) { printf("Trying to bootstrap into the network...\n"); IP_Port bootstrap_info; bootstrap_info.ip.uint32 = inet_addr(argv[1]); bootstrap_info.port = htons(atoi(argv[2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); DHT_bootstrap(dht, bootstrap_info, bootstrap_key); free(bootstrap_key); } int is_waiting_for_dht_connection = 1; while (1) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } do_DHT(dht); networking_poll(dht->c->lossless_udp->net); c_sleep(1); } return 0; }
Onions *new_onions(uint16_t port) { IP ip; ip_init(&ip, 1); ip.ip6.uint8[15] = 1; Onions *on = malloc(sizeof(Onions)); DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); on->onion = new_onion(dht); on->onion_a = new_onion_announce(dht); if (on->onion && on->onion_a) return on; return NULL; }
static Onions *new_onions(uint16_t port) { IP ip; ip_init(&ip, 1); ip.ip6.uint8[15] = 1; Onions *on = (Onions *)malloc(sizeof(Onions)); DHT *dht = new_DHT(NULL, new_networking(NULL, ip, port)); on->onion = new_onion(dht); on->onion_a = new_onion_announce(dht); TCP_Proxy_Info inf = {{{0}}}; on->onion_c = new_onion_client(new_net_crypto(NULL, dht, &inf)); if (on->onion && on->onion_a && on->onion_c) { return on; } return NULL; }
/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return NULL; IP ip; ip.i = 0; m->net = new_networking(ip, PORT); if (m->net == NULL) { free(m); return NULL; } m->net_crypto = new_net_crypto(m->net); if (m->net_crypto == NULL) { kill_networking(m->net); free(m); return NULL; } m->dht = new_DHT(m->net_crypto); if (m->dht == NULL) { kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m); return NULL; } new_keys(m->net_crypto); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); friendreq_init(&(m->fr), m->net_crypto); LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); return m; }
int main(int argc, char *argv[]) { openlog(DAEMON_NAME, LOG_NOWAIT | LOG_PID, LOG_DAEMON); if (argc < 2) { syslog(LOG_ERR, "Please specify a path to a configuration file as the first argument. Exiting.\n"); return 1; } char *cfg_file_path = argv[1]; char *pid_file_path, *keys_file_path; int port; int enable_ipv6; int enable_lan_discovery; if (get_general_config(cfg_file_path, &pid_file_path, &keys_file_path, &port, &enable_ipv6, &enable_lan_discovery)) { syslog(LOG_DEBUG, "General config read successfully\n"); } else { syslog(LOG_ERR, "Couldn't read config file: %s. Exiting.\n", cfg_file_path); return 1; } // not (1 <= port <= 65535) if (port < 1 || port > 65535) { syslog(LOG_ERR, "Invalid port: %d, must be 1 <= port <= 65535. Exiting.\n", port); return 1; } // Check if the PID file exists if (fopen(pid_file_path, "r")) { syslog(LOG_ERR, "Another instance of the daemon is already running, PID file %s exists. Exiting.\n", pid_file_path); return 1; } IP ip; ip_init(&ip, enable_ipv6); DHT *dht = new_DHT(new_net_crypto(new_networking(ip, port))); if (dht == NULL) { syslog(LOG_ERR, "Couldn't initialize Tox DHT instance. Exiting.\n"); return 1; } Onion *onion = new_onion(dht); Onion_Announce *onion_a = new_onion_announce(dht); if (!(onion && onion_a)) { syslog(LOG_ERR, "Couldn't initialize Tox Onion. Exiting.\n"); return 1; } if (enable_lan_discovery) { LANdiscovery_init(dht); } if (manage_keys(dht, keys_file_path)) { syslog(LOG_DEBUG, "Keys are managed successfully\n"); } else { syslog(LOG_ERR, "Couldn't read/write: %s. Exiting.\n", keys_file_path); return 1; } if (bootstrap_from_config(cfg_file_path, dht, enable_ipv6)) { syslog(LOG_DEBUG, "List of bootstrap servers read successfully\n"); } else { syslog(LOG_ERR, "Couldn't read list of bootstrap servers in %s. Exiting.\n", cfg_file_path); return 1; } print_public_key(dht->c->self_public_key); // Write the PID file FILE *pidf = fopen(pid_file_path, "w"); if (pidf == NULL) { syslog(LOG_ERR, "Can't open the PID file for writing: %s. Exiting.\n", pid_file_path); return 1; } free(pid_file_path); free(keys_file_path); // Fork off from the parent process pid_t pid = fork(); if (pid < 0) { syslog(LOG_ERR, "Forking failed. Exiting.\n"); return 1; } if (pid > 0) { syslog(LOG_DEBUG, "Forked successfully: PID: %d.\n", pid); return 0; } // Change the file mode mask umask(0); fprintf(pidf, "%d\n", pid); fclose(pidf); // Create a new SID for the child process if (setsid() < 0) { syslog(LOG_ERR, "SID creation failure. Exiting.\n"); return 1; } // Change the current working directory if ((chdir("/")) < 0) { syslog(LOG_ERR, "Couldn't change working directory to '/'. Exiting.\n"); return 1; } // Go quiet close(STDOUT_FILENO); close(STDIN_FILENO); close(STDERR_FILENO); uint64_t last_LANdiscovery = 0; uint16_t htons_port = htons(port); int waiting_for_dht_connection = 1; while (1) { do_DHT(dht); if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) { send_LANdiscovery(htons_port, dht); last_LANdiscovery = unix_time(); } networking_poll(dht->net); if (waiting_for_dht_connection && DHT_isconnected(dht)) { syslog(LOG_DEBUG, "Connected to other bootstrap server successfully.\n"); waiting_for_dht_connection = 0; } sleep; } return 1; }
int main(int argc, char *argv[]) { pid_t pid, sid; /* Process- and Session-ID */ struct server_conf_s server_conf; FILE *pidf; /* The PID file */ if (argc < 2) { printf("Please specify a configuration file.\n"); exit(EXIT_FAILURE); } server_conf = configure_server(argv[1]); /* initialize networking bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; DHT *dht = new_DHT(new_net_crypto(new_networking(ip, server_conf.port))); /* Read the config file */ printf("PID file: %s\n", server_conf.pid_file); printf("Key file: %s\n", server_conf.keys_file); if (server_conf.err == -1) printf("Config file not read.\n"); if (server_conf.err == -2) printf("No valid servers in list.\n"); /* Open PID file for writing - if an error happens, it will be caught down the line */ pidf = fopen(server_conf.pid_file, "w"); /* Manage the keys */ /* for now, just ignore any errors after this call. */ int tmperr = errno; manage_keys(dht, server_conf.keys_file); errno = tmperr; /* Public key */ int i; printf("\nPublic Key: "); for (i = 0; i < 32; ++i) { uint8_t ln, hn; ln = 0x0F & dht->c->self_public_key[i]; hn = 0xF0 & dht->c->self_public_key[i]; hn = hn >> 4; printf("%X%X", hn, ln); } printf("\n"); /* Bootstrap the DHT This one throws odd errors, too. Ignore. I assume they come from somewhere in the core. */ tmperr = errno; connect_to_servers(dht, server_conf.info); errno = tmperr; if (!DHT_isconnected(dht)) { puts("Could not establish DHT connection. Check server settings.\n"); exit(EXIT_FAILURE); } else { printf("Connected to DHT successfully.\n"); } /* If there's been an error, exit before forking off */ if (errno != 0) { perror("Error"); printf("Error(s) occured during start-up. Exiting.\n"); exit(EXIT_FAILURE); } /* Things that make the daemon work come past here. There should be nothing here but the daemon code and the main loop. */ /* Fork off from the parent process */ pid = fork(); if (pid < 0) { printf("Forking failed.\n"); exit(EXIT_FAILURE); } /* If we got a good PID, then we can exit the parent process. */ if (pid > 0) { printf("Forked successfully: %d\n", pid); /* Write the PID file */ fprintf(pidf, "%d\n", pid); fclose(pidf); /* Exit parent */ exit(EXIT_SUCCESS); } /* Change the file mode mask */ umask(0); /* Create a new SID for the child process */ sid = setsid(); if (sid < 0) { printf("SID creation failure.\n"); exit(EXIT_FAILURE); } /* Change the current working directory */ if ((chdir("/")) < 0) { exit(EXIT_FAILURE); } /* Go quiet */ close(STDOUT_FILENO); close(STDIN_FILENO); close(STDERR_FILENO); while (1) { do_DHT(dht); networking_poll(dht->c->lossless_udp->net); usleep(10000); } shutdown_networking(); exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { if (argc == 2 && !strncasecmp(argv[1], "-h", 3)) { printf("Usage (connected) : %s [--ipv4|--ipv6] IP PORT KEY\n", argv[0]); printf("Usage (unconnected): %s [--ipv4|--ipv6]\n", argv[0]); exit(0); } /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) exit(1); /* Initialize networking - Bind to ip 0.0.0.0 / [::] : PORT */ IP ip; ip_init(&ip, ipv6enabled); DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); perror("Initialization"); manage_keys(dht); printf("Public key: "); uint32_t i; FILE *file; file = fopen("PUBLIC_ID.txt", "w"); for (i = 0; i < 32; i++) { if (dht->c->self_public_key[i] < 16) printf("0"); printf("%hhX", dht->c->self_public_key[i]); fprintf(file, "%hhX", dht->c->self_public_key[i]); } fclose(file); printf("\n"); printf("Port: %u\n", ntohs(dht->c->lossless_udp->net->port)); if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } } int is_waiting_for_dht_connection = 1; uint64_t last_LANdiscovery = 0; LANdiscovery_init(dht); while (1) { if (is_waiting_for_dht_connection && DHT_isconnected(dht)) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } do_DHT(dht); if (last_LANdiscovery + (is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL) < unix_time()) { send_LANdiscovery(htons(PORT), dht->c); last_LANdiscovery = unix_time(); } networking_poll(dht->c->lossless_udp->net); c_sleep(1); } return 0; }