~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to agent/mibgroup/tcp-mib/data_access/tcpConn_linux.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-10 22:20:23 UTC
  • mto: (1.4.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: james.westby@ubuntu.com-20070510222023-3fr07xb9i17xvq32
Tags: upstream-5.3.1
ImportĀ upstreamĀ versionĀ 5.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  tcpConnTable MIB architecture support
3
3
 *
4
 
 * $Id: tcpConn_linux.c,v 1.2.2.1 2006/06/09 22:49:56 tanders Exp $
 
4
 * $Id: tcpConn_linux.c,v 1.5 2005/12/10 16:35:37 rstory Exp $
5
5
 */
6
6
#include <net-snmp/net-snmp-config.h>
7
7
#include <net-snmp/net-snmp-includes.h>
9
9
#include <net-snmp/agent/net-snmp-agent-includes.h>
10
10
#include <net-snmp/data_access/tcpConn.h>
11
11
 
12
 
#include "tcp-mib/tcpConnTable/tcpConnTable_constants.h"
 
12
#include "tcp-mib/tcpConnectionTable/tcpConnectionTable_constants.h"
 
13
#include "tcp-mib/data_access/tcpConn_private.h"
 
14
 
 
15
static int
 
16
linux_states[12] = { 1, 5, 3, 4, 6, 7, 11, 1, 8, 9, 2, 10 };
 
17
 
 
18
static int _load4(netsnmp_container *container, u_int flags);
 
19
#if defined (INET6)
 
20
static int _load6(netsnmp_container *container, u_int flags);
 
21
#endif
13
22
 
14
23
/*
15
24
 * initialize arch specific storage
59
68
    return -1;
60
69
}
61
70
 
 
71
 
62
72
/**
63
73
 *
64
74
 * @retval  0 no errors
65
75
 * @retval !0 errors
66
76
 */
67
77
int
68
 
netsnmp_arch_tcpconn_container_load(netsnmp_container *container)
 
78
netsnmp_arch_tcpconn_container_load(netsnmp_container *container,
 
79
                                    u_int load_flags )
 
80
{
 
81
    int rc = 0;
 
82
 
 
83
    rc = _load4(container, load_flags);
 
84
    if(rc < 0) {
 
85
        u_int flags = NETSNMP_ACCESS_TCPCONN_FREE_KEEP_CONTAINER;
 
86
        netsnmp_access_tcpconn_container_free(container, flags);
 
87
        return rc;
 
88
    }
 
89
 
 
90
#if defined (INET6)
 
91
    rc = _load6(container, load_flags);
 
92
    if(rc < 0) {
 
93
        u_int flags = NETSNMP_ACCESS_TCPCONN_FREE_KEEP_CONTAINER;
 
94
        netsnmp_access_tcpconn_container_free(container, flags);
 
95
        return rc;
 
96
    }
 
97
#endif
 
98
 
 
99
    return 0;
 
100
}
 
101
 
 
102
/**
 
103
 *
 
104
 * @retval  0 no errors
 
105
 * @retval !0 errors
 
106
 */
 
107
static int
 
108
_load4(netsnmp_container *container, u_int load_flags)
69
109
{
70
110
    int             rc = 0;
71
111
    FILE           *in;
72
112
    char            line[160];
73
 
    u_char          *buf;
74
 
    netsnmp_tcpconn_entry *entry;
75
113
    
76
114
    netsnmp_assert(NULL != container);
77
115
 
89
127
     */
90
128
    while (fgets(line, sizeof(line), in)) {
91
129
        netsnmp_tcpconn_entry *entry;
92
 
        static int      linux_states[12] =
93
 
            { 1, 5, 3, 4, 6, 7, 11, 1, 8, 9, 2, 10 };
94
 
        int             state, rc, local_port, remote_port;
95
 
 
96
 
        /*
97
 
         */
98
 
        entry = netsnmp_access_tcpconn_entry_create();
99
 
        if(NULL == entry) {
100
 
            rc = -3;
101
 
            break;
102
 
        }
103
 
 
104
 
        if (5 != (rc = sscanf(line, "%*d: %x:%x %x:%x %x",
105
 
                              &entry->indexes[NETSNMP_TCPCONN_IDX_LOCAL_ADDR],
106
 
                              &local_port,
107
 
                              &entry->indexes[NETSNMP_TCPCONN_IDX_REMOTE_ADDR],
108
 
                              &remote_port, &state))) {
109
 
            DEBUGMSGT(("access:tcpconn:container",
110
 
                       "error parsing line (%d != 5)\n", rc));
111
 
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
112
 
            netsnmp_access_tcpconn_entry_free(entry);
113
 
            continue;
114
 
        }
115
 
        DEBUGMSGT(("verbose:access:tcpconn:container"," line '%s'\n", line));
116
 
        entry->indexes[NETSNMP_TCPCONN_IDX_LOCAL_PORT] =
117
 
            htons((unsigned short) local_port);
118
 
        entry->indexes[NETSNMP_TCPCONN_IDX_REMOTE_PORT] =
119
 
            htons((unsigned short) remote_port);
120
 
        entry->tcpConnState = (state & 0xf) < 12 ? linux_states[state & 0xf] : 2;
121
 
 
122
 
        /*
123
 
         * add entry to container
124
 
         */
125
 
        CONTAINER_INSERT(container, entry);
126
 
    }
127
 
 
128
 
    fclose(in);
129
 
 
130
 
    if(rc<0)
131
 
        return rc;
132
 
 
133
 
    return 0;
134
 
}
 
130
        int             state, rc, local_port, remote_port, buf_len, offset, tmp_state;
 
131
        u_char          local_addr[10], remote_addr[10];
 
132
        u_char         *tmp_ptr;
 
133
 
 
134
        if (5 != (rc = sscanf(line, "%*d: %8[0-9A-Z]:%x %8[0-9A-Z]:%x %x",
 
135
                              local_addr, &local_port,
 
136
                              remote_addr, &remote_port, &tmp_state))) {
 
137
            DEBUGMSGT(("access:tcpconn:container",
 
138
                       "error parsing line (%d != 5)\n", rc));
 
139
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
 
140
            continue;
 
141
        }
 
142
        DEBUGMSGT(("verbose:access:tcpconn:container"," line '%s'\n", line));
 
143
 
 
144
        /*
 
145
         * check if we care about listen state
 
146
         */
 
147
        state = (tmp_state & 0xf) < 12 ? linux_states[tmp_state & 0xf] : 2;
 
148
        if (load_flags) {
 
149
            if (TCPCONNECTIONSTATE_LISTEN == state) {
 
150
                if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) {
 
151
                    DEBUGMSGT(("verbose:access:tcpconn:container",
 
152
                               " skipping listen\n"));
 
153
                    continue;
 
154
                }
 
155
            }
 
156
            else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) {
 
157
                    DEBUGMSGT(("verbose:access:tcpconn:container",
 
158
                               " skipping non-listen\n"));
 
159
                    continue;
 
160
            }
 
