int read_options(const char *fname) { FILE *fh; char line[1024], type[20], option[50], value[50]; int id; if (!(fh = fopen(fname, "rt"))) return -1; while (fgets(line, 1024, fh) != NULL) { // Check if this line is a commentary or empty line if (!strlen(line) || *line == '#') continue; // Get configuration option from setting line if (sscanf(line, "%s %s %[^\t\n]", type, option, value) == 3) { if (!strcasecmp(type, "set")) { if ((id = setting_id(option)) >= 0) { setting_set_value(id, value); } else { set_option_value(option, value); } } else if (!strcasecmp(type, "alias")) { set_alias_value(option, value); } else if (!strcasecmp(type, "bind")) { key_bind_action(key_action_id(option), key_from_str(value)); } else if (!strcasecmp(type, "unbind")) { key_unbind_action(key_action_id(option), key_from_str(value)); } } } fclose(fh); return 0; }
void setting_set_intvalue(int id, int value) { char strvalue[80]; sprintf(strvalue, "%d", value); setting_set_value(id, strvalue); }
int init_options(int no_config) { // Custom user conf file char *userconf = NULL; char *rcfile; char pwd[MAX_SETTING_LEN]; // Defualt savepath is current directory if (getcwd(pwd, MAX_SETTING_LEN)) { setting_set_value(SETTING_SAVEPATH, pwd); } // Initialize settings setting_set_value(SETTING_FILTER_METHODS, "REGISTER,INVITE,SUBSCRIBE,NOTIFY,OPTIONS,PUBLISH,MESSAGE"); // Add Call list column options set_option_value("cl.column0", "index"); set_option_value("cl.column1", "method"); set_option_value("cl.column2", "sipfrom"); set_option_value("cl.column3", "sipto"); set_option_value("cl.column4", "msgcnt"); set_option_value("cl.column5", "src"); set_option_value("cl.column6", "dst"); set_option_value("cl.column7", "state"); // Done if config file should not be read if(no_config) { return 0; } // Read options from configuration files read_options("/etc/sngreprc"); read_options("/usr/local/etc/sngreprc"); // Get user configuration if ((rcfile = getenv("SNGREPRC"))) { read_options(rcfile); } else if ((rcfile = getenv("HOME"))) { if ((userconf = sng_malloc(strlen(rcfile) + RCFILE_EXTRA_LEN))) { sprintf(userconf, "%s/.sngreprc", rcfile); read_options(userconf); sng_free(userconf); } } return 0; }
int capture_eep_set_client_url(const char *url) { char urlstr[256]; char address[256], port[256]; memset(urlstr, 0, sizeof(urlstr)); memset(address, 0, sizeof(address)); memset(port, 0, sizeof(port)); strncpy(urlstr, url, strlen(url)); if (sscanf(urlstr, "%*[^:]:%[^:]:%s", address, port) == 2) { setting_set_value(SETTING_EEP_SEND, SETTING_ON); setting_set_value(SETTING_EEP_SEND_ADDR, address); setting_set_value(SETTING_EEP_SEND_PORT, port); return 0; } return 1; }
static gboolean setting_add(Client *c, const char *name, DataType type, void *value, SettingFunction setter, int flags, void *data) { Setting *prop = g_slice_new0(Setting); prop->name = name; prop->type = type; prop->setter = setter; prop->flags = flags; prop->data = data; setting_set_value(c, prop, value, SETTING_SET); g_hash_table_insert(c->config.settings, (char*)name, prop); return TRUE; }
void setting_toggle(int id) { setting_t *sett = setting_by_id(id); if (sett) { if (sett->fmt == SETTING_FMT_STRING) return; if (sett->fmt == SETTING_FMT_NUMBER) return; if (sett->fmt == SETTING_FMT_ENUM) { setting_set_value(id, setting_enum_next(id, sett->value)); } } }
/** * @brief Main function logic * * Parse command line options and start running threads */ int main(int argc, char* argv[]) { int opt, idx, limit, only_calls, no_incomplete, i; const char *device, *outfile; char bpf[512]; const char *keyfile; const char *match_expr; int match_insensitive = 0, match_invert = 0; int no_interface = 0, quiet = 0, rtp_capture = 0; vector_t *infiles = vector_create(0, 1); // Program otptions static struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "version", no_argument, 0, 'V' }, { "device", required_argument, 0, 'd' }, { "input", required_argument, 0, 'I' }, { "output", required_argument, 0, 'O' }, #if defined(WITH_GNUTLS) || defined(WITH_OPENSSL) { "keyfile", required_argument, 0, 'k' }, #endif { "calls", no_argument, 0, 'c' }, { "rtp", no_argument, 0, 'r' }, { "limit", no_argument, 0, 'l' }, { "icase", no_argument, 0, 'i' }, { "invert", no_argument, 0, 'v' }, { "no-interface", no_argument, 0, 'N' }, { "dump-config", no_argument, 0, 'D' }, #ifdef USE_EEP { "eep-listen", required_argument, 0, 'L' }, { "eep-send", required_argument, 0, 'H' }, #endif { "quiet", no_argument, 0, 'q' }, }; // Initialize configuration options init_options(); // Get initial values for configurable arguments device = setting_get_value(SETTING_CAPTURE_DEVICE); outfile = setting_get_value(SETTING_CAPTURE_OUTFILE); keyfile = setting_get_value(SETTING_CAPTURE_KEYFILE); limit = setting_get_intvalue(SETTING_CAPTURE_LIMIT); only_calls = setting_enabled(SETTING_SIP_CALLS); no_incomplete = setting_enabled(SETTING_SIP_NOINCOMPLETE); rtp_capture = setting_enabled(SETTING_CAPTURE_RTP); // Parse command line arguments opterr = 0; char *options = "hVd:I:O:pqtW:k:crl:ivNqDL:H:"; while ((opt = getopt_long(argc, argv, options, long_options, &idx)) != -1) { switch (opt) { case 'h': usage(); return 0; case 'V': version(); return 0; case 'd': device = optarg; break; case 'I': vector_append(infiles, optarg); break; case 'O': outfile = optarg; break; case 'l': if(!(limit = atoi(optarg))) { fprintf(stderr, "Invalid limit value.\n"); return 0; } break; #if defined(WITH_GNUTLS) || defined(WITH_OPENSSL) case 'k': keyfile = optarg; break; #endif case 'c': only_calls = 1; setting_set_value(SETTING_SIP_CALLS, SETTING_ON); break; case 'r': rtp_capture = 1; setting_set_value(SETTING_CAPTURE_RTP, SETTING_ON); break; case 'i': match_insensitive++; break; case 'v': match_invert++; break; case 'N': no_interface = 1; setting_set_value(SETTING_CAPTURE_STORAGE, "none"); break; case 'q': quiet = 1; break; case 'D': key_bindings_dump(); settings_dump(); return 0; // Dark options for dummy ones case 'p': case 't': case 'W': break; #ifdef USE_EEP case 'L': capture_eep_set_server_url(optarg); break; case 'H': capture_eep_set_client_url(optarg); break; #endif case '?': if (strchr(options, optopt)) { fprintf(stderr, "-%c option requires an argument.\n", optopt); } else if (isprint(optopt)) { fprintf(stderr, "Unknown option -%c.\n", optopt); } else { fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt); } return 1; default: break; } } #if defined(WITH_GNUTLS) || defined(WITH_OPENSSL) // Set capture decrypt key file capture_set_keyfile(keyfile); // Check if we have a keyfile and is valid if (keyfile && !tls_check_keyfile(keyfile)) { fprintf(stderr, "%s does not contain a valid RSA private key.\n", keyfile); return 1; } #endif // Check if given argument is a file if (argc == 2 && (access(argv[1], F_OK) == 0)) { // Old legacy option to open pcaps without other arguments printf("%s seems to be a file: You forgot -I flag?\n", argv[1]); return 0; } // Initialize SIP Messages Storage sip_init(limit, only_calls, no_incomplete); // Set capture options capture_init(limit, rtp_capture); #ifdef USE_EEP // Initialize EEP if enabled capture_eep_init(); #endif // If we have an input file, load it if (vector_count(infiles)) { for (i = 0; i < vector_count(infiles); i++) { // Try to load file if (capture_offline(vector_item(infiles, i), outfile) != 0) return 1; } } else { // Check if all capture data is valid if (capture_online(device, outfile) != 0) return 1; } // Remove Input files vector vector_destroy(infiles); // More arguments pending! if (argv[optind]) { // Assume first pending argument is match expression match_expr = argv[optind++]; // Try to build the bpf filter string with the rest memset(bpf, 0, sizeof(bpf)); for (i = optind; i < argc; i++) sprintf(bpf + strlen(bpf), "%s ", argv[i]); // Check if this BPF filter is valid if (capture_set_bpf_filter(bpf) != 0) { // BPF Filter invalid, check incluiding match_expr match_expr = 0; // There is no need to parse all payload at this point // Build the bpf filter string memset(bpf, 0, sizeof(bpf)); for (i = optind - 1; i < argc; i++) sprintf(bpf + strlen(bpf), "%s ", argv[i]); // Check bpf filter is valid again if (capture_set_bpf_filter(bpf) != 0) { fprintf(stderr, "Couldn't install filter %s: %s\n", bpf, capture_last_error()); return 1; } } // Set the capture filter if (match_expr) if (sip_set_match_expression(match_expr, match_insensitive, match_invert)) { fprintf(stderr, "Unable to parse expression %s\n", match_expr); return 1; } } // Start a capture thread if (capture_launch_thread() != 0) { ncurses_deinit(); fprintf(stderr, "Failed to launch capture thread.\n"); return 1; } if (!no_interface) { // Initialize interface ncurses_init(); // This is a blocking call. // Create the first panel and wait for user input ui_create_panel(PANEL_CALL_LIST); wait_for_input(); } else { setbuf(stdout, NULL); while(capture_get_status() != CAPTURE_OFFLINE) { if (!quiet) printf("\rDialog count: %d", sip_calls_count()); usleep(500 * 1000); } if (!quiet) printf("\rDialog count: %d\n", sip_calls_count()); } // Capture deinit capture_deinit(); // Deinitialize interface ncurses_deinit(); // Deinitialize configuration options deinit_options(); // Deallocate sip stored messages sip_deinit(); // Leaving! return 0; }
VbCmdResult setting_run(Client *c, char *name, const char *param) { SettingType type = SETTING_SET; char modifier; int res, len; /* determine the type to names last char and param */ len = strlen(name); modifier = name[len - 1]; if (modifier == '?') { name[len - 1] = '\0'; type = SETTING_GET; } else if (modifier == '+') { name[len - 1] = '\0'; type = SETTING_APPEND; } else if (modifier == '^') { name[len - 1] = '\0'; type = SETTING_PREPEND; } else if (modifier == '-') { name[len - 1] = '\0'; type = SETTING_REMOVE; } else if (modifier == '!') { name[len - 1] = '\0'; type = SETTING_TOGGLE; } else if (!param) { type = SETTING_GET; } /* lookup a matching setting */ Setting *s = g_hash_table_lookup(c->config.settings, name); if (!s) { vb_echo(c, MSG_ERROR, TRUE, "Config '%s' not found", name); return CMD_ERROR | CMD_KEEPINPUT; } if (type == SETTING_GET) { setting_print(c, s); return CMD_SUCCESS | CMD_KEEPINPUT; } if (type == SETTING_TOGGLE) { if (s->type != TYPE_BOOLEAN) { vb_echo(c, MSG_ERROR, TRUE, "Could not toggle none boolean %s", s->name); return CMD_ERROR | CMD_KEEPINPUT; } gboolean value = !s->value.b; res = setting_set_value(c, s, &value, SETTING_SET); setting_print(c, s); /* make sure the new value set by the toggle keep visible */ res |= CMD_KEEPINPUT; } else { if (!param) { vb_echo(c, MSG_ERROR, TRUE, "No valid value"); return CMD_ERROR | CMD_KEEPINPUT; } /* convert sting value into internal used data type */ gboolean boolvar; int intvar; switch (s->type) { case TYPE_BOOLEAN: boolvar = g_ascii_strncasecmp(param, "true", 4) == 0 || g_ascii_strncasecmp(param, "on", 2) == 0; res = setting_set_value(c, s, &boolvar, type); break; case TYPE_INTEGER: intvar = g_ascii_strtoull(param, (char**)NULL, 10); res = setting_set_value(c, s, &intvar, type); break; default: res = setting_set_value(c, s, (void*)param, type); break; } } if (res & (CMD_SUCCESS | CMD_KEEPINPUT)) { return res; } vb_echo(c, MSG_ERROR, TRUE, "Could not set %s", s->name); return CMD_ERROR | CMD_KEEPINPUT; }