-
Notifications
You must be signed in to change notification settings - Fork 2
/
aircat.c
237 lines (202 loc) · 5.79 KB
/
aircat.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/* Airhook connect-and-test utility, copyright 2001 Dan Egnor.
* This software comes with ABSOLUTELY NO WARRANTY. You may redistribute it
* under the terms of the GNU General Public License, version 2.
* See the file COPYING for more details. */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "airhook.h"
enum { packet_size = 1500 };
struct message {
struct airhook_outgoing outgoing;
char data[airhook_message_size];
};
void send_message(struct airhook_socket *air,
unsigned char *begin, unsigned char *end)
{
struct airhook_data data;
struct message *msg = malloc(sizeof *msg);
if (NULL == msg) { perror("malloc"); exit(1); }
memcpy(msg->data, begin, end - begin);
data.begin = msg->data;
data.end = msg->data + (end - begin);
airhook_init_outgoing(&msg->outgoing, air, data, msg);
}
int run_airhook(int sock, struct sockaddr_in verify) {
unsigned long last_session = 0;
struct timeval now;
struct airhook_socket air;
unsigned char output[packet_size];
unsigned char input[airhook_message_size], *input_end = input;
airhook_init(&air, time(NULL));
gettimeofday(&now, NULL);
for (;;) {
int r;
fd_set rfd, wfd;
struct timeval tout, *pout = NULL;
struct airhook_data data;
struct airhook_status status;
struct airhook_outgoing *out;
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
FD_SET(0, &rfd);
FD_ZERO(&wfd);
status = airhook_status(&air);
if (status.remote_session != last_session) {
fprintf(stderr, "airhook: new remote session\n");
last_session = status.remote_session;
}
while (airhook_next_incoming(&air, &data)) {
fwrite(data.begin, data.end-data.begin, 1, stdout);
fflush(stdout);
}
if (now.tv_sec > status.next_transmit.second
|| (now.tv_sec == status.next_transmit.second
&& now.tv_usec > status.next_transmit.nanosecond / 1000))
FD_SET(sock, &wfd);
else {
tout.tv_sec = status.next_transmit.second;
tout.tv_usec = status.next_transmit.nanosecond / 1000;
if (tout.tv_sec > 0) {
if (tout.tv_usec < now.tv_usec) {
--tout.tv_sec;
tout.tv_usec += 1000000;
}
tout.tv_usec -= now.tv_usec;
tout.tv_sec -= now.tv_sec;
pout = &tout;
}
}
r = select(FD_SETSIZE, &rfd, &wfd, NULL, pout);
if (r < 0) {
perror("select");
return 1;
}
gettimeofday(&now, NULL);
if (r == 0) continue;
if (FD_ISSET(0, &rfd)) {
unsigned char *nl, *begin = input;
unsigned char * const old = input_end;
unsigned char * const limit = input + sizeof(input);
input_end = old + read(0, old, limit - old);
if (input_end <= old) return 0;
while ((nl = memchr(begin, '\n', input_end - begin))) {
send_message(&air, begin, nl + 1);
begin = nl + 1;
}
input_end -= begin - input;
memmove(input, begin, input_end - input);
while (input_end - input >= airhook_message_size) {
send_message(&air,
input, input + airhook_message_size);
input_end -= airhook_message_size;
memmove(input, input + airhook_message_size,
input_end - input);
}
}
if (FD_ISSET(sock, &rfd)) {
struct airhook_time when;
struct sockaddr_in from;
socklen_t fromlen = sizeof(from);
when.second = now.tv_sec;
when.nanosecond = now.tv_usec * 1000;
r = recvfrom(sock,
output, sizeof(output), 0,
(struct sockaddr *) &from, &fromlen);
if (r < 0)
perror("recvfrom");
else {
struct airhook_data data;
data.begin = output;
data.end = r + output;
if (!airhook_receive(&air, when, data))
fprintf(stderr, "invalid packet\n");
}
}
else if (FD_ISSET(sock, &wfd)) {
struct airhook_time when;
when.second = now.tv_sec;
when.nanosecond = now.tv_usec * 1000;
r = airhook_transmit(&air, when, packet_size, output);
if (r > 0) {
r = send(sock, output, r, 0);
if (r < 0) perror("send");
}
}
while (airhook_next_changed(&air, &out)) {
const struct airhook_outgoing_status status =
airhook_outgoing_status(out);
if (ah_confirmed == status.state) {
airhook_discard_outgoing(out);
free(status.user);
}
}
}
}
int main(int argc, char *argv[]) {
struct hostent *host;
int sock, local, remote;
struct sockaddr_in sin;
if (4 != argc) {
fputs(
"Airhook connect-and-test utility version 1, copyright 2001 Dan Egnor.\n"
"\n"
"usage: aircat lport rhost rport\n"
"Uses port <lport> and attempts to connect to a peer running on host\n"
"<rhost> and port <rport>. Lines of text from standard input are sent\n"
"as Airhook messages. Incoming messages are written to standard output.\n"
"\n"
"This software comes with ABSOLUTELY NO WARRANTY. You may redistribute it\n"
"under the terms of the GNU General Public License, Version 2.\n"
, stderr);
return 3;
}
local = atoi(argv[1]);
if (local <= 0 || local >= 65536) {
fprintf(stderr, "%s: invalid local port\n", argv[1]);
return 3;
}
remote = atoi(argv[3]);
if (remote <= 0 || remote >= 65536) {
fprintf(stderr, "%s: invalid remote port\n", argv[3]);
return 3;
}
host = gethostbyname(argv[2]);
if (NULL == host) {
herror(argv[2]);
return 2;
}
sin.sin_family = AF_INET;
if (sin.sin_family != host->h_addrtype
|| sizeof(sin.sin_addr) != host->h_length) {
fprintf(stderr, "%s: wrong address type\n", argv[2]);
return 2;
}
sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
perror("socket");
return 1;
}
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(local);
if (bind(sock, (struct sockaddr *) &sin, sizeof(sin))) {
perror("bind");
return 1;
}
sin.sin_addr = * (struct in_addr *) host->h_addr;
sin.sin_port = htons(remote);
if (connect(sock, (struct sockaddr *) &sin, sizeof(sin))) {
perror("connect");
return 1;
}
run_airhook(sock, sin);
return 1;
}