Пример #1
0
/*
 * Get server data and make sure there is a continuation response inside them.
 */
int
response_continuation(session *ssn, int tag)
{
	int r;
	ssize_t n;

	buffer_reset(&ibuf);

	do {
		buffer_check(&ibuf, ibuf.len + INPUT_BUF);
		if ((n = receive_response(ssn, ibuf.data + ibuf.len, 0, 1)) ==
		    -1)
			return -1;
		ibuf.len += n;

		if (check_bye(ibuf.data))
			return STATUS_BYE;
	} while ((r = check_tag(ibuf.data, ssn, tag)) == STATUS_NONE &&
	    !check_continuation(ibuf.data));

	if (r == STATUS_NO &&
	    (check_trycreate(ibuf.data) || get_option_boolean("create")))
		return STATUS_TRYCREATE;

	if (r == STATUS_NONE)
		return STATUS_CONTINUE;

	return r;
}
Пример #2
0
 virtual bool request(talas::protocol::tls::connection& connection) {
     bool success = true;
     if ((success = send_request(connection))) {
         success = receive_response(connection);
     }
     return success;
 }
Пример #3
0
static int raw_to_trans_context(const char *raw, char **transp)
{
	int ret;
	int32_t ret_val;
	int fd;

	*transp = NULL;

	fd = setransd_open();
	if (fd < 0)
		return fd;

	ret = send_request(fd, RAW_TO_TRANS_CONTEXT, raw, NULL);
	if (ret)
		goto out;

	ret = receive_response(fd, RAW_TO_TRANS_CONTEXT, transp, &ret_val);
	if (ret)
		goto out;

	ret = ret_val;
      out:
	close(fd);
	return ret;
}
Пример #4
0
bool pwrMeter::readData(int &watt, float &amp, float &kwh, float &pf, float &voltage)
{
// TODO (Ardypro#1#): 增加超时处理

    int response_length = 0;
    response_length = receive_response(RX_Buffer);

    if(response_length>0)
    {
        Comm1.nRx = response_length;
        Comm1.Status = 2 ;
        if(Analysis_data() == 1)
        {
            voltage    = (float)Voltage_data / 100;
            amp        = (float)Current_data / 1000;
            watt       = Power_data;
            kwh        = (float)Energy_data/3200;
            pf         = (float)Pf_data / 1000;
        }
        return true ;
    }
    else
    {
        return false ;
    }
}
Пример #5
0
/*
 * Process the data that server sent due to IMAP FETCH BODY[] client request,
 * ie. FETCH BODY[HEADER], FETCH BODY[TEXT], FETCH BODY[HEADER.FIELDS
 * (<fields>)], FETCH BODY[<part>].
 */
int
response_fetchbody(session *ssn, int tag, char **body, size_t *len)
{
	int r, match;
	unsigned int offset;
	ssize_t n;
	regexp *re;

	if (tag == -1)
		return -1;

	buffer_reset(&ibuf);

	match = -1;
	offset = 0;
	
	re = &responses[RESPONSE_FETCH_BODY];

	do {
		buffer_check(&ibuf, ibuf.len + INPUT_BUF);
		if ((n = receive_response(ssn, ibuf.data + ibuf.len, 0, 1)) ==
		    -1)
			return -1;
		ibuf.len += n;

		if (match != 0) {
			match = regexec(re->preg, ibuf.data, re->nmatch,
			    re->pmatch, 0);
			
			if (match == 0 && re->pmatch[2].rm_so != -1 &&
			    re->pmatch[2].rm_eo != -1) {
				*len = strtoul(ibuf.data + re->pmatch[2].rm_so,
				    NULL, 10);
				offset = re->pmatch[0].rm_eo + *len;
			}
		}

		if (offset != 0 && ibuf.len >= offset) {
			if (check_bye(ibuf.data + offset))
				return STATUS_BYE;
		}
	} while (ibuf.len < offset || (r = check_tag(ibuf.data + offset, ssn,
	    tag)) == STATUS_NONE);

	if (match == 0) {
		if (re->pmatch[2].rm_so != -1 &&
		    re->pmatch[2].rm_eo != -1) {
			*body = ibuf.data + re->pmatch[0].rm_eo;
		} else {
			*body = ibuf.data + re->pmatch[3].rm_so;
			*len = re->pmatch[3].rm_eo - re->pmatch[3].rm_so;
		}
	}

	return r;
}
Пример #6
0
int
send_clear_command ()
{
  set_nl_header (SR_C_CLEAR);
  set_attributes ();
  if (sendto_fd (sd, (char *) &req, req.n.nlmsg_len) < 0)
    return -1;
  receive_response ();
  return 0;
}
Пример #7
0
void info_command(int fd, const char* command, int lines, long timeout)
{
    printf("-> %s\n", command);
    send_bytes(fd, command);
    send_bytes(fd, "\r");

    for (int i = 0; i < lines; i++)
    {
        char response[128];
        receive_response(fd, response, 128, timeout);
        printf("<- %s\n", response);
    }

}
Пример #8
0
void enter_command_modus(int fd, long timeout)
{
    printf("<> Entering command modus\n");
    send_bytes(fd, "+++");
    char response[3];
    receive_response(fd, response, 3, timeout);
    printf("<- %s\n", response);
    
    if (strncmp(response, "OK", 2) != 0)
    {
        printf("Invalid response received\n");

        exit(1);
    }

}
Пример #9
0
/*
 * Process the greeting that server sends during connection.
 */
