Exemple #1
0
/*
* BEE::net_session_end() - End the session connection
*/
int BEE::net_session_end() {
    if (net->is_host) { // If we are the host
        for (auto& p : net->players) { // Iterate over the clients to disconnect them
            network_udp_send(p.second, net->channel, net->self_id, 2, 0); // Send a disconnection signal
            network_udp_close(&p.second); // Close the socket
        }

        // Reset connection info
        net->is_host = false;
    } else {
        network_udp_send(net->udp_send, net->channel, net->self_id, 2, net->self_id); // Send a disconnection signal to the server
    }

    // Close sockets and free data
    network_udp_close(&net->udp_send);
    network_udp_close(&net->udp_recv);
    network_packet_free(net->udp_data);

    // Reset connection and client info
    net->is_connected = false;
    net->servers.clear();
    net->name.clear();
    net->players.clear();
    net->data.clear();

    return 0; // Return 0 on success
}
int network_udp_send(UDPsocket udp, int channel, Uint8* data) {
	UDPpacket* d = network_packet_alloc(data[0]);
	d->data = data;

        int r = network_udp_send(udp, channel, d);

        network_packet_free(d);

        return r;
}
Exemple #3
0
/*
* network_udp_send() - Send data via UDP over the given channel
* ! When the function is called without a packet, simply call it with a temporary one
* @udp: the socket to send the data through
* @channel: the channel to use
* @data: the data to send as a packet
*/
int network_udp_send(UDPsocket udp, int channel, Uint8* data) {
	UDPpacket* d = network_packet_alloc(data[0]); // Allocate space for the data packet
	d->data = data; // Set the packet data

	int r = network_udp_send(udp, channel, d); // Send the packet

	network_packet_free(d); // Free the packet

	return r; // Return the amount of destinations the data was sent to on success, or -1 on failure
}
void t_ok_packet_append(void) {
	network_mysqld_ok_packet_t *ok_packet;
	network_packet *packet;

	ok_packet = network_mysqld_ok_packet_new();
	packet = network_packet_new();
	packet->data = g_string_new(NULL);

	/* check if a empty ok-packet is encoded correctly */
	g_assert_cmpint(0, ==, network_mysqld_proto_append_ok_packet(packet->data, ok_packet));
	g_assert_cmpint(7, ==, packet->data->len);
	g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\x00\x00\x00\x00\x00\x00\x00")));

	g_assert_cmpint(0, ==, network_mysqld_proto_get_ok_packet(packet, ok_packet));

	/* check if encoding and decoding works */
	ok_packet->warnings = 1;
	ok_packet->server_status = 2;
	ok_packet->insert_id = 3;
	ok_packet->affected_rows = 4;

	g_string_truncate(packet->data, 0);
	packet->offset = 0;

	g_assert_cmpint(0, ==, network_mysqld_proto_append_ok_packet(packet->data, ok_packet));
	g_assert_cmpint(7, ==, packet->data->len);
	g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\x00\x04\x03\x02\x00\x01\x00")));
	
	network_mysqld_ok_packet_free(ok_packet);

	ok_packet = network_mysqld_ok_packet_new();
	g_assert_cmpint(0, ==, network_mysqld_proto_get_ok_packet(packet, ok_packet));
	g_assert_cmpint(1, ==, ok_packet->warnings);
	g_assert_cmpint(2, ==, ok_packet->server_status);
	g_assert_cmpint(3, ==, ok_packet->insert_id);
	g_assert_cmpint(4, ==, ok_packet->affected_rows);

	network_mysqld_ok_packet_free(ok_packet);

	/* check if too-short packet is denied */
	ok_packet = network_mysqld_ok_packet_new();
	g_string_truncate(packet->data, 0);
	packet->offset = 0;
	g_assert_cmpint(-1, ==, network_mysqld_proto_get_ok_packet(packet, ok_packet));

	network_mysqld_ok_packet_free(ok_packet);

	g_string_free(packet->data, TRUE);
	network_packet_free(packet);
}
void t_err_packet_append(void) {
	network_mysqld_err_packet_t *err_packet;
	network_packet *packet;

	err_packet = network_mysqld_err_packet_new();
	packet = network_packet_new();
	packet->data = g_string_new(NULL);

	/* check if a empty ok-packet is encoded correctly */
	g_assert_cmpint(0, ==, network_mysqld_proto_append_err_packet(packet->data, err_packet));
	g_assert_cmpint(9, ==, packet->data->len);
	g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xff\x00\x00#07000")));

	g_assert_cmpint(0, ==, network_mysqld_proto_get_err_packet(packet, err_packet));

	/* check if encoding and decoding works */
	err_packet->errcode = 3;
	g_string_assign_len(err_packet->errmsg, C("test"));
	g_string_assign_len(err_packet->sqlstate, C("01234"));

	g_string_truncate(packet->data, 0);
	packet->offset = 0;

	g_assert_cmpint(0, ==, network_mysqld_proto_append_err_packet(packet->data, err_packet));
	g_assert_cmpint(13, ==, packet->data->len);
	g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xff\x03\x00#01234test")));
	
	network_mysqld_err_packet_free(err_packet);

	err_packet = network_mysqld_err_packet_new();
	g_assert_cmpint(0, ==, network_mysqld_proto_get_err_packet(packet, err_packet));
	g_assert_cmpint(3, ==, err_packet->errcode);
	g_assert_cmpstr("01234", ==, err_packet->sqlstate->str);
	g_assert_cmpstr("test", ==, err_packet->errmsg->str);

	network_mysqld_err_packet_free(err_packet);

	/* check if too-short packet is denied */
	err_packet = network_mysqld_err_packet_new();
	g_string_truncate(packet->data, 0);
	packet->offset = 0;
	g_assert_cmpint(-1, ==, network_mysqld_proto_get_err_packet(packet, err_packet));

	network_mysqld_err_packet_free(err_packet);

	g_string_free(packet->data, TRUE);
	network_packet_free(packet);
}
void t_eof_packet_append(void) {
	network_mysqld_eof_packet_t *eof_packet;
	network_packet *packet;

	eof_packet = network_mysqld_eof_packet_new();
	packet = network_packet_new();
	packet->data = g_string_new(NULL);

	/* check if a empty ok-packet is encoded correctly */
	g_assert_cmpint(0, ==, network_mysqld_proto_append_eof_packet(packet->data, eof_packet));
	g_assert_cmpint(5, ==, packet->data->len);
	g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xfe\x00\x00\x00\x00")));

	g_assert_cmpint(0, ==, network_mysqld_proto_get_eof_packet(packet, eof_packet));

	/* check if encoding and decoding works */
	eof_packet->warnings = 1;
	eof_packet->server_status = 2;

	g_string_truncate(packet->data, 0);
	packet->offset = 0;

	g_assert_cmpint(0, ==, network_mysqld_proto_append_eof_packet(packet->data, eof_packet));
	g_assert_cmpint(5, ==, packet->data->len);
	g_assert_cmpint(TRUE, ==, g_memeq(S(packet->data), C("\xfe\x01\x00\x02\x00")));
	
	network_mysqld_eof_packet_free(eof_packet);

	eof_packet = network_mysqld_eof_packet_new();
	g_assert_cmpint(0, ==, network_mysqld_proto_get_eof_packet(packet, eof_packet));
	g_assert_cmpint(1, ==, eof_packet->warnings);
	g_assert_cmpint(2, ==, eof_packet->server_status);

	network_mysqld_eof_packet_free(eof_packet);

	/* check if too-short packet is denied */
	eof_packet = network_mysqld_eof_packet_new();
	g_string_truncate(packet->data, 0);
	packet->offset = 0;
	g_assert_cmpint(-1, ==, network_mysqld_proto_get_eof_packet(packet, eof_packet));

	network_mysqld_eof_packet_free(eof_packet);

	g_string_free(packet->data, TRUE);
	network_packet_free(packet);
}
/**
 * @test 
 *   network_mysqld_masterinfo_get() can decode a protocol string and
 *   network_mysqld_masterinfo_append() can encode the internal structure and 
 *     turns it back into the orignal string
 */
