helper: types: rework h_u32_to_le() and similar for sparse
[openocd.git] / src / server / server.h
blobc9d4698af8094e80f5cb2ac8f728ccb1527bfe1b
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 * *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
9 * *
10 * Copyright (C) 2008 by Spencer Oliver *
11 * spen@spen-soft.co.uk *
12 ***************************************************************************/
14 #ifndef OPENOCD_SERVER_SERVER_H
15 #define OPENOCD_SERVER_SERVER_H
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
21 #include <helper/log.h>
22 #include <helper/replacements.h>
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
26 #endif
28 enum connection_type {
29 CONNECTION_TCP,
30 CONNECTION_PIPE,
31 CONNECTION_STDINOUT
34 #define CONNECTION_LIMIT_UNLIMITED (-1)
36 struct connection {
37 int fd;
38 int fd_out; /* When using pipes we're writing to a different fd */
39 struct sockaddr_in sin;
40 struct command_context *cmd_ctx;
41 struct service *service;
42 bool input_pending;
43 void *priv;
44 struct connection *next;
47 struct service_driver {
48 /** the name of the server */
49 const char *name;
50 /** optional minimal setup to accept a connection during keep-alive */
51 int (*new_connection_during_keep_alive_handler)(struct connection *connection);
52 /**
53 * complete code to accept a new connection.
54 * If 'new_connection_during_keep_alive_handler' above is present, this can be
55 * either called alone during the server_loop, or after the function above.
56 * Check the implementation in gdb_server.
57 * */
58 int (*new_connection_handler)(struct connection *connection);
59 /** callback to handle incoming data */
60 int (*input_handler)(struct connection *connection);
61 /** callback to tear down the connection */
62 int (*connection_closed_handler)(struct connection *connection);
63 /** called periodically to send keep-alive messages on the connection */
64 void (*keep_client_alive_handler)(struct connection *connection);
67 struct service {
68 char *name;
69 enum connection_type type;
70 char *port;
71 unsigned short portnumber;
72 int fd;
73 struct sockaddr_in sin;
74 int max_connections;
75 struct connection *connections;
76 int (*new_connection_during_keep_alive)(struct connection *connection);
77 int (*new_connection)(struct connection *connection);
78 int (*input)(struct connection *connection);
79 int (*connection_closed)(struct connection *connection);
80 void (*keep_client_alive)(struct connection *connection);
81 void *priv;
82 struct service *next;
85 int add_service(const struct service_driver *driver, const char *port,
86 int max_connections, void *priv);
87 int remove_service(const char *name, const char *port);
89 int server_host_os_entry(void);
90 int server_host_os_close(void);
92 int server_preinit(void);
93 int server_init(struct command_context *cmd_ctx);
94 int server_quit(void);
95 void server_free(void);
96 void exit_on_signal(int sig);
98 void server_keep_clients_alive(void);
100 int server_loop(struct command_context *command_context);
102 int server_register_commands(struct command_context *context);
104 int connection_write(struct connection *connection, const void *data, int len);
105 int connection_read(struct connection *connection, void *data, int len);
107 bool openocd_is_shutdown_pending(void);
110 * Defines an extended command handler function declaration to enable
111 * access to (and manipulation of) the server port number.
112 * Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
113 * to receive the specified port number.
115 COMMAND_HELPER(server_pipe_command, char **out);
117 COMMAND_HELPER(server_port_command, unsigned short *out);
119 #define ERROR_SERVER_REMOTE_CLOSED (-400)
120 #define ERROR_CONNECTION_REJECTED (-401)
122 #endif /* OPENOCD_SERVER_SERVER_H */