Example #1
0
void network_socket_free(network_socket *s) {
	if (!s) return;

	network_queue_free(s->send_queue);
	network_queue_free(s->recv_queue);
	network_queue_free(s->recv_queue_raw);

	if (s->response) network_mysqld_auth_response_free(s->response);
	if (s->challenge) network_mysqld_auth_challenge_free(s->challenge);

	network_address_free(s->dst);
	network_address_free(s->src);

	if (s->event.ev_base) { /* if .ev_base isn't set, the event never got added */
		event_del(&(s->event));
	}

	if (s->fd != -1) {
		closesocket(s->fd);
	}

    g_string_free(s->default_db, TRUE);
    g_string_free(s->charset_client, TRUE);
    g_string_free(s->charset_connection, TRUE);
    g_string_free(s->charset, TRUE);
    g_string_free(s->charset_results, TRUE);
    g_string_free(s->sql_mode, TRUE);

	g_free(s);
}
static void
t_network_address_tostring_ipv6() {
#ifdef AF_INET6
	network_address *addr;
	char buf[255];
	gsize buf_len = sizeof(buf);
	GError *gerr = NULL;

	addr = network_address_new();

	if (0 != network_address_set_address(addr, "[::1]")) {
		/* skip test, if resolving ::1 fails */
		network_address_free(addr);
		return;
	}

	buf_len = sizeof(buf); /* should be large enough */
	g_assert_cmpstr(network_address_tostring(addr, buf, &buf_len, NULL), ==, "::1");
	g_assert_cmpint(3 + 1, ==, buf_len);

	buf_len = 3; /* too small */
	g_assert(NULL == network_address_tostring(addr, buf, &buf_len, &gerr));
	g_assert_cmpint(NETWORK_ADDRESS_ERROR, ==, gerr->domain);
	g_assert_cmpint(NETWORK_ADDRESS_ERROR_DST_TOO_SMALL, ==, gerr->code);
	g_clear_error(&gerr);

	network_address_free(addr);
#endif
}
Example #3
0
void t_network_address_new() {
    network_address *addr;

    addr = network_address_new();

    network_address_free(addr);
}
void t_network_address_set() {
	network_address *addr;

	addr = network_address_new();

	g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:3306"), ==, 0);
	g_assert_cmpint(network_address_set_address(addr, "127.0.0.1"), ==, 0);

	g_log_set_always_fatal(G_LOG_FATAL_MASK);
	/* shouldn't crash.
	 *
	 * we can't test if it works as we can't know if the host has setup IPv6
	 */
	network_address_set_address(addr, "[::1]");
	network_address_set_address(addr, "[::1]:3306");

	/* should fail */	
	g_assert_cmpint(network_address_set_address(addr, "500.0.0.1"), ==, -1);
	g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:"), ==, -1);
	g_assert_cmpint(network_address_set_address(addr, "[::1]:"), ==, -1);
	g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:65536"), ==, -1);
	g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:-1"), ==, -1);

	network_address_free(addr);
}
static void
t_network_address_tostring_unix() {
#ifndef _WIN32
	network_address *addr;
	char buf[255];
	gsize buf_len = sizeof(buf);
	GError *gerr = NULL;

	addr = network_address_new();

	g_assert_cmpint(network_address_set_address(addr, "/foobar"), ==, 0);

	buf_len = sizeof(buf); /* should be large enough */
	g_assert_cmpstr(network_address_tostring(addr, buf, &buf_len, NULL), ==, "/foobar");
	g_assert_cmpint(7 + 1, ==, buf_len);

	buf_len = 3; /* too small */
	g_assert(NULL == network_address_tostring(addr, buf, &buf_len, &gerr));
	g_assert_cmpint(NETWORK_ADDRESS_ERROR, ==, gerr->domain);
	g_assert_cmpint(NETWORK_ADDRESS_ERROR_DST_TOO_SMALL, ==, gerr->code);
	g_clear_error(&gerr);

	network_address_free(addr);
#endif
}
void network_backend_free(network_backend_t *b) {
	if (!b) return;

	network_connection_pool_free(b->pool);

	if (b->addr)     network_address_free(b->addr);
	if (b->uuid)     g_string_free(b->uuid, TRUE);

	g_free(b);
}
Example #7
0
int network_backends_remove(network_backends_t *bs, guint index) {
	network_backend_t* b = bs->backends->pdata[index];
	if (b != NULL) {
		if (b->addr) network_address_free(b->addr);
		if (b->uuid) g_string_free(b->uuid, TRUE);
		g_mutex_lock(bs->backends_mutex);
		g_ptr_array_remove_index(bs->backends, index);
		g_mutex_unlock(bs->backends_mutex);
	}
	return 0;
}
/**
 * test if we convert addr->string correctly for IPv6
 */