void t_masterinfo_get(void) {
#define PACKET "15\nhostname-bin.000024\n2143897\n127.0.0.1\nroot\n123\n3306\n60\n0\n\n\n\n\n\n0\n"
	network_mysqld_masterinfo_t *info;
	network_packet *packet;
	GString *s;

	info = network_mysqld_masterinfo_new();
	g_assert(info);

	packet = network_packet_new();
	packet->data = g_string_new_len(C(PACKET));
	packet->offset = 0;

	g_assert_cmpint(network_mysqld_masterinfo_get(packet, info), !=, -1);

	g_assert_cmpstr(info->master_log_file->str, ==, "hostname-bin.000024");
	g_assert_cmpint(info->master_log_pos,       ==, 2143897);
	g_assert_cmpstr(info->master_host->str,     ==, "127.0.0.1");
	g_assert_cmpstr(info->master_user->str,     ==, "root");
	g_assert_cmpstr(info->master_password->str, ==, "123");
	g_assert_cmpint(info->master_port,          ==, 3306);
	g_assert_cmpint(info->master_connect_retry, ==, 60);
	g_assert_cmpint(info->master_ssl,           ==, 0); /* is disabled */
	g_assert_cmpint(info->master_ssl_verify_server_cert, ==, 0);

	s = g_string_new(NULL);
	g_assert_cmpint(network_mysqld_masterinfo_append(s, info), ==, 0);
	g_assert_cmpint(s->len, ==, sizeof(PACKET) - 1);
	g_assert_cmpint(TRUE, ==, g_memeq(S(s), C(PACKET)));
	g_string_free(s, TRUE);

	g_string_free(packet->data, TRUE);
	network_packet_free(packet);

	network_mysqld_masterinfo_free(info);
}
int network_packet_realloc(UDPpacket* packet, int size) {
        network_packet_free(packet);
        packet = network_packet_alloc(size);
        return (packet == NULL) ? 1 : 0;
}
Exemple #9
0
/*
* network_packet_realloc() - Reallocate space for the packet data
* ! Perhaps use this function if network_packet_resize() fails
* @packet: the packet to reallocate
* @size: the new size of the packet
*/
int network_packet_realloc(UDPpacket* packet, int size) {
	network_packet_free(packet); // Free the packet
	packet = network_packet_alloc(size); // Allocate the packet
	return (packet == nullptr) ? 1 : 0; // Return 0 on success and 1 on failure
}