summaryrefslogtreecommitdiff
path: root/utils.c
blob: b9a4bed3a4526712e84f9d3bc3d8b58e7ac3533e (plain)
1
2
3
4
5
6
7
8
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <stdlib.h>
#include <errno.h>
#include <stdint.h>
#include <limits.h>
#include <arpa/inet.h>
#include <string.h>

#include "main.h"
#include "utils.h"

static unsigned total_malloc_count = 0;
static int malloc_count = 0;

LIST_HEAD(malloc_list);

struct allocation {
	void *caller;
	void *ptr;
	size_t size;
	struct list_head list;
};

static void
__add_allocation(void *caller, void *ptr, size_t size)
{
	struct allocation *a = malloc(sizeof(*a));

	a->caller = caller;
	a->ptr = ptr;
	a->size = size;
	list_add(&a->list, &malloc_list);
}

#define add_allocation(p, s) __add_allocation(__builtin_extract_return_addr(__builtin_return_address(0)), p, s)

void *
__zmalloc(size_t size)
{
	void *ptr;

	fprintf(stderr, "zmalloc: Allocation size %zi\n", size);
	ptr = calloc(1, size);
	if (ptr) {
		add_allocation(ptr, size);
		total_malloc_count++;
		malloc_count++;
	}
	return ptr;
}

char *
__xstrdup(const char *s)
{
	char *r;

	fprintf(stderr, "xstrdup: Allocation size %zi\n", strlen(s) + 1);
	r = strdup(s);
	if (r) {
		add_allocation(r, strlen(s) + 1);
		total_malloc_count++;
		malloc_count++;
	}
	return r;
}

void
xfree(void *ptr)
{
	struct allocation *a, *tmp;
	unsigned delete_count = 0;

	if (!ptr)
		return;
	free(ptr);
	malloc_count--;

	list_for_each_entry_safe(a, tmp, &malloc_list, list) {
		if (a->ptr == ptr) {
			list_del(&a->list);
			free(a);
			delete_count++;
		}
	}

	if (delete_count != 1) {
		error("Delete count is %u for ptr 0x%p\n", delete_count, ptr);
		exit(EXIT_FAILURE);
	}
}

void
debug_resource_usage()
{
	struct allocation *a;

	fprintf(stderr, "Still malloced %i (total %u)\n",
		malloc_count, total_malloc_count);

	list_for_each_entry(a, &malloc_list, list) {
		fprintf(stderr, "* Allocation - caller: 0x%p, ptr: 0x%p, size %zu\n",
			a->caller, a->ptr, a->size);
	}
}

uint16_t sockaddr_port(struct sockaddr_in46 *addr)
{
	switch (addr->storage.ss_family) {
	case AF_INET:
		return ntohs(addr->in4.sin_port);
	case AF_INET6:
		return ntohs(addr->in6.sin6_port);
	default:
		return 0;
	}
}

char *
sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t buflen)
{
	char abuf[ADDRSTRLEN];

	switch (addr->storage.ss_family) {
	case AF_INET:
		snprintf(buf, buflen, "AF_INET4 %s %u",
			 inet_ntop(addr->in4.sin_family, &addr->in4.sin_addr, abuf, sizeof(abuf)),
			 (unsigned)ntohs(addr->in4.sin_port));
		break;
	case AF_INET6:
		snprintf(buf, buflen, "AF_INET6 %s %u",
			 inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, abuf, sizeof(abuf)),
			 (unsigned)ntohs(addr->in6.sin6_port));
		break;
	default:
		snprintf(buf, buflen, "AF_UNKNOWN");
		break;
	}

	return buf;
}

int
strtou16_strict(const char *str, uint16_t *result)
{
	char *end;
	long val;

	if (!str)
		return -EINVAL;

	errno = 0;
	val = strtol(str, &end, 10);

	if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
		return -EINVAL;
	
	if (errno != 0 && val == 0)
		return -EINVAL;

	if (end == str)
		return -EINVAL;

	if (*end != '\0')
		return -EINVAL;

	if (val < 1 || val > UINT16_MAX)
		return -EINVAL;

	if (result)
		*result = val;
	return 0;
}