summaryrefslogtreecommitdiff
path: root/src/bsnmp-regex.c
blob: dca188a46195702aca73387ee5cbcc23dbecd091 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

#include "usuals.h"

#include <sys/queue.h>
#include <sys/select.h>
#include <syslog.h>
#include <unistd.h>

#include <bsnmp/snmpmod.h>

#include "regex_tree.h"
#include "regex_oid.h"

/* our module handle */
static struct lmodule *module;

/* OIDs */
static const struct asn_oid oid_regex = OIDX_regexData;

/* the Object Resource registration index */
static u_int reg_index;

struct data_entry {
    uint32_t                index;
    TAILQ_ENTRY(data_entry) link;

    char                    *descr;
    uint64_t                last_update;
    uint64_t                value;
};

TAILQ_HEAD(data_entry_list, data_entry);

/* list of peers */
static struct data_entry_list entries = TAILQ_HEAD_INITIALIZER(entries);
static uint32_t entry_count = 0;

/* the initialisation function */
static int
module_init(struct lmodule *mod, int argc, char *argv[])
{
    module = mod;

    if (argc != 0) {
        syslog(LOG_ERR, "bad number of arguments for %s", __func__);
        return (EINVAL);
    }

    return 0;
}

/* Module is started */
static void
module_start(void)
{
    reg_index = or_register(&oid_regex, "The MIB for regex data.", module);
}

/* Called, when the module is to be unloaded after it was successfully loaded */
static int
module_fini(void)
{
    or_unregister(reg_index);
    return 0;
}

const struct snmp_module config = {
    .comment =      "This module implements SNMP listing of data from regular expressions",
    .init =         module_init,
    .start =        module_start,
    .fini =         module_fini,
    .tree =         regex_ctree,
    .tree_size =    regex_CTREE_SIZE,
};

/* System variables - read-only scalars only. */
int
op_data (struct snmp_context *ctx, struct snmp_value *value,
         u_int sub, u_int iidx, enum snmp_op op)
{
    asn_subid_t which = value->var.subs[sub - 1];

    switch (op) {
    case SNMP_OP_GET:
        break;

    case SNMP_OP_SET:
        return SNMP_ERR_NOT_WRITEABLE;

    default:
        ASSERT(0);
        break;
    }

    switch (which) {
    case LEAF_dataCount:
        value->v.integer = entry_count;
        break;

    default:
        ASSERT(0);
        break;
    }

    return SNMP_ERR_NOERROR;
}

int
op_dataentry (struct snmp_context *ctx, struct snmp_value *value,
             u_int sub, u_int iidx, enum snmp_op op)
{
    asn_subid_t which = value->var.subs[sub - 1];
    struct data_entry *data;

    switch (op) {
    case SNMP_OP_GETNEXT:
        data = NEXT_OBJECT_INT (&entries, &value->var, sub);
        if (data == NULL)
            return SNMP_ERR_NOSUCHNAME;
        value->var.len = sub + 1;
        value->var.subs[sub] = data->index;
        break;

    case SNMP_OP_GET:
        data = FIND_OBJECT_INT (&entries, &value->var, sub);
        if (data == NULL)
            return SNMP_ERR_NOSUCHNAME;
        break;

    case SNMP_OP_SET:
        if (index_decode (&value->var, sub, iidx, &data))
            return SNMP_ERR_NO_CREATION;
        data = FIND_OBJECT_INT (&entries, &value->var, sub);
        if (data != NULL)
            return SNMP_ERR_NOT_WRITEABLE;
        return SNMP_ERR_NO_CREATION;

    default:
        ASSERT(0);
        break;
    }

    switch (which) {
    case LEAF_dataIndex:
        value->v.integer = data->index;
        break;

    case LEAF_dataDescr:
        return (string_get (value, data->descr, -1));

    case LEAF_dataLast:
        value->v.uint32 = data->last_update;
        break;

    case LEAF_dataValue:
        value->v.uint32 = data->value;
        break;

    default:
        ASSERT(0);
        break;
    };

    return SNMP_ERR_NOERROR;
}