summaryrefslogtreecommitdiff
path: root/agent/mibgroup/mibII/udpTable.c
blob: cc1e780c063d1240d221e2b66b70e8b40380b699 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
 *  UDP MIB group Table implementation - udpTable.c
 *
 */

/* Portions of this file are subject to the following copyright(s).  See
 * the Net-SNMP's COPYING file for more details and other copyrights
 * that may apply:
 */
/*
 * Portions of this file are copyrighted by:
 * Copyright © 2003 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms specified in the COPYING file
 * distributed with the Net-SNMP package.
 */

#include <net-snmp/net-snmp-config.h>
#include "mibII_common.h"

#if HAVE_NETINET_UDP_H
#include <netinet/udp.h>
#endif
#if HAVE_NETINET_UDP_VAR_H
#include <netinet/udp_var.h>
#endif

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

#ifdef linux
#include "tcpTable.h"
#endif
#include "udp.h"
#include "udpTable.h"
#include "sysORTable.h"

#ifdef hpux11
#define	UDPTABLE_ENTRY_TYPE	mib_udpLsnEnt 
#define	UDPTABLE_LOCALADDRESS	LocalAddress 
#define	UDPTABLE_LOCALPORT	LocalPort 
#define	UDPTABLE_IS_TABLE
#else

#ifdef solaris2
typedef struct netsnmp_udpEntry_s netsnmp_udpEntry;
struct netsnmp_udpEntry_s {
    mib2_udpEntry_t   entry;
    netsnmp_udpEntry *inp_next;
};
#define	UDPTABLE_ENTRY_TYPE	netsnmp_udpEntry
#define	UDPTABLE_LOCALADDRESS	entry.udpLocalAddress 
#define	UDPTABLE_LOCALPORT	entry.udpLocalPort 
#define	UDPTABLE_IS_LINKED_LIST
#else

#if defined (WIN32) || defined (cygwin)
#include <iphlpapi.h>
#define	UDPTABLE_ENTRY_TYPE	MIB_UDPROW		/* ??? */
#define	UDPTABLE_LOCALADDRESS	dwLocalAddr
#define	UDPTABLE_LOCALPORT	dwLocalPort 
#define	UDPTABLE_IS_TABLE
#else			/* everything else */

#ifdef linux
#define INP_NEXT_SYMBOL		inp_next
#endif

#if defined(freebsd4) || defined(darwin) || defined(osf5)
typedef struct netsnmp_inpcb_s netsnmp_inpcb;
struct netsnmp_inpcb_s {
    struct inpcb    pcb;
    int             state;
    netsnmp_inpcb  *inp_next;
};
#define	UDPTABLE_ENTRY_TYPE	netsnmp_inpcb 
#define	UDPTABLE_LOCALADDRESS	pcb.inp_laddr.s_addr 
#define	UDPTABLE_LOCALPORT	pcb.inp_lport
#else
#define	UDPTABLE_ENTRY_TYPE	struct inpcb 
#define	UDPTABLE_LOCALADDRESS	inp_laddr.s_addr 
#define	UDPTABLE_LOCALPORT	inp_lport
#endif
#define	UDPTABLE_IS_LINKED_LIST

#endif                          /* WIN32 cygwin */
#endif                          /* solaris2 */
#endif                          /* hpux11 */

				/* Head of linked list, or root of table */
UDPTABLE_ENTRY_TYPE	*udp_head  = NULL;
int                      udp_size  = 0;	/* Only used for table-based systems */


	/*
	 *
	 * Initialization and handler routines are common to all architectures
	 *
	 */
#ifndef MIB_STATS_CACHE_TIMEOUT
#define MIB_STATS_CACHE_TIMEOUT	5
#endif
#ifndef UDP_STATS_CACHE_TIMEOUT
#define UDP_STATS_CACHE_TIMEOUT	MIB_STATS_CACHE_TIMEOUT
#endif

#ifdef UDP_ADDRESSES_IN_HOST_ORDER
#define UDP_ADDRESS_TO_HOST_ORDER(x) x
#define UDP_ADDRESS_TO_NETWORK_ORDER(x) htonl(x)
#else
#define UDP_ADDRESS_TO_HOST_ORDER(x) ntohl(x)
#define UDP_ADDRESS_TO_NETWORK_ORDER(x) x
#endif

