1 /*
2  */
3 
4 #include "graph.h"
5 
6 #define	MK_IFACE	(MK_LAST << 1)
7 
8 /******************************************************************************
9 Example of output format
10 
11 Output a list of known interfaces for each equipment under the form
12     option -t : output list of terminal interfaces
13     option -b : output list of backbone interfaces
14     without option : output list of all interfaces
15 
16 atrium-ce1 GigabitEthernet1/0 GigabitEthernet1/1 ...
17 xxx-ce1 ...
18 
19 ******************************************************************************/
20 
21 #define	TERMINAL	1
22 
23 /******************************************************************************
24 Output equipements and interfaces
25 ******************************************************************************/
26 
output_eq_ifaces(FILE * fp)27 void output_eq_ifaces (FILE *fp)
28 {
29     struct eq *eq ;
30     struct node *n ;
31 
32     for (eq = mobj_head (eqmobj) ; eq != NULL ; eq = eq->next)
33     {
34 	if (MK_ISSELECTED (eq))
35 	{
36 	    fprintf (fp, "%s", eq->name) ;
37 	    for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next)
38 	    {
39 		if (n->eq == eq
40 			&& n->nodetype == NT_L1
41 			&& ! MK_ISSET (n, MK_IFACE))
42 		    fprintf (fp, " %s", n->u.l1.ifname) ;
43 	    }
44 	    fprintf (fp, "\n") ;
45 	}
46     }
47 }
48 
49 /******************************************************************************
50 Mark interface
51 ******************************************************************************/
52 
mark_ifaces(int termif)53 void mark_ifaces (int termif)
54 {
55     struct node *n, *peer ;
56 
57     for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next)
58     {
59 	if (n->nodetype == NT_L1 && ! MK_ISSET (n, MK_IFACE))
60 	{
61 	    peer = get_neighbour (n, NT_L1) ;
62 	    if (termif)
63 	    {
64 		/* we don't want terminal interfaces */
65 		if (peer == NULL)
66 		    MK_SET (n, MK_IFACE) ;
67 	    }
68 	    else
69 	    {
70 		/* we don't want backbone interfaces */
71 		if (peer != NULL)
72 		{
73 		    MK_SET (n, MK_IFACE) ;
74 		    MK_SET (peer, MK_IFACE) ;		/* optimization */
75 		}
76 	    }
77 	}
78     }
79 }
80 
81 /******************************************************************************
82 Main function
83 ******************************************************************************/
84 
85 MOBJ *mobjlist [NB_MOBJ] ;
86 
usage(char * progname)87 void usage (char *progname)
88 {
89     fprintf (stderr, "Usage : %s [-a|-n cidr|-v vlan|-e regexp|-E regexp|-t|-m]* [-b|-B]\n", progname) ;
90     fprintf (stderr, "\t-b: backbone interfaces\n") ;
91     fprintf (stderr, "\t-B: non-backbone interfaces (i.e. terminal interfaces)\n") ;
92     exit (1) ;
93 }
94 
95 
main(int argc,char * argv[])96 int main (int argc, char *argv [])
97 {
98     int termif, backif ;
99     int c, err ;
100     char *prog, *errstr ;
101 
102 
103     prog = argv [0] ;
104     err = 0 ;
105     termif = backif = 0 ;
106 
107     sel_init () ;
108 
109     while ((c = getopt (argc, argv, "an:e:E:tv:mBb")) != -1) {
110 	switch (c)
111 	{
112 	    case 'a' :
113 	    case 'n' :
114 	    case 'e' :
115 	    case 'E' :
116 	    case 't' :
117 	    case 'v' :
118 	    case 'm' :
119 		if ((errstr = sel_register (c, optarg)) != NULL)
120 		{
121 		    fprintf (stderr, "%s: %s\n", prog, errstr) ;
122 		    err = 1 ;
123 		}
124 		break ;
125 	    case 'B' :
126 		termif = 1 ;
127 		break ;
128 	    case 'b' :
129 		backif = 1 ;
130 		break ;
131 	    case '?' :
132 	    default :
133 		usage (prog) ;
134 	}
135     }
136 
137     if (err)
138 	exit (1) ;
139 
140     argc -= optind ;
141     argv += optind ;
142 
143     if (argc != 0)
144 	usage (prog) ;
145 
146     if (termif == 0 && backif == 0)
147     {
148 	termif = 1 ;
149 	backif = 1 ;
150     }
151 
152     /*
153      * Read the graph and select a subgraph
154      */
155 
156     bin_read (stdin, mobjlist) ;
157     sel_mark () ;
158 
159     /*
160      * Grep interface type
161      */
162 
163     if (! termif)			/* we don't want terminal interfaces */
164 	mark_ifaces (TERMINAL) ;
165 
166     if (! backif)			/* we don't want backbone interfaces */
167 	mark_ifaces (! TERMINAL) ;
168 
169     /*
170      * Output graph
171      */
172 
173     output_eq_ifaces (stdout) ;
174 
175     sel_end () ;
176     exit (0) ;
177 }
178