1. xemacs
  2. liece

Source

liece / dcc / 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/* dcc.c
 * Copyright (C) 1998-2002  Daiki Ueno
 *
 * This file is part of Liece.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#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 <stdlib.h>
#include <signal.h>
#include <fcntl.h>
#include <netinet/in.h>
#define _GNU_SOURCE
#include <getopt.h>

#ifndef MAXHOSTNAMELEN
# define MAXHOSTNAMELEN 31
#endif

#ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
#endif

#ifdef HAVE_MEMMOVE
# ifdef HAVE_LIBGEN_H
#  include <libgen.h>
#  ifdef basename
#   undef basename
#  endif
# endif
# include <string.h>
#else
# define memmove(x,y,z) bcopy((y), (x), (z))
#endif

#ifndef HAVE_BASENAME
# define basename(path) (rindex((path), '/') + 1)
#endif

static void usage();
static int prepare_listen_port();
static int prepare_connect_port();

static int receive_file();
static int send_file();
static int select_loop();
static int chat_listen();
static int chat_connect();

static u_long primary_address_of();
static u_long extract_addr_of_string();
static u_long get_address_externally();

static char *progname;

void version () {
	printf("%s (Liece) 1.4.0\n"
				 "Copyright (C) 1998, 1999 Daiki Ueno\n"
				 "This is free software; see the source for copying conditions.  There is NO\n"
				 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n", 
				 progname);
}

void usage() {
  printf("Usage: %s [global-options] command [command-options-and-arguments]\n"
				 "where global-options are -v, -h, etc.\n"
				 "where command is one of send, receive, chat, resolve.\n"
				 "where command-options-and-arguments depend on the specific command.\n\n"
				 "send <host> <port> <filename>\n"
				 "receive <host> <port> <size> <filename>\n"
				 "chat listen <port>\n"
				 "chat connect <host> <port>\n"
				 "resolve [hosts ...]\n",
				 progname);
}

int prepare_listen_port (int ip_port) {
  int sock, tries;
  int opt = 1;
  static struct sockaddr_in server;

  
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("opening stream socket");
    exit(1);
  }

#ifdef SO_REUSEADDR
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, 
		 (char *)&opt, sizeof (opt)) < 0) {
    perror ("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 ("binding stream socket");
	exit (1);
      }
      perror ("binding stream socket. retry in 20 seconds");
      sleep (20);		/* wait 20 seconds and try again */
    } else
      break;
  }
  listen (sock, 64);
  return (sock);
}

