summaryrefslogtreecommitdiff
path: root/src/base/main.c
diff options
context:
space:
mode:
authordautor <karlo98.m@gmail.com>2024-11-16 20:12:45 +0100
committerdautor <karlo98.m@gmail.com>2024-11-16 20:12:45 +0100
commit9638b621fa567555e51c9bc61351a708221fd2ee (patch)
treed16248bfc8d0d7704da8c99585a580dda16f7518 /src/base/main.c
parent47778ccd67cbb3fb70dda706911d3166038ca010 (diff)
Add ng_pipe link
Diffstat (limited to 'src/base/main.c')
-rw-r--r--src/base/main.c101
1 files changed, 69 insertions, 32 deletions
diff --git a/src/base/main.c b/src/base/main.c
index 27ef1c2..60d76b4 100644
--- a/src/base/main.c
+++ b/src/base/main.c
@@ -1,5 +1,6 @@
#include "state.h"
#include <netgraph.h>
+#include "../util.h"
static char const *Arg0;
@@ -214,26 +215,30 @@ start_link_(link_ *R)
fprintf(stderr, "Failed to create netgraph socket: %s\n", strerror(errno));
return -1;
}
- struct ngm_connect D;
- char Path0[NG_PATHSIZ];
- strncpy(Path0, R->Peer[0].Address, sizeof(Path0));
- strncpy(D.path, R->Peer[1].Address, sizeof(D.path));
- strncpy(D.ourhook, R->Peer[0].Hook, sizeof(D.ourhook));
- strncpy(D.peerhook, R->Peer[1].Hook, sizeof(D.peerhook));
- if(NgSendMsg(Control, Path0, NGM_GENERIC_COOKIE, NGM_CONNECT, &D, sizeof(D)) == -1)
- {
- fprintf(stderr,
- "ngctl connect %s %s %s %s: %s\n",
- Path0,
- D.path,
- D.ourhook,
- D.peerhook,
- strerror(errno));
- close(Control);
- return -1;
+ int Result;
+ switch(R->Configuration.Type)
+ {
+ case link_type_direct:
+ {
+ Result =
+ ng_connect(Control, R->Peer[0].Address, R->Peer[1].Address, R->Peer[0].Hook, R->Peer[1].Hook);
+ break;
+ }
+ case link_type_pipe:
+ {
+ Result = Create_pipe(&R->pipe.ID,
+ Control,
+ R->Peer[0].Address,
+ R->Peer[1].Address,
+ R->Peer[0].Hook,
+ R->Peer[1].Hook);
+ break;
+ }
+ case link_type_COUNT:
+ default: __builtin_unreachable();
}
close(Control);
- return 0;
+ return Result;
}
static int
@@ -245,18 +250,32 @@ stop_link_(link_ *R)
fprintf(stderr, "Failed to create netgraph socket: %s\n", strerror(errno));
return -1;
}
- struct ngm_rmhook D;
- char Path0[NG_PATHSIZ];
- strncpy(Path0, R->Peer[0].Address, sizeof(Path0));
- strncpy(D.ourhook, R->Peer[0].Hook, sizeof(D.ourhook));
- if(NgSendMsg(Control, Path0, NGM_GENERIC_COOKIE, NGM_RMHOOK, &D, sizeof(D)) == -1)
+ switch(R->Configuration.Type)
{
- fprintf(stderr, "ngctl rmhook %s %s: %s\n", Path0, D.ourhook, strerror(errno));
- close(Control);
- return -1;
+ case link_type_direct:
+ {
+ struct ngm_rmhook D;
+ char Path0[NG_PATHSIZ];
+ strncpy(Path0, R->Peer[0].Address, sizeof(Path0));
+ strncpy(D.ourhook, R->Peer[0].Hook, sizeof(D.ourhook));
+ if(NgSendMsg(Control, Path0, NGM_GENERIC_COOKIE, NGM_RMHOOK, &D, sizeof(D)) == -1)
+ {
+ fprintf(stderr, "ngctl rmhook %s %s: %s\n", Path0, D.ourhook, strerror(errno));
+ close(Control);
+ return -1;
+ }
+ close(Control);
+ return 0;
+ }
+ case link_type_pipe:
+ {
+ int Result = DestroyNetgraphNode(R->pipe.ID, Control);
+ close(Control);
+ return Result;
+ }
+ case link_type_COUNT:
+ default: __builtin_unreachable();
}
- close(Control);
- return 0;
}
static int
@@ -265,7 +284,8 @@ start_link(experiment *E,
char const *NodeA,
char const *InterfaceA,
char const *NodeB,
- char const *InterfaceB)
+ char const *InterfaceB,
+ link_type Type)
{
if(FindLinkIndex(E, Name) != SIZE_MAX)
{
@@ -278,6 +298,7 @@ start_link(experiment *E,
C.Peer[0].Interface = strdup(InterfaceA);
C.Peer[1].Node = strdup(NodeB);
C.Peer[1].Interface = strdup(InterfaceB);
+ C.Type = Type;
}
link_ R;
{ // Start
@@ -606,7 +627,7 @@ static void __attribute__((noreturn)) usage(void)
"usage: %1$s show\n"
" %1$s node start [node-name] [configuration]\n"
" %1$s node stop [node-name]\n"
- " %1$s link start [link-name] [node-A-name] [interface-A-name] [node-B-name] [interface-B-name]\n"
+ " %1$s link start [link-name] [node-A-name] [interface-A-name] [node-B-name] [interface-B-name] [?type]\n"
" %1$s link stop [link-name]\n"
" %1$s node cmd [node-name] [command...]\n"
" %1$s node mod [node-name] [command...]\n",
@@ -717,8 +738,24 @@ Command_link(experiment *E, fd StateFD, int ArgCount, char **Arg)
int Result;
if(strcmp(Command, "start") == 0)
{
- if(ArgCount != 4) usage();
- Result = start_link(E, LinkName, Arg[0], Arg[1], Arg[2], Arg[3]);
+ if(ArgCount == 4)
+ {
+ Result = start_link(E, LinkName, Arg[0], Arg[1], Arg[2], Arg[3], link_type_direct);
+ } else if(ArgCount == 5)
+ {
+ link_type Type;
+ if(FromString_link_type(Arg[4], &Type) == -1)
+ {
+ fprintf(stderr, "Unknown link type '%s'\n", Arg[4]);
+ Result = -1;
+ } else
+ {
+ Result = start_link(E, LinkName, Arg[0], Arg[1], Arg[2], Arg[3], Type);
+ }
+ } else
+ {
+ usage();
+ }
} else if(strcmp(Command, "stop") == 0)
{
if(ArgCount != 0) usage();