#ifdef UDP_PORTS_IN_HOST_ORDER
#define UDP_PORT_TO_HOST_ORDER(x) x
#else
#define UDP_PORT_TO_HOST_ORDER(x) ntohs(x)
#endif


oid             udpTable_oid[] = { SNMP_OID_MIB2, 7, 5 };

void
init_udpTable(void)
{
    netsnmp_table_registration_info *table_info;
    netsnmp_iterator_info           *iinfo;
    netsnmp_handler_registration    *reginfo;

    DEBUGMSGTL(("mibII/udpTable", "Initialising UDP Table\n"));
    /*
     * Create the table data structure, and define the indexing....
     */
    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    if (!table_info) {
        return;
    }
    netsnmp_table_helper_add_indexes(table_info, ASN_IPADDRESS,
                                                 ASN_INTEGER, 0);
    table_info->min_column = UDPLOCALADDRESS;
    table_info->max_column = UDPLOCALPORT;


    /*
     * .... and iteration information ....
     */
    iinfo      = SNMP_MALLOC_TYPEDEF(netsnmp_iterator_info);
    if (!iinfo) {
        return;
    }
    iinfo->get_first_data_point = udpTable_first_entry;
    iinfo->get_next_data_point  = udpTable_next_entry;
    iinfo->table_reginfo        = table_info;
#if defined (WIN32) || defined (cygwin)
    iinfo->flags               |= NETSNMP_ITERATOR_FLAG_SORTED;
#endif /* WIN32 || cygwin */


    /*
     * .... and register the table with the agent.
     */
    reginfo = netsnmp_create_handler_registration("udpTable",
            udpTable_handler,
            udpTable_oid, OID_LENGTH(udpTable_oid),
            HANDLER_CAN_RONLY),
    netsnmp_register_table_iterator(reginfo, iinfo);

    /*
     * .... with a local cache
     */
    netsnmp_inject_handler( reginfo,
		    netsnmp_get_cache_handler(UDP_STATS_CACHE_TIMEOUT,
			   		udpTable_load, udpTable_free,
					udpTable_oid, OID_LENGTH(udpTable_oid)));
}



int
udpTable_handler(netsnmp_mib_handler          *handler,
                 netsnmp_handler_registration *reginfo,
                 netsnmp_agent_request_info   *reqinfo,
                 netsnmp_request_info         *requests)
{
    netsnmp_request_info  *request;
    netsnmp_variable_list *requestvb;
    netsnmp_table_request_info *table_info;
    UDPTABLE_ENTRY_TYPE	  *entry;
    oid      subid;
    long     port;
    in_addr_t addr;

    DEBUGMSGTL(("mibII/udpTable", "Handler - mode %s\n",
                    se_find_label_in_slist("agent_mode", reqinfo->mode)));
    switch (reqinfo->mode) {
    case MODE_GET:
        for (request=requests; request; request=request->next) {
            requestvb = request->requestvb;
            DEBUGMSGTL(( "mibII/udpTable", "oid: "));
            DEBUGMSGOID(("mibII/udpTable", requestvb->name,
                                           requestvb->name_length));
            DEBUGMSG((   "mibII/udpTable", "\n"));

            entry = (UDPTABLE_ENTRY_TYPE *)netsnmp_extract_iterator_context(request);
            if (!entry)
                continue;
            table_info = netsnmp_extract_table_info(request);
            subid      = table_info->colnum;

            switch (subid) {
            case UDPLOCALADDRESS:
#if defined(osf5) && defined(IN6_EXTRACT_V4ADDR)
                addr = ntohl(IN6_EXTRACT_V4ADDR(&entry->pcb.inp_laddr));
	        snmp_set_var_typed_value(requestvb, ASN_IPADDRESS,
                                         (u_char*)&addr,
                                         sizeof(uint32_t));
#else
                addr = UDP_ADDRESS_TO_HOST_ORDER(entry->UDPTABLE_LOCALADDRESS);
	        snmp_set_var_typed_value(requestvb, ASN_IPADDRESS,
                                         (u_char *)&addr,
                                         sizeof(uint32_t));
#endif
                break;
            case UDPLOCALPORT:
                port = UDP_PORT_TO_HOST_ORDER((u_short)entry->UDPTABLE_LOCALPORT);
	        snmp_set_var_typed_value(requestvb, ASN_INTEGER,
                                 (u_char *)&port, sizeof(port));
                break;
	    }
	}
        break;

    case MODE_GETNEXT:
    case MODE_GETBULK:
    case MODE_SET_RESERVE1:
    case MODE_SET_RESERVE2:
    case MODE_SET_ACTION:
    case MODE_SET_COMMIT:
    case MODE_SET_FREE:
    case MODE_SET_UNDO:
        snmp_log(LOG_WARNING, "mibII/udpTable: Unsupported mode (%d)\n",
                               reqinfo->mode);
        break;
    default:
        snmp_log(LOG_WARNING, "mibII/udpTable: Unrecognised mode (%d)\n",
                               reqinfo->mode);
        break;
    }

    return SNMP_ERR_NOERROR;
}

	/*
	 * Two forms of iteration hook routines:
	 *    One for when the UDP table is stored as a table
	 *    One for when the UDP table is stored as a linked list
	 *
	 * Also applies to the cache-handler free routine
	 */

