bud_error_t bud_config_load_backend(bud_config_t* config, JSON_Object* obj, bud_config_backend_t* backend, bud_hashmap_t* map, unsigned int* ext_count) { bud_error_t err; JSON_Value* val; const char* external; int r; bud_config_load_addr(obj, (bud_config_addr_t*) backend); backend->config = config; backend->xforward = -1; val = json_object_get_value(obj, "proxyline"); if (json_value_get_type(val) == JSONString) { const char* pline; pline = json_value_get_string(val); if (strcmp(pline, "haproxy") == 0) backend->proxyline = kBudProxylineHAProxy; else if (strcmp(pline, "json") == 0) backend->proxyline = kBudProxylineJSON; else return bud_error_dstr(kBudErrProxyline, pline); } else { backend->proxyline = val != NULL && json_value_get_boolean(val) ? kBudProxylineHAProxy : kBudProxylineNone; } val = json_object_get_value(obj, "x-forward"); if (val != NULL) backend->xforward = json_value_get_boolean(val); /* Set defaults here to use them in sni.c */ bud_config_set_backend_defaults(backend); r = bud_config_str_to_addr(backend->host, backend->port, &backend->addr); if (r != 0) return bud_error_num(kBudErrPton, r); external = json_object_get_string(obj, "external"); if (external == NULL) return bud_ok(); /* Insert backend into a hashmap */ err = bud_hashmap_insert(map, external, strlen(external), backend); if (!bud_is_ok(err)) return err; (*ext_count)++; return bud_ok(); }
/** * json_node_get_value: * @node: a #JsonNode * @value: (out caller-allocates): return location for an uninitialized value * * Retrieves a value from a #JsonNode and copies into @value. When done * using it, call g_value_unset() on the #GValue. */ void json_node_get_value (JsonNode *node, GValue *value) { g_return_if_fail (JSON_NODE_IS_VALID (node)); g_return_if_fail (JSON_NODE_TYPE (node) == JSON_NODE_VALUE); if (node->data.value) { g_value_init (value, JSON_VALUE_TYPE (node->data.value)); switch (JSON_VALUE_TYPE (node->data.value)) { case G_TYPE_INT64: g_value_set_int64 (value, json_value_get_int (node->data.value)); break; case G_TYPE_DOUBLE: g_value_set_double (value, json_value_get_double (node->data.value)); break; case G_TYPE_BOOLEAN: g_value_set_boolean (value, json_value_get_boolean (node->data.value)); break; case G_TYPE_STRING: g_value_set_string (value, json_value_get_string (node->data.value)); break; default: break; } } }
bud_error_t bud_config_load_frontend(JSON_Object* obj, bud_config_frontend_t* frontend) { JSON_Value* val; bud_config_load_addr(obj, (bud_config_addr_t*) frontend); frontend->max_send_fragment = -1; frontend->allow_half_open = -1; frontend->reneg_limit = -1; if (obj == NULL) return bud_ok(); frontend->security = json_object_get_string(obj, "security"); frontend->reneg_window = json_object_get_number(obj, "reneg_window"); val = json_object_get_value(obj, "reneg_limit"); if (val != NULL) frontend->reneg_limit = json_value_get_number(val); val = json_object_get_value(obj, "max_send_fragment"); if (val != NULL) frontend->max_send_fragment = json_value_get_number(val); val = json_object_get_value(obj, "allow_half_open"); if (val != NULL) frontend->allow_half_open = json_value_get_boolean(val); return bud_config_load_frontend_ifaces(obj, &frontend->interface); }
static EJSBool json_value_to_ejsval(JSON_Value *v, ejsval *rv) { switch (json_value_get_type (v)) { case JSONNull: *rv = _ejs_null; return EJS_TRUE; case JSONString: *rv = _ejs_string_new_utf8 (json_value_get_string(v)); return EJS_TRUE; case JSONNumber: *rv = NUMBER_TO_EJSVAL(json_value_get_number(v)); return EJS_TRUE; case JSONObject: { JSON_Object *obj = json_value_get_object (v); *rv = _ejs_object_create (_ejs_null); int count = json_object_get_count (obj); for (int i = 0; i < count; i ++) { const char *propkey = json_object_get_name (obj, i); ejsval propval; if (!json_value_to_ejsval (json_object_get_value (obj, propkey), &propval)) return EJS_FALSE; _ejs_object_setprop_utf8 (*rv, propkey, propval); } return EJS_TRUE; } case JSONArray: { JSON_Array *arr = json_value_get_array (v); int count = json_array_get_count (arr); *rv = _ejs_array_new (count, EJS_FALSE); for (int i = 0; i < count; i ++) { ejsval propkey = _ejs_number_new (i); ejsval propval; if (!json_value_to_ejsval (json_array_get_value (arr, i), &propval)) return EJS_FALSE; _ejs_object_setprop (*rv, propkey, propval); } return EJS_TRUE; } case JSONBoolean: *rv = BOOLEAN_TO_EJSVAL(json_value_get_boolean(v)); return EJS_TRUE; case JSONError: EJS_NOT_IMPLEMENTED(); return EJS_FALSE; } }
bud_error_t bud_config_load_frontend(JSON_Object* obj, bud_config_frontend_t* frontend) { bud_error_t err; JSON_Value* val; bud_config_load_addr(obj, (bud_config_addr_t*) frontend); frontend->server_preference = -1; frontend->ssl3 = -1; frontend->max_send_fragment = -1; frontend->allow_half_open = -1; if (obj == NULL) return bud_ok(); frontend->security = json_object_get_string(obj, "security"); frontend->ciphers = json_object_get_string(obj, "ciphers"); frontend->ecdh = json_object_get_string(obj, "ecdh"); frontend->cert_file = json_object_get_string(obj, "cert"); frontend->key_file = json_object_get_string(obj, "key"); frontend->reneg_window = json_object_get_number(obj, "reneg_window"); frontend->reneg_limit = json_object_get_number(obj, "reneg_limit"); frontend->ticket_key = json_object_get_string(obj, "ticket_key"); /* Get and verify NPN */ frontend->npn = json_object_get_array(obj, "npn"); err = bud_config_verify_npn(frontend->npn); if (!bud_is_ok(err)) goto fatal; val = json_object_get_value(obj, "server_preference"); if (val != NULL) frontend->server_preference = json_value_get_boolean(val); val = json_object_get_value(obj, "ssl3"); if (val != NULL) frontend->ssl3 = json_value_get_boolean(val); val = json_object_get_value(obj, "max_send_fragment"); if (val != NULL) frontend->max_send_fragment = json_value_get_number(val); val = json_object_get_value(obj, "allow_half_open"); if (val != NULL) frontend->allow_half_open = json_value_get_boolean(val); fatal: return err; }
char *json(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error) { JSON_Value *jv = json_parse_string(args->args[0]); JSON_Value_Type json_type = json_value_get_type(jv); if(json_type==JSONError){ *length=0; *is_null=1; *error=1; json_value_free(jv); return NULL; }else{ if(json_type == JSONObject){ JSON_Object *o = json_value_get_object(jv); JSON_Value *value = json_object_dotget_value(o,args->args[1]); if(value==NULL){ *length = 0; *is_null = 0; return NULL; }else{ *is_null = 0; switch(json_value_get_type(value)){ case JSONArray: *is_null = 1; break; case JSONError: case JSONObject: *is_null = 1; *error = 1; break; case JSONString: strcpy(initid->ptr,((char*)json_value_get_string(value))); break; case JSONNumber: sprintf(initid->ptr,"%f",json_value_get_number(value)); break; case JSONNull: *is_null = 1; break; case JSONBoolean: sprintf(initid->ptr,"%d",json_value_get_boolean(value)); break; } *length = initid->ptr == NULL ? 0 : strlen(initid->ptr); } }else{ *is_null = 1; } json_value_free(jv); return initid->ptr; } }
bud_error_t bud_config_load_backend(bud_config_t* config, JSON_Object* obj, bud_config_backend_t* backend) { JSON_Value* val; bud_config_load_addr(obj, (bud_config_addr_t*) backend); backend->config = config; backend->proxyline = -1; backend->xforward = -1; val = json_object_get_value(obj, "proxyline"); if (val != NULL) backend->proxyline = json_value_get_boolean(val); val = json_object_get_value(obj, "x-forward"); if (val != NULL) backend->xforward = json_value_get_boolean(val); /* Set defaults here to use them in sni.c */ bud_config_set_backend_defaults(backend); return bud_ok(); }
static void GenStat(Stat* s, JSON_Value* v) { switch (json_value_get_type(v)) { case JSONObject: { JSON_Object* o = json_value_get_object(v); size_t count = json_object_get_count(o); for (size_t i = 0; i < count; i++) { const char* name = json_object_get_name(o, i); GenStat(s, json_object_get_value(o, name)); s->stringLength += strlen(name); } s->objectCount++; s->memberCount += count; s->stringCount += count; } break; case JSONArray: { JSON_Array* a = json_value_get_array(v); size_t count = json_array_get_count(a); for (int i = 0; i < count; i++) GenStat(s, json_array_get_value(a, i)); s->arrayCount++; s->elementCount += count; } break; case JSONString: s->stringCount++; s->stringLength += strlen(json_value_get_string(v)); break; case JSONNumber: s->numberCount++; break; case JSONBoolean: if (json_value_get_boolean(v)) s->trueCount++; else s->falseCount++; break; case JSONNull: s->nullCount++; break; } }
static corto_int16 json_deserBoolean(void* o, corto_primitive t, JSON_Value *v) { CORTO_UNUSED(t); if (json_value_get_type(v) != JSONBoolean) { corto_seterr("expected boolean, got %s", json_valueTypeToString(v)); goto error; } *(corto_bool *)o = json_value_get_boolean(v); return 0; error: return -1; }
static mrb_value json_value_to_mrb_value(mrb_state* mrb, JSON_Value* value) { ARENA_SAVE; switch (json_value_get_type(value)) { case JSONError: case JSONNull: return mrb_nil_value(); case JSONString: return mrb_str_new_cstr(mrb, json_value_get_string(value)); case JSONNumber: return mrb_float_value(json_value_get_number(value)); case JSONObject: { mrb_value hash = mrb_hash_new(mrb); JSON_Object* object = json_value_get_object(value); size_t count = json_object_get_count(object); int n; for (n = 0; n < count; n++) { const char* name = json_object_get_name(object, n); mrb_hash_set(mrb, hash, mrb_str_new_cstr(mrb, name), json_value_to_mrb_value(mrb, json_object_get_value(object, name))); } return hash; } case JSONArray: { mrb_value ary; ary = mrb_ary_new(mrb); JSON_Array* array = json_value_get_array(value); size_t count = json_array_get_count(array); int n; for (n = 0; n < count; n++) { JSON_Value* elem = json_array_get_value(array, n); mrb_ary_push(mrb, ary, json_value_to_mrb_value(mrb, elem)); } return ary; } case JSONBoolean: if (json_value_get_boolean(value)) { return mrb_true_value(); } return mrb_false_value(); default: mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } return mrb_nil_value(); }
/** * json_node_get_double: * @node: a #JsonNode of type %JSON_NODE_VALUE * * Gets the double value stored inside a #JsonNode * * Return value: a double value. */ gdouble json_node_get_double (JsonNode *node) { g_return_val_if_fail (node != NULL, 0.0); if (JSON_NODE_TYPE (node) == JSON_NODE_NULL) return 0; if (JSON_VALUE_HOLDS_DOUBLE (node->data.value)) return json_value_get_double (node->data.value); if (JSON_VALUE_HOLDS_INT (node->data.value)) return (gdouble) json_value_get_int (node->data.value); if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value)) return (gdouble) json_value_get_boolean (node->data.value); return 0.0; }
/** * json_node_get_boolean: * @node: a #JsonNode of type %JSON_NODE_VALUE * * Gets the boolean value stored inside a #JsonNode * * Return value: a boolean value. */ gboolean json_node_get_boolean (JsonNode *node) { g_return_val_if_fail (JSON_NODE_IS_VALID (node), FALSE); if (JSON_NODE_TYPE (node) == JSON_NODE_NULL) return FALSE; if (JSON_VALUE_HOLDS_BOOLEAN (node->data.value)) return json_value_get_boolean (node->data.value); if (JSON_VALUE_HOLDS_INT (node->data.value)) return json_value_get_int (node->data.value) != 0; if (JSON_VALUE_HOLDS_DOUBLE (node->data.value)) return json_value_get_double (node->data.value) != 0.0; return FALSE; }
/** * * @property: (in): encoders.x.elements.element.property.name or source.elements.element.property.name */ gchar * jobdesc_element_property_value (gchar *job, gchar *property) { JSON_Value *val; JSON_Object *obj; JSON_Array *array; JSON_Value_Type type; JSON_Value *value; gchar *p; gint64 i; gdouble n; gint index; val = json_parse_string_with_comments (job); obj = json_value_get_object (val); if (g_str_has_prefix (property, "encoder")) { array = json_object_dotget_array (obj, "encoders"); sscanf (property, "encoder.%d", &index); obj = json_array_get_object (array, index); p = g_strrstr (property, "elements"); value = json_object_dotget_value (obj, p); } else if (g_str_has_prefix (property, "source")) { value = json_object_dotget_value (obj, property); } if (value == NULL) { json_value_free (val); return NULL; } type = json_value_get_type (value); switch (type) { case JSONString: p = g_strdup (json_value_get_string (value)); break; case JSONNumber: n = json_value_get_number (value); i = n; if (i == n) { p = g_strdup_printf ("%ld", i); } else { p = g_strdup_printf ("%f", n); } break; case JSONBoolean: if (json_value_get_boolean (value)) { p = g_strdup ("TRUE"); } else { p = g_strdup ("FALSE"); } break; default: GST_ERROR ("property value invalid."); } json_value_free (val); return p; }
int parse_SX1301_configuration(const char * conf_file) { int i; const char conf_obj[] = "SX1301_conf"; char param_name[32]; /* used to generate variable parameter names */ const char *str; /* used to store string value from JSON object */ struct lgw_conf_board_s boardconf; struct lgw_conf_rxrf_s rfconf; struct lgw_conf_rxif_s ifconf; JSON_Value *root_val; JSON_Object *root = NULL; JSON_Object *conf = NULL; JSON_Value *val; uint32_t sf, bw; /* try to parse JSON */ root_val = json_parse_file_with_comments(conf_file); root = json_value_get_object(root_val); if (root == NULL) { MSG("ERROR: %s id not a valid JSON file\n", conf_file); exit(EXIT_FAILURE); } conf = json_object_get_object(root, conf_obj); if (conf == NULL) { MSG("INFO: %s does not contain a JSON object named %s\n", conf_file, conf_obj); return -1; } else { MSG("INFO: %s does contain a JSON object named %s, parsing SX1301 parameters\n", conf_file, conf_obj); } /* set board configuration */ memset(&boardconf, 0, sizeof boardconf); /* initialize configuration structure */ val = json_object_get_value(conf, "lorawan_public"); /* fetch value (if possible) */ if (json_value_get_type(val) == JSONBoolean) { boardconf.lorawan_public = (bool)json_value_get_boolean(val); } else { MSG("WARNING: Data type for lorawan_public seems wrong, please check\n"); boardconf.lorawan_public = false; } val = json_object_get_value(conf, "clksrc"); /* fetch value (if possible) */ if (json_value_get_type(val) == JSONNumber) { boardconf.clksrc = (uint8_t)json_value_get_number(val); } else { MSG("WARNING: Data type for clksrc seems wrong, please check\n"); boardconf.clksrc = 0; } MSG("INFO: lorawan_public %d, clksrc %d\n", boardconf.lorawan_public, boardconf.clksrc); /* all parameters parsed, submitting configuration to the HAL */ if (lgw_board_setconf(boardconf) != LGW_HAL_SUCCESS) { MSG("WARNING: Failed to configure board\n"); } /* set configuration for RF chains */ for (i = 0; i < LGW_RF_CHAIN_NB; ++i) { memset(&rfconf, 0, sizeof(rfconf)); /* initialize configuration structure */ sprintf(param_name, "radio_%i", i); /* compose parameter path inside JSON structure */ val = json_object_get_value(conf, param_name); /* fetch value (if possible) */ if (json_value_get_type(val) != JSONObject) { MSG("INFO: no configuration for radio %i\n", i); continue; } /* there is an object to configure that radio, let's parse it */ sprintf(param_name, "radio_%i.enable", i); val = json_object_dotget_value(conf, param_name); if (json_value_get_type(val) == JSONBoolean) { rfconf.enable = (bool)json_value_get_boolean(val); } else { rfconf.enable = false; } if (rfconf.enable == false) { /* radio disabled, nothing else to parse */ MSG("INFO: radio %i disabled\n", i); } else { /* radio enabled, will parse the other parameters */ snprintf(param_name, sizeof param_name, "radio_%i.freq", i); rfconf.freq_hz = (uint32_t)json_object_dotget_number(conf, param_name); snprintf(param_name, sizeof param_name, "radio_%i.rssi_offset", i); rfconf.rssi_offset = (float)json_object_dotget_number(conf, param_name); snprintf(param_name, sizeof param_name, "radio_%i.type", i); str = json_object_dotget_string(conf, param_name); if (!strncmp(str, "SX1255", 6)) { rfconf.type = LGW_RADIO_TYPE_SX1255; } else if (!strncmp(str, "SX1257", 6)) { rfconf.type = LGW_RADIO_TYPE_SX1257; } else { MSG("WARNING: invalid radio type: %s (should be SX1255 or SX1257)\n", str); } snprintf(param_name, sizeof param_name, "radio_%i.tx_enable", i); val = json_object_dotget_value(conf, param_name); if (json_value_get_type(val) == JSONBoolean) { rfconf.tx_enable = (bool)json_value_get_boolean(val); } else { rfconf.tx_enable = false; } MSG("INFO: radio %i enabled (type %s), center frequency %u, RSSI offset %f, tx enabled %d\n", i, str, rfconf.freq_hz, rfconf.rssi_offset, rfconf.tx_enable); } /* all parameters parsed, submitting configuration to the HAL */ if (lgw_rxrf_setconf(i, rfconf) != LGW_HAL_SUCCESS) { MSG("WARNING: invalid configuration for radio %i\n", i); } } /* set configuration for LoRa multi-SF channels (bandwidth cannot be set) */ for (i = 0; i < LGW_MULTI_NB; ++i) { memset(&ifconf, 0, sizeof(ifconf)); /* initialize configuration structure */ sprintf(param_name, "chan_multiSF_%i", i); /* compose parameter path inside JSON structure */ val = json_object_get_value(conf, param_name); /* fetch value (if possible) */ if (json_value_get_type(val) != JSONObject) { MSG("INFO: no configuration for LoRa multi-SF channel %i\n", i); continue; } /* there is an object to configure that LoRa multi-SF channel, let's parse it */ sprintf(param_name, "chan_multiSF_%i.enable", i); val = json_object_dotget_value(conf, param_name); if (json_value_get_type(val) == JSONBoolean) { ifconf.enable = (bool)json_value_get_boolean(val); } else { ifconf.enable = false; } if (ifconf.enable == false) { /* LoRa multi-SF channel disabled, nothing else to parse */ MSG("INFO: LoRa multi-SF channel %i disabled\n", i); } else { /* LoRa multi-SF channel enabled, will parse the other parameters */ sprintf(param_name, "chan_multiSF_%i.radio", i); ifconf.rf_chain = (uint32_t)json_object_dotget_number(conf, param_name); sprintf(param_name, "chan_multiSF_%i.if", i); ifconf.freq_hz = (int32_t)json_object_dotget_number(conf, param_name); // TODO: handle individual SF enabling and disabling (spread_factor) MSG("INFO: LoRa multi-SF channel %i enabled, radio %i selected, IF %i Hz, 125 kHz bandwidth, SF 7 to 12\n", i, ifconf.rf_chain, ifconf.freq_hz); } /* all parameters parsed, submitting configuration to the HAL */ if (lgw_rxif_setconf(i, ifconf) != LGW_HAL_SUCCESS) { MSG("WARNING: invalid configuration for LoRa multi-SF channel %i\n", i); } } /* set configuration for LoRa standard channel */ memset(&ifconf, 0, sizeof(ifconf)); /* initialize configuration structure */ val = json_object_get_value(conf, "chan_Lora_std"); /* fetch value (if possible) */ if (json_value_get_type(val) != JSONObject) { MSG("INFO: no configuration for LoRa standard channel\n"); } else { val = json_object_dotget_value(conf, "chan_Lora_std.enable"); if (json_value_get_type(val) == JSONBoolean) { ifconf.enable = (bool)json_value_get_boolean(val); } else { ifconf.enable = false; } if (ifconf.enable == false) { MSG("INFO: LoRa standard channel %i disabled\n", i); } else { ifconf.rf_chain = (uint32_t)json_object_dotget_number(conf, "chan_Lora_std.radio"); ifconf.freq_hz = (int32_t)json_object_dotget_number(conf, "chan_Lora_std.if"); bw = (uint32_t)json_object_dotget_number(conf, "chan_Lora_std.bandwidth"); switch(bw) { case 500000: ifconf.bandwidth = BW_500KHZ; break; case 250000: ifconf.bandwidth = BW_250KHZ; break; case 125000: ifconf.bandwidth = BW_125KHZ; break; default: ifconf.bandwidth = BW_UNDEFINED; } sf = (uint32_t)json_object_dotget_number(conf, "chan_Lora_std.spread_factor"); switch(sf) { case 7: ifconf.datarate = DR_LORA_SF7; break; case 8: ifconf.datarate = DR_LORA_SF8; break; case 9: ifconf.datarate = DR_LORA_SF9; break; case 10: ifconf.datarate = DR_LORA_SF10; break; case 11: ifconf.datarate = DR_LORA_SF11; break; case 12: ifconf.datarate = DR_LORA_SF12; break; default: ifconf.datarate = DR_UNDEFINED; } MSG("INFO: LoRa standard channel enabled, radio %i selected, IF %i Hz, %u Hz bandwidth, SF %u\n", ifconf.rf_chain, ifconf.freq_hz, bw, sf); } if (lgw_rxif_setconf(8, ifconf) != LGW_HAL_SUCCESS) { MSG("WARNING: invalid configuration for LoRa standard channel\n"); } } /* set configuration for FSK channel */ memset(&ifconf, 0, sizeof(ifconf)); /* initialize configuration structure */ val = json_object_get_value(conf, "chan_FSK"); /* fetch value (if possible) */ if (json_value_get_type(val) != JSONObject) { MSG("INFO: no configuration for FSK channel\n"); } else { val = json_object_dotget_value(conf, "chan_FSK.enable"); if (json_value_get_type(val) == JSONBoolean) { ifconf.enable = (bool)json_value_get_boolean(val); } else { ifconf.enable = false; } if (ifconf.enable == false) { MSG("INFO: FSK channel %i disabled\n", i); } else { ifconf.rf_chain = (uint32_t)json_object_dotget_number(conf, "chan_FSK.radio"); ifconf.freq_hz = (int32_t)json_object_dotget_number(conf, "chan_FSK.if"); bw = (uint32_t)json_object_dotget_number(conf, "chan_FSK.bandwidth"); if (bw <= 7800) ifconf.bandwidth = BW_7K8HZ; else if (bw <= 15600) ifconf.bandwidth = BW_15K6HZ; else if (bw <= 31200) ifconf.bandwidth = BW_31K2HZ; else if (bw <= 62500) ifconf.bandwidth = BW_62K5HZ; else if (bw <= 125000) ifconf.bandwidth = BW_125KHZ; else if (bw <= 250000) ifconf.bandwidth = BW_250KHZ; else if (bw <= 500000) ifconf.bandwidth = BW_500KHZ; else ifconf.bandwidth = BW_UNDEFINED; ifconf.datarate = (uint32_t)json_object_dotget_number(conf, "chan_FSK.datarate"); MSG("INFO: FSK channel enabled, radio %i selected, IF %i Hz, %u Hz bandwidth, %u bps datarate\n", ifconf.rf_chain, ifconf.freq_hz, bw, ifconf.datarate); } if (lgw_rxif_setconf(9, ifconf) != LGW_HAL_SUCCESS) { MSG("WARNING: invalid configuration for FSK channel\n"); } } json_value_free(root_val); return 0; }
static mrb_value json_value_to_mrb_value(mrb_state* mrb, JSON_Value* value) { mrb_value ret; switch (json_value_get_type(value)) { case JSONError: case JSONNull: ret = mrb_nil_value(); break; case JSONString: ret = mrb_str_new_cstr(mrb, json_value_get_string(value)); break; case JSONNumber: { double d = json_value_get_number(value); if (floor(d) == d) { ret = mrb_fixnum_value(d); } else { ret = mrb_float_value(mrb, d); } } break; case JSONObject: { mrb_value hash = mrb_hash_new(mrb); JSON_Object* object = json_value_get_object(value); size_t count = json_object_get_count(object); size_t n; for (n = 0; n < count; n++) { int ai = mrb_gc_arena_save(mrb); const char* name = json_object_get_name(object, n); mrb_hash_set(mrb, hash, mrb_str_new_cstr(mrb, name), json_value_to_mrb_value(mrb, json_object_get_value(object, name))); mrb_gc_arena_restore(mrb, ai); } ret = hash; } break; case JSONArray: { mrb_value ary; JSON_Array* array; size_t n, count; ary = mrb_ary_new(mrb); array = json_value_get_array(value); count = json_array_get_count(array); for (n = 0; n < count; n++) { int ai = mrb_gc_arena_save(mrb); JSON_Value* elem = json_array_get_value(array, n); mrb_ary_push(mrb, ary, json_value_to_mrb_value(mrb, elem)); mrb_gc_arena_restore(mrb, ai); } ret = ary; } break; case JSONBoolean: if (json_value_get_boolean(value)) ret = mrb_true_value(); else ret = mrb_false_value(); break; default: mrb_raise(mrb, E_ARGUMENT_ERROR, "invalid argument"); } return ret; }
bud_error_t bud_config_load(bud_config_t* config) { int i; bud_error_t err; JSON_Value* json; JSON_Value* val; JSON_Object* frontend; JSON_Object* obj; JSON_Object* log; JSON_Object* avail; JSON_Array* contexts; if (config->piped) { char* content; ASSERT(config->loop != NULL, "Loop should be present"); err = bud_read_file_by_fd(config->loop, 0, &content); if (!bud_is_ok(err)) goto end; err = bud_hashmap_insert(&config->files.hashmap, kPipedConfigPath, strlen(kPipedConfigPath), content); if (!bud_is_ok(err)) { free(content); goto end; } json = json_parse_string(content); } else if (config->inlined) { json = json_parse_string(config->path); } else { const char* contents; err = bud_config_load_file(config, config->path, &contents); if (!bud_is_ok(err)) goto end; json = json_parse_string(contents); } if (json == NULL) { err = bud_error_dstr(kBudErrJSONParse, config->path); goto end; } obj = json_value_get_object(json); if (obj == NULL) { err = bud_error(kBudErrJSONNonObjectRoot); goto failed_alloc_path; } err = bud_config_load_tracing(&config->trace, json_object_get_object(obj, "tracing")); if (!bud_is_ok(err)) goto failed_alloc_path; /* Allocate contexts and backends */ contexts = json_object_get_array(obj, "contexts"); config->context_count = contexts == NULL ? 0 : json_array_get_count(contexts); config->contexts = calloc(config->context_count + 1, sizeof(*config->contexts)); if (config->contexts == NULL) { err = bud_error_str(kBudErrNoMem, "bud_context_t"); goto failed_alloc_contexts; } config->json = json; /* Workers configuration */ config->worker_count = -1; config->restart_timeout = -1; config->master_ipc = -1; val = json_object_get_value(obj, "workers"); if (val != NULL) config->worker_count = json_value_get_number(val); val = json_object_get_value(obj, "restart_timeout"); if (val != NULL) config->restart_timeout = json_value_get_number(val); val = json_object_get_value(obj, "master_ipc"); if (val != NULL) config->master_ipc = json_value_get_boolean(val); /* Logger configuration */ log = json_object_get_object(obj, "log"); config->log.stdio = -1; config->log.syslog = -1; if (log != NULL) { config->log.level = json_object_get_string(log, "level"); config->log.facility = json_object_get_string(log, "facility"); val = json_object_get_value(log, "stdio"); if (val != NULL) config->log.stdio = json_value_get_boolean(val); val = json_object_get_value(log, "syslog"); if (val != NULL) config->log.syslog = json_value_get_boolean(val); } /* Availability configuration */ avail = json_object_get_object(obj, "availability"); config->availability.death_timeout = -1; config->availability.revive_interval = -1; config->availability.retry_interval = -1; config->availability.max_retries = -1; if (avail != NULL) { val = json_object_get_value(avail, "death_timeout"); if (val != NULL) config->availability.death_timeout = json_value_get_number(val); val = json_object_get_value(avail, "revive_interval"); if (val != NULL) config->availability.revive_interval = json_value_get_number(val); val = json_object_get_value(avail, "retry_interval"); if (val != NULL) config->availability.retry_interval = json_value_get_number(val); val = json_object_get_value(avail, "max_retries"); if (val != NULL) config->availability.max_retries = json_value_get_number(val); } /* Frontend configuration */ frontend = json_object_get_object(obj, "frontend"); err = bud_config_load_frontend(frontend, &config->frontend); if (!bud_is_ok(err)) goto failed_alloc_contexts; /* Load frontend's context */ err = bud_context_load(frontend, &config->contexts[0]); if (!bud_is_ok(err)) goto failed_alloc_contexts; /* Backend configuration */ config->balance = json_object_get_string(obj, "balance"); err = bud_config_load_backend_list(config, obj, &config->contexts[0].backend); if (!bud_is_ok(err)) goto failed_alloc_contexts; /* User and group configuration */ config->user = json_object_get_string(obj, "user"); config->group = json_object_get_string(obj, "group"); /* SNI configuration */ bud_config_read_pool_conf(obj, "sni", &config->sni); /* OCSP Stapling configuration */ bud_config_read_pool_conf(obj, "stapling", &config->stapling); /* SSL Contexts */ /* TODO(indutny): sort them and do binary search */ for (i = 0; i < config->context_count; i++) { bud_context_t* ctx; /* NOTE: contexts[0] - is a default context */ ctx = &config->contexts[i + 1]; obj = json_array_get_object(contexts, i); if (obj == NULL) { err = bud_error(kBudErrJSONNonObjectCtx); goto failed_load_context; } err = bud_context_load(obj, ctx); if (!bud_is_ok(err)) goto failed_load_context; err = bud_config_load_backend_list(config, obj, &ctx->backend); if (!bud_is_ok(err)) goto failed_load_context; } bud_config_set_defaults(config); return bud_config_init(config); failed_load_context: /* Deinitalize contexts */ for (i++; i >= 0; i--) { bud_context_t* ctx; ctx = &config->contexts[i]; free(ctx->backend.list); ctx->backend.list = NULL; } failed_alloc_contexts: free(config->contexts); config->contexts = NULL; free(config->trace.dso); config->trace.dso = NULL; failed_alloc_path: json_value_free(json); config->json = NULL; end: return err; }
bud_error_t bud_sni_from_json(bud_config_t* config, struct json_value_t* json, bud_context_t* ctx) { JSON_Object* obj; JSON_Value* val; const char* cert_str; const char* key_str; const char* pass_str; JSON_Array* cert_strs; JSON_Array* key_strs; JSON_Array* pass_strs; bud_error_t err; cert_str = NULL; key_str = NULL; pass_str = NULL; cert_strs = NULL; key_strs = NULL; pass_strs = NULL; obj = json_value_get_object(json); val = json_object_get_value(obj, "cert"); if (json_value_get_type(val) == JSONString) cert_str = json_value_get_string(val); else cert_strs = json_value_get_array(val); val = json_object_get_value(obj, "key"); if (json_value_get_type(val) == JSONString) key_str = json_value_get_string(val); else key_strs = json_value_get_array(val); val = json_object_get_value(obj, "passphrase"); if (json_value_get_type(val) == JSONString) pass_str = json_value_get_string(val); else pass_strs = json_value_get_array(val); if (obj == NULL || !((cert_str != NULL && key_str != NULL) || (cert_strs != NULL && key_strs != NULL))) { err = bud_error_str(kBudErrJSONParse, "<SNI Response>"); goto fatal; } /* Load NPN from response */ memset(ctx, 0, sizeof(*ctx)); ctx->cert_file = cert_str; ctx->key_file = key_str; ctx->key_pass = pass_str; ctx->cert_files = cert_strs; ctx->key_files = key_strs; ctx->key_passes = pass_strs; ctx->ciphers = json_object_get_string(obj, "ciphers"); ctx->ecdh = json_object_get_string(obj, "ecdh"); ctx->ticket_key = json_object_get_string(obj, "ticket_key"); ctx->npn = json_object_get_array(obj, "npn"); ctx->ca_array = json_object_get_array(obj, "ca"); val = json_object_get_value(obj, "request_cert"); if (val != NULL) ctx->request_cert = json_value_get_boolean(val); err = bud_config_load_backend_list(config, obj, &ctx->backend); if (!bud_is_ok(err)) goto fatal; err = bud_context_init(config, ctx); /* Make sure that deallocated values won't be used */ ctx->cert_file = NULL; ctx->key_file = NULL; ctx->key_pass = NULL; ctx->cert_files = NULL; ctx->key_files = NULL; ctx->key_passes = NULL; if (!bud_is_ok(err)) goto fatal; return bud_ok(); fatal: if (!bud_is_ok(err)) { SSL_CTX_free(ctx->ctx); ctx->ctx = NULL; } free(ctx->backend.list); ctx->backend.list = NULL; return err; }
bud_config_t* bud_config_load(uv_loop_t* loop, const char* path, bud_error_t* err) { int i; int context_count; JSON_Value* json; JSON_Value* val; JSON_Object* obj; JSON_Object* log; JSON_Object* frontend; JSON_Object* backend; JSON_Array* contexts; bud_config_t* config; bud_context_t* ctx; json = json_parse_file(path); if (json == NULL) { *err = bud_error_str(kBudErrJSONParse, path); goto end; } obj = json_value_get_object(json); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectRoot); goto failed_get_object; } contexts = json_object_get_array(obj, "contexts"); context_count = contexts == NULL ? 0 : json_array_get_count(contexts); config = calloc(1, sizeof(*config) + context_count * sizeof(*config->contexts)); if (config == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_config_t"); goto failed_get_object; } config->loop = loop; config->json = json; /* Workers configuration */ config->worker_count = -1; config->restart_timeout = -1; val = json_object_get_value(obj, "workers"); if (val != NULL) config->worker_count = json_value_get_number(val); val = json_object_get_value(obj, "restart_timeout"); if (val != NULL) config->restart_timeout = json_value_get_number(val); /* Logger configuration */ log = json_object_get_object(obj, "log"); config->log.stdio = -1; config->log.syslog = -1; if (log != NULL) { config->log.level = json_object_get_string(log, "level"); config->log.facility = json_object_get_string(log, "facility"); val = json_object_get_value(log, "stdio"); if (val != NULL) config->log.stdio = json_value_get_boolean(val); val = json_object_get_value(log, "syslog"); if (val != NULL) config->log.syslog = json_value_get_boolean(val); } /* Frontend configuration */ frontend = json_object_get_object(obj, "frontend"); config->frontend.proxyline = -1; config->frontend.keepalive = -1; config->frontend.server_preference = -1; config->frontend.ssl3 = -1; if (frontend != NULL) { config->frontend.port = (uint16_t) json_object_get_number(frontend, "port"); config->frontend.host = json_object_get_string(frontend, "host"); config->frontend.security = json_object_get_string(frontend, "security"); config->frontend.npn = json_object_get_array(frontend, "npn"); config->frontend.ciphers = json_object_get_string(frontend, "ciphers"); config->frontend.cert_file = json_object_get_string(frontend, "cert"); config->frontend.key_file = json_object_get_string(frontend, "key"); config->frontend.reneg_window = json_object_get_number(frontend, "reneg_window"); config->frontend.reneg_limit = json_object_get_number(frontend, "reneg_limit"); *err = bud_config_verify_npn(config->frontend.npn); if (!bud_is_ok(*err)) goto failed_get_index; val = json_object_get_value(frontend, "proxyline"); if (val != NULL) config->frontend.proxyline = json_value_get_boolean(val); val = json_object_get_value(frontend, "keepalive"); if (val != NULL) config->frontend.keepalive = json_value_get_number(val); val = json_object_get_value(frontend, "server_preference"); if (val != NULL) config->frontend.server_preference = json_value_get_boolean(val); val = json_object_get_value(frontend, "ssl3"); if (val != NULL) config->frontend.ssl3 = json_value_get_boolean(val); } /* Backend configuration */ backend = json_object_get_object(obj, "backend"); config->backend.keepalive = -1; if (backend != NULL) { config->backend.port = (uint16_t) json_object_get_number(backend, "port"); config->backend.host = json_object_get_string(backend, "host"); val = json_object_get_value(backend, "keepalive"); if (val != NULL) config->backend.keepalive = json_value_get_number(val); } /* SNI configuration */ bud_config_read_pool_conf(obj, "sni", &config->sni); /* OCSP Stapling configuration */ bud_config_read_pool_conf(obj, "stapling", &config->stapling); /* SSL Contexts */ /* TODO(indutny): sort them and do binary search */ for (i = 0; i < context_count; i++) { /* NOTE: contexts[0] - is a default context */ ctx = &config->contexts[i + 1]; obj = json_array_get_object(contexts, i); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectCtx); goto failed_get_index; } ctx->servername = json_object_get_string(obj, "servername"); ctx->servername_len = ctx->servername == NULL ? 0 : strlen(ctx->servername); ctx->cert_file = json_object_get_string(obj, "cert"); ctx->key_file = json_object_get_string(obj, "key"); ctx->npn = json_object_get_array(obj, "npn"); ctx->ciphers = json_object_get_string(obj, "ciphers"); *err = bud_config_verify_npn(ctx->npn); if (!bud_is_ok(*err)) goto failed_get_index; } config->context_count = context_count; bud_config_set_defaults(config); *err = bud_ok(); return config; failed_get_index: free(config); failed_get_object: json_value_free(json); end: return NULL; }
bud_config_t* bud_config_load(const char* path, int inlined, bud_error_t* err) { int i; JSON_Value* json; JSON_Value* val; JSON_Object* obj; JSON_Object* tmp; JSON_Object* log; JSON_Object* avail; JSON_Array* contexts; JSON_Array* backend; bud_config_t* config; bud_context_t* ctx; if (inlined) json = json_parse_string(path); else json = json_parse_file(path); if (json == NULL) { *err = bud_error_str(kBudErrJSONParse, path); goto end; } obj = json_value_get_object(json); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectRoot); goto failed_get_object; } config = calloc(1, sizeof(*config)); if (config == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_config_t"); goto failed_get_object; } /* Copy path or inlined config value */ config->path = strdup(path); if (config->path == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_config_t strcpy(path)"); goto failed_alloc_path; } config->inlined = inlined; /* Allocate contexts and backends */ contexts = json_object_get_array(obj, "contexts"); backend = json_object_get_array(obj, "backend"); config->context_count = contexts == NULL ? 0 : json_array_get_count(contexts); config->backend_count = backend == NULL ? 0 : json_array_get_count(backend); config->contexts = calloc(config->context_count + 1, sizeof(*config->contexts)); config->backend = calloc(config->backend_count, sizeof(*config->backend)); if (config->contexts == NULL || config->backend == NULL) { *err = bud_error_str(kBudErrNoMem, "bud_context_t"); goto failed_get_index; } config->json = json; /* Workers configuration */ config->worker_count = -1; config->restart_timeout = -1; val = json_object_get_value(obj, "workers"); if (val != NULL) config->worker_count = json_value_get_number(val); val = json_object_get_value(obj, "restart_timeout"); if (val != NULL) config->restart_timeout = json_value_get_number(val); /* Logger configuration */ log = json_object_get_object(obj, "log"); config->log.stdio = -1; config->log.syslog = -1; if (log != NULL) { config->log.level = json_object_get_string(log, "level"); config->log.facility = json_object_get_string(log, "facility"); val = json_object_get_value(log, "stdio"); if (val != NULL) config->log.stdio = json_value_get_boolean(val); val = json_object_get_value(log, "syslog"); if (val != NULL) config->log.syslog = json_value_get_boolean(val); } /* Availability configuration */ avail = json_object_get_object(obj, "availability"); config->availability.death_timeout = -1; config->availability.revive_interval = -1; config->availability.retry_interval = -1; config->availability.max_retries = -1; if (avail != NULL) { val = json_object_get_value(avail, "death_timeout"); if (val != NULL) config->availability.death_timeout = json_value_get_number(val); val = json_object_get_value(avail, "revive_interval"); if (val != NULL) config->availability.revive_interval = json_value_get_number(val); val = json_object_get_value(avail, "retry_interval"); if (val != NULL) config->availability.retry_interval = json_value_get_number(val); val = json_object_get_value(avail, "max_retries"); if (val != NULL) config->availability.max_retries = json_value_get_number(val); } /* Frontend configuration */ *err = bud_config_load_frontend(json_object_get_object(obj, "frontend"), &config->frontend); if (!bud_is_ok(*err)) goto failed_get_index; /* Backend configuration */ config->balance = json_object_get_string(obj, "balance"); for (i = 0; i < config->backend_count; i++) { bud_config_load_backend(config, json_array_get_object(backend, i), &config->backend[i]); } /* SNI configuration */ bud_config_read_pool_conf(obj, "sni", &config->sni); /* OCSP Stapling configuration */ bud_config_read_pool_conf(obj, "stapling", &config->stapling); /* SSL Contexts */ /* TODO(indutny): sort them and do binary search */ for (i = 0; i < config->context_count; i++) { /* NOTE: contexts[0] - is a default context */ ctx = &config->contexts[i + 1]; obj = json_array_get_object(contexts, i); if (obj == NULL) { *err = bud_error(kBudErrJSONNonObjectCtx); goto failed_get_index; } ctx->servername = json_object_get_string(obj, "servername"); ctx->servername_len = ctx->servername == NULL ? 0 : strlen(ctx->servername); ctx->cert_file = json_object_get_string(obj, "cert"); ctx->key_file = json_object_get_string(obj, "key"); ctx->npn = json_object_get_array(obj, "npn"); ctx->ciphers = json_object_get_string(obj, "ciphers"); ctx->ecdh = json_object_get_string(obj, "ecdh"); ctx->ticket_key = json_object_get_string(obj, "ticket_key"); tmp = json_object_get_object(obj, "backend"); if (tmp != NULL) { ctx->backend = &ctx->backend_st; bud_config_load_backend(config, tmp, ctx->backend); } *err = bud_config_verify_npn(ctx->npn); if (!bud_is_ok(*err)) goto failed_get_index; } bud_config_set_defaults(config); *err = bud_ok(); return config; failed_get_index: free(config->contexts); config->contexts = NULL; free(config->backend); config->backend = NULL; free(config->path); config->path = NULL; failed_alloc_path: free(config); failed_get_object: json_value_free(json); end: return NULL; }