This repository has been archived by the owner on Jun 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
191 lines (151 loc) · 3.72 KB
/
main.c
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
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sqlite3.h>
#define DATABASE_PATH "./hosts.sqlite3"
#define CREATE_STMT "CREATE TABLE IF NOT EXISTS `rules` (" \
"`ip_address` TEXT NOT NULL, " \
"`host_name` TEXT NOT NULL, " \
"PRIMARY KEY(`ip_address`, `host_name`)" \
");"
sqlite3 *db;
#define INSERT_RULE_STMT "INSERT OR IGNORE INTO rules (ip_address, host_name) VALUES (?, ?)"
#define MAX_IP_ADDRESS_LENGTH 64
#define MAX_HOST_NAME_LENGTH 1024
int parse_hosts_file(const char *path) {
FILE *fp = fopen(path, "rb");
if (fp == NULL) return 1;
sqlite3_stmt *stmt;
int rc = sqlite3_prepare_v2(db, INSERT_RULE_STMT, -1, &stmt, NULL);
if (rc == SQLITE_OK) {
rc = sqlite3_exec(db, "BEGIN TRANSACTION", NULL, NULL, NULL);
}
if (rc != SQLITE_OK) {
sqlite3_finalize(stmt);
sqlite3_close(db);
fclose(fp);
return 1;
}
char *line = NULL;
size_t len;
ssize_t read;
char ip_address[MAX_IP_ADDRESS_LENGTH];
char host_name[MAX_HOST_NAME_LENGTH];
while ((read = getline(&line, &len, fp)) != -1) {
// Ignore empty lines
if (read == 0 ||
(read == 1 && line[0] == '\n') ||
(read == 1 && line[0] == '\r') ||
(read == 2 && line[0] == '\r' && line[1] == '\n')) {
continue;
}
// Ignore commented-out lines
if (line[0] == '#') {
continue;
}
ssize_t l = 0, r = 0;
// Remove possible comment
char *p = strchr(line, '#');
if (p != NULL) {
*p = '\0';
r = p - line - 1;
} else {
r = read - 1;
}
// Find whitespace offsets
while (line[l] && isspace(line[l])) l++;
while (line[r] && isspace(line[r])) r--;
if (l > r) {
continue;
}
// Get the IP address
ssize_t n = 0;
while (l <= r && (line[l] == '.' || line[l] == ':' || isxdigit(line[l]))) {
ip_address[n++] = line[l++];
}
ip_address[n] = '\0';
if (strlen(ip_address) == 0) {
continue;
}
if (sqlite3_bind_text(stmt, 1, ip_address, -1, SQLITE_TRANSIENT) != SQLITE_OK) {
free(line);
fclose(fp);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 1;
}
// Get the host names
while (l <= r) {
// Skip whitespace
while (l <= r && isspace(line[l])) l++;
ssize_t j = 0;
while (l <= r && !isspace(line[l])) {
host_name[j++] = line[l++];
}
host_name[j] = '\0';
if (strlen(host_name) == 0) {
continue;
}
rc = sqlite3_bind_text(stmt, 2, host_name, -1, SQLITE_TRANSIENT);
if (rc == SQLITE_OK) {
rc = sqlite3_step(stmt);
}
if (rc == SQLITE_DONE) {
rc = sqlite3_reset(stmt);
}
if (rc != SQLITE_OK) {
free(line);
fclose(fp);
sqlite3_finalize(stmt);
sqlite3_close(db);
return 1;
}
}
}
free(line);
if (fclose(fp) != 0) return 1;
rc = sqlite3_exec(db, "END TRANSACTION", NULL, NULL, NULL);
sqlite3_finalize(stmt);
if (rc != SQLITE_OK) {
sqlite3_close(db);
return 1;
}
return 0;
}
int main(const int argc, const char **argv) {
char *hosts_file = NULL;
if (argc == 2) {
hosts_file = (char *) *(argv + 1);
} else {
puts("Invalid number of arguments!");
if (argc == 1) {
printf("Usage: %s <hosts-file>\nExample: %s /etc/hosts\n", argv[0], argv[0]);
}
return 1;
}
puts("Parsing:");
puts(hosts_file);
int rc = sqlite3_open(DATABASE_PATH, &db);
if (rc != SQLITE_OK) {
puts("Failed to open or create the database!");
return 1;
}
if (sqlite3_exec(db, CREATE_STMT, NULL, NULL, NULL) != SQLITE_OK) {
sqlite3_close(db);
puts("Failed to create the rules table!");
return 1;
}
if (parse_hosts_file(hosts_file) != 0) {
sqlite3_close(db);
puts("Failed to parse the hosts file!");
return 1;
}
if(sqlite3_close(db) != SQLITE_OK) {
puts("Failed to close the database!");
return 1;
}
puts("Sucessfully parsed the hosts file!");
return 0;
}