#ifdef	UDPTABLE_IS_TABLE
netsnmp_variable_list *
udpTable_first_entry(void **loop_context,
                     void **data_context,
                     netsnmp_variable_list *index,
                     netsnmp_iterator_info *data)
{
    /*
     * XXX - How can we tell if the cache is valid?
     *       No access to 'reqinfo'
     */
    if (udp_size == 0)
        return NULL;

    /*
     * Point to the first entry, and use the
     * 'next_entry' hook to retrieve this row
     */
    *loop_context = 0;
    return udpTable_next_entry( loop_context, data_context, index, data );
}

netsnmp_variable_list *
udpTable_next_entry( void **loop_context,
                     void **data_context,
                     netsnmp_variable_list *index,
                     netsnmp_iterator_info *data)
{
    int i = (int)*loop_context;
    long port;

    if (udp_size < i)
        return NULL;

    /*
     * Set up the indexing for the specified row...
     */
#if defined (WIN32) || defined (cygwin)
    port = ntohl((u_long)udp_head[i].UDPTABLE_LOCALADDRESS);
    snmp_set_var_value(index, (u_char *)&port,
                                  sizeof(udp_head[i].UDPTABLE_LOCALADDRESS));
#else
    snmp_set_var_value(index, (u_char *)&udp_head[i].UDPTABLE_LOCALADDRESS,
                                  sizeof(udp_head[i].UDPTABLE_LOCALADDRESS));
#endif
    port = UDP_PORT_TO_HOST_ORDER((u_short)udp_head[i].UDPTABLE_LOCALPORT);
    snmp_set_var_value(index->next_variable,
                               (u_char*)&port, sizeof(port));
    /*
     * ... return the data structure for this row,
     * and update the loop context ready for the next one.
     */
    *data_context = (void*)&udp_head[i];
    *loop_context = (void*)++i;
    return index;
}

void
udpTable_free(netsnmp_cache *cache, void *magic)
{
#if defined (WIN32) || defined (cygwin)
    if (udp_head) {
		/* the allocated structure is a count followed by table entries */
		free((char *)(udp_head) - sizeof(DWORD));
	}
#else
    if (udp_head)
        free(udp_head);
#endif
    udp_head = NULL;
    udp_size = 0;
}
#else
#ifdef UDPTABLE_IS_LINKED_LIST
netsnmp_variable_list *
udpTable_first_entry(void **loop_context,
                     void **data_context,
                     netsnmp_variable_list *index,
                     netsnmp_iterator_info *data)
{
    /*
     * XXX - How can we tell if the cache is valid?
     *       No access to 'reqinfo'
     */
    if (udp_head == 0)
        return NULL;

    /*
     * Point to the first entry, and use the
     * 'next_entry' hook to retrieve this row
     */
    *loop_context = (void*)udp_head;
    return udpTable_next_entry( loop_context, data_context, index, data );
}

