ref: cd39cdd045070768ff17282f55b30991abc82dea
parent: f31bebf7e813616b83b255d665bf1283abef5250
author: sirjofri <sirjofri@sirjofri.de>
date: Sat Aug 30 18:38:36 EDT 2025
adds connections
--- /dev/null
+++ b/conns.c
@@ -1,0 +1,73 @@
+#include <u.h>
+#include <libc.h>
+#include <String.h>
+#include "dat.h"
+#include "fns.h"
+
+/* calculated: these tuples refer to systems */
+static char *connsrv[] = {+ "auth", "dns", "ns", "mx", "fs", "wins", "ntp", "time",
+};
+
+int
+isconnsrv(char *key)
+{+ int n = sizeof(connsrv) / sizeof(*connsrv);
+ for (int i = 0; i < n; i++)
+ if (strcmp(connsrv[i], key) == 0)
+ return 1;
+ return 0;
+}
+
+Conn *conns = nil;
+int nconns = 0;
+
+static Conn*
+findconn(Sys *from, Sys *to)
+{+ Conn *c;
+ for (int i = 0; i < nconns; i++) {+ c = &conns[i];
+ if (c->from == from && c->to == to)
+ return c;
+ }
+
+ if (!conns) {+ nconns = 1;
+ conns = mallocz(sizeof(Conn), 1);
+ c = conns;
+ } else {+ conns = realloc(conns, sizeof(Conn) * (nconns+1));
+ c = &conns[nconns];
+ nconns++;
+ }
+ c->from = from;
+ c->to = to;
+ c->types = nil;
+ return c;
+}
+
+void
+addconn(Sys *from, Sys *to, char *type)
+{+ Conn *c;
+
+ if (!isconnsrv(type))
+ return;
+
+ c = findconn(from, to);
+
+ if (c->types)
+ s_append(c->types, ",");
+ else
+ c->types = s_new();
+
+ s_append(c->types, type);
+}
+
+void
+forconn(void (*f)(Conn*,void*), void *aux)
+{+ for (int i = 0; i < nconns; i++)
+ f(&conns[i], aux);
+}
--- a/dat.h
+++ b/dat.h
@@ -3,6 +3,7 @@
typedef struct Sys Sys;
typedef struct Ipnet Ipnet;
typedef struct Pos Pos;
+typedef struct Conn Conn;
extern char *netdir;
extern char *systuples[];
@@ -37,4 +38,10 @@
struct Ipnet {Block;
+};
+
+struct Conn {+ Sys *from;
+ Sys *to;
+ String *types;
};
--- a/draw.c
+++ b/draw.c
@@ -1,14 +1,17 @@
#include <u.h>
#include <libc.h>
+#include <String.h>
#include "dat.h"
#include "fns.h"
static int lineheight = 10;
-static int charwidth = 7;
+static int charwidth = 6;
static int boxoffsetx = 2;
static int boxoffsety = 2;
+static int labeloffset = 20;
+
void
dheader()
{@@ -60,14 +63,18 @@
"%d %d lineto\n"
"%d %d lineto\n"
"%d %d lineto\n"
- "%d %d lineto\n"
+ "closepath\n"
+ "gsave\n"
+ "1 setgray\n"
+ "fill\n"
+ "grestore\n"
"1 setlinewidth\n"
+ "0 setgray\n"
"stroke\n",
p1.x, p1.y,
p2.x, p1.y,
p2.x, p2.y,
- p1.x, p2.y,
- p1.x, p1.y);
+ p1.x, p2.y);
}
static void
@@ -75,7 +82,7 @@
{Pos *p = aux;
- drawtext(*p, 1, "%s: %s", t->key, t->value);
+ drawtext(*p, 1, "%s%s: %s", t->ipnet ? "*" : "", t->key, t->value);
p->y -= lineheight;
}
@@ -86,9 +93,9 @@
Pos p = b->p;
p.y = pageheight - p.y;
+ drawbox(b);
np = p;
fortuple(b, drawtuple, &np);
- drawbox(b);
}
static void
@@ -109,4 +116,87 @@
b->height = lineheight * b->ntuples;
fortuple(b, tuplesize, b);
b->width *= charwidth;
+}
+
+static void
+dlabel(Pos p, float angle, char *label)
+{+ int n = charwidth * strlen(label) + 4;
+
+ print("gsave\n");+
+ print(
+ "%d %d translate\n"
+ "%f rotate\n"
+ "newpath\n"
+ "-2 -5 moveto\n"
+ "%d %d rlineto\n"
+ "%d %d rlineto\n"
+ "%d %d rlineto\n"
+ "closepath\n"
+ "gsave\n"
+ "1 setgray\n"
+ "fill\n"
+ "grestore\n"
+ "0 setgray\n"
+ "0.5 setlinewidth\n"
+ "stroke\n",
+ p.x, p.y,
+ angle,
+ n, 0,
+ 0, lineheight+2,
+ -n, 0
+ );
+
+ print(
+ "newpath\n"
+ "0 -2 moveto\n"
+ "(%s) show\n",
+ label);
+
+ print("grestore\n");+}
+
+void
+dconn(Block *from, Block *to, char *label)
+{+ Pos p1, p2, c;
+ float fx, fy, d;
+ int m;
+
+ p1 = from->p;
+ p2 = to->p;
+ p1.y = pageheight - p1.y;
+ p2.y = pageheight - p2.y;
+
+ p1.x += from->width/2;
+ p1.y -= from->height/2;
+ p2.x += from->width/2;
+ p2.y -= from->height/2;
+
+ c.x = pagewidth/2;
+ c.y = pageheight/2;
+
+ print("newpath\n"+ "%d %d moveto\n"
+ "%d %d %d %d %d %d curveto\n"
+ "1 setlinewidth\n"
+ "stroke\n",
+ p1.x, p1.y,
+ c.x, c.y, c.x, c.y,
+ p2.x, p2.y);
+
+ m = from->width > from->height ? from->width : from->height;
+ m /= 2;
+ m += labeloffset;
+ fx = c.x - p1.x;
+ fy = c.y - p1.y;
+ d = sqrt(fx*fx + fy*fy);
+ fx /= d;
+ fy /= d;
+ p1.x += fx * m;
+ p1.y += fy * m;
+
+ fx = atan2(fy, fx) * 180./PI;
+ dlabel(p1, fx, label);
}
--- a/fns.h
+++ b/fns.h
@@ -4,6 +4,7 @@
void dblock(Block*);
void setblocksize(Block*);
+void dconn(Block*, Block*, char*);
/* tuple functions */
Tuple *findtuple(Block*, char*);
@@ -16,6 +17,11 @@
Sys *findsys(char*,char*);
void forsys(void (*f)(Sys*,void*), void*);
int numsystems(void);
+
+/* conn functions */
+void addconn(Sys*, Sys*, char*);
+void forconn(void (*f)(Conn*,void*), void*);
+int isconnsrv(char*);
/* ipnet functions */
int isnetarg(char*);
--- a/mkfile
+++ b/mkfile
@@ -7,5 +7,8 @@
sys.$O\
ipnet.$O\
tuple.$O\
+ conns.$O\
+
+HFILES=dat.h fns.h
</sys/src/cmd/mkone
--- a/netgraph.c
+++ b/netgraph.c
@@ -2,6 +2,7 @@
#include <libc.h>
#include <bio.h>
#include <ndb.h>
+#include <String.h>
#include "dat.h"
#include "fns.h"
@@ -60,7 +61,7 @@
fortuple(s, dumptuples, nil);
}
-void
+static void
calcsyslayout(Sys *s, void *aux)
{int n = numsystems();
@@ -80,7 +81,31 @@
(*ip)++;
}
+static void
+calcconn(Tuple *t, void *aux)
+{+ Sys *from = aux;
+ Sys *to = findsys(t->key, t->value);
+ if (!to)
+ return;
+ if (to == from)
+ return;
+ addconn(from, to, t->key);
+}
+
void
+calcconns(Sys *s, void*)
+{+ fortuple(s, calcconn, s);
+}
+
+static void
+drawconns(Conn *c, void*)
+{+ dconn(c->from, c->to, s_to_c(c->types));
+}
+
+static void
drawsystems(Sys *s, void*)
{dblock(s);
@@ -90,7 +115,7 @@
main(int argc, char **argv)
{Biobuf *bin;
- char *stp = "sys,ether,ip,dom,auth,authdom";
+ char *stp = "sys,ether,ip,ipnet,dom,auth,authdom";
char *s;
int i;
@@ -118,10 +143,11 @@
Bterm(bin);
-
dheader();
i = 0;
forsys(calcsyslayout, &i);
+ forsys(calcconns, nil);
+ forconn(drawconns, nil);
forsys(drawsystems, nil);
dfooter();
}
--- a/sys.c
+++ b/sys.c
@@ -2,9 +2,11 @@
#include <libc.h>
#include <bio.h>
#include <ndb.h>
+#include <String.h>
#include "dat.h"
#include "fns.h"
+/* user request: these tuples are systems */
static char *sysargs[] = {"sys", "ip", "dom",
};
@@ -54,10 +56,9 @@
issysarg(char *key)
{int n = sizeof(sysargs) / sizeof(*sysargs);
- for (int i = 0; i < n; i++) {+ for (int i = 0; i < n; i++)
if (strcmp(sysargs[i], key) == 0)
return 1;
- }
return 0;
}
@@ -81,6 +82,9 @@
return;
addtuple(s, key, val, 1);
+
+ if (!isconnsrv(key))
+ return;
if (gensys("sys", val))return;
--- a/tuple.c
+++ b/tuple.c
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <String.h>
#include "dat.h"
#include "fns.h"
--
⑨