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
45 #define UNNAMED "\\<unnamed\\>"
47 static int DotCmd(int ac, char **av);
49 const struct ngcmd dot_cmd = {
52 "Produce a GraphViz (.dot) of the entire netgraph.",
53 "If no outputfile is specified, stdout will be assumed.",
54 { "graphviz", "confdot" }
58 DotCmd(int ac, char **av)
60 struct ng_mesg *nlresp;
61 struct namelist *nlist;
68 while ((ch = getopt(ac, av, "")) != EOF) {
72 return (CMDRTN_USAGE);
82 f = fopen(av[0], "w");
84 warn("Could not open %s for writing", av[0]);
85 return (CMDRTN_ERROR);
92 return (CMDRTN_USAGE);
95 /* Get list of nodes */
96 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL,
98 warn("send listnodes msg");
101 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) {
102 warn("recv listnodes msg");
106 nlist = (struct namelist *)nlresp->data;
107 fprintf(f, "graph netgraph {\n");
108 /* TODO: implement rank = same or subgraphs at some point */
109 fprintf(f, "\tedge [ weight = 1.0 ];\n");
110 fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n");
111 for (i = 0; i < nlist->numnames; i++)
112 fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n",
113 (uintmax_t)nlist->nodeinfo[i].id,
114 nlist->nodeinfo[i].name[0] != '\0' ?
115 nlist->nodeinfo[i].name : UNNAMED,
116 nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id);
117 fprintf(f, "\t};\n");
119 fprintf(f, "\tsubgraph cluster_disconnected {\n");
120 fprintf(f, "\t\tbgcolor = pink;\n");
121 for (i = 0; i < nlist->numnames; i++)
122 if (nlist->nodeinfo[i].hooks == 0)
123 fprintf(f, "\t\t\"%jx\";\n",
124 (uintmax_t)nlist->nodeinfo[i].id);
125 fprintf(f, "\t};\n");
127 for (i = 0; i < nlist->numnames; i++) {
128 struct ng_mesg *hlresp;
129 struct hooklist *hlist;
130 struct nodeinfo *ninfo;
131 char path[NG_PATHSIZ];
134 (void)snprintf(path, sizeof(path), "[%jx]:",
135 (uintmax_t)nlist->nodeinfo[i].id);
137 /* Get node info and hook list */
138 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS,
141 warn("send listhooks msg");
144 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) {
146 warn("recv listhooks msg");
150 hlist = (struct hooklist *)hlresp->data;
151 ninfo = &hlist->nodeinfo;
152 if (ninfo->hooks == 0) {
157 fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n");
158 for (j = 0; j < ninfo->hooks; j++)
159 fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n",
160 (uintmax_t)nlist->nodeinfo[i].id,
161 hlist->link[j].ourhook, hlist->link[j].ourhook);
162 fprintf(f, "\t};\n");
164 fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n");
165 for (j = 0; j < ninfo->hooks; j++)
166 fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n",
167 (uintmax_t)nlist->nodeinfo[i].id,
168 (uintmax_t)nlist->nodeinfo[i].id,
169 hlist->link[j].ourhook);
170 fprintf(f, "\t};\n");
172 for (j = 0; j < ninfo->hooks; j++) {
173 /* Only print the edges going in one direction. */
174 if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id)
176 fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n",
177 (uintmax_t)nlist->nodeinfo[i].id,
178 hlist->link[j].ourhook,
179 (uintmax_t)hlist->link[j].nodeinfo.id,
180 hlist->link[j].peerhook);
194 return (CMDRTN_ERROR);