Seregon/zftpd

Zero-copy FTP/HTTP Daemon compatible with all POSIX systems

C/11.0 KB/No license
ffi/c_core/pal_ffi.c
zftpd / ffi / c_core / pal_ffi.c
1/*
2MIT License
3 
4Copyright (c) 2026 Seregon
5 
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12 
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15 
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24 
25#include "pal_ffi.h"
26 
27#include "../../include/event_loop.h"
28#include "../../include/ftp_server.h"
29#include "../../include/http_server.h"
30#include "../../include/pal_alloc.h"
31 
32#include <stdlib.h>
33#include <string.h>
34 
35/*===========================================================================*
36 * MEMORY ALLOCATOR
37 *===========================================================================*/
38 
39int pal_ffi_alloc_init_default(void) { return pal_alloc_init_default(); }
40 
41void *pal_ffi_malloc(size_t size) { return pal_malloc(size); }
42 
43void pal_ffi_free(void *ptr) { pal_free(ptr); }
44 
45void *pal_ffi_calloc(size_t nmemb, size_t size) {
46 return pal_calloc(nmemb, size);
47}
48 
49void *pal_ffi_realloc(void *ptr, size_t size) { return pal_realloc(ptr, size); }
50 
51void *pal_ffi_aligned_alloc(size_t alignment, size_t size) {
52 return pal_aligned_alloc(alignment, size);
53}
54 
55size_t pal_ffi_alloc_arena_free_approx(void) {
56 return pal_alloc_arena_free_approx();
57}
58 
59/*===========================================================================*
60 * EVENT LOOP
61 *===========================================================================*/
62 
63void *pal_ffi_event_loop_create(void) { return (void *)event_loop_create(); }
64 
65int pal_ffi_event_loop_run(void *loop) {
66 if (loop == NULL)
67 return PAL_FFI_ERR_INVALID_PARAM;
68 return event_loop_run((event_loop_t *)loop);
69}
70 
71void pal_ffi_event_loop_stop(void *loop) {
72 if (loop != NULL) {
73 event_loop_stop((event_loop_t *)loop);
74 }
75}
76 
77void pal_ffi_event_loop_destroy(void *loop) {
78 if (loop != NULL) {
79 event_loop_destroy((event_loop_t *)loop);
80 }
81}
82 
83/*===========================================================================*
84 * FTP SERVER
85 *===========================================================================*/
86 
87void *pal_ffi_ftp_server_create(const char *bind_ip, uint16_t port,
88 const char *root_path) {
89 if (bind_ip == NULL || root_path == NULL)
90 return NULL;
91 
92 ftp_server_context_t *ctx =
93 (ftp_server_context_t *)pal_malloc(sizeof(ftp_server_context_t));
94 if (ctx == NULL)
95 return NULL;
96 
97 ftp_error_t err = ftp_server_init(ctx, bind_ip, port, root_path);
98 if (err != FTP_OK) {
99 pal_free(ctx);
100 return NULL;
101 }
102 
103 return (void *)ctx;
104}
105 
106int pal_ffi_ftp_server_start(void *server) {
107 if (server == NULL)
108 return PAL_FFI_ERR_INVALID_PARAM;
109 return (int)ftp_server_start((ftp_server_context_t *)server);
110}
111 
112int pal_ffi_ftp_server_is_running(const void *server) {
113 if (server == NULL)
114 return 0;
115 return ftp_server_is_running((const ftp_server_context_t *)server);
116}
117 
118uint32_t pal_ffi_ftp_server_get_active_sessions(const void *server) {
119 if (server == NULL)
120 return 0;
121 return ftp_server_get_active_sessions((const ftp_server_context_t *)server);
122}
123 
124void pal_ffi_ftp_server_stop(void *server) {
125 if (server != NULL) {
126 ftp_server_stop((ftp_server_context_t *)server);
127 }
128}
129 
130void pal_ffi_ftp_server_destroy(void *server) {
131 if (server != NULL) {
132 ftp_server_cleanup((ftp_server_context_t *)server);
133 pal_free(server);
134 }
135}
136 
137/*===========================================================================*
138 * HTTP SERVER
139 *===========================================================================*/
140 
141#if defined(ENABLE_ZHTTPD) && (ENABLE_ZHTTPD == 1)
142void *pal_ffi_http_server_create(void *loop, uint16_t port) {
143 if (loop == NULL)
144 return NULL;
145 return (void *)http_server_create((event_loop_t *)loop, port);
146}
147 
148void pal_ffi_http_server_destroy(void *server) {
149 if (server != NULL) {
150 http_server_destroy((http_server_t *)server);
151 }
152}
153#else
154void *pal_ffi_http_server_create(void *loop, uint16_t port) {
155 (void)loop;
156 (void)port;
157 return NULL;
158}
159 
160void pal_ffi_http_server_destroy(void *server) { (void)server; }
161#endif
162