161
        }
 
162
 
 
163
        /*
 
164
         */
 
165
        entry = netsnmp_access_tcpconn_entry_create();
 
166
        if(NULL == entry) {
 
167
            rc = -3;
 
168
            break;
 
169
        }
 
170
 
 
171
        entry->loc_port = htons((unsigned short) local_port);
 
172
        entry->rmt_port = htons((unsigned short) remote_port);
 
173
        entry->tcpConnState = state;
 
174
        buf_len = strlen(local_addr);
 
175
        netsnmp_assert(8 == buf_len);
 
176
        offset = 0;
 
177
        tmp_ptr = entry->loc_addr;
 
178
        rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len,
 
179
                                   &offset, 0, local_addr, NULL);
 
180
        entry->loc_addr_len = offset;
 
181
        if ( 4 != entry->loc_addr_len ) {
 
182
            DEBUGMSGT(("access:tcpconn:container",
 
183
                       "error parsing local addr (%d != 4)\n",
 
184
                       entry->loc_addr_len));
 
185
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
 
186
            netsnmp_access_tcpconn_entry_free(entry);
 
187
            continue;
 
188
        }
 
189
 
 
190
        buf_len = strlen(remote_addr);
 
191
        netsnmp_assert(8 == buf_len);
 
192
        offset = 0;
 
193
        tmp_ptr = entry->rmt_addr;
 
194
        rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len,
 
195
                                   &offset, 0, remote_addr, NULL);
 
196
        entry->rmt_addr_len = offset;
 
197
        if ( 4 != entry->rmt_addr_len ) {
 
198
            DEBUGMSGT(("access:tcpconn:container",
 
199
                       "error parsing remote addr (%d != 4)\n",
 
200
                       entry->rmt_addr_len));
 
201
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
 
202
            netsnmp_access_tcpconn_entry_free(entry);
 
203
            continue;
 
204
        }
 
205
 
 
206
        /*
 
207
         * add entry to container
 
208
         */
 
209
        entry->arbitrary_index = CONTAINER_SIZE(container) + 1;
 
210
        CONTAINER_INSERT(container, entry);
 
211
    }
 
212
 
 
213
    fclose(in);
 
214
 
 
215
    if(rc<0)
 
216
        return rc;
 
217
 
 
218
    return 0;
 
219
}
 
220
 
 
221
#if defined (INET6)
 
222
/**
 
223
 *
 
224
 * @retval  0 no errors
 
225
 * @retval !0 errors
 
226
 */
 
227
static int
 
228
_load6(netsnmp_container *container, u_int load_flags)
 
