Esempio n. 1
0
File: main.c Progetto: MigNov/mBench
void net_io_process(int server)
{
	unsigned long nioBufferArray[6] = { 4, 128, 512, 1024, 2048, 4096 };				// in kB
	int err;

	if (server) {
		printf("Listening on %s\n", net_get_bound_addr());
		if ((err = net_listen(0, NET_IPV4)) < 0)
			DPRINTF("Error listening on %s: %s\n", net_get_bound_addr(), strerror(-err));

		DPRINTF("Server run complete...\n");
	}
	else {
		int sock;

		fprintf(stderr, "Trying connection to %s\n", net_get_connect_addr());
		sock = net_connect(NULL, 0, NET_IPV4);
		if (sock <= 0)
			fprintf(stderr, "Connection to %s failed: %s\n", net_get_connect_addr(), strerror(-sock));
		else {
			int ii, nioIdx;
			float tm = 0.0, cpu = 0.0;

			results->net_res_size = (sizeof(nioBufferArray) / sizeof(nioBufferArray[0]));

			fprintf(stderr, "Network: Getting %d results, this may take some time\n", results->net_res_size);

			results->net = (tIOResults *)malloc( results->net_res_size * sizeof(tIOResults) );
			nioIdx = 0;

			for (ii = 0; ii < (sizeof(nioBufferArray) / sizeof(nioBufferArray[0])); ii++) {
				unsigned long long total = 0, chunk = 0;
				char size_total[16] = { 0 }, size_chunk[16] = { 0 }, size_thp[16] = { 0 };

				total = nioBufSize;
				chunk = (unsigned long long)nioBufferArray[ii] * (1 << 10);
				net_write_command(sock, total, chunk, chunk, &tm, &cpu);

				strncpy(results->net[nioIdx].operation, NET_OP_READ(outType), sizeof(results->net[nioIdx].operation));
				results->net[nioIdx].size = total;
				results->net[nioIdx].throughput = total / tm;
				results->net[nioIdx].chunk_size = chunk;
				results->net[nioIdx++].cpu_usage = cpu;

				io_get_size(total, 0, size_total, 16);
				io_get_size(chunk, 0, size_chunk, 16);
				io_get_size_double(total / tm, prec, size_thp, 16);
				DPRINTF("Network benchmark on %s with buffer size %s: %s/s (CPU: %.*f%%)\n", size_total, size_chunk, size_thp, prec, cpu);
			}

			if (net_server_terminate(sock))
				DPRINTF("Server socket terminated\n");
		}
	}
}
/* ask the client to "change plugin", so that it will send us the password in cleartext...
 *
 * HELP:
 * - http://lists.mysql.com/commits/136992
 * - http://bugs.mysql.com/bug.php?id=57442
 * - wireshark...
 */
void flex_change_plugin_to_cleartext(MYSQL_PLUGIN_VIO *vio, struct auth_flex_data *d_auth_flex_data)
{
    char * __attribute__ ((unused)) pkt_change_plugin = "mysql_clear_password";

#if DBMS_mysql < 57

    void **addr_net_ptr = NULL;

    /* we cannot call vio->write_packet() because send_plugin_request_packet() would concatenate
     *  and send the current auth pluging + the requested one (\254 + "mysql_native_password\0" + "mysql_clear_password\0")
     * so, below we send the "plugin change" request ourselves on the wire. (\254 + "mysql_clear_password\0")
     */
    {
        /* be careful..., arithmetic on a (void **) would move 4x faster than on a (void *)... */
        addr_net_ptr = (void *)(((void *)d_auth_flex_data->addr_scramble_ptr) + sizeof(void *) * 4 + sizeof(ulong));
    }
    DEBUG xsyslog(LOG_LOCAL7 | LOG_NOTICE, "%s : addr_scramble/%p addr_net/%p (diff %ld)", __func__,
                  d_auth_flex_data->addr_scramble_ptr, addr_net_ptr, (long int)(addr_net_ptr - d_auth_flex_data->addr_scramble_ptr));

    net_write_command(*addr_net_ptr, 254, pkt_change_plugin, strlen(pkt_change_plugin) + 1, "", 0);

#else /* DBMS_mysql < 57 */

    /* for mysql 5.7, things get a bit hairy.
     *
     * we have to simulate a MYSQL_PLUGIN_VIO struct, fill in some interesting fields
     * and send it back to our mysql client so it can retry auth with desired plugin.
     *
     * don't do this at home, kids.
     *
     */

    struct MYSQL_PLUGIN_VIO_FAKE
    {
        MYSQL_PLUGIN_VIO plugin_vio;
        MYSQL_SERVER_AUTH_INFO auth_info;
        void *acl_user;
        struct st_plugin_int {
            LEX_STRING name;
            struct st_mysql_plugin *plugin;
        } *plugin;
        LEX_STRING db;
        struct {
            char *plugin, *pkt;
            uint pkt_len;
        } cached_client_reply;
        struct {
            char *pkt;
            uint pkt_len;
        } cached_server_packet;
        int packets_read, packets_written;
        enum { SUCCESS, FAILURE, RESTART } status;
    };

    struct MYSQL_PLUGIN_VIO_FAKE *vio_fake = (struct MYSQL_PLUGIN_VIO_FAKE *)vio;
    int old_status = vio_fake->status;
    vio_fake->status = RESTART;

    struct st_mysql_auth tmp_auth = {0};
    tmp_auth.client_auth_plugin = pkt_change_plugin;
    struct st_mysql_plugin tmp_plugin = {0};
    tmp_plugin.info = &tmp_auth;
    struct st_plugin_int tmp_int = {0};
    tmp_int.plugin = &tmp_plugin;

    void *old_plugin = vio_fake->plugin;
    vio_fake->plugin = &tmp_int;

    DEBUG xsyslog(LOG_LOCAL7 | LOG_NOTICE, "%s : packets read/written %d/%d", __func__,
                  vio_fake->packets_read, vio_fake->packets_written);
    vio->write_packet(vio, "", 0); /* This will somehow trigger the CHANGE_PLUGIN procedure... */
    vio_fake->plugin = old_plugin;
    vio_fake->status = old_status;

#endif /* DBMS_mysql < 57 */

}