From 2610239d62d744294e55d44e46937bd6dea87559 Mon Sep 17 00:00:00 2001 From: tiptorrent development team Date: Tue, 17 Aug 2021 00:05:31 +0200 Subject: initial commit --- src/core.h | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/core.h (limited to 'src/core.h') diff --git a/src/core.h b/src/core.h new file mode 100644 index 0000000..524dd62 --- /dev/null +++ b/src/core.h @@ -0,0 +1,83 @@ +#ifndef _TIP_CORE_H +#define _TIP_CORE_H + +#include +#include "list.h" +#include +#include + +#define TIP_MSG_REQUEST_MAXLEN 131072 + +extern const char *root; +#define DEFAULT_MAX_CLIENTS 3 +extern int max_clients; +extern int num_clients; +extern bool redirect; +/* max_client logic only applies for files larger than 1024 bytes. */ +#define FILE_SIZE_THRESHOLD 1024 + +enum tip_client_state { + TIP_CLIENT_PENDING = 0, + TIP_CLIENT_RECEIVING_HEADER, + TIP_CLIENT_RECEIVING_PAYLOAD, + TIP_CLIENT_PROCESSING_REQUEST, + TIP_CLIENT_PROCESSING_REQUEST_2, + TIP_CLIENT_PROCESSING_REQUEST_3, +}; + +struct tip_client { + struct list_head list; + struct ev_io io; + struct ev_timer timer; + struct sockaddr_in addr; + enum tip_client_state state; + char buf[TIP_MSG_REQUEST_MAXLEN]; + unsigned int buf_len; + unsigned int msg_len; + int content_length; + char auth_token[64]; + + /* for file serving. */ + const char *uri; + const char *path; + size_t size; + int fd; + off_t offset; + + /* for redirection. */ + bool redirect; + struct sockaddr_in redirect_addr; + bool allow_redirect; +}; + +static inline int tip_client_socket(const struct tip_client *cli) +{ + return cli->io.fd; +} + +void tip_client_pending(struct tip_client *cli); +bool tip_client_redirect(struct tip_client *cli); + +extern struct ev_loop *tip_main_loop; + +int tip_socket_server_init(const char *port); +void tip_server_accept_cb(struct ev_loop *loop, struct ev_io *io, int events); + +int tip_client_state_process_payload(struct tip_client *cli); +int tip_client_state_process_payload_reply(struct tip_client *cli); +int tip_client_state_process_payload_bulk(struct tip_client *cli); + +enum tip_http_method { + TIP_METHOD_GET = 0, + TIP_METHOD_POST, + TIP_METHOD_NO_HTTP +}; + +struct tip_client_redirect { + struct list_head list; + struct sockaddr_in addr; + const char *uri; + struct ev_timer timer; +}; + +#endif -- cgit v1.2.3-18-g5258