File: biosig_server.c

package info (click to toggle)
biosig4c%2B%2B 1.3.0-2.2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,144 kB
  • sloc: ansic: 30,471; cpp: 7,543; makefile: 1,630; python: 65; awk: 58; sh: 4; ruby: 2
file content (958 lines) | stat: -rw-r--r-- 28,361 bytes parent folder | download | duplicates (2)
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
/*

    $Id: biosig_server.c,v 1.7 2009-04-08 15:55:56 schloegl Exp $
    Copyright (C) 2009 Alois Schloegl <a.schloegl@ieee.org>
    This file is part of the "BioSig for C/C++" repository 
    (biosig4c++) at http://biosig.sf.net/ 

    BioSig 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 3
    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, see <http://www.gnu.org/licenses/>. 
    
*/

#include "biosig-network.h"

#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <syslog.h>
#include <sys/wait.h>
#include <signal.h>

#ifdef WITH_MICROMED 
void sopen_trc_read(HDRTYPE *hdr);
#endif
#ifdef WITH_PDP 
void sopen_pdp_read(HDRTYPE *hdr);
#endif

const char *LOGFILE = "/tmp/biosig/biosigd.log";

/* 
	Key ID table and related functions 
*/
struct IDTABLE_T {
	uint64_t id;
	uint64_t gtime;	// time of file generation 
	struct sockaddr_in addr;
} *IdTablePtr; 
size_t IdTableLen; 


/*
	compare uint32_t
*/
int u64cmp(const void *a,const void *b) 
{
	unsigned int k=7;
	int r=0;
	while (!r && (k<sizeof(uint64_t))) {
		r = ((uint8_t*)a)[k] - ((uint8_t*)b)[k];
		k--; 
	}	
	return(r); 	 	
}

/*
	generate new unique id
 */
uint64_t GetNewId() {

	int k; 
	uint64_t c;
	FILE *fid = fopen("/dev/urandom","r"); 
	do {
		fread(&c,sizeof(c),1,fid); 
	} while (bsearch(&c, IdTablePtr, IdTableLen, sizeof(IDTABLE_T), &u64cmp)); 
	fclose(fid); 
		
	k = IdTableLen; 
	IdTableLen++; 
	IdTablePtr = (IDTABLE_T*)realloc(IdTablePtr,IdTableLen*sizeof(IDTABLE_T));
	while ((c < IdTablePtr[k].id) && (k>0)) {
		IdTablePtr[k].id = IdTablePtr[k-1].id;
		k--;
	}	
	IdTablePtr[k].id = c;
	IdTablePtr[k].gtime = t_time2gdf_time(time(NULL));
//	memcpy(&IdTablePtr[k].addr, addr, sizeof(struct sockaddr_in));
	
	return(c); 
}; 


/*TODO: semaphore */
size_t LoadIdTable(const char *path) {

	IdTableLen = 0; 
	DIR *d = opendir(path); 
	if (d==NULL) {
		mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
		return(0); 
	}	

	uint64_t ID; 
	size_t N=0; 
    	struct dirent *dp;
    	do {
        	errno = 0;
        	if ((dp = readdir(d)) != NULL) {
			IdTablePtr = (IDTABLE_T*)realloc(IdTablePtr,(N+1)*sizeof(IDTABLE_T));
			cat64(dp->d_name,&ID);
			if (ID != 0) {
				IdTablePtr[N].id = ID;
				N++;
			}	
        	}
	} while (dp != NULL);
	closedir(d); 

	qsort(IdTablePtr,N,sizeof(IDTABLE_T),&u64cmp);
	IdTableLen = N; 

	fprintf(stdout,"Load Id Table: %i entries found.\n",N);
		
	if (VERBOSE_LEVEL>8)
		for (int k=0; k<N; k++)
			fprintf(stdout,"%3i %016lx\n",k+1,IdTablePtr[k].id);

	return(N);
}; 


void sigchld_handler(int s)
{
    while(waitpid(-1, NULL, WNOHANG) > 0);
}


//#define VERBOSE_LEVEL 8

