aboutsummaryrefslogtreecommitdiff
path: root/src/zonefile.c
blob: 13a2a347691018279c4cfa5cd32665859e696545 (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
/*
 * src/zonefile.c
 * (c) 2021 Jonas Gunz <himself@jonasgunz.de>
 * License: MIT
 */

#include "zonefile.h"

int zonefile_string_split(char* _parts[], ssize_t _max, char* _str, char _delim) {
	unsigned int i, o, start;

	start = 0;
	for ( i=0; i < _max; i++ ) {
		for ( o=start; _str[o] && _str[o] != ' '; o++ );

		_parts[i] = &_str[start];

		if(!_str[o])
			break;

		_str[o] = '\0';

		start = o+1;
	}

	return (int)i + 1;
}

int zonefile_parse_line(database_t *_database, char *_line) {
	char *parts[5];
	int parts_len;

	/* Does this work? */
	memset(&parts, 0, sizeof(parts));

	parts_len = zonefile_string_split(parts, 4, _line, ' ');
	if (parts_len != 4) {
		LOGPRINTF(_LOG_ERROR, "Incomplete");
		return -1;
	}

	/* parts is the first 5 space-seperated parts of _line */

	return -1;
}

int zonefile_to_database (database_t *_database, char* _file) {
	FILE *zfile = NULL;
	char *line = NULL;
	ssize_t line_len  = 0;
	unsigned int line_cnt = 0;

	zfile = fopen(_file, "r");
	if (!zfile) {
		LOGPRINTF(_LOG_ERRNO, "Could not open %s", _file);
		return -1;
	}

	DEBUG("Parsing zonefile %s", _file)

	while(!feof(zfile)) {
		line_cnt ++;
		line_len = getline(&line, 0, zfile);

		/* getline includes the line break. ONLY UNIX ENDINGS!! */
		if( line[line_len - 2] == '\n' )
			line[line_len - 2] = '\0';

		if ( zonefile_parse_line(_database, line) < 0) {
			LOGPRINTF(_LOG_ERROR, "Error is in line %u", line_cnt)
			return -1;
		}

		free(line);
		line = NULL;
	}

	fclose(zfile);
	return -1;
}