/* * Receive command arguments via control socket. */ int recv_cmd(int *argc, char ***argv) { int i; static char *args[256]; for (i = 0; i < 255; ++i) { unsigned char length; if (recv(control, &length, 1, 0) != 1) { android_log(PRG_ERR, "Cannot get argument length"); return -1; } if (length == 0xFF) { break; } else { int offset = 0; args[i] = malloc(length + 1); while (offset < length) { int n = recv(control, &args[i][offset], length - offset, 0); if (n > 0) { offset += n; } else { android_log(PRG_ERR, "Cannot get argument value"); return -1; } } args[i][length] = 0; android_log(PRG_DEBUG, "Argument %d: %s", i, args[i]); } } android_log(PRG_DEBUG, "Received %d argument(s)", i); *argc = i; *argv = args; return 0; }
int send_req(const char *req) { int len = strlen(req); int pos; for (pos = 0; pos < len;) { int n = len - pos; if (n > 254) n = n % 254; android_log(PRG_DEBUG, "sending %d bytes via control socket", n); unsigned char x = (unsigned char)n; int rv = send(control, &x, 1, 0); if (rv <= 0) return -1; rv = send(control, req + pos, n, 0); if (rv <= 0) return -1; android_log(PRG_DEBUG, "sent %s (%d)", req + pos, rv); pos += rv; } return 0; }
int send_ack(int code) { unsigned char x = (unsigned char)code; android_log(PRG_DEBUG, "sending ack %u", x); if (send(control, &x, 1, 0) != 1) { android_log(PRG_ERR, "send_ack() failed"); return -1; } return 0; }
char * reverse (const char *host) { struct hostent hostinfo, *hostinfoptr; struct in_addr addr; int error; char *outhost; char buffer[BUFSIZE]; if (!host) { android_log (ANDROID_LOG_VERBOSE, "ERROR: reverse() called with NULL host"); return NULL; } xa_debug (1, "reverse() reverse resolving %s", host); if (inet_aton (host, &addr)) { hostinfoptr = ice_gethostbyaddr((char *) &addr, sizeof (struct in_addr), &hostinfo, buffer, BUFSIZE, &error); if (hostinfoptr && hostinfoptr->h_name) outhost = nstrdup (hostinfoptr->h_name); else outhost = NULL; ice_clean_hostent (); return outhost; } else return NULL; }
void timer_handle_transfer_statistics (time_t stime, time_t *trottime, time_t *justone, statistics_t *trotstat) { if (get_time() != *justone) { *justone = get_time(); if ((stime % 86400) == 0) { statistics_t stat, hourlystats; get_hourly_stats(&hourlystats); zero_stats(&info.hourly_stats); update_daily_statistics(&hourlystats); get_daily_stats(&stat); zero_stats(&info.daily_stats); update_total_statistics(&stat); write_daily_stats(&stat); } else if ((stime % 3600) == 0) { statistics_t stat; get_hourly_stats(&stat); zero_stats(&info.hourly_stats); update_daily_statistics(&stat); write_hourly_stats(&stat); } if ((stime % 60) == 0) { time_t delta; statistics_t stat; unsigned int total_bytes; double KB_per_sec = 0; get_running_stats(&stat); if (*trottime == 0) { *trottime = get_time(); get_running_stats(trotstat); } else { total_bytes = (stat.read_kilos - trotstat->read_kilos) + (stat.write_kilos - trotstat->write_kilos); delta = get_time() - *trottime; if (delta <= 0) { android_log(ANDROID_LOG_VERBOSE, "ERROR: Losing track of time.. is it xmas already? [%d - %d == %d <= 0]", get_time (), *trottime, delta); } else { KB_per_sec = (double)total_bytes / (double)delta; if (KB_per_sec < 40000000) { info.bandwidth_usage = KB_per_sec; } } get_running_stats(trotstat); *trottime = get_time(); } } } }
void *_serverstart() { android_log(ANDROID_LOG_VERBOSE,"ntripcaster status before starting:%d",get_ntripcaster_state()); set_run_path(appPath); thread_lib_init (); init_thread_tree (__LINE__, __FILE__); setup_defaults (); setup_signal_traps (); allocate_resources (); init_authentication_scheme (); parse_default_config_file (); initialize_network (); startup_mode (); android_log(ANDROID_LOG_VERBOSE,"ntripcaster status after ending:%d",get_ntripcaster_state()); pthread_exit(0); android_log(ANDROID_LOG_VERBOSE,"ntripcaster ended"); }
int open_control(void) { int i; if ((i = android_get_control_socket("openconnect")) == -1) { android_log(PRG_ERR, "No control socket"); return -1; } android_log(PRG_DEBUG, "Waiting for control socket"); if (listen(i, 1) == -1 || (control = accept(i, NULL, 0)) == -1) { android_log(PRG_ERR, "Cannot get control socket"); exit(-1); } close(i); return control; }
void error(const char *format, ...) { va_list ap; va_start(ap, format); android_log(LOG_ERR, format, ap); va_end(ap); }
void btd_debug(uint16_t index, const char *format, ...) { va_list ap; va_start(ap, format); android_log(LOG_DEBUG, format, ap); va_end(ap); }
void warn(const char *format, ...) { va_list ap; va_start(ap, format); android_log(LOG_WARN, format, ap); va_end(ap); }
void info(const char *format, ...) { va_list ap; va_start(ap, format); android_log(LOG_INFO, format, ap); va_end(ap); }
/* Writes the one line status report to the log and the console if needed */ void status_write(server_info_t *infostruct) { char *lt = get_log_time(); // if (running == SERVER_RUNNING) info.num_clients = (unsigned long int) count_clients(); android_log(ANDROID_LOG_VERBOSE, "Bandwidth:%fKB/s Sources:%ld Clients:%ld", info.bandwidth_usage, info.num_sources, info.num_clients); if (lt) free(lt); }
static jint NTRIPCaster_serverstop(JNIEnv* env, jclass clazz,jint force) { if (force) { android_log(ANDROID_LOG_VERBOSE,"ntripcaster brutal ending..."); pthread_kill(ntripcaster_thread, SIGKILL); return (jint)-2; } android_log(ANDROID_LOG_VERBOSE,"ntripcaster status:%d",get_ntripcaster_state()); kick_everything (); pthread_kill(ntripcaster_thread, SIGINT); if (pthread_join_timeout(ntripcaster_thread,1000)) { android_log(ANDROID_LOG_VERBOSE,"ntripcaster was not cleanly ended"); return (jint)-1; } return (jint)0; }
static jint NTRIPCaster_serverstart(JNIEnv* env, jclass clazz, jint port, jstring conf_filename) { jint ret = 0; const char *filename = (*env)->GetStringUTFChars(env, conf_filename, 0); (*env)->ReleaseStringUTFChars(env,conf_filename, filename); if(pthread_create(&ntripcaster_thread, NULL, &_serverstart, NULL)) { android_log(ANDROID_LOG_ERROR,"Error creating thread\n"); return (jint)-1; } return (jint)ret; }
/* * This is called to handle a brand new connection, in it's own thread. * Nothing is know about the type of the connection. * Assert Class: 3 */ void *handle_connection(void *arg) { connection_t *con = (connection_t *)arg; char line[BUFSIZE] = ""; int res; thread_init(); if (!con) { android_log(ANDROID_LOG_VERBOSE, "handle_connection: got NULL connection"); thread_exit(0); } if (info.reverse_lookups) con->hostname = reverse(con->host); sock_set_blocking(con->sock, SOCK_BLOCK); /* Fill line[] with the user header, ends with \n\n */ if ((res = sock_read_lines(con->sock, line, BUFSIZE)) <= 0) { android_log(ANDROID_LOG_VERBOSE, "Socket error on connection %lu", con->id); kick_not_connected(con, "Socket error"); thread_exit(0); } if (ice_strncmp(line, "GET", 3) == 0) { client_login(con, line); } else if (ice_strncmp(line, "SOURCE", 6) == 0 || ice_strncmp(line, "POST", 4) == 0) { source_login (con, line); } else { write_400 (con); kick_not_connected(con, "Invalid header"); } thread_exit(0); return NULL; }
void zero_stats(statistics_t *stat) { if (!stat) { android_log (ANDROID_LOG_VERBOSE, "WARNING: zero_stats() called with NULL stat pointer"); return; } stat->read_bytes = 0; stat->read_kilos = 0; stat->write_bytes = 0; stat->write_kilos = 0; stat->client_connections = 0; stat->source_connections = 0; stat->client_connect_time = 0; stat->source_connect_time = 0; }
static int ui_android_write(UI *ui, UI_STRING *uis) { char *str, *pos; switch (UI_get_string_type(uis)) { case UIT_ERROR: case UIT_INFO: str = UI_get0_output_string(uis); while ((pos = strchr(ui_buf, "\n"))) *pos = " "; strlcat(ui_buf, str, sizeof(ui_buf)); android_log(PRG_DEBUG, "called ui_android_write() %s", str); break; default: break; } return 1; }
static int ui_android_read(UI *ui, UI_STRING *uis) { int ok = 0; int num = 0; const char *str; char **args; switch (UI_get_string_type(uis)) { case UIT_BOOLEAN: android_log(PRG_DEBUG, "called ui_android_read() output boolean %s", UI_get0_output_string(uis)); android_log(PRG_DEBUG, "called ui_android_read() action boolean %s", UI_get0_action_string(uis)); // UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO case UIT_PROMPT: str = UI_get0_output_string(uis); android_log(PRG_DEBUG, "called ui_android_read() output prompt %s", str); break; case UIT_VERIFY: str = UI_get0_output_string(uis); android_log(PRG_DEBUG, "called ui_android_read() verify output %s", str); break; default: return 1; break; } strlcat(ui_buf, "=X=", sizeof(ui_buf)); strlcat(ui_buf, str, sizeof(ui_buf)); process: if (send_req(ui_buf) < 0) { android_log(PRG_ERR, "send_req() failed"); goto out; } if (recv_cmd(&num, &args) < 0) { android_log(PRG_ERR, "recv_cmd() failed"); goto out; } if (num != 1) { android_log(PRG_ERR, "parameter number mismatch"); goto out; } android_log(PRG_DEBUG, "ui_android_read() cmd: %s %d", args[0], num); send_ack(num); UI_set_result(ui, uis, args[0]); ok = 1; out: if (num) free_cmd(num, args); ui_buf[0] = '\0'; return ok; }
static int ui_android_flush(UI *ui) { android_log(PRG_DEBUG, "called ui_android_flush()"); return 1; }
static int ui_android_close(UI *ui) { android_log(PRG_DEBUG, "called ui_android_close()"); return 1; }
static int ui_android_open(UI *ui) { ui_buf[0] = '\0'; android_log(PRG_DEBUG, "called ui_android_open()"); return 1; }
connection_t * get_connection (sock_t *sock) { int sockfd; socklen_t sin_len; connection_t *con; fd_set rfds; struct timeval tv; int i, maxport = 0; struct sockaddr_in *sin = (struct sockaddr_in *)nmalloc(sizeof(struct sockaddr_in)); if (!sin) { android_log (ANDROID_LOG_VERBOSE, "WARNING: Weird stuff in get_connection. nmalloc returned NULL sin"); return NULL; } /* setup sockaddr structure */ sin_len = sizeof(struct sockaddr_in); memset(sin, 0, sin_len); /* try to accept a connection */ FD_ZERO(&rfds); for (i = 0; i < MAXLISTEN; i++) { if (sock_valid (sock[i])) { FD_SET(sock[i], &rfds); if (sock[i] > maxport) maxport = sock[i]; } } maxport += 1; tv.tv_sec = 0; tv.tv_usec = 30000; if (select(maxport, &rfds, NULL, NULL, &tv) > 0) { for (i = 0; i < MAXLISTEN; i++) { if (sock_valid (sock[i]) && FD_ISSET(sock[i], &rfds)) break; } } else { nfree(sin); return NULL; } sockfd = sock_accept(sock[i], (struct sockaddr *)sin, &sin_len); if (sockfd >= 0) { con = create_connection(); if (!sin) { xa_debug (1, "ERROR: NULL sockaddr struct, wft???"); return NULL; } con->host = create_malloced_ascii_host(&(sin->sin_addr)); con->sock = sockfd; con->sin = sin; con->sinlen = sin_len; xa_debug (2, "DEBUG: Getting new connection on socket %d from host %s", sockfd, con->host ? con->host : "(null)"); con->hostname = NULL; con->headervars = NULL; con->id = new_id (); con->connect_time = get_time (); #ifdef HAVE_LIBWRAP if (!sock_check_libwrap(sockfd, unknown_connection_e)) { kick_not_connected (con, "Access Denied (tcp wrappers) [generic connection]"); return NULL; } #endif return con; } if (!is_recoverable (errno)) xa_debug (1, "WARNING: accept() failed with on socket %d, max: %d, [%d:%s]", sock[i], maxport, errno, strerror(errno)); nfree (sin); return NULL; }