const char   path[] = "/tmp/biosig/\0                .gdf";
void DoJob(int ns)
{
		char fullfilename[] = "/tmp/biosig/                .gdf";
		const size_t pathlen = strlen(path); 
		char *filename = fullfilename+pathlen;  

//    		size_t sizbuf = BSCS_MAX_BUFSIZ+8;
		uint8_t inbuf[BSCS_MAX_BUFSIZ+8];
		HDRTYPE *hdr = NULL; 
		uint64_t ID = 0; 
		uint32_t STATUS = STATE_INIT;
		char stopflag = 0; 
		size_t datalen = 0; 
		mesg_t msg; 	

		if ((VERBOSE_LEVEL>7) && errno) {
			errno = 0; 
			fprintf(stdout,"## errno=%i %s\n",errno,strerror(errno));
		}	 

		/* Create a new SID for the child process */
		pid_t sid = setsid();
		if (sid < 0) {
			exit(-1);
		}

		/*server identification */
		const char *greeting="Hi there,\n this is your experimental BSCS server. \n It is useful for testing the BioSig client-server architecture.\n";
		msg.STATE = BSCS_VERSION_01 | BSCS_SEND_MSG | STATE_INIT | BSCS_NO_ERROR;
		msg.LEN = b_endian_u32(strlen(greeting)); 
		int s;
		s = send(ns,&msg,8,0);
		s = send(ns, greeting, strlen(greeting), 0);
		
	    	while (!stopflag) // wait for command
		{
			fprintf(stdout,":> ");
	   		ssize_t count = recv(ns, &msg, 8, 0);
			fprintf(stdout,"STATUS=%08x c=%i MSG=%08x len=%i,errno=%i %s\n",STATUS,count,b_endian_u32(msg.STATE),b_endian_u32(msg.LEN),errno,strerror(errno)); 
			size_t  LEN = b_endian_u32(msg.LEN);  

			FILE *fid = fopen(LOGFILE,"a");
			fprintf(stdout,"STATUS=%08x c=%i MSG=%08x len=%i,errno=%i %s\n",STATUS,count,b_endian_u32(msg.STATE),b_endian_u32(msg.LEN),errno,strerror(errno)); 
			fclose(fid); 
			errno = 0; 

	   		if (count==0) {
				fprintf(stdout,"connection lost! %i %i %i %s\n",stopflag,count,errno,strerror(errno)); 
				LEN = 0;
				stopflag=1;
				continue;
	   		} 
	   		else if (count<0) {
				fprintf(stdout,"invalid packet! %i %i %i %s\n",stopflag,count,errno,strerror(errno)); 
				LEN = 0;
	   		} 
			else if ((msg.STATE & CMD_MASK) == BSCS_NOP) 
			{	// no operation 

				count = 0; 
				while (count<LEN) {
			   		count += recv(ns, inbuf, min(BSCS_MAX_BUFSIZ,LEN-count), 0);
				}
				msg.STATE = BSCS_VERSION_01 | BSCS_NOP | BSCS_REPLY | (msg.STATE & STATE_MASK) | BSCS_NO_ERROR;

				// send reply 
				msg.LEN = b_endian_u32(0);
				s = send(ns, &msg, 8, 0);	
			}

			else if ((STATUS == STATE_INIT) && 
			    	 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_OPEN | STATE_INIT))) 
			{
/****************************************************************************************
	OPEN FILE with ID 
 ****************************************************************************************/

				if (VERBOSE_LEVEL>7) fprintf(stdout,"open %i %i %016lx\n",LEN,count,ID);

				hdr = constructHDR(0,0);
				if (LEN==0) //|| ((LEN==8) && (ID==0))) 	// no ID, send back id, open(w) 
				{
					ID = GetNewId(); 
					c64ta(ID, filename); 
					hdr->FLAG.ANONYMOUS = 1; 	// do not store name 
					STATUS = STATE_OPEN_WRITE_HDR; 
					msg.STATE = BSCS_VERSION_01 | BSCS_OPEN_W | BSCS_REPLY | STATE_OPEN_WRITE_HDR;
					msg.LEN = b_endian_u32(8);
					*(uint64_t*)&msg.LOAD  = l_endian_u64(ID); 
					s = send(ns, &msg, 16, 0);

		FILE *fid = fopen(LOGFILE,"a");
		fprintf(fid,"\t> %s",filename); 
		fclose(fid); 

				}
				else if (LEN == 8) 		// open(r)
				{
		   			count = recv(ns, &msg.LOAD, 8, 0);
					ID = leu64p(&msg.LOAD);

					c64ta(ID, filename); 
					// ToDo: replace SOPEN with simple function, e.g. access to event table not needed here 
					hdr->FLAG.ANONYMOUS = 1; 	// do not store name 
					hdr->FileName = fullfilename;
					hdr = sopen(fullfilename,"r",hdr);
					msg.LEN = b_endian_u32(0);
					if (hdr->FILE.OPEN==0) {
						msg.STATE = BSCS_VERSION_01 | BSCS_OPEN_R | BSCS_REPLY | STATE_INIT | BSCS_ERROR_CANNOT_OPEN_FILE;
						STATUS = STATE_INIT; 
					} else if (serror() || (hdr->NRec < 0)) {
						sclose(hdr);
						msg.STATE = BSCS_VERSION_01 | BSCS_OPEN_R | BSCS_REPLY | STATE_INIT | BSCS_ERROR_CANNOT_OPEN_FILE;
						STATUS = STATE_INIT; 
					} else 	{
						msg.STATE = BSCS_VERSION_01 | BSCS_OPEN_R | BSCS_REPLY | STATE_OPEN_READ;
						STATUS = STATE_OPEN_READ; 
					}
					s = send(ns, &msg, 8, 0);	
		FILE *fid = fopen(LOGFILE,"a");
		fprintf(fid,"\t< %s",filename); 
		fclose(fid); 
				}
				else //if (LEN != 8) 
				{ 
					STATUS = STATE_INIT; 
					msg.STATE = BSCS_VERSION_01 | BSCS_OPEN_W | BSCS_REPLY | STATE_INIT | BSCS_ERROR_INCORRECT_PACKET_LENGTH;
					msg.LEN = b_endian_u32(0);
					s = send(ns, &msg, 8, 0);	
				}
			}	

			else if ((STATUS == (msg.STATE & STATE_MASK)) && 
				 (LEN == 0) && 
				 ((msg.STATE & (VER_MASK | CMD_MASK)) == (BSCS_VERSION_01 | BSCS_CLOSE))) 
			{	// close 
/****************************************************************************************
	CLOSE FILE 
 ****************************************************************************************/

				if (STATUS != (msg.STATE & STATE_MASK)) fprintf(stdout,"Close: status does not fit\n");
	
				msg.STATE = BSCS_VERSION_01 | BSCS_CLOSE | BSCS_REPLY | STATE_INIT;
				msg.LEN = b_endian_u32(0);

				if (LEN != 0) 
					msg.STATE = BSCS_VERSION_01 | BSCS_CLOSE | BSCS_REPLY | STATE_INIT | BSCS_ERROR_INCORRECT_PACKET_LENGTH;

				if ((STATUS == STATE_OPEN_WRITE_HDR) || (STATUS == STATE_OPEN_WRITE)) {
					if (hdr->NRec != datalen/hdr->AS.bpb)
						hdr->NRec = -1; // this triggers sclose to compute the correct size  
				} 
								
				if ((hdr!=NULL) && ((STATUS != STATE_OPEN_READ) || (STATUS != STATE_OPEN_READ)) ) 
					sclose(hdr); 

				if (STATUS != STATE_INIT) {
					destructHDR(hdr); 
					hdr = NULL;
				}	

				hdr = NULL; 
				if (serror())
					msg.STATE = BSCS_VERSION_01 | BSCS_CLOSE | BSCS_REPLY | STATE_INIT | BSCS_ERROR_CLOSE_FILE;

				STATUS = STATE_INIT; 

				s = send(ns, &msg, 8, 0);	

			}

			else if ((STATUS == STATE_OPEN_WRITE_HDR) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_SEND_HDR | STATE_OPEN_WRITE_HDR))) 
			{	
/****************************************************************************************
	SEND HEADER
 ****************************************************************************************/
 
				//case BSCS_SEND_HDR:  	// send header information
				hdr->AS.Header = (uint8_t*)realloc(hdr->AS.Header,LEN); 
				hdr->HeadLen = LEN; 
				count = 0; 
				while (count<LEN) {
if (VERBOSE_LEVEL>8) fprintf(stdout,"SND HDR: c=%i,LEN=%i\n",count,LEN);				
					count += recv(ns, hdr->AS.Header+count, LEN-count, 0);
				}
if (VERBOSE_LEVEL>8) fprintf(stdout,"SND HDR: c=%i,LEN=%i\n",count,LEN);				

				hdr->TYPE = GDF; 
				hdr->FLAG.ANONYMOUS = 1; 	// do not store name 
				gdfbin2struct(hdr);

				hdr->EVENT.N = 0; 
				hdr->EVENT.POS = NULL; 
				hdr->EVENT.TYP = NULL; 
				hdr->EVENT.DUR = NULL; 
				hdr->EVENT.CHN = NULL; 
				
				c64ta(ID, filename); 
				hdr->FileName  = fullfilename;
				hdr->FILE.COMPRESSION = 0; 
				ifopen(hdr,"w"); 
				hdr->FILE.OPEN = 2; 
				count=ifwrite(hdr->AS.Header, 1, hdr->HeadLen, hdr);
				datalen = 0;

if (VERBOSE_LEVEL>8) fprintf(stdout,"SND HDR: count=%i\n",count);				
	
				if (errno) {	
					// check for errors in gdfbin2struct, ifopen and ifwrite		
					STATUS = STATE_OPEN_WRITE_HDR; 
					msg.STATE = BSCS_VERSION_01 | BSCS_SEND_HDR | BSCS_REPLY | STATE_OPEN_WRITE_HDR | BSCS_ERROR_COULD_NOT_WRITE_HDR;
				}	
				else {
					STATUS = STATE_OPEN_WRITE; 
					msg.STATE = BSCS_VERSION_01 | BSCS_SEND_HDR | BSCS_REPLY | STATE_OPEN_WRITE;
				}	
				msg.LEN = b_endian_u32(0);
if (VERBOSE_LEVEL>8) fprintf(stdout,"SND HDR RPLY: %08x\n",msg.STATE);				
				s = send(ns, &msg, 8, 0);	
			}


			else if ((STATUS == STATE_OPEN_WRITE) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_SEND_DAT | STATE_OPEN_WRITE))) 
			{	
/****************************************************************************************
	SEND DATA
 ****************************************************************************************/
				//case BSCS_SEND_DAT:   	// send data block 
				// ToDo: check corrupt packet
					count = 0; 
					while (count<LEN) {
						// ToDo: check if streams would be faster 
						// fprintf(stdout,"SEND_DAT %li %li %li\n",datalen,count,LEN);
					   	ssize_t c = recv(ns, inbuf, min(BSCS_MAX_BUFSIZ,LEN-count), 0);
						datalen += ifwrite(inbuf, 1, c, hdr);
					   	count += c;
					}
					//STATUS = STATE_OPEN_WRITE; 

				if (errno) {	
					// check for errors in ifwrite		
					msg.STATE = BSCS_VERSION_01 | BSCS_SEND_DAT | BSCS_REPLY | STATE_OPEN_WRITE | BSCS_ERROR_COULD_NOT_WRITE_DAT;
				}	
				else {
					msg.STATE = BSCS_VERSION_01 | BSCS_SEND_DAT | BSCS_REPLY | STATE_OPEN_WRITE;
				}	
				msg.LEN = b_endian_u32(0);
				s = send(ns, &msg, 8, 0);	
			}

			else if ((STATUS == STATE_OPEN_WRITE) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_SEND_EVT | STATE_OPEN_WRITE))) 
			{	
/****************************************************************************************
	SEND EVENTS
 ****************************************************************************************/
				//case BSCS_SEND_EVT:  	// send event information 

				
		   		count = recv(ns, inbuf+8, min(8,LEN), 0);
		   		if (count<8) continue; 

				char  flag = inbuf[8];
				int   N = inbuf[9] + (inbuf[10] + inbuf[11]*256)*256; 
				//float Fs = lef32p(inbuf+12); 
				
				if (LEN != (8 + N * (flag==3 ? 12u : 6u)))
				{
					msg.STATE = BSCS_VERSION_01 | BSCS_SEND_EVT | BSCS_REPLY | STATE_OPEN_WRITE | BSCS_ERROR_INCORRECT_PACKET_LENGTH;
				}

				else if (N>0) {
					size_t n = hdr->EVENT.N;
					hdr->EVENT.N += N;
	 				hdr->EVENT.POS = (uint32_t*) realloc(hdr->EVENT.POS, hdr->EVENT.N*sizeof(*hdr->EVENT.POS) );
					hdr->EVENT.TYP = (uint16_t*) realloc(hdr->EVENT.TYP, hdr->EVENT.N*sizeof(*hdr->EVENT.TYP) );
					hdr->EVENT.DUR = (uint32_t*) realloc(hdr->EVENT.DUR, hdr->EVENT.N*sizeof(*hdr->EVENT.DUR) );
					hdr->EVENT.CHN = (uint16_t*) realloc(hdr->EVENT.CHN, hdr->EVENT.N*sizeof(*hdr->EVENT.CHN) );
	
					// read EVENT.POS
					count = 0; 
					LEN = N*sizeof(*hdr->EVENT.POS);
					while (count < LEN) {
						count += recv(ns, (uint8_t*)(hdr->EVENT.POS+n)+count, LEN-count, 0);
					}
					// read EVENT.TYP
					count = 0; 
					LEN = N*sizeof(*hdr->EVENT.TYP);
					while (count < LEN) {
						count += recv(ns, (uint8_t*)(hdr->EVENT.TYP+n)+count, LEN-count, 0);
					}
					// read EVENT.DUR and EVENT.CHN
					if (flag==3) {
						count = 0; 
						LEN = N*sizeof(*hdr->EVENT.POS);
						while (count < LEN) {
							count += recv(ns, (uint8_t*)(hdr->EVENT.DUR+n)+count, LEN-count, 0);
						}
						count = 0; 
						LEN = N*sizeof(*hdr->EVENT.TYP);
						while (count < LEN) {
							count += recv(ns, (uint8_t*)(hdr->EVENT.CHN+n)+count, LEN-count, 0);
						}
					}	
					else {
						for (size_t k=n; k<hdr->EVENT.N; k++) {
							hdr->EVENT.DUR[k] = 0; 
							hdr->EVENT.CHN[k] = 0; 
						}
					}
#if __BYTE_ORDER == __BIG_ENDIAN
					// do byte swapping if needed 
					for (size_t k=n; k<hdr->EVENT.N; k++) {
						hdr->EVENT.POS[k] = l_endian_u32(hdr->EVENT.POS[k]); 
						hdr->EVENT.TYP[k] = l_endian_u16(hdr->EVENT.TYP[k]); 
					}
					if (flag==3) {	
						for (size_t k=n; k<hdr->EVENT.N; k++) {
							hdr->EVENT.DUR[k] = l_endian_u32(hdr->EVENT.DUR[k]); 
							hdr->EVENT.CHN[k] = l_endian_u16(hdr->EVENT.CHN[k]); 
						}
					} 
#endif
					msg.STATE = BSCS_VERSION_01 | BSCS_SEND_EVT | BSCS_REPLY | STATE_OPEN_WRITE;
				}
				msg.LEN = b_endian_u32(0);
				s = send(ns, &msg, 8, 0);	
			}

			else if ((msg.STATE & CMD_MASK) ==  BSCS_SEND_MSG ) 
			{	// might become obsolet (?)
				// ToDo: check corrupt packet
				count = 0; 
				ssize_t c=0;
				while (count<LEN) {
					int len = min(BSCS_MAX_BUFSIZ-1,LEN-count);	
				   	c = recv(ns, inbuf, len, 0);
			   		inbuf[c-1]=0;
					fprintf(stdout,"got message <%s>\n",(char*)(inbuf));
				   	count += c;
				}
				//system((char*)(inbuf));
			// TODO: reply message
			}

			else if ((STATUS == STATE_OPEN_READ) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_REQU_HDR | STATE_OPEN_READ))) 
			{	
/****************************************************************************************
	REQUEST HEADER
 ****************************************************************************************/
				msg.STATE = BSCS_VERSION_01 | BSCS_REQU_HDR | BSCS_REPLY | STATE_OPEN_READ;
				msg.LEN = b_endian_u32(hdr->HeadLen);
				s = send(ns, &msg, 8, 0);	
				s = send(ns, hdr->AS.Header, hdr->HeadLen, 0);	

			}

			else if ((STATUS == STATE_OPEN_READ) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_REQU_DAT | STATE_OPEN_READ))) 
			{	
/****************************************************************************************
	REQUEST DATA
 ****************************************************************************************/
	   			count += recv(ns, inbuf, 8, 0);
		   		size_t length = leu32p(inbuf);
		   		size_t start = leu32p(inbuf+4);
				sread_raw(start, length, hdr, 0);

				length = min(length, hdr->AS.first + hdr->AS.length - start);
				msg.STATE = BSCS_VERSION_01 | BSCS_REQU_DAT | BSCS_REPLY | STATE_OPEN_READ;
				msg.LEN = b_endian_u32(hdr->AS.bpb*length);
				s = send(ns, &msg, 8, 0);	
				s = send(ns, hdr->AS.rawdata + hdr->AS.bpb*(start-hdr->AS.first), hdr->AS.bpb*length, 0);	
				//send(ns, hdr->AS.rawdata, (hdr->AS.length-hdr->AS.first)*hdr->AS.bpb,0);
			}

			else if ((STATUS == STATE_OPEN_READ) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_REQU_EVT | STATE_OPEN_READ))) 
			{	
/****************************************************************************************
	REQUEST EVENT TABLE 
 ****************************************************************************************/
				size_t len = 0; 
				if ((hdr->TYPE == GDF) && (hdr->AS.rawEventData == NULL) && (hdr->NRec>=0)) {
					// event table from GDF file 
					ifseek(hdr, hdr->HeadLen + hdr->AS.bpb*hdr->NRec, SEEK_SET);
					// READ EVENTTABLE 
					hdr->AS.rawEventData = (uint8_t*)realloc(hdr->AS.rawEventData,8);
					int c = ifread(hdr->AS.rawEventData, sizeof(uint8_t), 8, hdr);
	    				uint8_t *buf = hdr->AS.rawEventData;
			
					if (c<8) {
						hdr->EVENT.N = 0;
					}	
					else if (hdr->VERSION < 1.94) {
						hdr->EVENT.N = leu32p(buf + 4);
					}	
					else {
						hdr->EVENT.N = buf[1] + (buf[2] + buf[3]*256)*256; 
					}	
					int sze = (buf[0]>1) ? 12 : 6;
					len = 8+hdr->EVENT.N*sze;
					hdr->AS.rawEventData = (uint8_t*)realloc(hdr->AS.rawEventData,len);
					ifread(hdr->AS.rawEventData+8, 1, len-8, hdr);
					ifseek(hdr, hdr->HeadLen+hdr->AS.bpb*hdr->FILE.POS, SEEK_SET); 
					// note: no conversion to HDR.EVENT structure needed on server-side 
				}
				else if ((hdr->TYPE != GDF) && (hdr->AS.rawEventData == NULL))
					len = hdrEVT2rawEVT(hdr); 

				else if ((hdr->TYPE == GDF) && (hdr->AS.rawEventData != NULL)){
					uint8_t *buf = hdr->AS.rawEventData; 
					if (hdr->VERSION < 1.94) 
						hdr->EVENT.N = leu32p(buf + 4);
					else	
						hdr->EVENT.N = buf[1] + (buf[2] + buf[3]*256)*256; 
				
					int sze = (buf[0]>1) ? 12 : 6;
					
					if (hdr->EVENT.N>0) {
						len = 8+sze*hdr->EVENT.N;
					}
				}

				msg.STATE = BSCS_VERSION_01 | BSCS_REQU_HDR | BSCS_REPLY | STATE_OPEN_READ;
				if (len <= 8) {
					msg.LEN = b_endian_u32(0);
					s = send(ns, &msg, 8, 0);
				} else {
					msg.LEN = b_endian_u32(len);
					s = send(ns, &msg, 8, 0);
					s = send(ns, hdr->AS.rawEventData, len, 0);
				}
			}

			else if ((STATUS == STATE_OPEN_WRITE_HDR) && 
				 ((msg.STATE & ~ERR_MASK) == (BSCS_VERSION_01 | BSCS_PUT_FILE | STATE_OPEN_WRITE_HDR))) 
			{	
/****************************************************************************************
	PUT FILE 
 ****************************************************************************************/

 
				//case BSCS_PUT_FILE:  	// send header information
				uint32_t errcode = 0; 

				c64ta(ID, filename); 
				char *f2 = (char*)malloc(strlen(fullfilename)+5); 
				strcpy(f2,fullfilename); 
				strcat(f2,".tmp"); 

				/*********** temporary file - not checked *********/
				int sdo = open(f2,O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); 
				
				HDRTYPE *hdr = constructHDR(0,0);
				hdr->FileName = f2;
#ifdef WITH_PDP 
				hdr->AS.Header = (uint8_t*) malloc(LEN+1);
#endif

				const int BUFLEN = 1024;
				char buf[BUFLEN]; 

				count  = 0; 
				while (count<LEN) {
					size_t len = recv(ns, buf, min(LEN-count, BUFLEN), 0);
#ifdef WITH_PDP 
					memcpy(hdr->AS.Header+count,buf,len);
#endif
					count += write(sdo, buf, len);
					//fprintf(stdout,"\b\b\b\b%02i%% ",100.0*count/LEN);
				}
				close(sdo);
				hdr->AS.Header[count]=0;
				hdr->HeadLen = count;

				if (LEN-count) {
					errcode = 1;
//fprintf(stdout,"errcode=1 %i\n",LEN-count);
				}
				else {
				/************ read temporary file, ... ************/
				int status = 0;
#ifdef WITH_PDP 
				sopen_pdp_read(hdr);
				
				sread(NULL,0,hdr->NRec,hdr);	// rawdata -> data.block
				status=serror();

				if (status) {
					errcode = 2;
#else
        				{
#endif
					sopen(f2,"r",hdr);
					if ((status=serror())) 
					        errcode = 11;
					
					else {
						count = sread(NULL,0,hdr->NRec,hdr);
						if ((status=serror())) 
							errcode = 12;
						
						sclose(hdr);
						if ((status=serror())) 
							errcode = 13;
						
					}
				}

				if (VERBOSE_LEVEL>7) fprintf(stdout,"put file: errcode=%i\n",errcode); 

				/********* ... and convert to GDF file *************/
				if (!status) {
					hdr->FileName = fullfilename;
					hdr->TYPE = GDF;
					hdr->VERSION = 2; 
					sopen(fullfilename,"w",hdr);
					if ((status=serror())) {
						errcode = 21;
					} else {
						//count = swrite(hdr->data.block, hdr->NRec, hdr);
						ifwrite(hdr->AS.rawdata,hdr->AS.bpb,hdr->NRec,hdr);
						if ((status=serror()))
							errcode = 22;
						sclose(hdr);
						if ((status=serror()))
							errcode = 23;
					}	
				}

				if (VERBOSE_LEVEL>7) fprintf(stdout,"put file: errcode=%i\n",errcode); 

				}
				destructHDR(hdr);
				free(f2);

				STATUS = STATE_INIT;
				msg.STATE = BSCS_VERSION_01 | BSCS_PUT_FILE | BSCS_REPLY | STATE_INIT | b_endian_u32(errcode);
				msg.LEN = b_endian_u32(0);
				s = send(ns, &msg, 8, 0);
			}

			else if (( 
				 (msg.STATE & ~ERR_MASK & ~STATE_MASK) == (BSCS_VERSION_01 | BSCS_GET_FILE))) 
			{	
/****************************************************************************************
	GET FILE 
 ****************************************************************************************/

	   			count = recv(ns, &msg.LOAD, 8, 0);
				ID = leu64p(&msg.LOAD);
				c64ta(ID, filename); 

				if (VERBOSE_LEVEL>7) fprintf(stdout,"get file: %s\n",filename); 

fprintf(stdout,"get file: %016lx\n",ID);

				int sdi = open(fullfilename,O_RDONLY,S_IFREG);

fprintf(stdout,"get file: %016lx %i\n",ID,sdi);

				if (sdi<0) {
					msg.STATE = BSCS_VERSION_01 | BSCS_GET_FILE | BSCS_REPLY | STATUS | BSCS_ERROR_CANNOT_OPEN_FILE;
					msg.LEN = b_endian_u32(0);
					s = send(ns, &msg, 8, 0);	
				} 
				else {		
					struct stat FileBuf;
					stat(fullfilename,&FileBuf);
					uint32_t LEN = FileBuf.st_size;

					msg.STATE = BSCS_VERSION_01 | BSCS_GET_FILE | BSCS_REPLY | STATUS | BSCS_NO_ERROR;
					msg.LEN = b_endian_u32(LEN);
					s = send(ns, &msg, 8, 0);	

					const int BUFLEN = 1024;
					char *buf[BUFLEN]; 
					count = 0; 
					while (count<LEN) {
						size_t len = read(sdi, buf, min(LEN-count,BUFLEN));
						count += send(ns, buf, len, 0); 
					}
					//if (FileBuf.st_size-count); // TODO: error handling
					close(sdi); 
				}
				s = send(ns, &msg, 8, 0);	
			}

			else if ((msg.STATE & CMD_MASK) == BSCS_NOP) 
			{	
/****************************************************************************************
	NO OPERATION
 ****************************************************************************************/
				;
			}


			else if (msg.STATE & BSCS_REPLY) 
			{	// ignore reply messages
				;
	   		} 
			else 
			{
				fprintf(stdout,"unknown packet: state=%08x len=%i\n",b_endian_u32(msg.STATE),b_endian_i32(msg.LEN)); 
				msg.STATE = BSCS_VERSION_01 | BSCS_ERROR;
				msg.LEN = b_endian_u32(0);
				s = send(ns, &msg, 8, 0);	
			}
		}
}

