2019-06-12 15:02:43 +02:00
|
|
|
#include "../src/ipc.h"
|
2020-01-01 12:11:34 +01:00
|
|
|
// #include "../src/log.h"
|
2019-06-12 15:02:43 +02:00
|
|
|
#include "../src/utils.h"
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
#define CLOG_DEBUG(a, ...) LOG_DEBUG (""); LOG_DEBUG("\033[36m" a "\033[00m", #__VA_ARGS__); LOG_DEBUG ("")
|
2019-07-27 15:48:10 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
void chomp (char *str, size_t len)
|
|
|
|
{
|
|
|
|
if (str[len - 1] == '\n') {
|
|
|
|
str[len - 1] = '\0';
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
if (str[len - 2] == '\n') {
|
|
|
|
str[len - 2] = '\0';
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* This program is under heavy developement.
|
|
|
|
* Still many things to do.
|
|
|
|
* NOT READY FOR PRODUCTION.
|
|
|
|
*
|
|
|
|
Server side:
|
|
|
|
1. sock_tcp = tcp socket
|
|
|
|
2. ipc_server_init
|
|
|
|
3. ipc_add (ipc_server, sock_tcp)
|
|
|
|
4. wait_event
|
|
|
|
if reading on extra socket
|
|
|
|
if reading on sock_tcp
|
|
|
|
sock_client = accept
|
|
|
|
ipc_add_fd sock_client
|
2019-07-27 15:48:10 +02:00
|
|
|
elif
|
|
|
|
if socket bind to another (client to service or service to client)
|
|
|
|
reading on fd ; writing on related fd
|
|
|
|
else
|
|
|
|
connection from the client:
|
|
|
|
1. client sends service name
|
2020-07-04 14:40:55 +02:00
|
|
|
2. ipcd establishes a connection to the service
|
2019-07-27 15:48:10 +02:00
|
|
|
3. ack
|
2019-06-12 15:02:43 +02:00
|
|
|
else
|
|
|
|
lolwat shouldn't happen :(
|
|
|
|
elif reading on usual socket
|
|
|
|
do something
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define SERVICE_NAME "simpletcp"
|
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
struct ipc_ctx *ctx = NULL;
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
void handle_disconnection (int fd)
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
2019-07-27 15:48:10 +02:00
|
|
|
int delfd;
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
delfd = ipc_switching_del (&ctx->switchdb, fd);
|
2019-07-27 15:48:10 +02:00
|
|
|
if (delfd >= 0) {
|
|
|
|
close (delfd);
|
2020-07-04 19:02:43 +02:00
|
|
|
ipc_del_fd (ctx, delfd);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
close (fd);
|
2020-07-04 19:02:43 +02:00
|
|
|
ipc_del_fd (ctx, fd);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
// printf ("ctx.switchdb\n");
|
|
|
|
ipc_switching_print (&ctx->switchdb);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 14:40:55 +02:00
|
|
|
void tcp_connection (int fd)
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
SECURE_BUFFER_DECLARATION (char, buf, BUFSIZ);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
ssize_t len = recv (fd, buf, BUFSIZ, 0);
|
|
|
|
if (len <= 0) {
|
|
|
|
handle_disconnection (fd);
|
2020-01-01 12:11:34 +01:00
|
|
|
return;
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
buf[len] = '\0';
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
// XXX: for testing purposes
|
|
|
|
chomp (buf, len);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
// TODO: tests
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q ((send (fd, "OK", 2, 0) <= 0), "sending a message", EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
printf ("connection to %s\n", buf);
|
2020-07-08 12:40:24 +02:00
|
|
|
struct ipc_error ret = ipc_connection_switched (ctx, buf);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
fprintf (stderr, "%s\n", ret.error_message);
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
ipc_switching_add (&ctx->switchdb, fd, ctx->pollfd[ctx->size-1].fd);
|
2020-07-08 12:40:24 +02:00
|
|
|
ipc_ctx_fd_type (ctx, fd, IPC_CONNECTION_TYPE_SWITCHED);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
int accept_new_client (int serverfd)
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
|
|
|
SECURE_DECLARATION (struct sockaddr_in, client);
|
|
|
|
socklen_t addrlen = 0;
|
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
int sock_fd_client;
|
2020-01-01 12:11:34 +01:00
|
|
|
T_PERROR_Q (((sock_fd_client =
|
|
|
|
accept (serverfd, (struct sockaddr *)&client, &addrlen)) == -1), "accept new client",
|
|
|
|
EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-08 12:40:24 +02:00
|
|
|
// adding a client, for now not switched:
|
|
|
|
// tcpd should handle the first message (getting the service name)
|
|
|
|
ipc_add_fd (ctx, sock_fd_client);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
return sock_fd_client;
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 14:40:55 +02:00
|
|
|
void main_loop (int argc, char **argv)
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
argc = argc; // FIXME: useless
|
2019-07-27 15:48:10 +02:00
|
|
|
int serverfd;
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
SECURE_DECLARATION (struct sockaddr_in, my_addr);
|
2019-06-12 15:02:43 +02:00
|
|
|
socklen_t addrlen;
|
|
|
|
|
|
|
|
// socket factory
|
2020-01-01 12:11:34 +01:00
|
|
|
if ((serverfd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
|
|
|
|
perror ("socket");
|
|
|
|
return;
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
int yes = 1;
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
if (setsockopt (serverfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (int)) == -1) {
|
|
|
|
perror ("setsockopt");
|
|
|
|
return;
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
// init local addr structure and other params
|
2020-01-01 12:11:34 +01:00
|
|
|
my_addr.sin_family = AF_INET;
|
|
|
|
my_addr.sin_port = htons (atoi (argv[1]));
|
2019-06-12 15:02:43 +02:00
|
|
|
my_addr.sin_addr.s_addr = INADDR_ANY;
|
2020-01-01 12:11:34 +01:00
|
|
|
addrlen = sizeof (struct sockaddr_in);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// bind addr structure with socket
|
2020-01-01 12:11:34 +01:00
|
|
|
if (bind (serverfd, (struct sockaddr *)&my_addr, addrlen) == -1) {
|
|
|
|
perror ("bind");
|
|
|
|
return;
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
// set the socket in passive mode (only used for accept())
|
|
|
|
// and set the list size for pending connection
|
2020-01-01 12:11:34 +01:00
|
|
|
if (listen (serverfd, 5) == -1) {
|
|
|
|
perror ("listen");
|
|
|
|
return;
|
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
SECURE_DECLARATION (struct ipc_event, event);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
ipc_add_fd (ctx, serverfd);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
int cpt = 0;
|
|
|
|
|
|
|
|
int timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
while (1) {
|
2019-07-27 15:48:10 +02:00
|
|
|
// ipc_wait_event provides one event at a time
|
2019-06-12 15:02:43 +02:00
|
|
|
// warning: event->m is free'ed if not NULL
|
2020-01-01 12:11:34 +01:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
ipc_ctx_print (ctx);
|
2020-07-06 08:43:06 +02:00
|
|
|
TEST_IPC_WAIT_EVENT_Q (ipc_wait_event (ctx, &event, &timer), EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
switch (event.type) {
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_TIMER:{
|
|
|
|
printf ("timed out!\n");
|
2020-07-04 14:40:55 +02:00
|
|
|
timer = 10000;
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
|
|
|
break;
|
2020-07-04 19:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_SWITCH:{
|
2020-07-04 19:02:43 +02:00
|
|
|
printf ("switch happened, from %d\n", event.origin);
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPC_EVENT_TYPE_EXTRA_SOCKET:
|
|
|
|
{
|
|
|
|
// NEW CLIENT
|
2020-07-04 14:21:02 +02:00
|
|
|
if (event.origin == serverfd) {
|
2020-01-01 12:11:34 +01:00
|
|
|
int sock_fd_client = accept_new_client (serverfd);
|
2020-07-04 19:02:43 +02:00
|
|
|
cpt++;
|
|
|
|
printf ("TCP connection: %d ctx connected\n", cpt);
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("new TCP client has the fd %d\n", sock_fd_client);
|
2019-07-27 15:48:10 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
// CLIENT IS TALKING
|
|
|
|
else {
|
2020-07-04 19:02:43 +02:00
|
|
|
// Test: if the socket already is in the switch, this means we can just switch the packet.
|
|
|
|
// Is the socket in the switch db?
|
2020-07-04 14:40:55 +02:00
|
|
|
tcp_connection (event.origin);
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPC_EVENT_TYPE_CONNECTION:
|
|
|
|
{
|
2020-07-04 19:02:43 +02:00
|
|
|
cpt++;
|
|
|
|
printf ("connection: %d ctx connected\n", cpt);
|
2020-07-04 14:21:02 +02:00
|
|
|
printf ("new client has the fd %d\n", event.origin);
|
2020-01-01 12:11:34 +01:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPC_EVENT_TYPE_DISCONNECTION:
|
|
|
|
{
|
2020-07-04 19:02:43 +02:00
|
|
|
cpt--;
|
|
|
|
printf ("disconnection: %d ctx remaining\n", cpt);
|
2020-01-01 12:11:34 +01:00
|
|
|
};
|
|
|
|
break;
|
2020-07-04 19:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_MESSAGE:
|
|
|
|
{
|
|
|
|
struct ipc_message *m = event.m;
|
|
|
|
if (m->length > 0) {
|
|
|
|
printf ("message received (type %d): %.*s\n", m->type, m->length, m->payload);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
2020-07-04 14:21:02 +02:00
|
|
|
// m->fd = 3;
|
2020-07-04 19:02:43 +02:00
|
|
|
TEST_IPC_P (ipc_write (ctx, m), "server write");
|
2020-01-01 12:11:34 +01:00
|
|
|
};
|
|
|
|
break;
|
2020-07-04 19:02:43 +02:00
|
|
|
|
2020-07-04 14:40:55 +02:00
|
|
|
case IPC_EVENT_TYPE_TX:
|
2020-07-04 14:21:02 +02:00
|
|
|
{
|
|
|
|
printf ("a message was sent\n");
|
|
|
|
}
|
|
|
|
break;
|
2020-07-04 19:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
case IPC_EVENT_TYPE_ERROR:
|
2020-07-04 14:40:55 +02:00
|
|
|
fprintf (stderr, "a problem happened with client %d\n", event.origin);
|
2020-01-01 12:11:34 +01:00
|
|
|
break;
|
2020-07-04 19:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
default:
|
2020-07-04 19:02:43 +02:00
|
|
|
fprintf (stderr, "there must be a problem, event not set: %d\n", event.type);
|
|
|
|
exit(1);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
2020-01-01 12:11:34 +01:00
|
|
|
}
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// should never go there
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
void exit_program (int signal)
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("Quitting, signal: %d\n", signal);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 14:40:55 +02:00
|
|
|
// Close then free remaining ctx.
|
2020-07-04 19:02:43 +02:00
|
|
|
ipc_close_all (ctx);
|
2020-07-04 19:29:59 +02:00
|
|
|
ipc_ctx_free (ctx);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2019-07-27 15:48:10 +02:00
|
|
|
// free, free everything!
|
2020-07-04 19:02:43 +02:00
|
|
|
free (ctx);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
exit (EXIT_SUCCESS);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-07-04 14:40:55 +02:00
|
|
|
* service ping-pong: send back everything sent by the ctx
|
2019-06-12 15:02:43 +02:00
|
|
|
* stop the program on SIG{TERM,INT,ALRM,USR{1,2},HUP} signals
|
|
|
|
*/
|
|
|
|
|
2020-07-04 14:40:55 +02:00
|
|
|
int main (int argc, char *argv[])
|
2019-06-12 15:02:43 +02:00
|
|
|
{
|
|
|
|
// check the number of args on command line
|
2020-01-01 12:11:34 +01:00
|
|
|
if (argc != 2) {
|
|
|
|
printf ("USAGE: %s port_num\n", argv[0]);
|
|
|
|
exit (EXIT_FAILURE);
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
printf ("pid = %d\n", getpid ());
|
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
ctx = malloc (sizeof (struct ipc_ctx));
|
|
|
|
memset(ctx, 0, sizeof (struct ipc_ctx));
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-07-04 19:02:43 +02:00
|
|
|
struct ipc_error ret = ipc_server_init (ctx, SERVICE_NAME);
|
2020-01-01 12:11:34 +01:00
|
|
|
if (ret.error_code != IPC_ERROR_NONE) {
|
|
|
|
fprintf (stderr, "%s\n", ret.error_message);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2020-07-04 19:02:43 +02:00
|
|
|
printf ("Listening on [%s].\n", ctx->cinfos[0].spath);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
printf ("MAIN: server created\n");
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
signal (SIGHUP , exit_program);
|
|
|
|
signal (SIGALRM , exit_program);
|
|
|
|
signal (SIGUSR1 , exit_program);
|
|
|
|
signal (SIGUSR2 , exit_program);
|
|
|
|
signal (SIGTERM , exit_program);
|
|
|
|
signal (SIGINT , exit_program);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
2020-01-01 12:11:34 +01:00
|
|
|
// the service will loop until the end of time, or a signal
|
2020-07-04 14:40:55 +02:00
|
|
|
main_loop (argc, argv);
|
2019-06-12 15:02:43 +02:00
|
|
|
|
|
|
|
// main_loop should not return
|
2020-01-01 12:11:34 +01:00
|
|
|
return EXIT_FAILURE;
|
2019-06-12 15:02:43 +02:00
|
|
|
}
|