00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 #include <stdio.h>
00085 #include <stdlib.h>
00086 #include <errno.h>
00087 #include <sys/types.h>
00088 #include <sys/socket.h>
00089 #include <netinet/in.h>
00090 #include <sys/time.h>
00091 #include <netdb.h>
00092 #include <string.h>
00093 #include <unistd.h>
00094 #include "udanax.h"
00095 #include "players.h"
00096 #include "port.h"
00097
00098 #define ERROR -1
00099 #define typerequest int
00100 #define MAX_PLAYERS 25
00101
00102
00103 extern char hostname[];
00104 extern int portname;
00105
00106 int fdtoplayer[32];
00107 Tumbler defaultaccount = { 0, 0, 0, 0, { 1, 1, 0, 14, 0, 0, 0, 0, 0, 0, 0 } };
00108
00109 PLAYER player[MAX_PLAYERS];
00110 int n_players = 0;
00111 fd_set inputfds;
00112 int nfds = 0;
00113
00114 int main_socket = ERROR;
00115
00116 extern bool establishprotocol(FILE *inp, FILE *outp);
00117
00125 void
00126 new_players(
00127 PLAYER player[],
00128 int *n_playersp,
00129 int block,
00130 Session *sess)
00131
00132
00133
00134 {
00135 int s;
00136 int rc;
00137 struct sockaddr_in from;
00138 socklen_t fromlen = sizeof(from);
00139 struct timeval t;
00140
00141
00142
00143
00144
00145 int open_sock();
00146
00147 if (main_socket == ERROR)
00148 main_socket = open_sock();
00149
00150
00151
00152 t.tv_sec = 0L;
00153 t.tv_usec = 3L;
00154
00155 for (;;) {
00156 fd_set readfds;
00157 FD_ZERO(&readfds);
00158 FD_SET(main_socket, &readfds);
00159
00160
00161 t.tv_sec = 0L;
00162 t.tv_usec = 3L;
00163 if ((rc = select(32, &readfds, 0, 0, &t)) == -1) {
00164 perror("new_players:select");
00165 fflush(stdout);
00166 break;
00167 }
00168 if (rc > 0 || block) {
00169 if ((s = accept(main_socket, (sockaddr *) &from, &fromlen)) < 0) {
00170 perror("new_players:accept");
00171 fflush(stdout);
00172 break;
00173 }
00174 if (*n_playersp >= MAX_PLAYERS) {
00175 L("TOOMANY frontends: won't log another one\n");
00176 close(s);
00177 } else {
00178 block = false;
00179 #ifndef DISTRIBUTION
00180 L("accepted connection from %d ", s);
00181 #endif
00182
00183
00184 player[*n_playersp].socket = s;
00185 if ((player[*n_playersp].inp = fdopen(s, "r")) == NULL) {
00186 perror("fdopen(r)");
00187 break;
00188 }
00189 if ((sess->outp = player[*n_playersp].outp = fdopen(s, "w")) == NULL) {
00190 perror("fdopen(w)");
00191 break;
00192 }
00193 sess->inp = player[*n_playersp].inp;
00194
00195 #ifndef DISTRIBUTION
00196 L("%s\n", (char *) &(player[*n_playersp].account));
00197 #endif
00198 if (!establishprotocol(sess->inp, sess->outp)) {
00199 break;
00200 }
00201
00202
00203
00204 player[*n_playersp].wantsout = false;
00205
00206 fdtoplayer[s] = *n_playersp;
00207 FD_SET(s, &inputfds);
00208 nfds = max(s, nfds);
00209 (*n_playersp)++;
00210 break;
00211 }
00212 } else
00213 break;
00214 }
00215 }
00216
00224 void
00225 leave(
00226 PLAYER player[],
00227 int *xn_players)
00228 {
00229 register int i;
00230
00231 for (i = 0; i < *xn_players; i++)
00232 if (player[i].wantsout) {
00233 FD_CLR(player[i].socket, &inputfds);
00234
00235
00236 if (close(player[i].socket) != 0) {
00237 L("user %d: ", i);
00238 perror("close player.socket");
00239 }
00240 if (*xn_players > 1) {
00241 player[i] = player[*xn_players - 1];
00242 }
00243 (*xn_players)--;
00244 --i;
00245 }
00246 }
00247
00255 int
00256 open_sock()
00257 {
00258 int s;
00259 struct sockaddr_in sockaddr;
00260
00261
00262 if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
00263 perror("socket()");
00264 exit(1);
00265 }
00266
00267
00268
00269 sockaddr.sin_family = AF_INET;
00270 sockaddr.sin_port = htons(portname);
00271 sockaddr.sin_addr.s_addr = INADDR_ANY;
00272
00273 L("calling bind s = %d \n", s);
00274
00275 if (bind(s, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) < 0) {
00276 perror("bind()");
00277 exit(1);
00278 }
00279 if (listen(s, 0) < 0) {
00280 perror("listen()");
00281 exit(1);
00282 }
00283 return (s);
00284 }
00285
00293 void
00294 crash(
00295 int )
00296 {
00297 int i;
00298
00299 L("CRASH while dealing with user %d\n", user);
00300 for (i = 0; i < 32; i++)
00301 close(i);
00302 exit(9);
00303 }
00304
00312 bool
00313 isthisusersdocument(
00314 Session *sess,
00315 Tumbler *tp)
00316 {
00317 return tumbleraccounteq(tp, &(player[user].account));
00318 }