int channelList_load(netsnmp_cache *cache, void *vmagic) { switch_cache_db_handle_t *dbh; char sql[1024] = ""; channelList_free(cache, NULL); if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) { return 0; } idx = 1; sprintf(sql, "SELECT * FROM channels WHERE hostname='%s' ORDER BY created_epoch", switch_core_get_switchname()); switch_cache_db_execute_sql_callback(dbh, sql, channelList_callback, NULL, NULL); switch_cache_db_release_db_handle(&dbh); return 0; }
static switch_xml_t xml_url_fetch(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, void *user_data) { switch_xml_t xml = NULL; char *data = NULL; xml_binding_t *binding = (xml_binding_t *) user_data; char hostname[256] = ""; char basic_data[512]; unsigned char buf[16336] = ""; ssize_t len = -1, bytes = 0; scgi_handle_t handle = { 0 }; switch_stream_handle_t stream = { 0 }; char *txt = NULL; strncpy(hostname, switch_core_get_switchname(), sizeof(hostname)); if (!binding) { return NULL; } switch_snprintf(basic_data, sizeof(basic_data), "hostname=%s§ion=%s&tag_name=%s&key_name=%s&key_value=%s", hostname, section, switch_str_nil(tag_name), switch_str_nil(key_name), switch_str_nil(key_value)); data = switch_event_build_param_string(params, basic_data, binding->vars_map); switch_assert(data); scgi_add_param(&handle, "REQUEST_METHOD", "POST"); scgi_add_param(&handle, "SERVER_PROTOCOL", "HTTP/1.0"); scgi_add_param(&handle, "REQUEST_URI", binding->uri); scgi_add_body(&handle, data); if (scgi_connect(&handle, binding->host, binding->port, binding->timeout * 1000) == SCGI_SUCCESS) { scgi_send_request(&handle); SWITCH_STANDARD_STREAM(stream); txt = (char *) stream.data; while((len = scgi_recv(&handle, buf, sizeof(buf))) > 0) { char *expanded = switch_event_expand_headers(params, (char *)buf); bytes += len; if (bytes > XML_SCGI_MAX_BYTES) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Data too big!\n"); len = -1; break; } stream.write_function(&stream, "%s", expanded); txt = (char *) stream.data; if (expanded != (char *)buf) { free(expanded); } memset(buf, 0, sizeof(buf)); } scgi_disconnect(&handle); if (len < 0 && (!txt || !strlen(txt))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s Connection Read Failed: [%s]\n", binding->url, handle.err); goto end; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s Connection Failed: [%s]\n", binding->url, handle.err); goto end; } if (GLOBAL_DEBUG) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "DEBUG:\nURL: %s\nPOST_DATA:\n%s\n\nRESPONSE:\n-----\n%s\n-----\n", binding->url, data, switch_str_nil(txt)); } if (bytes && txt) { if (!(xml = switch_xml_parse_str_dynamic(txt, FALSE))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result! [%s]\ndata: [%s] RESPONSE[%s]\n", binding->url, data, switch_str_nil(txt)); } txt = NULL; } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received error trying to fetch %s\ndata: [%s] RESPONSE [%s]\n", binding->url, data, switch_str_nil(txt)); } end: switch_safe_free(data); switch_safe_free(txt); return xml; }
static switch_xml_t xml_url_fetch(const char *section, const char *tag_name, const char *key_name, const char *key_value, switch_event_t *params, void *user_data) { char filename[512] = ""; CURL *curl_handle = NULL; struct config_data config_data; switch_xml_t xml = NULL; char *data = NULL; switch_uuid_t uuid; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; xml_binding_t *binding = (xml_binding_t *) user_data; char *file_url; struct curl_slist *slist = NULL; long httpRes = 0; struct curl_slist *headers = NULL; char hostname[256] = ""; char basic_data[512]; char *uri = NULL; char *dynamic_url = NULL; strncpy(hostname, switch_core_get_switchname(), sizeof(hostname)); if (!binding) { return NULL; } if ((file_url = strstr(binding->url, "file:"))) { file_url += 5; if (!(xml = switch_xml_parse_file(file_url))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result!\n"); } return xml; } switch_snprintf(basic_data, sizeof(basic_data), "hostname=%s§ion=%s&tag_name=%s&key_name=%s&key_value=%s", hostname, section, switch_str_nil(tag_name), switch_str_nil(key_name), switch_str_nil(key_value)); data = switch_event_build_param_string(params, basic_data, binding->vars_map); switch_assert(data); if (binding->use_dynamic_url) { if (!params) { switch_event_create(¶ms, SWITCH_EVENT_REQUEST_PARAMS); switch_assert(params); } switch_event_add_header_string(params, SWITCH_STACK_TOP, "hostname", hostname); switch_event_add_header_string(params, SWITCH_STACK_TOP, "section", switch_str_nil(section)); switch_event_add_header_string(params, SWITCH_STACK_TOP, "tag_name", switch_str_nil(tag_name)); switch_event_add_header_string(params, SWITCH_STACK_TOP, "key_name", switch_str_nil(key_name)); switch_event_add_header_string(params, SWITCH_STACK_TOP, "key_value", switch_str_nil(key_value)); dynamic_url = switch_event_expand_headers(params, binding->url); switch_assert(dynamic_url); } else { dynamic_url = binding->url; } if (binding->use_get_style == 1) { uri = malloc(strlen(data) + strlen(dynamic_url) + 16); switch_assert(uri); sprintf(uri, "%s%c%s", dynamic_url, strchr(dynamic_url, '?') != NULL ? '&' : '?', data); } switch_uuid_get(&uuid); switch_uuid_format(uuid_str, &uuid); switch_snprintf(filename, sizeof(filename), "%s%s.tmp.xml", SWITCH_GLOBAL_dirs.temp_dir, uuid_str); curl_handle = curl_easy_init(); headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded"); if (!strncasecmp(binding->url, "https", 5)) { curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0); curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0); } memset(&config_data, 0, sizeof(config_data)); config_data.name = filename; config_data.max_bytes = XML_CURL_MAX_BYTES; if ((config_data.fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)) > -1) { if (!zstr(binding->cred)) { curl_easy_setopt(curl_handle, CURLOPT_HTTPAUTH, binding->auth_scheme); curl_easy_setopt(curl_handle, CURLOPT_USERPWD, binding->cred); } curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, headers); if (binding->method != NULL) curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, binding->method); curl_easy_setopt(curl_handle, CURLOPT_POST, !binding->use_get_style); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1); curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 10); if (!binding->use_get_style) curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, data); curl_easy_setopt(curl_handle, CURLOPT_URL, binding->use_get_style ? uri : dynamic_url); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, file_callback); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *) &config_data); curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "freeswitch-xml/1.0"); if (binding->timeout) { curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, binding->timeout); curl_easy_setopt(curl_handle, CURLOPT_NOSIGNAL, 1); } if (binding->disable100continue) { slist = curl_slist_append(slist, "Expect:"); curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, slist); } if (binding->enable_cacert_check) { curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, TRUE); } if (binding->ssl_cert_file) { curl_easy_setopt(curl_handle, CURLOPT_SSLCERT, binding->ssl_cert_file); } if (binding->ssl_key_file) { curl_easy_setopt(curl_handle, CURLOPT_SSLKEY, binding->ssl_key_file); } if (binding->ssl_key_password) { curl_easy_setopt(curl_handle, CURLOPT_SSLKEYPASSWD, binding->ssl_key_password); } if (binding->ssl_version) { if (!strcasecmp(binding->ssl_version, "SSLv3")) { curl_easy_setopt(curl_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_SSLv3); } else if (!strcasecmp(binding->ssl_version, "TLSv1")) { curl_easy_setopt(curl_handle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); } } if (binding->ssl_cacert_file) { curl_easy_setopt(curl_handle, CURLOPT_CAINFO, binding->ssl_cacert_file); } if (binding->enable_ssl_verifyhost) { curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 2); } if (binding->cookie_file) { curl_easy_setopt(curl_handle, CURLOPT_COOKIEJAR, binding->cookie_file); curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, binding->cookie_file); } curl_easy_perform(curl_handle); curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &httpRes); curl_easy_cleanup(curl_handle); curl_slist_free_all(headers); curl_slist_free_all(slist); close(config_data.fd); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Opening temp file!\n"); } if (config_data.err) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error encountered! [%s]\ndata: [%s]\n", binding->url, data); xml = NULL; } else { if (httpRes == 200) { if (!(xml = switch_xml_parse_file(filename))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing Result! [%s]\ndata: [%s]\n", binding->url, data); } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Received HTTP error %ld trying to fetch %s\ndata: [%s]\n", httpRes, binding->url, data); xml = NULL; } } /* Debug by leaving the file behind for review */ if (keep_files_around) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "XML response is in %s\n", filename); } else { if (unlink(filename) != 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "XML response file [%s] delete failed\n", filename); } } switch_safe_free(data); if (binding->use_get_style == 1) switch_safe_free(uri); if (binding->use_dynamic_url && dynamic_url != binding->url) switch_safe_free(dynamic_url); return xml; }
int handle_systemStats(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests) { netsnmp_request_info *request = NULL; oid subid; switch_time_t uptime; uint32_t int_val = 0; switch(reqinfo->mode) { case MODE_GET: subid = requests->requestvb->name[reginfo->rootoid_len - 2]; snmp_log(LOG_DEBUG, "systemStats OID-suffix requested (%d)\n", (int) subid); switch (subid) { case SS_UPTIME: uptime = switch_core_uptime() / 10000; snmp_set_var_typed_value(requests->requestvb, ASN_TIMETICKS, (u_char *) &uptime, sizeof(uptime)); break; case SS_SESSIONS_SINCE_STARTUP: int_val = switch_core_session_id() - 1; snmp_set_var_typed_integer(requests->requestvb, ASN_COUNTER, int_val); break; case SS_CURRENT_SESSIONS: int_val = switch_core_session_count(); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_MAX_SESSIONS: switch_core_session_ctl(SCSC_MAX_SESSIONS, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_CURRENT_CALLS: { switch_cache_db_handle_t *dbh; char sql[1024] = ""; if (switch_core_db_handle(&dbh) != SWITCH_STATUS_SUCCESS) { return SNMP_ERR_GENERR; } sprintf(sql, "SELECT COUNT(*) FROM calls WHERE hostname='%s'", switch_core_get_switchname()); switch_cache_db_execute_sql_callback(dbh, sql, sql_count_callback, &int_val, NULL); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); switch_cache_db_release_db_handle(&dbh); } break; case SS_SESSIONS_PER_SECOND: switch_core_session_ctl(SCSC_LAST_SPS, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_MAX_SESSIONS_PER_SECOND: switch_core_session_ctl(SCSC_SPS, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS_PER_SECOND: switch_core_session_ctl(SCSC_SPS_PEAK, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS_PER_FIVEMIN: switch_core_session_ctl(SCSC_SPS_PEAK_FIVEMIN, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS: switch_core_session_ctl(SCSC_SESSIONS_PEAK, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; case SS_PEAK_SESSIONS_FIVEMIN: switch_core_session_ctl(SCSC_SESSIONS_PEAK_FIVEMIN, &int_val); snmp_set_var_typed_integer(requests->requestvb, ASN_GAUGE, int_val); break; default: snmp_log(LOG_WARNING, "Unregistered OID-suffix requested (%d)\n", (int) subid); netsnmp_set_request_error(reqinfo, request, SNMP_NOSUCHOBJECT); } break; default: /* we should never get here, so this is a really bad error */ snmp_log(LOG_ERR, "Unknown mode (%d) in handle_systemStats\n", reqinfo->mode); return SNMP_ERR_GENERR; } return SNMP_ERR_NOERROR; }
static void event_handler(switch_event_t *event) { uint8_t send = 0; if (globals.running != 1) { return; } if (event->subclass_name && (!strcmp(event->subclass_name, MULTICAST_EVENT) || !strcmp(event->subclass_name, MULTICAST_PEERUP) || !strcmp(event->subclass_name, MULTICAST_PEERDOWN))) { char *event_name, *sender; if ((event_name = switch_event_get_header(event, "orig-event-name")) && !strcasecmp(event_name, "HEARTBEAT") && (sender = switch_event_get_header(event, "orig-multicast-sender"))) { struct peer_status *p; time_t now = switch_epoch_time_now(NULL); if (!(p = switch_core_hash_find(globals.peer_hash, sender))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Host %s not already in hash\n", sender); p = switch_core_alloc(module_pool, sizeof(struct peer_status)); p->active = SWITCH_FALSE; p->lastseen = 0; /*} else { */ /*switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Host %s last seen %d seconds ago\n", sender, now - p->lastseen); */ } if (!p->active) { switch_event_t *local_event; if (switch_event_create_subclass(&local_event, SWITCH_EVENT_CUSTOM, MULTICAST_PEERUP) == SWITCH_STATUS_SUCCESS) { char lastseen[21]; switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "Peer", sender); if (p->lastseen) { switch_snprintf(lastseen, sizeof(lastseen), "%d", (int) p->lastseen); } else { switch_snprintf(lastseen, sizeof(lastseen), "%s", "Never"); } switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "Lastseen", lastseen); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Peer %s has come up; last seen: %s\n", sender, lastseen); switch_event_fire(&local_event); } } p->active = SWITCH_TRUE; p->lastseen = now; switch_core_hash_insert(globals.peer_hash, sender, p); } /* ignore our own events to avoid ping pong */ return; } if (event->event_id == SWITCH_EVENT_RELOADXML) { switch_mutex_lock(globals.mutex); switch_core_hash_destroy(&globals.event_hash); globals.event_hash = NULL; if (globals.psk) { switch_safe_free(globals.psk); globals.psk = NULL; } switch_core_hash_init(&globals.event_hash, module_pool); memset(globals.event_list, 0, SWITCH_EVENT_ALL + 1); if (load_config() != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Failed to reload config file\n"); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Event Multicast Reloaded\n"); } switch_mutex_unlock(globals.mutex); } if (event->event_id == SWITCH_EVENT_HEARTBEAT) { switch_hash_index_t *cur; switch_ssize_t keylen; const void *key; void *value; time_t now = switch_epoch_time_now(NULL); struct peer_status *last; char *host; for (cur = switch_hash_first(NULL, globals.peer_hash); cur; cur = switch_hash_next(cur)) { switch_hash_this(cur, &key, &keylen, &value); host = (char *) key; last = (struct peer_status *) value; if (last->active && (now - (last->lastseen)) > 60) { switch_event_t *local_event; last->active = SWITCH_FALSE; if (switch_event_create_subclass(&local_event, SWITCH_EVENT_CUSTOM, MULTICAST_PEERDOWN) == SWITCH_STATUS_SUCCESS) { char lastseen[21]; switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "Peer", host); switch_snprintf(lastseen, sizeof(lastseen), "%d", (int) last->lastseen); switch_event_add_header_string(local_event, SWITCH_STACK_BOTTOM, "Lastseen", lastseen); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Peer %s has gone down; last seen: %s\n", host, lastseen); switch_event_fire(&local_event); } } } } switch_mutex_lock(globals.mutex); if (globals.event_list[(uint8_t) SWITCH_EVENT_ALL]) { send = 1; } else if ((globals.event_list[(uint8_t) event->event_id])) { if (event->event_id != SWITCH_EVENT_CUSTOM || (event->subclass_name && switch_core_hash_find(globals.event_hash, event->subclass_name))) { send = 1; } } switch_mutex_unlock(globals.mutex); if (send) { char *packet; switch (event->event_id) { case SWITCH_EVENT_LOG: return; default: switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Multicast-Sender", switch_core_get_switchname()); if (switch_event_serialize(event, &packet, SWITCH_TRUE) == SWITCH_STATUS_SUCCESS) { size_t len; char *buf; #ifdef HAVE_OPENSSL int outlen, tmplen; EVP_CIPHER_CTX ctx; char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1]; switch_uuid_t uuid; switch_uuid_get(&uuid); switch_uuid_format(uuid_str, &uuid); len = strlen(packet) + SWITCH_UUID_FORMATTED_LENGTH + EVP_MAX_IV_LENGTH + strlen((char *) MAGIC); #else len = strlen(packet) + strlen((char *) MAGIC); #endif buf = malloc(len + 1); memset(buf, 0, len + 1); switch_assert(buf); #ifdef HAVE_OPENSSL if (globals.psk) { switch_copy_string(buf, uuid_str, SWITCH_UUID_FORMATTED_LENGTH); EVP_CIPHER_CTX_init(&ctx); EVP_EncryptInit(&ctx, EVP_bf_cbc(), NULL, NULL); EVP_CIPHER_CTX_set_key_length(&ctx, strlen(globals.psk)); EVP_EncryptInit(&ctx, NULL, (unsigned char *) globals.psk, (unsigned char *) uuid_str); EVP_EncryptUpdate(&ctx, (unsigned char *) buf + SWITCH_UUID_FORMATTED_LENGTH, &outlen, (unsigned char *) packet, (int) strlen(packet)); EVP_EncryptUpdate(&ctx, (unsigned char *) buf + SWITCH_UUID_FORMATTED_LENGTH + outlen, &tmplen, (unsigned char *) MAGIC, (int) strlen((char *) MAGIC)); outlen += tmplen; EVP_EncryptFinal(&ctx, (unsigned char *) buf + SWITCH_UUID_FORMATTED_LENGTH + outlen, &tmplen); outlen += tmplen; len = (size_t) outlen + SWITCH_UUID_FORMATTED_LENGTH; *(buf + SWITCH_UUID_FORMATTED_LENGTH + outlen) = '\0'; } else { #endif switch_copy_string(buf, packet, len); switch_copy_string(buf + strlen(packet), (char *) MAGIC, strlen((char *) MAGIC) + 1); #ifdef HAVE_OPENSSL } #endif switch_socket_sendto(globals.udp_socket, globals.addr, 0, buf, &len); switch_safe_free(packet); switch_safe_free(buf); } break; } } return; }