1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2005 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
7 * Copyright (C) 2007,2008 Øyvind Harboe *
8 * oyvind.harboe@zylin.com *
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
21 #include <helper/log.h>
22 #include <helper/replacements.h>
24 #ifdef HAVE_NETINET_IN_H
25 #include <netinet/in.h>
28 enum connection_type
{
34 #define CONNECTION_LIMIT_UNLIMITED (-1)
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
;
44 struct connection
*next
;
47 struct service_driver
{
48 /** the name of the server */
50 /** optional minimal setup to accept a connection during keep-alive */
51 int (*new_connection_during_keep_alive_handler
)(struct connection
*connection
);
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.
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
);
69 enum connection_type type
;
71 unsigned short portnumber
;
73 struct sockaddr_in sin
;
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
);
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 */