-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
216 lines (163 loc) · 5.2 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
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
/**
* @author Gar|k <garik.djan@gmail.com>
* @copyright (c) 2016, http://c0dedgarik.blogspot.ru/
* @version 0.1
*
*/
#include "main.h"
static int sigterm;
#define CHECK_TERM() if(0 != sigterm) break;
static void
sig_handler(int signum) {
sigterm = 1;
}
static void
print_usage(const char * name) {
err_quit("Usage: %s [KEY]... IP-LIST\n\n\
\t-n\tnumber asynchronous requests, default %d\n\
\t-o\toutput file found domains, default stdout\n\n", name, MAXPENDING);
}
static int
print_stat(options_t *options, const long time) {
char buf[MAXLINE];
size_t len;
len = snprintf(buf, sizeof (buf),
"Crawler: %zu; found: %zu; \
pending requests: %d; time: %ld milliseconds\n",
options->counters.domains,
options->counters.found,
options->pending_requests,
time
);
return writen(OUT_DEFAULT, buf, len);
}
static domain_t *
create_domain(options_t *options, const char *buf, const size_t size) {
domain_t * domain = (domain_t *) malloc(sizeof (domain_t));
if (NULL != domain) {
domain->options = options;
off_t offset = 0;
unsigned int *len = (unsigned int *) buf;
offset += sizeof (unsigned int);
domain->domain = (char *) malloc((*len) + 1);
memcpy(domain->domain, &buf[offset], *len);
domain->domain[*len] = 0; // null char
offset += *len;
len = (unsigned int *) &buf[offset];
offset += sizeof (unsigned int);
memcpy(&domain->servaddr.sin_addr, &buf[offset], *len);
offset += *len;
domain->servaddr.sin_family = AF_INET;
domain->servaddr.sin_port = htons(80);
domain->index_search = 0;
__sync_fetch_and_add(&domain->options->counters.domains, 1);
debug("%s: %s\n", domain->domain, inet_ntoa((struct in_addr) domain->servaddr.sin_addr));
if(http_request(domain) < 0) {
err_ret("http_request");
}
}
return domain;
}
void
free_domain(domain_t *domain) {
debug("-- free domain %s", domain->domain);
if (NULL != domain->domain) {
free(domain->domain);
}
if(domain->io.fd > 0) { /* close socket */
close(domain->io.fd);
}
free(domain);
}
static void
main_loop(options_t *options) {
off_t offset = 0;
char buf[MAXLINE];
size_t pending_requests = 0;
while (offset < options->file.len) {
file_record rec;
size_t len = readn(options->file.in, &rec, sizeof (rec));
if (sizeof (rec) == len) {
offset += len;
if (MAGIC == rec.magic && rec.data_len > 0) {
len = readn(options->file.in, buf, rec.data_len);
if (rec.data_len == len) {
offset += len;
domain_t * domain = create_domain(options, buf, rec.data_len);
if (NULL != domain) {
++pending_requests;
}
if (MAXPENDING == pending_requests) {
debug("pending_requests: %zu", pending_requests);
ev_run(options->loop, 0);
pending_requests = 0;
}
}
} else {
debug("error file_record");
continue;
}
} else {
err_ret("error read");
break;
}
CHECK_TERM();
}
if (pending_requests > 0) {
debug("pending_requests: %zu", pending_requests);
ev_run(options->loop, 0);
pending_requests = 0;
}
}
int
main(int argc, char** argv) {
const long time_start = mtime();
options_t options;
char *opts = "n:o:";
int opt, status = EXIT_SUCCESS;
if (1 == argc) {
print_usage(argv[0]);
}
sigterm = 0;
(void) signal(SIGHUP, sig_handler);
(void) signal(SIGINT, sig_handler);
(void) signal(SIGTERM, sig_handler);
memset(&options, 0, sizeof (options_t));
options.file.out = OUT_DEFAULT;
options.loop = EV_DEFAULT;
options.pending_requests = MAXPENDING;
while ((opt = getopt(argc, argv, opts)) != -1) {
switch (opt) {
case 'n':
options.pending_requests = atoi(optarg);
break;
case 'o':
if ((options.file.out = open(optarg, O_APPEND | O_CREAT | O_WRONLY,
S_IRUSR | S_IWUSR)) < 0) {
err_sys("[E] open output file %s", optarg);
}
break;
case '?':
print_usage(argv[0]);
}
}
if (argc == optind) {
print_usage(argv[0]);
}
if ((options.file.in = open(argv[optind], O_RDONLY)) > 0) {
struct stat statbuf;
if (fstat(options.file.in, &statbuf) < 0 && S_ISREG(statbuf.st_mode)) {
err_ret("fstat");
status = EXIT_FAILURE;
} else {
options.file.len = statbuf.st_size;
(void) main_loop(&options);
}
close(options.file.in);
} else {
err_ret("[E] domain list %s", argv[optind]);
status = EXIT_FAILURE;
}
print_stat(&options, (mtime() - time_start));
return status;
}