summaryrefslogtreecommitdiffstats
path: root/src/core.h
diff options
context:
space:
mode:
authortiptorrent development team <tiptorrent@soleta.eu>2021-08-17 00:05:31 +0200
committertiptorrent development team <tiptorrent@soleta.eu>2021-09-29 15:47:43 +0200
commit2610239d62d744294e55d44e46937bd6dea87559 (patch)
treea14c9da8d4156e31cd311b036287e7aa9c90a40f /src/core.h
initial commit
Diffstat (limited to 'src/core.h')
-rw-r--r--src/core.h83
1 files changed, 83 insertions, 0 deletions
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 <ev.h>
+#include "list.h"
+#include <stdbool.h>
+#include <netinet/in.h>
+
+#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