Check-in [00184e2c99]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Added standalone file_sync Updated file_sync to correct warnings issued by GCC4
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:00184e2c99e16eee17ac69190348ac5eb941279e
User & Date: rkeene 2008-11-07 21:32:42
Context
2008-11-07
21:33
Added DPRINTF() macro to non-DEBUG builds check-in: dc0365821d user: rkeene tags: trunk
21:32
Added standalone file_sync Updated file_sync to correct warnings issued by GCC4 check-in: 00184e2c99 user: rkeene tags: trunk
2008-11-06
20:53
Updated file_sync tool to use the same blocksize size in the sender as the receiver. check-in: 73a43a3c3b user: rkeene tags: trunk
Changes

Added tools/file_sync-standalone.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
#ifdef DEBUG
#define PRINT_LINE fprintf(stderr, "%lu:%s:%i:%s(): ", (unsigned long) time(NULL), __FILE__, __LINE__, __func__); 
#define PRINTERR_D(x...) { PRINT_LINE; fprintf(stderr, x); fprintf(stderr, "\n"); }
#define PERROR(x) { PRINT_LINE; perror(x); }
#define DPERROR(x) PERROR(x)
#define DPRINTF(x...) { PRINT_LINE; fprintf(stderr, x); fprintf(stderr, "\n"); }
#else
#define PRINT_LINE /**/
#define PRINTERR_D(x...) /**/
#define PERROR(x) perror(x);
#define DPERROR(x) /**/
#define DPRINTF(x...) /**/
#endif
#define CHECKPOINT PRINTERR_D("*** CHECKPOINT REACHED ***")
#define SPOTVAR_LLX(x) PRINTERR_D(#x " = %llx", (unsigned long long) x)
#define SPOTVAR_LLU(x) PRINTERR_D(#x " = %llu", (unsigned long long) x)
#define SPOTVAR_LU(x) PRINTERR_D(#x " = %lu", (unsigned long) x)
#define SPOTVAR_D(x) PRINTERR_D(#x " = %lf", x)
#define SPOTVAR_X(x) PRINTERR_D(#x " = 0x%x", (unsigned int) x)
#define SPOTVAR_I(x) PRINTERR_D(#x " = %i", (int) x)
#define SPOTVAR_P(x) PRINTERR_D(#x " = %p", x)
#define SPOTVAR_S(x) PRINTERR_D(#x " = \"%s\"", x)
#define PRINTERR(x...) { PRINT_LINE; fprintf(stderr, x); fprintf(stderr, "\n"); fflush(stderr); }
#define LOG(x...) { PRINT_LINE; fprintf(stderr, "LOG: " x); fprintf(stderr, "\n"); }

#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <endian.h>
#include <signal.h>
#include <limits.h>
#include <stdint.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <netdb.h>
#include <time.h>

#ifndef INT_MAX
#define INT_MAX (0x7fffffff)
#endif

#ifndef O_LARGEFILE
#define O_LARGEFILE 0
#endif

#ifndef __BIG_ENDIAN
#define __BIG_ENDIAN 4321
#endif
#ifndef __LITTLE_ENDIAN
#define __LITTLE_ENDIAN 1234
#endif
#ifndef __BYTE_ORDER
#ifdef WORDS_BIGENDIAN
#define __BYTE_ORDER __BIG_ENDIAN
#else
#define __BYTE_ORDER __LITTLE_ENDIAN
#endif
#endif


/* MD4.H FOLLOWS */
/* MD4.H - header file for MD4C.C
 */

/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
   rights reserved.

   License to copy and use this software is granted provided that it
   is identified as the "RSA Data Security, Inc. MD4 Message-Digest
   Algorithm" in all material mentioning or referencing this software
   or this function.

   License is also granted to make and use derivative works provided
   that such works are identified as "derived from the RSA Data
   Security, Inc. MD4 Message-Digest Algorithm" in all material
   mentioning or referencing the derived work.

   RSA Data Security, Inc. makes no representations concerning either
   the merchantability of this software or the suitability of this
   software for any particular purpose. It is provided "as is"
   without express or implied warranty of any kind.

   These notices must be retained in any copies of any part of this
   documentation and/or software.
 */

/* MD4 context. */
typedef struct {
  uint32_t state[4];    /* state (ABCD) */
  uint32_t count[2];    /* number of bits, modulo 2^64 (lsb first) */
  unsigned char buffer[64];                         /* input buffer */
} rsaref_MD4_CTX;

