kfsw-services/src/ftp/ftp_server.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | #include <zephyr/fs/fs.h> | ||
| 8 | #include <zephyr/kernel.h> | ||
| 9 | #include <zephyr/sys/atomic.h> | ||
| 10 | |||
| 11 | #include <kfsw/platform/storage.h> | ||
| 12 | #include <kfsw/services/ftp.h> | ||
| 13 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FTP | ||
| 14 | #include <kfsw/services/log.h> | ||
| 15 | |||
| 16 | #include "ftp_link.h" | ||
| 17 | |||
| 18 | #define KFSW_FTP_SERVER_POLL_MS 100U | ||
| 19 | |||
| 20 | static bool initialized; | ||
| 21 | static bool threads_started; | ||
| 22 | static struct kfsw_ftp_listener server_listener; | ||
| 23 | static atomic_t server_started; | ||
| 24 | static atomic_t server_busy; | ||
| 25 | static struct kfsw_ftp_workspace server_workspace; | ||
| 26 | |||
| 27 | K_MSGQ_DEFINE(kfsw_ftp_connection_queue, sizeof(struct kfsw_ftp_link), 1, sizeof(void *)); | ||
| 28 | |||
| 29 | 16 | static uint8_t response_opcode(uint8_t request_opcode) | |
| 30 | { | ||
| 31 | 16 | switch (request_opcode) { | |
| 32 | 4 | case KFSW_FTP_OP_MKDIR_REQUEST: | |
| 33 | 4 | return KFSW_FTP_OP_MKDIR_RESPONSE; | |
| 34 | 8 | case KFSW_FTP_OP_REMOVE_REQUEST: | |
| 35 | 8 | return KFSW_FTP_OP_REMOVE_RESPONSE; | |
| 36 | ✗ | case KFSW_FTP_OP_LIST_REQUEST: | |
| 37 | ✗ | return KFSW_FTP_OP_LIST_END; | |
| 38 | ✗ | case KFSW_FTP_OP_STAT_REQUEST: | |
| 39 | ✗ | return KFSW_FTP_OP_STAT_RESPONSE; | |
| 40 | 2 | case KFSW_FTP_OP_PUT_REQUEST: | |
| 41 | 2 | return KFSW_FTP_OP_PUT_RESULT; | |
| 42 | ✗ | case KFSW_FTP_OP_GET_REQUEST: | |
| 43 | ✗ | return KFSW_FTP_OP_GET_RESULT; | |
| 44 | 2 | default: | |
| 45 | 2 | return KFSW_FTP_OP_GET_RESULT; | |
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | 50 | static uint8_t wire_status(int result) | |
| 50 | { | ||
| 51 | return (result == -EBADMSG) ? KFSW_FTP_STATUS_INVALID_REQUEST | ||
| 52 | 50 | : kfsw_ftp_errno_to_wire_status(result); | |
| 53 | } | ||
| 54 | |||
| 55 | 64 | static int send_status(struct kfsw_ftp_link *link, uint8_t opcode, uint32_t request_id, | |
| 56 | uint8_t status, uint32_t total_size, uint32_t crc32) | ||
| 57 | { | ||
| 58 | 64 | const struct kfsw_ftp_message response = { | |
| 59 | .opcode = opcode, | ||
| 60 | .status = status, | ||
| 61 | .request_id = request_id, | ||
| 62 | .total_size = total_size, | ||
| 63 | .crc32 = crc32, | ||
| 64 | }; | ||
| 65 | |||
| 66 | 64 | return kfsw_ftp_link_send(link, &response); | |
| 67 | } | ||
| 68 | |||
| 69 | /* | ||
| 70 | * PUT_READY and GET_INFO differ from a plain status by carrying where the | ||
| 71 | * transfer starts: what the receiving side already holds, as agreed by the | ||
| 72 | * side that holds it. | ||
| 73 | */ | ||
| 74 | 10 | static int send_ready(struct kfsw_ftp_link *link, uint8_t opcode, uint32_t request_id, | |
| 75 | uint32_t offset, uint32_t total_size, uint32_t crc32) | ||
| 76 | { | ||
| 77 | 10 | const struct kfsw_ftp_message response = { | |
| 78 | .opcode = opcode, | ||
| 79 | .status = KFSW_FTP_STATUS_OK, | ||
| 80 | .request_id = request_id, | ||
| 81 | .offset = offset, | ||
| 82 | .total_size = total_size, | ||
| 83 | .crc32 = crc32, | ||
| 84 | }; | ||
| 85 | |||
| 86 | 10 | return kfsw_ftp_link_send(link, &response); | |
| 87 | } | ||
| 88 | |||
| 89 | /** A path-only request carries a path and leaves every payload field zero. */ | ||
| 90 | 42 | static bool path_request_is_valid(const struct kfsw_ftp_message *request) | |
| 91 | { | ||
| 92 | 42 | return (request->data_size == 0U) && (request->status == 0U) && (request->flags == 0U) && | |
| 93 | 84 | (request->offset == 0U) && (request->total_size == 0U) && (request->crc32 == 0U); | |
| 94 | } | ||
| 95 | |||
| 96 | 42 | static int copy_request_path(const struct kfsw_ftp_message *request, char *virtual_path, | |
| 97 | size_t virtual_path_size) | ||
| 98 | { | ||
| 99 | 42 | if (!path_request_is_valid(request)) { | |
| 100 | 4 | return -EBADMSG; | |
| 101 | } | ||
| 102 | 38 | return kfsw_ftp_copy_message_path(request, virtual_path, virtual_path_size); | |
| 103 | } | ||
| 104 | |||
| 105 | 12 | static int resolve_message_path(const struct kfsw_ftp_message *request, bool allow_root) | |
| 106 | { | ||
| 107 | char virtual_path[KFSW_FTP_MAX_PATH_SIZE + 1U]; | ||
| 108 | int result; | ||
| 109 | |||
| 110 | 12 | result = kfsw_ftp_copy_message_path(request, virtual_path, sizeof(virtual_path)); | |
| 111 | 12 | if (result != 0) { | |
| 112 | ✗ | return result; | |
| 113 | } | ||
| 114 | 12 | return kfsw_ftp_resolve_path(virtual_path, allow_root, server_workspace.path, | |
| 115 | sizeof(server_workspace.path)); | ||
| 116 | } | ||
| 117 | |||
| 118 | 4 | static int serve_mkdir(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *request) | |
| 119 | { | ||
| 120 | char virtual_path[KFSW_FTP_MAX_PATH_SIZE + 1U]; | ||
| 121 | 4 | int result = copy_request_path(request, virtual_path, sizeof(virtual_path)); | |
| 122 | |||
| 123 | 4 | if (result == 0) { | |
| 124 | 4 | result = kfsw_ftp_local_mkdir(virtual_path, &server_workspace); | |
| 125 | } | ||
| 126 | 4 | return send_status(link, KFSW_FTP_OP_MKDIR_RESPONSE, request->request_id, | |
| 127 | 4 | wire_status(result), 0U, 0U); | |
| 128 | } | ||
| 129 | |||
| 130 | 22 | static int serve_remove(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *request) | |
| 131 | { | ||
| 132 | char virtual_path[KFSW_FTP_MAX_PATH_SIZE + 1U]; | ||
| 133 | 22 | int result = copy_request_path(request, virtual_path, sizeof(virtual_path)); | |
| 134 | |||
| 135 | 22 | if (result == 0) { | |
| 136 | 14 | result = kfsw_ftp_local_remove(virtual_path, &server_workspace); | |
| 137 | } | ||
| 138 | 22 | return send_status(link, KFSW_FTP_OP_REMOVE_RESPONSE, request->request_id, | |
| 139 | 22 | wire_status(result), 0U, 0U); | |
| 140 | } | ||
| 141 | |||
| 142 | 12 | static int serve_stat(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *request) | |
| 143 | { | ||
| 144 | char virtual_path[KFSW_FTP_MAX_PATH_SIZE + 1U]; | ||
| 145 | 12 | struct kfsw_ftp_stat info = {0}; | |
| 146 | 12 | int result = copy_request_path(request, virtual_path, sizeof(virtual_path)); | |
| 147 | |||
| 148 | 12 | if (result == 0) { | |
| 149 | 10 | result = kfsw_ftp_local_stat(virtual_path, &server_workspace, &info); | |
| 150 | } | ||
| 151 | |||
| 152 | 12 | const struct kfsw_ftp_message response = { | |
| 153 | .opcode = KFSW_FTP_OP_STAT_RESPONSE, | ||
| 154 | 6 | .flags = (result == 0) ? (uint8_t)info.type : 0U, | |
| 155 | 12 | .status = wire_status(result), | |
| 156 | 12 | .request_id = request->request_id, | |
| 157 | 12 | .total_size = info.size, | |
| 158 | 12 | .crc32 = info.crc32, | |
| 159 | }; | ||
| 160 | 12 | return kfsw_ftp_link_send(link, &response); | |
| 161 | } | ||
| 162 | |||
| 163 | struct list_entry_sender { | ||
| 164 | struct kfsw_ftp_link *link; | ||
| 165 | uint32_t request_id; | ||
| 166 | int result; | ||
| 167 | }; | ||
| 168 | |||
| 169 | 14 | static bool send_list_entry(const struct kfsw_ftp_entry *entry, void *context) | |
| 170 | { | ||
| 171 | 14 | struct list_entry_sender *sender = context; | |
| 172 | struct kfsw_ftp_message response; | ||
| 173 | |||
| 174 | 14 | memset(&response, 0, sizeof(response)); | |
| 175 | 14 | response.opcode = KFSW_FTP_OP_LIST_ENTRY; | |
| 176 | 14 | response.flags = (uint8_t)entry->type; | |
| 177 | 14 | response.request_id = sender->request_id; | |
| 178 | 14 | response.total_size = entry->size; | |
| 179 | 14 | response.path = (const uint8_t *)entry->name; | |
| 180 | 14 | response.path_size = (uint16_t)strnlen(entry->name, KFSW_FTP_MAX_PATH_SIZE); | |
| 181 | 14 | sender->result = kfsw_ftp_link_send(sender->link, &response); | |
| 182 | 14 | return sender->result == 0; | |
| 183 | } | ||
| 184 | |||
| 185 | 4 | static int serve_list(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *request) | |
| 186 | { | ||
| 187 | 4 | struct list_entry_sender sender = { | |
| 188 | .link = link, | ||
| 189 | 4 | .request_id = request->request_id, | |
| 190 | }; | ||
| 191 | char virtual_path[KFSW_FTP_MAX_PATH_SIZE + 1U]; | ||
| 192 | 4 | int result = copy_request_path(request, virtual_path, sizeof(virtual_path)); | |
| 193 | |||
| 194 | 4 | if (result == 0) { | |
| 195 | 4 | result = kfsw_ftp_local_list(virtual_path, &server_workspace, send_list_entry, | |
| 196 | &sender); | ||
| 197 | } | ||
| 198 | 4 | if (sender.result != 0) { | |
| 199 | /* A failed entry send stops the walk; report it over any close error. */ | ||
| 200 | ✗ | result = sender.result; | |
| 201 | } | ||
| 202 | 4 | return send_status(link, KFSW_FTP_OP_LIST_END, request->request_id, wire_status(result), 0U, | |
| 203 | 0U); | ||
| 204 | } | ||
| 205 | |||
| 206 | 4 | static int prepare_upload_target(struct kfsw_ftp_transfer *transfer, bool may_resume) | |
| 207 | { | ||
| 208 | struct fs_dirent entry; | ||
| 209 | int result; | ||
| 210 | |||
| 211 | result = | ||
| 212 | 4 | kfsw_ftp_make_temporary_path(server_workspace.path, server_workspace.temporary_path, | |
| 213 | sizeof(server_workspace.temporary_path)); | ||
| 214 | 4 | if (result != 0) { | |
| 215 | ✗ | return result; | |
| 216 | } | ||
| 217 | 4 | result = fs_stat(server_workspace.path, &entry); | |
| 218 | 4 | if ((result == 0) && (entry.type == FS_DIR_ENTRY_DIR)) { | |
| 219 | ✗ | return -EISDIR; | |
| 220 | } | ||
| 221 | 4 | if ((result != 0) && (result != -ENOENT)) { | |
| 222 | ✗ | return result; | |
| 223 | } | ||
| 224 | |||
| 225 | 4 | if (may_resume) { | |
| 226 | 2 | result = kfsw_ftp_partial_resume_point(server_workspace.path, &server_workspace, | |
| 227 | transfer->total_size, transfer->crc32, | ||
| 228 | &transfer->offset, &transfer->actual_crc32); | ||
| 229 | 2 | if (result != 0) { | |
| 230 | ✗ | return result; | |
| 231 | } | ||
| 232 | } | ||
| 233 | /* Refused before the note, so a refused upload leaves nothing behind. */ | ||
| 234 | 4 | result = kfsw_ftp_check_space(server_workspace.path, | |
| 235 | 4 | transfer->total_size - transfer->offset); | |
| 236 | 4 | if (result != 0) { | |
| 237 | ✗ | return result; | |
| 238 | } | ||
| 239 | 4 | if (transfer->offset == 0U) { | |
| 240 | /* A fresh partial needs its note before any data can be kept. */ | ||
| 241 | 2 | return kfsw_ftp_partial_note_write(server_workspace.path, transfer->total_size, | |
| 242 | transfer->crc32); | ||
| 243 | } | ||
| 244 | 2 | kfsw_log_info("FTP: continuing %s at %u of %u bytes", server_workspace.path, | |
| 245 | transfer->offset, transfer->total_size); | ||
| 246 | 2 | return 0; | |
| 247 | } | ||
| 248 | |||
| 249 | 6 | static int serve_put(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *request) | |
| 250 | { | ||
| 251 | 6 | struct kfsw_ftp_transfer transfer = { | |
| 252 | .link = link, | ||
| 253 | .workspace = &server_workspace, | ||
| 254 | 6 | .request_id = request->request_id, | |
| 255 | 6 | .total_size = request->total_size, | |
| 256 | 6 | .crc32 = request->crc32, | |
| 257 | .data_opcode = KFSW_FTP_OP_PUT_DATA, | ||
| 258 | }; | ||
| 259 | int result; | ||
| 260 | |||
| 261 | 6 | if ((request->data_size != 0U) || (request->status != 0U) || | |
| 262 | 6 | ((request->flags & (uint8_t)~KFSW_FTP_FLAG_RESUME) != 0U) || (request->offset != 0U)) { | |
| 263 | 2 | result = -EBADMSG; | |
| 264 | } else { | ||
| 265 | 4 | result = resolve_message_path(request, false); | |
| 266 | } | ||
| 267 | #if CONFIG_KFSW_FWU_STANDARD | ||
| 268 | /* A put to the reserved path is a firmware upload. */ | ||
| 269 | ✗ | if ((result == 0) && kfsw_ftp_path_is_firmware(request->path, request->path_size)) { | |
| 270 | ✗ | result = kfsw_ftp_transfer_open_firmware_sink(&transfer); | |
| 271 | ✗ | if (result != 0) { | |
| 272 | ✗ | return send_status(link, KFSW_FTP_OP_PUT_RESULT, request->request_id, | |
| 273 | ✗ | wire_status(result), 0U, 0U); | |
| 274 | } | ||
| 275 | } | ||
| 276 | ✗ | if ((result == 0) && !transfer.firmware) { | |
| 277 | #else | ||
| 278 | 6 | if (result == 0) { | |
| 279 | #endif | ||
| 280 | 4 | result = prepare_upload_target(&transfer, | |
| 281 | 4 | (request->flags & KFSW_FTP_FLAG_RESUME) != 0U); | |
| 282 | 4 | if (result == 0) { | |
| 283 | 4 | result = kfsw_ftp_transfer_open_sink(&transfer, | |
| 284 | server_workspace.temporary_path); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | 6 | if (result != 0) { | |
| 288 | 2 | return send_status(link, KFSW_FTP_OP_PUT_RESULT, request->request_id, | |
| 289 | 2 | wire_status(result), 0U, 0U); | |
| 290 | } | ||
| 291 | |||
| 292 | /* The temporary file exists from here on, so the client may start sending. | ||
| 293 | * The offset tells the client where to pick up; zero means from the start. | ||
| 294 | */ | ||
| 295 | 4 | result = send_ready(link, KFSW_FTP_OP_PUT_READY, request->request_id, transfer.offset, | |
| 296 | 4 | request->total_size, request->crc32); | |
| 297 | 4 | if (result == 0) { | |
| 298 | 4 | result = kfsw_ftp_transfer_receive(&transfer); | |
| 299 | } | ||
| 300 | 4 | result = kfsw_ftp_transfer_finish(&transfer, server_workspace.path, | |
| 301 | server_workspace.temporary_path, result); | ||
| 302 | 4 | return send_status(link, KFSW_FTP_OP_PUT_RESULT, request->request_id, wire_status(result), | |
| 303 | transfer.offset, transfer.actual_crc32); | ||
| 304 | } | ||
| 305 | |||
| 306 | /* | ||
| 307 | * What a download may skip. The client states how much it already holds and | ||
| 308 | * which file that was, and the claim is only good when it describes the file | ||
| 309 | * this node holds now: a file rewritten between two passes is sent whole | ||
| 310 | * rather than stitched together from two versions of itself. | ||
| 311 | */ | ||
| 312 | 6 | static uint32_t accepted_resume_offset(const struct kfsw_ftp_message *request, | |
| 313 | const struct kfsw_ftp_transfer *transfer) | ||
| 314 | { | ||
| 315 | 6 | if (((request->flags & KFSW_FTP_FLAG_RESUME) == 0U) || (request->offset == 0U)) { | |
| 316 | 2 | return 0U; | |
| 317 | } | ||
| 318 | 4 | if ((request->total_size != transfer->total_size) || (request->crc32 != transfer->crc32)) { | |
| 319 | 2 | return 0U; | |
| 320 | } | ||
| 321 | /* A claim to the whole file leaves nothing to send, and a claim past its | ||
| 322 | * end is nonsense. Either way the file goes out from the start. | ||
| 323 | */ | ||
| 324 | 2 | if (request->offset >= transfer->total_size) { | |
| 325 | ✗ | return 0U; | |
| 326 | } | ||
| 327 | 2 | return request->offset; | |
| 328 | } | ||
| 329 | |||
| 330 | 8 | static int serve_get(struct kfsw_ftp_link *link, const struct kfsw_ftp_message *request) | |
| 331 | { | ||
| 332 | 8 | struct kfsw_ftp_transfer transfer = { | |
| 333 | .link = link, | ||
| 334 | .workspace = &server_workspace, | ||
| 335 | 8 | .request_id = request->request_id, | |
| 336 | .data_opcode = KFSW_FTP_OP_GET_DATA, | ||
| 337 | }; | ||
| 338 | struct fs_dirent entry; | ||
| 339 | int result; | ||
| 340 | |||
| 341 | 8 | if ((request->data_size != 0U) || (request->status != 0U) || | |
| 342 | 8 | ((request->flags & (uint8_t)~KFSW_FTP_FLAG_RESUME) != 0U)) { | |
| 343 | ✗ | result = -EBADMSG; | |
| 344 | } else { | ||
| 345 | 8 | result = resolve_message_path(request, false); | |
| 346 | } | ||
| 347 | 8 | if (result == 0) { | |
| 348 | 8 | result = fs_stat(server_workspace.path, &entry); | |
| 349 | } | ||
| 350 | 8 | if ((result == 0) && (entry.type != FS_DIR_ENTRY_FILE)) { | |
| 351 | ✗ | result = -EISDIR; | |
| 352 | } | ||
| 353 | 8 | if (result == 0) { | |
| 354 | 6 | result = kfsw_ftp_file_crc(server_workspace.path, &server_workspace, | |
| 355 | &transfer.total_size, &transfer.crc32); | ||
| 356 | } | ||
| 357 | 8 | if (result == 0) { | |
| 358 | /* Set before the open, which positions the file at it. */ | ||
| 359 | 6 | transfer.offset = accepted_resume_offset(request, &transfer); | |
| 360 | 6 | result = kfsw_ftp_transfer_open_source(&transfer, server_workspace.path); | |
| 361 | } | ||
| 362 | 8 | if (result != 0) { | |
| 363 | 2 | return send_status(link, KFSW_FTP_OP_GET_RESULT, request->request_id, | |
| 364 | 2 | wire_status(result), 0U, 0U); | |
| 365 | } | ||
| 366 | 6 | if (transfer.offset != 0U) { | |
| 367 | 2 | kfsw_log_info("FTP: sending %s from %u of %u bytes", server_workspace.path, | |
| 368 | transfer.offset, transfer.total_size); | ||
| 369 | } | ||
| 370 | |||
| 371 | /* The offset names what this node is not going to send again; zero means | ||
| 372 | * the claim was refused and the file comes whole. | ||
| 373 | */ | ||
| 374 | 6 | result = send_ready(link, KFSW_FTP_OP_GET_INFO, request->request_id, transfer.offset, | |
| 375 | transfer.total_size, transfer.crc32); | ||
| 376 | 6 | if (result == 0) { | |
| 377 | 6 | result = kfsw_ftp_transfer_send(&transfer); | |
| 378 | } else { | ||
| 379 | ✗ | (void)fs_close(&transfer.file); | |
| 380 | } | ||
| 381 | 6 | return send_status(link, KFSW_FTP_OP_GET_RESULT, request->request_id, | |
| 382 | 6 | kfsw_ftp_errno_to_wire_status(result), transfer.offset, transfer.crc32); | |
| 383 | } | ||
| 384 | |||
| 385 | 76 | void kfsw_ftp_serve_connection(struct kfsw_ftp_link *link) | |
| 386 | { | ||
| 387 | struct kfsw_ftp_link_frame frame; | ||
| 388 | 76 | int result = kfsw_ftp_link_receive(link, &frame); | |
| 389 | |||
| 390 | 76 | if (result != 0) { | |
| 391 | 6 | (void)send_status(link, KFSW_FTP_OP_GET_RESULT, 0U, | |
| 392 | 2 | ((result == -EPROTONOSUPPORT) || (result == -ENOTSUP)) | |
| 393 | ? KFSW_FTP_STATUS_UNSUPPORTED | ||
| 394 | : KFSW_FTP_STATUS_INVALID_REQUEST, | ||
| 395 | 0U, 0U); | ||
| 396 | 18 | return; | |
| 397 | } | ||
| 398 | /* The read-only root is checked before anything opens a file. */ | ||
| 399 | 72 | if ((frame.message.opcode == KFSW_FTP_OP_MKDIR_REQUEST) || | |
| 400 | 64 | (frame.message.opcode == KFSW_FTP_OP_REMOVE_REQUEST) || | |
| 401 | 34 | (frame.message.opcode == KFSW_FTP_OP_PUT_REQUEST)) { | |
| 402 | char path[KFSW_FTP_MAX_PATH_SIZE + 1U]; | ||
| 403 | |||
| 404 | 88 | if ((kfsw_ftp_copy_message_path(&frame.message, path, sizeof(path)) == 0) && | |
| 405 | 42 | kfsw_ftp_path_is_read_only(path)) { | |
| 406 | 14 | (void)send_status(link, response_opcode(frame.message.opcode), | |
| 407 | frame.message.request_id, | ||
| 408 | 14 | kfsw_ftp_errno_to_wire_status(-EROFS), 0U, 0U); | |
| 409 | 14 | kfsw_ftp_link_release(&frame); | |
| 410 | 14 | return; | |
| 411 | } | ||
| 412 | } | ||
| 413 | |||
| 414 | 58 | switch (frame.message.opcode) { | |
| 415 | 4 | case KFSW_FTP_OP_MKDIR_REQUEST: | |
| 416 | 4 | (void)serve_mkdir(link, &frame.message); | |
| 417 | 4 | break; | |
| 418 | 22 | case KFSW_FTP_OP_REMOVE_REQUEST: | |
| 419 | 22 | (void)serve_remove(link, &frame.message); | |
| 420 | 22 | break; | |
| 421 | 4 | case KFSW_FTP_OP_LIST_REQUEST: | |
| 422 | 4 | (void)serve_list(link, &frame.message); | |
| 423 | 4 | break; | |
| 424 | 12 | case KFSW_FTP_OP_STAT_REQUEST: | |
| 425 | 12 | (void)serve_stat(link, &frame.message); | |
| 426 | 12 | break; | |
| 427 | 6 | case KFSW_FTP_OP_PUT_REQUEST: | |
| 428 | 6 | (void)serve_put(link, &frame.message); | |
| 429 | 6 | break; | |
| 430 | 8 | case KFSW_FTP_OP_GET_REQUEST: | |
| 431 | 8 | (void)serve_get(link, &frame.message); | |
| 432 | 8 | break; | |
| 433 | 2 | default: | |
| 434 | 2 | (void)send_status(link, response_opcode(frame.message.opcode), | |
| 435 | frame.message.request_id, KFSW_FTP_STATUS_INVALID_REQUEST, 0U, | ||
| 436 | 0U); | ||
| 437 | 2 | break; | |
| 438 | } | ||
| 439 | 58 | kfsw_ftp_link_release(&frame); | |
| 440 | } | ||
| 441 | |||
| 442 | ✗ | static void ftp_worker(void *arg1, void *arg2, void *arg3) | |
| 443 | { | ||
| 444 | ARG_UNUSED(arg1); | ||
| 445 | ARG_UNUSED(arg2); | ||
| 446 | ARG_UNUSED(arg3); | ||
| 447 | |||
| 448 | ✗ | for (;;) { | |
| 449 | struct kfsw_ftp_link link; | ||
| 450 | |||
| 451 | ✗ | if (k_msgq_get(&kfsw_ftp_connection_queue, &link, | |
| 452 | ✗ | K_MSEC(KFSW_FTP_SERVER_POLL_MS)) != 0) { | |
| 453 | ✗ | continue; | |
| 454 | } | ||
| 455 | ✗ | if (kfsw_ftp_link_is_open(&link)) { | |
| 456 | ✗ | kfsw_ftp_serve_connection(&link); | |
| 457 | ✗ | kfsw_ftp_link_close(&link); | |
| 458 | } | ||
| 459 | ✗ | atomic_clear(&server_busy); | |
| 460 | } | ||
| 461 | } | ||
| 462 | |||
| 463 | ✗ | static void reject_busy_connection(struct kfsw_ftp_link *link) | |
| 464 | { | ||
| 465 | struct kfsw_ftp_link_frame frame; | ||
| 466 | |||
| 467 | ✗ | if (kfsw_ftp_link_receive(link, &frame) == 0) { | |
| 468 | ✗ | (void)send_status(link, response_opcode(frame.message.opcode), | |
| 469 | frame.message.request_id, KFSW_FTP_STATUS_BUSY, 0U, 0U); | ||
| 470 | ✗ | kfsw_ftp_link_release(&frame); | |
| 471 | } | ||
| 472 | ✗ | kfsw_ftp_link_close(link); | |
| 473 | ✗ | } | |
| 474 | |||
| 475 | ✗ | static void ftp_acceptor(void *arg1, void *arg2, void *arg3) | |
| 476 | { | ||
| 477 | ARG_UNUSED(arg1); | ||
| 478 | ARG_UNUSED(arg2); | ||
| 479 | ARG_UNUSED(arg3); | ||
| 480 | |||
| 481 | ✗ | for (;;) { | |
| 482 | struct kfsw_ftp_link link; | ||
| 483 | |||
| 484 | ✗ | if (atomic_get(&server_started) == 0) { | |
| 485 | ✗ | k_sleep(K_MSEC(KFSW_FTP_SERVER_POLL_MS)); | |
| 486 | ✗ | continue; | |
| 487 | } | ||
| 488 | ✗ | if (kfsw_ftp_link_accept(&server_listener, &link, KFSW_FTP_SERVER_POLL_MS) != 0) { | |
| 489 | ✗ | continue; | |
| 490 | } | ||
| 491 | ✗ | if (!atomic_cas(&server_busy, 0, 1)) { | |
| 492 | ✗ | reject_busy_connection(&link); | |
| 493 | ✗ | continue; | |
| 494 | } | ||
| 495 | /* A successful put transfers the connection to the worker. */ | ||
| 496 | ✗ | if (k_msgq_put(&kfsw_ftp_connection_queue, &link, K_NO_WAIT) != 0) { | |
| 497 | ✗ | atomic_clear(&server_busy); | |
| 498 | ✗ | reject_busy_connection(&link); | |
| 499 | } | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | K_THREAD_DEFINE(kfsw_ftp_worker_thread, CONFIG_KFSW_FTP_WORKER_STACK_SIZE, ftp_worker, NULL, NULL, | ||
| 504 | NULL, CONFIG_KFSW_FTP_WORKER_PRIORITY, 0, SYS_FOREVER_MS); | ||
| 505 | K_THREAD_DEFINE(kfsw_ftp_acceptor_thread, CONFIG_KFSW_FTP_ACCEPTOR_STACK_SIZE, ftp_acceptor, NULL, | ||
| 506 | NULL, NULL, CONFIG_KFSW_FTP_ACCEPTOR_PRIORITY, 0, SYS_FOREVER_MS); | ||
| 507 | |||
| 508 | 12 | bool kfsw_ftp_server_is_busy(void) | |
| 509 | { | ||
| 510 | 12 | return atomic_get(&server_busy) != 0; | |
| 511 | } | ||
| 512 | |||
| 513 | 18 | int kfsw_ftp_init(void) | |
| 514 | { | ||
| 515 | int result; | ||
| 516 | |||
| 517 | 18 | if (initialized) { | |
| 518 | 4 | return 0; | |
| 519 | } | ||
| 520 | 14 | if (!kfsw_storage_is_ready()) { | |
| 521 | ✗ | return -EACCES; | |
| 522 | } | ||
| 523 | 14 | result = fs_mkdir(KFSW_FTP_ROOT_PATH); | |
| 524 | 14 | if ((result != 0) && (result != -EEXIST)) { | |
| 525 | ✗ | return result; | |
| 526 | } | ||
| 527 | 14 | result = fs_mkdir(KFSW_FTP_EXCHANGE_PATH); | |
| 528 | 14 | if ((result != 0) && (result != -EEXIST)) { | |
| 529 | ✗ | return result; | |
| 530 | } | ||
| 531 | 14 | initialized = true; | |
| 532 | 14 | return 0; | |
| 533 | } | ||
| 534 | |||
| 535 | 4 | int kfsw_ftp_start(void) | |
| 536 | { | ||
| 537 | int result; | ||
| 538 | |||
| 539 | 4 | if (!initialized) { | |
| 540 | ✗ | return -EACCES; | |
| 541 | } | ||
| 542 | 4 | if (atomic_get(&server_started) != 0) { | |
| 543 | ✗ | return 0; | |
| 544 | } | ||
| 545 | 4 | if (!kfsw_ftp_link_is_ready()) { | |
| 546 | ✗ | return -EACCES; | |
| 547 | } | ||
| 548 | 4 | result = kfsw_ftp_link_listen(&server_listener); | |
| 549 | 4 | if (result != 0) { | |
| 550 | ✗ | return result; | |
| 551 | } | ||
| 552 | 4 | atomic_set(&server_started, 1); | |
| 553 | 4 | if (!threads_started) { | |
| 554 | 4 | k_thread_start(kfsw_ftp_worker_thread); | |
| 555 | 4 | k_thread_start(kfsw_ftp_acceptor_thread); | |
| 556 | 4 | threads_started = true; | |
| 557 | } | ||
| 558 | 4 | return 0; | |
| 559 | } | ||
| 560 | |||
| 561 | 134 | bool kfsw_ftp_is_started(void) | |
| 562 | { | ||
| 563 | 134 | return initialized && (atomic_get(&server_started) != 0); | |
| 564 | } | ||
| 565 |