u_long get_address_externally(char *ircserver) {
  int i, len, dummy;
  u_long addr;
  struct hostent *hp;
  struct sockaddr_in server, client;

  addr = 0xc6290004;                          /* dummy addr --- rootA */
  if (ircserver && (hp = gethostbyname(ircserver)) != NULL) {
    addr = ntohl(((struct in_addr *)hp->h_addr_list[0])->s_addr);
  }
  if ((dummy = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("opening stream socket");
    return -1;
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(addr);
  server.sin_port = htons(7);                 /* dummy port --- echo */
  for (i = 0; i < 8; i++) {
    server.sin_zero[i] = 0;
  }
  if (connect(dummy, (struct sockaddr *)&server, sizeof(server)) < 0) {
    perror ("connecting remote socket");
    return -1;
  }
  len = sizeof(client);
  if (getsockname(dummy, (struct sockaddr *)&client, &len) < 0) 
    return -1;
  close(dummy);
  return ntohl(client.sin_addr.s_addr);
}


/*
 * 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 addr, bytessent = 0;
  char buf[ BUFSIZ * 8 ];
  fd_set readfds, writefds, fdset;
  struct stat statbuf;
  char namebuf[ MAXHOSTNAMELEN ];
  struct hostent *hp;
  struct sockaddr_in sin;

  if ((ifd = open (ifile, O_RDONLY)) < 0) { 
    /* error in opening file to send */
    close(ofd);
    return 1;
  }

  gethostname(namebuf, sizeof (namebuf));
  fstat (ifd, &statbuf);

  sock = prepare_listen_port(port);
  len = sizeof (struct sockaddr_in);
  if (getsockname(sock, (struct sockaddr *)&sin, &len) == 0) 
    port = ntohs(sin.sin_port);
 
	if ((addr = get_address_externally (NULL)) < 0) {
		gethostname(namebuf, sizeof (namebuf));
		if (hp = gethostbyname(namebuf)) 
			addr = ((struct in_addr *) (hp->h_addr_list)[0])->s_addr;
		else
			return 2;
	}

	printf ("DCC send %s %d %u %d\n", ifile, port, addr, statbuf.st_size);
  
  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[ BUFSIZ * 8 ];
  fd_set readfds, writefds, fdset;
  u_long netsize;
    
  if ((ofd = open(ifile, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
    fprintf(stderr, "open: opening file: %s\n", ifile);
    return 1;
  }
  ifd = prepare_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;
}

/*
 * select_loop(int sfd)
 * listens fd given, reads stdin and sends it to socket 
 * anything read from socket is send to stdout
 */
int select_loop (int sfd) {
  int ofd, len, bytesreceived = 0;
  char buf[ BUFSIZ * 8 ];
  fd_set readfds, writefds, fdset;

  for (;;) {
    FD_ZERO (&readfds);
    FD_SET (sfd, &readfds);
    FD_SET (0, &readfds);
    if (select (32, &readfds, 0, 0, 0) < 0) {
      perror ("select");
      close (sfd);
      return 1;
    }
	
    if (FD_ISSET (sfd, &readfds)) {
      if ((len = read(sfd, buf, sizeof (buf))) == 0) {
	close (sfd);
	return 0;
      }
      write (1, buf, len);
      FD_CLR (sfd, &readfds);
    }
    if (FD_ISSET (0, &readfds))	{
      if ((len = read (0, buf, sizeof (buf))) == 0) {
	close (sfd);
	return 0;
      }
      write(sfd, buf, len);
      FD_CLR (ofd, &readfds);
    }
  }
}

int prepare_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 ("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 ("connecting remote socket");
    return 0;
  }
  
  return sock;
}

u_long extract_addr_of_string (char *str) {
  u_long result = 0;

#ifndef HAVE_STRTOUL
  while (*str++) 
    result = result * 10 + *str - '0';
#else /* !HAVE_STRTOUL */
  result = strtoul(str, NULL, 10);
#endif /* HAVE_STRTOUL */
  return result;
}

u_long primary_address_of (char *host) {   
  struct hostent *hp;
  u_long addr;
  
  if ((hp = gethostbyname(host)) == NULL)
    addr = inet_addr(host);
  else
    memmove(&addr, hp->h_addr_list[ 0 ], 4);
  
  return ntohl(addr);
}

int chat_listen(int port) {
  struct sockaddr_in sin;
  struct hostent *hp;
	u_long addr;
  int sock, len;
  char namebuf[ MAXHOSTNAMELEN ];
    
  sock = prepare_listen_port (port);
  
  len = sizeof (struct sockaddr_in);
  if (getsockname(sock, (struct sockaddr *)&sin, &len) == 0) 
    port = ntohs(sin.sin_port);

	if ((addr = get_address_externally (NULL)) < 0) {
		gethostname(namebuf, sizeof (namebuf));
		if (hp = gethostbyname(namebuf)) 
			addr = ((struct in_addr *) (hp->h_addr_list)[0])->s_addr;
		else
			return 2;
	}

	printf("DCC chat %u %d\n", addr, port);
  
  if ((sock = accept(sock, (struct sockaddr *) 0, (int *) 0)) > -1) {
		printf("DCC chat established\n");
    return select_loop(sock);
	}
  
  return 1;
}

int chat_connect(u_long host, int port) {
  int sock;
  
  if ((sock = prepare_connect_port(host, port)) > -1) {
    printf("DCC chat established\n");
    return select_loop(sock);
  }
  
  return 1;
}


int main (int argc, char **argv) {
  char *host = "localhost";
  char *action;
  int c, status = 0;

  progname = (char *)basename(argv[ 0 ]);

	while (1)	{
		int this_option_optind = optind ? optind : 1;
		int option_index = 0;
		static struct option long_options[] =	{
			{"version", 0, 0, 'v'},
			{"help", 0, 0, 'h'},
			{0, 0, 0, 0}
		};
			
		c = getopt_long (argc, argv, "vh", long_options, &option_index);
		if (c == -1)
			break;
		
		switch (c) {
		case 'v':
			version();
			exit(1);
			break;
		case 'h':
			usage();
			exit(1);
			break;
		default:
			break;
		}
	}

  if (argc > 1) {
    action = argv[ 1 ];
  } else {
    usage();
    exit(1);
  }

  if (!strcmp(action, "resolve")) {
    if (argc < 3) {
      usage();
      exit(1);
    } else {
      u_long i, addr;
      for (i = 2; i < argc; i++) {
	addr = primary_address_of(argv[i]);
	if (addr != -1)
	  printf("%u\n", addr);
	else
	  printf("0\n");
      }
      status = 0;
    }
  }

  if (!strcmp(action, "send")) {
    if (argc != 4) {
      usage();
      exit(1);
    }
    status = send_file (atoi(argv[ 2 ]), argv[ 3 ]);
  } else if (!strcmp(action, "receive")) {
    if (argc != 6) {
      usage();
      exit(1);
    }
    status = 
      receive_file (extract_addr_of_string(argv[ 2 ]),
		    atoi(argv[ 3 ]), atoi(argv[ 4 ]), argv[ 5 ]);
  } else if (!strcmp(action, "chat")) {
    if (argc > 3) {
      if (!strcmp(argv[ 2 ], "listen")) {
	if (argc != 4) {
	  usage();
	  exit(1);
	}
	status = chat_listen(atoi(argv[ 3 ]));
      } else if (!strcmp(argv[ 2 ], "connect")) {
	if (argc != 5) {
	  usage();
	  exit(1);
	}
	status = chat_connect(extract_addr_of_string(argv[ 3 ]), 
			      atoi(argv[ 4 ]));
      } else {
	usage();
	exit(1);
      }	
    }
  } else {
    usage();
    exit(1);
  }

  return status;
}

/*
 * Local variables:
 *  compile-command: "gcc -DHAVE_STRTOUL -Wall -O6 -o dcc dcc.c"
 *  c-indent-level: 2
 *  c-basic-offset: 2
 *  tab-width: 2
 * End:
 */