Пример #1
0
/****************************************************************************
read 4 bytes of a smb packet and return the smb length of the packet
store the result in the buffer
This version of the function will return a length of zero on receiving
a keepalive packet.
timeout is in milliseconds.
****************************************************************************/
static ssize_t
read_smb_length_return_keepalive (int fd, char *inbuf, unsigned int timeout)
{
    ssize_t len = 0;
    int msg_type;
    BOOL ok = False;

    while (!ok)
    {
        if (timeout > 0)
            ok = (read_with_timeout (fd, inbuf, 4, 4, timeout) == 4);
        else
            ok = (read_data (fd, inbuf, 4) == 4);

        if (!ok)
            return (-1);

        len = smb_len (inbuf);
        msg_type = CVAL (inbuf, 0);

        if (msg_type == 0x85)
            DEBUG (5, ("Got keepalive packet\n"));
    }

    DEBUG (10, ("got smb length of %d\n", (int) len));

    return (len);
}
Пример #2
0
int receive_file_pipe(struct sockaddr_in *send_adr, socklen_t *send_adr_sz, char *filename, uint32_t seg_size) {
    // unpack thread local storage, extract this thread's reading port.
    argstruct* tls = (argstruct*)pthread_getspecific(thread_key);
    int clnt_thread_read_fd = tls->clnt_pipe_read_fd;

    off_t current_filesize = get_file_size(filename);
    uint32_t curr_segment_num = (current_filesize % seg_size != 0) ? 1 + current_filesize / seg_size : current_filesize / seg_size;

    char* response = (char*)malloc(sizeof(char)*MAX_BUFFER_SIZE);
    ssize_t response_len;
    buffer *b;
	create_buffer(&b, 1024);

    do {
        clnt_thread_rcv_t pkg = read_with_timeout(clnt_thread_read_fd, TIMEOUT, 0, NUM_TRY);
        printf("read with TIMEOUT %d second, NUMTRY %d times finished.\n", TIMEOUT, NUM_TRY);
        
        // unpack the pkg
        response = pkg.pac;
        response_len = (ssize_t)(pkg.pac_len);
        *send_adr = pkg.clnt_addr;
        *send_adr_sz = sizeof(struct sockaddr_in);

        akh_pdu_header *pheader = (akh_pdu_header *)response;

        // time-out
        if(response_len == -1)
            return -1;
        else if(pheader->msg_type == SS) {
            printf("SS seq_num => %d\n", pheader->seq_num);
            if(pheader->seq_num == curr_segment_num) {
                write_segment(response + sizeof(akh_pdu_header), response_len - sizeof(akh_pdu_header), filename);
                curr_segment_num++;
            }
            else {
		        push(b, response, response_len);
            }
            uint32_t temp;
            do {
                temp = curr_segment_num;
                pop(b, response, &response_len);
                pheader = (akh_pdu_header *)response;
                if(pheader->seq_num == curr_segment_num) {
                    write_segment(response + sizeof(akh_pdu_header), response_len - sizeof(akh_pdu_header), filename);
                    curr_segment_num++;
                }
                push(b, response, response_len);
            } while(pheader->seq_num == temp);

        }

        puts("< receive file segment >");
        printf("msg_type = %x\tseq_num = %d\n", pheader->msg_type, pheader->seq_num);
    } while(((akh_pdu_header *)response)->msg_type == SS);

    return ((akh_pdu_header *)response)->msg_type;
}
Пример #3
0
/*
 * Connect to watchman's socket.  Sets a socket send and receive
 * timeout of `timeout`.  Pass a {0} for no-timeout.  On error,
 * returns NULL and, if `error` is non-NULL, fills it in.
 */
struct watchman_connection *
watchman_connect(struct timeval timeout, struct watchman_error *error)
{
    struct watchman_connection *conn = NULL;
    /* If an environment variable WATCHMAN_SOCK is set, establish a connection
       to that address. Otherwise, run `watchman get-sockname` to start the
       daemon and retrieve its address. */
    const char *sockname_env = getenv("WATCHMAN_SOCK");
    if (sockname_env) {
        conn = watchman_sock_connect(sockname_env, timeout, error);
        goto done;
    }
    struct watchman_popen *p = watchman_popen_getsockname(error);
    if (p == NULL) {
        return NULL;
    }

    char buf[WATCHMAN_GET_SOCKNAME_MAX + 1];

    proto_t proto = read_with_timeout(p->fd, buf, WATCHMAN_GET_SOCKNAME_MAX, timeout);

    if (watchman_pclose(error, p)) {
        goto done;
    }
    if (proto_is_null(proto)) {
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Got bad or no JSON/BSER from watchman get-sockname");
        goto done;
    }
    if (!proto_is_object(proto)) {
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Got bad JSON/BSER from watchman get-sockname: object expected");
        goto bad_proto;
    }
    proto_t sockname_obj = proto_object_get(proto, "sockname");
    if (proto_is_null(sockname_obj)) {
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Got bad JSON/BSER from watchman get-sockname: "
                     "sockname element expected");
        goto bad_proto;
    }
    if (!proto_is_string(sockname_obj)) {
        watchman_err(error, WATCHMAN_ERR_WATCHMAN_BROKEN,
                     "Got bad JSON/BSER from watchman get-sockname:"
                     " sockname is not string");
        goto bad_proto;
    }
    const char *sockname = proto_strdup(sockname_obj);
    conn = watchman_sock_connect(sockname, timeout, error);
