| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_WEBSOCKET_JSONRPC_H |
| 4 | #define NETDATA_WEBSOCKET_JSONRPC_H |
| 5 | |
| 6 | #include "websocket-internal.h" |
| 7 | |
| 8 | // JSON-RPC 2.0 protocol constants |
| 9 | #define JSONRPC_VERSION "2.0" |
| 10 | |
| 11 | // JSON-RPC error codes as per specification |
| 12 | typedef enum { |
| 13 | // Official JSON-RPC 2.0 error codes |
| 14 | JSONRPC_ERROR_PARSE_ERROR = -32700, // Invalid JSON was received by the server |
| 15 | JSONRPC_ERROR_INVALID_REQUEST = -32600, // The JSON sent is not a valid Request object |
| 16 | JSONRPC_ERROR_METHOD_NOT_FOUND = -32601, // The method does not exist / is not available |
| 17 | JSONRPC_ERROR_INVALID_PARAMS = -32602, // Invalid method parameter(s) |
| 18 | JSONRPC_ERROR_INTERNAL_ERROR = -32603, // Internal JSON-RPC error |
| 19 | |
| 20 | // -32000 to -32099 are reserved for implementation-defined server-errors |
| 21 | JSONRPC_ERROR_SERVER_ERROR = -32000, // Generic server error |
| 22 | |
| 23 | // Netdata specific error codes (using reserved server-error range) |
| 24 | JSONRPC_ERROR_NETDATA_PERMISSION_DENIED = -32030, // Permission denied |
| 25 | JSONRPC_ERROR_NETDATA_NOT_SUPPORTED = -32031, // Feature not supported |
| 26 | JSONRPC_ERROR_NETDATA_RATE_LIMIT = -32032, // Rate limit exceeded |
| 27 | } JSONRPC_ERROR_CODE; |
| 28 | |
| 29 | // Method handler function type |
| 30 | typedef void (*jsonrpc_method_handler)(WS_CLIENT *wsc, struct json_object *request, uint64_t id); |
| 31 | |
| 32 | // Initialize WebSocket JSON-RPC protocol |
| 33 | void websocket_jsonrpc_initialize(void); |
| 34 | |
| 35 | // Process a WebSocket message as JSON-RPC |
| 36 | bool websocket_jsonrpc_process_message(WS_CLIENT *wsc, const char *message, size_t length); |
| 37 | |
| 38 | // WebSocket protocol handler callbacks for JSON-RPC |
| 39 | void jsonrpc_on_connect(struct websocket_server_client *wsc); |
| 40 | void jsonrpc_on_message_callback(struct websocket_server_client *wsc, const char *message, size_t length, WEBSOCKET_OPCODE opcode); |
| 41 | void jsonrpc_on_close(struct websocket_server_client *wsc, WEBSOCKET_CLOSE_CODE code, const char *reason); |
| 42 | void jsonrpc_on_disconnect(struct websocket_server_client *wsc); |
| 43 | |
| 44 | // Response functions |
| 45 | void websocket_jsonrpc_response_result(WS_CLIENT *wsc, struct json_object *result, uint64_t id); |
| 46 | void websocket_jsonrpc_response_error(WS_CLIENT *wsc, JSONRPC_ERROR_CODE code, const char *message, uint64_t id); |
| 47 | void websocket_jsonrpc_response_error_with_data(WS_CLIENT *wsc, JSONRPC_ERROR_CODE code, const char *message, |
| 48 | struct json_object *data, uint64_t id); |
| 49 | |
| 50 | // Helper functions |
| 51 | bool websocket_jsonrpc_validate_request(struct json_object *request); |
| 52 | |
| 53 | // Utility function to extract parameters from a request |
| 54 | struct json_object *websocket_jsonrpc_get_params(struct json_object *request); |
| 55 | |
| 56 | #endif // NETDATA_WEBSOCKET_JSONRPC_H |