void oss_get_bucket_uri(const oss_request_options_t *options, const aos_string_t *bucket, aos_http_request_t *req) { int32_t proto_len; const char *raw_endpoint_str; aos_string_t raw_endpoint; generate_proto(options, req); proto_len = strlen(req->proto); raw_endpoint_str = aos_pstrdup(options->pool, &options->config->endpoint) + proto_len; raw_endpoint.len = options->config->endpoint.len - proto_len; raw_endpoint.data = options->config->endpoint.data + proto_len; if (is_valid_ip(raw_endpoint_str)) { req->resource = apr_psprintf(options->pool, "%.*s", bucket->len, bucket->data); } else { req->resource = apr_psprintf(options->pool, "%.*s/", bucket->len, bucket->data); } if (options->config->is_cname || is_valid_ip(raw_endpoint_str)) { req->host = apr_psprintf(options->pool, "%.*s", raw_endpoint.len, raw_endpoint.data); req->uri = apr_psprintf(options->pool, "%.*s", bucket->len, bucket->data); } else { req->host = apr_psprintf(options->pool, "%.*s.%.*s", bucket->len, bucket->data, raw_endpoint.len, raw_endpoint.data); req->uri = apr_psprintf(options->pool, "%s", ""); } }
void Logger::init(const char * serverIp) { ASSERT(is_valid_ip(serverIp)); u32 ip = str_to_ip(serverIp); init(ip); }
Host *db_read_host(MYSQL_ROW row, MYSQL_FIELD *fields, gint num_fields, Host *default_host) { Host *host; gchar **tokens, **tok; gint i; host = host_new(); if (default_host != NULL && BUS_IS_HOST(default_host)) { host_set_user(host, host_get_user(default_host)); host_set_email(host, host_get_email(default_host)); host_set_backupdir(host, host_get_backupdir(default_host)); host_set_archivedir(host, host_get_archivedir(default_host)); host_set_max_age(host, host_get_max_age(default_host)); host_set_max_age_incr(host, host_get_max_age_incr(default_host)); host_set_max_age_full(host, host_get_max_age_full(default_host)); host_set_max_incr(host, host_get_max_incr(default_host)); host_set_rsync_opts(host, host_get_rsync_opts(default_host)); host->excludes = g_list_clone(default_host->excludes); host->schedule = g_list_clone(default_host->schedule); host->srcdirs = g_list_clone(default_host->srcdirs); host->ips = g_list_clone(default_host->ips); } for (i = 0; i < num_fields; i++) { if (g_strcmp0(fields[i].name, "id") == 0) { host_set_mysql_id(host, atoi(row[i])); } if (g_strcmp0(fields[i].name, "name") == 0) { if (row[i] == 0 || strlen(row[i]) == 0) { syslog(LOG_ERR, "Invalid name"); g_object_unref(host); continue; } host_set_name(host, row[i]); } if (g_strcmp0(fields[i].name, "hostname") == 0) { if (row[i] == NULL || strlen(row[i]) == 0) { syslog(LOG_ERR, "Invalid hostname"); g_object_unref(host); continue; } host_set_hostname(host, row[i]); } if (g_strcmp0(fields[i].name, "user") == 0) { host_set_user(host, row[i] ? row[i] : DEFAULT_USER); } if (g_strcmp0(fields[i].name, "email") == 0) { if (row[i] != NULL) { host_set_email(host, row[i]); } } if (g_strcmp0(fields[i].name, "max_incr") == 0) { host_set_max_incr(host, row[i] ? atoi(row[i]) : DEFAULT_MAX_INCR); } if (g_strcmp0(fields[i].name, "max_age") == 0) { host_set_max_age(host, row[i] ? atof(row[i]) : DEFAULT_MAX_AGE); } if (g_strcmp0(fields[i].name, "max_age_incr") == 0) { host_set_max_age_incr(host, row[i] ? atof(row[i]) : DEFAULT_MAX_AGE_INCR); } if (g_strcmp0(fields[i].name, "max_age_full") == 0) { host_set_max_age_full(host, row[i] ? atof(row[i]) : DEFAULT_MAX_AGE_FULL); } if (g_strcmp0(fields[i].name, "rsync_opts") == 0) { host_set_rsync_opts(host, row[i] ? row[i] : DEFAULT_RSYNC_OPTS); } if (g_strcmp0(fields[i].name, "archivedir") == 0) { host_set_archivedir(host, row[i] ? row[i] : ""); } if (g_strcmp0(fields[i].name, "backupdir") == 0) { host_set_backupdir(host, row[i] ? row[i] : DEFAULT_BACKUPDIR); } if (g_strcmp0(fields[i].name, "schedule") == 0) { if (row[i] != NULL) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0) { host_add_schedule(host, str); } } g_strfreev(tokens); } } if (g_strcmp0(fields[i].name, "excludes") == 0) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0) { host_add_exclude(host, str); } } g_strfreev(tokens); } if (g_strcmp0(fields[i].name, "ips") == 0) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str; str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0 && is_valid_ip(str)) { host_add_ip(host, str); } } g_strfreev(tokens); } if (g_strcmp0(fields[i].name, "srcdirs") == 0) { tokens = g_strsplit(row[i], "\n", 0); for (tok = tokens; *tok != NULL; tok++) { gchar *str = g_strstrip(*tok); if (str != NULL && strlen(str) > 0) { host_add_srcdir(host, str); } } g_strfreev(tokens); } } return (host); }
/* * build_activity_clause takes agent request parameters for an item key which * targets the pg_stat_actity table and creates an SQL clause to filter * results. * * This function should be reused for all items which query the pg_stat_activity * table. * * request is the agent request containing the user parameters. * buf is the character buffer to which the clause is written. * params is a pointer to a PGparams type which stores query parameters. * has_clause determines if the the clause starts with "WITH" or "AND". * * Returns non-zero on success. */ static int build_activity_clause(const AGENT_REQUEST *request, char *buf, PGparams *params, int has_clause) { const char *__function_name = "build_activity_clause"; // Function name for log file int i = 0; char *param = NULL; char *clause = (0 < has_clause ? PG_AND : PG_WHERE); int pgi = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); // iterate over the available parameters for(i = 0; i < 4; i++) { param = get_rparam(request, PARAM_FIRST + i); if(!strisnull(param)) { switch(i) { case 0: // <database> *params = param_append(*params, param); if(is_oid(param)) zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s datid = $%i", clause, ++pgi); else zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s datname = $%i", clause, ++pgi); break; case 1: // <user> *params = param_append(*params, param); if(is_oid(param)) zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s usesysid = $%i", clause, ++pgi); else zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s usename = $%i", clause, ++pgi); break; case 2: // <client> *params = param_append(*params, param); if(is_valid_ip(param)) zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s client_addr = $%i::inet", clause, ++pgi); else // requires v9.1+ zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s client_hostname = $%i", clause, ++pgi); break; case 3: // <waiting> if(0 == strncmp("true\0", param, 5)) { zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s waiting = TRUE", clause); } else if(0 == strncmp("false\0", param, 6)) { zbx_snprintf(buf, MAX_CLAUSE_LEN, " %s waiting = FALSE", clause); } else { zabbix_log(LOG_LEVEL_ERR, "Unsupported parameter value: \"%s\" in %s", param, request->key); return 0; } break; } buf += strlen(buf); clause = PG_AND; } } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return 1; }
//======================================= static int lwifi_startsta( lua_State* L ) { LinkStatusTypeDef link; network_InitTypeDef_st wNetConfig; size_t len=0; lua_system_param_t lua_system_param; uint8_t has_default = 0; signed retry_interval = 0; int con_wait = 0; int tmo = mico_get_time(); // check if wifi is already connected memset(&link, 0x00, sizeof(link)); micoWlanGetLinkStatus(&link); if (!lua_istable(L, 1)) { // ==== Call without parameters, return connection status === if (link.is_connected != 0) lua_pushboolean(L, true); else lua_pushboolean(L, false); return 1; } // ==== parameters exists, configure and start ==== if (wifi_sta_started == 1) _stopWifiSta(); memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); // check if default params exists if (getLua_systemParams(&lua_system_param) == 1) { if ((strlen(lua_system_param.wifi_ssid) > 0) && (strlen(lua_system_param.wifi_key) > 0)) { has_default = 1; } } //wait for connection lua_getfield(L, 1, "wait"); if (!lua_isnil(L, -1)) { int wfc = luaL_checkinteger( L, -1 ); if ((wfc > 0) && (wfc < 16)) con_wait = wfc * 1000; else return luaL_error( L, "wait must be 1 ~ 15"); } //ssid lua_getfield(L, 1, "ssid"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *ssid = luaL_checklstring( L, -1, &len ); if (len >= 32) return luaL_error( L, "ssid: <32" ); strncpy(wNetConfig.wifi_ssid,ssid,len); } else return luaL_error( L, "wrong arg type:ssid" ); } else if (has_default == 1) { strcpy(wNetConfig.wifi_ssid, lua_system_param.wifi_ssid); } else return luaL_error( L, "arg: ssid needed" ); //pwd lua_getfield(L, 1, "pwd"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *pwd = luaL_checklstring( L, -1, &len ); if (len >= 64) return luaL_error( L, "pwd: <64" ); if (len > 0) strncpy(wNetConfig.wifi_key,pwd,len); else strcpy(wNetConfig.wifi_key,""); } else return luaL_error( L, "wrong arg type: pwd" ); } else if (has_default == 1) { strcpy(wNetConfig.wifi_key, lua_system_param.wifi_key); } else return luaL_error( L, "arg: pwd needed" ); //dhcp wNetConfig.dhcpMode = DHCP_Client; lua_getfield(L, 1, "dhcp"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *pwd = luaL_checklstring( L, -1, &len ); if (strcmp(pwd, "disable") == 0) wNetConfig.dhcpMode = DHCP_Disable; } else return luaL_error( L, "wrong arg type: dhcp" ); } //ip lua_getfield(L, 1, "ip"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *ip = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "ip: <16" ); if (is_valid_ip(ip) == false) return luaL_error( L, "ip invalid" ); strncpy(wNetConfig.local_ip_addr,ip,len); } else return luaL_error( L, "wrong arg type:ip" ); } else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: ip needed" ); //netmask lua_getfield(L, 1, "netmask"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *netmask = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "netmask: <16" ); if (is_valid_ip(netmask) == false) return luaL_error( L, "netmask invalid" ); strncpy(wNetConfig.net_mask,netmask,len); } else return luaL_error( L, "wrong arg type: netmask" ); } else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: netmask needed" ); //gateway lua_getfield(L, 1, "gateway"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *gateway = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "gateway: <16" ); if (is_valid_ip(gateway) == false) return luaL_error( L, "gateway invalid" ); strncpy(wNetConfig.gateway_ip_addr,gateway,len); } else return luaL_error( L, "wrong arg type: gateway" ); } else if(wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: gateway needed" ); //dnsSrv lua_getfield(L, 1, "dnsSrv"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *dnsSrv = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "dnsSrv: <16" ); if (is_valid_ip(dnsSrv) == false) return luaL_error( L, "dnsSrv invalid" ); strncpy(wNetConfig.dnsServer_ip_addr,dnsSrv,len); } else return luaL_error( L, "wrong arg type: dnsSrv" ); } else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: dnsSrv needed" ); //retry_interval lua_getfield(L, 1, "retry_interval"); if (!lua_isnil(L, -1)) { retry_interval = (signed)luaL_checknumber( L, -1 ); if (retry_interval < 0) return luaL_error( L, "retry_interval: >=0ms" ); if (retry_interval == 0) retry_interval = 0x7FFFFFFF; } else retry_interval = 1000; wNetConfig.wifi_retry_interval = retry_interval; gL = L; //notify, set CB function for wifi state change if (wifi_status_changed_STA != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_STA); wifi_status_changed_STA = LUA_NOREF; if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 2); // copy argument (func) to the top of stack wifi_status_changed_STA = luaL_ref(L, LUA_REGISTRYINDEX); } mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler, NULL ); //start wNetConfig.wifi_mode = Station; micoWlanStart(&wNetConfig); wifi_sta_started = 1; if (con_wait == 0) { lua_pushboolean(L, false); return 1; } tmo = mico_get_time(); micoWlanGetLinkStatus(&link); while (link.is_connected == 0) { if ((mico_get_time() - tmo) > con_wait) break; mico_thread_msleep(50); luaWdgReload(); micoWlanGetLinkStatus(&link); } if (link.is_connected == 0) lua_pushboolean(L, false); else lua_pushboolean(L, true); return 1; }
//====================================== static int lwifi_startap( lua_State* L ) { //4 stations Max network_InitTypeDef_st wNetConfig; size_t len=0; memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); if (!lua_istable(L, 1)) { // ==== Call without parameters, return status === if (wifi_ap_started == 1) lua_pushboolean(L, true); else lua_pushboolean(L, false); return 1; } //ssid lua_getfield(L, 1, "ssid"); if (!lua_isnil(L, -1)) { if( lua_isstring(L, -1) ) { const char *ssid = luaL_checklstring( L, -1, &len ); if(len >= 32) return luaL_error( L, "ssid: <32" ); strncpy(wNetConfig.wifi_ssid,ssid,len); } else return luaL_error( L, "wrong arg type: ssid" ); } else return luaL_error( L, "arg: ssid needed" ); //pwd lua_getfield(L, 1, "pwd"); if (!lua_isnil(L, -1)) { if( lua_isstring(L, -1) ) { const char *pwd = luaL_checklstring( L, -1, &len ); if (len >= 64) return luaL_error( L, "pwd: <64" ); if (len > 0) strncpy(wNetConfig.wifi_key, pwd, len); else strcpy(wNetConfig.wifi_key, ""); } else return luaL_error( L, "wrong arg type: pwd" ); } else return luaL_error( L, "arg: pwd needed" ); //ip lua_getfield(L, 1, "ip"); if (!lua_isnil(L, -1)) { if( lua_isstring(L, -1) ) { const char *ip = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "ip: <16" ); if (is_valid_ip(ip) == false) return luaL_error( L, "ip invalid" ); strncpy(wNetConfig.local_ip_addr, ip, len); } else return luaL_error( L, "wrong arg type: ip" ); } else { strcpy(wNetConfig.local_ip_addr, "11.11.11.1"); } //netmask lua_getfield(L, 1, "netmask"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *netmask = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "netmask: <16" ); if (is_valid_ip(netmask) == false) return luaL_error( L, "netmask invalid" ); strncpy(wNetConfig.net_mask,netmask,len); } else return luaL_error( L, "wrong arg type: netmask" ); } else { strcpy(wNetConfig.net_mask, "255.255.255.0"); } //gateway lua_getfield(L, 1, "gateway"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *gateway = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "gateway: <16" ); if (is_valid_ip(gateway) == false) return luaL_error( L, "gateway invalid" ); strncpy(wNetConfig.gateway_ip_addr,gateway,len); } else return luaL_error( L, "wrong arg type: gateway" ); } else { strcpy(wNetConfig.gateway_ip_addr,"11.11.11.1"); } //dnsSrv lua_getfield(L, 1, "dnsSrv"); if (!lua_isnil(L, -1)) { if ( lua_isstring(L, -1) ) { const char *dnsSrv = luaL_checklstring( L, -1, &len ); if (len >= 16) return luaL_error( L, "dnsSrv: <16" ); if (is_valid_ip(dnsSrv) == false) return luaL_error( L, "dnsSrv invalid" ); strncpy(wNetConfig.dnsServer_ip_addr,dnsSrv,len); } else return luaL_error( L, "wrong arg type: dnsSrv" ); } else { strcpy(wNetConfig.dnsServer_ip_addr, "11.11.11.1"); } //retry_interval signed retry_interval = 0; lua_getfield(L, 1, "retry_interval"); if (!lua_isnil(L, -1)) { retry_interval = (signed)luaL_checknumber( L, -1 ); if (retry_interval < 0) return luaL_error( L, "retry_interval: >=0ms" ); if (retry_interval == 0) retry_interval = 0x7FFFFFFF; } else retry_interval = 1000; wNetConfig.wifi_retry_interval = retry_interval; //notify gL = L; if (wifi_status_changed_AP != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP); wifi_status_changed_AP = LUA_NOREF; if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 2); // copy argument (func) to the top of stack if (wifi_status_changed_AP != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP); wifi_status_changed_AP = luaL_ref(L, LUA_REGISTRYINDEX); } mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler, NULL ); //start wifi_ap_started = 0; wNetConfig.dhcpMode = DHCP_Server; wNetConfig.wifi_mode = Soft_AP; micoWlanStart(&wNetConfig); int tmo = mico_get_time(); while (wifi_ap_started == 0) { if ((mico_get_time() - tmo) > 1000) break; mico_thread_msleep(10); luaWdgReload(); } if (wifi_ap_started == 1) lua_pushboolean(L, true); else lua_pushboolean(L, false); return 1; }
int nmrp_do(struct nmrpd_args *args) { struct nmrp_pkt tx, rx; uint8_t *src, dest[6]; uint16_t len, region; char *filename; time_t beg; int i, status, ulreqs, expect, upload_ok, autoip; struct ethsock *sock; uint32_t intf_addr; void (*sigh_orig)(int); struct { struct in_addr addr; struct in_addr mask; } PACKED ipconf; if (args->op != NMRP_UPLOAD_FW) { fprintf(stderr, "Operation not implemented.\n"); return 1; } if (!mac_parse(args->mac, dest)) { fprintf(stderr, "Invalid MAC address '%s'.\n", args->mac); return 1; } if ((ipconf.mask.s_addr = inet_addr(args->ipmask)) == INADDR_NONE) { fprintf(stderr, "Invalid subnet mask '%s'.\n", args->ipmask); return 1; } if (!args->ipaddr) { autoip = true; /* The MAC of the device that was used to test this utility starts * with a4:2b:8c, hence 164 (0xa4) and 183 (0x2b + 0x8c) */ args->ipaddr = "10.164.183.252"; if (!args->ipaddr_intf) { args->ipaddr_intf = "10.164.183.253"; } } else if (args->ipaddr_intf) { autoip = true; } else { autoip = false; } if ((ipconf.addr.s_addr = inet_addr(args->ipaddr)) == INADDR_NONE) { fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr); return 1; } if (args->ipaddr_intf && (intf_addr = inet_addr(args->ipaddr_intf)) == INADDR_NONE) { fprintf(stderr, "Invalid IP address '%s'.\n", args->ipaddr_intf); return 1; } if (args->file_local && strcmp(args->file_local, "-") && access(args->file_local, R_OK) == -1) { fprintf(stderr, "Error accessing file '%s'.\n", args->file_local); return 1; } if (args->file_remote) { if (!tftp_is_valid_filename(args->file_remote)) { fprintf(stderr, "Invalid remote filename '%s'.\n", args->file_remote); return 1; } } if (args->region) { region = htons(to_region_code(args->region)); if (!region) { fprintf(stderr, "Invalid region code '%s'.\n", args->region); return 1; } } else { region = 0; } status = 1; sock = ethsock_create(args->intf, ETH_P_NMRP); if (!sock) { return 1; } gsock = sock; garp = 0; sigh_orig = signal(SIGINT, sigh); if (!autoip) { status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask); if (status <= 0) { if (!status) { fprintf(stderr, "Address %s/%s cannot be used on interface %s.\n", args->ipaddr, args->ipmask, args->intf); } goto out; } } else { if (verbosity) { printf("Adding %s to interface %s.\n", args->ipaddr_intf, args->intf); } if (ethsock_ip_add(sock, intf_addr, ipconf.mask.s_addr, &gundo) != 0) { goto out; } } if (ethsock_set_timeout(sock, args->rx_timeout)) { goto out; } src = ethsock_get_hwaddr(sock); if (!src) { goto out; } memcpy(tx.eh.ether_shost, src, 6); memcpy(tx.eh.ether_dhost, dest, 6); tx.eh.ether_type = htons(ETH_P_NMRP); msg_init(&tx.msg, NMRP_C_ADVERTISE); msg_opt_add(&tx.msg, NMRP_O_MAGIC_NO, "NTGR", 4); msg_hton(&tx.msg); i = 0; upload_ok = 0; beg = time(NULL); while (1) { printf("\rAdvertising NMRP server on %s ... %c", args->intf, spinner[i]); fflush(stdout); i = (i + 1) & 3; if (pkt_send(sock, &tx) < 0) { perror("sendto"); goto out; } status = pkt_recv(sock, &rx); if (status == 0 && memcmp(rx.eh.ether_dhost, src, 6) == 0) { break; } else if (status == 1) { goto out; } else { if ((time(NULL) - beg) >= 60) { printf("\nNo response after 60 seconds. Bailing out.\n"); goto out; } } } printf("\n"); expect = NMRP_C_CONF_REQ; ulreqs = 0; do { if (expect != NMRP_C_NONE && rx.msg.code != expect) { fprintf(stderr, "Received %s while waiting for %s!\n", msg_code_str(rx.msg.code), msg_code_str(expect)); } msg_init(&tx.msg, NMRP_C_NONE); status = 1; switch (rx.msg.code) { case NMRP_C_ADVERTISE: printf("Received NMRP advertisement from %s.\n", mac_to_str(rx.eh.ether_shost)); status = 1; goto out; case NMRP_C_CONF_REQ: tx.msg.code = NMRP_C_CONF_ACK; msg_opt_add(&tx.msg, NMRP_O_DEV_IP, &ipconf, 8); msg_opt_add(&tx.msg, NMRP_O_FW_UP, NULL, 0); #ifdef NMRPFLASH_SET_REGION if (region) { msg_opt_add(&tx.msg, NMRP_O_DEV_REGION, ®ion, 2); } #endif expect = NMRP_C_TFTP_UL_REQ; printf("Received configuration request from %s.\n", mac_to_str(rx.eh.ether_shost)); memcpy(tx.eh.ether_dhost, rx.eh.ether_shost, 6); printf("Sending configuration: ip %s, mask %s.\n", args->ipaddr, args->ipmask); memcpy(arpmac, rx.eh.ether_shost, 6); memcpy(&arpip, &ipconf.addr, sizeof(ipconf.addr)); if (ethsock_arp_add(sock, arpmac, &arpip) != 0) { goto out; } garp = 1; break; case NMRP_C_TFTP_UL_REQ: if (!upload_ok) { if (++ulreqs > 5) { printf("Bailing out after %d upload requests.\n", ulreqs); tx.msg.code = NMRP_C_CLOSE_REQ; break; } } else { if (verbosity) { printf("Ignoring extra upload request.\n"); } ethsock_set_timeout(sock, args->ul_timeout); tx.msg.code = NMRP_C_KEEP_ALIVE_REQ; break; } len = 0; filename = msg_opt_data(&rx.msg, NMRP_O_FILE_NAME, &len); if (filename) { if (!args->file_remote) { args->file_remote = filename; } printf("Received upload request: filename '%.*s'.\n", len, filename); } else if (!args->file_remote) { args->file_remote = args->file_local; printf("Received upload request with empty filename.\n"); } status = 0; if (args->tftpcmd) { printf("Executing '%s' ... \n", args->tftpcmd); setenv("IP", inet_ntoa(ipconf.addr), 1); setenv("MAC", mac_to_str(rx.eh.ether_shost), 1); setenv("NETMASK", inet_ntoa(ipconf.mask), 1); status = system(args->tftpcmd); } if (!status && args->file_local) { if (!autoip) { status = is_valid_ip(sock, &ipconf.addr, &ipconf.mask); if (status < 0) { goto out; } else if (!status) { printf("IP address of %s has changed. Please assign a " "static ip to the interface.\n", args->intf); tx.msg.code = NMRP_C_CLOSE_REQ; break; } } if (verbosity) { printf("Using remote filename '%s'.\n", args->file_remote); } if (!strcmp(args->file_local, "-")) { printf("Uploading from stdin ... "); } else { printf("Uploading %s ... ", leafname(args->file_local)); } fflush(stdout); status = tftp_put(args); } if (!status) { printf("OK\nWaiting for remote to respond.\n"); upload_ok = 1; ethsock_set_timeout(sock, args->ul_timeout); tx.msg.code = NMRP_C_KEEP_ALIVE_REQ; expect = NMRP_C_NONE; } else if (status == -2) { expect = NMRP_C_TFTP_UL_REQ; } else { goto out; } break; case NMRP_C_KEEP_ALIVE_REQ: tx.msg.code = NMRP_C_KEEP_ALIVE_ACK; ethsock_set_timeout(sock, args->ul_timeout); printf("Received keep-alive request.\n"); break; case NMRP_C_CLOSE_REQ: tx.msg.code = NMRP_C_CLOSE_ACK; break; case NMRP_C_CLOSE_ACK: status = 0; goto out; default: fprintf(stderr, "Unknown message code 0x%02x!\n", rx.msg.code); msg_dump(&rx.msg, 0); } if (tx.msg.code != NMRP_C_NONE) { msg_hton(&tx.msg); if (pkt_send(sock, &tx) < 0) { perror("sendto"); goto out; } if (tx.msg.code == NMRP_C_CLOSE_REQ) { goto out; } } if (rx.msg.code == NMRP_C_CLOSE_REQ) { printf("Remote finished. Closing connection.\n"); break; } status = pkt_recv(sock, &rx); if (status) { if (status == 2) { fprintf(stderr, "Timeout while waiting for %s.\n", msg_code_str(expect)); } goto out; } ethsock_set_timeout(sock, args->rx_timeout); } while (1); status = 0; if (ulreqs) { printf("Reboot your device now.\n"); } else { printf("No upload request received.\n"); } out: signal(SIGINT, sigh_orig); gsock = NULL; ethsock_arp_del(sock, arpmac, &arpip); ethsock_ip_del(sock, &gundo); ethsock_close(sock); return status; }
/* * Custom key pg.backends.count * * Returns statistics for connected backends (remote clients) * * Parameters: * 0: connection string * 1: connection database * 2: filter by database oid name * 3: filter by user OID or name * 4: filter by hostname or IP address of the connected host * 5: return only waiting backends * * Returns: u */ int PG_BACKENDS_COUNT(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "PG_BACKENDS_COUNT"; // Function name for log file char query[MAX_QUERY_LEN]; char *p = &query[0]; int i = 0; char *param = NULL; char *clause = PG_WHERE; PGparams pgparams = NULL; int pgi = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); // Build the sql query memset(query, 0, MAX_QUERY_LEN); zbx_strlcpy(p, PGSQL_GET_BACKENDS, MAX_QUERY_LEN); p += strlen(p); // iterate over the available parameters for(i = 0; i < 4; i++) { param = get_rparam(request, PARAM_FIRST + i); if(!strisnull(param)) { switch(i) { case 0: // <database> pgparams = param_append(pgparams, param); if(is_oid(param)) zbx_snprintf(p, MAX_CLAUSE_LEN, " %s datid=$%i", clause, ++pgi); else zbx_snprintf(p, MAX_CLAUSE_LEN, " %s datname=$%i", clause, ++pgi); break; case 1: // <user> pgparams = param_append(pgparams, param); if(is_oid(param)) zbx_snprintf(p, MAX_CLAUSE_LEN, " %s usesysid=$%i", clause, ++pgi); else zbx_snprintf(p, MAX_CLAUSE_LEN, " %s usename=$%i", clause, ++pgi); break; case 2: // <client> pgparams = param_append(pgparams, param); if(is_valid_ip(param)) zbx_snprintf(p, MAX_CLAUSE_LEN, " %s client_addr = $%i::inet", clause, ++pgi); else // requires v9.1+ zbx_snprintf(p, MAX_CLAUSE_LEN, " %s client_hostname=$%i", clause, ++pgi); break; case 3: // <waiting> if(0 == strncmp("true", param, 4)) { zbx_snprintf(p, MAX_CLAUSE_LEN, " %s waiting=TRUE", clause); } else if(0 == strncmp("false", param, 5)) { zbx_snprintf(p, MAX_CLAUSE_LEN, " %s waiting=FALSE", clause); } else { zabbix_log(LOG_LEVEL_ERR, "Unsupported 'Waiting' parameter: %s in %s()", param, __function_name); goto out; } break; } p += strlen(p); clause = PG_AND; } } ret = pg_get_int(request, result, query, pgparams); out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return ret; }
/*cfg={} cfg.ssid="" cfg.pwd="" cfg.ip (optional,default:11.11.11.1) cfg.netmask(optional,default:255.255.255.0) cfg.gateway(optional,default:11.11.11.1) cfg.dnsSrv(optional,default:11.11.11.1) cfg.retry_interval(optional,default:1000ms) wifi.startap(cfg,function(optional))*/ static int lwifi_startap( lua_State* L ) {//4 stations Max network_InitTypeDef_st wNetConfig; size_t len=0; memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st)); if (!lua_istable(L, 1)) return luaL_error( L, "table arg needed" ); //ssid lua_getfield(L, 1, "ssid"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the string { const char *ssid = luaL_checklstring( L, -1, &len ); if(len>32) return luaL_error( L, "ssid:<32" ); strncpy(wNetConfig.wifi_ssid,ssid,len); } else return luaL_error( L, "wrong arg type:ssid" ); } else return luaL_error( L, "arg: ssid needed" ); //pwd lua_getfield(L, 1, "pwd"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the string { const char *pwd = luaL_checklstring( L, -1, &len ); if(len>64) return luaL_error( L, "pwd:<64" ); if(len>0) strncpy(wNetConfig.wifi_key,pwd,len); else strcpy(wNetConfig.wifi_key,""); } else return luaL_error( L, "wrong arg type:pwd" ); } else return luaL_error( L, "arg: pwd needed" ); //ip lua_getfield(L, 1, "ip"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *ip = luaL_checklstring( L, -1, &len ); if(len>16) return luaL_error( L, "ip:<16" ); if(is_valid_ip(ip)==false) return luaL_error( L, "ip invalid" ); strncpy(wNetConfig.local_ip_addr,ip,len); } else return luaL_error( L, "wrong arg type:ip" ); } else { strcpy(wNetConfig.local_ip_addr,"11.11.11.1"); //return luaL_error( L, "arg: ip needed" ); } //netmask lua_getfield(L, 1, "netmask"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *netmask = luaL_checklstring( L, -1, &len ); if(len>16) return luaL_error( L, "netmask:<16" ); if(is_valid_ip(netmask)==false) return luaL_error( L, "netmask invalid" ); strncpy(wNetConfig.net_mask,netmask,len); } else return luaL_error( L, "wrong arg type:netmask" ); } else { strcpy(wNetConfig.net_mask,"255.255.255.0"); //return luaL_error( L, "arg: netmask needed" ); } //gateway lua_getfield(L, 1, "gateway"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *gateway = luaL_checklstring( L, -1, &len ); if(len>16) return luaL_error( L, "gateway:<16" ); if(is_valid_ip(gateway)==false) return luaL_error( L, "gateway invalid" ); strncpy(wNetConfig.gateway_ip_addr,gateway,len); } else return luaL_error( L, "wrong arg type:gateway" ); } else { strcpy(wNetConfig.gateway_ip_addr,"11.11.11.1"); // return luaL_error( L, "arg: gateway needed" ); } //dnsSrv lua_getfield(L, 1, "dnsSrv"); if (!lua_isnil(L, -1)){ /* found? */ if( lua_isstring(L, -1) ) // deal with the ssid string { const char *dnsSrv = luaL_checklstring( L, -1, &len ); if(len>16) return luaL_error( L, "dnsSrv:<16" ); if(is_valid_ip(dnsSrv)==false) return luaL_error( L, "dnsSrv invalid" ); strncpy(wNetConfig.dnsServer_ip_addr,dnsSrv,len); } else return luaL_error( L, "wrong arg type:dnsSrv" ); } else { strcpy(wNetConfig.dnsServer_ip_addr,"11.11.11.1"); //return luaL_error( L, "arg: dnsSrv needed" ); } //retry_interval signed retry_interval=0; lua_getfield(L, 1, "retry_interval"); if (!lua_isnil(L, -1)){ /* found? */ retry_interval= luaL_checknumber( L, -1 ); if(retry_interval<=0) return luaL_error( L, "retry_interval:>0ms" ); } else retry_interval = 1000; wNetConfig.wifi_retry_interval = retry_interval; /*MCU_DBG("wifi_ssid:%s\r\n",wNetConfig.wifi_ssid); MCU_DBG("wifi_key:%s\r\n",wNetConfig.wifi_key); MCU_DBG("local_ip_addr:%s\r\n",wNetConfig.local_ip_addr); MCU_DBG("net_mask:%s\r\n",wNetConfig.net_mask); MCU_DBG("gateway_ip_addr:%s\r\n",wNetConfig.gateway_ip_addr); MCU_DBG("dnsServer_ip_addr:%s\r\n",wNetConfig.dnsServer_ip_addr); MCU_DBG("wifi_retry_interval:%d\r\n",wNetConfig.wifi_retry_interval);*/ //notify gL = L; if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) { lua_pushvalue(L, 2); // copy argument (func) to the top of stack if(wifi_status_changed_AP != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP); wifi_status_changed_AP = luaL_ref(L, LUA_REGISTRYINDEX); MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler ); } else { if(wifi_status_changed_AP != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP); wifi_status_changed_AP = LUA_NOREF; } //start wNetConfig.dhcpMode = DHCP_Server; wNetConfig.wifi_mode = Soft_AP; micoWlanStart(&wNetConfig); return 0; }
int main(int argc, char *argv[]) { //setHttpAuth("admin:admin"); //setHttpData("{\"ins_api\":{\"version\":\"1.2\",\"type\":\"cli_show\",\"chunk\":\"0\",\"sid\":\"1\",\"input\":\"show version\",\"output_format\":\"json\"}}"); //appendHttpHeader("Content-Type: application/json"); //printf("%s\n",httpFunction("http://192.168.113.50/ins")); OV_Version = OV200; char *oneViewAddress; //main2(); //sleep(40); // Check for Debug Mode if (getenv("OV_DEBUG")) { debug = 1; // debug mode enabled } else { debug = 0; // debug mode disabled } // Peform an initial check to see what parameters have been passed char path[100]; if (argc >1) { oneViewAddress = argv[1]; if (is_valid_ip(oneViewAddress)) { // Create a string based upon the path to the sessionID identifySystem(oneViewAddress); sprintf(path, "/.%s_ov",oneViewAddress); } else { printMessage(YELLOW, "DEBUG", "Invalid IP Address"); return 1; } } if(argc < 3) { printMessage(YELLOW, "DEBUG", "No parameters passed"); fprintf(stderr, "usage: %s ADDRESS COMMAND <parameters>\n", argv[0]); // Somewhat over the top logo fprintf(stderr, " \ _ _ ____ ___ __ ___\n\ | | | | _ \\ / _ \\ _ __ __\\ \\ / (_) _____ __\n\ | |_| | |_) | | | | | '_ \\ / _ \\ \\ / /| |/ _ \\ \\ /\\ / /\n\ | _ | __/ | |_| | | | | __/\\ V / | | __/\\ V V /\n\ |_| |_|_| \\___/|_| |_|\\___| \\_/ |_|\\___| \\_/\\_/ \n\n"); //Print the relevant helps ovCreatePrintHelp(); ovShowPrintHelp(); ovCopyPrintHelp(); ovMessageBusHelp(); return 1; } char url[URL_SIZE]; char *httpData; json_t *root = NULL; json_error_t error; // Determine the action to be executed if (stringMatch(argv[2], "LOGIN")) { // Login to HP OneView SetHttpMethod(DCHTTPPOST); ovLogin(argv, path); return 0; } else if (stringMatch(argv[2], "SHOW")) { // Show/Query information from HP OneView char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID\n"); return 1; } ovShow(sessionID, argc, argv); return 0; // return sucess } else if (stringMatch(argv[2], "CREATE")) { char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID\n"); return 1; } ovCreate(sessionID, argv); return 0; // Return success } else if (strstr(argv[2], "COPY")) { char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID\n"); return 1; } ovCopy(sessionID, argv); return 0; //return success } else if (strstr(argv[2], "CLONE")) { char *sessionID = readSessionIDforHost(path); if (!sessionID) { printf("[ERROR] No session ID"); return 1; } // DEBUG OVID output //printf("[DEBUG] OVID:\t %s\n",sessionID); if (strstr(argv[3], "SERVER-PROFILES")) { snprintf(url, URL_SIZE, URL_FORMAT, argv[1], "server-profiles"); // Call to HP OneView API httpData = getRequestWithUrlAndHeader(url, sessionID); if(!httpData) return 1; //printf("[DEBUG] JSON: %s\n", httpData); root = json_loads(httpData, 0, &error); // Find Server Profile first //int fieldCount = argc -5; //argv[0] is the path to the program json_t *memberArray = json_object_get(root, "members"); if (json_array_size(memberArray) != 0) { size_t index; json_t *serverProfile = NULL; //char *json_text; json_array_foreach(memberArray, index, serverProfile) { const char *uri = json_string_value(json_object_get(serverProfile, "uri")); char *cloneuri = argv[4]; printf ("%s / %s /n", cloneuri, url); if (uri != NULL) { if (stringMatch(cloneuri, (char *)uri)) { //json_text = json_dumps(serverProfile, JSON_INDENT(4)); //4 is close to a tab //printf("%s\n", json_text); // json_object_del(serverProfile, "uri"); json_object_del(serverProfile, "serialNumber"); json_object_del(serverProfile, "uuid"); json_object_del(serverProfile, "taskUri"); json_object_del(serverProfile, "status"); json_object_del(serverProfile, "inProgress"); json_object_del(serverProfile, "modified"); json_object_del(serverProfile, "eTag"); json_object_del(serverProfile, "created"); json_object_del(serverProfile, "serverHardwareUri"); json_object_del(serverProfile, "enclosureBay"); json_object_del(serverProfile, "enclosureUri"); //json_object_del(serverProfile, "connections"); json_t *connections, *connectionsArray= json_object_get(serverProfile, "connections"); json_array_foreach(connectionsArray, index, connections) { json_object_del(connections, "mac"); json_object_del(connections, "wwnn"); json_object_del(connections, "wwpn"); } int profileCount = atoi(argv[5]); if (profileCount != 0){ char name[100]; //strcat(name, json_string_value(json_object_get(serverProfile, "name"))); //strcat(name, ) //const char *profileName = json_string_value(json_object_get(serverProfile, "name")); char profileName[100]; strcpy(profileName, json_string_value(json_object_get(serverProfile, "name"))); for (int i =0; i <profileCount; i++) { sprintf(name, "%s_%d",profileName, i); printf("%s\n", name); json_string_set( json_object_get(serverProfile, "name"), name); httpData = postRequestWithUrlAndDataAndHeader(url, json_dumps(serverProfile, JSON_ENSURE_ASCII), sessionID); if(!httpData) return 1; } } //printf("%s\n", json_string_value(json_object_get(serverProfile, "name"))); //json_dumps(root, JSON_ENSURE_ASCII) /* httpData = postRequestWithUrlAndDataAndHeader(url, json_dumps(serverProfile, JSON_ENSURE_ASCII), sessionID); if(!httpData) return 1; //free(json_text); json_text = json_dumps(serverProfile, JSON_INDENT(4)); //4 is close to a tab printf("%s\n", json_text); free(json_text); */ } }