summaryrefslogtreecommitdiffstats
path: root/helper.c
blob: 2dc907e129832eacb9dc2fafb286413de138f0aa (plain) (blame)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "helper.h"
#include <socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <stdarg.h>

DebugLevel _debugLevel = DEBUG_WARNING;

void bail(char *args, ...)
{
	printf("ERROR: ");
	va_list argList;
	va_start(argList, args);
	vprintf(args, argList);
	va_end(argList);
	printf("\n");
	fflush(stdout);
	fflush(stderr);
	exit(1);
}

/**
 * 0 = SUCCESS, -1 = ERROR
 * on success, memory pointer and size variable will be updated.
 */
int helper_realloc(char **memory, size_t *curSize, const size_t newSize, const char *location)
{
	void *newBlock = realloc(*memory, newSize);
	if (newBlock == NULL) {
		printf("OUT OF MEMORY when trying to realloc to %d bytes at %s\n", (int)newSize, location);
		return -1;
	}
	*memory = newBlock;
	if (curSize != NULL) *curSize = newSize;
	return 0;
}

int helper_connect4(char *address, int port, char *ip)
{
	if (sizeof(struct in_addr) != 4) bail("Ach nöö ach nöö");
	if (inet_pton(AF_INET, address, ip) == 1) {
		const int sock = socket_tcp4b();
		if (sock == -1) return -1;
		if (socket_connect4(sock, ip, port) == 0) {
			return sock;
		}
		close(sock);
		return -1;
	}
	struct addrinfo hints;
	struct addrinfo *result, *rp;
	memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	const int ret = getaddrinfo(address, NULL, &hints, &result);
	if (ret != 0) {
		fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
		return -1;
	}
	for (rp = result; rp != NULL; rp = rp->ai_next) {
		const int sock = socket_tcp4b();
		if (sock == -1) continue;
		if (rp->ai_addr != NULL) {
			memcpy(ip, &((struct sockaddr_in*)rp->ai_addr)->sin_addr, 4);
			if (socket_connect4(sock, ip, port) == 0) {
				freeaddrinfo(result);
				return sock; // Success
			}
		}
		close(sock);
	}
	freeaddrinfo(result);
	return -1;
}

void helper_nonblock(const int fd)
{
	fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
}

static void helper_printavaInt(struct AttributeValueAssertion* a,const char* rel)
{
	printf("(%.*s%s", (int)a->desc.l, a->desc.s, rel);
	if (a->value.l != 0 && a->value.s != NULL) {
		printf("%.*s)", (int)a->value.l,a->value.s);
	} else {
		putchar(')');
	}
}

void helper_printava(struct AttributeValueAssertion* a,const char* rel)
{
	helper_printavaInt(a, rel);
	putchar('\n');
}

void helper_printal(struct AttributeDescriptionList* a)
{
	while (a) {
		if (a->a.s && a->a.l) printf("%.*s", (int)a->a.l,a->a.s);
		a=a->next;
		if (a) {
			putchar(',');
		}
	}
	putchar('\n');
}

void helper_printpal(struct PartialAttributeList *pal)
{
	printf("-- PAL --\n");
	while (pal) {
			printf("entry (");
			if (pal->type.l && pal->type.s) printf("%.*s", (int)pal->type.l, pal->type.s);
		printf("): ");
		helper_printal(pal->values);
		pal = pal->next;
	}
}

static void helper_printfilterInt(struct Filter* f)
{
	if (f == NULL) {
		printf("(nullfilter)");
		return;
	}
	while (f != NULL) {
		switch (f->type) {
		case AND:
			printf("(&");
mergesub:
			helper_printfilterInt(f->x);
			putchar(')');
			break;
		case OR:
			printf("(|");
			goto mergesub;
		case NOT:
			printf("(!");
			goto mergesub;
		case EQUAL:
			helper_printavaInt(&f->ava,"=");
			break;
		case SUBSTRING: {
			struct Substring* s=f->substrings;
			int first=1;
			printf("(%.*s",(int)f->ava.desc.l,f->ava.desc.s);
			printf(" has ");
			while (s) {
				if (!first) {
					printf(" and ");
				}
				first=0;
				switch(s->substrtype) {
				case prefix:
					printf("prefix \"");
					break;
				case any:
					printf("substr \"");
					break;
				case suffix:
					printf("suffix \"");
					break;
				}
				printf("%.*s\"",(int)s->s.l,s->s.s);
				s=s->next;
			}
			putchar(')');
		}
		break;
		case GREATEQUAL:
			helper_printavaInt(&f->ava,">=");
			break;
		case LESSEQUAL:
			helper_printavaInt(&f->ava,"<=");
			break;
		case PRESENT:
			f->ava.value.l = 0;
			helper_printavaInt(&f->ava,"=*");
			break;
		case APPROX:
			helper_printavaInt(&f->ava,"~=");
			break;
		case EXTENSIBLE:
			printf("[extensible]");
			break;
		default:
			printf("????????");
		}
		f = f->next;
	}
}

void helper_printfilter(struct Filter* f)
{
	helper_printfilterInt(f);
	putchar('\n');
}

void plog(const DebugLevel messageLevel, char *args, ...)
{
	if (messageLevel > _debugLevel) return;
	va_list argList;
	va_start(argList, args);
	vprintf(args, argList);
	va_end(argList);
	putchar('\n');
	fflush(stdout);
}