static void rsaref_MD4Init(rsaref_MD4_CTX *);
static void rsaref_MD4Update(rsaref_MD4_CTX *, unsigned char *, unsigned int);
static void rsaref_MD4Final(unsigned char [16], rsaref_MD4_CTX *);
/* MD4.H ENDS */

uint64_t net_bytesout = 0;
uint64_t net_bytesin = 0;

static uint64_t htonll(uint64_t n) {
        uint64_t retval;

#if __BYTE_ORDER == __BIG_ENDIAN
        retval = n;
#else
        retval = ((uint64_t) htonl(n & 0xFFFFFFFFLLU)) << 32;
        retval |= htonl((n & 0xFFFFFFFF00000000LLU) >> 32);
#endif
        return(retval);
}

static uint64_t ntohll(uint64_t n) {
        uint64_t retval;

#if __BYTE_ORDER == __BIG_ENDIAN
        retval = n;
#else
        retval = ((uint64_t) ntohl(n & 0xFFFFFFFFLLU)) << 32;
        retval |= ntohl((n & 0xFFFFFFFF00000000LLU) >> 32);
#endif
        return(retval);
}

static int net_listen(const int port) {
	struct sockaddr_in localname;
	int sockFd;

	sockFd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
	localname.sin_family = AF_INET;
	localname.sin_port = htons(port);
	localname.sin_addr.s_addr = INADDR_ANY;

	if (sockFd < 0) {
		return(-1);
	}

	if (bind(sockFd, (struct sockaddr *) &localname, sizeof(localname)) < 0) {
		return(-1);
	}

	if (listen(sockFd, 1024) < 0) {
		return(-1);
	}

	return(sockFd);
}


static int net_connect_tcp(const char *host, const int port) {
	int sockid;
	struct hostent *hostinfo;
	struct sockaddr_in sock;

	if ( (sock.sin_addr.s_addr=inet_addr(host) )==-1) {
		if ((hostinfo=gethostbyname(host))==NULL) return(-1);
		memcpy(&sock.sin_addr.s_addr,hostinfo->h_addr_list[0],hostinfo->h_length);
	}

	sock.sin_family=AF_INET;
	sock.sin_port=htons(port);
	if ((sockid=socket(AF_INET, SOCK_STREAM, 0))<0) {
		return(-1);
	}
	if (connect(sockid, (struct sockaddr *) &sock, sizeof(sock))<0) {
		close(sockid);
		return(-1);
	}
	return(sockid);
}

static int print_help(int ret) {
	fprintf(stderr, "Usage: file_sync -t <host> <port> <file> [<blocksize>]\n");
	fprintf(stderr, "Usage: file_sync -r <port> <file>\n");
	return(ret);
}

static ssize_t read_large(int fd, unsigned char *buf, size_t count) {
	size_t bytestoread;
	ssize_t read_ret;
	ssize_t retval = 0;

	while (count) {
		bytestoread = count;

		if (bytestoread >= INT_MAX) {
			bytestoread = INT_MAX - 1;
		}

		read_ret = read(fd, buf, bytestoread);
		if (read_ret < 0) {
			/*
			 * Only indicate an error if we have not read anything
			 * into the buffer.  Subsequent calls to read() should
			 * generate the error again.  Hopefully.
			 */
			if (retval == 0) {
				retval = -1;
			}
			break;
		}

		if (read_ret == 0) {
			break;
		}

		count -= read_ret;
		buf += read_ret;
		retval += read_ret;
	}

	return(retval);
}

static ssize_t write_large(int fd, unsigned char *buf, size_t count) {
	size_t bytestowrite;
	ssize_t write_ret;
	ssize_t retval = 0;

	while (count) {
		bytestowrite = count;

		if (bytestowrite >= INT_MAX) {
			bytestowrite = INT_MAX - 1;
		}

		write_ret = write(fd, buf, bytestowrite);
		if (write_ret < 0) {
			if (retval == 0) {
				retval = -1;
			}
			break;
		}

		if (write_ret == 0) {
			break;
		}

		count -= write_ret;
		buf += write_ret;
		retval += write_ret;
	}

	return(retval);
}