//int main (int argc, char *argv[]) {
int main () {

	int sd, ns; 		
	socklen_t fromlen;
	int addrlen;
	struct sigaction sa;
	time_t timer;	
	pid_t sid; 

	fprintf(stdout,"01## errno=%i %s\n",errno,strerror(errno)); 
	LoadIdTable(path); 
	fprintf(stdout,"11## errno=%i %s\n",errno,strerror(errno)); 

	timer=time(NULL);	
	char *t = asctime(localtime(&timer));
	t[24]=0;

	FILE *fid = fopen(LOGFILE,"a");
	fprintf(fid,"\n%s\tserver started",t); 
	fclose(fid); 


#ifdef IPv6	// IPv4 and IPv6
	int status;
	struct addrinfo hints, *servinfo, *p;
    	struct sockaddr_storage sain;
    	char s[INET6_ADDRSTRLEN];
    	int rv;
    	int yes=1;


	memset(&hints, 0, sizeof hints); // make sure the struct is empty
	hints.ai_family = AF_UNSPEC;     // don't care IPv4 or IPv6
	hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
   	hints.ai_flags = AI_PASSIVE; // use my IP


	// get ready to connect
	status = getaddrinfo(NULL, "SERVER_PORT", &hints, &servinfo);
   	if(status<0) {
	        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
	   	return(BSCS_UNKNOWN_HOST);
	}
   	if(servinfo==NULL) return(BSCS_UNKNOWN_HOST);

    	// loop through all the results and bind to the first we can
    	for(p = servinfo; p != NULL; p = p->ai_next) {
        	if ((sd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
	            perror("server: socket");
        	    continue;
        	}

	        if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) {
			perror("setsockopt");
            		exit(1);
        	}

        	if (bind(sd, p->ai_addr, p->ai_addrlen) == -1) {
            		close(sd);
            		perror("server: bind");
            		continue;
        	}
        	break;
    	}
    	if (p == NULL)  {
        	fprintf(stderr, "server: failed to bind\n");
		return(BSCS_CANNOT_BIND_PORT);
    	}

    	freeaddrinfo(servinfo); // all done with this structure


#else

    	struct sockaddr_in sain;
    	fromlen = sizeof(sain); 

    	/*
    	 * Get a socket to work with.  This socket will
    	 * be in the UNIX domain, and will be a
    	 * stream socket.
    	 */
    	if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        	perror("server: socket");
        	exit(1);
    	}

   	/* bind server port */
    	sain.sin_family = AF_INET;
    	sain.sin_addr.s_addr = htonl(INADDR_ANY);
    	sain.sin_port = htons(SERVER_PORT);

   	if(bind(sd, (struct sockaddr *) &sain, sizeof(sain))<0) {
      		perror("cannot bind port ");
      		return(-1); 
   	}

