1 2 /* 3 * dot.c 4 * 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 * $FreeBSD$ 39 */ 40 41 #include <inttypes.h> 42 43 #include "ngctl.h" 44 45 #define UNNAMED "\\<unnamed\\>" 46 47 static int DotCmd(int ac, char **av); 48 49 const struct ngcmd dot_cmd = { 50 DotCmd, 51 "dot [outputfile]", 52 "Produce a GraphViz (.dot) of the entire netgraph.", 53 "If no outputfile is specified, stdout will be assumed.", 54 { "graphviz", "confdot" } 55 }; 56 57 static int 58 DotCmd(int ac, char **av) 59 { 60 struct ng_mesg *nlresp; 61 struct namelist *nlist; 62 FILE *f = stdout; 63 int ch; 64 u_int i; 65 66 /* Get options */ 67 optind = 1; 68 while ((ch = getopt(ac, av, "")) != EOF) { 69 switch (ch) { 70 case '?': 71 default: 72 return (CMDRTN_USAGE); 73 break; 74 } 75 } 76 ac -= optind; 77 av += optind; 78 79 /* Get arguments */ 80 switch (ac) { 81 case 1: 82 f = fopen(av[0], "w"); 83 if (f == NULL) { 84 warn("Could not open %s for writing", av[0]); 85 return (CMDRTN_ERROR); 86 } 87 case 0: 88 break; 89 default: 90 if (f != stdout) 91 (void)fclose(f); 92 return (CMDRTN_USAGE); 93 } 94 95 /* Get list of nodes */ 96 if (NgSendMsg(csock, ".", NGM_GENERIC_COOKIE, NGM_LISTNODES, NULL, 97 0) < 0) { 98 warn("send listnodes msg"); 99 goto error; 100 } 101 if (NgAllocRecvMsg(csock, &nlresp, NULL) < 0) { 102 warn("recv listnodes msg"); 103 goto error; 104 } 105 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"); 118 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"); 126 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]; 132 u_int j; 133 134 (void)snprintf(path, sizeof(path), "[%jx]:", 135 (uintmax_t)nlist->nodeinfo[i].id); 136 137 /* Get node info and hook list */ 138 if (NgSendMsg(csock, path, NGM_GENERIC_COOKIE, NGM_LISTHOOKS, 139 NULL, 0) < 0) { 140 free(nlresp); 141 warn("send listhooks msg"); 142 goto error; 143 } 144 if (NgAllocRecvMsg(csock, &hlresp, NULL) < 0) { 145 free(nlresp); 146 warn("recv listhooks msg"); 147 goto error; 148 } 149 150 hlist = (struct hooklist *)hlresp->data; 151 ninfo = &hlist->nodeinfo; 152 if (ninfo->hooks == 0) { 153 free(hlresp); 154 continue; 155 } 156 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"); 163 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"); 171 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) 175 continue; 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); 181 } 182 free(hlresp); 183 } 184 185 fprintf(f, "};\n"); 186 187 free(nlresp); 188 if (f != stdout) 189 (void)fclose(f); 190 return (CMDRTN_OK); 191 error: 192 if (f != stdout) 193 (void)fclose(f); 194 return (CMDRTN_ERROR); 195 } 196