static void func_new_from_value (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { // read arguments NCDValRef arg_value; if (!NCDVal_ListRead(params->args, 1, &arg_value)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(arg_value)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // parse value uintmax_t value; if (!ncd_read_uintmax(arg_value, &value)) { ModuleLog(i, BLOG_ERROR, "wrong value"); goto fail0; } // check overflow if (value > SIZE_MAX) { ModuleLog(i, BLOG_ERROR, "value too large"); goto fail0; } func_new_templ(vo, i, value); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void new_templ (NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_not) { // check arguments NCDValRef arg; if (!NCDVal_ListRead(params->args, 1, &arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { ModuleLog(i, BLOG_ERROR, "bad argument"); goto fail0; } // compute logical value of argument int c = arg_val; // signal up if needed if ((is_not && !c) || (!is_not && c)) { NCDModuleInst_Backend_Up(i); } return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void new_number_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, number_compute_func cfunc) { struct number_instance *o = vo; o->i = i; NCDValRef n1_arg; NCDValRef n2_arg; if (!NCDVal_ListRead(params->args, 2, &n1_arg, &n2_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } uintmax_t n1; if (!ncd_read_uintmax(n1_arg, &n1)) { ModuleLog(o->i, BLOG_ERROR, "wrong first argument"); goto fail0; } uintmax_t n2; if (!ncd_read_uintmax(n2_arg, &n2)) { ModuleLog(o->i, BLOG_ERROR, "wrong second argument"); goto fail0; } o->error = cfunc(i, n1, n2, &o->value); NCDModuleInst_Backend_Up(i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // read arguments NCDValRef arg_choices; NCDValRef arg_default_result; if (!NCDVal_ListRead(params->args, 2, &arg_choices, &arg_default_result)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsList(arg_choices)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // iterate choices int have_result = 0; size_t count = NCDVal_ListCount(arg_choices); for (size_t j = 0; j < count; j++) { NCDValRef c = NCDVal_ListGet(arg_choices, j); // check choice type if (!NCDVal_IsList(c)) { ModuleLog(i, BLOG_ERROR, "wrong choice type"); goto fail0; } // read choice NCDValRef c_cond; NCDValRef c_result; if (!NCDVal_ListRead(c, 2, &c_cond, &c_result)) { ModuleLog(i, BLOG_ERROR, "wrong choice contents arity"); goto fail0; } if (!NCDVal_IsString(c_cond)) { ModuleLog(i, BLOG_ERROR, "wrong choice condition type"); goto fail0; } // update result if (!have_result && ncd_read_boolean(c_cond)) { o->result = c_result; have_result = 1; } } // default? if (!have_result) { o->result = arg_default_result; } // signal up NCDModuleInst_Backend_Up(o->i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, parse_func pfunc) { struct instance *o = vo; o->i = i; // read arguments NCDValRef str_arg; if (!NCDVal_ListRead(params->args, 1, &str_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(str_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail0; } // init mem NCDValMem_Init(&o->mem); // parse o->succeeded = pfunc(i, NCDVal_StringMemRef(str_arg), &o->mem, &o->value); // signal up NCDModuleInst_Backend_Up(i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { // check arguments NCDValRef exit_code_arg; if (!NCDVal_ListRead(params->args, 1, &exit_code_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(exit_code_arg)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // parse exit code uintmax_t exit_code; if (!ncd_read_uintmax(exit_code_arg, &exit_code) || exit_code >= INT_MAX) { ModuleLog(i, BLOG_ERROR, "wrong exit code value"); goto fail0; } // signal up NCDModuleInst_Backend_Up(i); // initiate exit (before up!) NCDModuleInst_Backend_InterpExit(i, exit_code); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static int parse_options (NCDModuleInst *i, NCDValRef options, size_t *out_read_size) { ASSERT(out_read_size) *out_read_size = DEFAULT_READ_BUF_SIZE; if (!NCDVal_IsInvalid(options)) { if (!NCDVal_IsMap(options)) { ModuleLog(i, BLOG_ERROR, "options argument is not a map"); return 0; } int num_recognized = 0; NCDValRef value; if (!NCDVal_IsInvalid(value = NCDVal_MapGetValue(options, "read_size"))) { uintmax_t read_size; if (!ncd_read_uintmax(value, &read_size) || read_size > SIZE_MAX || read_size == 0) { ModuleLog(i, BLOG_ERROR, "wrong read_size"); return 0; } num_recognized++; *out_read_size = read_size; } if (NCDVal_MapCount(options) > num_recognized) { ModuleLog(i, BLOG_ERROR, "unrecognized options present"); return 0; } } return 1; }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // read arguments NCDValRef target_arg; if (!NCDVal_ListRead(params->args, 1, &target_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(target_arg)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // parse name string if (!AliasNames_InitNames(o, i->params->iparams->string_index, NCDVal_StringData(target_arg), NCDVal_StringLength(target_arg))) { ModuleLog(i, BLOG_ERROR, "make_names failed"); goto fail0; } // signal up NCDModuleInst_Backend_Up(o->i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // check arguments NCDValRef str1_arg; NCDValRef str2_arg; if (!NCDVal_ListRead(params->args, 2, &str1_arg, &str2_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(str1_arg) || !NCDVal_IsString(str2_arg)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // compare o->result = (NCDVal_Compare(str1_arg, str2_arg) == 0); // signal up NCDModuleInst_Backend_Up(i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void prefix_to_mask_func_init (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { // read arguments NCDValRef prefix_arg; if (!NCDVal_ListRead(params->args, 1, &prefix_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(prefix_arg)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // parse prefix int prefix; if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(prefix_arg), &prefix)) { ModuleLog(i, BLOG_ERROR, "bad prefix"); goto fail0; } // make mask uint32_t mask = ipaddr_ipv4_mask_from_prefix(prefix); addr_func_init_templ(vo, i, mask); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new_from_value (NCDModuleInst *i) { // read arguments NCDValue *arg_value; if (!NCDValue_ListRead(i->args, 1, &arg_value)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (NCDValue_Type(arg_value) != NCDVALUE_STRING) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } // parse value uintmax_t value; if (!parse_unsigned_integer(NCDValue_StringValue(arg_value), &value)) { ModuleLog(i, BLOG_ERROR, "wrong value"); goto fail0; } // check overflow if (value > SIZE_MAX) { ModuleLog(i, BLOG_ERROR, "value too large"); goto fail0; } func_new_templ(i, value); return; fail0: NCDModuleInst_Backend_SetError(i); NCDModuleInst_Backend_Dead(i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // check arguments NCDValRef arg; if (!NCDVal_ListRead(params->args, 1, &arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsStringNoNulls(arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail0; } o->ifname = NCDVal_StringMemRef(arg); // init client NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler); // compile regex if (regcomp(&o->reg, DEVPATH_REGEX, REG_EXTENDED)) { ModuleLog(o->i, BLOG_ERROR, "regcomp failed"); goto fail1; } // set no devpath o->devpath = NULL; return; fail1: NCDUdevClient_Free(&o->client); fail0: NCDModuleInst_Backend_DeadError(i); }
static void listener_handler (struct instance *o) { ASSERT(!o->dying) BReactor *reactor = o->i->params->iparams->reactor; BPendingGroup *pg = BReactor_PendingGroup(reactor); struct connection *c = malloc(sizeof(*c)); if (!c) { ModuleLog(o->i, BLOG_ERROR, "malloc failed"); goto fail0; } c->inst = o; LinkedList0_Prepend(&o->connections_list, &c->connections_list_node); if (!BConnection_Init(&c->con, BConnection_source_listener(&o->listener, &c->addr), reactor, c, (BConnection_handler)connection_con_handler)) { ModuleLog(o->i, BLOG_ERROR, "BConnection_Init failed"); goto fail1; } BConnection_SendAsync_Init(&c->con); BConnection_RecvAsync_Init(&c->con); StreamPassInterface *con_send_if = BConnection_SendAsync_GetIf(&c->con); StreamRecvInterface *con_recv_if = BConnection_RecvAsync_GetIf(&c->con); PacketPassInterface_Init(&c->recv_if, RECV_MTU, (PacketPassInterface_handler_send)connection_recv_if_handler_send, c, pg); if (!PacketProtoDecoder_Init(&c->recv_decoder, con_recv_if, &c->recv_if, pg, c, (PacketProtoDecoder_handler_error)connection_recv_decoder_handler_error)) { ModuleLog(o->i, BLOG_ERROR, "PacketProtoDecoder_Init failed"); goto fail2; } PacketStreamSender_Init(&c->send_pss, con_send_if, PACKETPROTO_ENCLEN(SEND_MTU), pg); PacketPassFifoQueue_Init(&c->send_queue, PacketStreamSender_GetInput(&c->send_pss), pg); LinkedList0_Init(&c->requests_list); LinkedList0_Init(&c->replies_list); c->state = CONNECTION_STATE_RUNNING; ModuleLog(o->i, BLOG_INFO, "connection initialized"); return; fail2: PacketPassInterface_Free(&c->recv_if); BConnection_RecvAsync_Free(&c->con); BConnection_SendAsync_Free(&c->con); BConnection_Free(&c->con); fail1: LinkedList0_Remove(&o->connections_list, &c->connections_list_node); free(c); fail0: return; }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // check arguments NCDValRef ms_start_arg; NCDValRef ms_stop_arg = NCDVal_NewInvalid(); if (!NCDVal_ListRead(params->args, 1, &ms_start_arg) && !NCDVal_ListRead(params->args, 2, &ms_start_arg, &ms_stop_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } uintmax_t ms; btime_t ms_start; if (NCDVal_IsString(ms_start_arg) && NCDVal_StringEqualsId(ms_start_arg, NCD_STRING_EMPTY)) { ms_start = -1; } else { if (!ncd_read_uintmax(ms_start_arg, &ms) || ms > INT64_MAX) { ModuleLog(o->i, BLOG_ERROR, "wrong start time"); goto fail0; } ms_start = ms; } if (NCDVal_IsInvalid(ms_stop_arg) || (NCDVal_IsString(ms_stop_arg) && NCDVal_StringEqualsId(ms_stop_arg, NCD_STRING_EMPTY))) { o->ms_stop = -1; } else { if (!ncd_read_uintmax(ms_stop_arg, &ms) || ms > INT64_MAX) { ModuleLog(o->i, BLOG_ERROR, "wrong stop time"); goto fail0; } o->ms_stop = ms; } // init timer BTimer_Init(&o->timer, 0, timer_handler, o); // set not dying o->dying = 0; if (ms_start < 0) { // go up NCDModuleInst_Backend_Up(i); } else { // set timer BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, ms_start); } return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // read arguments NCDValRef input_arg; NCDValRef regex_arg; if (!NCDVal_ListRead(params->args, 2, &input_arg, ®ex_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(input_arg) || !NCDVal_IsStringNoNulls(regex_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail0; } o->input = NCDVal_StringMemRef(input_arg); // make sure we don't overflow regoff_t if (o->input.len > INT_MAX) { ModuleLog(o->i, BLOG_ERROR, "input string too long"); goto fail0; } // null terminate regex NCDValNullTermString regex_nts; if (!NCDVal_StringNullTerminate(regex_arg, ®ex_nts)) { ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); goto fail0; } // compile regex regex_t preg; int ret = regcomp(&preg, regex_nts.data, REG_EXTENDED); NCDValNullTermString_Free(®ex_nts); if (ret != 0) { ModuleLog(o->i, BLOG_ERROR, "regcomp failed (error=%d)", ret); goto fail0; } // execute match o->matches[0].rm_so = 0; o->matches[0].rm_eo = o->input.len; o->succeeded = (regexec(&preg, o->input.ptr, MAX_MATCHES, o->matches, REG_STARTEND) == 0); // free regex regfree(&preg); // signal up NCDModuleInst_Backend_Up(o->i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (NCDModuleInst *i) { // allocate instance struct instance *o = malloc(sizeof(*o)); if (!o) { ModuleLog(i, BLOG_ERROR, "failed to allocate instance"); goto fail0; } NCDModuleInst_Backend_SetUser(i, o); // init arguments o->i = i; // check arguments NCDValue *template_name_arg; NCDValue *args_arg; if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail1; } if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail1; } // signal up. // Do it before creating the process so that the process starts initializing before our own process continues. NCDModuleInst_Backend_Up(o->i); // copy arguments NCDValue args; if (!NCDValue_InitCopy(&args, args_arg)) { ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed"); goto fail1; } // create process if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_event)process_handler_event)) { ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed"); NCDValue_Free(&args); goto fail1; } // set state working o->state = STATE_WORKING; return; fail1: free(o); fail0: NCDModuleInst_Backend_SetError(i); NCDModuleInst_Backend_Dead(i); }
static void func_new (NCDModuleInst *i) { // allocate instance struct instance *o = malloc(sizeof(*o)); if (!o) { ModuleLog(i, BLOG_ERROR, "failed to allocate instance"); goto fail0; } NCDModuleInst_Backend_SetUser(i, o); // init arguments o->i = i; // init string ExpString s; if (!ExpString_Init(&s)) { ModuleLog(i, BLOG_ERROR, "ExpString_Init failed"); goto fail1; } // append arguments NCDValue *arg = NCDValue_ListFirst(o->i->args); while (arg) { if (NCDValue_Type(arg) != NCDVALUE_STRING) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail2; } if (!ExpString_Append(&s, NCDValue_StringValue(arg))) { ModuleLog(i, BLOG_ERROR, "ExpString_Append failed"); goto fail2; } arg = NCDValue_ListNext(o->i->args, arg); } // set string o->string = ExpString_Get(&s); // signal up NCDModuleInst_Backend_Up(o->i); return; fail2: ExpString_Free(&s); fail1: free(o); fail0: NCDModuleInst_Backend_SetError(i); NCDModuleInst_Backend_Dead(i); }
static void stat_func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_lstat) { struct stat_instance *o = vo; o->i = i; NCDValRef filename_arg; if (!NCDVal_ListRead(params->args, 1, &filename_arg)) { ModuleLog(i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsString(filename_arg)) { ModuleLog(i, BLOG_ERROR, "wrong type"); goto fail0; } o->succeeded = 0; if (!NCDVal_IsStringNoNulls(filename_arg)) { goto out; } // null terminate filename NCDValNullTermString filename_nts; if (!NCDVal_StringNullTerminate(filename_arg, &filename_nts)) { ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); goto fail0; } int res; if (is_lstat) { res = lstat(filename_nts.data, &o->result); } else { res = stat(filename_nts.data, &o->result); } NCDValNullTermString_Free(&filename_nts); if (res < 0) { goto out; } o->succeeded = 1; out: NCDModuleInst_Backend_Up(i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void arpprobe_handler (struct instance *o, int event) { switch (event) { case BARPPROBE_EVENT_EXIST: { ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_NOEXIST) ModuleLog(o->i, BLOG_INFO, "exist"); if (o->state == STATE_NOEXIST) { // signal down NCDModuleInst_Backend_Down(o->i); } // signal up NCDModuleInst_Backend_Up(o->i); // set state exist o->state = STATE_EXIST; } break; case BARPPROBE_EVENT_NOEXIST: { ASSERT(o->state == STATE_UNKNOWN || o->state == STATE_EXIST) ModuleLog(o->i, BLOG_INFO, "noexist"); if (o->state == STATE_EXIST) { // signal down NCDModuleInst_Backend_Down(o->i); } // signal up NCDModuleInst_Backend_Up(o->i); // set state noexist o->state = STATE_NOEXIST; } break; case BARPPROBE_EVENT_ERROR: { ModuleLog(o->i, BLOG_ERROR, "error"); // die instance_free(o, 1); return; } break; default: ASSERT(0); } }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int is_not, int is_or) { struct instance *o = vo; o->i = i; // compute value from arguments if (is_not) { NCDValRef arg; if (!NCDVal_ListRead(params->args, 1, &arg)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { ModuleLog(o->i, BLOG_ERROR, "bad argument"); goto fail0; } o->value = !arg_val; } else { o->value = (is_or ? 0 : 1); size_t count = NCDVal_ListCount(params->args); for (size_t j = 0; j < count; j++) { NCDValRef arg = NCDVal_ListGet(params->args, j); int this_value; if (!ncd_read_boolean(arg, &this_value)) { ModuleLog(o->i, BLOG_ERROR, "bad argument"); goto fail0; } if (is_or) { o->value = o->value || this_value; } else { o->value = o->value && this_value; } } } // signal up NCDModuleInst_Backend_Up(o->i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // check arguments if (!NCDVal_ListRead(params->args, 0)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } // init users list LinkedList1_Init(&o->users); // init rdownups list LinkedList0_Init(&o->rdownups_list); // set not up o->up = 0; // set not dying o->dying = 0; // signal up NCDModuleInst_Backend_Up(o->i); return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_die (void *vo) { struct instance *o = vo; // remove route int res = 0; // to remove warning switch (o->type) { case TYPE_NORMAL: res = NCDIfConfig_remove_ipv6_route(o->dest, &o->gateway, o->metric, o->ifname_nts.data); break; case TYPE_IFONLY: res = NCDIfConfig_remove_ipv6_route(o->dest, NULL, o->metric, o->ifname_nts.data); break; case TYPE_BLACKHOLE: res = NCDIfConfig_remove_ipv6_blackhole_route(o->dest, o->metric); break; default: ASSERT(0); } if (!res) { ModuleLog(o->i, BLOG_ERROR, "failed to remove route"); } // free ifname nts NCDValNullTermString_Free(&o->ifname_nts); NCDModuleInst_Backend_Dead(o->i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // read arguments NCDValRef arg_ifname; NCDValRef arg_addr; if (!NCDVal_ListRead(params->args, 2, &arg_ifname, &arg_addr)) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsStringNoNulls(arg_ifname) || !NCDVal_IsString(arg_addr)) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail0; } // parse address uint32_t addr; if (!ipaddr_parse_ipv4_addr_bin(NCDVal_StringData(arg_addr), NCDVal_StringLength(arg_addr), &addr)) { ModuleLog(o->i, BLOG_ERROR, "wrong address"); goto fail0; } // null terminate ifname NCDValNullTermString ifname_nts; if (!NCDVal_StringNullTerminate(arg_ifname, &ifname_nts)) { ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); goto fail0; } // init arpprobe int res = BArpProbe_Init(&o->arpprobe, ifname_nts.data, addr, i->params->iparams->reactor, o, (BArpProbe_handler)arpprobe_handler); NCDValNullTermString_Free(&ifname_nts); if (!res) { ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed"); goto fail0; } // set state unknown o->state = STATE_UNKNOWN; return; fail0: NCDModuleInst_Backend_DeadError(i); }
static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) { struct instance *o = vo; o->i = i; // read arguments NCDValRef ifname_arg; NCDValRef addr_arg; NCDValRef prefix_arg = NCDVal_NewInvalid(); if (!NCDVal_ListRead(params->args, 2, &ifname_arg, &addr_arg) && !NCDVal_ListRead(params->args, 3, &ifname_arg, &addr_arg, &prefix_arg) ) { ModuleLog(o->i, BLOG_ERROR, "wrong arity"); goto fail0; } if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsString(addr_arg) || (!NCDVal_IsInvalid(prefix_arg) && !NCDVal_IsString(prefix_arg)) ) { ModuleLog(o->i, BLOG_ERROR, "wrong type"); goto fail0; } // null terminate ifname if (!NCDVal_StringNullTerminate(ifname_arg, &o->ifname_nts)) { ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); goto fail0; } if (NCDVal_IsInvalid(prefix_arg)) { if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringMemRef(addr_arg), &o->ifaddr)) { ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation address"); goto fail1; } } else { if (!ipaddr_parse_ipv4_addr(NCDVal_StringMemRef(addr_arg), &o->ifaddr.addr)) { ModuleLog(o->i, BLOG_ERROR, "wrong address"); goto fail1; } if (!ipaddr_parse_ipv4_prefix(NCDVal_StringMemRef(prefix_arg), &o->ifaddr.prefix)) { ModuleLog(o->i, BLOG_ERROR, "wrong prefix"); goto fail1; } } // add address if (!NCDIfConfig_add_ipv4_addr(o->ifname_nts.data, o->ifaddr)) { ModuleLog(o->i, BLOG_ERROR, "failed to add IP address"); goto fail1; } // signal up NCDModuleInst_Backend_Up(o->i); return; fail1: NCDValNullTermString_Free(&o->ifname_nts); fail0: NCDModuleInst_Backend_DeadError(i); }
static int compute_multiply (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out) { if (n1 > UINTMAX_MAX / n2) { ModuleLog(i, BLOG_ERROR, "multiplication overflow"); return 0; } *out = n1 * n2; return 1; }
static int compute_modulo (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out) { if (n2 == 0) { ModuleLog(i, BLOG_ERROR, "modulo modulus is zero"); return 0; } *out = n1 % n2; return 1; }
static int parse_value (NCDModuleInst *i, MemRef str, NCDValMem *mem, NCDValRef *out) { if (!NCDValParser_Parse(str, mem, out)) { ModuleLog(i, BLOG_ERROR, "failed to parse value"); return 0; } return 1; }
void timer_handler (struct instance *o) { ASSERT(!o->started) ModuleLog(o->i, BLOG_INFO, "retrying"); // try starting process again try_process(o); }
static int compute_subtract (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out) { if (n1 < n2) { ModuleLog(i, BLOG_ERROR, "subtraction underflow"); return 0; } *out = n1 - n2; return 1; }
static int compute_divide (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, uintmax_t *out) { if (n2 == 0) { ModuleLog(i, BLOG_ERROR, "division quotient is zero"); return 0; } *out = n1 / n2; return 1; }