Ejemplo n.º 1
0
/* Sets up the object. */
int http_init(http_t *client, char *msg)
{
	int rc = 0;

	if (client->ssl_enabled)
		client->tcp.ip.port = HTTPS_DEFAULT_PORT;

	do {
		TRY(local_set_params(client));
		TRY(tcp_init(&client->tcp, msg, client->verbose));
		if (client->ssl_enabled)
			TRY(ssl_init(client, msg));
	}
	while (0);

	if (rc) {
		http_exit(client);
		return rc;
	}

	client->initialized = 1;

	return 0;
}
Ejemplo n.º 2
0
/* 
	Sets up the object.

	- ...
*/
RC_TYPE http_client_init(HTTP_CLIENT *p_self)
{
	RC_TYPE rc;
	do
	{
		/*set local params*/
		rc = local_set_params(p_self);
		if (rc != RC_OK)
		{
			break;
		}


		/*call super*/
		rc = super_init(&p_self->super);
		if (rc != RC_OK)
		{
			break;
		}

		/*local init*/

	}
	while(0);

	if (rc != RC_OK)
	{
		http_client_shutdown(p_self);		
	}
	else
	{
		p_self->initialized = TRUE;
	}
	
	return rc;
}
Ejemplo n.º 3
0
RC_TYPE http_client_init_async(HTTP_CLIENT *p_self,CB_EXIT_COND p_exit_func,void *p_cb_data)
{
	RC_TYPE rc;
	do
	{
		/*set local params*/
		rc = local_set_params(p_self);
		if (rc != RC_OK)
		{
			break;
		}


		/*call super*/
		rc = super_init_async(&p_self->super);
		if (rc != RC_OK)
		{
			break;
		}

		/*local init*/

	}
	while(0);

	if (rc != RC_OK)
	{
		http_client_shutdown(p_self);
	}
	else
	{
		p_self->initialized = TRUE;
	}

	return rc;
}
Ejemplo n.º 4
0
/*
  Sets up the object.

  - ...
*/
int tcp_initialize(tcp_sock_t *p_self, char *msg)
{
	int rc;
	struct timeval sv;
	int svlen = sizeof(sv);
	char host[NI_MAXHOST];

	do {
		local_set_params(p_self);

		/*call the super */
		rc = ip_initialize(&p_self->super);
		if (rc != 0) {
			break;
		}

		/* local object initalizations */
		if (p_self->super.type == TYPE_TCP) {
			p_self->super.socket = socket(AF_INET, SOCK_STREAM, 0);
			if (p_self->super.socket == -1) {
				int code = os_get_socket_error();

				logit(LOG_ERR, "Error creating client socket: %s", strerror(code));
				rc = RC_IP_SOCKET_CREATE_ERROR;
				break;
			}

			/* Call to socket() OK, allow tcp_shutdown() to run to
			 * prevent socket leak if any of the below calls fail. */
			p_self->initialized = 1;

			if (p_self->super.bound == 1) {
				if (bind
				    (p_self->super.socket,
				     (struct sockaddr *)&p_self->super.local_addr,
				     sizeof(struct sockaddr_in)) < 0) {
					int code = os_get_socket_error();

					logit(LOG_WARNING,
					      "Failed binding client socket to local address: %s",
					      strerror(code));
					rc = RC_IP_SOCKET_BIND_ERROR;
					break;
				}
			}
		} else {
			p_self->initialized = 1;	/* Allow tcp_shutdown() to run. */
			rc = RC_IP_BAD_PARAMETER;
		}

		/* set timeouts */
		sv.tv_sec = p_self->super.timeout / 1000;	/* msec to sec */
		sv.tv_usec = (p_self->super.timeout % 1000) * 1000;	/* reminder to usec */
		setsockopt(p_self->super.socket, SOL_SOCKET, SO_RCVTIMEO, &sv, svlen);
		setsockopt(p_self->super.socket, SOL_SOCKET, SO_SNDTIMEO, &sv, svlen);

		if (!getnameinfo
		    (&p_self->super.remote_addr, p_self->super.remote_len, host,
		     NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) {
			logit(LOG_INFO, "%s, connecting to %s(%s)", msg,
			      p_self->super.p_remote_host_name, host);
		}

		if (0 != connect(p_self->super.socket, &p_self->super.remote_addr, p_self->super.remote_len)) {
			int code = os_get_socket_error();

			logit(LOG_WARNING, "Failed connecting to remote server: %s", strerror(code));
			rc = RC_IP_CONNECT_FAILED;
			break;
		}
	}
	while (0);

	if (rc != 0) {
		tcp_shutdown(p_self);
		return rc;
	}

	return 0;
}
Ejemplo n.º 5
0
/* On error tcp_exit() is called by upper layers. */
int tcp_init(tcp_sock_t *tcp, char *msg, int verbose)
{
	int rc = 0;
	char host[NI_MAXHOST];
	struct timeval sv;
	struct sockaddr sa;
	socklen_t salen;

	ASSERT(tcp);

	do {
		int sd;

		TRY(local_set_params(tcp));
		TRY(ip_init(&tcp->ip));

		if (tcp->ip.type != TYPE_TCP)
			return RC_IP_BAD_PARAMETER;

		sd = socket(AF_INET, SOCK_STREAM, 0);
		if (sd == -1) {
			logit(LOG_ERR, "Error creating client socket: %s", strerror(errno));
			rc = RC_IP_SOCKET_CREATE_ERROR;
			break;
		}

		/* Call to socket() OK, allow tcp_exit() to run to
		 * prevent socket leak if any of the below calls fail. */
		tcp->ip.socket = sd;
		tcp->initialized  = 1;

		if (tcp->ip.bound == 1) {
			if (bind(sd, (struct sockaddr *)&tcp->ip.local_addr, sizeof(struct sockaddr_in)) < 0) {
				logit(LOG_WARNING, "Failed binding client socket to local address: %s", strerror(errno));
				rc = RC_IP_SOCKET_BIND_ERROR;
				break;
			}
		}

		/* Attempt to set TCP timers, silently fall back to OS defaults */
		sv.tv_sec  =  tcp->ip.timeout / 1000;
		sv.tv_usec = (tcp->ip.timeout % 1000) * 1000;
		setsockopt(tcp->ip.socket, SOL_SOCKET, SO_RCVTIMEO, &sv, sizeof(sv));
		setsockopt(tcp->ip.socket, SOL_SOCKET, SO_SNDTIMEO, &sv, sizeof(sv));

		sa    = tcp->ip.remote_addr;
		salen = tcp->ip.remote_len;
		if (!getnameinfo(&sa, salen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) {
			if (verbose > 0)
				logit(LOG_INFO, "%s, connecting to %s (%s:%d)", msg, tcp->ip.p_remote_host_name, host, tcp->ip.port);
		}
		else
			logit(LOG_ERR, "%s, failed resolving %s!", msg, host);

		if (connect(sd, &sa, salen)) {
			if (!check_error(sd, tcp->ip.timeout))
				break; /* OK */

			logit(LOG_WARNING, "Failed connecting to remote server: %s", strerror(errno));
			rc = RC_IP_CONNECT_FAILED;
			break;
		}
	} while (0);

	if (rc) {
		tcp_exit(tcp);
		return rc;
	}

	return 0;
}
Ejemplo n.º 6
0
Archivo: tcp.c Proyecto: ebichu/dd-wrt
static RC_TYPE do_tcp_create_socket(TCP_SOCKET *p_self,LINGER so_linger,int timeout)
{
	RC_TYPE	rc=RC_IP_SOCKET_CREATE_ERROR;


	do {

		local_set_params(p_self);

		/*call the super*/
		rc = super_initialize(&p_self->super);

		if (rc != RC_OK)
		{
			break;
		}

		if (!(p_self->super.type == TYPE_TCP))
		{
			rc = RC_IP_BAD_PARAMETER;
		}
		else {			

			struct addrinfo *addr=p_self->super.addr;
			int				socket_index=0;


			do {

				p_self->super.socket[socket_index]=
					socket(addr->ai_family,addr->ai_socktype,addr->ai_protocol);

				p_self->super.addr_ar[socket_index]=addr;

				addr=addr->ai_next;

				/*only error if no sockets at all created*/
				if (!(p_self->super.socket[socket_index] == INVALID_SOCKET)) {

					rc=RC_OK;

					/* set timeouts */
					setsockopt(p_self->super.socket[socket_index],SOL_SOCKET,SO_RCVTIMEO,
						(char*) &timeout,sizeof(timeout));

					setsockopt(p_self->super.socket[socket_index],SOL_SOCKET,SO_SNDTIMEO,
						(char*) &timeout,sizeof(timeout));

					setsockopt(p_self->super.socket[socket_index],SOL_SOCKET,SO_LINGER,
						(char *) &so_linger,sizeof(LINGER));									

					if (!(addr)) {

						break;
					}
					else {

						/*socket already 1 from ip construct*/

						p_self->super.server_socket_count++;

						p_self->super.socket=safe_realloc(p_self->super.socket,sizeof(SOCKET)*
							p_self->super.server_socket_count);

						p_self->super.addr_ar=safe_realloc(p_self->super.addr_ar,sizeof(struct addrinfo *)*
							p_self->super.server_socket_count);

						socket_index++;

						p_self->super.socket[socket_index]=INVALID_SOCKET;
						p_self->super.addr_ar[socket_index]=NULL;
					}
				}				

			} while(addr);
		}
	} 
	while(0);

	return rc;
}