summaryrefslogtreecommitdiff
path: root/src/ipsort.c
blob: f7d37b9e5265865913b43cdb73faa4f7ba5ab2bf (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
/*
// AUTHOR
// N. Nielsen
//
// VERSION
// 0.2
//
// LICENSE
// This software is in the public domain.
//
// The software is provided "as is", without warranty of any kind,
// express or implied, including but not limited to the warranties
// of merchantability, fitness for a particular purpose, and
// noninfringement. In no event shall the author(s) be liable for any
// claim, damages, or other liability, whether in an action of
// contract, tort, or otherwise, arising from, out of, or in connection
// with the software or the use or other dealings in the software.
//
// SUPPORT
// Send bug reports to: <nielsen@memberwebs.com>
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <db.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>

#include <netinet/in.h>

void readIps(FILE* fIn);
void writeIps(FILE* fOut);

/* DB of traffic accessed recently */
DB* g_db = NULL;

/* TODO: Use an actual compare function */

int main(int argc, char* argv[])
{
	BTREEINFO bi;

	memset(&bi, 0, sizeof(bi));
	/* bi.compare = compareIp; */

	g_db = dbopen(NULL, O_CREAT | O_RDWR, 0, DB_BTREE, (const void*)&bi);

	if(g_db == NULL)
		errx(1, "db initialization failed");

	readIps(stdin);
	writeIps(stdout);
}

/*
 * Parse helpers
 */
static void eatSpace(FILE* file)
{
	char ch;

	do
	{
		ch = getc(file);

		if(ch == '#')
		{
			while(ch != '\n')
				ch = getc(file);
		}
	}
	while(isspace(ch) && !feof(file) && !ferror(file));

	ungetc(ch, file);
}

/*
 * Read Input
 */
void readIps(FILE* fIn)
{
	DBT key, value;
	struct in_addr addr;
	char address[16];

	key.data = &addr;
	key.size = sizeof(addr);
	value.data = &addr;
	value.size = sizeof(addr);

	eatSpace(fIn);

	while(!feof(fIn) && !ferror(fIn))
	{
		char ch;
		int i = 0;

		/* Read a word and store the first 16 chars */
		while(!feof(fIn) && !ferror(fIn))
		{
			ch = fgetc(fIn);

			if(isspace(ch))
				break;

			if(i < 16)
				address[i] = ch;

			i++;
		}

		address[i] = 0;

		if(!inet_pton(AF_INET, address, (void*)&addr))
			warnx("invalid ip in address: %s", address);

		if(g_db->put(g_db, &key, &value, 0) < 0)
			err(1, "database error: %s\n");

		eatSpace(fIn);
	}

	if(ferror(fIn))
		err(1, "error reading addresses");
}

/*
 * Write collated output back out
 */
void writeIps(FILE* fOut)
{
	DBT key, value;
	int ret;

	struct in_addr prev;			/* The previous address */

	struct in_addr top;		/* The address at the top of the current stack */
	int size;				/* The stack size */

	memset(&key, 0, sizeof(key));
	memset(&value, 0, sizeof(value));

	/* Get first key from DB */
	ret = g_db->seq(g_db, &key, &value, R_FIRST);

	while(ret == 0)
	{
		printf("%s\n", inet_ntoa(*((struct in_addr*)key.data)));
		ret = g_db->seq(g_db, &key, &value, R_NEXT);
	}
}