keep-alive: drop link with log framework
[fw/openocd] / src / server / server.h
1 /***************************************************************************
2  *   Copyright (C) 2005 by Dominic Rath                                    *
3  *   Dominic.Rath@gmx.de                                                   *
4  *                                                                         *
5  *   Copyright (C) 2007,2008 Ã˜yvind Harboe                                 *
6  *   oyvind.harboe@zylin.com                                               *
7  *                                                                         *
8  *   Copyright (C) 2008 by Spencer Oliver                                  *
9  *   spen@spen-soft.co.uk                                                  *
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  *   This program is distributed in the hope that it will be useful,       *
17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
19  *   GNU General Public License for more details.                          *
20  *                                                                         *
21  *   You should have received a copy of the GNU General Public License     *
22  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
23  ***************************************************************************/
24
25 #ifndef OPENOCD_SERVER_SERVER_H
26 #define OPENOCD_SERVER_SERVER_H
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <helper/log.h>
33 #include <helper/replacements.h>
34
35 #ifdef HAVE_NETINET_IN_H
36 #include <netinet/in.h>
37 #endif
38
39 enum connection_type {
40         CONNECTION_TCP,
41         CONNECTION_PIPE,
42         CONNECTION_STDINOUT
43 };
44
45 #define CONNECTION_LIMIT_UNLIMITED              (-1)
46
47 struct connection {
48         int fd;
49         int fd_out;     /* When using pipes we're writing to a different fd */
50         struct sockaddr_in sin;
51         struct command_context *cmd_ctx;
52         struct service *service;
53         bool input_pending;
54         void *priv;
55         struct connection *next;
56 };
57
58 struct service_driver {
59         /** the name of the server */
60         const char *name;
61         /** optional minimal setup to accept a connection during keep-alive */
62         int (*new_connection_during_keep_alive_handler)(struct connection *connection);
63         /**
64          * complete code to accept a new connection.
65          * If 'new_connection_during_keep_alive_handler' above is present, this can be
66          * either called alone during the server_loop, or after the function above.
67          * Check the implementation in gdb_server.
68          * */
69         int (*new_connection_handler)(struct connection *connection);
70         /** callback to handle incoming data */
71         int (*input_handler)(struct connection *connection);
72         /** callback to tear down the connection */
73         int (*connection_closed_handler)(struct connection *connection);
74         /** called periodically to send keep-alive messages on the connection */
75         void (*keep_client_alive_handler)(struct connection *connection);
76 };
77
78 struct service {
79         char *name;
80         enum connection_type type;
81         char *port;
82         unsigned short portnumber;
83         int fd;
84         struct sockaddr_in sin;
85         int max_connections;
86         struct connection *connections;
87         int (*new_connection_during_keep_alive)(struct connection *connection);
88         int (*new_connection)(struct connection *connection);
89         int (*input)(struct connection *connection);
90         int (*connection_closed)(struct connection *connection);
91         void (*keep_client_alive)(struct connection *connection);
92         void *priv;
93         struct service *next;
94 };
95
96 int add_service(const struct service_driver *driver, const char *port,
97                 int max_connections, void *priv);
98 int remove_service(const char *name, const char *port);
99
100 int server_host_os_entry(void);
101 int server_host_os_close(void);
102
103 int server_preinit(void);
104 int server_init(struct command_context *cmd_ctx);
105 int server_quit(void);
106 void server_free(void);
107 void exit_on_signal(int sig);
108
109 void server_keep_clients_alive(void);
110
111 int server_loop(struct command_context *command_context);
112
113 int server_register_commands(struct command_context *context);
114
115 int connection_write(struct connection *connection, const void *data, int len);
116 int connection_read(struct connection *connection, void *data, int len);
117
118 /**
119  * Defines an extended command handler function declaration to enable
120  * access to (and manipulation of) the server port number.
121  * Call server_port like a normal COMMAND_HANDLER with an extra @a out parameter
122  * to receive the specified port number.
123  */
124 COMMAND_HELPER(server_pipe_command, char **out);
125
126 COMMAND_HELPER(server_port_command, unsigned short *out);
127
128 #define ERROR_SERVER_REMOTE_CLOSED              (-400)
129 #define ERROR_CONNECTION_REJECTED               (-401)
130
131 #endif /* OPENOCD_SERVER_SERVER_H */