2024-12-31 16:34:29 +00:00
|
|
|
// Written by retoor@molodetz.nl
|
|
|
|
|
|
|
|
// This source code sets up a simple TCP server that listens for connections and
|
|
|
|
// handles cleanup on exit. The server is intended to interact with an upstream
|
|
|
|
// server defined by its host and port.
|
|
|
|
|
|
|
|
// Imports: Custom includes 'py.h' and 'sock.h' for additional functionality.
|
|
|
|
|
|
|
|
// MIT License
|
|
|
|
|
2024-12-31 01:57:48 +00:00
|
|
|
#include "py.h"
|
2024-12-31 16:34:29 +00:00
|
|
|
#include "sock.h"
|
2024-12-31 05:44:19 +00:00
|
|
|
#include <Python.h>
|
2024-12-31 01:57:48 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2024-12-31 16:34:29 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN 1
|
2024-12-31 01:57:48 +00:00
|
|
|
#define LISTEN_PORT 2222
|
|
|
|
#define UPSTREAM_HOST "127.0.0.1"
|
|
|
|
#define UPSTREAM_PORT 9999
|
|
|
|
|
|
|
|
void cleanup() {
|
|
|
|
close(epoll_fd);
|
|
|
|
close(listen_fd);
|
|
|
|
py_destruct();
|
|
|
|
printf("Graceful exit.\n");
|
|
|
|
}
|
2024-12-31 16:34:29 +00:00
|
|
|
|
2024-12-31 01:57:48 +00:00
|
|
|
void handle_sigint(int sig) {
|
|
|
|
printf("\nCtrl+C pressed.\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
if (signal(SIGINT, handle_sigint) == SIG_ERR) {
|
|
|
|
perror("Failed to register signal handler");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
atexit(cleanup);
|
|
|
|
|
2024-12-31 16:34:29 +00:00
|
|
|
serve(LISTEN_PORT);
|
2024-12-31 01:57:48 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|