static ssize_t read_large_net(int fd, void *buf, size_t count) {
	ssize_t retval;

	retval = read_large(fd, buf, count);

	if (retval > 0) {
		net_bytesin += retval;
	}

	DPRINTF("read_large_net(fd=%i, buf=%p, count=%lu) = %li", fd, buf, (unsigned long) count, (long) retval);

	return(retval);
}

static ssize_t write_large_net(int fd, void *buf, size_t count) {
	ssize_t retval;

	retval = write_large(fd, buf, count);
	if (retval > 0) {
		net_bytesout += retval;
	}

	DPRINTF("write_large_net(fd=%i, buf=%p, count=%lu) = %li", fd, buf, (unsigned long) count, (long) retval);

	return(retval);
}

static int sync_transmit(const char *host, int port, const char *file, uint64_t blocksize) {
	rsaref_MD4_CTX mdctx;
	unsigned char md4buf[16];
	unsigned char *buf;
	uint64_t filesize;
	uint32_t blockok_size, blockidx;
	uint8_t *blockok, blockok_val;
	ssize_t read_ret, write_ret;
	off_t lseek_ret;
	off_t curpos, destpos;
	int retval = 0;
	int sockfd;
	int fd;
	int i;

	fd = open(file, O_RDONLY | O_LARGEFILE);
	if (fd < 0) {
		CHECKPOINT;
		return(-1);
	}

	lseek_ret = lseek(fd, 0, SEEK_END);
	if (lseek_ret < 0) {
		close(fd);
		CHECKPOINT;
		return(-1);
	}

	filesize = lseek_ret;

	blockok_size = filesize / (8 * blocksize);
	if ((filesize % (8 * blocksize)) != 0) {
		blockok_size++;
	}

	blockok = malloc(blockok_size);
	if (!blockok) {
		close(fd);
		CHECKPOINT;
		return(-1);
	}

	lseek(fd, 0, SEEK_SET);

	sockfd = net_connect_tcp(host, port);
	if (sockfd < 0) {
		close(fd);
		CHECKPOINT;
		return(-1);
	}

	buf = calloc(blocksize, 1);
	if (!buf) {
		close(fd);
		close(sockfd);
		CHECKPOINT;
		return(-1);
	}

	filesize = htonll(filesize);
	blocksize = htonl(blocksize);

	write_ret = write_large_net(sockfd, &filesize, sizeof(filesize));
	if (write_ret != sizeof(filesize)) {
		close(fd);
		close(sockfd);
		CHECKPOINT;
		return(-1);
	}

	write_ret = write_large_net(sockfd, &blocksize, sizeof(blocksize));
	if (write_ret != sizeof(blocksize)) {
		close(fd);
		close(sockfd);
		CHECKPOINT;
		return(-1);
	}

	filesize = ntohll(filesize);
	blocksize = ntohl(blocksize);

	while (1) {
		read_ret = read_large(fd, buf, blocksize);
		if (read_ret < 0) {
			retval = -1;
			CHECKPOINT;
			break;
		}

		if (read_ret == 0) {
			CHECKPOINT;
			break;
		}

		rsaref_MD4Init(&mdctx);
		rsaref_MD4Update(&mdctx, buf, read_ret);
		rsaref_MD4Final(md4buf, &mdctx);

		write_ret = write_large_net(sockfd, md4buf, sizeof(md4buf));
		if (write_ret != sizeof(md4buf)) {
			retval = -1;
			CHECKPOINT;
			break;
		}
	}

	if (retval != -1) {
		read_ret = read_large_net(sockfd, blockok, blockok_size);

		if (read_ret != blockok_size) {
			retval = -1;
			CHECKPOINT;
		}
	}

	lseek(fd, 0, SEEK_SET);
	curpos = 0;

	if (retval != -1) {
		for (blockidx = 0; blockidx < blockok_size; blockidx++) {
			for (i = 0; i < 8; i++) {
				blockok_val = blockok[blockidx] & (1<<i);
				if (!blockok_val) {

					destpos = (blockidx * 8 + i) * blocksize;

					if (curpos != destpos) {
						lseek_ret = lseek(fd, destpos, SEEK_SET);
						if (lseek_ret != destpos) {
							retval = -1;
							CHECKPOINT;
							break;
						}
						curpos = lseek_ret;
					}

					lseek_ret = curpos;

					if (lseek_ret >= filesize) {
						continue;
					}

					read_ret = read_large(fd, buf, blocksize);
					if (read_ret != blocksize) {
						if ((lseek_ret + read_ret) != filesize || read_ret < 0) {
							SPOTVAR_I(blockidx);
							SPOTVAR_LLU(lseek_ret);
							SPOTVAR_LLU(read_ret);
							SPOTVAR_LLU(filesize);
							retval = -1;
							CHECKPOINT;
							break;
						}
					}

					curpos += read_ret;

					write_ret = write_large_net(sockfd, buf, read_ret);
					if (write_ret != read_ret) {
						SPOTVAR_LLU(write_ret);
						SPOTVAR_LLU(read_ret);
						retval = -1;
						CHECKPOINT;
						break;
					}
				}
			}
			if (retval == -1) {
				break;
			}
		}
	}

	close(fd);
	close(sockfd);

	return(retval);
}

