File: interface.c

package info (click to toggle)
ganglia-monitor-core 2.5.7-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,300 kB
  • ctags: 3,900
  • sloc: ansic: 27,889; sh: 8,492; makefile: 148
file content (138 lines) | stat: -rw-r--r-- 3,499 bytes parent folder | download | duplicates (4)
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
#include "dnet.h"
#include <stdlib.h>

#if 0
static int
print_intf(const struct intf_entry *entry, void *arg)
{
   struct addr bcast;
   uint32_t mask;
   int i;

   /* We only want interfaces that are up and multicast-enabled */
   if (!   (entry->intf_flags & INTF_FLAG_UP) ||
       !   (entry->intf_flags & INTF_FLAG_MULTICAST) )
      return (0);

   printf("%s: UP and MULTICAST-ENABLED", entry->intf_name);

   printf("\n");

   if (entry->intf_addr.addr_type == ADDR_TYPE_IP) {
      addr_btom(entry->intf_addr.addr_bits, &mask, IP_ADDR_LEN);
      mask = ntohl(mask);
      addr_bcast(&entry->intf_addr, &bcast);

      if (entry->intf_dst_addr.addr_type == ADDR_TYPE_IP) {
         printf("\tinet %s --> %s netmask 0x%x broadcast %s\n",
             ip_ntoa(&entry->intf_addr.addr_ip),
             addr_ntoa(&entry->intf_dst_addr),
             mask, addr_ntoa(&bcast));
      } else {
         printf("\tinet %s netmask 0x%x broadcast %s\n",
             ip_ntoa(&entry->intf_addr.addr_ip),
             mask, addr_ntoa(&bcast));
      }
   }
   if (entry->intf_link_addr.addr_type == ADDR_TYPE_ETH)
      printf("\tlink %s\n", addr_ntoa(&entry->intf_link_addr));

   for (i = 0; i < entry->intf_alias_num; i++)
      printf("\talias %s\n", addr_ntoa(&entry->intf_alias_addrs[i]));

   return (0);
}
#endif

static int
get_first_multicast_interface_callback(const struct intf_entry *entry, void *arg)
{
   if ( *(struct intf_entry **)arg != NULL )
      return 0;

   /* Must be up, multicast-enabled and NOT loopback */
   if (! (entry->intf_flags & INTF_FLAG_UP) ||
         (entry->intf_flags & INTF_FLAG_LOOPBACK) ||
       ! (entry->intf_flags & INTF_FLAG_MULTICAST) )
      return 0;

   *(struct intf_entry**)arg = (struct intf_entry *) malloc ( sizeof(struct intf_entry) );
   if (! *(struct intf_entry**)arg )
      return 0;

   memcpy( *(struct intf_entry**)arg, entry, sizeof(struct intf_entry) );
}

struct intf_entry *
get_first_multicast_interface ( void )
{
   intf_t *intf;
   struct intf_entry* entry = NULL;

   intf = intf_open();
   intf_loop( intf, get_first_multicast_interface_callback, (void *)&entry);
   intf_close( intf );
   return entry;
}

static int
get_first_interface_callback(const struct intf_entry *entry, void *arg)
{
   if ( *(struct intf_entry **)arg != NULL )
      return 0;

   /* Must be up and NOT loopback */
   if (! (entry->intf_flags & INTF_FLAG_UP) ||
         (entry->intf_flags & INTF_FLAG_LOOPBACK) )
      return 0;

   *(struct intf_entry**)arg = (struct intf_entry *) malloc ( sizeof(struct intf_entry) );
   if (! *(struct intf_entry**)arg )
      return 0;

   memcpy( *(struct intf_entry**)arg, entry, sizeof(struct intf_entry) );
}

struct intf_entry *
get_first_interface ( void )
{
   intf_t *intf;
   struct intf_entry* entry = NULL;

   intf = intf_open();
   intf_loop( intf, get_first_interface_callback, (void *)&entry);
   intf_close( intf );
   return entry;
}

struct intf_entry *
get_interface ( char *name )
{
   intf_t *intf;
   struct intf_entry *entry;
   int rval;

   entry = (struct intf_entry *) malloc ( sizeof( struct intf_entry ) );
   if( !entry )
      return NULL;

   strncpy( entry->intf_name, name, 60); 
   intf = intf_open();
   rval = intf_get( intf, entry ); 
   intf_close(intf);
   if(rval)
      return NULL;
   else
      return entry;
}

#if 0
void
get_all_interfaces( void )
{
   intf_t *intf;
   intf = intf_open();
   intf_loop( intf, print_intf, NULL );
   intf_close( intf );
}
#endif