diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/gdb_server.c | 50 | ||||
-rw-r--r-- | src/server/server.c | 99 | ||||
-rw-r--r-- | src/server/server.h | 3 | ||||
-rw-r--r-- | src/server/telnet_server.c | 64 |
4 files changed, 155 insertions, 61 deletions
diff --git a/src/server/gdb_server.c b/src/server/gdb_server.c index b0c09961..4b99922c 100644 --- a/src/server/gdb_server.c +++ b/src/server/gdb_server.c @@ -17,7 +17,11 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#ifdef HAVE_CONFIG_H #include "config.h" +#endif + +#include "replacements.h" #include "gdb_server.h" @@ -32,21 +36,6 @@ #include <unistd.h> #include <stdlib.h> -#ifndef HAVE_STRNDUP -#include <stdio.h> -char* strndup(const char *s, size_t n) -{ - size_t len = strnlen (s, n); - char *new = (char *) malloc (len + 1); - - if (new == NULL) - return NULL; - - new[len] = '\0'; - return (char *) memcpy (new, s, len); -} -#endif - #if 0 #define _DEBUG_GDB_IO_ #endif @@ -93,11 +82,26 @@ int gdb_get_char(connection_t *connection, int* next_char) return ERROR_OK; } - while ((gdb_con->buf_cnt = read(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0) + while ((gdb_con->buf_cnt = read_socket(connection->fd, gdb_con->buffer, GDB_BUFFER_SIZE)) <= 0) { if (gdb_con->buf_cnt == 0) return ERROR_SERVER_REMOTE_CLOSED; +#ifdef _WIN32 + errno = WSAGetLastError(); + + switch(errno) + { + case WSAEWOULDBLOCK: + usleep(1000); + break; + case WSAECONNABORTED: + return ERROR_SERVER_REMOTE_CLOSED; + default: + ERROR("read: %d", strerror(errno)); + exit(-1); + } +#else switch(errno) { case EAGAIN: @@ -111,6 +115,7 @@ int gdb_get_char(connection_t *connection, int* next_char) ERROR("read: %s", strerror(errno)); exit(-1); } +#endif } debug_buffer = malloc(gdb_con->buf_cnt + 1); @@ -155,14 +160,14 @@ int gdb_put_packet(connection_t *connection, char *buffer, int len) DEBUG("sending packet '$%s#%2.2x'", debug_buffer, my_checksum); free(debug_buffer); - write(connection->fd, "$", 1); + write_socket(connection->fd, "$", 1); if (len > 0) - write(connection->fd, buffer, len); - write(connection->fd, "#", 1); + write_socket(connection->fd, buffer, len); + write_socket(connection->fd, "#", 1); snprintf(checksum, 3, "%2.2x", my_checksum); - write(connection->fd, checksum, 2); + write_socket(connection->fd, checksum, 2); if ((retval = gdb_get_char(connection, &reply)) != ERROR_OK) return retval; @@ -310,12 +315,12 @@ int gdb_get_packet(connection_t *connection, char *buffer, int *len) if (my_checksum == strtoul(checksum, NULL, 16)) { - write (connection->fd, "+", 1); + write_socket(connection->fd, "+", 1); break; } WARNING("checksum error, requesting retransmission"); - write(connection->fd, "-", 1); + write_socket(connection->fd, "-", 1); } return ERROR_OK; @@ -1087,6 +1092,7 @@ int gdb_init() DEBUG("gdb service for target %s at port %i", target->type->name, gdb_port + i); + i++; target = target->next; } diff --git a/src/server/server.c b/src/server/server.c index 628c4925..5d7df1af 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -17,6 +17,12 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "replacements.h" + #include "server.h" #include "log.h" @@ -29,7 +35,6 @@ #include <errno.h> #include <unistd.h> #include <sys/types.h> -#include <sys/socket.h> #include <fcntl.h> #include <signal.h> @@ -63,7 +68,7 @@ int add_connection(service_t *service, command_context_t *cmd_ctx) } else { - close(c->fd); + close_socket(c->fd); INFO("attempted '%s' connection rejected", service->name); free(c); } @@ -97,7 +102,7 @@ int remove_connection(service_t *service, connection_t *connection) { service->connections = next; service->connection_closed(c); - close(c->fd); + close_socket(c->fd); command_done(c->cmd_ctx); @@ -119,7 +124,6 @@ int add_service(char *name, enum connection_type type, unsigned short port, int { service_t *c, *p; int so_reuseaddr_option = 1; - int oldopts; c = malloc(sizeof(service_t)); @@ -141,10 +145,9 @@ int add_service(char *name, enum connection_type type, unsigned short port, int exit(-1); } - setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, &so_reuseaddr_option, sizeof(int)); + setsockopt(c->fd, SOL_SOCKET, SO_REUSEADDR, (void*)&so_reuseaddr_option, sizeof(int)); - oldopts = fcntl(c->fd, F_GETFL, 0); - fcntl(c->fd, F_SETFL, oldopts | O_NONBLOCK); + socket_nonblock(c->fd); memset(&c->sin, 0, sizeof(c->sin)); c->sin.sin_family = AF_INET; @@ -205,6 +208,31 @@ int remove_service(unsigned short port) return ERROR_OK; } +int remove_services() +{ + service_t *c = services; + + /* loop service */ + while(c) + { + service_t *next = c->next; + + if (c->name) + free(c->name); + + if (c->priv) + free(c->priv); + + /* delete service */ + free(c); + + /* remember the last service for unlinking */ + c = next; + } + + return ERROR_OK; +} + int server_loop(command_context_t *command_context) { service_t *service; @@ -217,8 +245,10 @@ int server_loop(command_context_t *command_context) /* used in accept() */ int retval; +#ifndef _WIN32 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) ERROR("couldn't set SIGPIPE to SIG_IGN"); +#endif /* do regular tasks after at most 10ms */ tv.tv_sec = 0; @@ -256,11 +286,26 @@ int server_loop(command_context_t *command_context) } } +#ifndef _WIN32 /* add STDIN to read_fds */ FD_SET(fileno(stdin), &read_fds); +#endif if ((retval = select(fd_max + 1, &read_fds, NULL, NULL, &tv)) == -1) { +#ifdef _WIN32 + + errno = WSAGetLastError(); + + if (errno == WSAEINTR) + FD_ZERO(&read_fds); + else + { + ERROR("error during select: %d", strerror(errno)); + exit(-1); + } +#else + if (errno == EINTR) FD_ZERO(&read_fds); else @@ -268,6 +313,7 @@ int server_loop(command_context_t *command_context) ERROR("error during select: %s", strerror(errno)); exit(-1); } +#endif } target_call_timer_callbacks(); @@ -300,7 +346,7 @@ int server_loop(command_context_t *command_context) unsigned int address_size = sizeof(sin); int tmp_fd; tmp_fd = accept(service->fd, (struct sockaddr *)&service->sin, &address_size); - close(tmp_fd); + close_socket(tmp_fd); INFO("rejected '%s' connection, no more connections allowed", service->name); } } @@ -328,6 +374,7 @@ int server_loop(command_context_t *command_context) } } +#ifndef _WIN32 if (FD_ISSET(fileno(stdin), &read_fds)) { if (getc(stdin) == 'x') @@ -335,17 +382,53 @@ int server_loop(command_context_t *command_context) shutdown_openocd = 1; } } +#endif } return ERROR_OK; } +#ifdef _WIN32 +BOOL WINAPI ControlHandler(DWORD dwCtrlType) +{ + shutdown_openocd = 1; + return TRUE; +} +#endif + int server_init() { +#ifdef _WIN32 + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD( 2, 2 ); + + if (WSAStartup(wVersionRequested, &wsaData) != 0) + { + ERROR("Failed to Open Winsock"); + exit(-1); + } + + SetConsoleCtrlHandler( ControlHandler, TRUE ); +#endif + return ERROR_OK; } +int server_close() +{ + remove_services(); + +#ifdef _WIN32 + WSACleanup(); + SetConsoleCtrlHandler( ControlHandler, FALSE ); +#endif + + return ERROR_OK; +} + int server_register_commands(command_context_t *context) { register_command(context, NULL, "shutdown", handle_shutdown_command, diff --git a/src/server/server.h b/src/server/server.h index 625c364e..ddf0b97d 100644 --- a/src/server/server.h +++ b/src/server/server.h @@ -22,10 +22,9 @@ #include "command.h" #include "binarybuffer.h" +#include "replacements.h" #include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> enum connection_type { diff --git a/src/server/telnet_server.c b/src/server/telnet_server.c index a2704e9c..b573ec5c 100644 --- a/src/server/telnet_server.c +++ b/src/server/telnet_server.c @@ -17,6 +17,12 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "replacements.h" + #include "telnet_server.h" #include "server.h" @@ -47,15 +53,15 @@ void telnet_prompt(connection_t *connection) { telnet_connection_t *t_con = connection->priv; - write(connection->fd, t_con->prompt, strlen(t_con->prompt)); + write_socket(connection->fd, t_con->prompt, strlen(t_con->prompt)); } int telnet_output(struct command_context_s *cmd_ctx, char* line) { connection_t *connection = cmd_ctx->output_handler_priv; - write(connection->fd, line, strlen(line)); - write(connection->fd, "\r\n\0", 3); + write_socket(connection->fd, line, strlen(line)); + write_socket(connection->fd, "\r\n\0", 3); return ERROR_OK; } @@ -109,13 +115,13 @@ int telnet_new_connection(connection_t *connection) command_set_output_handler(connection->cmd_ctx, telnet_output, connection); /* negotiate telnet options */ - write(connection->fd, negotiate, strlen(negotiate)); + write_socket(connection->fd, negotiate, strlen(negotiate)); /* print connection banner */ if (telnet_service->banner) { - write(connection->fd, telnet_service->banner, strlen(telnet_service->banner)); - write(connection->fd, "\r\n\0", 3); + write_socket(connection->fd, telnet_service->banner, strlen(telnet_service->banner)); + write_socket(connection->fd, "\r\n\0", 3); } telnet_prompt(connection); @@ -138,13 +144,13 @@ void telnet_clear_line(connection_t *connection, telnet_connection_t *t_con) /* move to end of line */ if (t_con->line_cursor < t_con->line_size) { - write(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); + write_socket(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); } /* backspace, overwrite with space, backspace */ while (t_con->line_size > 0) { - write(connection->fd, "\b \b", 3); + write_socket(connection->fd, "\b \b", 3); t_con->line_size--; } t_con->line_cursor = 0; @@ -158,7 +164,7 @@ int telnet_input(connection_t *connection) telnet_connection_t *t_con = connection->priv; command_context_t *command_context = connection->cmd_ctx; - bytes_read = read(connection->fd, buffer, TELNET_BUFFER_SIZE); + bytes_read = read_socket(connection->fd, buffer, TELNET_BUFFER_SIZE); if (bytes_read == 0) return ERROR_SERVER_REMOTE_CLOSED; @@ -182,7 +188,7 @@ int telnet_input(connection_t *connection) { if (isprint(*buf_p)) /* printable character */ { - write(connection->fd, buf_p, 1); + write_socket(connection->fd, buf_p, 1); if (t_con->line_cursor == t_con->line_size) { t_con->line[t_con->line_size++] = *buf_p; @@ -194,10 +200,10 @@ int telnet_input(connection_t *connection) memmove(t_con->line + t_con->line_cursor + 1, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); t_con->line[t_con->line_cursor++] = *buf_p; t_con->line_size++; - write(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); + write_socket(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); for (i = t_con->line_cursor; i < t_con->line_size; i++) { - write(connection->fd, "\b", 1); + write_socket(connection->fd, "\b", 1); } } } @@ -225,7 +231,7 @@ int telnet_input(connection_t *connection) } t_con->line[t_con->line_size] = 0; - write(connection->fd, "\r\n\x00", 3); + write_socket(connection->fd, "\r\n\x00", 3); if (strcmp(t_con->line, "history") == 0) { @@ -234,8 +240,8 @@ int telnet_input(connection_t *connection) { if (t_con->history[i]) { - write(connection->fd, t_con->history[i], strlen(t_con->history[i])); - write(connection->fd, "\r\n\x00", 3); + write_socket(connection->fd, t_con->history[i], strlen(t_con->history[i])); + write_socket(connection->fd, "\r\n\x00", 3); } } telnet_prompt(connection); @@ -299,16 +305,16 @@ int telnet_input(connection_t *connection) if (t_con->line_cursor != t_con->line_size) { int i; - write(connection->fd, "\b", 1); + write_socket(connection->fd, "\b", 1); t_con->line_cursor--; t_con->line_size--; memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor); - write(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); - write(connection->fd, " \b", 2); + write_socket(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); + write_socket(connection->fd, " \b", 2); for (i = t_con->line_cursor; i < t_con->line_size; i++) { - write(connection->fd, "\b", 1); + write_socket(connection->fd, "\b", 1); } } else @@ -316,7 +322,7 @@ int telnet_input(connection_t *connection) t_con->line_size--; t_con->line_cursor--; /* back space: move the 'printer' head one char back, overwrite with space, move back again */ - write(connection->fd, "\b \b", 3); + write_socket(connection->fd, "\b \b", 3); } } } @@ -328,7 +334,7 @@ int telnet_input(connection_t *connection) { if (t_con->line_cursor > 0) { - write(connection->fd, "\b", 1); + write_socket(connection->fd, "\b", 1); t_con->line_cursor--; } t_con->state = TELNET_STATE_DATA; @@ -337,7 +343,7 @@ int telnet_input(connection_t *connection) { if (t_con->line_cursor < t_con->line_size) { - write(connection->fd, t_con->line + t_con->line_cursor++, 1); + write_socket(connection->fd, t_con->line + t_con->line_cursor++, 1); } t_con->state = TELNET_STATE_DATA; } @@ -382,7 +388,7 @@ int telnet_input(connection_t *connection) { if (t_con->line_cursor > 0) { - write(connection->fd, "\b", 1); + write_socket(connection->fd, "\b", 1); t_con->line_cursor--; } t_con->state = TELNET_STATE_DATA; @@ -391,7 +397,7 @@ int telnet_input(connection_t *connection) { if (t_con->line_cursor < t_con->line_size) { - write(connection->fd, t_con->line + t_con->line_cursor++, 1); + write_socket(connection->fd, t_con->line + t_con->line_cursor++, 1); } t_con->state = TELNET_STATE_DATA; } @@ -404,7 +410,7 @@ int telnet_input(connection_t *connection) t_con->line_size = strlen(t_con->history[last_history]); t_con->line_cursor = t_con->line_size; memcpy(t_con->line, t_con->history[last_history], t_con->line_size + 1); - write(connection->fd, t_con->line, t_con->line_size); + write_socket(connection->fd, t_con->line, t_con->line_size); t_con->current_history = last_history; } t_con->state = TELNET_STATE_DATA; @@ -418,7 +424,7 @@ int telnet_input(connection_t *connection) t_con->line_size = strlen(t_con->history[next_history]); t_con->line_cursor = t_con->line_size; memcpy(t_con->line, t_con->history[next_history], t_con->line_size + 1); - write(connection->fd, t_con->line, t_con->line_size); + write_socket(connection->fd, t_con->line, t_con->line_size); t_con->current_history = next_history; } t_con->state = TELNET_STATE_DATA; @@ -445,14 +451,14 @@ int telnet_input(connection_t *connection) memmove(t_con->line + t_con->line_cursor, t_con->line + t_con->line_cursor + 1, t_con->line_size - t_con->line_cursor); /* print remainder of buffer */ - write(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); + write_socket(connection->fd, t_con->line + t_con->line_cursor, t_con->line_size - t_con->line_cursor); /* overwrite last char with whitespace */ - write(connection->fd, " \b", 2); + write_socket(connection->fd, " \b", 2); /* move back to cursor position*/ for (i = t_con->line_cursor; i < t_con->line_size; i++) { - write(connection->fd, "\b", 1); + write_socket(connection->fd, "\b", 1); } } |