+static int snmp_agent_fill_index_list(table_definition_t *td,
+ value_list_t const *vl) {
+ int ret;
+ int i;
+ netsnmp_variable_list *key = td->index_list_cont;
+ char const *ptr;
+
+ for (i = 0; i < td->index_keys_len; i++) {
+ /* var should never be NULL */
+ assert(key != NULL);
+ ptr = NULL;
+ ret = 0;
+ const index_key_src_t source = td->index_keys[i].source;
+ c_avl_tree_t *const tokens = td->tokens[source];
+ /* Generating list filled with all data necessary to generate an OID */
+ switch (source) {
+ case INDEX_HOST:
+ ptr = vl->host;
+ break;
+ case INDEX_PLUGIN:
+ ptr = vl->plugin;
+ break;
+ case INDEX_PLUGIN_INSTANCE:
+ ptr = vl->plugin_instance;
+ break;
+ case INDEX_TYPE:
+ ptr = vl->type;
+ break;
+ case INDEX_TYPE_INSTANCE:
+ ptr = vl->type_instance;
+ break;
+ default:
+ ERROR(PLUGIN_NAME ": Unknown index key source provided");
+ return -EINVAL;
+ }
+ if (ret != 0)
+ return -EINVAL;
+
+ /* Parsing input string if necessary */
+ if (td->index_keys[i].regex) {
+ regmatch_t m = {-1, -1};
+
+ /* Parsing input string */
+ ret = snmp_agent_parse_index_key(ptr, &td->index_keys[i].regex_info,
+ td->index_keys[i].group, &m);
+ if (ret != 0) {
+ ERROR(PLUGIN_NAME ": Error executing regex");
+ return ret;
+ }
+
+ /* Tokenizing input string if not done yet */
+ if (td->tokens_done == false)
+ ret = snmp_agent_tokenize(ptr, tokens, &m, key);
+
+ if (ret != 0)
+ return -1;
+
+ if (td->index_keys[i].type == ASN_INTEGER) {
+ int val = strtol(ptr + m.rm_so, NULL, 0);
+
+#ifdef HAVE_NETSNMP_OLD_API
+ ret = snmp_set_var_value(key, (const u_char *)&val, sizeof(val));
+#else
+ ret = snmp_set_var_value(key, &val, sizeof(val));
+#endif
+ } else
+#ifdef HAVE_NETSNMP_OLD_API
+ ret = snmp_set_var_value(key, (const u_char *)(ptr + m.rm_so),
+ m.rm_eo - m.rm_so);
+#else
+ ret = snmp_set_var_value(key, ptr + m.rm_so, m.rm_eo - m.rm_so);
+#endif
+ } else
+#ifdef HAVE_NETSNMP_OLD_API
+ ret = snmp_set_var_value(key, (const u_char *)ptr, strlen(ptr));
+#else
+ ret = snmp_set_var_value(key, ptr, strlen(ptr));
+#endif
+ key = key->next_variable;
+ }
+
+ /* Tokens for all source strings are generated */
+ for (i = 0; i < MAX_KEY_SOURCES; i++)
+ td->tokens_done = true;
+
+ return 0;
+}
+
+static int snmp_agent_prep_index_list(table_definition_t const *td,
+ netsnmp_variable_list **index_list) {
+ /* Generating list having only the structure (with no values) letting us
+ * know how to parse an OID*/
+ for (int i = 0; i < td->index_keys_len; i++) {
+ switch (td->index_keys[i].source) {
+ case INDEX_HOST:
+ case INDEX_PLUGIN:
+ case INDEX_PLUGIN_INSTANCE:
+ case INDEX_TYPE:
+ case INDEX_TYPE_INSTANCE:
+ snmp_varlist_add_variable(index_list, NULL, 0, td->index_keys[i].type,
+ NULL, 0);
+ break;
+ default:
+ ERROR(PLUGIN_NAME ": Unknown index key source provided");
+ return -EINVAL;
+ }
+ }
+ return 0;
+}
+
+static int snmp_agent_generate_index(table_definition_t *td,
+ value_list_t const *vl, oid_t *index_oid) {
+
+ /* According to given information by index_keys list
+ * index OID is going to be built
+ */
+ int ret = snmp_agent_fill_index_list(td, vl);
+ if (ret != 0)
+ return -EINVAL;
+
+ /* Building only index part OID (without table prefix OID) */
+ ret = build_oid_noalloc(index_oid->oid, sizeof(index_oid->oid),
+ &index_oid->oid_len, NULL, 0, td->index_list_cont);
+ if (ret != SNMPERR_SUCCESS) {
+ ERROR(PLUGIN_NAME ": Error building index OID");
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+/* It appends one OID to the end of another */
+static int snmp_agent_append_oid(oid_t *out, const oid_t *in) {
+
+ if (out->oid_len + in->oid_len > MAX_OID_LEN) {
+ ERROR(PLUGIN_NAME ": Cannot create OID. Output length is too long!");
+ return -EINVAL;
+ }
+ memcpy(&out->oid[out->oid_len], in->oid, in->oid_len * sizeof(oid));
+ out->oid_len += in->oid_len;
+
+ return 0;
+}
+
+static int snmp_agent_register_oid_string(const oid_t *oid,
+ const oid_t *index_oid,