1 /***************************************************************************
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
25 #include "tcl_server.h"
36 #define TCL_SERVER_VERSION "TCL Server 0.1"
37 #define TCL_MAX_LINE (4096)
39 typedef struct tcl_connection_s {
42 char tc_line[TCL_MAX_LINE];
43 int tc_outerror; /* flag an output error */
46 extern Jim_Interp *interp;
47 static unsigned short tcl_port = 0;
50 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
53 static int tcl_new_connection(connection_t *connection);
54 static int tcl_input(connection_t *connection);
55 static int tcl_output(connection_t *connection, const void *buf, ssize_t len);
56 static int tcl_closed(connection_t *connection);
58 /* write data out to a socket.
60 * this is a blocking write, so the return value must equal the length, if
61 * that is not the case then flag the connection with an output error.
63 int tcl_output(connection_t *connection, const void *data, ssize_t len)
66 tcl_connection_t *tclc;
68 tclc = connection->priv;
69 if (tclc->tc_outerror)
70 return ERROR_SERVER_REMOTE_CLOSED;
72 wlen = write_socket(connection->fd, data, len);
76 LOG_ERROR("error during write: %d != %d", (int)wlen, (int)len);
77 tclc->tc_outerror = 1;
78 return ERROR_SERVER_REMOTE_CLOSED;
82 static int tcl_new_connection(connection_t *connection)
84 tcl_connection_t *tclc;
86 tclc = malloc(sizeof(tcl_connection_t));
88 return ERROR_CONNECTION_REJECTED;
90 memset(tclc, 0, sizeof(tcl_connection_t));
91 connection->priv = tclc;
95 static int tcl_input(connection_t *connection)
102 tcl_connection_t *tclc;
105 rlen = read_socket(connection->fd, &in, sizeof(in));
108 LOG_ERROR("error during read: %s", strerror(errno));
109 return ERROR_SERVER_REMOTE_CLOSED;
112 tclc = connection->priv;
114 return ERROR_CONNECTION_REJECTED;
116 /* push as much data into the line as possible */
117 for (i = 0; i < rlen; i++)
119 if (!isprint(in[i]) && !isspace(in[i]))
122 tclc->tc_linedrop = 1;
126 /* buffer the data */
127 tclc->tc_line[tclc->tc_lineoffset] = in[i];
128 if (tclc->tc_lineoffset < TCL_MAX_LINE)
129 tclc->tc_lineoffset++;
131 tclc->tc_linedrop = 1;
136 /* process the line */
137 if (tclc->tc_linedrop) {
138 #define ESTR "line too long\n"
139 retval = tcl_output(connection, ESTR, sizeof(ESTR));
140 if (retval != ERROR_OK)
145 tclc->tc_line[tclc->tc_lineoffset-1] = '\0';
146 retval = Jim_Eval_Named(interp, tclc->tc_line, "remote:connection",1);
147 result = Jim_GetString(Jim_GetResult(interp), &reslen);
148 retval = tcl_output(connection, result, reslen);
149 if (retval != ERROR_OK)
151 if (memchr(result, '\n', reslen) == NULL)
152 tcl_output(connection, "\n", 1);
155 tclc->tc_lineoffset = 0;
156 tclc->tc_linedrop = 0;
162 static int tcl_closed(connection_t *connection)
164 /* cleanup connection context */
165 if (connection->priv) {
166 free(connection->priv);
167 connection->priv = NULL;
178 LOG_WARNING("no tcl port specified, using default port 6666");
182 retval = add_service("tcl", CONNECTION_TCL, tcl_port, 1, tcl_new_connection, tcl_input, tcl_closed, NULL);
186 int tcl_register_commands(command_context_t *cmd_ctx)
188 register_command(cmd_ctx, NULL, "tcl_port", handle_tcl_port_command, COMMAND_CONFIG, "");
192 static int handle_tcl_port_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
195 tcl_port = strtoul(args[0], NULL, 0);