Esempio n. 1
0
Connection *Connection_new(const char *in_addr)
{
	DEBUG(("alloc Connection\n"));
	Connection *connection = Alloc_alloc_T(Connection);
	if(connection == NULL) {
		Module_set_error(GET_MODULE(), "Out of memory while allocating Connection");
		return NULL;
	}
	connection->state = CS_CLOSED;
	connection->current_batch = NULL;
	connection->current_executor = NULL;
	connection->parser = ReplyParser_new();
	if(connection->parser == NULL) {
		Connection_free(connection);
		return NULL;
	}

	//copy socket addr
	if(ADDR_SIZE < snprintf(connection->addr, ADDR_SIZE, "%s", in_addr)) {
		Module_set_error(GET_MODULE(), "Invalid address for Connection");
		Connection_free(connection);
		return NULL;
	}

	//parse addr
	int port = DEFAULT_IP_PORT;
	char *pport;
	char addr[ADDR_SIZE];
	DEBUG(("parsing connection addr: %s\n", connection->addr));
	if(NULL == (pport = strchr(connection->addr, ':'))) {
		port = DEFAULT_IP_PORT;
		snprintf(addr, ADDR_SIZE, "%s", connection->addr);
	}
	else {
		port = atoi(pport + 1);
		snprintf(addr, (pport - connection->addr + 1), "%s", connection->addr);
	}
	connection->sa.sin_family = AF_INET;
	connection->sa.sin_port = htons(port);
	if(!inet_pton(AF_INET, addr, &connection->sa.sin_addr)) {
		Module_set_error(GET_MODULE(), "Could not parse ip address");
		Connection_free(connection);
		return NULL;
	}
	DEBUG(("Connection ip: '%s', port: %d\n", addr, port));

	connection->sockfd = 0;

	return connection;
}
Esempio n. 2
0
Connection *Connection_new(const char *in_addr)
{
	DEBUG(("alloc Connection\n"));
	Connection *connection = Alloc_alloc_T(Connection);
	if(connection == NULL) {
		Module_set_error(GET_MODULE(), "Out of memory while allocating Connection");
		return NULL;
	}
	connection->state = CS_CLOSED;
	connection->current_batch = NULL;
	connection->current_executor = NULL;
	connection->parser = ReplyParser_new();
	if(connection->parser == NULL) {
		Connection_free(connection);
		return NULL;
	}

	//copy address
	int invalid_address = 0;
	char *service;
	if (NULL == (service = strchr(in_addr, ':'))) {
		invalid_address = ADDR_SIZE < snprintf(connection->addr, ADDR_SIZE, "%s", in_addr)
				|| SERV_SIZE < snprintf(connection->serv, SERV_SIZE, "%s", XSTR(DEFAULT_IP_PORT));
	}
	else {
		int addr_length = service - in_addr + 1;
		if (ADDR_SIZE < addr_length) {
			invalid_address = 1;
		}
		else {
			snprintf(connection->addr, addr_length, "%s", in_addr);
			invalid_address = SERV_SIZE < snprintf(connection->serv, SERV_SIZE, "%s", service + 1);
		}
	}
	if (invalid_address) {
		Module_set_error(GET_MODULE(), "Invalid address for Connection");
		Connection_free(connection);
		return NULL;
	}
	DEBUG(("Connection address: '%s', service: '%s'\n", connection->addr, connection->serv));

	connection->addrinfo = NULL;
	connection->sockfd = 0;

	return connection;
}