- Added support for native MinGW builds (thanks to Spencer Oliver and Michael Fischer...
[openocd.git] / src / server / server.c
index 628c4925a582b7d9ee8eaf5d2ff79d5e05b7d703..5d7df1af11491a766d4826f35d7c88ef30bdb09e 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);
        }
@@ -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,

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)