static int set_servers (struct global *g) { int ret = 0; // count servers size_t num_entries = count_entries(g); // allocate sort array struct dns_sort_entry *sort_entries = BAllocArray(num_entries, sizeof(sort_entries[0])); if (!sort_entries) { goto fail0; } // fill sort array num_entries = 0; for (LinkedList1Node *n = LinkedList1_GetFirst(&g->instances); n; n = LinkedList1Node_Next(n)) { struct instance *o = UPPER_OBJECT(n, struct instance, instances_node); for (LinkedList1Node *en = LinkedList1_GetFirst(&o->entries); en; en = LinkedList1Node_Next(en)) { struct dns_entry *e = UPPER_OBJECT(en, struct dns_entry, list_node); sort_entries[num_entries].line = e->line; sort_entries[num_entries].priority= e->priority; num_entries++; } } // sort by priority // use a custom insertion sort instead of qsort() because we want a stable sort struct dns_sort_entry temp; BInsertionSort(sort_entries, num_entries, sizeof(sort_entries[0]), dns_sort_comparator, &temp); ExpString estr; if (!ExpString_Init(&estr)) { goto fail1; } for (size_t i = 0; i < num_entries; i++) { if (!ExpString_Append(&estr, sort_entries[i].line)) { goto fail2; } } // set servers if (!NCDIfConfig_set_resolv_conf(ExpString_Get(&estr), ExpString_Length(&estr))) { goto fail2; } ret = 1; fail2: ExpString_Free(&estr); fail1: BFree(sort_entries); fail0: return ret; }
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 int generate_val (NCDValue *value, ExpString *out_str) { switch (NCDValue_Type(value)) { case NCDVALUE_STRING: { const char *str = NCDValue_StringValue(value); size_t len = NCDValue_StringLength(value); if (!ExpString_AppendChar(out_str, '"')) { goto fail; } for (size_t i = 0; i < len; i++) { if (str[i] == '\0') { char buf[5]; snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)str[i]); if (!ExpString_Append(out_str, buf)) { goto fail; } continue; } if (str[i] == '"' || str[i] == '\\') { if (!ExpString_AppendChar(out_str, '\\')) { goto fail; } } if (!ExpString_AppendChar(out_str, str[i])) { goto fail; } } if (!ExpString_AppendChar(out_str, '"')) { goto fail; } } break; case NCDVALUE_LIST: { if (!ExpString_AppendChar(out_str, '{')) { goto fail; } int is_first = 1; for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) { if (!is_first) { if (!ExpString_Append(out_str, ", ")) { goto fail; } } if (!generate_val(e, out_str)) { goto fail; } is_first = 0; } if (!ExpString_AppendChar(out_str, '}')) { goto fail; } } break; case NCDVALUE_MAP: { if (!ExpString_AppendChar(out_str, '[')) { goto fail; } int is_first = 1; for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) { NCDValue *eval = NCDValue_MapKeyValue(value, ekey); if (!is_first) { if (!ExpString_Append(out_str, ", ")) { goto fail; } } if (!generate_val(ekey, out_str)) { goto fail; } if (!ExpString_AppendChar(out_str, ':')) { goto fail; } if (!generate_val(eval, out_str)) { goto fail; } is_first = 0; } if (!ExpString_AppendChar(out_str, ']')) { goto fail; } } break; case NCDVALUE_VAR: { if (!ExpString_Append(out_str, NCDValue_VarName(value))) { goto fail; } } break; case NCDVALUE_INVOC: { if (!generate_val(NCDValue_InvocFunc(value), out_str)) { goto fail; } if (!ExpString_AppendChar(out_str, '(')) { goto fail; } if (!generate_val(NCDValue_InvocArg(value), out_str)) { goto fail; } if (!ExpString_AppendChar(out_str, ')')) { goto fail; } } break; default: ASSERT(0); } return 1; fail: return 0; }