File: biosig_client.c

package info (click to toggle)
biosig4c++ 1.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 6,220 kB
  • sloc: ansic: 41,218; cpp: 8,946; sh: 4,365; makefile: 1,758; python: 87; awk: 73; php: 40; perl: 36; java: 14; ruby: 7
file content (299 lines) | stat: -rw-r--r-- 8,504 bytes parent folder | download
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
/*

    Copyright (C) 2009,2015,2016 Alois Schloegl <alois.schloegl@gmail.com>
    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 <errno.h>
#ifndef _WIN32
# include <pwd.h>
#endif
#include <sys/stat.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>



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

	int s=0; 
    	register int sd;
	struct stat info;
	int state; 
	
	sd = bscs_connect(argv[1]); 

   	if(sd<0) {
      		perror("cannot connect ");
      		exit(1);
   	}

	fprintf(stdout,"client 122 %i %s <%s>\n",errno,strerror(errno),getenv("HOME"));
	
	char path2keys[1024];
	char *str;
#ifdef _WIN32
	str = strncpy(path2keys,getenv("HOME"),1023);
#elif 0
	// TODO: getpwuid can cause a memory leak in glibc2.15 and earlier 
	struct passwd *p = getpwuid(geteuid());	
	str = strncpy(path2keys,p->pw_dir,1023);
	//path2keys = strdup(p->pw_dir); 
	if (VERBOSE_LEVEL>7)  fprintf(stdout,"Name:%s\nPw:%s\nuid/gui: %i/%i\nreal name: %s\n$HOME:%s\nSHELL:%s\n",p->pw_name,p->pw_passwd,p->pw_uid,p->pw_gid,p->pw_gecos,p->pw_dir,p->pw_shell);
#endif
	str += strlen(path2keys);
	str[0] = FILESEP;
	str = (char*)memcpy(str+1,".biosig",7)+7;
	str[0] = FILESEP;
	str[1] = 0;
	size_t path2keysLength = strlen(path2keys); 
	stat(path2keys, &info);
	if (!(S_ISDIR(info.st_mode)))
		mkdir(path2keys,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); // read/write/search permissions for owner and group, and with read/search permissions for others.

	SERVER_STATE = STATE_INIT; 
	
	HDRTYPE *hdr=constructHDR(0,0); 
	uint64_t ID = 0; 

//	bscs_send_msg(sd,"Hello Server");
	fprintf(stdout,"client 125 %i %s\n",errno,strerror(errno));

	char clbuf[1000]; 
	char *cmd;
	while (1) {
		fprintf(stdout,"prompt$:");		
	     	cmd = fgets(clbuf,1000,stdin); 

		if (!strncasecmp(clbuf,"exit",4) || !strncasecmp(clbuf,"bye",3)) {
			fprintf(stdout,"\n%p>\n",cmd);
			bscs_close(sd);
			close(sd);
			break; 
		}		 			
//		fprintf(stdout,"cmd=<%s> %i\n",cmd,strncasecmp(cmd,"close",5));
		
		if (cmd[0]=='!') 
			system(cmd+1); 
		else if (!strncasecmp(cmd,"ow+c",4)) {
			ID = 0; 
			s=bscs_open(sd, &ID);
			fprintf(stdout,"ow ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
			s=bscs_close(sd);
			fprintf(stdout,"c ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
		}
		else if (!strncasecmp(cmd,"or+c",4)) {
			ID = htole64(0x233ab6dfc96f664fLL);
			s=bscs_open(sd, &ID);
			fprintf(stdout,"or ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
//		   	hdr->TYPE = unknown; 
//			getfiletype(hdr);
			bscs_requ_hdr(sd,hdr);
			bscs_requ_dat(sd,0,hdr->NRec,hdr);
//			bscs_requ_evt(sd,hdr);
			s=bscs_close(sd);
			fprintf(stdout,"c ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
		}
		else if (!strncasecmp(cmd,"openr",5)) {
			ID = htole64(0x233ab6dfc96f664fLL);

			ID = htole64(0x233ab6dfc96f664fLL);
			fprintf(stdout,"or ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
			s=bscs_open(sd, &ID);
		}
		else if (!strncasecmp(cmd,"openw",5)) {
			char *fn = cmd+5;
			char *ix; 
		     	while (isspace(fn[0])) fn++; 
		     	ix = fn; 
		     	while (!isspace(ix[0])) ix++; 
		     	ix[0]=0;
		     	
		     	hdr = sopen(fn,"r",hdr);

			ID = 0; 
			s=bscs_open(sd, &ID);
			fprintf(stdout,"openw: ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
		}
		else if (!strncasecmp(cmd,"sendhdr",7)) {
			s= bscs_send_hdr(sd, hdr);
		}
		else if (!strncasecmp(cmd,"senddat",7)) {
			s= bscs_send_dat(sd, hdr->AS.rawdata, hdr->AS.bpb*hdr->AS.length);
			fprintf(stdout,"sent dat %i\n",s);
		}
		else if (!strncasecmp(cmd,"sendevt",7)) {
			if (hdr->TYPE != GDF) hdrEVT2rawEVT(hdr); 
			if (hdr->EVENT.N>0) s= bscs_send_evt(sd, hdr);
		}
		
		else if (!strncasecmp(cmd,"hdr",3)) {
			bscs_requ_hdr(sd,hdr);
		}
		else if (!strncasecmp(cmd,"show1",5)) {
			hdr2ascii(hdr,stdout,1);
		}
		else if (!strncasecmp(cmd,"show2",5)) {
			hdr2ascii(hdr,stdout,2);
		}
		else if (!strncasecmp(cmd,"show3",5)) {
			hdr2ascii(hdr,stdout,3);
		}
		else if (!strncasecmp(cmd,"show4",5)) {
			hdr2ascii(hdr,stdout,4);
		}
		else if (!strncasecmp(cmd,"dat",3)) {
			bscs_requ_dat(sd,0,hdr->NRec,hdr);
		}
		else if (!strncasecmp(cmd,"evt",3)) {
			bscs_requ_evt(sd,hdr);
		}
		else if (!strncasecmp(cmd,"ow",2)) {
			ID = 0; 
			s=bscs_open(sd, &ID);
			fprintf(stdout,"ow ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
		}
		else if (!strncasecmp(cmd,"close",5)) {
			s=bscs_close(sd);
			fprintf(stdout,"oc ID=%016"PRIx64" s=%i %08x\n",ID,s,s);
		}
		else if (!strncasecmp(cmd,"requ ",5)) {
			char *fn = cmd+5;
			char *ix; 
		     	while (isspace(fn[0])) fn++; 
		     	ix = fn; 
		     	while (!isspace(ix[0])) ix++; 
		     	ix[0]=0;
		     	cat64(fn,&ID);
			bscs_open(sd, &ID);
fprintf(stdout,"11 %i\n",hdr->EVENT.N);
			bscs_requ_hdr(sd,hdr);
		   	hdr->TYPE = unknown; 
fprintf(stdout,"12 %i\n",hdr->EVENT.N);
			getfiletype(hdr);
fprintf(stdout,"13 %s %i\n",GetFileTypeString(hdr->TYPE),hdr->EVENT.N);
			bscs_requ_hdr(sd,hdr);
fprintf(stdout,"14a %i %i %i\n",0,(int)hdr->NRec,hdr->EVENT.N);
			bscs_requ_dat(sd,0,hdr->NRec,hdr);
fprintf(stdout,"14b %i\n",hdr->EVENT.N);
//			bscs_requ_evt(sd,hdr);
fprintf(stdout,"14c %i\n",hdr->EVENT.N);
		   	if (hdr->TYPE == GDF) {
fprintf(stdout,"15\n");
				;//hdr2ascii(hdr,stdout,3);
fprintf(stdout,"16 %i\n",hdr->EVENT.N);
			}	
			bscs_close(sd);
		}
		else if (!strncasecmp(cmd,"send ",5)) {
			char *fn = cmd+5;
			char *ix; 
		     	while (isspace(fn[0])) fn++; 
		     	ix = fn; 
		     	while (!isspace(ix[0])) ix++; 
		     	ix[0]=0;
		     	
		     	hdr = sopen(fn,"r",NULL);
//			if (hdr->TYPE!=GDF) {
			if (serror2(hdr)) {
				stat(fn, &info);
		     		FILE *fid = fopen(fn,"r"); 
		     		if (fid==NULL)
			     		fprintf(stdout,"file %s not found\n",fn);
		     		else {	

			     		uint8_t *buf = (uint8_t*) malloc(info.st_size);
					ssize_t count = fread(buf, 1, info.st_size, fid); 
			     		fclose(fid); 

					ID = 0; 
					if ((state = bscs_open(sd,&ID))) // write-open
						fprintf(stdout,"BSCS_OPEN failed: state=%08x\n",state);
					else
					{
				     		/* write key file */ 
						char *tmp = strrchr(fn,FILESEP);
						if (tmp==NULL) tmp = fn; 
						strcpy(path2keys+path2keysLength,tmp);
			
						fid = fopen(path2keys,"w");	// TODO: prevent overwritting
						fprintf(fid,"key4biosig: host=%s ID=%016"PRIx64" ",argv[1],ID);
						fclose(fid);

						fprintf(stdout,"open_w ID=%016"PRIx64"\n",ID);
						hdr->HeadLen = count; 
						bscs_send_dat(sd, buf, count);
						bscs_close(sd);
					}	
					free(buf); 
		     		}
		     	}	
		     	else {
		     	
				sread_raw(0,hdr->NRec,hdr,1,NULL,0); 	// collapse rawdata (remove obsolete channels)
				size_t bpb = bpb8_collapsed_rawdata(hdr)>>3;
				if (serror2(hdr)) {
					sclose(hdr);
					exit(-1);
				}	
		
				ID = 0; 
				bscs_open(sd,&ID); // write-open
		     		/* write key file */ 

				char *tmp = strrchr(fn,FILESEP);
				if (tmp==NULL) tmp = fn; 
				strcpy(path2keys+path2keysLength,tmp);

				FILE *fid = fopen(path2keys,"w");	// TODO: prevent overwritting
				if (fid==NULL) 
					fprintf(stdout,"error: %i %s\n",errno, strerror(errno));		     		

				fprintf(fid,"bscs://%s/%016"PRIx64"",argv[1],ID);
				fclose(fid);

				fprintf(stdout,"open_w  ID=%016"PRIx64" len=%"PRIiPTR"\n",ID,hdr->AS.length);
				s= bscs_send_hdr(sd, hdr);
				fprintf(stdout,"sent hdr %i %i\n",s,hdr->AS.bpb);

				s = bscs_send_dat(sd, hdr->AS.rawdata, hdr->AS.length*bpb);
				fprintf(stdout,"sent dat %i %"PRIiPTR"\n",s,bpb);
					
				if (hdr->TYPE != GDF) hdrEVT2rawEVT(hdr); 
				if (hdr->EVENT.N>0) s= bscs_send_evt(sd, hdr);
				fprintf(stdout,"sent evt %i\n",s);
				s = bscs_close(sd);
				fprintf(stdout,"closed %08x\n",s);
			     			
			     	sclose(hdr);
			}     	 
		}			
	}	

    /*
     * We can simply use close() to terminate the
     * connection, since we're done .
     */
//    bscs_disconnect(sd);
	return(0);
}