1
2 /*
3 * dot.c
4 *
5 * Copyright (c) 2019 Lutz Donnerhacke
6 * Copyright (c) 2004 Brian Fundakowski Feldman
7 * Copyright (c) 1996-1999 Whistle Communications, Inc.
8 * All rights reserved.
9 *
10 * Subject to the following obligations and disclaimer of warranty, use and
11 * redistribution of this software, in source or object code forms, with or
12 * without modifications are expressly permitted by Whistle Communications;
13 * provided, however, that:
14 * 1. Any and all reproductions of the source or object code must include the
15 * copyright notice above and the following disclaimer of warranties; and
16 * 2. No rights are granted, in any manner or form, to use Whistle
17 * Communications, Inc. trademarks, including the mark "WHISTLE
18 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
19 * such appears in the above copyright notice or in the software.
20 *
21 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
22 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
23 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
24 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
26 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
27 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
28 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
29 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
30 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
31 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
32 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
37 * OF SUCH DAMAGE.
38 *
39 * $FreeBSD$
40 */
41
42 #include <err.h>
43 #include <inttypes.h>
44 #include <netgraph.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48
49 #include "ngctl.h"
50
51 #define UNNAMED "\\<unnamed\\>"
52
53 static int DotCmd(int ac, char **av);
54
55 const struct ngcmd dot_cmd = {
56 DotCmd,
57 "dot [-c] [outputfile]",
58 "Produce a GraphViz (.dot) of the entire netgraph.",
59 "If no outputfile is specified, stdout will be assumed."
60 " The optional -c argument generates a graph without separate"
61 " structures for edge names. Such a graph is more compact.",
62 { "graphviz", "confdot" }
63 };
64
65 static int
DotCmd(int ac,char ** av)66 DotCmd(int ac, char **av)
67 {
68 struct ng_mesg *nlresp;
69 struct namelist *nlist;
70 FILE *f = stdout;
71 int ch;
72 int compact = 0;
73 u_int i;
74
75 /* Get options */
76 optind = 1;
77 while ((ch = getopt(ac, av, "c")) != -1) {
78 switch (ch) {
79 case 'c':
80 compact = 1;
81 break;
82 case '?':
83 default:
84 return (CMDRTN_USAGE);
85 break;
86 }
87 }
88 ac -= optind;
89 av += optind;
90
91 /* Get arguments */
92 switch (ac) {
93 case 1:
94 f = fopen(av[0], "w");
95 if (f == NULL) {
96 warn("Could not open %s for writing", av[0]);
97 return (CMDRTN_ERROR);
98 }
99 case 0:
100 break;
101 default:
102 if (f != stdout)
103 (void)fclose(f);
104 return (CMDRTN_USAGE);
105 }
106
107 /* Get list of nodes */
108 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
109 0) < 0) {
110 warn("send listnodes msg");
111 goto error;
112 }
113 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) {
114 warn("recv listnodes msg");
115 goto error;
116 }
117
118 nlist = (struct namelist *)nlresp->data;
119 if (compact) {
120 fprintf(f, "digraph netgraph {\n");
121 fprintf(f, "\tedge [ dir = \"none\", fontsize = 10 ];\n");
122 } else {
123 fprintf(f, "graph netgraph {\n");
124 /* TODO: implement rank = same or subgraphs at some point */
125 fprintf(f, "\tedge [ weight = 1.0 ];\n");
126 }
127 fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
128 for (i = 0; i < nlist->numnames; i++)
129 fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
130 (uintmax_t)nlist->nodeinfo[i].id,
131 nlist->nodeinfo[i].name[0] != '\0' ?
132 nlist->nodeinfo[i].name : UNNAMED,
133 nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
134 fprintf(f, "\t};\n");
135
136 fprintf(f, "\tsubgraph cluster_disconnected {\n");
137 fprintf(f, "\t\tbgcolor = pink;\n");
138 for (i = 0; i < nlist->numnames; i++)
139 if (nlist->nodeinfo[i].hooks == 0)
140 fprintf(f, "\t\t\"%jx\";\n",
141 (uintmax_t)nlist->nodeinfo[i].id);
142 fprintf(f, "\t};\n");
143
144 for (i = 0; i < nlist->numnames; i++) {
145 struct ng_mesg *hlresp;
146 struct hooklist *hlist;
147 struct nodeinfo *ninfo;
148 char path[NG_PATHSIZ];
149 u_int j;
150
151 (void)snprintf(path, sizeof(path), "[%jx]:",
152 (uintmax_t)nlist->nodeinfo[i].id);
153
154 /* Get node info and hook list */
155 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
156 NULL, 0) < 0) {
157 free(nlresp);
158 warn("send listhooks msg");
159 goto error;
160 }
161 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) {
162 free(nlresp);
163 warn("recv listhooks msg");
164 goto error;
165 }
166
167 hlist = (struct hooklist *)hlresp->data;
168 ninfo = &hlist->nodeinfo;
169 if (ninfo->hooks == 0) {
170 free(hlresp);
171 continue;
172 }
173
174 if (!compact) {
175 fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
176 for (j = 0; j < ninfo->hooks; j++)
177 fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
178 (uintmax_t)nlist->nodeinfo[i].id,
179 hlist->link[j].ourhook, hlist->link[j].ourhook);
180 fprintf(f, "\t};\n");
181
182 fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
183 for (j = 0; j < ninfo->hooks; j++)
184 fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
185 (uintmax_t)nlist->nodeinfo[i].id,
186 (uintmax_t)nlist->nodeinfo[i].id,
187 hlist->link[j].ourhook);
188 fprintf(f, "\t};\n");
189 }
190
191 for (j = 0; j < ninfo->hooks; j++) {
192 /* Only print the edges going in one direction. */
193 if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
194 continue;
195 if (compact) {
196 fprintf(f, "\t\"%jx\" -> \"%jx\" [ headlabel = \"%s\", taillabel = \"%s\" ] ;\n",
197 (uintmax_t)hlist->link[j].nodeinfo.id,
198 (uintmax_t)nlist->nodeinfo[i].id,
199 hlist->link[j].ourhook,
200 hlist->link[j].peerhook);
201 } else {
202 fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
203 (uintmax_t)nlist->nodeinfo[i].id,
204 hlist->link[j].ourhook,
205 (uintmax_t)hlist->link[j].nodeinfo.id,
206 hlist->link[j].peerhook);
207 }
208 }
209 free(hlresp);
210 }
211
212 fprintf(f, "}\n");
213
214 free(nlresp);
215 if (f != stdout)
216 (void)fclose(f);
217 return (CMDRTN_OK);
218 error:
219 if (f != stdout)
220 (void)fclose(f);
221 return (CMDRTN_ERROR);
222 }
223