static void wd_tracker_register(wd_tracker_t *tracker) { wi_enumerator_t *enumerator; wi_address_t *address; wi_array_t *arguments; wi_string_t *ip, *string; uint32_t message; wi_boolean_t fatal = false; if(!wi_lock_trylock(tracker->register_lock)) return; enumerator = wi_array_data_enumerator(tracker->addresses); while((address = wi_enumerator_next_data(enumerator))) { tracker->active = false; tracker->address = NULL; ip = wi_address_string(address); wi_log_info(WI_STR("Trying %@ for tracker %@..."), ip, tracker->host); tracker->socket = wi_autorelease(wi_socket_init_with_address(wi_socket_alloc(), address, WI_SOCKET_TCP)); if(!tracker->socket) { wi_log_err(WI_STR("Could not create socket for tracker %@: %m"), tracker->host); continue; } if(!wi_socket_connect(tracker->socket, 30.0)) { wi_log_err(WI_STR("Could not connect to tracker %@: %m"), tracker->host); continue; } if(!wi_socket_connect_tls(tracker->socket, tracker->tls, 30.0)) { wi_log_err(WI_STR("Could not connect to tracker %@: %m"), tracker->host); continue; } if(!wd_tracker_write(tracker, WI_STR("HELLO"))) continue; if(!wd_tracker_read(tracker, &message, &arguments)) continue; if(message != 200) { string = wi_array_components_joined_by_string(arguments, WI_STR(" ")); wi_log_err(WI_STR("Could not register with tracker %@: Unexpected reply \"%u %@\""), tracker->host, message, string); fatal = true; continue; } if(!wd_tracker_write(tracker, WI_STR("CLIENT %#@"), wd_server_version_string)) continue; if(!wd_tracker_write(tracker, WI_STR("REGISTER %#@%c%#@%c%#@%c%u%c%#@"), tracker->category, WD_FIELD_SEPARATOR, wd_settings.url, WD_FIELD_SEPARATOR, wd_settings.name, WD_FIELD_SEPARATOR, wd_settings.bandwidth, WD_FIELD_SEPARATOR, wd_settings.description)) continue; if(!wd_tracker_read(tracker, &message, &arguments)) continue; if(message != 700 || wi_array_count(arguments) < 1) { string = wi_array_components_joined_by_string(arguments, WI_STR(" ")); wi_log_err(WI_STR("Could not register with tracker %@: Unexpected reply \"%u %@\""), tracker->host, message, string); break; } wi_release(tracker->key); tracker->key = wi_retain(WI_ARRAY(arguments, 0)); tracker->public_key = wi_retain(wi_socket_ssl_public_key(tracker->socket)); if(!tracker->public_key) { wi_log_err(WI_STR("Could not get public key from the tracker %@: %m"), tracker->host); break; } wi_log_info(WI_STR("Registered with the tracker %@"), tracker->host); tracker->active = true; tracker->address = address; break; } wi_lock_unlock(tracker->register_lock); }
static void _wi_log_vlog(int priority, wi_string_t *fmt, va_list ap) { wi_string_t *string; FILE *fp = NULL; const char *cstring, *name, *path; char date[_WI_LOG_DATE_SIZE]; string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); cstring = wi_string_cstring(string); if(wi_lock_trylock(_wi_log_lock)) { name = wi_string_cstring(wi_process_name(wi_process())); _wi_log_date(date); if(wi_log_stdout || wi_log_stderr) { fp = wi_log_stdout ? stdout : stderr; fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); } else if(wi_log_startup && priority < LOG_INFO) { fp = stderr; fprintf(fp, "%s: %s\n", name, cstring); } else if(wi_log_tool) { fp = (priority < LOG_INFO) ? stderr : stdout; fprintf(fp, "%s: %s\n", name, cstring); } else if(wi_log_plain) { fp = (priority < LOG_INFO) ? stderr : stdout; fprintf(fp, "%s\n", cstring); } if(fp) fflush(fp); if(wi_log_syslog) syslog(priority, "%s", cstring); if(wi_log_file && wi_log_path) { path = wi_string_cstring(wi_full_path(wi_log_path)); fp = fopen(path, "a"); if(fp) { fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); fclose(fp); if(_wi_log_lines > 0 && wi_log_limit > 0) { if(_wi_log_lines % (int) ((float) wi_log_limit / 10.0f) == 0) { _wi_log_truncate(path); _wi_log_lines = wi_log_limit; } } _wi_log_lines++; } else { fprintf(stderr, "%s: %s: %s\n", name, path, strerror(errno)); } } if(wi_log_callback) (*wi_log_callback)(string); if(wi_log_startup && priority == LOG_ERR) exit(1); wi_lock_unlock(_wi_log_lock); } wi_release(string); }