<commit_before>/*
 * qstat 2.8
 * by <NAME>
 *
 * New Half-Life2 query protocol
 * Copyright 2005 <NAME>
 *
 * Licensed under the Artistic License, see LICENSE.txt for license terms
 *
 */

#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#include <stdlib.h>
#include <stdio.h>

#include "debug.h"
#include "qstat.h"
#include "packet_manip.h"

#define A2S_GETCHALLENGE      "\xFF\xFF\xFF\xFF\x57"
#define A2S_CHALLENGERESPONSE 0x41
#define A2S_INFO              "\xFF\xFF\xFF\xFF\x54Source Engine Query"
#define A2S_INFORESPONSE_HL1  0x6D
#define A2S_INFORESPONSE_HL2  0x49
#define A2S_PLAYER            "\xFF\xFF\xFF\xFF\x55"
#define A2S_PLAYERRESPONSE    0x44
#define A2S_RULES             "\xFF\xFF\xFF\xFF\x56"
#define A2S_RULESRESPONSE     0x45

struct a2s_status
{
    unsigned sent_challenge : 1;
    unsigned have_challenge : 1;
    unsigned sent_info : 1;
    unsigned have_info : 1;
    unsigned sent_player : 1;
    unsigned have_player : 1;
    unsigned sent_rules : 1;
    unsigned have_rules : 1;
    unsigned challenge;
};

void send_a2s_request_packet(struct qserver *server)
{
    struct a2s_status* status = (struct a2s_status*)server->master_query_tag;

    if(qserver_send_initial(server, A2S_INFO, sizeof(A2S_INFO)) == -1)
	goto error;

    status->sent_info = 1;

    if(get_server_rules || get_player_info)
	server->next_rule = ""; // trigger calling send_a2s_rule_request_packet

    return;

error:
    cleanup_qserver(server, 1);
}

void send_a2s_rule_request_packet(struct qserver *server)
{
    struct a2s_status* status = (struct a2s_status*)server->master_query_tag;

    if(!get_server_rules && !get_player_info)
    {
	goto error;
    }

    do
    {
	if(!status->have_challenge)
	{
	    if(qserver_send_initial(server, A2S_GETCHALLENGE, sizeof(A2S_GETCHALLENGE)-1) == -1)
		goto error;
	    status->sent_challenge = 1;
	    break;
	}
	else if(get_server_rules && !status->have_rules)
	{
	    char buf[sizeof(A2S_RULES)-1+4] = A2S_RULES;
	    memcpy(buf+sizeof(A2S_RULES)-1, &status->challenge, 4);
	    if(qserver_send_initial(server, buf, sizeof(buf)) == -1)
		goto error;
	    status->sent_rules = 1;
	    break;
	}
	else if(get_player_info && !status->have_player)
	{
	    char buf[sizeof(A2S_PLAYER)-1+4] = A2S_PLAYER;
	    memcpy(buf+sizeof(A2S_PLAYER)-1, &status->challenge, 4);
	    if(qserver_send_initial(server, buf, sizeof(buf)) == -1)
		goto error;
	    status->sent_player = 1;
	    break;
	}
	else
	{
	    // we are probably called due to timeout, restart.<commit_msg>add some debugging code<commit_after>/*
 * qstat 2.8
 * by <NAME>
 *
 * New Half-Life2 query protocol
 * Copyright 2005 <NAME>
 *
 * Licensed under the Artistic License, see LICENSE.txt for license terms
 *
 */

#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif
#include <stdlib.h>
#include <stdio.h>

#include "debug.h"
#include "qstat.h"
#include "packet_manip.h"

#define A2S_GETCHALLENGE      "\xFF\xFF\xFF\xFF\x57"
#define A2S_CHALLENGERESPONSE 0x41
#define A2S_INFO              "\xFF\xFF\xFF\xFF\x54Source Engine Query"
#define A2S_INFORESPONSE_HL1  0x6D
#define A2S_INFORESPONSE_HL2  0x49
#define A2S_PLAYER            "\xFF\xFF\xFF\xFF\x55"
#define A2S_PLAYERRESPONSE    0x44
#define A2S_RULES             "\xFF\xFF\xFF\xFF\x56"
#define A2S_RULESRESPONSE     0x45

struct a2s_status
{
    unsigned sent_challenge : 1;
    unsigned have_challenge : 1;
    unsigned sent_info : 1;
    unsigned have_info : 1;
    unsigned sent_player : 1;
    unsigned have_player : 1;
    unsigned sent_rules : 1;
    unsigned have_rules : 1;
    unsigned challenge;
};

void send_a2s_request_packet(struct qserver *server)
{
    struct a2s_status* status = (struct a2s_status*)server->master_query_tag;

    if(qserver_send_initial(server, A2S_INFO, sizeof(A2S_INFO)) == -1)
	goto error;

    status->sent_info = 1;

    if(get_server_rules || get_player_info)
	server->next_rule = ""; // trigger calling send_a2s_rule_request_packet

    return;

error:
    cleanup_qserver(server, 1);
}

void send_a2s_rule_request_packet(struct qserver *server)
{
    struct a2s_status* status = (struct a2s_status*)server->master_query_tag;

    if(!get_server_rules && !get_player_info)
    {
	goto error;
    }

    do
    {
	if(!status->have_challenge)
	{
	    debug(3, "sending challenge");
	    if(qserver_send_initial(server, A2S_GETCHALLENGE, sizeof(A2S_GETCHALLENGE)-1) == -1)
		goto error;
	    status->sent_challenge = 1;
	    break;
	}
	else if(get_server_rules && !status->have_rules)
	{
	    char buf[sizeof(A2S_RULES)-1+4] = A2S_RULES;
	    memcpy(buf+sizeof(A2S_RULES)-1, &status->challenge, 4);
	    debug(3, "sending rule query");
	    if(qserver_send_initial(server, buf, sizeof(buf)) == -1)
		goto error;
	    status->sent_rules = 1;
	    break;
	}
	else if(get_player_info && !status->have_player)
	{
	    char buf[sizeof(A2S_PLAYER)-1+4] = A2S_PLAYER;
	    memcpy(buf+sizeof(A2S_PLAYER)-1, &status->challenge, 4);
	    debug(3, "sending player query");
	    if(qserver_send_initial(server, buf, sizeof(buf)) == -1)
		goto error;
	    status->sent_player = 1;
	    break;
	}
	else
	{
	    debug(3, "timeout");
	    // we are probably called due to timeout, restart.
{'fraction_non_alphanumeric': 0.12073591414334994, 'fraction_numerical': 0.03372939823687236, 'mean_word_length': 3.598237885462555, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6446562', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2004, 'n_words': 399}