3 #define _POSIX_C_SOURCE 200809L
18 extern char **environ;
22 int giStack_InFD = -1;
23 int giNumStackArgs = 1;
24 char *gasStackArgs[MAX_ARGS] = {"nettest",NULL};
27 void Stack_AddDevice(const char *Ident, const void *MacAddr)
30 Stack_AddArg("%02x%02x%02x%02x%02x%02x:unix:%s",
31 ((uint8_t*)MacAddr)[0],
32 ((uint8_t*)MacAddr)[1],
33 ((uint8_t*)MacAddr)[2],
34 ((uint8_t*)MacAddr)[3],
35 ((uint8_t*)MacAddr)[4],
36 ((uint8_t*)MacAddr)[5],
40 void Stack_AddInterface(const char *Name, int AddrType, const void *Addr, int MaskBits)
46 Stack_AddArg("%s,%i.%i.%i.%i/%i", Name,
55 assert(AddrType == 4);
56 fprintf(stderr, "Stack_AddInterface: Bad address type %i\n", AddrType);
61 void Stack_AddRoute(int Type, const void *Network, int MaskBits, const void *NextHop)
65 void Stack_AddArg(const char *Fmt, ...)
67 if( giNumStackArgs == MAX_ARGS ) {
68 fprintf(stderr, "ERROR: Too many arguments to stack, %i max\n", MAX_ARGS);
74 size_t len = vsnprintf(NULL, 0, Fmt, args);
77 char *arg = malloc(len+1);
81 vsnprintf(arg, len+1, Fmt, args);
84 assert( arg[len] == '\0' );
85 gasStackArgs[giNumStackArgs++] = arg;
88 void sigchld_handler(int signum)
92 fprintf(stderr, "FAILURE: Child exited (%i)\n", status);
95 int Stack_Start(const char *Subcommand)
97 Stack_AddArg(Subcommand);
99 for( int i = 3; i < 16; i ++ )
100 fcntl(i, F_SETFD, FD_CLOEXEC);
102 //signal(SIGCHLD, sigchld_handler);
106 giStack_InFD = fds_in[1];
107 fcntl(giStack_InFD, F_SETFD, FD_CLOEXEC);
110 fp = fopen("stdout.txt", "a"); fprintf(fp, "--- Startup\n"); fclose(fp);
111 fp = fopen("stderr.txt", "a"); fprintf(fp, "--- Startup\n"); fclose(fp);
113 posix_spawn_file_actions_t fa;
114 posix_spawn_file_actions_init(&fa);
115 posix_spawn_file_actions_adddup2(&fa, fds_in[0], 0);
116 posix_spawn_file_actions_addopen(&fa, 1, "stdout.txt", O_CREAT|O_APPEND|O_WRONLY, 0644);
117 posix_spawn_file_actions_addopen(&fa, 2, "stderr.txt", O_CREAT|O_APPEND|O_WRONLY, 0644);
119 int rv = posix_spawn(&giStack_PID, "./nettest", &fa, NULL, gasStackArgs, environ);
122 fprintf(stderr, "posix_spawn failed: %s", strerror(rv));
126 posix_spawn_file_actions_destroy(&fa);
130 void Stack_Kill(void)
134 kill(giStack_PID, SIGTERM);
138 for( int i = 1; i < giNumStackArgs; i ++ )
139 free(gasStackArgs[i]);
143 int Stack_SendCommand(const char *Fmt, ...)
147 size_t len = vsnprintf(NULL, 0, Fmt, args);
151 vsnprintf(command, len+1, Fmt, args);
154 write(giStack_InFD, command, len);
155 write(giStack_InFD, "\n", 1);