better simple-tcpd
parent
df373aea6a
commit
e8cd339622
|
@ -42,7 +42,7 @@ void chomp (char *str, size_t len)
|
|||
else
|
||||
connection from the client:
|
||||
1. client sends service name
|
||||
2. networkd establishes a connection to the service
|
||||
2. ipcd establishes a connection to the service
|
||||
3. ack
|
||||
else
|
||||
lolwat shouldn't happen :(
|
||||
|
@ -52,26 +52,26 @@ void chomp (char *str, size_t len)
|
|||
|
||||
#define SERVICE_NAME "simpletcp"
|
||||
|
||||
struct networkd *ipcd_ctx;
|
||||
struct ipcd *ipcd = NULL;
|
||||
|
||||
void handle_disconnection (int fd)
|
||||
{
|
||||
int delfd;
|
||||
|
||||
delfd = ipc_switching_del (ipcd_ctx->TCP_TO_IPC, fd);
|
||||
delfd = ipc_switching_del (&ipcd->TCP_TO_IPCFD, fd);
|
||||
if (delfd >= 0) {
|
||||
close (delfd);
|
||||
ipc_del_fd (ipcd_ctx->clients, delfd);
|
||||
ipc_del_fd (&ipcd->ctx, delfd);
|
||||
}
|
||||
|
||||
close (fd);
|
||||
ipc_del_fd (ipcd_ctx->clients, fd);
|
||||
ipc_del_fd (&ipcd->ctx, fd);
|
||||
|
||||
// printf ("TCP_TO_IPC\n");
|
||||
ipc_switching_print (ipcd_ctx->TCP_TO_IPC);
|
||||
// printf ("TCP_TO_IPCFD\n");
|
||||
ipc_switching_print (&ipcd->TCP_TO_IPCFD);
|
||||
}
|
||||
|
||||
void tcp_connection (char **env, int fd)
|
||||
void tcp_connection (int fd)
|
||||
{
|
||||
SECURE_BUFFER_DECLARATION (char, buf, BUFSIZ);
|
||||
|
||||
|
@ -89,16 +89,14 @@ void tcp_connection (char **env, int fd)
|
|||
// TODO: tests
|
||||
T_PERROR_Q ((send (fd, "OK", 2, 0) <= 0), "sending a message", EXIT_FAILURE);
|
||||
|
||||
SECURE_DECLARATION (struct ipc_ctx, tcp_to_ipc_ci);
|
||||
|
||||
struct ipc_error ret = ipc_connection (env, &tcp_to_ipc_ci, buf);
|
||||
struct ipc_error ret = ipc_connection (&ipcd->ctx, buf);
|
||||
if (ret.error_code != IPC_ERROR_NONE) {
|
||||
fprintf (stderr, "%s\n", ret.error_message);
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
ipc_switching_add (ipcd_ctx->TCP_TO_IPC, fd, tcp_to_ipc_ci.fd);
|
||||
ipc_add_fd (ipcd_ctx->clients, tcp_to_ipc_ci.fd);
|
||||
ipc_switching_add (&ipcd->TCP_TO_IPCFD, fd, tcp_to_ipc_ci.fd);
|
||||
ipc_add_fd (&ipcd->ctx, tcp_to_ipc_ci.fd);
|
||||
}
|
||||
|
||||
int accept_new_client (int serverfd)
|
||||
|
@ -112,12 +110,12 @@ int accept_new_client (int serverfd)
|
|||
EXIT_FAILURE);
|
||||
|
||||
// adding a client
|
||||
ipc_add_fd (ipcd_ctx->clients, sock_fd_client);
|
||||
ipc_add_fd (&ipcd->ctx, sock_fd_client);
|
||||
|
||||
return sock_fd_client;
|
||||
}
|
||||
|
||||
void main_loop (int argc, char **argv, char **env)
|
||||
void main_loop (int argc, char **argv)
|
||||
{
|
||||
argc = argc; // FIXME: useless
|
||||
int serverfd;
|
||||
|
@ -155,24 +153,22 @@ void main_loop (int argc, char **argv, char **env)
|
|||
return;
|
||||
}
|
||||
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->clients, sizeof (struct ipc_ctx),, EXIT_FAILURE);
|
||||
SECURE_DECLARATION (struct ipc_event, event);
|
||||
|
||||
ipc_add_fd (ipcd_ctx->clients, serverfd);
|
||||
ipc_add_fd (&ipcd->ctx, serverfd);
|
||||
|
||||
while (1) {
|
||||
// ipc_wait_event provides one event at a time
|
||||
// warning: event->m is free'ed if not NULL
|
||||
long timer = 10;
|
||||
int timer = 10000;
|
||||
|
||||
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event_networkd (ipcd_ctx->clients, ipcd_ctx->srv, &event, ipcd_ctx->TCP_TO_IPC, &timer)
|
||||
, EXIT_FAILURE);
|
||||
TEST_IPC_WAIT_EVENT_Q (ipc_events_loop (&ipcd->ctx, &event, &timer), EXIT_FAILURE);
|
||||
|
||||
switch (event.type) {
|
||||
case IPC_EVENT_TYPE_TIMER:{
|
||||
printf ("timed out!\n");
|
||||
|
||||
timer = 10;
|
||||
timer = 10000;
|
||||
}
|
||||
break;
|
||||
case IPC_EVENT_TYPE_SWITCH:{
|
||||
|
@ -185,29 +181,29 @@ void main_loop (int argc, char **argv, char **env)
|
|||
// NEW CLIENT
|
||||
if (event.origin == serverfd) {
|
||||
int sock_fd_client = accept_new_client (serverfd);
|
||||
ipcd_ctx->cpt++;
|
||||
printf ("TCP connection: %d clients connected\n", ipcd_ctx->cpt);
|
||||
ipcd->cpt++;
|
||||
printf ("TCP connection: %d ctx connected\n", ipcd->cpt);
|
||||
printf ("new TCP client has the fd %d\n", sock_fd_client);
|
||||
}
|
||||
// CLIENT IS TALKING
|
||||
else {
|
||||
tcp_connection (env, event.origin);
|
||||
tcp_connection (event.origin);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case IPC_EVENT_TYPE_CONNECTION:
|
||||
{
|
||||
ipcd_ctx->cpt++;
|
||||
printf ("connection: %d clients connected\n", ipcd_ctx->cpt);
|
||||
ipcd->cpt++;
|
||||
printf ("connection: %d ctx connected\n", ipcd->cpt);
|
||||
printf ("new client has the fd %d\n", event.origin);
|
||||
};
|
||||
break;
|
||||
|
||||
case IPC_EVENT_TYPE_DISCONNECTION:
|
||||
{
|
||||
ipcd_ctx->cpt--;
|
||||
printf ("disconnection: %d clients remaining\n", ipcd_ctx->cpt);
|
||||
ipcd->cpt--;
|
||||
printf ("disconnection: %d ctx remaining\n", ipcd->cpt);
|
||||
};
|
||||
break;
|
||||
case IPC_EVENT_TYPE_MESSAGE:
|
||||
|
@ -218,16 +214,16 @@ void main_loop (int argc, char **argv, char **env)
|
|||
}
|
||||
|
||||
// m->fd = 3;
|
||||
TEST_IPC_P (ipc_write (event.clients, m), "server write");
|
||||
TEST_IPC_P (ipc_write (event.ctx, m), "server write");
|
||||
};
|
||||
break;
|
||||
case IPC_EVENT_TYPE_ERROR:
|
||||
case IPC_EVENT_TYPE_TX:
|
||||
{
|
||||
printf ("a message was sent\n");
|
||||
}
|
||||
break;
|
||||
case IPC_EVENT_TYPE_ERROR:
|
||||
fprintf (stderr, "a problem happened with client %d\n", (event.origin)->fd);
|
||||
fprintf (stderr, "a problem happened with client %d\n", event.origin);
|
||||
break;
|
||||
default:
|
||||
fprintf (stderr, "there must be a problem, event not set\n");
|
||||
|
@ -242,26 +238,22 @@ void exit_program (int signal)
|
|||
{
|
||||
printf ("Quitting, signal: %d\n", signal);
|
||||
|
||||
// Close then free remaining clients.
|
||||
ipc_close_all (ipcd_ctx->clients);
|
||||
ipc_ctx_free (ipcd_ctx->clients);
|
||||
// Close then free remaining ctx.
|
||||
ipc_close_all (&ipcd->ctx);
|
||||
// ipc_ctx_free (&ipcd->ctx);
|
||||
|
||||
// free, free everything!
|
||||
free (ipcd_ctx->clients);
|
||||
free (ipcd_ctx->srv);
|
||||
free (ipcd_ctx->TCP_TO_IPC->collection);
|
||||
free (ipcd_ctx->TCP_TO_IPC);
|
||||
free (ipcd_ctx);
|
||||
free (ipcd);
|
||||
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
* service ping-pong: send back everything sent by the clients
|
||||
* service ping-pong: send back everything sent by the ctx
|
||||
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
|
||||
*/
|
||||
|
||||
int main (int argc, char *argv[], char **env)
|
||||
int main (int argc, char *argv[])
|
||||
{
|
||||
// check the number of args on command line
|
||||
if (argc != 2) {
|
||||
|
@ -271,17 +263,14 @@ int main (int argc, char *argv[], char **env)
|
|||
|
||||
printf ("pid = %d\n", getpid ());
|
||||
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx, sizeof (struct networkd) ,, EXIT_FAILURE);
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->TCP_TO_IPC, sizeof (struct ipc_switchings) ,, EXIT_FAILURE);
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->TCP_TO_IPC->collection, sizeof (struct ipc_switching) ,, EXIT_FAILURE);
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd_ctx->srv, sizeof (struct ipc_ctx),, EXIT_FAILURE);
|
||||
SECURE_BUFFER_HEAP_ALLOCATION_Q (ipcd, sizeof (struct ipcd),, EXIT_FAILURE);
|
||||
|
||||
struct ipc_error ret = ipc_server_init (env, ipcd_ctx->srv, SERVICE_NAME);
|
||||
struct ipc_error ret = ipc_server_init (&ipcd->ctx, SERVICE_NAME);
|
||||
if (ret.error_code != IPC_ERROR_NONE) {
|
||||
fprintf (stderr, "%s\n", ret.error_message);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
printf ("Listening on [%s].\n", ipcd_ctx->srv->spath);
|
||||
printf ("Listening on [%s].\n", ipcd->ctx->spath);
|
||||
|
||||
printf ("MAIN: server created\n");
|
||||
|
||||
|
@ -293,7 +282,7 @@ int main (int argc, char *argv[], char **env)
|
|||
signal (SIGINT , exit_program);
|
||||
|
||||
// the service will loop until the end of time, or a signal
|
||||
main_loop (argc, argv, env);
|
||||
main_loop (argc, argv);
|
||||
|
||||
// main_loop should not return
|
||||
return EXIT_FAILURE;
|
||||
|
|
Reference in New Issue