static int sync_receive(int port, const char *file) {
	rsaref_MD4_CTX mdctx;
	unsigned char md4buf[16], check_md4buf[16];
	unsigned char *buf;
	uint64_t filesize, filesize_s;
	uint64_t blocksize;
	uint32_t blockok_size, blockidx = 0;
	uint8_t *blockok, blockok_val;
	ssize_t cur_read_ret, read_ret, write_ret;
	size_t bytestowrite;
	off_t lseek_ret;
	off_t curpos, destpos;
	int sockfd, master_sockfd;
	int retval = 0;
	int fd;
	int skipbytes;
	int i = 0;

	fd = open(file, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
	if (fd < 0) {
		CHECKPOINT;
		return(-1);
	}

	master_sockfd = net_listen(port);
	if (master_sockfd < 0) {
		close(fd);
		CHECKPOINT;
		return(-1);
	}

	sockfd = accept(master_sockfd, NULL, 0);
	if (sockfd < 0) {
		close(fd);
		close(master_sockfd);
		CHECKPOINT;
		return(-1);
	}

	read_ret = read_large_net(sockfd, &filesize, sizeof(filesize));
	if (read_ret != sizeof(filesize)) {
		close(fd);
		close(sockfd);
		close(master_sockfd);
		CHECKPOINT;
		return(-1);
	}

	read_ret = read_large_net(sockfd, &blocksize, sizeof(blocksize));
	if (read_ret != sizeof(blocksize)) {
		close(fd);
		close(sockfd);
		close(master_sockfd);
		CHECKPOINT;
		return(-1);
	}

	filesize_s = filesize = ntohll(filesize);
	blocksize = ntohl(blocksize);

	blockok_size = filesize / (8 * blocksize);
	if ((filesize % (8 * blocksize)) != 0) {
		blockok_size++;
	}

	blockok = malloc(blockok_size);
	if (!blockok) {
		close(fd);
		close(sockfd);
		close(master_sockfd);
		CHECKPOINT;
		return(-1);
	}

	buf = calloc(blocksize, 1);
	if (!buf) {
		close(fd);
		close(sockfd);
		close(master_sockfd);
		CHECKPOINT;
		return(-1);
	}

	while (filesize) {
		cur_read_ret = read_ret = read_large(fd, buf, blocksize);
		if (read_ret < 0) {
			retval = -1;
			CHECKPOINT;
			break;
		}

		bytestowrite = blocksize;
		if (bytestowrite > filesize) {
			bytestowrite = filesize;
		}

		if (read_ret != blocksize) {
			memset(buf, '\0', bytestowrite);
			read_ret = bytestowrite;
			skipbytes = bytestowrite;
		} else {
			skipbytes = 0;
		}

		rsaref_MD4Init(&mdctx);
		rsaref_MD4Update(&mdctx, buf, read_ret);
		rsaref_MD4Final(md4buf, &mdctx);

		read_ret = read_large_net(sockfd, check_md4buf, sizeof(check_md4buf));
		if (read_ret != sizeof(check_md4buf)) {
			retval = -1;
			CHECKPOINT;
			break;
		}

		if (memcmp(check_md4buf, md4buf, sizeof(md4buf)) == 0) {
			blockok_val = 1;
		} else {
			blockok_val = 0;
		}

		if (i == 0 || i == 8) {
			if (i == 8) {
				blockidx++;
			}
			i = 0;
			blockok[blockidx] = 0;
		}
		blockok[blockidx] |= (blockok_val<<i);
		i++;

		filesize -= bytestowrite;
	}

	if (retval != -1) {
		write_ret = write_large_net(sockfd, blockok, blockok_size);
		if (write_ret != blockok_size) {
			retval = -1;
		}
	}

	lseek(fd, 0, SEEK_SET);
	curpos = 0;

	if (retval != -1) {
		for (blockidx = 0; blockidx < blockok_size; blockidx++) {
			for (i = 0; i < 8; i++) {
				blockok_val = blockok[blockidx] & (1<<i);
				if (!blockok_val) {

					destpos = (blockidx * 8 + i) * blocksize;
					if (curpos != destpos) {
						lseek_ret = lseek(fd, destpos, SEEK_SET);
						if (lseek_ret != destpos) {
							retval = -1;
							CHECKPOINT;
							break;
						}

						curpos = lseek_ret;
					}

					lseek_ret = curpos;

					if (lseek_ret >= filesize_s) {
						SPOTVAR_LLU(lseek_ret);
						SPOTVAR_LLU(filesize_s);
						SPOTVAR_I(i);
						continue;
					}

					read_ret = read_large_net(sockfd, buf, blocksize);
					if (read_ret != blocksize) {
						if ((lseek_ret + read_ret) != filesize_s || read_ret < 0) {
							SPOTVAR_I(blockidx);
							SPOTVAR_LLU(lseek_ret);
							SPOTVAR_LLU(read_ret);
							SPOTVAR_LLU(filesize_s);
							retval = -1;
							CHECKPOINT;
							break;
						}
					}

					write_ret = write_large(fd, buf, read_ret);
					if (write_ret != read_ret) {
						retval = -1;
						CHECKPOINT;
						break;
					}

					curpos += write_ret;
				}
			}
			if (retval == -1) {
				break;
			}
		}
	}

	close(fd);
	close(sockfd);
	close(master_sockfd);

	return(retval);
}

int main(int argc, char **argv) {
	char *mode, *host, *port_str, *file, *blocksize_str;
	uint32_t blocksize;
	int func_ret;
	int retval = EXIT_SUCCESS;
	int port;

	if (argc < 2) {
		return(print_help(EXIT_FAILURE));
	}

#ifdef HAVE_SIGNAL
#ifdef SIGPIPE
	signal(SIGPIPE, SIG_IGN);
#endif
#endif

	mode = argv[1];
	if (strcmp(mode, "-t") == 0) {
		if (argc < 5 || argc > 6) {
			return(print_help(EXIT_FAILURE));
		}

		host = argv[2];
		port_str = argv[3];
		file = argv[4];
		if (argc == 6) {
			blocksize_str = argv[5];
			blocksize = strtoul(blocksize_str, NULL, 0);
		} else {
			blocksize = 256 * 1024;
		}

		port = strtoul(port_str, NULL, 10);

		func_ret = sync_transmit(host, port, file, blocksize); 
		if (func_ret < 0) {
			fprintf(stderr, "Failed.\n");
			retval = 1;
		}
	} else if (strcmp(mode, "-r") == 0) {
		if (argc != 4) {
			return(print_help(EXIT_FAILURE));
		}

		port_str = argv[2];
		file = argv[3];

		port = strtoul(port_str, NULL, 10);

		func_ret = sync_receive(port, file);
		if (func_ret < 0) {
			fprintf(stderr, "Failed.\n");
			retval = 1;
		}
	} else {
		return(print_help(EXIT_FAILURE));
	}

	printf("Bytes In: %llu, Bytes Out: %llu\n", (unsigned long long) net_bytesin, (unsigned long long) net_bytesout);

	return(retval);
}


/* MD4.C FOLLOWS */
/* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
 */

/* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.

   License to copy and use this software is granted provided that it
   is identified as the "RSA Data Security, Inc. MD4 Message-Digest
   Algorithm" in all material mentioning or referencing this software
   or this function.

   License is also granted to make and use derivative works provided
   that such works are identified as "derived from the RSA Data
   Security, Inc. MD4 Message-Digest Algorithm" in all material
   mentioning or referencing the derived work.

   RSA Data Security, Inc. makes no representations concerning either
   the merchantability of this software or the suitability of this
   software for any particular purpose. It is provided "as is"
   without express or implied warranty of any kind.

   These notices must be retained in any copies of any part of this
   documentation and/or software.
 */

/* Constants for MD4Transform routine.
 */
#define S11 3
#define S12 7
#define S13 11
#define S14 19
#define S21 3
#define S22 5
#define S23 9
#define S24 13
#define S31 3
#define S32 9
#define S33 11
#define S34 15

static void MD4Transform(uint32_t [4], unsigned char [64]);
static void Encode(unsigned char *, uint32_t *, unsigned int);
static void Decode(uint32_t *, unsigned char *, unsigned int);

static unsigned char PADDING[64] = {
  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

/* F, G and H are basic MD4 functions.
 */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))

