summaryrefslogtreecommitdiff
path: root/agent/mibgroup/ip-mib/data_access/ipaddress_solaris2.c
blob: e2485e33c6c5cac05234dd0db652d2c5f41dc74c (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 *  IP-MIB architecture support
 *
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-features.h>
#include <net-snmp/net-snmp-includes.h>

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

#include "ip-mib/ipAddressTable/ipAddressTable_constants.h"

#include "kernel_sunos5.h"
#include "mibII/mibII_common.h"

netsnmp_feature_child_of(ipaddress_arch_entry_copy, ipaddress_common)

static int _load_v4(netsnmp_container *container, int idx_offset);
#if defined( NETSNMP_ENABLE_IPV6 )
static int _load_v6(netsnmp_container *container, int idx_offset);
#endif

/*
 * initialize arch specific storage
 *
 * @retval  0: success
 * @retval <0: error
 */
int
netsnmp_arch_ipaddress_entry_init(netsnmp_ipaddress_entry *entry)
{
    init_kernel_sunos5();
    return 0;
}

/*
 * cleanup arch specific storage
 */
void
netsnmp_arch_ipaddress_entry_cleanup(netsnmp_ipaddress_entry *entry)
{
    /*
     * Nothing to do.
     */
}

#ifndef NETSNMP_FEATURE_REMOVE_IPADDRESS_ARCH_ENTRY_COPY
/*
 * copy arch specific storage
 */
int
netsnmp_arch_ipaddress_entry_copy(netsnmp_ipaddress_entry *lhs,
                                  netsnmp_ipaddress_entry *rhs)
{
    /*
     * Nothing to do. 
     */
    return 0;
}
#endif /* NETSNMP_FEATURE_REMOVE_IPADDRESS_ARCH_ENTRY_COPY */

/*
 * create a new entry
 */
int
netsnmp_arch_ipaddress_create(netsnmp_ipaddress_entry *entry)
{
    if (NULL == entry)
        return -1;

    DEBUGMSGT(("access:ipaddress:create", "not applicable\n"));
        return 0;
}

/*
 * delete an entry
 */
int
netsnmp_arch_ipaddress_delete(netsnmp_ipaddress_entry *entry)
{
    if (NULL == entry)
        return -1;

    DEBUGMSGT(("access:ipaddress:create", "not applicable\n"));
    return 0;
}

/**
 *
 * @retval  0 no errors
 * @retval !0 errors
 */
int
netsnmp_arch_ipaddress_container_load(netsnmp_container *container,
                                      u_int load_flags)
{
    int rc = 0, idx_offset = 0;

    if (!(load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_IPV6_ONLY)) {
        rc = _load_v4(container, idx_offset);
        if(rc < 0) {
            u_int flags = NETSNMP_ACCESS_IPADDRESS_FREE_KEEP_CONTAINER;
            netsnmp_access_ipaddress_container_free(container, flags);
        }
    }

#if defined( NETSNMP_ENABLE_IPV6 )

    if (!(load_flags & NETSNMP_ACCESS_IPADDRESS_LOAD_IPV4_ONLY)) {
        if (rc < 0)
            rc = 0;

        idx_offset = rc;

        rc = _load_v6(container, idx_offset);
        if(rc < 0) {
            u_int flags = NETSNMP_ACCESS_IPADDRESS_FREE_KEEP_CONTAINER;
            netsnmp_access_ipaddress_container_free(container, flags);
        }
    }
#endif

    /*
     * return no errors (0) if we found any interfaces
     */
    if(rc > 0)
        rc = 0;
    return rc;
}

/*
 * @retval >=idx_offset ok
 * @retval -1 memory allocation error
 * @retval -2 interface lookup error
 * @retval -3 container error
 */
static int
_load_v4(netsnmp_container *container, int idx_offset)
{
    mib2_ipAddrEntry_t      ipae;
    netsnmp_ipaddress_entry *entry;
    req_e                   req = GET_FIRST;
    int                     rc = 0;

    DEBUGMSGTL(("access:ipaddress:container", "loading v4\n"));
    while ((rc = getMibstat(MIB_IP_ADDR, &ipae, sizeof(ipae), req,
                            &Get_everything, NULL)) == 0) {
        req = GET_NEXT;
        entry = netsnmp_access_ipaddress_entry_create();
        if (entry == NULL)
            return (-1);    
        if (ipae.ipAdEntAddr == INADDR_ANY)
            continue;

        ipae.ipAdEntIfIndex.o_bytes[ipae.ipAdEntIfIndex.o_length] = '\0';
        DEBUGMSGTL(("access:ipaddress:container", "found if %s\n",
                    ipae.ipAdEntIfIndex.o_bytes));
        /* Obtain interface index */
        entry->if_index = 
            netsnmp_access_interface_index_find(ipae.ipAdEntIfIndex.o_bytes);
        if (entry->if_index == 0) {
            DEBUGMSGTL(("access:ipaddress:container", "cannot find if %s\n",
                        ipae.ipAdEntIfIndex.o_bytes));
            netsnmp_access_ipaddress_entry_free(entry);
            return (-2);    
        }

        if (strchr((const char *)&ipae.ipAdEntIfIndex.o_bytes, ':') != 0)
            entry->flags |= NETSNMP_ACCESS_IPADDRESS_ISALIAS;

        /* Get the address */
        entry->ia_address_len = sizeof(ipae.ipAdEntAddr);
        netsnmp_assert(entry->ia_address_len == 4 &&
            entry->ia_address_len <= sizeof(entry->ia_address));
        memcpy(&entry->ia_address, &ipae.ipAdEntAddr, entry->ia_address_len);

        /* prefix */
        entry->ia_prefix_len = ipae.ipAdEntInfo.ae_subnet_len;

        /* set the Origin */
        if (ipae.ipAdEntInfo.ae_flags & IFF_DHCPRUNNING)
            entry->ia_origin = IPADDRESSORIGINTC_DHCP;
        else
            entry->ia_origin = IPADDRESSORIGINTC_MANUAL;

        /* set ipv4 constants */
        entry->ia_type = IPADDRESSTYPE_UNICAST;
        entry->ia_status = IPADDRESSSTATUSTC_PREFERRED;

        entry->ns_ia_index = ++idx_offset;

        DEBUGMSGTL(("access:ipaddress:container", "insert if %" NETSNMP_PRIo "u, addrlen %d\n", 
                    entry->if_index, entry->ia_address_len));

        if (CONTAINER_INSERT(container, entry) < 0) {
            DEBUGMSGTL(("access:ipaddress:container", "unable to insert %s\n", 
                        ipae.ipAdEntIfIndex.o_bytes));
            netsnmp_access_ipaddress_entry_free(entry);
            return (-3);
        }
    }
    return (idx_offset);
}

/*
 * @retval >=idx_offset ok
 * @retval -1 memory allocation error
 * @retval -2 interface lookup error
 * @retval -3 container error
 */
#if defined( NETSNMP_ENABLE_IPV6 )
static int
_load_v6(netsnmp_container *container, int idx_offset)
{
    mib2_ipv6AddrEntry_t    ip6ae;
    netsnmp_ipaddress_entry *entry;
    req_e                   req = GET_FIRST;
    int                     rc = 0;

    DEBUGMSGTL(("access:ipaddress:container", "loading v6... cache %d\n",
                MIB_IP6_ADDR));
    while ((rc = getMibstat(MIB_IP6_ADDR, &ip6ae, sizeof(ip6ae), req,
                            &Get_everything, NULL)) == 0) {
        req = GET_NEXT;
        entry = netsnmp_access_ipaddress_entry_create();
        if (entry == NULL)
            return (-1);    
        if (memcmp((const void *)&ip6ae.ipv6AddrAddress,
                   (const void *)&in6addr_any,
                   sizeof (ip6ae.ipv6AddrAddress)) == 0)
            continue;

        ip6ae.ipv6AddrIfIndex.o_bytes[ip6ae.ipv6AddrIfIndex.o_length] = '\0';
        DEBUGMSGTL(("access:ipaddress:container", "found if %s\n",
                    ip6ae.ipv6AddrIfIndex.o_bytes));

        /* Obtain interface index */
        entry->if_index = 
            netsnmp_access_interface_index_find(
            ip6ae.ipv6AddrIfIndex.o_bytes);
        if (entry->if_index == 0) {
            DEBUGMSGTL(("access:ipaddress:container", "cannot find if %s\n", 
                        ip6ae.ipv6AddrIfIndex.o_bytes));
            netsnmp_access_ipaddress_entry_free(entry);
            return (-2);    
        }

        /* Get the address */
        entry->ia_address_len = sizeof(ip6ae.ipv6AddrAddress);
        netsnmp_assert(entry->ia_address_len == 16 &&
                       entry->ia_address_len <= sizeof(entry->ia_address));
        memcpy(&entry->ia_address, &ip6ae.ipv6AddrAddress, 
               entry->ia_address_len);
               
        /* prefix */
        entry->ia_prefix_len = ip6ae.ipv6AddrPfxLength;

        /* type is anycast? (mib2.h: 1 = yes, 2 = no) */
        entry->ia_type = (ip6ae.ipv6AddrAnycastFlag == 1) ? 
            IPADDRESSTYPE_ANYCAST : IPADDRESSTYPE_UNICAST;

        /* origin (mib2.h: 1 = stateless, 2 = stateful, 3 = unknown) */
        DEBUGMSGTL(("access:ipaddress:container", "origin %d\n", 
                        ip6ae.ipv6AddrType));
        if (ip6ae.ipv6AddrType == 1)
            entry->ia_origin = IPADDRESSORIGINTC_LINKLAYER;
        else if (ip6ae.ipv6AddrInfo.ae_flags & IFF_DHCPRUNNING)
            entry->ia_origin = IPADDRESSORIGINTC_DHCP;
        else
            entry->ia_origin = IPADDRESSORIGINTC_MANUAL;
        
        /* status */
        entry->ia_status = ip6ae.ipv6AddrStatus;

        entry->ns_ia_index = ++idx_offset;
        
        DEBUGMSGTL(("access:ipaddress:container", "insert if %" NETSNMP_PRIo "u, addrlen %d\n", 
                    entry->if_index, entry->ia_address_len));

        if (CONTAINER_INSERT(container, entry) < 0) {
            DEBUGMSGTL(("access:ipaddress:container", "unable to insert %s\n", 
                        ip6ae.ipv6AddrIfIndex.o_bytes));
            netsnmp_access_ipaddress_entry_free(entry);
            return (-3);
        }
    }    
    return (idx_offset);
}
#endif /* defined( NETSNMP_ENABLE_IPV6 ) */