shithub: netgraph

ref: 59402bf42b8b199218f1f0d111aaf273a3c1806a
dir: /conns.c/

View raw version
#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);
}