/* ROTATE_LEFT rotates x left n bits.
 */
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))

/* FF, GG and HH are transformations for rounds 1, 2 and 3 */
/* Rotation is separate from addition to prevent recomputation */
#define FF(a, b, c, d, x, s) { \
    (a) += F ((b), (c), (d)) + (x); \
    (a) = ROTATE_LEFT ((a), (s)); \
  }
#define GG(a, b, c, d, x, s) { \
    (a) += G ((b), (c), (d)) + (x) + (uint32_t)0x5a827999; \
    (a) = ROTATE_LEFT ((a), (s)); \
  }
#define HH(a, b, c, d, x, s) { \
    (a) += H ((b), (c), (d)) + (x) + (uint32_t)0x6ed9eba1; \
    (a) = ROTATE_LEFT ((a), (s)); \
  }

/* MD4 initialization. Begins an MD4 operation, writing a new context.
 */
static void rsaref_MD4Init (context)
rsaref_MD4_CTX *context;                                        /* context */
{
  context->count[0] = context->count[1] = 0;

  /* Load magic initialization constants.
   */
  context->state[0] = 0x67452301;
  context->state[1] = 0xefcdab89;
  context->state[2] = 0x98badcfe;
  context->state[3] = 0x10325476;
}

/* MD4 block update operation. Continues an MD4 message-digest
     operation, processing another message block, and updating the
     context.
 */
