bool fw3_ubus_connect(void) { bool status = false; uint32_t id; struct ubus_context *ctx = ubus_connect(NULL); struct blob_buf b = { }; if (!ctx) goto out; if (ubus_lookup_id(ctx, "network.interface", &id)) goto out; if (ubus_invoke(ctx, id, "dump", NULL, dump_cb, NULL, 2000)) goto out; status = true; if (ubus_lookup_id(ctx, "service", &id)) goto out; blob_buf_init(&b, 0); blobmsg_add_string(&b, "type", "firewall"); ubus_invoke(ctx, id, "get_data", b.head, procd_data_cb, NULL, 2000); blob_buf_free(&b); out: if (ctx) ubus_free(ctx); return status; }
static int init_spec_data(struct ubus_context *ctx) { uint32_t iid = id; // get switch port modlue id ubus_lookup_id(ctx,"bridge.swVlan", &id); //printf("bridge.port id = %x\n", id); if(0 == id){ fprintf(stderr,"get bridge.port ubus id faild\n"); return -1; } if(id == iid)/* wait ctx stable */ init_spec_data(ctx); return 0; }
int UBusSubscriber::UnSubscribe(std::string &event) { uint32_t id; int ret; ILOG_TRACE(UBUS_SUBSCRIBER); if (ubus_lookup_id(_ubus, event.c_str(), &id)) { ILOG_ERROR(UBUS_SUBSCRIBER, "ubus lookup id %s failed\n", event.c_str()); return -1; } ret = ubus_unsubscribe(_ubus, &_subscriber, id); if (ret) { ILOG_ERROR(UBUS_SUBSCRIBER,"event %s unsubscribe failed (err: %s)\n", event.c_str(), ubus_strerror(ret)); return ret; } ILOG_DEBUG(UBUS_SUBSCRIBER, "unsubscribe event %s success\n", event.c_str()); return 0; }
static int client_ubus_process(char *ubus_object, char *ubus_method, char *argv) { static struct ubus_request req; uint32_t id; int ret, ret_ubus_invoke; const char *ubus_socket = NULL; struct ubus_context *ctx_local; ctx_local = ubus_connect(ubus_socket); if (!ctx_local) { printf("Failed to connect to ubus\n"); return -1; } if (ubus_lookup_id(ctx_local, ubus_object, &id)) { printf("Failed to look up test object\n"); return -1; } blob_buf_init(&b_local, 0); blobmsg_add_string(&b_local, "cmd", argv); ret_ubus_invoke = ubus_invoke(ctx_local, id, ubus_method, b_local.head, receive_call_result_data, 0, 20000); if(ret_ubus_invoke == 0 || ret_ubus_invoke == 7) { ret = 0; } else { ret = -1; } ubus_free(ctx_local); return ret; }
static void init_loop_cb(struct uloop_timeout *timeout) { uint32_t id; // replace *d char key_iv[32] = {0}; char repeater_pubkey[] = "-----BEGIN PUBLIC KEY-----\n"\ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyUF9+Td53E0lEqP8eRyM\n"\ "DoREPIety4BJscApuPwcGxiVA+75mYw8AFKlTaWlLL7w/qyuxnOiNm6TTFCxJGI1\n"\ "C9GnABm8myC5hnDSC6qE+E1Al6wZgyI4rkC+Qr7GFI9B8r9bM+RdKyMxD9JuXUcx\n"\ "+rx/TPnJttUfAkCloYStd5LiCel6c/bfWiR5hyxLjXC5Cz5cdpzXwvyvgEBvZHxX\n"\ "A5jqPVfWcJ3P3s88ogPxx52iM4dzLtzWB6BOmZA/3SbvZle2Efy7mVAl/9gkwvgf\n"\ "Dm/TSnMbnUBim3H5CIlECsvs2RuvI5og0S3oEO4B8TQQjujSqbA6h+EcZc9Djmh2\n"\ "vQIDAQAB\n"\ "-----END PUBLIC KEY-----\n"; unsigned char *encrypted=malloc(MAX_ALLOC_LEN); unsigned char *base64=malloc(MAX_ALLOC_LEN); RAND_seed(rnd_seed, sizeof rnd_seed); /* or RSA_PKCS1_PADDING/OAEP may fail */ random_uuid(repeater_key); random_uuid(repeater_iv); memcpy(key_iv, repeater_key, 16); memcpy(key_iv+16, repeater_iv, 16); int encrypted_length= public_encrypt(key_iv, 32, repeater_pubkey, encrypted); if(encrypted_length == -1) { printLastError("Public Encrypt failed "); exit(0); } printf("Encrypted length =%d\n",encrypted_length); printf("Try to init_loop_cb...\n"); int base64_length = base64Encode(base64, encrypted, encrypted_length); // printf("base64 len: %d\n", base64_length); // printf("base64: %s\n", base64); if(ubus_lookup_id(sys->tbus_ctx, "netapi", &id)){ dlog("can't lookup netapi\n"); goto fail; } blob_buf_init(&traffic_b, 0); blobmsg_add_field(&traffic_b, BLOBMSG_TYPE_UNSPEC, "data", base64, base64_length); ubus_invoke(sys->tbus_ctx, id, "init", traffic_b.head, receive_call_result_data, NULL, TRAFFICD_INIT_TIMEOUT); free(encrypted); free(base64); return; fail: free(encrypted); free(base64); uloop_timeout_set(&sd->timeout_init, TRAFFICD_INIT_LOOP_TIME); }
int main(int argc, char **argv) { static struct ubus_request req; struct ubus_context *ctx; uint32_t id; const char *ubus_socket = NULL; int ch, ret, lines = 0; static struct blob_buf b; int tries = 5; signal(SIGPIPE, SIG_IGN); while ((ch = getopt(argc, argv, "u0fcs:l:r:F:p:S:P:h:")) != -1) { switch (ch) { case 'u': log_udp = 1; break; case '0': log_trailer_null = 1; break; case 's': ubus_socket = optarg; break; case 'r': log_ip = optarg++; log_port = argv[optind++]; break; case 'F': log_file = optarg; break; case 'p': pid_file = optarg; break; case 'P': log_prefix = optarg; break; case 'f': log_follow = 1; break; case 'l': lines = atoi(optarg); break; case 'S': log_size = atoi(optarg); if (log_size < 1) log_size = 1; log_size *= 1024; break; case 'h': hostname = optarg; break; default: return usage(*argv); } } uloop_init(); ctx = ubus_connect(ubus_socket); if (!ctx) { fprintf(stderr, "Failed to connect to ubus\n"); return -1; } ubus_add_uloop(ctx); /* ugly ugly ugly ... we need a real reconnect logic */ do { ret = ubus_lookup_id(ctx, "log", &id); if (ret) { fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret)); sleep(1); continue; } blob_buf_init(&b, 0); if (lines) blobmsg_add_u32(&b, "lines", lines); else if (log_follow) blobmsg_add_u32(&b, "lines", 0); if (log_follow) { if (pid_file) { FILE *fp = fopen(pid_file, "w+"); if (fp) { fprintf(fp, "%d", getpid()); fclose(fp); } } } if (log_ip && log_port) { openlog("logread", LOG_PID, LOG_DAEMON); log_type = LOG_NET; sender.cb = log_handle_fd; retry.cb = log_handle_reconnect; uloop_timeout_set(&retry, 1000); } else if (log_file) { log_type = LOG_FILE; sender.fd = open(log_file, O_CREAT | O_WRONLY| O_APPEND, 0600); if (sender.fd < 0) { fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno)); exit(-1); } } else { sender.fd = STDOUT_FILENO; } ubus_invoke_async(ctx, id, "read", b.head, &req); req.fd_cb = logread_fd_cb; ubus_complete_request_async(ctx, &req); uloop_run(); ubus_free(ctx); uloop_done(); } while (ret && tries--); return ret; }