Beispiel #1
0
static bool_t hdhomerun_control_connect_sock(struct hdhomerun_control_sock_t *cs)
{
	if (cs->sock != -1) {
		return TRUE;
	}

	/* Find ip address. */
	uint32_t device_ip = cs->device_ip;
	if (device_ip == 0) {
		struct hdhomerun_discover_device_t result;
		if (hdhomerun_discover_find_device(cs->device_id, &result) <= 0) {
			return FALSE;
		}
		device_ip = result.ip_addr;
	}

	/* Create socket. */
	cs->sock = (int)socket(AF_INET, SOCK_STREAM, 0);
	if (cs->sock == -1) {
		return FALSE;
	}

	/* Set timeouts. */
	setsocktimeout(cs->sock, SOL_SOCKET, SO_SNDTIMEO, 1000);
	setsocktimeout(cs->sock, SOL_SOCKET, SO_RCVTIMEO, 1000);

	/* Initiate connection. */
	struct sockaddr_in sock_addr;
	memset(&sock_addr, 0, sizeof(sock_addr));
	sock_addr.sin_family = AF_INET;
	sock_addr.sin_addr.s_addr = htonl(device_ip);
	sock_addr.sin_port = htons(HDHOMERUN_CONTROL_TCP_PORT);
	if (connect(cs->sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) != 0) {
		hdhomerun_control_close_sock(cs);
		return FALSE;
	}

	/* Success. */
	return TRUE;
}
Beispiel #2
0
/* rfc1413 - return remote user name, given socket structures */
API_EXPORT(char *) ap_rfc1413(conn_rec *conn, server_rec *srv)
{
    RFC_USER_STATIC char user[RFC1413_USERLEN + 1];	/* XXX */
    RFC_USER_STATIC char *result;
    RFC_USER_STATIC int sock;

    result = FROM_UNKNOWN;

    sock = ap_psocket_ex(conn->pool, AF_INET, SOCK_STREAM, IPPROTO_TCP, 1);
    if (sock < 0) {
    	ap_log_error(APLOG_MARK, APLOG_CRIT, srv,
    		    "socket: rfc1413: error creating socket");
    	conn->remote_logname = result;
    }

    /*
     * Set up a timer so we won't get stuck while waiting for the server.
     */
#ifdef MULTITHREAD
    if (setsocktimeout(sock, ap_rfc1413_timeout) == 0) {
        if (get_rfc1413(sock, &conn->local_addr, &conn->remote_addr, user, srv) >= 0)
            result = ap_pstrdup (conn->pool, user);
    }
#else
    if (ap_setjmp(timebuf) == 0) {
	ap_set_callback_and_alarm(ident_timeout, ap_rfc1413_timeout);

	if (get_rfc1413(sock, &conn->local_addr, &conn->remote_addr, user, srv) >= 0)
	    result = user;
    }
    ap_set_callback_and_alarm(NULL, 0);
#endif
    ap_pclosesocket(conn->pool, sock);
    conn->remote_logname = result;

    return conn->remote_logname;
}
Beispiel #3
0
struct hdhomerun_video_sock_t *hdhomerun_video_create(uint16_t listen_port, size_t buffer_size)
{
	/* Create object. */
	struct hdhomerun_video_sock_t *vs = (struct hdhomerun_video_sock_t *)calloc(1, sizeof(struct hdhomerun_video_sock_t));
	if (!vs) {
		return NULL;
	}

	pthread_mutex_init(&vs->lock, NULL);

	/* Reset sequence tracking. */
	hdhomerun_video_flush(vs);

	/* Buffer size. */
	vs->buffer_size = (buffer_size / VIDEO_DATA_PACKET_SIZE) * VIDEO_DATA_PACKET_SIZE;
	if (vs->buffer_size == 0) {
		free(vs);
		return NULL;
	}
	vs->buffer_size += VIDEO_DATA_PACKET_SIZE;

	/* Create buffer. */
	vs->buffer = (uint8_t *)malloc(vs->buffer_size);
	if (!vs->buffer) {
		free(vs);
		return NULL;
	}
	
	/* Create socket. */
	vs->sock = (int)socket(AF_INET, SOCK_DGRAM, 0);
	if (vs->sock == -1) {
		free(vs->buffer);
		free(vs);
		return NULL;
	}

	/* Expand socket buffer size. */
	int rx_size = 1024 * 1024;
	setsockopt(vs->sock, SOL_SOCKET, SO_RCVBUF, (char *)&rx_size, sizeof(rx_size));

	/* Set timeouts. */
	setsocktimeout(vs->sock, SOL_SOCKET, SO_SNDTIMEO, 1000);
	setsocktimeout(vs->sock, SOL_SOCKET, SO_RCVTIMEO, 1000);

	/* Bind socket. */
	if (!hdhomerun_video_bind_sock(vs, listen_port)) {
		close(vs->sock);
		free(vs->buffer);
		free(vs);
		return NULL;
	}

	/* Start thread. */
	if (pthread_create(&vs->thread, NULL, &hdhomerun_video_thread_execute, vs) != 0) {
		close(vs->sock);
		free(vs->buffer);
		free(vs);
		return NULL;
	}

	/* Success. */
	return vs;
}
Beispiel #4
0
struct hdhomerun_dhcp_t *hdhomerun_dhcp_create(uint32_t bind_address)
{
	if (bind_address != 0) {
		if ((bind_address & 0xFFFF0000) != 0xA9FE0000) {
			return NULL;
		}
	}

	/* Create socket. */
	int sock = (int)socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1) {
		return NULL;
	}

	/* Set timeout. */
	setsocktimeout(sock, SOL_SOCKET, SO_RCVTIMEO, 1000);

	/* Allow broadcast. */
	int sock_opt = 1;
	setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&sock_opt, sizeof(sock_opt));

	/* Allow reuse. */
	sock_opt = 1;
	setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&sock_opt, sizeof(sock_opt));

	/* Bind socket. */
	struct sockaddr_in sock_addr;
	memset(&sock_addr, 0, sizeof(sock_addr));
	sock_addr.sin_family = AF_INET;
	sock_addr.sin_addr.s_addr = htonl(bind_address);
	sock_addr.sin_port = htons(67);
	if (bind(sock, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) != 0) {
		close(sock);
		return NULL;
	}

	/* Allocate object. */
	struct hdhomerun_dhcp_t *dhcp = (struct hdhomerun_dhcp_t *)calloc(1, sizeof(struct hdhomerun_dhcp_t));
	if (!dhcp) {
		close(sock);
		return NULL;
	}

	dhcp->sock = sock;

	if (bind_address != 0) {
		dhcp->local_address = bind_address;
	} else {
		dhcp->local_address = 0xA9FEFFFF;
	}

	/* Spawn thread. */
	if (pthread_create(&dhcp->thread, NULL, &hdhomerun_dhcp_thread_execute, dhcp) != 0) {
		close(sock);
		free(dhcp);
		return NULL;
	}

	/* Success. */
	return dhcp;
}