void t_network_address_resolve_ipv6() {
	network_address *addr;

	addr = network_address_new();
	if (0 != network_address_set_address(addr, "[::1]")) {
		/* skip test, if resolving ::1 fails */
		network_address_free(addr);

		return;
	}

	/* _set_address() should set the port number */

	/* reset the name to see that _refresh_name() updates to the right value */
	g_string_truncate(addr->name, 0);

	network_address_refresh_name(addr);

	g_assert_cmpstr(addr->name->str, ==, "[::1]:3306");

	network_address_free(addr);
}
Example #9
0
void
admin_network_addr_free(
    gpointer*			addr_ptr
)
{
    admin_network_addr_t*			addr;


    addr	= (admin_network_addr_t*)addr_ptr;

    network_address_free(addr->addr);

    g_free(addr);
}
Example #10
0
void network_backend_free(network_backend_t *b) {
	if (!b) return;

	guint i;
	for (i = 0; i < b->pools->len; ++i) {
		network_connection_pool* pool = g_ptr_array_index(b->pools, i);
		network_connection_pool_free(pool);
	}
	g_ptr_array_free(b->pools, TRUE);

	if (b->addr)     network_address_free(b->addr);
	if (b->uuid)     g_string_free(b->uuid, TRUE);

	g_free(b);
}
Example #11
0
void t_network_address_set() {
    network_address *addr;

    addr = network_address_new();

    g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:3306"), ==, 0);
    g_assert_cmpint(network_address_set_address(addr, "127.0.0.1"), ==, 0);

    g_log_set_always_fatal(G_LOG_FATAL_MASK);

    /* should fail */
    g_assert_cmpint(network_address_set_address(addr, "500.0.0.1"), ==, -1);
    g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:"), ==, -1);
    g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:65536"), ==, -1);
    g_assert_cmpint(network_address_set_address(addr, "127.0.0.1:-1"), ==, -1);

    network_address_free(addr);
}
Example #12
0
/**
 * test if we decode the port number correctly
 */
void t_network_address_resolve() {
    network_address *addr;

    g_test_bug("43313");

    addr = network_address_new();
    network_address_set_address(addr, "127.0.0.1:3306");

    /* _set_address() should set the port number */
    g_assert_cmpint(ntohs(addr->addr.ipv4.sin_port), ==, 3306);

    /* reset the name to see that _refresh_name() updates to the right value */
    g_string_truncate(addr->name, 0);

    network_address_refresh_name(addr);

    g_assert_cmpstr(addr->name->str, ==, "127.0.0.1:3306");

    network_address_free(addr);
}
Example #13
0
/**
 * connect a socket
 *
 * the con->dst->addr has to be set before 
 * 
 * @param con    a socket 
 * @return       NETWORK_SOCKET_SUCCESS on connected, NETWORK_SOCKET_ERROR on error
 *
 * @see network_address_set_address()
 */
