summaryrefslogtreecommitdiff
path: root/agent/mibgroup/tcp-mib/data_access/tcpConn_common.c
blob: bba01fe63d28e5bdaeb2af14ca3376bf051cf4d3 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 *  TcpConn MIB architecture support
 *
 * $Id$
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/data_access/tcpConn.h>

#include "tcp-mib/tcpConnectionTable/tcpConnectionTable_constants.h"
#include "tcp-mib/data_access/tcpConn_private.h"

/**---------------------------------------------------------------------*/
/*
 * local static prototypes
 */
static void _access_tcpconn_entry_release(netsnmp_tcpconn_entry * entry,
                                            void *unused);

/**---------------------------------------------------------------------*/
/*
 * external per-architecture functions prototypes
 *
 * These shouldn't be called by the general public, so they aren't in
 * the header file.
 */
extern int
netsnmp_arch_tcpconn_container_load(netsnmp_container* container,
                                      u_int load_flags);
extern int
netsnmp_arch_tcpconn_entry_init(netsnmp_tcpconn_entry *entry);
extern int
netsnmp_arch_tcpconn_entry_copy(netsnmp_tcpconn_entry *lhs,
                                  netsnmp_tcpconn_entry *rhs);
extern void
netsnmp_arch_tcpconn_entry_cleanup(netsnmp_tcpconn_entry *entry);



/**---------------------------------------------------------------------*/
/*
 * container functions
 */
/**
 */
netsnmp_container *
netsnmp_access_tcpconn_container_init(u_int flags)
{
    netsnmp_container *container1;

    DEBUGMSGTL(("access:tcpconn:container", "init\n"));

    /*
     * create the container
     */
    container1 = netsnmp_container_find("access:tcpconn:table_container");
    if (NULL == container1) {
        snmp_log(LOG_ERR, "tcpconn primary container not found\n");
        return NULL;
    }
    container1->container_name = strdup("tcpConnTable");

    return container1;
}

/**
 * @retval NULL  error
 * @retval !NULL pointer to container
 */
netsnmp_container*
netsnmp_access_tcpconn_container_load(netsnmp_container* container, u_int load_flags)
{
    int rc;

    DEBUGMSGTL(("access:tcpconn:container", "load\n"));

    if (NULL == container)
        container = netsnmp_access_tcpconn_container_init(load_flags);
    if (NULL == container) {
        snmp_log(LOG_ERR, "no container specified/found for access_tcpconn\n");
        return NULL;
    }

    rc =  netsnmp_arch_tcpconn_container_load(container, load_flags);
    if (0 != rc) {
        netsnmp_access_tcpconn_container_free(container,
                                                NETSNMP_ACCESS_TCPCONN_FREE_NOFLAGS);
        container = NULL;
    }

    return container;
}

void
netsnmp_access_tcpconn_container_free(netsnmp_container *container, u_int free_flags)
{
    DEBUGMSGTL(("access:tcpconn:container", "free\n"));

    if (NULL == container) {
        snmp_log(LOG_ERR, "invalid container for netsnmp_access_tcpconn_free\n");
        return;
    }

    if(! (free_flags & NETSNMP_ACCESS_TCPCONN_FREE_DONT_CLEAR)) {
        /*
         * free all items.
         */
        CONTAINER_CLEAR(container,
                        (netsnmp_container_obj_func*)_access_tcpconn_entry_release,
                        NULL);
    }

    if(! (free_flags & NETSNMP_ACCESS_TCPCONN_FREE_KEEP_CONTAINER))
        CONTAINER_FREE(container);
}

/**---------------------------------------------------------------------*/
/*
 * tcpconn_entry functions
 */
/**
 */
/**
 */
