- added support for Asix Presto JTAG interface (thanks to Pavel Chromy and Asix for...
[fw/openocd] / src / server / server.c
index a034da7928e73cafa9497be592eabe345a36eddb..4c4fdecccd1972b203ec9a2c9ac262879680915b 100644 (file)
  *   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);
        }
@@ -86,21 +91,18 @@ int add_connection(service_t *service, command_context_t *cmd_ctx)
 
 int remove_connection(service_t *service, connection_t *connection)
 {
-       connection_t *c, *p = NULL;
-
+       connection_t *c = service->connections;
+       
        /* find connection */
-       for (c = service->connections; c; c = c->next)
+       while(c)
        {
+               connection_t *next = c->next;
+               
                if (c->fd == connection->fd)
-               {
-                       /* unlink connection */
-                       if (p)
-                               p->next = c->next;
-                       else
-                               service->connections = c->next;
-                       
+               {       
+                       service->connections = next;
                        service->connection_closed(c);
-                       close(c->fd);
+                       close_socket(c->fd);
                        
                        command_done(c->cmd_ctx);
                        
@@ -112,7 +114,7 @@ int remove_connection(service_t *service, connection_t *connection)
                }
                
                /* remember the last connection for unlinking */
-               p = c;
+               c = next;
        }
        
        return ERROR_OK;
@@ -122,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));
        
@@ -144,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;
@@ -182,33 +182,57 @@ int add_service(char *name, enum connection_type type, unsigned short port, int
 
 int remove_service(unsigned short port)
 {
-       service_t *c, *p = NULL;
-
+       service_t *c = services;
+       
        /* find service */
-       for (c = services; c; c = c->next)
+       while(c)
        {
+               service_t *next = c->next;
+               
                if (c->port == port)
-               {
-                       /* unlink service */
-                       if (p)
-                               p->next = c->next;
-                       else
-                               services = c->next;
-                       
+               {       
                        if (c->name)
                                free(c->name);
                        
+                       if (c->priv)
+                               free(c->priv);
+                       
                        /* delete service */
                        free(c);
                }
                
                /* remember the last service for unlinking */
-               p = c;
+               c = next;
        }
        
        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;
@@ -221,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;
@@ -260,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: %s", strerror(errno));
+                               exit(-1);
+                       }
+#else
+
                        if (errno == EINTR)
                                FD_ZERO(&read_fds);
                        else
@@ -272,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();
@@ -304,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);
                                }
                        }
@@ -332,6 +374,7 @@ int server_loop(command_context_t *command_context)
                        }
                }
                
+#ifndef _WIN32
                if (FD_ISSET(fileno(stdin), &read_fds))
                {
                        if (getc(stdin) == 'x')
@@ -339,17 +382,68 @@ int server_loop(command_context_t *command_context)
                                shutdown_openocd = 1;
                        }
                }
+#else
+               MSG msg;
+               while (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
+               {
+                       if (msg.message==WM_QUIT) shutdown_openocd = 1;
+               }
+#endif
        }
        
        return ERROR_OK;
 }
 
+#ifdef _WIN32
+BOOL WINAPI ControlHandler(DWORD dwCtrlType)
+{
+    shutdown_openocd = 1;
+    return TRUE;
+}
+
+void sig_handler(int sig) {
+    shutdown_openocd = 1;
+}
+#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 );
+
+        signal(SIGINT, sig_handler);
+        signal(SIGTERM, sig_handler);
+        signal(SIGBREAK, sig_handler);
+        signal(SIGABRT, sig_handler);
+#endif
+
        
        return ERROR_OK;
 }
 
+int server_quit()
+{
+       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,