netsnmp_variable_list *
udpTable_next_entry( void **loop_context,
                     void **data_context,
                     netsnmp_variable_list *index,
                     netsnmp_iterator_info *data)
{
    UDPTABLE_ENTRY_TYPE	 *entry = (UDPTABLE_ENTRY_TYPE *)*loop_context;
    long port;
    long addr;

    if (!entry)
        return NULL;

    /*
     * Set up the indexing for the specified row...
     */
#if defined(osf5) && defined(IN6_EXTRACT_V4ADDR)
                snmp_set_var_value(index,
                              (u_char*)&IN6_EXTRACT_V4ADDR(&entry->pcb.inp_laddr),
                                 sizeof(IN6_EXTRACT_V4ADDR(&entry->pcb.inp_laddr)));
#else
    addr = UDP_ADDRESS_TO_NETWORK_ORDER((in_addr_t)entry->UDPTABLE_LOCALADDRESS);
    snmp_set_var_value(index, (u_char *)&addr,
                                 sizeof(addr));
#endif
    port = UDP_PORT_TO_HOST_ORDER(entry->UDPTABLE_LOCALPORT);
    snmp_set_var_value(index->next_variable,
                               (u_char*)&port, sizeof(port));

    /*
     * ... return the data structure for this row,
     * and update the loop context ready for the next one.
     */
    *data_context = (void*)entry;
    *loop_context = (void*)entry->INP_NEXT_SYMBOL;
    return index;
}

void
udpTable_free(netsnmp_cache *cache, void *magic)
{
    UDPTABLE_ENTRY_TYPE	 *p;
    while (udp_head) {
        p = udp_head;
        udp_head = udp_head->INP_NEXT_SYMBOL;
        free(p);
    }

    udp_head = NULL;
}
#endif		/* UDPTABLE_IS_LINKED_LIST */
#endif		/* UDPTABLE_IS_TABLE */


	/*
	 *
	 * The cache-handler loading routine is the main
	 *    place for architecture-specific code
	 *
	 * Load into either a table structure, or a linked list
	 *    depending on the system architecture
	 */


#ifdef hpux11
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    int             fd;
    struct nmparms  p;
    int             val = 0;
    unsigned int    ulen;
    int             ret;

    udpTable_free(NULL, NULL);

    if ((fd = open_mib("/dev/ip", O_RDONLY, 0, NM_ASYNC_OFF)) >= 0) {
        p.objid = ID_udpLsnNumEnt;
        p.buffer = (void *) &val;
        ulen = sizeof(int);
        p.len = &ulen;
        if ((ret = get_mib_info(fd, &p)) == 0)
            udp_size = val;

        if (udp_size > 0) {
            ulen = (unsigned) udp_size *sizeof(mib_udpLsnEnt);
            udp_head = (mib_udpLsnEnt *) malloc(ulen);
            p.objid = ID_udpLsnTable;
            p.buffer = (void *) udp_head;
            p.len = &ulen;
            if ((ret = get_mib_info(fd, &p)) < 0) {
                udp_size = 0;
            }
        }

        close_mib(fd);
    }

    if (udp_size > 0) {
        DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
        return 0;
    }
    DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (hpux11)\n"));
    return -1;
}
#else                           /* hpux11 */

#ifdef linux
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    FILE           *in;
    char            line[256];

    udpTable_free(cache, NULL);

    if (!(in = fopen("/proc/net/udp", "r"))) {
        DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (linux)\n"));
        snmp_log(LOG_ERR, "snmpd: cannot open /proc/net/udp ...\n");
        return -1;
    }

    /*
     * scan proc-file and build up a linked list 
     * This will actually be built up in reverse,
     *   but since the entries are unsorted, that doesn't matter.
     */
    while (line == fgets(line, sizeof(line), in)) {
        struct inpcb    pcb, *nnew;
        unsigned int    state, lport;

        if (3 != sscanf(line, "%*d: %x:%x %*x:%*x %x",
                        &pcb.inp_laddr.s_addr, &lport, &state))
            continue;

        if (state != 7)         /* fix me:  UDP_LISTEN ??? */
            continue;

        /* store in network byte order */
        pcb.inp_laddr.s_addr = htonl(pcb.inp_laddr.s_addr);
        pcb.inp_lport = htons((unsigned short) (lport));

        nnew = SNMP_MALLOC_TYPEDEF(struct inpcb);
        if (nnew == NULL)
            break;
        memcpy(nnew, &pcb, sizeof(struct inpcb));
        nnew->inp_next = udp_head;
        udp_head       = nnew;
    }

    fclose(in);

    DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
    return 0;
}
#else                           /* linux */

