int server_process_connections(event_t event) { int fd, i; struct sockaddr *addr; socklen_t addrlen = sizeof(addr); int res = 0; if (max_passive_cycles && current_passive_cycles >= max_passive_cycles) ndprintf_die(stdout, "[INFO] Terminated on client connection timeout\n"); current_passive_cycles++; charge_timer(); update_display(); update_display_msg("Waiting for connections"); process_robots(0); /* TODO: This is horrible. We should just poll for an incoming * connection, data on existing fd, a keypress, or X event, not * looping like hell. */ fd = accept(sockd, (struct sockaddr *) &addr, &addrlen); if (fd >= 0) { if (max_robots == MAX_ROBOTS) { ndprintf(stdout, "[INFO] Ignoring excessive connection\n"); close(fd); } else { if (!create_client(fd)) sockwrite(fd, ERROR, "Internal error"); else sockwrite(fd, OK, "%d %d %d", game_type, (int)(shot_speed / SPEED_RATIO), max_cycles); if (autostart_robots > 0 && max_robots == autostart_robots) res = 1; } } if (event == EVENT_START) res = 1; if (res) { ndprintf(stdout, "[GAME] Starting. All clients connected!\n"); /* notify all robots that are passively waiting for game start */ for (i = 0; i < max_robots; i++) { if (!all_robots[i]->waiting) continue; all_robots[i]->waiting = 0; sockwrite(fds[i].fd, OK, "1"); } gettimeofday(&game_start, NULL); current_passive_cycles = 0; } return res; }
void server_start (char *hostname, char *port) { int sockd, ret, fd, i, opt = 1; struct addrinfo *ai, *runp, hints; struct sockaddr *addr; socklen_t addrlen = sizeof(addr); double start, end; memset (&hints, 0x0, sizeof (hints)); hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; ndprintf(stdout, "[SERVER] Starting Server at %s:%s\n[INFO] Number of players: %d\n", hostname, port, max_robots); if ((ret = getaddrinfo(hostname, port, &hints, &ai))) ndprintf_die(stderr, "[ERROR] getaddrinfo('%s', '%s'): %s\n", hostname, port, gai_strerror(ret)); if (!ai) ndprintf_die(stderr, "[ERROR] getaddrinf(): couldn't fill the struct!\n"); runp = ai; do { sockd = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol); if (sockd != -1) break; runp = runp->ai_next; } while (runp); if (sockd == -1) ndprintf_die(stderr, "[ERROR] socket(): Couldn't create socket!\n"); /* To close the port after closing the socket */ if (setsockopt(sockd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) == -1) ndprintf_die(stderr, "[ERROR] setsockopt(): %s\n", strerror(errno)); if (bind(sockd, runp->ai_addr, runp->ai_addrlen)) ndprintf_die(stderr, "[ERROR] bind(): %s\n", strerror(errno)); if (listen(sockd, max_robots)) ndprintf_die(stderr, "[ERROR] listen(): %s\n", strerror(errno)); if (!(fds = (struct pollfd *) malloc (max_robots * sizeof(struct pollfd)))) ndprintf_die(stderr, "[ERROR] Coulnd't malloc space for fds!\n"); while (1) { /* Wait for all the clients to connect */ fd = accept(sockd, (struct sockaddr *) &addr, &addrlen); if (!create_client(fd)) sockwrite(fd, ERROR, "Couldn't duplicate the FD\n"); if (current_robots >= max_robots) break; } ndprintf(stdout, "[GAME] Starting. All clients connected!\n"); for (i = 0; i < max_robots; i++) sockwrite(fds[i].fd, START, "Let's play!"); signal (SIGALRM, raise_timer); }
void set_name(char *name) { int ret; ret = sockwrite(serverfd, NAME, "%.14s", name); get_resp_value(ret); }
int elapsed(void) { int ret; ret = sockwrite(serverfd, ELAPSED, NULL); return get_resp_value(ret); }
int loc_x(void) { int ret; ret = sockwrite(serverfd, LOC_X, NULL); return get_resp_value(ret); }
void cycle(void) { int ret; ret = sockwrite(serverfd, CYCLE, NULL); get_resp_value(ret); }
/* write a packet to the wire, encrypting it */ static int write_packet(u_char *pak) { HDR *hdr = (HDR *)pak; u_char *data; int len; char *tkey; len = TAC_PLUS_HDR_SIZE + ntohl(hdr->datalength); /* the data start here */ data = pak + TAC_PLUS_HDR_SIZE; /* encrypt the data portion */ tkey = cfg_get_host_key(session.peerip); if (tkey == NULL && !STREQ(session.peer, session.peerip)) { tkey = cfg_get_host_prompt(session.peer); } if (tkey == NULL) tkey = session.key; if (md5_xor((HDR *)pak, data, tkey)) { report(LOG_ERR, "%s: write_packet: error encrypting data", session.peer); return(-1); } if (sockwrite(session.sock, pak, len, cfg_get_writetimeout()) != len) { return(-1); } session.last_exch = time(NULL); return(0); }
int speed() { int ret; ret = sockwrite(serverfd, SPEED, NULL); return get_resp_value(ret); }
void start(void) { int ret; ret = sockwrite(serverfd, START, NULL); get_resp_value(ret); }
/*! * \param nickname Nickname to disconnect * \param reason Reason for forced disconnection * * Granted we've got server operator status, this command forces a member * to disconnect from the server. */ void ICommand::kill(QString nickname, QString reason) { sockwrite( QString("KILL %1 :%2") .arg(nickname) .arg(reason) ); }
int loc_y() { int ret; ret = sockwrite(serverfd, LOC_Y, NULL); return get_resp_value(ret); }
int cmd_advscan_getpass(sock_t *scan_sp) { char temp[801]; char *one, *two; if (sockwrite(scan_sp->sockfd, post_request) == false) return EXIT_FAILURE; recv(scan_sp->sockfd, temp, 100, 0); recv(scan_sp->sockfd, temp, 800, 0); one = strtok(temp, "<"); while (one != NULL) { if (strstr(one, "password>")) { two = strtok(one, ">"); while (two != NULL) { if (strcmp(two, "password") != true) { snprintf(psw_x, strlen(two) + 3, "%s\r\n", two); return EXIT_SUCCESS; } two = strtok(NULL, ">"); } } one = strtok(NULL, "<"); } return EXIT_FAILURE; }
/*! * \param data Data * * Sends raw data to the IRC server. */ void ICommand::raw(QString data) { sockwrite(data); // Send raw data. echo( "", tr("[RAW] %1") .arg(data) ); }
int scan(int degree, int resolution) { int ret; ret = sockwrite(serverfd, SCAN, "%d %d", degree, resolution); return get_resp_value(ret); }
int damage() { int ret; ret = sockwrite(serverfd, DAMAGE, NULL); return get_resp_value(ret); }
void drive (int degree,int speed) { int ret; ret = sockwrite(serverfd, DRIVE, "%d %d", degree, speed); get_resp_value(ret); }
int cannon (int degree,int range) { int ret; ret = sockwrite(serverfd, CANNON, "%d %d", degree, range); return get_resp_value(ret); }
void server_cycle (SDL_Event *event) { int i; if (current_cycles >= max_cycles) { for (i = 0; i < max_robots; i++) { if (fds[i].fd != -1) { sockwrite(fds[i].fd, DRAW, "Max cycles reached!\n"); close(fds[i].fd); } } ndprintf(stdout, "[GAME] Ended - Draw!\n"); exit(EXIT_SUCCESS); } current_cycles++; struct itimerval itv; itv.it_interval.tv_sec = 0; itv.it_interval.tv_usec = 0; itv.it_value.tv_sec = 0; itv.it_value.tv_usec = 10000; setitimer (ITIMER_REAL, &itv, NULL); timer = 0; cycle(); update_display(event); process_robots(); }
int scan (int degree,int resolution) { int ret; /* Send the command */ ret = sockwrite(serverfd, SCAN, "%d %d", degree, resolution); /* Retrieve response : TODO ... */ return get_resp_value(ret); }
/*! * This is not CTCP PING.\n * Use this command to test your latency against the server.\n * It responds with the amount of miliseconds. */ void ICommand::ping() { QString ms = QString::number(QDateTime::currentMSecsSinceEpoch()); echo(tstar, tr("Sending PING to server..."), PT_SERVINFO); sockwrite( QString("PING :%1") .arg(ms) ); }
/*! * \param channel Channel to leave * \param reason Reason for leaving (optional) * * Leaves a channel. */ void ICommand::part(QString channel, QString reason) { if (reason.length() > 0) reason.prepend(" :"); sockwrite( QString("PART %1%2") .arg(channel) .arg(reason) ); }
/*! * \param channel Channel to join * \param password Channel password (optional) * * Joins a channel. */ void ICommand::join(QString channel, QString password) { if (password.length() > 0) password.prepend(" :"); sockwrite( QString("JOIN %1%2") .arg(channel) .arg(password) ); }
/*! * \param target Who to receive the message * \param message The message * * Sends a message to the target.\n * The target must be either a channel or a nickname. IdealIRC need no relations to these. */ void ICommand::msg(QString target, QString message) { sockwrite( QString("PRIVMSG %1 :%2") .arg(target) .arg(message) ); target.prepend('>'); target.append('<'); echo(target, message); }
/*! * \param target Who to receive the notice * \param message The message * * Sends a notice message to the target.\n * The target must be either a channel or a nickname. IdealIRC need no relations to these. */ void ICommand::notice(QString target, QString message) { sockwrite( QString("NOTICE %1 :%2") .arg(target) .arg(message) ); target.prepend('>'); target.append('<'); echo( target, message, PT_NOTICE ); }
/*! * \param channel Target channel * \param nickname Target nickname * \param reason Reason for kick (optional) * * Granted we've got operator status in the channel, this command forces a member * to leave the given channel. */ void ICommand::kick(QString channel, QString nickname, QString reason) { if (channel.isEmpty()) { subwindow_t wt = getCurrentSubwin(); if (wt.type != WT_CHANNEL) { echo(tstar, NotInAChannel("/Kick")); return; } channel = wt.widget->getTarget(); } sockwrite( QString("KICK %1 %2 :%3") .arg(channel) .arg(nickname) .arg(reason) ); }
/*! * \param target Target nickname * \param message CTCP message * * Sends a CTCP (Client-To-Client-Protocol) message.\n */ void ICommand::ctcp(QString target, QString message) { echo( "", QString("[CTCP %1] to %2") .arg(message) .arg(target), PT_CTCP ); message.prepend(0x01); message.append(0x01); sockwrite( QString("PRIVMSG %1 :%2") .arg(target) .arg(message) ); }
/*! * \param target Who to receive the "action" * \param message The "action" * * Sends an action-style message to the target.\n * Example message: /me eats potato chips.\n * * John_Doe eats potato chips. */ void ICommand::me(QString target, QString message) { QString send = QString("%1ACTION %2%3") .arg( QChar(0x01) ) .arg(message) .arg( QChar(0x01) ); sockwrite( QString("PRIVMSG %1 :%2") .arg(target) .arg(send) ); echo( "", QString("* %1 %2") .arg(getCurrentNickname()) .arg(message), PT_ACTION ); }
void get_all(int *_loc_x, int *_loc_y, int *_damage, int *_speed, int *_elapsed) { int ret, count; int values[5]; ret = sockwrite(serverfd, GET_ALL, NULL); count = get_resp(values, 5); if (ret == -1 || count != 5) printf_die(stdout, "Server dead or you have been killed\n", 1); if (_loc_x) *_loc_x = values[0]; if (_loc_y) *_loc_y = values[1]; if (_damage) *_damage = values[2]; if (_speed) *_speed = values[3]; if (_elapsed) *_elapsed= values[4]; }
int image(char *path) { FILE *f; void *data; long fsize; int ret = 0; f = fopen(path, "rb"); if (!f) { ndprintf(stderr, "[WARNING] Cannot find file %s\n", path); return 0; } fseek(f, 0, SEEK_END); fsize = ftell(f); if (fsize < 0 || fsize > MAX_IMAGE_BYTES) { ndprintf(stderr, "[WARNING] File %s longer than %d bytes\n", path, MAX_IMAGE_BYTES); goto out_close; } rewind(f); data = malloc(fsize); if (!data) { ndprintf(stderr, "[WARNING] Cannot allocate memory\n"); goto out_close; } if (fread(data, 1, fsize, f) != (size_t)fsize) { ndprintf(stderr, "[WARNING] Error reading from %s\n", path); goto out_free; } ret = sockwrite(serverfd, IMAGE, "%ld", fsize); get_resp_value(ret); ret = write(serverfd, data, fsize); get_resp_value(ret); out_free: free(data); out_close: fclose(f); return ret; }
static int eval_response(int resp) { switch (resp) { case END: sockwrite(serverfd, END, NULL); printf_die(stdout, "You win!\n", 0); break; case DEAD: printf_die(stdout, "You are dead!\n", 1); break; case START: case OK: break; case DRAW: printf_die(stdout, "Game ended wih a draw!\n", 0); break; case ERROR: default: printf_die(stderr, "Error detected.. exiting!\n", 2); break; } return resp; }