#endif 

    	/*
    	 * Listen on the socket.
    	 */
    	if (listen(sd, 10) < 0) {
        	perror("server: listen");
        	exit(1);
    	}

 	sa.sa_handler = sigchld_handler; // reap all dead processes
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGCHLD, &sa, NULL) == -1) {
		perror("sigaction");
		exit(1);
	}

	printf("server: waiting for connections...\n");

    	while(1) {
    		/*
    		 * Accept connections.  When we accept one, ns
	    	 * will be connected to the client.  fsain will
    		 * contain the address of the client.
	    	 */
	    	fromlen = sizeof(sain); 	
		if ((ns = accept(sd, (struct sockaddr*)&sain, &fromlen)) < 0) {
        		perror("server: accept");
	        	exit(1);
    		}
#ifdef IPv6
		inet_ntop(sain.ss_family, get_in_addr((struct sockaddr *)&sain),s, sizeof s);
	        printf("server: got connection from %s\n", s);
#else 
		char hostname[100],service[100];
  		bzero(hostname,100);
  		bzero(service,100);
  		addrlen = sizeof(struct sockaddr);
		if (getnameinfo((struct sockaddr *)&sain,addrlen,hostname,100,service,100,0) != 0)
     		{
		fprintf(stdout,"--- errno=%i %s\n",errno,strerror(errno)); 
//    			perror("getnameinfo");
     		}

		printf("Connection received from host (%s) on remote port (%s)\n",hostname,service);
#endif

#ifndef VERBOSE_LEVEL
//VERBOSE_LEVEL = 8; 
#endif 

		// TODO: LOG FILE 

		char dst[INET6_ADDRSTRLEN];
		inet_ntop(AF_INET, &sain.sin_addr,dst, INET6_ADDRSTRLEN);
		timer=time(NULL);	
		char *t = asctime(localtime(&timer));
		t[24]=0;


		FILE *fid = fopen(LOGFILE,"a");
		fprintf(fid,"\n%s\t%s\t%s",t,dst,hostname); 
		fclose(fid); 
		
	        if (!fork()) { // this is the child process
			close(sd); // child doesn't need the listener

			/* Change the file mode mask */
			umask(0);

			/* Change the current working directory.  This prevents the current
   			   directory from being locked; hence not being able to remove it. */

			if ((chdir("/")) < 0) {
				syslog( LOG_ERR, "unable to change directory to %s, code %d (%s)","/", errno, strerror(errno) );
			        exit(-1);
			}
			    
			/* Redirect standard files to /dev/null */

			freopen( "/dev/null", "r", stdin);
			freopen( "/dev/null", "w", stdout);
			freopen( "/dev/null", "w", stderr);
			
			if (VERBOSE_LEVEL>7) fprintf(stdout,"server123 err=%i %s\n",errno,strerror(errno));

			errno = 0; 
			DoJob(ns);
	
		    	close(ns);
        	    	exit(0);
	        }
    		close(ns);
	} 	

    	close(sd);
    	exit(0);
}