static void mat_loop_cb(struct uloop_timeout *timeout) { void *c, *t; char buf[16]; int i; if(sys->tbus_ctx->sock.fd >= 0){ refresh_arp_table(); update_mat_table(); blob_buf_init(&traffic_b, 0); blobmsg_add_string(&traffic_b, "hw", sys->ap_hw); c = blobmsg_open_array(&traffic_b, "mat"); for (i = 0; i < arp->idx; i++) { t = blobmsg_open_table(&traffic_b, "arp"); blobmsg_add_string(&traffic_b, "sta", arp->arps[i].hw); if(arp->arps[i].ip){ sprintf(buf, "%u.%u.%u.%u", NIPQUAD(arp->arps[i].ip)); blobmsg_add_string(&traffic_b, "ip", buf); } blobmsg_close_table(&traffic_b, t); } blobmsg_close_array(&traffic_b, c); ubus_send_event(sys->tbus_ctx, sys->cfg.tbus_listen_event, traffic_b.head); D(SYSTEM, "send msg and wait %d(s)\n", TRAFFICD_WIFIAP_LOOP_TIME / 1000); }else{ D(SYSTEM, "ctx->sock.fd[%d]\n", sys->tbus_ctx->sock.fd); } uloop_timeout_set(timeout, TRAFFICD_WIFIAP_LOOP_TIME); }
// ubus call test_ubus helloworld '{"id":1,"msg":"hello","array":["a","b"]}' static int test_hello(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_attr *tb[__HELLO_MAX]; int tmp_id; char *tmp_msg = NULL; int len; struct blob_attr *attr; void *arr; blobmsg_parse(hello_policy, __HELLO_MAX, tb, blob_data(msg), blob_len(msg)); blob_buf_init(&b, 0); if(tb[HELLO_ID]) { tmp_id = blobmsg_get_u32(tb[HELLO_ID]); blobmsg_add_u32(&b, "id", tmp_id); } if(tb[HELLO_MSG]) { tmp_msg = blobmsg_get_string(tb[HELLO_MSG]); blobmsg_add_string(&b, "msg", tmp_msg); } if(tb[HELLO_ARRAY] && blobmsg_type(tb[HELLO_ARRAY]) == BLOBMSG_TYPE_ARRAY) { arr = blobmsg_open_array(&b, "array"); len = blobmsg_data_len(tb[HELLO_ARRAY]); __blob_for_each_attr(attr, blobmsg_data(tb[HELLO_ARRAY]), len) { if (blobmsg_type(attr) == BLOBMSG_TYPE_STRING) { char *tmp = blobmsg_get_string(attr); blobmsg_add_blob(&b, attr); printf("array=%s\n", tmp); } } blobmsg_close_array(&b, arr); }
int igmp_rpc(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { struct blob_buf bb; IGMPtable table[MAX_IGMP_ENTRY]; FILE *snptable; char line[256]; int idx = 0; void *t, *a; if ((snptable = fopen("/proc/net/igmp_snooping", "r"))) { while(fgets(line, sizeof(line), snptable) != NULL) { remove_newline(line); table[idx].exists = false; if(sscanf(single_space(line),"%s %s %s %s %x %x %x %s %x %x %x %d %x %d", table[idx].bridge, table[idx].device, table[idx].srcdev, table[idx].tags, &(table[idx].lantci), &(table[idx].wantci), &(table[idx].group), table[idx].mode, &(table[idx].RxGroup), &(table[idx].source), &(table[idx].reporter), &(table[idx].timeout), &(table[idx].Index), &(table[idx].ExcludPt)) == 14) { table[idx].exists = true; idx++; } } fclose(snptable); } else return UBUS_STATUS_NOT_FOUND; blob_buf_init(&bb, 0); a = blobmsg_open_array(&bb, "table"); for (idx = 0; idx < MAX_IGMP_ENTRY; idx++) { if (!table[idx].exists) break; t = blobmsg_open_table(&bb, NULL); blobmsg_add_string(&bb,"bridge", table[idx].bridge); blobmsg_add_string(&bb,"device", table[idx].device); blobmsg_add_string(&bb,"srcdev", table[idx].srcdev); blobmsg_add_string(&bb,"tags", table[idx].tags); blobmsg_add_u32(&bb,"lantci", table[idx].lantci); blobmsg_add_u32(&bb,"wantci", table[idx].wantci); blobmsg_add_string(&bb,"group", convert_to_ipaddr(table[idx].group)); blobmsg_add_string(&bb,"mode", table[idx].mode); blobmsg_add_string(&bb,"rxgroup", convert_to_ipaddr(table[idx].RxGroup)); blobmsg_add_string(&bb,"source", convert_to_ipaddr(table[idx].source)); blobmsg_add_string(&bb,"reporter", convert_to_ipaddr(table[idx].reporter)); blobmsg_add_u32(&bb,"timeout", table[idx].timeout); blobmsg_add_u32(&bb,"index", table[idx].Index); blobmsg_add_u32(&bb,"excludpt", table[idx].ExcludPt); blobmsg_close_table(&bb, t); } blobmsg_close_array(&bb, a); ubus_send_reply(ctx, req, bb.head); return 0; }
static int backup_finish_list(struct blob_buf *blob, int status, void *priv) { struct backup_state *s = priv; if (!s->open) return UBUS_STATUS_NO_DATA; blobmsg_close_array(blob, s->array); return UBUS_STATUS_OK; }
static int system_info(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { void *c; time_t now; struct tm *tm; struct sysinfo info; now = time(NULL); if (!(tm = localtime(&now))) return UBUS_STATUS_UNKNOWN_ERROR; if (sysinfo(&info)) return UBUS_STATUS_UNKNOWN_ERROR; blob_buf_init(&b, 0); blobmsg_add_u32(&b, "uptime", info.uptime); blobmsg_add_u32(&b, "localtime", mktime(tm)); c = blobmsg_open_array(&b, "load"); blobmsg_add_u32(&b, NULL, info.loads[0]); blobmsg_add_u32(&b, NULL, info.loads[1]); blobmsg_add_u32(&b, NULL, info.loads[2]); blobmsg_close_array(&b, c); c = blobmsg_open_table(&b, "memory"); blobmsg_add_u64(&b, "total", info.mem_unit * info.totalram); blobmsg_add_u64(&b, "free", info.mem_unit * info.freeram); blobmsg_add_u64(&b, "shared", info.mem_unit * info.sharedram); blobmsg_add_u64(&b, "buffered", info.mem_unit * info.bufferram); blobmsg_close_table(&b, c); c = blobmsg_open_table(&b, "swap"); blobmsg_add_u64(&b, "total", info.mem_unit * info.totalswap); blobmsg_add_u64(&b, "free", info.mem_unit * info.freeswap); blobmsg_close_table(&b, c); ubus_send_reply(ctx, req, b.head); return UBUS_STATUS_OK; }
static void print_syscalls(int policy, const char *json) { void *c; int i; set_syscall("rt_sigaction", 1); set_syscall("sigreturn", 1); set_syscall("rt_sigreturn", 1); set_syscall("exit_group", 1); set_syscall("exit", 1); blob_buf_init(&b, 0); c = blobmsg_open_array(&b, "whitelist"); for (i = 0; i < ARRAY_SIZE(syscall_names); i++) { if (!syscall_count[i]) continue; if (syscall_names[i]) { if (debug) printf("syscall %d (%s) was called %d times\n", i, syscall_names[i], syscall_count[i]); blobmsg_add_string(&b, NULL, syscall_names[i]); } else { ERROR("no name found for syscall(%d)\n", i); } } blobmsg_close_array(&b, c); blobmsg_add_u32(&b, "policy", policy); if (json) { FILE *fp = fopen(json, "w"); if (fp) { fprintf(fp, "%s", blobmsg_format_json_indent(b.head, true, 0)); fclose(fp); INFO("saving syscall trace to %s\n", json); } else { ERROR("failed to open %s\n", json); } } else { printf("%s\n", blobmsg_format_json_indent(b.head, true, 0)); } }
static int system_board(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { void *c; char line[256]; char *key, *val, *next; struct utsname utsname; FILE *f; blob_buf_init(&b, 0); if (uname(&utsname) >= 0) { blobmsg_add_string(&b, "kernel", utsname.release); blobmsg_add_string(&b, "hostname", utsname.nodename); } if ((f = fopen("/proc/cpuinfo", "r")) != NULL) { while(fgets(line, sizeof(line), f)) { key = strtok(line, "\t:"); val = strtok(NULL, "\t\n"); if (!key || !val) continue; if (!strcasecmp(key, "system type") || !strcasecmp(key, "processor") || !strcasecmp(key, "model name")) { strtoul(val + 2, &key, 0); if (key == (val + 2) || *key != 0) { blobmsg_add_string(&b, "system", val + 2); break; } } } fclose(f); } if ((f = fopen("/tmp/sysinfo/model", "r")) != NULL || (f = fopen("/proc/device-tree/model", "r")) != NULL) { if (fgets(line, sizeof(line), f)) { val = strtok(line, "\t\n"); if (val) blobmsg_add_string(&b, "model", val); } fclose(f); } else if ((f = fopen("/proc/cpuinfo", "r")) != NULL) { while(fgets(line, sizeof(line), f)) { key = strtok(line, "\t:"); val = strtok(NULL, "\t\n"); if (!key || !val) continue; if (!strcasecmp(key, "machine") || !strcasecmp(key, "hardware")) { blobmsg_add_string(&b, "model", val + 2); break; } } fclose(f); } if ((f = fopen("/etc/openwrt_release", "r")) != NULL) { c = blobmsg_open_table(&b, "release"); while (fgets(line, sizeof(line), f)) { char *dest; char ch; key = line; val = strchr(line, '='); if (!val) continue; *(val++) = 0; if (!strcasecmp(key, "DISTRIB_ID")) key = "distribution"; else if (!strcasecmp(key, "DISTRIB_RELEASE")) key = "version"; else if (!strcasecmp(key, "DISTRIB_REVISION")) key = "revision"; else if (!strcasecmp(key, "DISTRIB_CODENAME")) key = "codename"; else if (!strcasecmp(key, "DISTRIB_TARGET")) key = "target"; else if (!strcasecmp(key, "DISTRIB_DESCRIPTION")) key = "description"; else continue; dest = blobmsg_alloc_string_buffer(&b, key, strlen(val)); if (!dest) { ERROR("Failed to allocate blob.\n"); continue; } while (val && (ch = *(val++)) != 0) { switch (ch) { case '\'': case '"': next = strchr(val, ch); if (next) *next = 0; strcpy(dest, val); if (next) val = next + 1; dest += strlen(dest); break; case '\\': *(dest++) = *(val++); break; } } blobmsg_add_string_buffer(&b); } blobmsg_close_array(&b, c); fclose(f); } ubus_send_reply(ctx, req, b.head); return UBUS_STATUS_OK; }