-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcc.c
351 lines (316 loc) · 7.45 KB
/
dcc.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
** see file irchat-copyright.el for change log and copyright info
*/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netdb.h>
#include <stdio.h>
#include <signal.h>
#include <fcntl.h>
#include <netinet/in.h>
#ifdef _AIX
#include <sys/select.h>
#endif
#ifdef USE_BCOPY
#define memmove(x,y,z) bcopy((y), (x), (z))
#endif
int setup_listen_port (int ip_port)
{
int sock, tries;
int opt = 1;
static struct sockaddr_in server;
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("SLP(): opening stream socket");
exit (1);
}
#ifdef SO_REUSEADDR
if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof (opt)) < 0)
{
perror ("SLP(): setsockopt SO_REUSEADDR");
}
#endif
/* Bind a port to listen for new connections */
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons (ip_port);
for (tries = 0; tries < 10; tries++)
{
if (bind (sock, (struct sockaddr *) &server, sizeof (server)))
{
if (tries >= 9)
{
perror ("SLP(): binding stream socket");
exit (1);
}
perror ("SLP(): binding stream socket. retry in 20 seconds");
sleep (20); /* wait 20 seconds and try again */
}
else
break;
}
listen (sock, 64);
return (sock);
}
/*
* send_file(int port, char *ifile)
* listens to connections to port, and when connection established
* sends ifile to that socket
*/
int send_file (int port, char *ifile)
{
int sock, ifd, ofd, len;
u_long bytessent = 0;
char buf[1024 * 8];
fd_set readfds, writefds, fdset;
struct stat statbuf;
char namebuf[31];
struct hostent *hp;
ifd = open (ifile, O_RDONLY);
if (ifd < 0)
{ /* error in opening file to send */
close (ofd);
return 1;
}
gethostname (namebuf, sizeof (namebuf));
fstat (ifd, &statbuf);
hp = gethostbyname (namebuf);
if (hp)
{
printf ("DCC send %s %d %u %d\n",
ifile, port,
ntohl (((struct in_addr *) (hp->h_addr_list)[0])->s_addr),
statbuf.st_size);
}
else
return 2;
sock = setup_listen_port (port);
ofd = accept (sock, (struct sockaddr *) 0, (int *) 0);
while ((len = read (ifd, buf, sizeof (buf))) > 0)
{
write (ofd, buf, len);
bytessent += len;
while ((len = read (ofd, buf, sizeof (u_long))) &&
ntohl (*(u_long *) buf) != bytessent);
}
close (ofd);
close (ifd);
printf ("*** DCC file %s sent\n", ifile);
return 0;
}
/*
* receive_file(u_long host, int port, char *ifile)
* connects to (host,port) and reads everything send from there
* for every packet received gives back how much actually got
* puts everything in ifile
*/
int receive_file (u_long host, int port, int size, char *ifile)
{
int sock, ifd, ofd, len, bytesreceived = 0, toread, prev = 0;
char buf[1024 * 8];
fd_set readfds, writefds, fdset;
u_long netsize;
if ((ofd = open (ifile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0)
{
fprintf (stderr, "ERROR opening file: %s\n", ifile);
return 1;
}
ifd = setup_connect_port (host, port);
if ((toread = sizeof (buf)) > size)
toread = size;
while (bytesreceived < size && (len = read (ifd, buf, toread)) > 0)
{
write (ofd, buf, len);
bytesreceived += len;
netsize = htonl (bytesreceived);
lseek (ifd, 0, 2);
write (ifd, &netsize, 4);
lseek (ifd, 0, 2);
if (toread > size - bytesreceived)
toread = size - bytesreceived;
if (bytesreceived - prev > size / 5)
{
printf ("DCC %s %d%% (%d/%d bytes) received\n", ifile,
100 * bytesreceived / size, bytesreceived, size);
prev = bytesreceived;
}
}
printf ("*** DCC file %s received\n", ifile);
close (ifd);
close (ofd);
return 0;
}
/*
* my_listen(int port)
* listens port given, reads stdin and sends it to socket
* anything read from socket is send to stdout
*/
int my_listen (int port)
{
int sock, sfd, ofd, len, bytesreceived = 0;
char buf[1024 * 8];
fd_set readfds, writefds, fdset;
sock = setup_listen_port (port);
sfd = accept (sock, (struct sockaddr *) 0, (int *) 0);
for (;;)
{
FD_ZERO (&readfds);
FD_SET (sfd, &readfds);
FD_SET (0, &readfds);
if (select (32, &readfds, 0, 0, 0) < 0)
{
perror ("ML(): select");
close (sfd);
return 1;
}
if (FD_ISSET (sfd, &readfds))
{
len = read (sfd, buf, sizeof (buf));
if (len == 0)
{
close (sfd);
return 0;
}
write (1, buf, len);
FD_CLR (sfd, &readfds);
}
if (FD_ISSET (0, &readfds))
{
len = read (0, buf, sizeof (buf));
if (len == 0)
{
close (sfd);
return 0;
}
write (sfd, buf, len);
FD_CLR (ofd, &readfds);
}
}
}
int setup_connect_port (u_long host, int port)
{
int sock;
static struct hostent *hp;
static struct sockaddr_in server;
sock = socket (AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror ("SCP(): opening stream socket");
exit (1);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = ntohl (host);
server.sin_port = htons (port);
if (connect (sock, (struct sockaddr *) &server, sizeof (server)) < 0)
{
perror ("SCP(): connecting remote socket");
return 0;
}
return sock;
}
/* some environments do not have strtoul() */
u_long fuckingatoul (char *str)
{
u_long result = 0;
while (*str)
{
result = result * 10 + *str - '0';
str++;
}
return result;
}
unsigned long primaryipaddressof (char *host)
{
struct hostent *hp;
unsigned long addr;
hp = gethostbyname(host);
if (hp == NULL)
addr = inet_addr(host);
else
memmove(&addr, hp->h_addr_list[0], 4);
return ntohl(addr);
}
int main (int argc, char **argv)
{
char *host = "localhost";
char *action;
int status = 0;
if (argc > 1)
{
action = argv[1];
}
else
{
fprintf (stderr, "Usage: %s send <port> <filename>\n", argv[0]);
fprintf (stderr, "Usage: %s receive <host> <port> <size> <filename>\n", argv[0]);
fprintf (stderr, "Usage: %s listen <port>\n", argv[0]);
fprintf (stderr, "Usage: %s resolve <host> [<host>]\n", argv[0]);
exit (1);
}
if (!strcmp (action, "resolve"))
{
if (argc < 3)
{
fprintf (stderr, "Wrong number of parameters.\n");
fprintf (stderr, "Usage: %s resolve <host>\n", argv[0]);
exit (1);
}
else
{
u_long i, addr;
for (i = 2; i < argc; i++)
{
addr = primaryipaddressof(argv[i]);
if (addr != -1)
printf("%u\n", addr);
else
printf("0\n");
}
status = 0;
}
}
if (!strcmp (action, "send"))
{
if (argc != 4)
{
fprintf (stderr, "Wrong number of parameters.\n");
fprintf (stderr, "Usage: %s send <port> <filename>\n", argv[0]);
exit (1);
}
status = send_file (atoi (argv[2]), argv[3]);
}
else if (!strcmp (action, "receive"))
{
if (argc != 6)
{
fprintf (stderr, "Wrong number of parameters.\n");
fprintf (stderr,
"Usage: %s receive <host> <port> <size> <filename>\n",
argv[0]);
exit (1);
}
status = receive_file (fuckingatoul (argv[2]),
atoi (argv[3]),
atoi (argv[4]), argv[5]);
}
else if (!strcmp (action, "listen"))
{
if (argc != 3)
{
fprintf (stderr, "Not enough parameters.\n");
fprintf (stderr, "Usage: %s listen <port>\n", argv[0]);
}
status = my_listen (atoi (argv[2]));
}
return status;
}
/*
** eof
*/