Exemple #1
0
void LocalClient::ListenTaskThread::update_positions() {
	command_arg_mask_union cmd_arg_mask;
	copy_from_buffer(&cmd_arg_mask, PTCL_CMD_ARG_FIELD);
	unsigned num_clients = cmd_arg_mask.ch[1];

	static const size_t serial_data_size = sizeof(Car().data_serial);
	static const size_t PTCL_POS_DATA_SIZE = sizeof(unsigned short) + serial_data_size;

	//size_t total_size = PTCL_HEADER_LENGTH;

	for (unsigned i = 0; i < num_clients; ++i) {
		unsigned short id;
		copy_from_buffer(&id, sizeof(id), PTCL_HEADER_LENGTH + i*PTCL_POS_DATA_SIZE);
		auto it = peers.find(id);
		
		if (it == peers.end()) {
			PRINT("update_positions: unknown peer id included in peer list (%u)\n", id);
			// request another peer list, reconstruct etc.
		}
		else {
			size_t offset = PTCL_HEADER_LENGTH + i*PTCL_POS_DATA_SIZE + sizeof(id);
			peers.update_peer_data(it, buffer + offset, serial_data_size);
		}
	}
	LocalClient::posupd_timer.begin();	

}
Exemple #2
0
void showmotd(char *source)
{
  struct buffer_block *dyn = NULL;
  char buffer[512];
  int fd, l;

  alarm(3);
  fd = open(MOTD_FILE, O_RDONLY);
  if (fd < 0)
  {
    notice(source, "MOTD is empty");
    return;
  }

  if (CurrentSendQ > HIGHSENDQTHRESHOLD)
  {
    notice(source, "Cannot process your request at this time. Try again later.");
    return;
  }

  while ((l = read(fd, buffer, 511)) > 0)
  {
    copy_to_buffer(&dyn, buffer, l);
  }
  close(fd);
  alarm(0);

  while (dyn != NULL)
  {
    char *ptr;
    copy_from_buffer(&dyn, buffer, '\n', 199);
    if ((ptr = strchr(buffer, '\n')) != NULL)
      *ptr = '\0';
    else
      continue;
    if (*buffer == '\0')
      strcpy(buffer, " ");
    notice(source, buffer);
  }
}
Exemple #3
0
void http_show_help(http_socket * hsock, char *command)
{
  char buffer[512], buffer2[200];
  struct buffer_block *dyn = NULL;
  register char *ptr, *ptr2;
  register int i, index = 0, found = 0;
  int file, l;

  listinit();

  /* find the command index */
  for (i = 0; i <= N; i++)
  {
    if (!strcasecmp(command, commands[i].name))
    {
      found = 1;
      index = i;
      break;
    }
    else if (!strncasecmp(command, commands[i].name, strlen(command)))
    {
      found++;
      index = i;
    }
  }

  buffer[0] = '\0';

  sendto_http(hsock, HTTP_HEADER, command);
  sendto_http(hsock, HTTP_BODY);

  sendto_http(hsock, "<H1>HELP ON %s</H1>\n", (*command) ? command : "COMMANDS");
  sendto_http(hsock, "<PRE>\n");

  if (found > 1)
  {
    if (*command)
      sendto_http(hsock, "%s is ambiguous\n", command);
  }
  else if (found == 0)
  {
    if (!strcasecmp(command, "INFO"))
    {
      sprintf(buffer, "%s/INFO", HELP_DIR);
    }
    else if (!strcasecmp(command, "FORM"))
    {
      sprintf(buffer, "%s/FORM", HELP_DIR);
    }
    else
    {
      sendto_http(hsock, "No help on %s.\n", command);
    }
  }
  else
  {
    sprintf(buffer, "%s/%s", HELP_DIR, commands[index].file);
  }

  if (buffer[0] != '\0')
  {
    alarm(2);
    file = open(buffer, O_RDONLY);
    if (file < 0)
    {
      alarm(0);
      if (found)
	sendto_http(hsock, "The help file for command %s "
	  "is not available\n", commands[index].name);
      else
	sendto_http(hsock, "This help file is not available\n");
    }
    else
    {
      alarm(2);
      while ((l = read(file, buffer, 511)) > 0)
      {
	copy_to_buffer(&dyn, buffer, l);
      }
      alarm(0);
      close(file);

      while (dyn != NULL)
      {
	copy_from_buffer(&dyn, buffer, '\n', 199);
	if ((ptr = strchr(buffer, '\n')) == NULL)
	  continue;
	if ((ptr = strstr(buffer, "$NICK")) != NULL)
	{
	  *ptr = '\0';
	  sprintf(buffer2, "%s%s%s", buffer, mynick, ptr + 5);
	  strcpy(buffer, buffer2);
	}

	/* Some nasty char quoting required.. */
	ptr = buffer;
	ptr2 = buffer2;
	do
	{
	  if (*ptr == '<')
	  {
	    ptr2[0] = '&';
	    ptr2[1] = 'l';
	    ptr2[2] = 't';
	    ptr2[3] = ';';
	    ptr2 += 4;
	  }
	  else if (*ptr == '>')
	  {
	    ptr2[0] = '&';
	    ptr2[1] = 'g';
	    ptr2[2] = 't';
	    ptr2[3] = ';';
	    ptr2 += 4;
	  }
	  else
	  {
	    ptr2[0] = ptr[0];
	    ptr2++;
	  }
	}
	while (*(ptr++) != '\0');
	sendto_http(hsock, "%s", buffer2);
      }
    }
    sendto_http(hsock, "</PRE>\n");
  }
  else
  {	/* show list of commands */
    sendto_http(hsock, "</PRE>\n<H2>List of commands:</H2>\n");
    sendto_http(hsock, "<FONT SIZE=+1>\n");
    for (i = 0; i < N + 1; i++)
    {
      if (commands[i].Access <= 500)
	sendto_http(hsock, "<A HREF=\"/HELP/%s\">%s</A>\n",
	  commands[i].name, commands[i].name);
    }
    sendto_http(hsock, "</FONT><P>\n");
  }

  sendto_http(hsock, "<HR>%s\n", HTTP_FOOTER);
  sendto_http(hsock, "</BODY></HTML>\n");
  hsock->status = HTTP_ENDING;
}
Exemple #4
0
void showhelp(char *source, char *chan, char *args)
{
  struct buffer_block *dyn = NULL;
  char buffer[512], word[80], *ptr;
  int i, l, index = 0, found = 0, file, linecount = 0;

  if (CurrentSendQ > HIGHSENDQTHRESHOLD)
  {
    notice(source, "Cannot process your request at this time. Try again later.");
    return;
  }

  GetWord(0, args, word);
  if (!*word)
    strcpy(word, "help");

  listinit();

  /* find the command index */
  for (i = 0; i <= N; i++)
  {
    if (!strcasecmp(word, commands[i].name))
    {
      found = 1;
      index = i;
      break;
    }
    else if (!strncasecmp(word, commands[i].name, strlen(word)))
    {
      found++;
      index = i;
    }
  }

  if (found > 1)
  {
    sprintf(buffer, "%s is ambiguous", word);
    notice(source, buffer);
    return;
  }
  else if (found == 0)
  {
    if (!strcasecmp(word, "INFO"))
    {
      sprintf(buffer, "%s/INFO", HELP_DIR);
    }
    else if (!strcasecmp(word, "FORM"))
    {
      sprintf(buffer, "%s/FORM", HELP_DIR);
    }
    else
    {
      sprintf(buffer, "No help on %s. Please use "
	"showcommands to get a list of commands "
	"available to you", word);
      notice(source, buffer);
      return;
    }
  }
  else
  {
    sprintf(buffer, "%s/%s", HELP_DIR, commands[index].file);
  }

  alarm(2);
  file = open(buffer, O_RDONLY);
  alarm(0);
  if (file < 0)
  {
    if (found)
      sprintf(buffer, "The help file for command %s "
	"is not available", commands[index].name);
    else
      sprintf(buffer, "This help file is not available");

    notice(source, buffer);
    return;
  }

  if (found)
  {
    sprintf(buffer,
      " HELP on %-20s               Minimum access: %4d ",
      commands[index].name, commands[index].Access);
    notice(source, buffer);
  }

  alarm(3);
  while ((l = read(file, buffer, 511)) > 0)
  {
    copy_to_buffer(&dyn, buffer, l);
  }
  alarm(0);
  close(file);

  while (dyn != NULL)
  {
    copy_from_buffer(&dyn, buffer, '\n', 199);
    if ((ptr = strchr(buffer, '\n')) != NULL)
      *ptr = '\0';
    else
      continue;
    if (*buffer == '\0')
      strcpy(buffer, " ");
    string_swap(buffer, 512, "$NICK", mynick);
    string_swap(buffer, 512, "$SERVER", SERVERNAME);
    notice(source, buffer);
    linecount++;
  }

  if (linecount > 0)
    CheckFloodFlood(source, linecount);
}
Exemple #5
0
void LocalClient::ListenTaskThread::handle_current_packet() {

	PTCLHEADERDATA header;
	protocol_get_header_data(buffer, &header);

	if (header.protocol_id != PROTOCOL_ID) {
		PRINT( "dropping packet. Reason: protocol_id mismatch (%d)\n", header.protocol_id);
		return;
	}

	if (header.sender_id != ID_SERVER) {
		//PRINT( "unexpected sender id. expected ID_SERVER (%x), got %x instead.\n", ID_SERVER, sender_id);
		return;
	}

	const unsigned char &cmd = header.cmd_arg_mask.ch[0];

	switch (cmd) {

		case S_POSITION_UPDATE:
		// don't apply older pupd-package data. we also have an interpolation mechanism :P
			if (header.seq_number > latest_posupd_seq_number) {
				update_positions();
				latest_posupd_seq_number = header.seq_number;
			}
			break;

		case S_PONG: {
			latency_ms = LocalClient::Ping.time_since_last_ping_ms();
			unsigned embedded_seq_number;
			copy_from_buffer(&embedded_seq_number, sizeof(embedded_seq_number), PTCL_HEADER_LENGTH);
			if (embedded_seq_number != Ping._latest_ping_seq_number) {
				PRINT("Received S_PONG from server, but the embedded seq_number (%u) doesn't match with the expected one (%d).\n", embedded_seq_number, Ping._latest_ping_seq_number);
			}
			Ping._latest_ping_seq_number = 0; 
			break;
		}
		case S_CLIENT_CHAT_MESSAGE: {
			// the sender id is embedded into the datafield (bytes 12-14) of the packet
			unsigned short sender_id = 0;
			copy_from_buffer(&sender_id, sizeof(sender_id), PTCL_HEADER_LENGTH);
			auto it = peers.find(sender_id);
			if (it != peers.end()) {
			std::string chatmsg(buffer + PTCL_HEADER_LENGTH + sizeof(sender_id));
				if (!Server::is_running()) {
				PRINT("[%s] <%s>: %s\n", get_timestamp().c_str(), it->second.info.name.c_str(), chatmsg.c_str());
				}
			}
				else {
				// perhaps request a new peer list from the server. :P
				PRINT("warning: server broadcast S_CLIENT_CHAT_MESSAGE with unknown sender id %u!\n", sender_id);
			}
			break;
		}

		case S_SHUTDOWN:
			PRINT( "Client: Received S_SHUTDOWN from server (server going down).\n");
			LocalClient::request_shutdown(); // this will break from the recvfrom loop gracefully
			// this will cause LocalClient::stop() to be called from the main (rendering) thread
			break;

		case C_TERMINATE:
			PRINT("Client:Received C_TERMINATE from self. Stopping.\n");
			LocalClient::request_shutdown();
			break;
	
		case S_PEER_LIST:
			construct_peer_list();
		break;

		case S_CLIENT_DISCONNECT: {
			unsigned short id;
			copy_from_buffer(&id, sizeof(id), PTCL_HEADER_LENGTH);
			auto it = peers.find(id);
			if (it == peers.end()) {
				//PRINT( "Warning: received S_CLIENT_DISCONNECT with unknown id %u.\n", id);
			}
			else {
				PRINT( "Client %s (id %u) disconnected.\n", it->second.info.name.c_str(), id);
				peers.erase(id);
			}
			break;
		}
	
		default:
			PRINT("Warning: received unknown command char %u from server.\n", cmd);
			break;
	}

}