1 /* 2 * dot.c 3 * 4 * Copyright (c) 2019 Lutz Donnerhacke 5 * Copyright (c) 2004 Brian Fundakowski Feldman 6 * Copyright (c) 1996-1999 Whistle Communications, Inc. 7 * All rights reserved. 8 * 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. 19 * 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 36 * OF SUCH DAMAGE. 37 */ 38 39 #include <err.h> 40 #include <inttypes.h> 41 #include <netgraph.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <unistd.h> 45 46 #include "ngctl.h" 47 48 #define UNNAMED "\\<unnamed\\>" 49 50 static int DotCmd(int ac, char **av); 51 52 const struct ngcmd dot_cmd = { 53 DotCmd, 54 "dot [-c] [outputfile]", 55 "Produce a GraphViz (.dot) of the entire netgraph.", 56 "If no outputfile is specified, stdout will be assumed." 57 " The optional -c argument generates a graph without separate" 58 " structures for edge names. Such a graph is more compact.", 59 { "graphviz", "confdot" } 60 }; 61 62 static int 63 DotCmd(int ac, char **av) 64 { 65 struct ng_mesg *nlresp; 66 struct namelist *nlist; 67 FILE *f = stdout; 68 int ch; 69 int compact = 0; 70 u_int i; 71 72 /* Get options */ 73 optreset = 1; 74 optind = 1; 75 while ((ch = getopt(ac, av, "c")) != -1) { 76 switch (ch) { 77 case 'c': 78 compact = 1; 79 break; 80 default: 81 return (CMDRTN_USAGE); 82 break; 83 } 84 } 85 ac -= optind; 86 av += optind; 87 88 /* Get arguments */ 89 switch (ac) { 90 case 1: 91 f = fopen(av[0], "w"); 92 if (f == NULL) { 93 warn("Could not open %s for writing", av[0]); 94 return (CMDRTN_ERROR); 95 } 96 case 0: 97 break; 98 default: 99 if (f != stdout) 100 (void)fclose(f); 101 return (CMDRTN_USAGE); 102 } 103 104 /* Get list of nodes */ 105 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL, 106 0) < 0) { 107 warn("send listnodes msg"); 108 goto error; 109 } 110 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) { 111 warn("recv listnodes msg"); 112 goto error; 113 } 114 115 nlist = (struct namelist *)nlresp->data; 116 if (compact) { 117 fprintf(f, "digraph netgraph {\n"); 118 fprintf(f, "\tedge [ dir = \"none\", fontsize = 10 ];\n"); 119 } else { 120 fprintf(f, "graph netgraph {\n"); 121 /* TODO: implement rank = same or subgraphs at some point */ 122 fprintf(f, "\tedge [ weight = 1.0 ];\n"); 123 } 124 fprintf(f, "\tnode [ shape = record, fontsize = 12 ] {\n"); 125 for (i = 0; i < nlist->numnames; i++) 126 fprintf(f, "\t\t\"%jx\" [ label = \"{%s:|{%s|[%jx]:}}\" ];\n", 127 (uintmax_t)nlist->nodeinfo[i].id, 128 nlist->nodeinfo[i].name[0] != '\0' ? 129 nlist->nodeinfo[i].name : UNNAMED, 130 nlist->nodeinfo[i].type, (uintmax_t)nlist->nodeinfo[i].id); 131 fprintf(f, "\t};\n"); 132 133 fprintf(f, "\tsubgraph cluster_disconnected {\n"); 134 fprintf(f, "\t\tbgcolor = pink;\n"); 135 for (i = 0; i < nlist->numnames; i++) 136 if (nlist->nodeinfo[i].hooks == 0) 137 fprintf(f, "\t\t\"%jx\";\n", 138 (uintmax_t)nlist->nodeinfo[i].id); 139 fprintf(f, "\t};\n"); 140 141 for (i = 0; i < nlist->numnames; i++) { 142 struct ng_mesg *hlresp; 143 struct hooklist *hlist; 144 struct nodeinfo *ninfo; 145 char path[NG_PATHSIZ]; 146 u_int j; 147 148 (void)snprintf(path, sizeof(path), "[%jx]:", 149 (uintmax_t)nlist->nodeinfo[i].id); 150 151 /* Get node info and hook list */ 152 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, 153 NULL, 0) < 0) { 154 free(nlresp); 155 warn("send listhooks msg"); 156 goto error; 157 } 158 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) { 159 free(nlresp); 160 warn("recv listhooks msg"); 161 goto error; 162 } 163 164 hlist = (struct hooklist *)hlresp->data; 165 ninfo = &hlist->nodeinfo; 166 if (ninfo->hooks == 0) { 167 free(hlresp); 168 continue; 169 } 170 171 if (!compact) { 172 fprintf(f, "\tnode [ shape = octagon, fontsize = 10 ] {\n"); 173 for (j = 0; j < ninfo->hooks; j++) 174 fprintf(f, "\t\t\"%jx.%s\" [ label = \"%s\" ];\n", 175 (uintmax_t)nlist->nodeinfo[i].id, 176 hlist->link[j].ourhook, hlist->link[j].ourhook); 177 fprintf(f, "\t};\n"); 178 179 fprintf(f, "\t{\n\t\tedge [ weight = 2.0, style = bold ];\n"); 180 for (j = 0; j < ninfo->hooks; j++) 181 fprintf(f, "\t\t\"%jx\" -- \"%jx.%s\";\n", 182 (uintmax_t)nlist->nodeinfo[i].id, 183 (uintmax_t)nlist->nodeinfo[i].id, 184 hlist->link[j].ourhook); 185 fprintf(f, "\t};\n"); 186 } 187 188 for (j = 0; j < ninfo->hooks; j++) { 189 /* Only print the edges going in one direction. */ 190 if (hlist->link[j].nodeinfo.id > nlist->nodeinfo[i].id) 191 continue; 192 if (compact) { 193 fprintf(f, "\t\"%jx\" -> \"%jx\" [ headlabel = \"%s\", taillabel = \"%s\" ] ;\n", 194 (uintmax_t)hlist->link[j].nodeinfo.id, 195 (uintmax_t)nlist->nodeinfo[i].id, 196 hlist->link[j].ourhook, 197 hlist->link[j].peerhook); 198 } else { 199 fprintf(f, "\t\"%jx.%s\" -- \"%jx.%s\";\n", 200 (uintmax_t)nlist->nodeinfo[i].id, 201 hlist->link[j].ourhook, 202 (uintmax_t)hlist->link[j].nodeinfo.id, 203 hlist->link[j].peerhook); 204 } 205 } 206 free(hlresp); 207 } 208 209 fprintf(f, "}\n"); 210 211 free(nlresp); 212 if (f != stdout) 213 (void)fclose(f); 214 return (CMDRTN_OK); 215 error: 216 if (f != stdout) 217 (void)fclose(f); 218 return (CMDRTN_ERROR); 219 } 220