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
51 #define UNNAMED "\\<unnamed\\>"
53 static int DotCmd(int ac, char **av);
55 const struct ngcmd dot_cmd = {
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" }
66 DotCmd(int ac, char **av)
68 struct ng_mesg *nlresp;
69 struct namelist *nlist;
77 while ((ch = getopt(ac, av, "c")) != -1) {
84 return (CMDRTN_USAGE);
94 f = fopen(av[0], "w");
96 warn("Could not open %s for writing", av[0]);
97 return (CMDRTN_ERROR);
104 return (CMDRTN_USAGE);
107 /* Get list of nodes */
108 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
110 warn("send listnodes msg");
113 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) {
114 warn("recv listnodes msg");
118 nlist = (struct namelist *)nlresp->data;
120 fprintf(f, "digraph netgraph {\n");
121 fprintf(f, "\tedge [ dir = \"none\", fontsize = 10 ];\n");
123 fprintf(f, "graph netgraph {\n");
124 /* TODO: implement rank = same or subgraphs at some point */
125 fprintf(f, "\tedge [ weight = 1.0 ];\n");
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");
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");
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];
151 (void)snprintf(path, sizeof(path), "[%jx]:",
152 (uintmax_t)nlist->nodeinfo[i].id);
154 /* Get node info and hook list */
155 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
158 warn("send listhooks msg");
161 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) {
163 warn("recv listhooks msg");
167 hlist = (struct hooklist *)hlresp->data;
168 ninfo = &hlist->nodeinfo;
169 if (ninfo->hooks == 0) {
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");
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");
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)
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);
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);
221 return (CMDRTN_ERROR);