/** * Manages the messages submission. * \return True in case of success, false instead. * [PSR] */ int answer() { PLAYER *pp; int newsock; static unsigned long mode; /* Changed from u_long. [PSR] */ static char name[NAMELEN]; static char team; static int enter_status; static unsigned int socklen; /* Edited from static int in order to match accept() parameter. [PSR] */ static unsigned long machine; /* Edited from u_long in order to match accept() parameter. [PSR] */ static u_int32_t uid; static SOCKET sockstruct; char *cp1, *cp2; int flags; u_int32_t version; int i; # ifdef INTERNET socklen = sizeof sockstruct; # else socklen = sizeof sockstruct - 1; # endif errno = 0; newsock = accept(main_socket, (struct sockaddr *) &sockstruct, &socklen); if (newsock < 0) { if (errno == EINTR) { return false; } # ifdef LOG iso_syslog(LOG_ERR, "accept: %m"); # else perror("accept"); # endif cleanup(1); } # ifdef INTERNET machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr); # else if (machine == 0) { machine = gethostid(); } # endif version = htonl((u_int32_t) HUNT_VERSION); write_and_push(newsock, (char *) &version, LONGLEN); /* Authentication. [PSR] */ #ifdef INTERNET if(password_hash!=NULL) { /* A password has been set. [PSR] */ write_and_push(newsock, &c_auth, SHORTLEN); bool auth = false; for(i=0; i<MAXATTEMPT && !auth; i++) { /* 3 password attempts. [PSR] */ safe_read(newsock, &client_psw, (13 + 1) * sizeof(char)); if(strcmp(client_psw, password_hash)!=0) { /* Authentication failed. New request. [PSR] */ write_and_push(newsock, &c_auth, SHORTLEN); } else { /* Authentication successful. [PSR]*/ write_and_push(newsock, &c_auth_success, SHORTLEN); auth = true; } } if(!auth) { write_and_push(newsock, &c_refuse, SHORTLEN); safe_close(newsock); return false; } } else { /* No authentication. [PSR] */ write_and_push(newsock, &c_auth_success, SHORTLEN); } #endif safe_read(newsock, (char *) &uid, LONGLEN); uid = ntohl((unsigned long) uid); safe_read(newsock, name, NAMELEN); safe_read(newsock, &team, 1); safe_read(newsock, (char *) &enter_status, LONGLEN); enter_status = ntohl((unsigned long) enter_status); safe_read(newsock, ttyname_glob, NAMELEN); safe_read(newsock, (char *) &mode, sizeof mode); mode = ntohl(mode); /* * Turn off blocking I/O, so a slow or dead terminal won't stop * the game. All subsequent reads check how many bytes they read. */ flags = fcntl(newsock, F_GETFL, 0); flags |= O_NDELAY; (void) fcntl(newsock, F_SETFL, flags); /* * Make sure the name contains only printable characters * since we use control characters for cursor control * between driver and player processes */ for (cp1 = cp2 = name; *cp1 != '\0'; cp1++) { if (isprint((unsigned char)*cp1) || *cp1 == ' ') { *cp2++ = *cp1; } } *cp2 = '\0'; # ifdef INTERNET if (mode == C_MESSAGE) { char buf[BUFSIZ + 1]; int n; if (team == ' ') { (void) sprintf(buf, "%s: ", name); } else { (void) sprintf(buf, "%s[%c]: ", name, team); } n = strlen(buf); for (pp = player; pp < end_player; pp++) { cgoto(pp, HEIGHT, 0); outstr(pp, buf, n); } while ((n = read(newsock, buf, BUFSIZ)) > 0) for (pp = player; pp < end_player; pp++) { outstr(pp, buf, n); } for (pp = player; pp < end_player; pp++) { ce(pp); sendcom(pp, REFRESH); sendcom(pp, READY, 0); (void) fflush(pp->p_output); } safe_close(newsock); return false; } else # endif # ifdef MONITOR if (mode == C_MONITOR) { if (end_monitor < &monitor[MAXMON]) { pp = end_monitor++; i = pp - monitor + MAXPL + 3; } else { socklen = 0; write_and_push(newsock, (char *) &socklen, sizeof socklen); safe_close(newsock); return false; } } else # endif if (end_player < &player[MAXPL]) { pp = end_player++; i = pp - player + 3; } else { socklen = 0; write_and_push(newsock, (char *) &socklen, sizeof socklen); safe_close(newsock); return false; } #ifdef MONITOR if (mode == C_MONITOR && team == ' ') { team = '*'; } #endif pp->p_ident = get_ident(machine, uid, name, team); pp->p_output = fdopen(newsock, "w"); pp->p_death[0] = '\0'; pp->p_fd = newsock; fdset[i].fd = newsock; fdset[i].events = POLLIN; pp->p_y = 0; pp->p_x = 0; # ifdef MONITOR if (mode == C_MONITOR) { stmonitor(pp); } else # endif stplayer(pp, enter_status); return true; }
int answer() { PLAYER *pp; int newsock; static u_long mode; static char name[NAMELEN]; static char team; static int enter_status; static int socklen; static u_long machine; static u_int32_t uid; static SOCKET sockstruct; char *cp1, *cp2; int flags; u_int32_t version; int i; # ifdef INTERNET socklen = sizeof sockstruct; # else socklen = sizeof sockstruct - 1; # endif errno = 0; newsock = accept(Socket, (struct sockaddr *) &sockstruct, &socklen); if (newsock < 0) { if (errno == EINTR) return FALSE; # ifdef LOG syslog(LOG_ERR, "accept: %m"); # else perror("accept"); # endif cleanup(1); } # ifdef INTERNET machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr); # else if (machine == 0) machine = gethostid(); # endif version = htonl((u_int32_t) HUNT_VERSION); (void) write(newsock, (char *) &version, LONGLEN); (void) read(newsock, (char *) &uid, LONGLEN); uid = ntohl((unsigned long) uid); (void) read(newsock, name, NAMELEN); (void) read(newsock, &team, 1); (void) read(newsock, (char *) &enter_status, LONGLEN); enter_status = ntohl((unsigned long) enter_status); (void) read(newsock, Ttyname, NAMELEN); (void) read(newsock, (char *) &mode, sizeof mode); mode = ntohl(mode); /* * Turn off blocking I/O, so a slow or dead terminal won't stop * the game. All subsequent reads check how many bytes they read. */ flags = fcntl(newsock, F_GETFL, 0); flags |= O_NDELAY; (void) fcntl(newsock, F_SETFL, flags); /* * Make sure the name contains only printable characters * since we use control characters for cursor control * between driver and player processes */ for (cp1 = cp2 = name; *cp1 != '\0'; cp1++) if (isprint((unsigned char)*cp1) || *cp1 == ' ') *cp2++ = *cp1; *cp2 = '\0'; # ifdef INTERNET if (mode == C_MESSAGE) { char buf[BUFSIZ + 1]; int n; if (team == ' ') (void) sprintf(buf, "%s: ", name); else (void) sprintf(buf, "%s[%c]: ", name, team); n = strlen(buf); for (pp = Player; pp < End_player; pp++) { cgoto(pp, HEIGHT, 0); outstr(pp, buf, n); } while ((n = read(newsock, buf, BUFSIZ)) > 0) for (pp = Player; pp < End_player; pp++) outstr(pp, buf, n); for (pp = Player; pp < End_player; pp++) { ce(pp); sendcom(pp, REFRESH); sendcom(pp, READY, 0); (void) fflush(pp->p_output); } (void) close(newsock); return FALSE; } else # endif # ifdef MONITOR if (mode == C_MONITOR) if (End_monitor < &Monitor[MAXMON]) { pp = End_monitor++; i = pp - Monitor + MAXPL + 3; } else { socklen = 0; (void) write(newsock, (char *) &socklen, sizeof socklen); (void) close(newsock); return FALSE; } else # endif if (End_player < &Player[MAXPL]) { pp = End_player++; i = pp - Player + 3; } else { socklen = 0; (void) write(newsock, (char *) &socklen, sizeof socklen); (void) close(newsock); return FALSE; } #ifdef MONITOR if (mode == C_MONITOR && team == ' ') team = '*'; #endif pp->p_ident = get_ident(machine, uid, name, team); pp->p_output = fdopen(newsock, "w"); pp->p_death[0] = '\0'; pp->p_fd = newsock; fdset[i].fd = newsock; fdset[i].events = POLLIN; pp->p_y = 0; pp->p_x = 0; # ifdef MONITOR if (mode == C_MONITOR) stmonitor(pp); else # endif stplayer(pp, enter_status); return TRUE; }
answer() { register PLAYER *pp; register int newsock; register FILE *tmpfd; # ifdef MONITOR static FLAG monitor; # endif MONITOR static char name[NAMELEN]; static int socklen; static u_long machine; static u_long uid; static SOCKET sockstruct; # ifdef OLDIPC extern SOCKET Daemon; # endif OLDIPC # ifdef INTERNET socklen = sizeof sockstruct; # else socklen = sizeof sockstruct - 1; # endif INTERNET errno = 0; # ifndef OLDIPC if ((newsock = accept(Socket, &sockstruct, &socklen)) < 0) # else OLDIPC if (accept(Socket, &sockstruct) < 0) # endif OLDIPC { if (errno == EINTR) return; perror("accept"); cleanup(1); } # ifdef OLDIPC newsock = Socket; Socket = socket(SOCK_STREAM, 0, (struct sockaddr *) &Daemon, SO_ACCEPTCONN); if (Socket < 0) { perror("new accept socket"); cleanup(1); } Sock_mask = (1 << Socket); Fds_mask |= Sock_mask; if (Socket >= Num_fds) Num_fds = Socket + 1; # endif OLDIPC tmpfd = fdopen(newsock, "w"); # ifdef INTERNET machine = ntohl(((struct sockaddr_in *) &sockstruct)->sin_addr.s_addr); # else INTERNET if (machine == 0) machine = gethostid(); # endif INTERNET (void) putw(getpid(), tmpfd); (void) read(newsock, (char *) &uid, sizeof uid); uid = ntohl(uid); (void) read(newsock, name, NAMELEN); (void) read(newsock, Ttyname, NAMELEN); # ifdef MONITOR (void) read(newsock, (char *) &monitor, sizeof monitor); # endif MONITOR if (reached_limit(machine)) { socklen = 0; (void) write(newsock, (char *) &socklen, sizeof socklen); (void) close(newsock); # ifdef OLDIPC Fds_mask &= ~(1 << newsock); # endif OLDIPC return; } # ifdef MONITOR if (monitor) if (End_monitor < &Monitor[MAXMON]) pp = End_monitor++; else { socklen = 0; (void) write(newsock, (char *) &socklen, sizeof socklen); (void) close(newsock); return; } else # endif MONITOR if (End_player < &Player[MAXPL]) pp = End_player++; else { socklen = 0; (void) write(newsock, (char *) &socklen, sizeof socklen); (void) close(newsock); return; } pp->p_ident = get_ident(machine, uid, name); pp->p_output = tmpfd; pp->p_death[0] = '\0'; pp->p_fd = newsock; pp->p_mask = (1 << pp->p_fd); # ifndef OLDIPC Fds_mask |= pp->p_mask; if (pp->p_fd >= Num_fds) Num_fds = pp->p_fd + 1; # endif OLDIPC pp->p_y = 0; pp->p_x = 0; # ifdef MONITOR if (monitor) stmonitor(pp); else # endif MONITOR stplayer(pp); }