Пример #1
0
void
sock_set_client_default(int fd)
{
	sock_set_linger_default(fd);
	sock_set_nodelay(fd, TRUE);
	sock_set_tcpquickack(fd, TRUE);
}
Пример #2
0
int
sock_connect (const char *url, GError **err)
{
	struct sockaddr_storage sas;
	gsize sas_len = sizeof(sas);

	if (!grid_string_to_sockaddr (url, (struct sockaddr*) &sas, &sas_len)) {
		g_error_transmit(err, NEWERROR(EINVAL, "invalid URL"));
		return -1;
	}

	int fd = socket_nonblock(sas.ss_family, SOCK_STREAM, 0);
	if (0 > fd) {
		g_error_transmit(err, NEWERROR(EINVAL, "socket error: (%d) %s", errno, strerror(errno)));
		return -1;
	}

	sock_set_reuseaddr(fd, TRUE);

	if (0 != metautils_syscall_connect (fd, (struct sockaddr*)&sas, sas_len)) {
		if (errno != EINPROGRESS && errno != 0) {
			g_error_transmit(err, NEWERROR(EINVAL, "connect error: (%d) %s", errno, strerror(errno)));
			metautils_pclose (&fd);
			return -1;
		}
	}

	sock_set_linger_default(fd);
	sock_set_nodelay(fd, TRUE);
	sock_set_tcpquickack(fd, TRUE);
	*err = NULL;
	return fd;
}
Пример #3
0
static int
_connect(const gchar *url, GError **err)
{
	struct addr_info_s ai;

	memset(&ai, 0, sizeof(ai));

	if (!grid_string_to_addrinfo(url, NULL, &ai)) {
		*err = NEWERROR(EINVAL, "invalid URL");
		return -1;
	}
	if (!ai.port) {
		*err = NEWERROR(EINVAL, "no port");
		return -1;
	}

	int fd = addrinfo_connect_nopoll(&ai, 1000, err);
	if (0 > fd)
		return -1;

	sock_set_linger_default(fd);
	sock_set_nodelay(fd, TRUE);
	sock_set_tcpquickack(fd, TRUE);
	*err = NULL;
	return fd;
}
Пример #4
0
static int
tcpip_open(const gchar *h, const gchar *p)
{

	GRID_DEBUG("opening tcp ip connection");
	struct evutil_addrinfo ai_hint, *ai_res = NULL;

	int rc;
	socklen_t ss_len;
	struct sockaddr_storage ss;
	int fd;

	fd = socket(PF_INET, SOCK_STREAM, 0);
	if (fd < 0) {
		GRID_ERROR("accept error on fd=%d : %s", fd, strerror(errno));
		return -1;
	}

	sock_set_linger_default(fd);
	sock_set_reuseaddr(fd, TRUE);

	bzero(&ai_hint, sizeof(ai_hint));
	ai_hint.ai_flags = AI_NUMERICHOST;
	ai_hint.ai_family = PF_INET;
	ai_hint.ai_socktype = SOCK_STREAM;
	rc = evutil_getaddrinfo(h, p, &ai_hint, &ai_res);
	if (rc != 0) {
		errno = rc;
		return -1;
	}

	bzero(&ss, sizeof(ss));
	ss_len = ai_res->ai_addrlen;
	g_memmove(&ss, (ai_res->ai_addr), ss_len);
	evutil_freeaddrinfo(ai_res);

	switch (connect(fd, (struct sockaddr *) &ss, ss_len)) {
		case 0:
			return fd;
		case -1:
			if (errno==EALREADY || errno==EAGAIN || errno==EINPROGRESS || errno==EWOULDBLOCK) {
				errno = 0;
				return fd;
			}
			return -1;
		default:/* unexpected */
			return -1;
	}
}
Пример #5
0
gint
accept_do (ACCEPT_POOL ap, addr_info_t *cltaddr, GError **err)
{
	struct sockaddr_storage sa;
	socklen_t saSize;
	int clt;

	errno=0;

	if (!ap) {
		GSETERROR(err,"Invalid parameter");
		return -1;
	}

	if (!ap->srv || ap->count<=0) {
		GSETERROR(err, "Empty server socket pool");
		return -1;
	}

	memset(&sa, 0, sizeof(sa));
	saSize = sizeof(sa);
	clt = UNSAFE_accept_do (ap, (struct sockaddr*) &sa, &saSize, err);

	if (clt < 0)
		return -1;

	if (cltaddr != NULL) {
		memset(cltaddr, 0, sizeof(addr_info_t));
		addrinfo_from_sockaddr(cltaddr, (struct sockaddr*)&sa, saSize);
	}

	/*Set all the helpful socket options*/
	if (gridd_flags & GRIDD_FLAG_NOLINGER)
		sock_set_linger_default(clt);
	if (gridd_flags & GRIDD_FLAG_KEEPALIVE)
		sock_set_keepalive(clt, TRUE);
	if (gridd_flags & GRIDD_FLAG_QUICKACK)
		sock_set_tcpquickack(clt, TRUE);

	return clt;
}
Пример #6
0
static struct network_client_s *
_endpoint_manage_event(struct network_server_s *srv, struct endpoint_s *e)
{
	int fd;
	struct sockaddr_storage ss;
	socklen_t ss_len;

retry:
	memset(&ss, 0, sizeof(ss));
	ss_len = sizeof(ss);
	fd = accept_nonblock(e->fd, (struct sockaddr*)&ss, &ss_len);

	if (0 > fd) {
		if (errno == EINTR)
			goto retry;
		if (errno != EAGAIN && errno != EWOULDBLOCK)
			GRID_WARN("fd=%d ACCEPT error (%d %s)", e->fd, errno, strerror(errno));
		return NULL;
	}

	switch (e->flags) {
		case NETSERVER_THROUGHPUT:
			sock_set_cork(fd, TRUE);
			break;
		case NETSERVER_LATENCY:
			sock_set_linger_default(fd);
			sock_set_nodelay(fd, TRUE);
			sock_set_tcpquickack(fd, TRUE);
			break;
		default:
			break;
	}

	struct network_client_s *clt = SLICE_NEW0(struct network_client_s);
	if (NULL == clt) {
		metautils_pclose(&fd);
		_cnx_notify_close(srv);
		return NULL;
	}

	switch (_cnx_notify_accept(srv)) {
		case EXCESS_SOFT:
			clt->current_error = NEWERROR(CODE_UNAVAILABLE, "Server overloaded.");
		case EXCESS_NONE:
			break;
		case EXCESS_HARD:
			metautils_pclose(&fd);
			_cnx_notify_close(srv);
			return NULL;
	}

	clt->main_stats = srv->stats;
	clt->server = srv;
	clt->fd = fd;
	grid_sockaddr_to_string((struct sockaddr*)&ss,
			clt->peer_name, sizeof(clt->peer_name));
	_client_sock_name(fd, clt->local_name, sizeof(clt->local_name));
	clt->time.cnx = network_server_bogonow(srv);
	clt->events = CLT_READ;

	clt->input.first = clt->input.last = NULL;
	clt->output.first = clt->output.last = NULL;

	if (e->factory_hook)
		e->factory_hook(e->factory_udata, clt);
	return clt;
}