int
response_greeting(session *ssn)
{

	buffer_reset(&ibuf);

	if (receive_response(ssn, ibuf.data, 0, 1) == -1)
		return -1;

	verbose("S (%d): %s", ssn->socket, ibuf.data);

	if (check_bye(ibuf.data))
		return STATUS_BYE;

	if (check_preauth(ibuf.data))
		return STATUS_PREAUTH;

	return STATUS_NONE;
}
Пример #10
0
void execute_command(int fd, const char* command, const char* expected_response, long timeout)
{
    const int len = strlen(expected_response);
    printf("-> %s\n", command);
    send_bytes(fd, command);
    send_bytes(fd, "\r");
    char* response = new char[len + 1];
    receive_response(fd, response, len + 1, timeout);
    printf("<- %s\n", response);

    if (strncmp(response, expected_response, len) != 0)
    {
        printf("Invalid response received\n");

        exit(1);
    }

    delete[] response;
}
Пример #11
0
/*
 * Process the greeting that server sends during connection.
 */
int
response_greeting(session *ssn)
{

	buffer_reset(&ibuf);

	if (receive_response(ssn, ibuf.data) == -1)
		return -1;

	if (check_bye(ibuf.data))
		return -1;

	verbose("S (%d): %s", ssn->socket, ibuf.data);

	if (xstrcasestr(ibuf.data, "* PREAUTH"))
		return STATUS_RESPONSE_PREAUTH;

	return STATUS_RESPONSE_NONE;
}
Пример #12
0
void*
receive_thread(void* arg)
{
    int socket_fd = get_client_socket_fd();

    struct sockaddr_in server_address;
    socklen_t server_address_len = sizeof(server_address);

    ssize_t msg_len;
    char msg[MTN_MSG_LEN + 1];
    while((msg_len = recvfrom(socket_fd, msg, MTN_MSG_LEN, 0,
                (struct sockaddr*) &server_address, &server_address_len)))
    {
        msg[msg_len] = '\0';

        if(msg_len >= MTN_MSG_TYPE_LEN)
        {
            mtn_message_type type = get_message_type(msg);

            switch(type)
            {
                case MTN_RESPONSE:
                    receive_response(msg, &server_address);
                    break;
                case MTN_DM_HEAD:
                    receive_data_message_head(msg, &server_address);
                    break;
                case MTN_DM_BLOCK:
                    receive_data_message_block(msg, &server_address);
                    break;
                default:
                    break;
            }
        }
    }

    pthread_exit(NULL);
}
Пример #13
0
static int raw_context_to_color(const char *raw, char **colors)
{
	int ret;
	int32_t ret_val;
	int fd;

	fd = setransd_open();
	if (fd < 0)
		return fd;

	ret = send_request(fd, RAW_CONTEXT_TO_COLOR, raw, NULL);
	if (ret)
		goto out;

	ret = receive_response(fd, RAW_CONTEXT_TO_COLOR, colors, &ret_val);
	if (ret)
		goto out;

	ret = ret_val;
out:
	close(fd);
	return ret;
}
static void *
replay_thread(void *arg)
{
	struct iovec iov[6];
	char space[1] = " ", crlf[2] = "\r\n";
	struct replay_thread *thr = arg;
	struct message *msg;
	enum shmlogtag tag;
	size_t len;
	char *ptr;
	const char *next;

	int i;

	int reopen = 1;

	while ((msg = mailbox_get(&thr->mbox)) != NULL) {
		tag = msg->tag;
		len = msg->len;
		ptr = msg->ptr;

		thread_log(2, 0, "%s(%s)", VSL_tags[tag], msg->ptr);

		switch (tag) {
		case SLT_RxRequest:
			if (thr->method != NULL)
				thr->bogus = 1;
			else
				thr->method = trimline(thr, ptr);
			break;

		case SLT_RxURL:
			if (thr->url != NULL)
				thr->bogus = 1;
			else
				thr->url = trimline(thr, ptr);
			break;

		case SLT_RxProtocol:
			if (thr->proto != NULL)
				thr->bogus = 1;
			else
				thr->proto = trimline(thr, ptr);
			break;

		case SLT_RxHeader:
			if (thr->nhdr >= sizeof thr->hdr / sizeof *thr->hdr) {
				thr->bogus = 1;
			} else {
				thr->hdr[thr->nhdr++] = trimline(thr, ptr);
				if (isprefix(ptr, "connection:", &next))
					thr->conn = trimline(thr, next);
			}
			break;

		default:
			break;
		}

		freez(msg->ptr);
		freez(msg);

		if (tag != SLT_ReqEnd)
			continue;

		if (!thr->method || !thr->url || !thr->proto) {
			thr->bogus = 1;
		} else if (strcmp(thr->method, "GET") != 0 &&
		    strcmp(thr->method, "HEAD") != 0) {
			thr->bogus = 1;
		} else if (strcmp(thr->proto, "HTTP/1.0") == 0) {
			reopen = !(thr->conn &&
			    strcasecmp(thr->conn, "keep-alive") == 0);
		} else if (strcmp(thr->proto, "HTTP/1.1") == 0) {
			reopen = (thr->conn &&
			    strcasecmp(thr->conn, "close") == 0);
		} else {
			thr->bogus = 1;
		}

		if (thr->bogus) {
			thread_log(1, 0, "bogus");
			goto clear;
		}

		if (thr->sock == -1) {
			for (;;) {
				thread_log(1, 0, "sleeping before connect...");
				usleep(1000 * (thr->fd % 3001));
				if ((thr->sock = VSS_connect(addr_info)) >= 0)
					break;
				thread_log(0, errno, "connect failed");
			}
		}

		thread_log(1, 0, "%s %s %s", thr->method, thr->url, thr->proto);

		iov[0].iov_base = thr->method;
		iov[0].iov_len = strlen(thr->method);
		iov[2].iov_base = thr->url;
		iov[2].iov_len = strlen(thr->url);
		iov[4].iov_base = thr->proto;
		iov[4].iov_len = strlen(thr->proto);
		iov[1].iov_base = iov[3].iov_base = space;
		iov[1].iov_len = iov[3].iov_len = 1;
		iov[5].iov_base = crlf;
		iov[5].iov_len = 2;
		if (writev(thr->sock, iov, 6) == -1) {
			thread_log(0, errno, "writev()");
			goto close;
		}

		for (i = 0; i < thr->nhdr; ++i) {
			thread_log(2, 0, "%d %s", i, thr->hdr[i]);
			iov[0].iov_base = thr->hdr[i];
			iov[0].iov_len = strlen(thr->hdr[i]);
			iov[1].iov_base = crlf;
			iov[1].iov_len = 2;
			if (writev(thr->sock, iov, 2) == -1) {
				thread_log(0, errno, "writev()");
				goto close;
			}
		}
		if (write(thr->sock, crlf, 2) == -1) {
			thread_log(0, errno, "writev()");
			goto close;
		}
		if (receive_response(thr) || reopen) {
close:
			thread_log(1, 0, "close");
			assert(thr->sock != -1);
			close(thr->sock);
			thr->sock = -1;
		}

		sleep(1);
clear:
		/* clean up */
		thread_clear(thr);
	}

	/* leftovers */
	thread_clear(thr);

	return (0);
}
Пример #15
0
int retrieve_stock_price(struct stock_price* price_info, char* url, parse_body_string_fun parse_body_string)
{
	int nret = 0;
	struct request * request = NULL;
	char *host_name = NULL;
	char *full_path = NULL;

	char *request_string = NULL;
	char *response_string = NULL;
	char *body_string = NULL;
	int body_size;
	char *p = NULL;

	struct ghbnwt_context *ctx = NULL;
	struct sockaddr_in sa;

	int res;
	int bufsize;

	int sock;

	url_parse (url, &host_name, &full_path);
	request = prepare_request(host_name, full_path);
	nret = 1;

	// Get server's ip adress.
	ctx =(struct ghbnwt_context *) malloc(sizeof(struct ghbnwt_context));
	ctx->host_name = host_name;
	ctx->hptr = gethostbyname (ctx->host_name);
	if (!ctx->hptr)
	{
		nret = -1;
		goto exit;
	}

	// Get socket descriptor.
	sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);

	// Connect to server.
	memset(&sa, 0, sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons (80);
	memcpy (&sa.sin_addr, *ctx->hptr->h_addr_list, 4);
	if(-1 == connect (sock, (const struct sockaddr *)&sa, sizeof(sa)))
	{
		nret = -1;
		goto exit;
	}

	// Send request.
	p = request_string = organize_request_string(request);
	bufsize = strlen(request_string);
	res = 0;
	while (bufsize > 0)
	{
		res = send(sock, p, bufsize, 0);
		if (res <= 0)
			break;
		p += res;
		bufsize -= res;
	}

	// Receive response.
	response_string = receive_response(sock, &body_string, &body_size);
	if (response_string == NULL)
	{
		nret = -1;
	}
	else if (parse_response(response_string) == 200)
	{
		body_string = read_body(sock, body_string, body_size);
		if (body_string)
		{
			// Parse string of body
			if (body_string)
				parse_body_string(price_info, body_string);
		}
	}
	else
	{
		nret = -1;
	}

exit:
	// Close a connection.
	close(sock);

	// Reclaim heap memory space.
	free(host_name);
	free(full_path);
	free(request->headers);
	free(request);
	free(request_string);
	free(response_string);
	free(body_string);

	return nret;
}