bad_proto:
    proto_free(proto);
done:
    return conn;
}
Пример #4
0
static int expect(int master, char *issue, char *expected)
{
	pstring buffer;
	int attempts, timeout, nread, len;
	BOOL match = False;

	for (attempts = 0; attempts < 2; attempts++)
	{
		if (!strequal(issue, "."))
		{
			if (lp_passwd_chat_debug())
				DEBUG(100, ("expect: sending [%s]\n", issue));

			write(master, issue, strlen(issue));
		}

		if (strequal(expected, "."))
			return True;

		timeout = 2000;
		nread = 0;
		buffer[nread] = 0;

		while ((len = read_with_timeout(master, buffer + nread, 1,
				 sizeof(buffer) - nread - 1, timeout)) > 0)
		{
			nread += len;
			buffer[nread] = 0;

			if ((match = unix_do_match(buffer, expected, False)))
				timeout = 200;
		}

		if (lp_passwd_chat_debug())
			DEBUG(100, ("expect: expected [%s] received [%s]\n",
				    expected, buffer));

		if (match)
			break;

		if (len < 0)
		{
			DEBUG(2, ("expect: %s\n", strerror(errno)));
			return False;
		}
	}

	return match;
}
Пример #5
0
/*******************************************************************
 read from a socket into memory.
 ********************************************************************/
BOOL prs_read(prs_struct *ps, int fd, size_t len, int timeout)
{
	BOOL ok;
	size_t prev_size = ps->buffer_size;
	if (!prs_grow(ps, len))
		return False;

	if (timeout > 0) {
		ok = (read_with_timeout(fd, &ps->data_p[prev_size],
		                            len, len,timeout) == len);
	} else {
		ok = (read_data(fd, &ps->data_p[prev_size], len) == len);
	}
	return ok;
}
Пример #6
0
Файл: uart.c Проект: dss91/omgps
/*
 * If the read size < expected length, retry even if EOF
 */
gboolean inline read_fixed_len(U1 *buf, int expected_len)
{
	int len = 0, count;

	while (TRUE) {
		count = read_with_timeout(&buf[len], expected_len);
		if (count == expected_len) {
			return TRUE;
		} else if (count <= 0) {
			uart_flush_output();
			return FALSE;
		} else {
			len += count;
			expected_len -= count;
			if (expected_len == 0)
				return TRUE;
		}
	}
}
Пример #7
0
/**
 * copy_between_fd is laid on top of write_in_full and read_with_timeout,
 * it returns total number byte that written, returned value is not equal
 * to expected_size to indicate an error. And it uses report_fd to report
 * progress, if invoke with report_fd == -1 then it report nothing.
 */
size_t copy_between_fd(int from, int to, int report_fd, off_t expected_size, int die_on_error){
	char buf[BUFSIZ];
	off_t total = 0;
	ssize_t nr;
	FILE *fp = NULL;

	if (report_fd != -1){
		fp = fdopen(report_fd, "w");
		if (fp == NULL)
			error("report_fd can be open with fdopen");
		setbuf(fp, NULL);
	}

	for (;;){
		nr = read_with_timeout(from, buf, BUFSIZ, TIMEOUT);
		if (nr == 0)
			break;
		if (nr < 0){
			if (die_on_error)
				fatal("timeout when reading");
			else
				break;
		}

		if (write_in_full(to, buf, nr) < 0){
			if (die_on_error)
				fatal("write error");
			else
				break;
		}
		total += nr;

		/* FIXME we should not report that frequently */
		if (fp != NULL)
			fprintf(fp, "\rtransported %.2f%%", (total/(double)expected_size)*100);
	}
	if (fp != NULL)
		fputc('\n', fp);

	return total;
}
Пример #8
0
void *copy_thread(void *info)
{
    signal(SIGWINCH, SIG_IGN);

    struct thread_info *ti = (struct thread_info *)info;
    int from = ti->from;
    int to = ti->to;

    char buffer[65536];
    while (1)
    {
        ssize_t r = read_with_timeout(from, buffer, sizeof(buffer), 1000000);
        if (r == -1) {
            fprintf(stderr, "Error reading from %d: %s\n", from, strerror(errno));
            break;
        }
        else if (r == 0) {
            if (ti->should_shutdown)
                shutdown(to, SHUT_WR);
            if (ti->terminate_on_read_eof) {
                exit(0);
                return voidp;
            }
            break;
        }
        else if (r == -2) {
            r = 0;
        }
        char *write_pos = buffer;
        while (r) {
            int w = write(to, write_pos, r);
            if (w > 0)
                r -= w;
            else if (w == 0 && r > 0) {
                fprintf(stderr, "Error: Cannot write %d bytes to %d: %s\n", w, to, strerror(errno));
                break;
            }
        }
    }
    return voidp;
}
Пример #9
0
		virtual nrpe::packet recv(const nrpe::packet &packet, boost::posix_time::seconds timeout) {
			std::vector<char> buf(packet.get_packet_length());
			read_with_timeout(buf, timeout);
			return nrpe::packet(&buf[0], buf.size(), packet.get_payload_length());
		}
