Exemplo n.º 1
0
void connect_cmd_handler(void *pMsg)
{
	char *arg = (char *)pMsg;
	uint32_t address;
	struct sockaddr_in addr;

	if (tcp_client_socket >= 0) {
		printf("Already connected to a remote device.\r\n");
		return;
	}

	/* Create TCP client socket. */
	if ((tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		printf("Failed to create TCP client socket.\r\n");
		return;
	}

	while (*arg == ' ') {
		arg++;
	}
	if ((address = nmi_inet_addr(arg)) == 0) {
		printf("Invalid IP address.\r\n");
		return;
	}

	/* Connect to the server. */
	printf("Connecting to [%s] ...\r\n", arg);
	addr.sin_family = AF_INET;
	addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT);
	addr.sin_addr.s_addr = address; /* _htonl(address); */
	if (connect(tcp_client_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
		printf("Failed to connect to the server.\r\n");
		close(tcp_client_socket);
		tcp_client_socket = 1;
		return;
	}
}
Exemplo n.º 2
0
int http_client_send_request(struct http_client_module *const module, const char *url,
	enum http_method method, struct http_entity *const entity, const char *ext_header)
{
	uint8_t flag = 0;
	struct sockaddr_in addr_in;
	const char *uri = NULL;
	int i = 0, j = 0, reconnect = 0;

	if (module == NULL) {
		return -EINVAL;
	}

	if (module->req.state > STATE_SOCK_CONNECTED) {
		return -EBUSY;
	}

	/* Separate host and uri */
	if (!strncmp(url, "http://", 7)) {
		i = 7;
	} else if (!strncmp(url, "https://", 8)) {
		i = 8;
	}
	reconnect = strncmp(module->host, url + i, strlen(module->host));

	for (; url[i] != '\0' && url[i] != '/'; i++) {
		module->host[j++] = url[i];
	}
	module->host[j] = '\0';
	uri = url + i;

	/* Checks the parameters. */
	if (strlen(module->host) == 0) {
		return -EINVAL;
	}

	if (strlen(uri) >= HTTP_MAX_URI_LENGTH) {
		return -ENAMETOOLONG;
	}

	if (module->req.ext_header != NULL) {
		free(module->req.ext_header);
	}
	if (ext_header != NULL) {
		module->req.ext_header = strdup(ext_header);
		if (module->req.ext_header == NULL) {
			return -ENOMEM;
		}
	} else {
		module->req.ext_header = NULL;
	}

	module->sending = 0;
	module->recved_size = 0;
	if (uri[0] == '/') {
		strcpy(module->req.uri, uri);
		} else {
		module->req.uri[0] = '/';
		if (uri[0] != 0) {
			strcpy(module->req.uri + 1, uri);
		}
	}

	if (entity != NULL) {
		memcpy(&module->req.entity, entity, sizeof(struct http_entity));
		} else {
		memset(&module->req.entity, 0, sizeof(struct http_entity));
	}

	module->req.method = method;
	
	switch (module->req.state) {
	case STATE_TRY_SOCK_CONNECT:
		if (!reconnect) {
			break; /* Currently try to connect to the same server. */
		}
	case STATE_SOCK_CONNECTED:
		if (!reconnect) {
			module->req.state = STATE_REQ_SEND_HEADER;
			/* Send request immediately. */
			_http_client_request(module);
			break;
		} else {
			/* Request to another peer. Disconnect and try connect again. */
			_http_client_clear_conn(module, 0);
		}
	case STATE_INIT:
		if (module->config.tls) {
			flag |= SOCKET_FLAGS_SSL;
		}
		module->sock = socket(AF_INET, SOCK_STREAM, flag);
		if (module->sock >= 0) {
			module_ref_inst[module->sock] = module;
			if (_is_ip(module->host)) {
				addr_in.sin_family = AF_INET;
				addr_in.sin_port = _htons(module->config.port);
				addr_in.sin_addr.s_addr = nmi_inet_addr((char *)module->host);
				connect(module->sock, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in));
			} else {
				gethostbyname((uint8*)module->host);
			}
			module->req.state = STATE_TRY_SOCK_CONNECT;
		} else {
			return -ENOSPC;
		}
		break;
	default:
		/* STATE_TRY_REQ */
		/* STATE_WAIT_RESP */
		/* STATE_RECV_RESP */
		/* Session was busy. Try again later. */
		return -EAGAIN;
	}

	return 0;
}