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

« back to all changes in this revision

Viewing changes to agent/mibgroup/if-mib/data_access/interface_ioctl.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
 *  Interface MIB architecture support
3
3
 *
4
 
 * $Id: interface_ioctl.c,v 1.8 2004/10/17 01:52:52 rstory Exp $
 
4
 * $Id: interface_ioctl.c,v 1.12.2.1 2006/01/25 16:26:38 dts12 Exp $
5
5
 */
6
6
#include <net-snmp/net-snmp-config.h>
7
7
#include <net-snmp/net-snmp-includes.h>
10
10
 
11
11
#include <net-snmp/agent/net-snmp-agent-includes.h>
12
12
#include <net-snmp/data_access/interface.h>
 
13
#include <net-snmp/data_access/ipaddress.h>
13
14
#include "if-mib/data_access/interface.h"
14
15
 
15
16
#ifdef HAVE_NET_IF_H
22
23
#include <sys/ioctl.h>
23
24
#endif
24
25
 
 
26
#include "interface_ioctl.h"
 
27
#include "ip-mib/data_access/ipaddress_ioctl.h"
 
28
 
25
29
/**
26
30
 * ioctl wrapper
27
31
 *
28
32
 * @param      fd : socket fd to use w/ioctl, or -1 to open/close one
29
 
 * @param ifentry : ifentry to update
 
33
 * @param  which
 
34
 * @param ifrq
 
35
 * param ifentry : ifentry to update
 
36
 * @param name
30
37
 *
31
38
 * @retval  0 : success
32
39
 * @retval -1 : invalid parameters
365
372
 * interface entry ifIndex ioctl wrapper
366
373
 *
367
374
 * @param      fd : socket fd to use w/ioctl, or -1 to open/close one
368
 
 * @param ifentry : ifentry to update
 
375
 * @param name   : ifentry to update
369
376
 *
370
377
 * @retval  0 : not found
371
378
 * @retval !0 : ifIndex
388
395
    return ifrq.ifr_ifindex;
389
396
#endif /* SIOCGIFINDEX */
390
397
}
 
398
 
 
399
/**
 
400
 * check an interface for ipv4 addresses
 
401
 *
 
402
 * @param sd      : open socket descriptor
 
403
 * @param if_name : optional name. takes precedent over if_index.
 
404
 * @param if_index: optional if index. only used if no if_name specified
 
405
 * @param flags   :
 
406
 *
 
407
 * @retval < 0 : error
 
408
 * @retval   0 : no ip v4 addresses
 
409
 * @retval   1 : 1 or more ip v4 addresses
 
410
 */
 
411
int
 
412
netsnmp_access_interface_ioctl_has_ipv4(int sd, const char *if_name,
 
413
                                        int if_index, u_int *flags)
 
414
{
 
415
    int             i, interfaces = 0;
 
416
    struct ifconf   ifc;
 
417
    struct ifreq   *ifrp;
 
418
 
 
419
    /*
 
420
     * one or the other
 
421
     */
 
422
    if ((NULL == flags) ||
 
423
        ((0 == if_index) && (NULL == if_name))) {
 
424
        return -1;
 
425
    }
 
426
 
 
427
    interfaces = netsnmp_access_ipaddress_ioctl_get_interface_count(sd, &ifc);
 
428
    if(interfaces < 0) {
 
429
        close(sd);
 
430
        return -2;
 
431
    }
 
432
    netsnmp_assert(NULL != ifc.ifc_buf);
 
433
 
 
434
    *flags &= ~NETSNMP_INTERFACE_FLAGS_HAS_IPV4;
 
435
 
 
436
    ifrp = ifc.ifc_req;
 
437
    for(i=0; i < interfaces; ++i, ++ifrp) {
 
438
 
 
439
        DEBUGMSGTL(("access:ipaddress:container",
 
440
                    " interface %d, %s\n", i, ifrp->ifr_name));
 
441
 
 
442
        /*
 
443
         * search for matching if_name or if_index
 
444
         */
 
445
        if (NULL != if_name) {
 
446
            if  (strncmp(if_name, ifrp->ifr_name, sizeof(ifrp->ifr_name)) != 0)
 
447
                continue;
 
448
        }
 
449
        else {
 
450
            /*
 
451
             * I think that Linux and Solaris both use ':' in the
 
452
             * interface name for aliases.
 
453
             */
 
454
            char *ptr = strchr(ifrp->ifr_name, ':');
 
455
            if (NULL != ptr)
 
456
                *ptr = 0;
 
457
            
 
458
            if (if_index !=
 
459
                netsnmp_access_interface_ioctl_ifindex_get(sd, ifrp->ifr_name))
 
460
                continue;
 
461
        }
 
462
 
 
463
        /*
 
464
         * check and set v4 or v6 flag, and break if we've found both
 
465
         */
 
466
        if (AF_INET == ifrp->ifr_addr.sa_family) {
 
467
            *flags |= NETSNMP_INTERFACE_FLAGS_HAS_IPV4;
 
468
            break;
 
469
        }
 
470
    }
 
471
 
 
472
    /*
 
473
     * clean up
 
474
     */
 
475
    free(ifc.ifc_buf);
 
476
 
 
477
    return 0;
 
478
}