util.c 933 Bytes
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3
#include <sys/types.h>

#ifndef _WIN32
4 5
#include <sys/time.h>
#include <sys/socket.h>
Jack Moffitt's avatar
Jack Moffitt committed
6
#include <unistd.h>
7 8
#else
#include <windows.h>
Jack Moffitt's avatar
Jack Moffitt committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#endif

#include "sock.h"

#include "config.h"
#include "util.h"

int util_read_header(int sock, char *buff, unsigned long len)
{
	fd_set rfds;
	int read_bytes, ret;
	unsigned long pos;
	char c;
	struct timeval tv;
	ice_config_t *config;

	config = config_get_config();

	read_bytes = 1;
	pos = 0;
	ret = 0;

	while ((read_bytes == 1) && (pos < (len - 1))) {
		read_bytes = 0;

		FD_ZERO(&rfds);
		FD_SET(sock, &rfds);

		tv.tv_sec = config->header_timeout;
		tv.tv_usec = 0;

		if (select(sock + 1, &rfds, NULL, NULL, &tv) > 0) {
			if ((read_bytes = recv(sock, &c, 1, 0))) {
				if (c != '\r') buff[pos++] = c;
				if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {
					ret = 1;
					break;
				}
			}
		} else {
			break;
		}
	}

	if (ret) buff[pos] = '\0';
	
	return ret;
}