#ifdef solaris2
static int
UDP_Cmp(void *addr, void *ep)
{
    if (memcmp((mib2_udpEntry_t *) ep, (mib2_udpEntry_t *) addr,
               sizeof(mib2_udpEntry_t)) == 0)
        return (0);
    else
        return (1);
}


int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    mib2_udpEntry_t   entry;
    netsnmp_udpEntry *nnew;
    netsnmp_udpEntry *prev_entry = NULL;


    udpTable_free(NULL, NULL);

    if (getMibstat(MIB_UDP_LISTEN, &entry, sizeof(mib2_udpEntry_t),
                   GET_FIRST, &UDP_Cmp, &entry) != 0) {
        DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (solaris)\n"));
        return -1;
    }

    while (1) {
        /*
         * Not interested in 'idle' entries, apparently....
         */
        DEBUGMSGTL(("mibII/udpTable", "UDP Entry %x:%d (%d)\n",
                     entry.udpLocalAddress, entry.udpLocalPort, entry.udpEntryInfo.ue_state));
        if (entry.udpEntryInfo.ue_state == MIB2_UDP_idle) {
            /*
             * Build up a linked list copy of the getMibstat results
             * Note that since getMibstat returns rows in sorted order,
	     *    we need to retain this order while building the list
	     *    so new entries are added onto the end of the list.
             * xxx-rks: WARNING: this is NOT TRUE on the sf cf solaris boxes.
             */
            nnew = SNMP_MALLOC_TYPEDEF(netsnmp_udpEntry);
            if (nnew == NULL)
                break;
            memcpy(&(nnew->entry), &entry, sizeof(mib2_udpEntry_t));
            if (!prev_entry)
	        udp_head = nnew;
	    else
	        prev_entry->inp_next = nnew;
	    prev_entry = nnew;
	}

        if (getMibstat(MIB_UDP_LISTEN, &entry, sizeof(mib2_udpEntry_t),
                       GET_NEXT, &UDP_Cmp, &entry) != 0)
	    break;
    }

    if (udp_head) {
        DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
        return 0;
    }
    DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (solaris)\n"));
    return -1;
}
#else                           /* solaris2 */

#if defined (WIN32) || defined (cygwin)
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    PMIB_UDPTABLE pUdpTable = NULL;
    DWORD         dwActualSize = 0;
    DWORD         status = NO_ERROR;

    /*
     * query for the buffer size needed 
     */
    status = GetUdpTable(pUdpTable, &dwActualSize, TRUE);
    if (status == ERROR_INSUFFICIENT_BUFFER) {
        pUdpTable = (PMIB_UDPTABLE) malloc(dwActualSize);
        if (pUdpTable != NULL) {
            /*
             * Get the sorted UDP table 
             */
            status = GetUdpTable(pUdpTable, &dwActualSize, TRUE);
        }
    }
    if (status == NO_ERROR) {
        DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
        udp_size = pUdpTable->dwNumEntries -1;  /* entries are counted starting with 0 */
        udp_head = pUdpTable->table;
        return 0;
    }
    DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (win32)\n"));
    if (pUdpTable)
	free(pUdpTable);
    return -1;
}
#else                           /* WIN32 cygwin*/

