5 * Copyright (c) 2004 Brian Fundakowski Feldman
6 * Copyright (c) 1996-1999 Whistle Communications, Inc.
9 * Subject to the following obligations and disclaimer of warranty, use and
10 * redistribution of this software, in source or object code forms, with or
11 * without modifications are expressly permitted by Whistle Communications;
12 * provided, however, that:
13 * 1. Any and all reproductions of the source or object code must include the
14 * copyright notice above and the following disclaimer of warranties; and
15 * 2. No rights are granted, in any manner or form, to use Whistle
16 * Communications, Inc. trademarks, including the mark "WHISTLE
17 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
18 * such appears in the above copyright notice or in the software.
20 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
21 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
22 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
23 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
25 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
26 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
27 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
28 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
29 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
30 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
31 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
50 #define UNNAMED "\\<unnamed\\>"
52 static int DotCmd(int ac, char **av);
54 const struct ngcmd dot_cmd = {
57 "Produce a GraphViz (.dot) of the entire netgraph.",
58 "If no outputfile is specified, stdout will be assumed.",
59 { "graphviz", "confdot" }
63 DotCmd(int ac, char **av)
65 struct ng_mesg *nlresp;
66 struct namelist *nlist;
73 while ((ch = getopt(ac, av, "")) != -1) {
77 return (CMDRTN_USAGE);
87 f = fopen(av[0], "w");
89 warn("Could not open %s for writing", av[0]);
90 return (CMDRTN_ERROR);
97 return (CMDRTN_USAGE);
100 /* Get list of nodes */
101 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
103 warn("send listnodes msg");
106 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) {
107 warn("recv listnodes msg");
111 nlist = (struct namelist *)nlresp->data;
112 fprintf(f, "graph netgraph {\n");
113 /* TODO: implement rank = same or subgraphs at some point */
114 fprintf(f, "\tedge [ weight = 1.0 ];\n");
115 fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
116 for (i = 0; i < nlist->numnames; i++)
117 fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
118 (uintmax_t)nlist->nodeinfo[i].id,
119 nlist->nodeinfo[i].name[0] != '\0' ?
120 nlist->nodeinfo[i].name : UNNAMED,
121 nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
122 fprintf(f, "\t};\n");
124 fprintf(f, "\tsubgraph cluster_disconnected {\n");
125 fprintf(f, "\t\tbgcolor = pink;\n");
126 for (i = 0; i < nlist->numnames; i++)
127 if (nlist->nodeinfo[i].hooks == 0)
128 fprintf(f, "\t\t\"%jx\";\n",
129 (uintmax_t)nlist->nodeinfo[i].id);
130 fprintf(f, "\t};\n");
132 for (i = 0; i < nlist->numnames; i++) {
133 struct ng_mesg *hlresp;
134 struct hooklist *hlist;
135 struct nodeinfo *ninfo;
136 char path[NG_PATHSIZ];
139 (void)snprintf(path, sizeof(path), "[%jx]:",
140 (uintmax_t)nlist->nodeinfo[i].id);
142 /* Get node info and hook list */
143 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
146 warn("send listhooks msg");
149 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) {
151 warn("recv listhooks msg");
155 hlist = (struct hooklist *)hlresp->data;
156 ninfo = &hlist->nodeinfo;
157 if (ninfo->hooks == 0) {
162 fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
163 for (j = 0; j < ninfo->hooks; j++)
164 fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
165 (uintmax_t)nlist->nodeinfo[i].id,
166 hlist->link[j].ourhook, hlist->link[j].ourhook);
167 fprintf(f, "\t};\n");
169 fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
170 for (j = 0; j < ninfo->hooks; j++)
171 fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
172 (uintmax_t)nlist->nodeinfo[i].id,
173 (uintmax_t)nlist->nodeinfo[i].id,
174 hlist->link[j].ourhook);
175 fprintf(f, "\t};\n");
177 for (j = 0; j < ninfo->hooks; j++) {
178 /* Only print the edges going in one direction. */
179 if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
181 fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
182 (uintmax_t)nlist->nodeinfo[i].id,
183 hlist->link[j].ourhook,
184 (uintmax_t)hlist->link[j].nodeinfo.id,
185 hlist->link[j].peerhook);
199 return (CMDRTN_ERROR);