network_socket_retval_t network_socket_bind(network_socket * con) {
	/* 
	 * HPUX:       int setsockopt(int s,    int level, int optname, const void *optval, int optlen);
	 * all others: int setsockopt(int s,    int level, int optname, const void *optval, socklen_t optlen);
	 */
#define SETSOCKOPT_OPTVAL_CAST (void *)

	g_return_val_if_fail(con->fd < 0, NETWORK_SOCKET_ERROR); /* socket is already bound */
	g_return_val_if_fail((con->socket_type == SOCK_DGRAM) || (con->socket_type == SOCK_STREAM), NETWORK_SOCKET_ERROR);

	if (con->socket_type == SOCK_STREAM) {
		g_return_val_if_fail(con->dst, NETWORK_SOCKET_ERROR);
		g_return_val_if_fail(con->dst->name->len > 0, NETWORK_SOCKET_ERROR);

		if (-1 == (con->fd = socket(con->dst->addr.common.sa_family, con->socket_type, 0))) {
			g_critical("%s: socket(%s) failed: %s (%d)", 
					G_STRLOC,
					con->dst->name->str,
					g_strerror(errno), errno);
			return NETWORK_SOCKET_ERROR;
		}

		if (con->dst->addr.common.sa_family == AF_INET || 
		    con->dst->addr.common.sa_family == AF_INET6) {
			/* TCP_NODELAY  is int on unix */
			/* SO_REUSEADDR is int on unix */
			int val;

			val = 1;
			if (0 != setsockopt(con->fd, IPPROTO_TCP, TCP_NODELAY, SETSOCKOPT_OPTVAL_CAST &val, sizeof(val))) {
				g_critical("%s: setsockopt(%s, IPPROTO_TCP, TCP_NODELAY) failed: %s (%d)", 
						G_STRLOC,
						con->dst->name->str,
						g_strerror(errno), errno);
				return NETWORK_SOCKET_ERROR;
			}
		
			if (0 != setsockopt(con->fd, SOL_SOCKET, SO_REUSEADDR, SETSOCKOPT_OPTVAL_CAST &val, sizeof(val))) {
				g_critical("%s: setsockopt(%s, SOL_SOCKET, SO_REUSEADDR) failed: %s (%d)", 
						G_STRLOC,
						con->dst->name->str,
						g_strerror(errno), errno);
				return NETWORK_SOCKET_ERROR;
			}
		}

		if (con->dst->addr.common.sa_family == AF_INET6) {
#ifdef IPV6_V6ONLY
			/* disable dual-stack IPv4-over-IPv6 sockets
			 *
			 * ... if it is supported:
			 * - Linux
			 * - Mac OS X
			 * - FreeBSD
			 * - Solaris 10 and later
			 *
			 * no supported on:
			 * - Solaris 9 and earlier
			 */

			/* IPV6_V6ONLY is int on unix */
			int val;

			val = 0;
			if (0 != setsockopt(con->fd, IPPROTO_IPV6, IPV6_V6ONLY, SETSOCKOPT_OPTVAL_CAST &val, sizeof(val))) {
				g_critical("%s: setsockopt(%s, IPPROTO_IPV6, IPV6_V6ONLY) failed: %s (%d)", 
						G_STRLOC,
						con->dst->name->str,
						g_strerror(errno), errno);
				return NETWORK_SOCKET_ERROR;
			}
#endif
		}

		if (-1 == bind(con->fd, &con->dst->addr.common, con->dst->len)) {
			gchar *address_copy;

			/* binding failed so the address/socket is already being used
			 * let's check if we can connect to it so we check if is being used 
			 * by some app
			 */
			if (-1 == connect(con->fd, &con->dst->addr.common, con->dst->len)) {
				g_debug("%s.%d: connect(%s) failed: %s (%d)", 
					__FILE__, __LINE__,
					con->dst->name->str,
					g_strerror(errno), errno);
				/* we can't connect to the socket so no one is listening on it. We need
				 * to unlink it (delete the name from the file system) to be able to
				 * re-use it.
				 * network_address_free does the unlink, but to re-use it we need
				 * to store the pathname associated with the socket before unlink it and
				 * create a new socket with it.
				 */
				address_copy = g_strdup(con->dst->name->str);
				con->dst->can_unlink_socket = TRUE;
				network_address_free(con->dst);

				con->dst = network_address_new();
				network_address_set_address(con->dst, address_copy);

				/* we can now free the address copy */
				g_free(address_copy);

				g_debug("%s: retrying to bind(%s)",
						G_STRLOC,
						con->dst->name->str);

				/* let's bind again with the new socket */
				if (-1 == bind(con->fd, &con->dst->addr.common, con->dst->len)) {
					g_critical("%s: bind(%s) failed: %s (%d)", 
						G_STRLOC,
						con->dst->name->str,
						g_strerror(errno), errno);

					return NETWORK_SOCKET_ERROR;
				}
			} else {
				g_critical("%s: bind(%s) failed: %s (%d)", 
					G_STRLOC,
					con->dst->name->str,
					g_strerror(errno), errno);

				return NETWORK_SOCKET_ERROR;	
			}
		}

		if (con->dst->addr.common.sa_family == AF_INET &&
		    con->dst->addr.ipv4.sin_port == 0) {
			struct sockaddr_in a;
			socklen_t          a_len = sizeof(a);

			if (0 != getsockname(con->fd, (struct sockaddr *)&a, &a_len)) {
				g_critical("%s: getsockname(%s) failed: %s (%d)", 
						G_STRLOC,
						con->dst->name->str,
						g_strerror(errno), errno);
				return NETWORK_SOCKET_ERROR;
			}
			con->dst->addr.ipv4.sin_port  = a.sin_port;
		} else if (con->dst->addr.common.sa_family == AF_INET6 &&
		           con->dst->addr.ipv6.sin6_port == 0) {
			struct sockaddr_in6 a;
			socklen_t          a_len = sizeof(a);

			if (0 != getsockname(con->fd, (struct sockaddr *)&a, &a_len)) {
				g_critical("%s: getsockname(%s) failed: %s (%d)", 
						G_STRLOC,
						con->dst->name->str,
						g_strerror(errno), errno);
				return NETWORK_SOCKET_ERROR;
			}
			con->dst->addr.ipv6.sin6_port  = a.sin6_port;
		}

		if (-1 == listen(con->fd, 128)) {
			g_critical("%s: listen(%s, 128) failed: %s (%d)",
					G_STRLOC,
					con->dst->name->str,
					g_strerror(errno), errno);
			return NETWORK_SOCKET_ERROR;
		}
	} else {
		/* UDP sockets bind the ->src address */
		g_return_val_if_fail(con->src, NETWORK_SOCKET_ERROR);
		g_return_val_if_fail(con->src->name->len > 0, NETWORK_SOCKET_ERROR);

		if (-1 == (con->fd = socket(con->src->addr.common.sa_family, con->socket_type, 0))) {
			g_critical("%s: socket(%s) failed: %s (%d)", 
					G_STRLOC,
					con->src->name->str,
					g_strerror(errno), errno);
			return NETWORK_SOCKET_ERROR;
		}

		if (-1 == bind(con->fd, &con->src->addr.common, con->src->len)) {
			g_critical("%s: bind(%s) failed: %s (%d)", 
					G_STRLOC,
					con->src->name->str,
					g_strerror(errno), errno);
			return NETWORK_SOCKET_ERROR;
		}
	}

	con->dst->can_unlink_socket = TRUE;
	return NETWORK_SOCKET_SUCCESS;
}