static void rsaref_MD4Update (context, input, inputLen)
rsaref_MD4_CTX *context;                                        /* context */
unsigned char *input;                                /* input block */
unsigned int inputLen;                     /* length of input block */
{
  unsigned int i, index, partLen;

  /* Compute number of bytes mod 64 */
  index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  /* Update number of bits */
  if ((context->count[0] += ((uint32_t)inputLen << 3))
      < ((uint32_t)inputLen << 3))
    context->count[1]++;
  context->count[1] += ((uint32_t)inputLen >> 29);

  partLen = 64 - index;

  /* Transform as many times as possible.
   */
  if (inputLen >= partLen) {
    memcpy
      ((unsigned char *)&context->buffer[index], (unsigned char *)input, partLen);
    MD4Transform (context->state, context->buffer);

    for (i = partLen; i + 63 < inputLen; i += 64)
      MD4Transform (context->state, &input[i]);

    index = 0;
  }
  else
    i = 0;

  /* Buffer remaining input */
  memcpy
    ((unsigned char *)&context->buffer[index], (unsigned char *)&input[i],
     inputLen-i);
}

/* MD4 finalization. Ends an MD4 message-digest operation, writing the
     the message digest and zeroizing the context.
 */
static void rsaref_MD4Final (digest, context)
unsigned char digest[16];                         /* message digest */
rsaref_MD4_CTX *context;                                        /* context */
{
  unsigned char bits[8];
  unsigned int index, padLen;

  /* Save number of bits */
  Encode (bits, context->count, 8);

  /* Pad out to 56 mod 64.
   */
  index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  padLen = (index < 56) ? (56 - index) : (120 - index);
  rsaref_MD4Update (context, PADDING, padLen);

  /* Append length (before padding) */
  rsaref_MD4Update (context, bits, 8);
  /* Store state in digest */
  Encode (digest, context->state, 16);

  /* Zeroize sensitive information.
   */
  memset ((unsigned char *)context, 0, sizeof (*context));
}

/* MD4 basic transformation. Transforms state based on block.
 */
