File: dot.c

package info (click to toggle)
plotnetcfg 0.4.1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 336 kB
  • sloc: ansic: 2,952; makefile: 56
file content (161 lines) | stat: -rw-r--r-- 4,020 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * This file is a part of plotnetcfg, a tool to visualize network config.
 * Copyright (C) 2014 Red Hat, Inc. -- Jiri Benc <jbenc@redhat.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <sys/socket.h>
#include <time.h>
#include "../frontend.h"
#include "../handler.h"
#include "../if.h"
#include "../label.h"
#include "../netns.h"
#include "../utils.h"
#include "../version.h"
#include "dot.h"

static void output_label(struct label *list)
{
	struct label *ptr;

	for (ptr = list; ptr; ptr = ptr->next)
		printf("\\n%s", ptr->text);
}

static void output_addresses(struct if_addr_entry *list)
{
	struct if_addr_entry *ptr;

	for (ptr = list; ptr; ptr = ptr->next) {
		printf("\\n%s", ptr->addr.formatted);
		if (ptr->peer.formatted)
			printf(" peer %s", ptr->peer.formatted);
	}
}

static void output_mtu(struct if_entry *ptr)
{
	if (ptr->mtu && ptr->mtu != 1500 && !(ptr->flags & IF_LOOPBACK))
		printf("\\nMTU %d", ptr->mtu);
}

static void output_ifaces_pass1(struct if_entry *list)
{
	struct if_entry *ptr;

	for (ptr = list; ptr; ptr = ptr->next) {
		printf("\"%s\" [label=\"%s", ifid(ptr), ptr->if_name);
		if (ptr->driver)
			printf(" (%s)", ptr->driver);
		output_label(ptr->label);
		output_mtu(ptr);
		output_addresses(ptr->addr);
		printf("\"");
		if (ptr->flags & IF_INTERNAL)
			printf(",style=dotted");
		else if (!(ptr->flags & IF_UP))
			printf(",style=filled,fillcolor=\"grey\"");
		else if (!(ptr->flags & IF_HAS_LINK))
			printf(",style=filled,fillcolor=\"pink\"");
		else
			printf(",style=filled,fillcolor=\"darkolivegreen1\"");
		if (ptr->warnings)
			printf(",color=\"red\"");
		printf("]\n");
	}
}

static void output_ifaces_pass2(struct if_entry *list)
{
	struct if_entry *ptr;

	for (ptr = list; ptr; ptr = ptr->next) {
		if (ptr->master) {
			printf("\"%s\" -> ", ifid(ptr));
			printf("\"%s\"", ifid(ptr->master));
			if (ptr->edge_label && !ptr->link)
				printf(" [label=\"%s\"]", ptr->edge_label);
			printf("\n");
		}
		if (ptr->link) {
			printf("\"%s\" -> ", ifid(ptr->link));
			printf("\"%s\"", ifid(ptr));
			if (ptr->edge_label)
				printf(" [label=\"%s\"]", ptr->edge_label);
			printf("\n");
		}
		if (ptr->peer &&
		    (((unsigned long)ptr > (unsigned long)ptr->peer) ||
		     !ptr->peer->peer)) {
			printf("\"%s\" -> ", ifid(ptr));
			printf("\"%s\" [dir=none%s]\n", ifid(ptr->peer),
			       ptr->flags & IF_PEER_WEAK ? ",style=dotted" : "");
		}
	}
}

static void output_warnings(struct netns_entry *root)
{
	struct netns_entry *ns;
	int was_label = 0;

	for (ns = root; ns; ns = ns->next) {
		if (ns->warnings) {
			if (!was_label)
				printf("label=\"");
			was_label = 1;
			output_label(ns->warnings);
		}
	}
	if (was_label) {
		printf("\"\n");
		printf("fontcolor=\"red\"\n");
	}
}

static void dot_output(struct netns_entry *root)
{
	struct netns_entry *ns;
	time_t cur;

	time(&cur);
	printf("// generated by plotnetcfg " VERSION " on %s", ctime(&cur));
	printf("digraph {\nnode [shape=box]\n");
	for (ns = root; ns; ns = ns->next) {
		if (ns->name) {
			printf("subgraph \"cluster_%s\" {\n", ns->name);
			printf("label=\"%s\"\n", ns->name);
			printf("fontcolor=\"black\"\n");
		}
		output_ifaces_pass1(ns->ifaces);
		if (ns->name)
			printf("}\n");
	}
	for (ns = root; ns; ns = ns->next) {
		output_ifaces_pass2(ns->ifaces);
	}
	output_warnings(root);
	printf("}\n");
}

static struct frontend fe_dot = {
	.format = "dot",
	.desc = "dot language (suitable for graphviz)",
	.output = dot_output,
};

void frontend_dot_register(void)
{
	frontend_register(&fe_dot);
}