netsnmp_tcpconn_entry *
netsnmp_access_tcpconn_entry_create(void)
{
    netsnmp_tcpconn_entry *entry =
        SNMP_MALLOC_TYPEDEF(netsnmp_tcpconn_entry);
    int rc = 0;

    DEBUGMSGTL(("verbose:access:tcpconn:entry", "create\n"));

    entry->oid_index.len = 1;
    entry->oid_index.oids = &entry->arbitrary_index;

    /*
     * init arch data
     */
    rc = netsnmp_arch_tcpconn_entry_init(entry);
    if (SNMP_ERR_NOERROR != rc) {
        DEBUGMSGT(("access:tcpconn:create","error %d in arch init\n", rc));
        netsnmp_access_tcpconn_entry_free(entry);
    }

    return entry;
}

/**
 */
void
netsnmp_access_tcpconn_entry_free(netsnmp_tcpconn_entry * entry)
{
    if (NULL == entry)
        return;

    DEBUGMSGTL(("verbose:access:tcpconn:entry", "free\n"));

    if (NULL != entry->arch_data)
        netsnmp_arch_tcpconn_entry_cleanup(entry);

    free(entry);
}

#ifdef TCPCONN_DELETE_SUPPORTED

/* XXX TODO: these are currently unsupported everywhere; to enable the
   functions first implement netsnmp_arch_tcpconn_entry_delete in the
   tcpConn_{OS}.c file and then define TCPCONN_DELETE_SUPPORTED in the
   tcpConn_{OS}.h file (which may need to be created first). */

/**
 * update underlying data store (kernel) for entry
 *
 * @retval  0 : success
 * @retval -1 : error
 */
int
netsnmp_access_tcpconn_entry_set(netsnmp_tcpconn_entry * entry)
{
    int rc = SNMP_ERR_NOERROR;

    if (NULL == entry) {
        netsnmp_assert(NULL != entry);
        return -1;
    }
    
    DEBUGMSGTL(("access:tcpconn:entry", "set\n"));

   
    /*
     * only option is delete
     */
    if (! (entry->flags & NETSNMP_ACCESS_TCPCONN_DELETE))
        return -1;
    
    rc = netsnmp_arch_tcpconn_entry_delete(entry);
    
    return rc;
}
#endif /* TCPCONN_DELETE_SUPPORTED */

/**
 * update an old tcpconn_entry from a new one
 *
 * @note: only mib related items are compared. Internal objects
 * such as oid_index, ns_ia_index and flags are not compared.
 *
 * @retval -1  : error
 * @retval >=0 : number of fileds updated
 */
int
netsnmp_access_tcpconn_entry_update(netsnmp_tcpconn_entry *lhs,
                                      netsnmp_tcpconn_entry *rhs)
{
    int rc, changed = 0;

    DEBUGMSGTL(("access:tcpconn:entry", "update\n"));

    if (lhs->tcpConnState != rhs->tcpConnState) {
        ++changed;
        lhs->tcpConnState = rhs->tcpConnState;
    }

    if (lhs->pid != rhs->pid) {
        ++changed;
        lhs->pid = rhs->pid;
    }

    /*
     * copy arch stuff. we don't care if it changed
     */
    rc = netsnmp_arch_tcpconn_entry_copy(lhs,rhs);
    if (0 != rc) {
        snmp_log(LOG_ERR,"arch tcpconn copy failed\n");
        return -1;
    }

    return changed;
}

/**---------------------------------------------------------------------*/
/*
 * Utility routines
 */

/**
 */
void
_access_tcpconn_entry_release(netsnmp_tcpconn_entry * entry, void *context)
{
    netsnmp_access_tcpconn_entry_free(entry);
}


#ifdef NETSNMP_TCPCONN_TEST

int
main(int argc, char** argv)
{
    netsnmp_container *container;

    netsnmp_config("debugTokens access:tcp,verbose:access:tcp,tcp,verbose:tcp");

    netsnmp_container_init_list();

    dodebug = 1;

    container = netsnmp_access_tcpconn_container_load(NULL, 0);
    
    netsnmp_access_tcpconn_container_free(container, 0);
}
#endif