SOCKET sockets_accept(SOCKET s, struct sockaddr *addr, socklen_t *addrlen) { SOCKET client = INVALID_SOCKET; sockets_begin(s, FD_ACCEPT); do { client = WSAAccept(s, addr, addrlen, NULL, 0); } while (SOCKET_ISERR(client) && !sockets_wait(INFINITE)); sockets_end(s); return client; }
static int gdb_server(int port) { int sock; int client; struct sockaddr_in addr; socklen_t len; int arg; struct gdb_data data; int i; sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if (SOCKET_ISERR(sock)) { pr_error("gdb: can't create socket"); return -1; } arg = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&arg, sizeof(arg)) < 0) pr_error("gdb: warning: can't reuse socket address"); addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { printc_err("gdb: can't bind to port %d: %s\n", port, last_error()); closesocket(sock); return -1; } if (listen(sock, 1) < 0) { pr_error("gdb: can't listen on socket"); closesocket(sock); return -1; } printc("Bound to port %d. Now waiting for connection...\n", port); len = sizeof(addr); client = sockets_accept(sock, (struct sockaddr *)&addr, &len); if (SOCKET_ISERR(client)) { pr_error("gdb: failed to accept connection"); closesocket(sock); return -1; } closesocket(sock); printc("Client connected from %s:%d\n", inet_ntoa(addr.sin_addr), htons(addr.sin_port)); gdb_init(&data, client); /* Put the hardware breakpoint setting into a known state. */ printc("Clearing all breakpoints...\n"); for (i = 0; i < device_default->max_breakpoints; i++) device_setbrk(device_default, i, 0, 0); #ifdef DEBUG_GDB printc("starting GDB reader loop...\n"); #endif gdb_reader_loop(&data); #ifdef DEBUG_GDB printc("... reader loop returned\n"); #endif closesocket(client); return data.error ? -1 : 0; }