#if (defined(NETSNMP_CAN_USE_SYSCTL) && defined(UDPCTL_PCBLIST))
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    size_t   len;
    int      sname[] = { CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_PCBLIST };
    char     *udpcb_buf = NULL;
    struct xinpgen *xig = NULL;
    UDPTABLE_ENTRY_TYPE  *nnew;

    udpTable_free(NULL, NULL);

    /*
     *  Read in the buffer containing the UDP table data
     */
    len = 0;
    if (sysctl(sname, 4, 0, &len, 0, 0) < 0 ||
       (udpcb_buf = malloc(len)) == NULL)
        return -1;
    if (sysctl(sname, 4, udpcb_buf, &len, 0, 0) < 0) {
        free(udpcb_buf);
        return -1;
    }

    /*
     *  Unpick this into the constituent 'xinpgen' structures, and extract
     *     the 'inpcb' elements into a linked list (built in reverse)
     */
    xig = (struct xinpgen *) udpcb_buf;
    xig = (struct xinpgen *) ((char *) xig + xig->xig_len);

    while (xig && (xig->xig_len > sizeof(struct xinpgen))) {
        nnew = SNMP_MALLOC_TYPEDEF(UDPTABLE_ENTRY_TYPE);
        if (!nnew)
            break;
#if defined(freebsd4) || defined(darwin)
        memcpy(nnew, &((struct xinpcb *) xig)->xi_inp, sizeof(struct inpcb));
	nnew->inp_next = udp_head;
#else
        memcpy(nnew, ((struct xinpcb *) xig)->xi_inp, sizeof(struct inpcb));
	nnew->next = udp_head;		/* XXX - ?? Check 'next' pointer */
#endif

	udp_head   = nnew;
        xig = (struct xinpgen *) ((char *) xig + xig->xig_len);
    }

    free(udpcb_buf);
    if (udp_head) {
        DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
        return 0;
    }
    DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (sysctl)\n"));
    return -1;
}
#else		/* (defined(NETSNMP_CAN_USE_SYSCTL) && defined(UDPCTL_PCBLIST)) */
#ifdef PCB_TABLE
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    struct inpcbtable table;
    struct inpcb   *nnew, *entry;

    udpTable_free(NULL, NULL);

    if (!auto_nlist(UDB_SYMBOL, (char *) &table, sizeof(table))) {
        DEBUGMSGTL(("mibII/udpTable", "Failed to read inpcbtable\n"));
        return -1;
    }

    /*
     *  Set up a linked list
     */
    entry  = table.inpt_queue.cqh_first;
    while (entry) {
   
        nnew = SNMP_MALLOC_TYPEDEF(struct inpcb);
        if (!nnew)
            break;

        if (!NETSNMP_KLOOKUP(entry, (char *) nnew, sizeof(struct inpcb))) {
            DEBUGMSGTL(("mibII/udpTable:udpTable_load", "klookup failed\n"));
            break;
        }

        entry    = nnew->inp_queue.cqe_next;	/* Next kernel entry */
	nnew->inp_queue.cqe_next = udp_head;
	udp_head = nnew;

        if (entry == table.inpt_queue.cqh_first)
            break;
    }

    if (udp_head) {
        DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
        return 0;
    }
    DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (pcb_table)\n"));
    return -1;
}

#else				/* PCB_TABLE */
#ifdef UDB_SYMBOL
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    struct inpcb   udp_inpcb;
    struct inpcb   *nnew, *entry;

    udpTable_free(NULL, NULL);

    if (!auto_nlist(UDB_SYMBOL, (char *) &udp_inpcb, sizeof(udp_inpcb))) {
        DEBUGMSGTL(("mibII/udpTable", "Failed to read udb_symbol\n"));
        return -1;
    }

    /*
     *  Set up a linked list
     */
    entry  = udp_inpcb.INP_NEXT_SYMBOL;
    while (entry) {
   
        nnew = SNMP_MALLOC_TYPEDEF(struct inpcb);
        if (!nnew)
            break;

        if (!NETSNMP_KLOOKUP(entry, (char *) nnew, sizeof(struct inpcb))) {
            DEBUGMSGTL(("mibII/udpTable:udpTable_load", "klookup failed\n"));
            break;
        }

        entry    = nnew->INP_NEXT_SYMBOL;		/* Next kernel entry */
	nnew->INP_NEXT_SYMBOL = udp_head;
	udp_head = nnew;

        if (entry == udp_inpcb.INP_NEXT_SYMBOL)
            break;
    }

    if (udp_head) {
        DEBUGMSGTL(("mibII/udpTable", "Loaded UDP Table\n"));
        return 0;
    }
    DEBUGMSGTL(("mibII/udpTable", "Failed to load UDP Table (udb_symbol)\n"));
    return -1;
}

#else				/* UDB_SYMBOL */
int
udpTable_load(netsnmp_cache *cache, void *vmagic)
{
    DEBUGMSGTL(("mibII/udpTable", "Loading UDP Table not implemented\n"));
    return -1;
}
#endif				/* UDB_SYMBOL */
#endif				/* PCB_TABLE */
#endif		/* (defined(NETSNMP_CAN_USE_SYSCTL) && defined(UDPCTL_PCBLIST)) */
#endif                          /* WIN32 cygwin*/
#endif                          /* linux */
#endif                          /* solaris2 */
#endif                          /* hpux11 */