Zero-copy FTP/HTTP Daemon compatible with all POSIX systems
| 1 | /* |
| 2 | MIT License |
| 3 | |
| 4 | Copyright (c) 2026 Seregon |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | of this software and associated documentation files (the "Software"), to deal |
| 8 | in the Software without restriction, including without limitation the rights |
| 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | copies of the Software, and to permit persons to whom the Software is |
| 11 | furnished to do so, subject to the following conditions: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all |
| 14 | copies or substantial portions of the Software. |
| 15 | |
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | SOFTWARE. |
| 23 | */ |
| 24 | |
| 25 | /** |
| 26 | * @file pal_ffi.h |
| 27 | * @brief C-Core Foreign Function Interface (FFI) for ZFTPD |
| 28 | * |
| 29 | * This header defines a stable, C ABI intended for consumption by |
| 30 | * high-level languages (Java, Rust, Python, Go, Zig). |
| 31 | * It uses opaque contexts (`void*`) and integer error codes to remain strictly |
| 32 | * decoupled from the internal data structures of zftpd. |
| 33 | */ |
| 34 | |
| 35 | #ifndef PAL_FFI_H |
| 36 | #define PAL_FFI_H |
| 37 | |
| 38 | #include <stddef.h> |
| 39 | #include <stdint.h> |
| 40 | |
| 41 | #ifdef __cplusplus |
| 42 | extern "C" { |
| 43 | #endif |
| 44 | |
| 45 | /* Visibility macros for shared library export */ |
| 46 | #if defined(_WIN32) || defined(__CYGWIN__) |
| 47 | #define PAL_FFI_EXPORT __declspec(dllexport) |
| 48 | #else |
| 49 | #define PAL_FFI_EXPORT __attribute__((visibility("default"))) |
| 50 | #endif |
| 51 | |
| 52 | /*===========================================================================* |
| 53 | * ERROR CODES |
| 54 | * Copied from ftp_types.h to keep FFI consumers independent |
| 55 | *===========================================================================*/ |
| 56 | #define PAL_FFI_OK 0 |
| 57 | #define PAL_FFI_ERR_INVALID_PARAM -1 |
| 58 | #define PAL_FFI_ERR_OUT_OF_MEMORY -2 |
| 59 | #define PAL_FFI_ERR_UNKNOWN -99 |
| 60 | |
| 61 | /*===========================================================================* |
| 62 | * MEMORY ALLOCATOR (pal_alloc.h) |
| 63 | *===========================================================================*/ |
| 64 | |
| 65 | /** |
| 66 | * @brief Initialize the global default allocator |
| 67 | * @return PAL_FFI_OK on success, negative error code on failure |
| 68 | */ |
| 69 | PAL_FFI_EXPORT int pal_ffi_alloc_init_default(void); |
| 70 | |
| 71 | /** |
| 72 | * @brief Allocate memory using the PAL allocator |
| 73 | * @param size Number of bytes to allocate |
| 74 | * @return Pointer to allocated memory or NULL if failed |
| 75 | */ |
| 76 | PAL_FFI_EXPORT void *pal_ffi_malloc(size_t size); |
| 77 | |
| 78 | /** |
| 79 | * @brief Free memory using the PAL allocator |
| 80 | * @param ptr Pointer to memory to free |
| 81 | */ |
| 82 | PAL_FFI_EXPORT void pal_ffi_free(void *ptr); |
| 83 | |
| 84 | /** |
| 85 | * @brief Allocate zero-initialized memory using the PAL allocator |
| 86 | * @param nmemb Number of elements |
| 87 | * @param size Size of each element |
| 88 | * @return Pointer to allocated memory or NULL if failed |
| 89 | */ |
| 90 | PAL_FFI_EXPORT void *pal_ffi_calloc(size_t nmemb, size_t size); |
| 91 | |
| 92 | /** |
| 93 | * @brief Reallocate memory using the PAL allocator |
| 94 | * @param ptr Pointer to existing memory |
| 95 | * @param size New size |
| 96 | * @return Pointer to new memory or NULL if failed |
| 97 | */ |
| 98 | PAL_FFI_EXPORT void *pal_ffi_realloc(void *ptr, size_t size); |
| 99 | |
| 100 | /** |
| 101 | * @brief Allocate aligned memory using the PAL allocator |
| 102 | * @param alignment Alignment boundary |
| 103 | * @param size Size of memory to allocate |
| 104 | * @return Pointer to allocated memory or NULL if failed |
| 105 | */ |
| 106 | PAL_FFI_EXPORT void *pal_ffi_aligned_alloc(size_t alignment, size_t size); |
| 107 | |
| 108 | /** |
| 109 | * @brief Get total free bytes in the arena (approximate) |
| 110 | * @return Free bytes |
| 111 | */ |
| 112 | PAL_FFI_EXPORT size_t pal_ffi_alloc_arena_free_approx(void); |
| 113 | |
| 114 | /*===========================================================================* |
| 115 | * EVENT LOOP (event_loop.h) |
| 116 | *===========================================================================*/ |
| 117 | |
| 118 | /** |
| 119 | * @brief Create an Event Loop context |
| 120 | * @return Opaque pointer to the event loop, or NULL on failure |
| 121 | */ |
| 122 | PAL_FFI_EXPORT void *pal_ffi_event_loop_create(void); |
| 123 | |
| 124 | /** |
| 125 | * @brief Run the Event Loop (blocking) |
| 126 | * @param loop Opaque pointer to the event loop |
| 127 | * @return 0 on clean exit, negative on error |
| 128 | */ |
| 129 | PAL_FFI_EXPORT int pal_ffi_event_loop_run(void *loop); |
| 130 | |
| 131 | /** |
| 132 | * @brief Stop the Event Loop |
| 133 | * @param loop Opaque pointer to the event loop |
| 134 | */ |
| 135 | PAL_FFI_EXPORT void pal_ffi_event_loop_stop(void *loop); |
| 136 | |
| 137 | /** |
| 138 | * @brief Destroy the Event Loop and free resources |
| 139 | * @param loop Opaque pointer to the event loop |
| 140 | */ |
| 141 | PAL_FFI_EXPORT void pal_ffi_event_loop_destroy(void *loop); |
| 142 | |
| 143 | /*===========================================================================* |
| 144 | * FTP SERVER (ftp_server.h) |
| 145 | *===========================================================================*/ |
| 146 | |
| 147 | /** |
| 148 | * @brief Create and initialize a new FTP server context |
| 149 | * @param bind_ip IP address to bind to ("0.0.0.0" for all) |
| 150 | * @param port Port number |
| 151 | * @param root_path Server root directory path |
| 152 | * @return Opaque pointer to the FTP server context, or NULL on failure |
| 153 | */ |
| 154 | PAL_FFI_EXPORT void *pal_ffi_ftp_server_create(const char *bind_ip, |
| 155 | uint16_t port, |
| 156 | const char *root_path); |
| 157 | |
| 158 | /** |
| 159 | * @brief Start the FTP server (begin listening) |
| 160 | * @param server Opaque pointer to the FTP server context |
| 161 | * @return PAL_FFI_OK on success, negative error code on failure |
| 162 | */ |
| 163 | PAL_FFI_EXPORT int pal_ffi_ftp_server_start(void *server); |
| 164 | |
| 165 | /** |
| 166 | * @brief Check if the FTP server is running |
| 167 | * @param server Opaque pointer to the FTP server context |
| 168 | * @return 1 if running, 0 if not |
| 169 | */ |
| 170 | PAL_FFI_EXPORT int pal_ffi_ftp_server_is_running(const void *server); |
| 171 | |
| 172 | /** |
| 173 | * @brief Get the number of active FTP sessions |
| 174 | * @param server Opaque pointer to the FTP server context |
| 175 | * @return Number of active sessions |
| 176 | */ |
| 177 | PAL_FFI_EXPORT uint32_t |
| 178 | pal_ffi_ftp_server_get_active_sessions(const void *server); |
| 179 | |
| 180 | /** |
| 181 | * @brief Stop the FTP server (graceful shutdown) |
| 182 | * @param server Opaque pointer to the FTP server context |
| 183 | */ |
| 184 | PAL_FFI_EXPORT void pal_ffi_ftp_server_stop(void *server); |
| 185 | |
| 186 | /** |
| 187 | * @brief Destroy the FTP server context and clean up resources |
| 188 | * @param server Opaque pointer to the FTP server context |
| 189 | */ |
| 190 | PAL_FFI_EXPORT void pal_ffi_ftp_server_destroy(void *server); |
| 191 | |
| 192 | /*===========================================================================* |
| 193 | * HTTP SERVER (http_server.h) |
| 194 | *===========================================================================*/ |
| 195 | |
| 196 | /** |
| 197 | * @brief Create and start an HTTP server on the given port |
| 198 | * @param loop Opaque pointer to the event loop |
| 199 | * @param port Port number |
| 200 | * @return Opaque pointer to the HTTP server context, or NULL on failure |
| 201 | */ |
| 202 | PAL_FFI_EXPORT void *pal_ffi_http_server_create(void *loop, uint16_t port); |
| 203 | |
| 204 | /** |
| 205 | * @brief Stop and destroy the HTTP server |
| 206 | * @param server Opaque pointer to the HTTP server context |
| 207 | */ |
| 208 | PAL_FFI_EXPORT void pal_ffi_http_server_destroy(void *server); |
| 209 | |
| 210 | #ifdef __cplusplus |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | #endif /* PAL_FFI_H */ |
| 215 |