229
{
 
230
    int             rc = 0;
 
231
    FILE           *in;
 
232
    char            line[180];
 
233
    
 
234
    netsnmp_assert(NULL != container);
 
235
 
 
236
#undef PROCFILE
 
237
#define PROCFILE "/proc/net/tcp6"
 
238
    if (!(in = fopen(PROCFILE, "r"))) {
 
239
        snmp_log(LOG_ERR,"could not open " PROCFILE "\n");
 
240
        return -2;
 
241
    }
 
242
    
 
243
    fgets(line, sizeof(line), in); /* skip header */
 
244
 
 
245
    /*
 
246
     *   sl  local_address rem_address   st tx_queue rx_queue tr tm->when retrnsmt   uid  timeout inode
 
247
     *  0: 00000000000000000000000000000001:1466 00000000000000000000000000000000:0000 0A 00000000:00000000 00:00000000 00000000   500        0 326699 1 efb81580 3000 0 0 2 -1
 
248
     */
 
249
    while (fgets(line, sizeof(line), in)) {
 
250
        netsnmp_tcpconn_entry *entry;
 
251
        int             state, rc, local_port, remote_port, buf_len, offset,
 
252
                        tmp_state;
 
253
        u_char          local_addr[48], remote_addr[48];
 
254
        u_char         *tmp_ptr;
 
255
 
 
256
        if (5 != (rc = sscanf(line, "%*d: %47[0-9A-Z]:%x %47[0-9A-Z]:%x %x",
 
257
                              local_addr, &local_port,
 
258
                              remote_addr, &remote_port, &tmp_state))) {
 
259
            DEBUGMSGT(("access:tcpconn:container",
 
260
                       "error parsing line (%d != 5)\n", rc));
 
261
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
 
262
            continue;
 
263
        }
 
264
        DEBUGMSGT(("verbose:access:tcpconn:container"," line '%s'\n", line));
 
265
 
 
266
        /*
 
267
         * check if we care about listen state
 
268
         */
 
269
        state = (tmp_state & 0xf) < 12 ? linux_states[tmp_state & 0xf] : 2;
 
270
        if (load_flags) {
 
271
            if (TCPCONNECTIONSTATE_LISTEN == state) {
 
272
                if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_NOLISTEN) {
 
273
                    DEBUGMSGT(("verbose:access:tcpconn:container",
 
274
                               " skipping listen\n"));
 
275
                    continue;
 
276
                }
 
277
            }
 
278
            else if (load_flags & NETSNMP_ACCESS_TCPCONN_LOAD_ONLYLISTEN) {
 
279
                    DEBUGMSGT(("verbose:access:tcpconn:container",
 
280
                               " skipping non-listen\n"));
 
281
                    continue;
 
282
            }
 
283
        }
 
284
 
 
285
        /*
 
286
         */
 
287
        entry = netsnmp_access_tcpconn_entry_create();
 
288
        if(NULL == entry) {
 
289
            rc = -3;
 
290
            break;
 
291
        }
 
292
 
 
293
        entry->loc_port = htons((unsigned short) local_port);
 
294
        entry->rmt_port = htons((unsigned short) remote_port);
 
295
        entry->tcpConnState = state;
 
296
 
 
297
        buf_len = strlen(local_addr);
 
298
        offset = 0;
 
299
        tmp_ptr = entry->loc_addr;
 
300
        rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len,
 
301
                                   &offset, 0, local_addr, NULL);
 
302
        entry->loc_addr_len = offset;
 
303
        if (( 16 != entry->loc_addr_len ) && ( 20 != entry->loc_addr_len )) {
 
304
            DEBUGMSGT(("access:tcpconn:container",
 
305
                       "error parsing local addr (%d != 16|20)\n",
 
306
                       entry->loc_addr_len));
 
307
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
 
308
            netsnmp_access_tcpconn_entry_free(entry);
 
309
            continue;
 
310
        }
 
311
 
 
312
        buf_len = strlen(remote_addr);
 
313
        offset = 0;
 
314
        tmp_ptr = entry->rmt_addr;
 
315
        rc = netsnmp_hex_to_binary(&tmp_ptr, &buf_len,
 
316
                                   &offset, 0, remote_addr, NULL);
 
317
        entry->rmt_addr_len = offset;
 
318
        if (( 16 != entry->rmt_addr_len ) && ( 20 != entry->rmt_addr_len )) {
 
319
            DEBUGMSGT(("access:tcpconn:container",
 
320
                       "error parsing remote addr (%d != 16|20)\n",
 
321
                       entry->rmt_addr_len));
 
322
            DEBUGMSGT(("access:tcpconn:container"," line '%s'\n", line));
 
323
            netsnmp_access_tcpconn_entry_free(entry);
 
324
            continue;
 
325
        }
 
326
 
 
327
 
 
328
        /*
 
329
         * add entry to container
 
330
         */
 
331
        entry->arbitrary_index = CONTAINER_SIZE(container) + 1;
 
332
        CONTAINER_INSERT(container, entry);
 
333
    }
 
334
 
 
335
    fclose(in);
 
336
 
 
337
    if(rc<0)
 
338
        return rc;
 
339
 
 
340
    return 0;
 
341
}
 
342
#endif /* INET6 */