static void MD4Transform (state, block)
uint32_t state[4];
unsigned char block[64];
{
  uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];

  Decode (x, block, 64);

  /* Round 1 */
  FF (a, b, c, d, x[ 0], S11); /* 1 */
  FF (d, a, b, c, x[ 1], S12); /* 2 */
  FF (c, d, a, b, x[ 2], S13); /* 3 */
  FF (b, c, d, a, x[ 3], S14); /* 4 */
  FF (a, b, c, d, x[ 4], S11); /* 5 */
  FF (d, a, b, c, x[ 5], S12); /* 6 */
  FF (c, d, a, b, x[ 6], S13); /* 7 */
  FF (b, c, d, a, x[ 7], S14); /* 8 */
  FF (a, b, c, d, x[ 8], S11); /* 9 */
  FF (d, a, b, c, x[ 9], S12); /* 10 */
  FF (c, d, a, b, x[10], S13); /* 11 */
  FF (b, c, d, a, x[11], S14); /* 12 */
  FF (a, b, c, d, x[12], S11); /* 13 */
  FF (d, a, b, c, x[13], S12); /* 14 */
  FF (c, d, a, b, x[14], S13); /* 15 */
  FF (b, c, d, a, x[15], S14); /* 16 */

  /* Round 2 */
  GG (a, b, c, d, x[ 0], S21); /* 17 */
  GG (d, a, b, c, x[ 4], S22); /* 18 */
  GG (c, d, a, b, x[ 8], S23); /* 19 */
  GG (b, c, d, a, x[12], S24); /* 20 */
  GG (a, b, c, d, x[ 1], S21); /* 21 */
  GG (d, a, b, c, x[ 5], S22); /* 22 */
  GG (c, d, a, b, x[ 9], S23); /* 23 */
  GG (b, c, d, a, x[13], S24); /* 24 */
  GG (a, b, c, d, x[ 2], S21); /* 25 */
  GG (d, a, b, c, x[ 6], S22); /* 26 */
  GG (c, d, a, b, x[10], S23); /* 27 */
  GG (b, c, d, a, x[14], S24); /* 28 */
  GG (a, b, c, d, x[ 3], S21); /* 29 */
  GG (d, a, b, c, x[ 7], S22); /* 30 */
  GG (c, d, a, b, x[11], S23); /* 31 */
  GG (b, c, d, a, x[15], S24); /* 32 */

  /* Round 3 */
  HH (a, b, c, d, x[ 0], S31); /* 33 */
  HH (d, a, b, c, x[ 8], S32); /* 34 */
  HH (c, d, a, b, x[ 4], S33); /* 35 */
  HH (b, c, d, a, x[12], S34); /* 36 */
  HH (a, b, c, d, x[ 2], S31); /* 37 */
  HH (d, a, b, c, x[10], S32); /* 38 */
  HH (c, d, a, b, x[ 6], S33); /* 39 */
  HH (b, c, d, a, x[14], S34); /* 40 */
  HH (a, b, c, d, x[ 1], S31); /* 41 */
  HH (d, a, b, c, x[ 9], S32); /* 42 */
  HH (c, d, a, b, x[ 5], S33); /* 43 */
  HH (b, c, d, a, x[13], S34); /* 44 */
  HH (a, b, c, d, x[ 3], S31); /* 45 */
  HH (d, a, b, c, x[11], S32); /* 46 */
  HH (c, d, a, b, x[ 7], S33); /* 47 */
  HH (b, c, d, a, x[15], S34); /* 48 */

  state[0] += a;
  state[1] += b;
  state[2] += c;
  state[3] += d;

  /* Zeroize sensitive information.
   */
  memset ((unsigned char *)x, 0, sizeof (x));
}

/* Encodes input (uint32_t) into output (unsigned char). Assumes len is
     a multiple of 4.
 */
static void Encode (output, input, len)
unsigned char *output;
uint32_t *input;
unsigned int len;
{
  unsigned int i, j;

  for (i = 0, j = 0; j < len; i++, j += 4) {
    output[j] = (unsigned char)(input[i] & 0xff);
    output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
    output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
    output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  }
}

/* Decodes input (unsigned char) into output (uint32_t). Assumes len is
     a multiple of 4.
 */
static void Decode (output, input, len)

uint32_t *output;
unsigned char *input;
unsigned int len;
{
  unsigned int i, j;

  for (i = 0, j = 0; j < len; i++, j += 4)
    output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
      (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
}
/* MD4.C ENDS */

Changes to tools/file_sync.c.

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
..
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
137
138
139
140
141
142
143

144
145
146
147
148
149
150
151
152
153
154
155
...
316
317
318
319
320
321
322

323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
...
579
580
581
582
583
584
585
586
587
588
589
		fprintf(fp, "%x", hash[i] & 0xf);
	}

	return;
}
#endif

