Debian Codegraph @

src/ipsetdot/ipsetdot.c

raw
1 /* -*- coding: utf-8 -*-
2 * ----------------------------------------------------------------------
3 * Copyright © 2010, libcorkipset authors
4 * All rights reserved.
5 *
6 * Please see the COPYING file in this distribution for license details.
7 * ----------------------------------------------------------------------
8 */
9
10
11 #include <errno.h>
12 #include <getopt.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <libcork/core.h>
19
20 #include "libcork/ipset.h"
21
22
23 static char *input_filename = NULL;
24 static char *output_filename = "-";
25 static bool verbose = false;
26
27
28 static struct option longopts[] = {
29 { "help", no_argument, NULL, 'h' },
30 { "output", required_argument, NULL, 'o' },
31 { "verbose", 0, NULL, 'v' },
32 { NULL, 0, NULL, 0 }
33 };
34
35 #define USAGE \
36 "Usage: ipsetdot [options] <input filename>\n"
37
38 #define FULL_USAGE \
39 USAGE \
40 "\n" \
41 "Creates a GraphViz file showing the BDD structure of an IP set.\n" \
42 "\n" \
43 "Options:\n" \
44 " <input filename>\n" \
45 " The binary set file to read. To read from stdin, use \"-\" as the\n" \
46 " filename.\n" \
47 " --output=<filename>, -o <filename>\n" \
48 " Writes the GraphViz representation of the binary IP set file to\n" \
49 " <filename>. If this option isn't given, then the contents will be\n" \
50 " written to standard output.\n" \
51 " --verbose, -v\n" \
52 " Show progress information about the files being read and written. If\n" \
53 " this option is not given, the only output will be any error messages\n" \
54 " that occur.\n" \
55 " --help\n" \
56 " Display this help and exit.\n" \
57 "\n" \
58 "Output format:\n" \
59 " Internally, IP sets are represented by a binary-decision diagram (BDD).\n" \
60 " The ipsetdot program can be used to produce a GraphViz file that describes\n" \
61 " the internal BDD structure for an IP set. The GraphViz representation can\n" \
62 " then be passed in to GraphViz's \"dot\" program, for instance, to generate\n" \
63 " an image of the BDD's graph structure.\n"
64
65
66 int
67 main(int argc, char **argv)
68 {
69 ipset_init_library();
70
71 /* Parse the command-line options. */
72
73 int ch;
74 while ((ch = getopt_long(argc, argv, "ho:", longopts, NULL)) != -1) {
75 switch (ch) {
76 case 'h':
77 fprintf(stdout, FULL_USAGE);
78 exit(0);
79
80 case 'o':
81 output_filename = optarg;
82 break;
83
84 case 'v':
85 verbose = true;
86 break;
87
88 default:
89 fprintf(stderr, USAGE);
90 exit(1);
91 }
92 }
93
94 argc -= optind;
95 argv += optind;
96
97 if (argc != 1) {
98 fprintf(stderr, "ipsetdot: You must specify exactly one input file.\n");
99 fprintf(stderr, USAGE);
100 exit(1);
101 }
102
103 input_filename = argv[0];
104
105 /* Read in the IP set files specified on the command line. */
106 struct ip_set *set = NULL;
107 FILE *stream;
108 bool close_stream;
109
110 /* Create a FILE object for the file. */
111 if (strcmp(input_filename, "-") == 0) {
112 if (verbose) {
113 fprintf(stderr, "Opening stdin...\n");
114 }
115 input_filename = "stdin";
116 stream = stdin;
117 close_stream = false;
118 } else {
119 if (verbose) {
120 fprintf(stderr, "Opening file %s...\n", input_filename);
121 }
122 stream = fopen(input_filename, "rb");
123 if (stream == NULL) {
124 fprintf(stderr, "Cannot open file %s:\n %s\n",
125 input_filename, strerror(errno));
126 exit(1);
127 }
128 close_stream = true;
129 }
130
131 /* Read in the IP set from the specified file. */
132 set = ipset_load(stream);
133 if (set == NULL) {
134 fprintf(stderr, "Error reading %s:\n %s\n",
135 input_filename, cork_error_message());
136 exit(1);
137 }
138
139 if (close_stream) {
140 fclose(stream);
141 }
142
143 /* Generate a GraphViz dot file for the set. */
144
145 FILE *ostream;
146 bool close_ostream;
147
148 if ((output_filename == NULL) || (strcmp(output_filename, "-") == 0)) {
149 if (verbose) {
150 fprintf(stderr, "Writing to stdout...\n");
151 }
152 ostream = stdout;
153 output_filename = "stdout";
154 close_ostream = false;
155 } else {
156 if (verbose) {
157 fprintf(stderr, "Writing to file %s...\n", output_filename);
158 }
159 ostream = fopen(output_filename, "wb");
160 if (ostream == NULL) {
161 fprintf(stderr, "Cannot open file %s:\n %s\n",
162 output_filename, strerror(errno));
163 exit(1);
164 }
165 close_ostream = true;
166 }
167
168 if (ipset_save_dot(ostream, set) != 0) {
169 fprintf(stderr, "Error saving IP set:\n %s\n",
170 cork_error_message());
171 exit(1);
172 }
173
174 ipset_free(set);
175
176 /* Close the output stream for exiting. */
177 if (close_ostream) {
178 fclose(ostream);
179 }
180
181 return 0;
182 }