receiver.c 1.09 KB
Newer Older
1
#include <malloc.h>
2
#include "../util/common.h"
3
4
5
6
7
8
9
#include "../util/dbg.h"
#include "receiver.h"

PrrtReceiver *PrrtReceiver_create(const char *host, uint16_t port)
{
    PrrtReceiver *recv = calloc(1, sizeof(PrrtReceiver));
    check_mem(recv);
Andreas Schmidt's avatar
Andreas Schmidt committed
10
    recv->host_name = strdup(host);
11
	check_mem(recv->host_name);
12
13
    recv->port = port;

14
15
16
17
18
19
20
21
22
23
24
	struct addrinfo *info;
	struct addrinfo hints;
	char portstr[sizeof(port)*8+1];
	snprintf(portstr, sizeof(portstr), "%u", (unsigned int) port);
	memset(&hints, 0x0, sizeof(hints));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_DGRAM;
	check(0 == getaddrinfo(host, portstr, &hints, &info), "getaddrinfo");

	recv->ai = info;

25
26
	recv->csi = PrrtChannelStateInformation_create();

27
28
29
    return recv;

    error:
30
31
32
33
34
    if (recv != NULL) {
		free((void *) recv->host_name);
		free(recv);
	}
    //PERROR("Memory issue.%s","");
Andreas Schmidt's avatar
Andreas Schmidt committed
35
    return NULL;
36
37
}

38
39
40
41
42
bool PrrtReceiver_destroy(PrrtReceiver *receiver) {
	if (receiver->csi != NULL) {
		PrrtChannelStateInformation_destroy(receiver->csi);
	}

43
	freeaddrinfo(receiver->ai);
44
45
46
	free((void *) receiver->host_name);
	free(receiver);
	return true;
47
}