static ssize_t read_large(int fd, char *buf, size_t count) {
	size_t bytestoread;
	ssize_t read_ret;
	ssize_t retval = 0;

	while (count) {
		bytestoread = count;

................................................................................
		buf += read_ret;
		retval += read_ret;
	}

	return(retval);
}

static ssize_t write_large(int fd, char *buf, size_t count) {
	size_t bytestowrite;
	ssize_t write_ret;
	ssize_t retval = 0;

	while (count) {
		bytestowrite = count;

................................................................................

	return(retval);
}

int sync_transmit(const char *host, int port, const char *file, uint64_t blocksize) {
	rsaref_MD4_CTX mdctx;
	unsigned char md4buf[16];

	uint64_t filesize;
	uint32_t blockok_size, blockidx;
	uint8_t *blockok, blockok_val;
	ssize_t read_ret, write_ret;
	char *buf;
	off_t lseek_ret;
	off_t curpos, destpos;
	int retval = 0;
	int sockfd;
	int fd;
	int i;

................................................................................

	return(retval);
}

int sync_receive(int port, const char *file) {
	rsaref_MD4_CTX mdctx;
	unsigned char md4buf[16], check_md4buf[16];

	uint64_t filesize, filesize_s;
	uint64_t blocksize;
	uint32_t blockok_size, blockidx = 0;
	uint8_t *blockok, blockok_val;
	ssize_t cur_read_ret, read_ret, write_ret;
	size_t bytestowrite;
	off_t lseek_ret;
	off_t curpos, destpos;
	char *buf;
	int sockfd, master_sockfd;
	int retval = 0;
	int fd;
	int skipbytes;
	int i = 0;

	fd = open(file, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
................................................................................
			fprintf(stderr, "Failed.\n");
			retval = 1;
		}
	} else {
		return(print_help(EXIT_FAILURE));
	}

	printf("Bytes In: %llu, Bytes Out: %llu\n", net_bytesin, net_bytesout);

	return(retval);
}







|







 







|







 







>




<







 







>








<







 







|



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
..
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
...
137
138
139
140
141
142
143
144
145
146
147
148

149
150
151
152
153
154
155
...
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331

332
333
334
335
336
337
338
...
579
580
581
582
583
584
585
586
587
588
589
		fprintf(fp, "%x", hash[i] & 0xf);
	}

	return;
}
#endif

static ssize_t read_large(int fd, unsigned char *buf, size_t count) {
	size_t bytestoread;
	ssize_t read_ret;
	ssize_t retval = 0;

	while (count) {
		bytestoread = count;

................................................................................
		buf += read_ret;
		retval += read_ret;
	}

	return(retval);
}

static ssize_t write_large(int fd, unsigned char *buf, size_t count) {
	size_t bytestowrite;
	ssize_t write_ret;
	ssize_t retval = 0;

	while (count) {
		bytestowrite = count;

................................................................................

	return(retval);
}

int sync_transmit(const char *host, int port, const char *file, uint64_t blocksize) {
	rsaref_MD4_CTX mdctx;
	unsigned char md4buf[16];
	unsigned char *buf;
	uint64_t filesize;
	uint32_t blockok_size, blockidx;
	uint8_t *blockok, blockok_val;
	ssize_t read_ret, write_ret;

	off_t lseek_ret;
	off_t curpos, destpos;
	int retval = 0;
	int sockfd;
	int fd;
	int i;

................................................................................

	return(retval);
}

int sync_receive(int port, const char *file) {
	rsaref_MD4_CTX mdctx;
	unsigned char md4buf[16], check_md4buf[16];
	unsigned char *buf;
	uint64_t filesize, filesize_s;
	uint64_t blocksize;
	uint32_t blockok_size, blockidx = 0;
	uint8_t *blockok, blockok_val;
	ssize_t cur_read_ret, read_ret, write_ret;
	size_t bytestowrite;
	off_t lseek_ret;
	off_t curpos, destpos;

	int sockfd, master_sockfd;
	int retval = 0;
	int fd;
	int skipbytes;
	int i = 0;

	fd = open(file, O_RDWR | O_CREAT | O_LARGEFILE, 0600);
................................................................................
			fprintf(stderr, "Failed.\n");
			retval = 1;
		}
	} else {
		return(print_help(EXIT_FAILURE));
	}

	printf("Bytes In: %llu, Bytes Out: %llu\n", (unsigned long long) net_bytesin, (unsigned long long) net_bytesout);

	return(retval);
}