// Listen for incoming SSH connections. // When a connection is established, write all data received to stdout. void server_pipe(char *host, int port) { ssh_bind b = ssh_bind_new(); ssh_session s = ssh_new(); ssh_bind_options_set(b, SSH_BIND_OPTIONS_BINDADDR, host); ssh_bind_options_set(b, SSH_BIND_OPTIONS_BINDPORT, &port); ssh_bind_options_set(b, SSH_BIND_OPTIONS_RSAKEY, "test-server-key"); ssh_bind_options_set(b, SSH_BIND_OPTIONS_LOG_VERBOSITY_STR, "5"); if(ssh_bind_listen(b) < 0) session_error(b, "listen"); if(ssh_bind_accept(b, s) != SSH_OK) session_error(b, "accept"); if(ssh_accept(s) < 0) session_error(s, "handshake"); int state = SERVER_CONNECTED; while(1) { ssh_message m = ssh_message_get(s); if(m) { int type = ssh_message_type(m); int subtype = ssh_message_subtype(m); ssh_message_auth_set_methods(m, SSH_AUTH_METHOD_PUBLICKEY); server_handle_message(s, m, type, subtype, &state); ssh_message_free(m); if(state == SERVER_CLOSED) { ssh_disconnect(s); ssh_bind_free(b); ssh_finalize(); return; } } else { session_error(s, "session"); } } }
/* Startet den Server */ void server_start() { unsigned int clientlen; int readBytes, childPid; struct sockaddr_in servaddr, clientaddr; servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(port); if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); exit(2); } if (bind(server_socket, (struct sockaddr*) &servaddr, sizeof(servaddr)) < 0) { perror("bind"); exit(3); } if (listen(server_socket, 5) < 0) { perror("listen"); exit(4); } clientlen = sizeof(clientaddr); for (;;) { /* Get new client socket conection */ if ((client_socket = accept(server_socket, (struct sockaddr *) &clientaddr, &clientlen)) < 0) { perror("accept"); exit(5); } /* Fork a child to handle the request */ if ((childPid = fork()) < 0) { perror("fork"); exit(6); } else if (childPid == 0) { if (debuglevel > 0) printf("Child: Client connected from host %s:%d\n", inet_ntoa( clientaddr.sin_addr), ntohs(clientaddr.sin_port)); while (client_socket > 0) { /* read request */ netcp_message *requestMessage = malloc(sizeof(netcp_message)); readBytes = read(client_socket, requestMessage, sizeof(netcp_message)); if (readBytes < 0) { perror("read"); server_close(); exit(7); } server_handle_message(requestMessage); free(requestMessage); } if (debuglevel > 0) printf("Client disconnected from host %s:%d\n", inet_ntoa( clientaddr.sin_addr), ntohs(clientaddr.sin_port)); exit(0); } else { if (debuglevel > 0) printf("Forking child %d for client %s:%d\n", childPid, inet_ntoa(clientaddr.sin_addr), ntohs( clientaddr.sin_port)); server_close(); } } }
/***************************************************************************** Get and handle: - new connections, - input from connections, - input from server operator in stdin Returns: 0 if went past end-of-turn timeout 2 if force_end_of_sniff found to be set 1 otherwise (got and processed something?) *****************************************************************************/ int sniff_packets(void) { int i; int max_desc; fd_set readfs; struct timeval tv; static time_t time_at_turn_end; static int year; int rv; #if defined (commentout) tv.tv_sec=0; tv.tv_usec=0; tv.tv_sec=1; tv.tv_usec=0; #endif tv.tv_sec=0; tv.tv_usec=0; FD_ZERO(&readfs); #if defined (commentout) FD_SET(0, &readfs); #endif FD_SET(sock, &readfs); #if defined (WIN32) max_desc = 0; /* W32 ignores first argument to select() */ #else max_desc=sock; #endif for(i=0; i<MAX_CONNECTIONS; i++) { if(connections[i].used) { FD_SET(connections[i].sock, &readfs); } #if !defined (WIN32) max_desc=MAX(connections[i].sock, max_desc); #endif } rv = select(max_desc+1, &readfs, NULL, NULL, &tv); if(rv==0) { /* timeout */ } #if defined (WIN32) if (rv == SOCKET_ERROR) { printf ("Got a SOCKET_ERROR from select()!!!\n"); } #endif /* new players connects */ if(FD_ISSET(sock, &readfs)) { printf ("got new connection\n"); if(server_accept_connection(sock)==-1) printf("failed accepting connection\n"); } #if defined (commentout) /* input from server operator */ else if(FD_ISSET(0, &readfs)) { int didget; char buf[BUF_SIZE+1]; if((didget=read(0, buf, BUF_SIZE))==-1) { printf ("read from stdin failed\n"); exit(1); } *(buf+didget)='\0'; printf ("Prompt garbage\n"); } #endif /* input from a player */ else { for(i=0; i<MAX_CONNECTIONS; i++) { if(connections[i].used && FD_ISSET(connections[i].sock, &readfs)) { int rv; rv = net_recv_message (connections[i].sock, &connections[i].msgbuf); if (rv < 0) { printf ("Lost connection to player...\n"); close_connection(&connections[i]); break; } else if (rv > 0) { server_handle_message (&connections[i]); } } } } return 1; }