Пример #10
0
void* play_game_one(void *data)
{
	int id = (int) data;
	char buffer[BUFFER_SIZE];
	int nwritten;
	int *player_hand_values = players_hand_values[id], *dealer_hand_values = dealers_hand_values[id];
	int *player_hand_suits = players_hand_suits[id], *dealer_hand_suits = dealers_hand_suits[id];
	int i;

	nplayers[id] = 0;
	ndealers[id] = 0;
	pthread_mutex_lock(&card_mutex);
	{
		for (i = 0; i < 2; ++i)
		{
			player_hand_values[nplayers[id]] = card_values[ncard];
			player_hand_suits[nplayers[id]] = card_suits[ncard];
			++ncard;
			++nplayers[id];
		}

		dealer_hand_values[ndealers[id]] = card_values[ncard];
		dealer_hand_suits[ndealers[id]] = card_suits[ncard];
		++ncard;
		++ndealers[id];
	}
	pthread_mutex_unlock(&card_mutex);

	buffer[0] = value_codes[player_hand_values[0]];
	buffer[1] = suit_codes[player_hand_suits[0]];
	buffer[2] = value_codes[player_hand_values[1]];
	buffer[3] = suit_codes[player_hand_suits[1]];
	buffer[4] = value_codes[dealer_hand_values[0]];
	buffer[5] = suit_codes[dealer_hand_suits[0]];
	buffer[6] = 0;

	if (BUFFER_SIZE != (nwritten = write(clientSockFds[id], buffer, BUFFER_SIZE)))
	{
		printf("Error! Couldn't write to client %d. Client will be disconnected.\n", clientSockFds[id]);
		close(clientSockFds[id]);
		clientSockFds[id] = 0;
		return NULL;
	}

	/* while player wishes to receive more cards */
	while (TRUE)
	{
		if (read_with_timeout(clientSockFds[id], buffer, tv_wait_rcv))
		{
			printf("\n");
			printf("Player %d Hand: ", clientSockFds[id]);
			display_state(player_hand_values, player_hand_suits, nplayers[id]);
			printf("Dealer Hand with player %d: ", clientSockFds[id]);
			display_state(dealer_hand_values, dealer_hand_suits, ndealers[id]);

			printf("I received from player %d: %s\n", clientSockFds[id], buffer);

			if (strcmp(buffer, HIT) == 0)
			{
				pthread_mutex_lock(&card_mutex);
				{
					player_hand_values[nplayers[id]] = card_values[ncard];
					player_hand_suits[nplayers[id]] = card_suits[ncard];

					buffer[0] = value_codes[player_hand_values[nplayers[id]]];
					buffer[1] = suit_codes[player_hand_suits[nplayers[id]]];
					buffer[2] = 0;

					++ncard;
					++nplayers[id];
				}
				pthread_mutex_unlock(&card_mutex);

				if (BUFFER_SIZE != (nwritten = write(clientSockFds[id], buffer, BUFFER_SIZE)))
				{
					printf("Error! Couldn't write to player %d.\n", clientSockFds[id]);
					close(clientSockFds[id]);
					clientSockFds[id] = 0;
					return NULL;
				}

				if (calc_sum(player_hand_values, nplayers[id]) > 21)
				{
					printf("Player %d busted. Dealer wins.\n", clientSockFds[id]);
					close(clientSockFds[id]);
					clientSockFds[id] = 0;
					return NULL;
				}
				else if (calc_sum(player_hand_values, nplayers[id]) == 21)
					break;
			}
			else if (strcmp(buffer, STAND) == 0)
				break;
			else
			{
				printf("Player %d didn't respond with a known command. Player will be disconnected.\n", clientSockFds[id]);
				close(clientSockFds[id]);
				clientSockFds[id] = 0;
				return NULL;
			}
		}
		else
		{
			printf("Player %d didn't respond on time. Player will be disconnected.\n", clientSockFds[id]);
			close(clientSockFds[id]);
			clientSockFds[id] = 0;
			return NULL;
		}
	}

	return NULL;
}