int hostnameEdit(const char *option2) { struct uci_context *c; struct uci_ptr p; int length2= strlen("system.@system[0].hostname=")+strlen(option2)+1; char *hostname = safe_malloc(length2); strcpy(hostname,"system.@system[0].hostname="); strcat(hostname,option2); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, hostname, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } uci_set(c,&p); uci_save(c, p.p); uci_commit(c, &p.p, false); uci_free_context (c); free(hostname); return(0); }
uint32_t system_helpers_get_nodeid() { char *opt; struct uci_ptr uciptr; int retval; long node_id; struct uci_context *ctx = uci_alloc_context(); if(ctx == NULL) return 0; opt = malloc(strlen(NODEIDPATH)+1); strcpy(opt, NODEIDPATH); memset(&uciptr, 0, sizeof(uciptr)); retval = uci_lookup_ptr(ctx, &uciptr, opt, true); if (retval != UCI_OK || uciptr.o == NULL) { free(opt); uci_free_context(ctx); return 0; } node_id = atol(uciptr.o->v.string); free(opt); uci_free_context(ctx); if (node_id <= 0 || node_id > UINT32_MAX) return 0; return (uint32_t) node_id; }
static void swconfig_load_uci(struct switch_dev *dev, const char *name) { struct uci_context *ctx; struct uci_package *p = NULL; struct uci_element *e; int ret = -1; ctx = uci_alloc_context(); if (!ctx) return; uci_load(ctx, name, &p); if (!p) { uci_perror(ctx, "Failed to load config file: "); goto out; } ret = swlib_apply_from_uci(dev, p); if (ret < 0) fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name); out: uci_free_context(ctx); exit(ret); }
bool config_init(void) { if (conf.uci_ctx != NULL) { uci_free_context(conf.uci_ctx); } return (conf.uci_ctx = uci_alloc_context()) ? true : false; }
static struct json_object * get_hostname(void) { struct json_object *ret = NULL; struct uci_context *ctx = uci_alloc_context(); ctx->flags &= ~UCI_FLAG_STRICT; char section[] = "system.@system[0]"; struct uci_ptr ptr; if (uci_lookup_ptr(ctx, &ptr, section, true)) goto error; struct uci_section *s = ptr.s; const char *hostname = uci_lookup_option_string(ctx, s, "pretty_hostname"); if (!hostname) hostname = uci_lookup_option_string(ctx, s, "hostname"); ret = gluonutil_wrap_string(hostname); error: uci_free_context(ctx); return ret; }
int ssidEdit(const char *option1) { struct uci_context *c; struct uci_ptr p; int length1= strlen("wireless.@wifi-iface[0].ssid=")+strlen(option1)+1; char *ssid = safe_malloc(length1); strcpy(ssid,"wireless.@wifi-iface[0].ssid="); strcat(ssid,option1); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } uci_set(c,&p); uci_save(c, p.p); uci_commit(c, &p.p, false); uci_free_context (c); free(ssid); return(0); }
/*############################## Functions ###################################*/ int check_internet_status(void) { p_debug("access get_wwanip\n"); char value[64]= "\0"; char uci_option_str[64]= "\0"; struct ifreq ifr_wlan1; int inet_sock_wlan1; int ret = 0; ctx=uci_alloc_context(); memset(uci_option_str, '\0', 64); memset(value, '\0', 64); strcpy(uci_option_str, "network.wan.workmode"); uci_get_option_value(uci_option_str, value); uci_free_context(ctx); p_debug("value = %s\n",value); if(!strcmp(value, "0")) strcpy(ifr_wlan1.ifr_name, "eth0.1"); else if(!strcmp(value, "1")) strcpy(ifr_wlan1.ifr_name, "apcli0"); else if(!strcmp(value, "2")) strcpy(ifr_wlan1.ifr_name, "ppp0"); else{ p_debug("unknown workmode\n"); return -1; } inet_sock_wlan1 = socket(AF_INET, SOCK_DGRAM, 0); if(-1 == inet_sock_wlan1) { p_debug("creat socket error!\n"); ret = -1; } if (ioctl(inet_sock_wlan1, SIOCGIFADDR, &ifr_wlan1) < 0) { p_debug("get no wan ip!\n"); ret = 0; }else { p_debug("get wan ip success!\n"); ret = 1; } if(-1 != inet_sock_wlan1) close(inet_sock_wlan1); return ret; }
char * ssidRead() { struct uci_context *c; struct uci_ptr p; char *ssid = safe_strdup ("wireless.@wifi-iface[0].ssid"); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } char *str = strdup(p.o->v.string); uci_free_context (c); free(ssid); return str; }
char * hostnameRead() { struct uci_context *c; struct uci_ptr p; char *ssid = strdup ("system.@system[0].hostname"); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, ssid, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } char *str = safe_strdup(p.o->v.string); uci_free_context (c); free(ssid); return str; }
char * urlRead() { struct uci_context *c; struct uci_ptr p; char *url = safe_strdup ("smartwifi.@smartwifi[0].imageurl"); c = uci_alloc_context(); if (uci_lookup_ptr (c, &p, url, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } char *str = strdup(p.o->v.string); uci_free_context (c); free(url); return str; }
int main(int argc, char **argv) { int opt, rc = 0; while ((opt = getopt(argc, argv, "hv")) != -1) { switch (opt) { case 'v': conf.verbosity++; break; case 'h': default: return usage(argv[0]); } } conf.flx_ufd.fd = open(FLX_DEV, O_RDWR); if (conf.flx_ufd.fd < 0) { perror(FLX_DEV); rc = 1; goto finish; } if (!configure_tty(conf.flx_ufd.fd)) { fprintf(stderr, "%s: Failed to configure tty params\n", FLX_DEV); rc = 2; goto finish; } if (!config_init()) { rc = 3; goto oom; } if (!config_load_all()) { rc = 4; goto finish; } conf.ubus_ctx = ubus_connect(NULL); if (!conf.ubus_ctx) { fprintf(stderr, "Failed to connect to ubus\n"); rc = 5; goto finish; } #ifdef WITH_YKW conf.ykw = ykw_new(YKW_DEFAULT_THETA); if (conf.ykw == NULL) { rc = 6; goto oom; } #endif mosquitto_lib_init(); snprintf(conf.mqtt.id, MQTT_ID_LEN, MQTT_ID_TPL, getpid()); conf.mosq = mosquitto_new(conf.mqtt.id, conf.mqtt.clean_session, &conf); if (!conf.mosq) { switch (errno) { case ENOMEM: rc = 7; goto oom; case EINVAL: fprintf(stderr, "mosq_new: Invalid id and/or clean_session.\n"); rc = 8; goto finish; } } rc = mosquitto_loop_start(conf.mosq); switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_loop_start: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_NOT_SUPPORTED: fprintf(stderr, "mosq_loop_start: No threading support.\n"); goto finish; }; rc = mosquitto_connect_async(conf.mosq, conf.mqtt.host, conf.mqtt.port, conf.mqtt.keepalive); switch (rc) { case MOSQ_ERR_INVAL: fprintf(stderr, "mosq_connect_async: Invalid input parameters.\n"); goto finish; case MOSQ_ERR_ERRNO: perror("mosq_connect_async"); goto finish; } uloop_init(); uloop_fd_add(&conf.flx_ufd, ULOOP_READ); uloop_timeout_set(&conf.timeout, CONFIG_ULOOP_TIMEOUT); ubus_add_uloop(conf.ubus_ctx); ubus_register_event_handler(conf.ubus_ctx, &conf.ubus_ev_sighup, CONFIG_UBUS_EV_SIGHUP); ubus_register_event_handler(conf.ubus_ctx, &conf.ubus_ev_shift_calc, CONFIG_UBUS_EV_SHIFT_CALC); uloop_run(); uloop_done(); goto finish; oom: fprintf(stderr, "error: Out of memory.\n"); finish: mosquitto_disconnect(conf.mosq); mosquitto_loop_stop(conf.mosq, false); mosquitto_destroy(conf.mosq); mosquitto_lib_cleanup(); ykw_free(conf.ykw); if (conf.ubus_ctx != NULL) { ubus_free(conf.ubus_ctx); } close(conf.flx_ufd.fd); uci_free_context(conf.uci_ctx); return rc; }
void ucix_cleanup(struct uci_context *ctx) { uci_free_context(ctx); }
int main(int argc, char *argv[]) { const char *lengthStr = getenv("CONTENT_LENGTH"); if (!lengthStr) { badRequestResponse("Request Invaild"); return EXIT_SUCCESS; } const size_t length = atoi(lengthStr); char content[MAX_CONTENT]; fgets(content, min(length + 1, MAX_CONTENT), stdin); json_object *json = json_tokener_parse(content); UCIContext *ctx = uci_alloc_context(); if (!json) { badRequestResponse("Invaild JSON"); goto fail; } json_object *value = NULL; json_bool ret = json_object_object_get_ex(json, "mode", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *mode = json_object_get_string(value); if (!strcmp(mode, "static")) { ret = json_object_object_get_ex(json, "ip", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *ip = json_object_get_string(value); if (!isVaildIP(ip)) { badRequestResponse("Invaild Argument: ip"); goto fail; } ret = json_object_object_get_ex(json, "netmask", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *netmask = json_object_get_string(value); if (!isVaildIP(netmask)) { badRequestResponse("Invaild Argument: netmask"); goto fail; } ret = json_object_object_get_ex(json, "gateway", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *gateway = json_object_get_string(value); if (!isVaildIP(gateway)) { badRequestResponse("Invaild Argument: gateway"); goto fail; } ret = json_object_object_get_ex(json, "dnsA", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *dnsA = json_object_get_string(value); if (!isVaildIP(dnsA)) { badRequestResponse("Invaild Argument: dnsA"); goto fail; } ret = json_object_object_get_ex(json, "dnsB", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *dnsB = json_object_get_string(value); if (!isVaildIP(dnsB)) { badRequestResponse("Invaild Argument: dnsB"); goto fail; } const char *commands[] = { "network.lan.proto", "network.lan.ipaddr", "network.lan.netmask", "network.lan.gateway", "network.lan.dns", NULL }; const char *values[] = { mode, ip, netmask, gateway, dnsA, NULL }; if (statusSetter(ctx, commands, values) < 0 || uci(ctx, UCI_COMMIT, "network.lan")) goto fail; } else if (!strcmp(mode, "dhcp")) { const char *commands[] = { "network.lan.proto", NULL }; const char *values[] = { mode, NULL }; if (statusSetter(ctx, commands, values) < 0 || uci(ctx, UCI_COMMIT, "network.lan")) goto fail; } else if (!strcmp(mode, "pppoe")) { ret = json_object_object_get_ex(json, "username", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *username = json_object_get_string(value); if (strlen(username) > MAX_CONTENT) { badRequestResponse("Invaild Argument: username"); goto fail; } ret = json_object_object_get_ex(json, "password", &value); if (!ret) { badRequestResponse("Incomplete Arguments"); goto fail; } const char *password = json_object_get_string(value); if (strlen(password) > MAX_CONTENT) { badRequestResponse("Invaild Argument: password"); goto fail; } const char *commands[] = { "network.lan.proto", "network.wan.username", "network.wan.password", NULL }; const char *values[] = { mode, username, password, NULL }; if (statusSetter(ctx, commands, values) < 0 || uci(ctx, UCI_COMMIT, "network.wan")) goto fail; } else { badRequestResponse("Invaild Argument: mode"); goto fail; } printf("Content-Type:application/json\n\n"); fflush(stdout); fail: uci_free_context(ctx); json_object_put(json); return EXIT_SUCCESS; }
/** * Nodewatcher agent entry point. */ int main(int argc, char **argv) { struct stat s; const char *ubus_socket = NULL; int log_option = 0; int c; while ((c = getopt(argc, argv, "s:")) != -1) { switch (c) { case 's': ubus_socket = optarg; break; case 'f': log_option |= LOG_PERROR; break; default: break; } } /* Open the syslog facility */ openlog("nw-agent", log_option, LOG_DAEMON); /* Create directory for temporary run files */ if (stat("/var/run/nodewatcher-agent", &s)) mkdir("/var/run/nodewatcher-agent", 0700); umask(0077); /* Setup signal handlers */ signal(SIGPIPE, SIG_IGN); /* TODO: Handle SIGHUP to reload? */ /* Seed random generator */ unsigned int seed; int rc = nw_read_random_bytes(&seed, sizeof(seed)); if (rc < 0) { fprintf(stderr, "ERROR: Failed to seed random generator!\n"); return -1; } srandom(seed); /* Initialize event loop */ uloop_init(); /* Attempt to establish connection to ubus daemon */ for (;;) { ubus = ubus_connect(ubus_socket); if (!ubus) { syslog(LOG_WARNING, "Failed to connect to ubus!"); sleep(10); continue; } break; } ubus_add_uloop(ubus); /* Initialize UCI context */ uci = uci_alloc_context(); if (!uci) { syslog(LOG_ERR, "Failed to initialize UCI!"); return -1; } /* Discover and initialize modules */ if (nw_module_init(ubus, uci) != 0) { syslog(LOG_ERR, "Unable to initialize modules!"); return -1; } /* Initialize the scheduler */ if (nw_scheduler_init() != 0) { syslog(LOG_ERR, "Unable to initialize scheduler!"); return -1; } /* Initialize the output exporter */ if (nw_output_init(uci) != 0) { syslog(LOG_ERR, "Unable to initialize output exporter!"); return -1; } /* Enter the event loop */ uloop_run(); ubus_free(ubus); uci_free_context(uci); uloop_done(); return 0; }