5 * Copyright (c) 2019 Lutz Donnerhacke
6 * Copyright (c) 2004 Brian Fundakowski Feldman
7 * Copyright (c) 1996-1999 Whistle Communications, Inc.
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.
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
49 #define UNNAMED "\\<unnamed\\>"
51 static int DotCmd(int ac, char **av);
53 const struct ngcmd dot_cmd = {
55 "dot [-c] [outputfile]",
56 "Produce a GraphViz (.dot) of the entire netgraph.",
57 "If no outputfile is specified, stdout will be assumed."
58 " The optional -c argument generates a graph without separate"
59 " structures for edge names. Such a graph is more compact.",
60 { "graphviz", "confdot" }
64 DotCmd(int ac, char **av)
66 struct ng_mesg *nlresp;
67 struct namelist *nlist;
75 while ((ch = getopt(ac, av, "c")) != -1) {
82 return (CMDRTN_USAGE);
92 f = fopen(av[0], "w");
94 warn("Could not open %s for writing", av[0]);
95 return (CMDRTN_ERROR);
102 return (CMDRTN_USAGE);
105 /* Get list of nodes */
106 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
108 warn("send listnodes msg");
111 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) {
112 warn("recv listnodes msg");
116 nlist = (struct namelist *)nlresp->data;
118 fprintf(f, "digraph netgraph {\n");
119 fprintf(f, "\tedge [ dir = \"none\", fontsize = 10 ];\n");
121 fprintf(f, "graph netgraph {\n");
122 /* TODO: implement rank = same or subgraphs at some point */
123 fprintf(f, "\tedge [ weight = 1.0 ];\n");
125 fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
126 for (i = 0; i < nlist->numnames; i++)
127 fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
128 (uintmax_t)nlist->nodeinfo[i].id,
129 nlist->nodeinfo[i].name[0] != '\0' ?
130 nlist->nodeinfo[i].name : UNNAMED,
131 nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
132 fprintf(f, "\t};\n");
134 fprintf(f, "\tsubgraph cluster_disconnected {\n");
135 fprintf(f, "\t\tbgcolor = pink;\n");
136 for (i = 0; i < nlist->numnames; i++)
137 if (nlist->nodeinfo[i].hooks == 0)
138 fprintf(f, "\t\t\"%jx\";\n",
139 (uintmax_t)nlist->nodeinfo[i].id);
140 fprintf(f, "\t};\n");
142 for (i = 0; i < nlist->numnames; i++) {
143 struct ng_mesg *hlresp;
144 struct hooklist *hlist;
145 struct nodeinfo *ninfo;
146 char path[NG_PATHSIZ];
149 (void)snprintf(path, sizeof(path), "[%jx]:",
150 (uintmax_t)nlist->nodeinfo[i].id);
152 /* Get node info and hook list */
153 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
156 warn("send listhooks msg");
159 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) {
161 warn("recv listhooks msg");
165 hlist = (struct hooklist *)hlresp->data;
166 ninfo = &hlist->nodeinfo;
167 if (ninfo->hooks == 0) {
173 fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
174 for (j = 0; j < ninfo->hooks; j++)
175 fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
176 (uintmax_t)nlist->nodeinfo[i].id,
177 hlist->link[j].ourhook, hlist->link[j].ourhook);
178 fprintf(f, "\t};\n");
180 fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
181 for (j = 0; j < ninfo->hooks; j++)
182 fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
183 (uintmax_t)nlist->nodeinfo[i].id,
184 (uintmax_t)nlist->nodeinfo[i].id,
185 hlist->link[j].ourhook);
186 fprintf(f, "\t};\n");
189 for (j = 0; j < ninfo->hooks; j++) {
190 /* Only print the edges going in one direction. */
191 if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
194 fprintf(f, "\t\"%jx\" -> \"%jx\" [ headlabel = \"%s\", taillabel = \"%s\" ] ;\n",
195 (uintmax_t)hlist->link[j].nodeinfo.id,
196 (uintmax_t)nlist->nodeinfo[i].id,
197 hlist->link[j].ourhook,
198 hlist->link[j].peerhook);
200 fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
201 (uintmax_t)nlist->nodeinfo[i].id,
202 hlist->link[j].ourhook,
203 (uintmax_t)hlist->link[j].nodeinfo.id,
204 hlist->link[j].peerhook);
219 return (CMDRTN_ERROR);