DD_client.c
28 KB
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
/* $Id: DD_client.c,v 1.4 2012/10/18 09:25:58 budnik Exp $ */
/*==============================================================
* DD SYSTEM
* DD_client.c
* V.6.0
* List of changes:
* November 1995, Version 1.0
* August 1999 :Access security system was installed by Alexandr Grigoriev (v3.0)
* October 22, 2002 - Communication ideology is changed (V.4.0)
* Oct 29, 2002 - Add DD_GetVersion() and some bags are corrected (V.4.1)
* May 1, 2003 - Network error is developped. (The header receiving is changed): V.4.2
* Nov 18, 2004 - Cach error is fixed.
* Jun 17, 2007 - Fedorov, V.4.5 Create new VI and Update data requests
* Sep 24, 2007 - Fedorov, V.4.6 Processing of return of Get_Data
* Dec 03, 2007 - Budnik, V.4.7 There may be several sessions with DD Server inside ONE
* Client session. Keep UserHost & UserName
* Oct 18, 2012 - Knizhnikova, V.4.8 new DD_SetTimeInfo() function
* Dec 15, 2012 - Fedorov, V.6.0. DD_GetMultiData(). Request structure is very changed, CurrentData is not a global structure
* Dec 05, 2017 - Renard, V.7.0.
*===============================================================*/
#include <unistd.h>
#include "DD.h"
#include "DD_comm.h"
#include "DD_Access.h"
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define VERSION "V.6.0"
/*------------------ GLOBAL STATIC VARIABLES --------------------------*/
int SocketID = -1; /* Global socket id for this communication session */
char UserName[USRLENGTH]="\0"; /* string for user name */
char UserHost[MAXHOSTLENGTH] = "\0"; /* IP of user's host in a string form */
int UserID; /* ID of user at the user's host */
extern int errno;
/*================== FUNCTIONS =================================*/
int getInfoFromHostName(const char *hostName, struct sockaddr_in *addr)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
hints.ai_protocol = 0;
hints.ai_canonname = NULL;
hints.ai_addr = NULL;
hints.ai_next = NULL;
struct addrinfo *result;
int s;
s = getaddrinfo(hostName, NULL, &hints, &result);
if (s != 0)
{
return NOHOST;
}
struct sockaddr_in *h;
h = (struct sockaddr_in *) result->ai_addr;
memcpy(addr,h,sizeof(struct sockaddr_in));
freeaddrinfo(result);
return 0;
}
/*----------------------- GET User Host-------------------------------*/
int GetUserHost()
{
char HostName[HOSTNAMELENGTH];
struct hostent *Host;
unsigned int HostAddress;
int userid;
static unsigned int m1 = 0X000000FF;
static unsigned int m2 = 0X0000FF00;
static unsigned int m3 = 0X00FF0000;
static unsigned int m4 = 0XFF000000;
unsigned int b1,b2,b3,b4;
userid = getuid(); /* Get a client user ID */
/* UserHost can be set by extern WEB function in case of WEB communicatio,
* or it should be defined as local host if DD_client is installed at USER
* computer
*/
if(UserHost[0] == '\0')
{
gethostname(HostName,HOSTNAMELENGTH); /* Get a client host name */
struct sockaddr_in addr;
if (getInfoFromHostName(HostName,&addr) != 0)
return NOHOST;
unsigned int b1, b2, b3, b4;
sscanf(inet_ntoa( addr.sin_addr), "%3u.%3u.%3u.%3u", &b1, &b2, &b3, &b4);
snprintf(UserHost, MAXHOSTLENGTH, "%03u.%03u.%03u.%03u", b1, b2, b3, b4);
}
return userid;
}
/*-------------------------------------------------------------------*/
/*------------------ MAKE_CONNECTION ---------------------------*/
/*
* The function returns opened socket, ready to read and write
*/
int GetSocket()
/*
* Connect with remote server, and returns socket ID
* In case of error return error number according DD_comm.h
*/
{
/*-------- Variables for resource file -----------------*/
char rname[PATHLENGTH]; /* name of res file */
char line[PATHLENGTH];
char *path;
FILE *resfile; /* resource file */
char *pline;
/*---------- Variables for network ---------------------*/
static char RemSerName[PATHLENGTH]; /* name of remote server */
struct servent *serv; /* pointer to static structure of
port number of registrated service */
static struct sockaddr_in IntAFAddr; /* structure of internet address of type AF_INET
see <netinet/in.h>
to get name of socket we should
to convert pointer to this structure
to pointer to "srtruct sockaddr" where
all fields are "char array". */
int AFAddrLen = sizeof(struct sockaddr_in); /* size of internet address, <netinet/in.h> 18 bytes*/
int ScAddrLen = sizeof(struct sockaddr); /* size of socket address, <sys/socket.h> 18 bytes*/
int cc,i;
int arg = 0;
int normaddr;
/*
*---------- Getting socket and its address ---------------------*/
/*---------------- HOST and PORT ------------------------------*/
if((path = getenv("DDPATH")) == NULL)
{
strcpy(rname,"DD.res");
}
else
{
strcpy(rname,path);
strcat(rname, "/DD.res");
}
/*
(void *)strcpy(rname,"./DD.res");
*/
if((resfile = fopen(rname,"r")) == NULL)
{
fprintf(stderr,"No resource file, sorry\n");
exit(1);
}
pline = fgets(RemSerName, PATHLENGTH-1, resfile);
pline = fgets(line, PATHLENGTH-1, resfile);
fclose(resfile);
RemSerName[strlen(RemSerName)-1] = '\0';
sscanf(line,"%d",&(normaddr));
if (getInfoFromHostName(RemSerName,&IntAFAddr) != 0)
return NOHOST;
IntAFAddr.sin_port = htons((uint16_t)normaddr); /* Conversion of the port number to
universal net form */
/*---------------- END of FILE READING --------------------------*/
/*
*------- Trying to open socket with this address and name ---------
*/
if((SocketID = socket(AF_INET,SOCK_STREAM,PROTOCOL)) < 0)
{
perror("Socket:"); return(-1);
}
/* Connection to server socket on remote computer */
if(connect(SocketID,(struct sockaddr *)&IntAFAddr,AFAddrLen) < 0)
{ perror("connect"); return(-1); }
/*
* Get Host name and Host ID for security application
*/
if((UserID = GetUserHost()) < 0) return(NOHOST);
return SocketID;
}
/*------------------- end of GetSocket ---------------------*/
/*==================== Receive From Net ======================*/
/*
* Smart buffered receiver
*/
int ReceiveFromNet(char *buf, int PacketSize)
{
int CurLength = 0;
int i,cc;
do
{
i = 0;
while(((cc = recv(SocketID,buf+ CurLength,PacketSize - CurLength,0)) < 0) && (i < TRY)) i++;
if(cc < 0) return -1;
CurLength = CurLength + cc;
} while(CurLength < PacketSize);
return 1;
}
/*------------------ Open Virtual Instrument -----------------------*/
/*
* If no connection, connect to server, then try to order data set and return
* ID if OK or ErrorNumber if not
*/
int DD_SetVariable(char *InstrName)
{
static caddr_t buff = NULL;
static XDR xdrs;
int InstrID; /* Instrumnet ID to return */
int cc,i;
int Request = OPENINSREQ;
int hostlen,userlen;
char *PUserHost = UserHost, *PUserName = UserName;
/* We need this because XDR works with adresses of pointers */
/* If there is no connection, try to get it */
if(SocketID < 0)
if((SocketID = GetSocket()) < 0) return(NOCONNECTION);
/* Encoding request by XDR to buf */
/* Buffer and stream allocation */
buff = (caddr_t)malloc(REQUESTLENGTH);
xdrmem_create(&xdrs, buff, REQUESTLENGTH, XDR_ENCODE);
xdr_int(&xdrs,&Request);
xdr_string(&xdrs, &InstrName, strlen(InstrName));
/* Send Open Virtual Instrument request */
if((cc = send(SocketID,buff,REQUESTLENGTH,0)) < 0)
{
perror("DD_SetVariable:");
free(buff);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
xdrmem_create(&xdrs, buff, REQUESTLENGTH, XDR_FREE);
/*-------- preparation an authorization request ----------------*/
if(UserName[0] == '\0') strcpy(UserName,NONAME);
hostlen = strlen(UserHost);
userlen = strlen(UserName);
xdrmem_create(&xdrs, buff,REQUESTLENGTH, XDR_ENCODE);
xdr_int(&xdrs,&UserID);
xdr_string(&xdrs, &PUserHost, hostlen);
xdr_string(&xdrs, &PUserName, userlen);
/* fprintf(stderr,"userid %d host %s DD_name %s\n",UserID,UserHost,UserName); */
/* Send request */
if((cc = send(SocketID,buff,REQUESTLENGTH,0)) < 0)
{
perror("DD_Set_Variable:");
free(buff);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buff);
xdr_destroy(&xdrs);
/* Get reply */
buff = (caddr_t)malloc(REPLYLENGTH);
/*
i=0;
while(((cc = recv(SocketID,buff,REPLYLENGTH,0)) < 0) && (i < TRY)) i++;
*/
if((cc = ReceiveFromNet(buff, REPLYLENGTH)) < 0)
{
perror("DD_SET_Variable:");
free(buff);
return(REPLYRECERR);
}
xdrmem_create(&xdrs, buff, REPLYLENGTH, XDR_DECODE);
xdr_int(&xdrs,&InstrID);
/* Free all */
free(buff);
xdr_destroy(&xdrs);
return InstrID;
}
/*----------------------- DD_GetInform -----------------------------*/
/*
* Returns "OK" if OK, fill DD_Data structure
* Return negative value if error
*/
int DD_GetInform(int VarID,char *AttrName,DD_data_t **data)
{
static void *v = NULL;
static DD_data_t idd = {DD_CHAR, 0, NULL,1, &v};
static caddr_t buf = NULL;
static XDR xdrs;
int id;
int cc;
int length;
int op = DATAINFOREQ;
int type; /* type of information or error */
int DimNumbers;
int PacketSize;
int CurLength;
int i;
u_int count;
u_int MaxNumber;
/*------------------- Empty static data ------------------------*/
if(idd.Dimensions != NULL)
{
free(idd.Dimensions);
idd.Dimensions = NULL;
}
if(idd.Variables[0] != NULL)
{
free(idd.Variables[0]);
idd.Variables[0] = NULL;
}
/*------------------ Check the Name length -----------------------*/
if(strlen(AttrName) > MAXVARLENGTH) length = MAXVARLENGTH;
else length = strlen(AttrName);
/*----------- Allocation of memory and stream --------------------*/
buf = (caddr_t)malloc(REQUESTLENGTH);
xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);
/*------------------- Encoding the request ----------------------*/
xdr_int(&xdrs,&op);
xdr_int(&xdrs,&VarID);
xdr_string(&xdrs, &AttrName, length);
/*------------------------ Send request ---------------------------*/
if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
{
perror("DD_GetInform:");
free(buf);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buf);
xdr_destroy(&xdrs);
/*------------------------ Get reply header ------------------------*/
buf = (caddr_t)malloc(REPLYLENGTH);
/*
i=0;
while(((cc = recv(SocketID,buf,REPLYLENGTH,0)) < 0) && (i < TRY)) i++;
if(cc < 0)
*/
if((cc = ReceiveFromNet(buf, REPLYLENGTH)) < 0)
{
perror("DD_GetInform:");
free(buf);
return(REPLYRECERR);
}
xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
xdr_int(&xdrs,&type);
if(type < 0)
{
free(buf);
xdr_destroy(&xdrs);
return(type);
}
xdr_int(&xdrs,&DimNumbers);
xdr_int(&xdrs,&PacketSize);
free(buf);
xdr_destroy(&xdrs);
idd.DimNumber = DimNumbers;
idd.type = type;
/*---------------------------- Get Data Packet ----------------------*/
if(PacketSize > 0) buf = (caddr_t)malloc(PacketSize);
else return(REPLYRECERR);
if((cc = ReceiveFromNet(buf, PacketSize)) < 0)
{
perror("DD_GetInform:");
free(buf);
return(REPLYRECERR);
}
/*--------------------- Decoding data packet ------------------------*/
if(DimNumbers > 0)
{
/*----------- Memory allocation and xdr creating -----------------*/
xdrmem_create(&xdrs, buf, (u_int)PacketSize, XDR_DECODE);
/*----------------- Dimensions decoding -------------------------*/
if(idd.Dimensions !=NULL) free(idd.Dimensions);
idd.Dimensions = (int *)malloc(DimNumbers*sizeof(int));
xdr_array(&xdrs, (caddr_t *)&(idd.Dimensions),
&count,
(u_int )DimNumbers,
unixlen[DD_INT],
(xdrproc_t)xdr_int);
count = 1;
for(i=0; i < DimNumbers;i++)
count = count*idd.Dimensions[i];
MaxNumber = count;
}
else
{
free(buf);
return(REPLYRECERR);
}
/*---------------- Allocate memory for Variables and decoding ------------------*/
if(idd.Variables[0] != NULL) free(idd.Variables[0]);
idd.Variables[0] = (int *)malloc(unixlen[type] * MaxNumber);
xdr_array(&xdrs,((caddr_t*)(&idd.Variables[0])),&count,
MaxNumber, unixlen[type], (xdrproc_t)ConvFunc[type]);
/*-------------------------- Free all ------------------------------*/
free(buf);
xdr_destroy(&xdrs);
*data = &idd;
return OK;
}
/*====================================================
* DD_SetTime()
*====================================================*/
/*-----------------------------------------------------
* VarID - ID of Data Set
* Time - string in DD type
* Returns 1 if OK and negative in case of err
*---------------------------------------------------*/
int DD_SetTime(int VarID,char *Time)
{
static caddr_t buf = NULL;
static XDR xdrs;
static int op = TIMESETREQ;
int type;
int cc,i;
int DoneFlag = 0;
while(DoneFlag == 0)
{
/* Allocatein memory and stream */
buf = (caddr_t)malloc(REQUESTLENGTH);
xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);
/* Encoding the request */
xdr_int(&xdrs,&op);
xdr_int(&xdrs,&VarID);
xdr_string(&xdrs, &Time, TIMELENGTH);
/* Send request */
if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
{
perror("DD_SetTime:");
free(buf);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buf);
xdr_destroy(&xdrs);
/* Get reply header */
buf = (caddr_t)malloc(REPLYLENGTH);
if((cc = ReceiveFromNet(buf, REPLYLENGTH)) < 0)
{
perror("DD_SetTime:");
free(buf);
return(REPLYRECERR);
}
xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
xdr_int(&xdrs,&type);
free(buf);
xdr_destroy(&xdrs);
if(type < 0)
{
if(type == CACHTOOREC) sleep((unsigned )(FILEACCMARG));
else return type;
}
else DoneFlag = 1;
}
return 1 ;
}
/*==================================================================
* DD_SetTimeInfo()
*=================================================================*/
/*----------------------------------------------------------
* VarID - ID of Data Set
* Time - string in DD type
* RealTime - double : time value pointer points to
* Returns 1 if OK and negative in case of err
*----------------------------------------------------------*/
int DD_SetTimeInfo(int VarID, char *Time, double *RealTime)
{
int err;
static char TimeInt[17] = "0000000000001000\0";
DD_data_t *data;
err = DD_SetTime(VarID, Time);
if (err < 1) return err;
err = DD_GetData(VarID, "Time", TimeInt, &data);
if (err < 0) return err;
if (data->VarNumber < 1) return OUTOFTIME;
if (data->type == DD_CHAR) {
*RealTime = DD_Time2Double((char *)data->Variables[0]);
}
else if (data->type == DD_DOUBLE) {
*RealTime = *((double*)data->Variables[0]);
}
else {
return DATAFILEERR;
}
//BRE - When the corresponding time is the last one of a file, MOREDATA is returned by DDServer.
// => force recall of DD_GetData to open the next file and finish correctly the request
if (err == MOREDATA)
{
if ((err = DD_GetData(VarID, (char *)"Time", TimeInt, &data)) < 0)
{
while ((err == WAITEXTCALL) || (err == TRYAGAIN))
{
sleep(2);
err = DD_GetData(VarID, (char *)"Time", TimeInt, &data);
}
}
}
return 1 ;
}
/*=========================================================
* DD_GetData()
* Just an interface to the DD_GetMultiData()
*========================================================*/
int DD_GetData(int VarID, char *VarName, char *TimeInt, DD_data_t **data)
{
char *xxx=VarName;
int err;
err = DD_GetMultiData(VarID, 1, &xxx, TimeInt, data, 1);
return err;
}
/*=========================================================
* DD_GetMultiData()
*========================================================*/
/*---------------------------------------------------------
* VarID - ID of open virtual instrument
* VarSize - the size of the array names
* VarNames - araay of the variables names
* TimeInt - Time interval in DD_time style
* data - returned data
* Return values:
* positives:
* MOREDELAY - call this function again after delay
* MOREDATA - call this function again to complete request
* OK - all data received
* negative value - error (see DD.h)
*---------------------------------------------------------*/
int DD_GetMultiData(int VarID,size_t VarSize, char **VarNames,char *TimeInt,DD_data_t **data, int BackFlag)
{
static DD_data_t *CurrentData = NULL; /* Data records array to keep until the next call */
static size_t OldVarSize = 0;
static caddr_t buf = NULL;
static XDR xdrs;
static int LastPacketFlag = OK;
int id;
int cc;
int *NameLength = NULL; /* Array of lengths */
int XDRStrLen; /* xdr length of a string */
int XDRReqlength; // Length in bytes the XDR stream
int op = DATAGETREQ;
int type; /* type of information or error */
int DimNumbers;
int PacketSize;
int CurLength;
u_int count, MaxSize;
int i;
/* Empty static data */
for(id = 0; id < OldVarSize; id++)
{
if(CurrentData[id].Dimensions != NULL)
{
free(CurrentData[id].Dimensions);
CurrentData[id].Dimensions = NULL;
}
if(CurrentData[id].Variables != NULL)
{
for(i = 0; i< CurrentData[id].VarNumber; i++)
free(CurrentData[id].Variables[i]);
free(CurrentData[id].Variables);
CurrentData[id].Variables = NULL;
}
}
free(CurrentData);
/*--------------------------------------
* Create a new Current Data empty array
*-------------------------------------*/
OldVarSize = VarSize;
CurrentData = (DD_data_t *)malloc(sizeof(DD_data_t)*VarSize);
for(id = 0; id < VarSize; id++)
{
CurrentData[id].Dimensions = NULL;
CurrentData[id].Variables = NULL;
CurrentData[id].DimNumber = 0;
CurrentData[id].VarNumber = 0;
CurrentData[id].type = 0;
}
/*--------------------------------------------------------------
* The follows request preparation and send is performed only
* if the priveous data packed is completely received
*--------------------------------------------------------------*/
if((LastPacketFlag == OK) || (LastPacketFlag == MOREDELAY))
{
/*---------------------------------------
* Preparation of the array the name lengths
* Note that the names are cut down to the MAXVARLENGTH
* Calculation the packet size
*---------------------------------------*/
NameLength = (int *)malloc(sizeof(int)*VarSize);
XDRReqlength = 8; /* the length of the first+last arguments */
for(id = 0; id < VarSize; id++)
{
if(strlen(VarNames[id]) > MAXVARLENGTH) NameLength[id] = MAXVARLENGTH;
else NameLength[id] = strlen(VarNames[id]);
XDRStrLen = (NameLength[id] / 4) * 4;
if((NameLength[id] % 4) > 0) XDRStrLen += 4;
XDRReqlength += XDRStrLen + 4; // String length + 4 bytes length coding
//fprintf(stderr,"Variable %s, length %d, total %d\n",VarNames[id],XDRStrLen,XDRReqlength);
}
/*--------------------------------
* The first packet of the request of the standard size
* Allocation corresponding memory and stream
*------------------------------*/
buf = (caddr_t)malloc(REQUESTLENGTH);
xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);
/* Encoding the request */
xdr_int(&xdrs,&op);
xdr_int(&xdrs,&VarID);
xdr_string(&xdrs,&TimeInt,TIMELENGTH);
xdr_int(&xdrs,&XDRReqlength);
/* Send the first request */
if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
{
perror("DD_GetData: First Request");
free(buf);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buf);
xdr_destroy(&xdrs);
/*--------------------------------
* The Second packet of XDRReqlength size
* Allocation corresponding memory and stream
*------------------------------*/
buf = (caddr_t)malloc(XDRReqlength);
xdrmem_create(&xdrs, buf, XDRReqlength, XDR_ENCODE);
xdr_int(&xdrs,(int *)(&VarSize));
for(id = 0; id < VarSize; id++) xdr_string(&xdrs, &VarNames[id], NameLength[id]);
xdr_int(&xdrs,&BackFlag);
/* Send the second request */
if((cc = send(SocketID,buf,XDRReqlength,0)) < 0)
{
perror("DD_GetData:Second Packet");
free(buf);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buf);
xdr_destroy(&xdrs);
}
/*-----------------------------------------------------------
* Get the VarSize reply headers and VarSize data
*----------------------------------------------------------*/
for(id = 0; id < VarSize; id++)
{
/*-------------------
* The header
*-----------------*/
buf = (caddr_t)malloc(REPLYLENGTH);
if((cc = ReceiveFromNet(buf, REPLYLENGTH)) < 0)
{
perror("DD_GetData:Error while header receive");
free(buf);
return(REPLYRECERR);
}
xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
xdr_int(&xdrs,&type);
xdr_int(&xdrs,&DimNumbers);
xdr_int(&xdrs,&PacketSize);
free(buf);
xdr_destroy(&xdrs);
if((type < 0) && (type != NOVAR)) /* Server returns unrecoverable error */
{
fprintf(stderr,"DD_GetData: Server returns %d, unrecovable error\n",type);
return type;
}
/*----------- Header is received ----------------------*/
CurrentData[id].DimNumber = DimNumbers - 1;
CurrentData[id].type = type;
/*--------------- Receive Data Packet ------------------------------*/
if(PacketSize > 0) buf = (caddr_t)malloc(PacketSize);
else return(REPLYRECERR);
if((cc = ReceiveFromNet(buf, PacketSize)) < 0)
{
perror("DD_GetData: Error while data packet receive");
free(buf);
return(REPLYRECERR);
}
/*----------------- Decoding data packet --------------------*/
if(DimNumbers > 1)
{
xdrmem_create(&xdrs, buf, (u_int)PacketSize, XDR_DECODE);
count = DimNumbers-1;
/* allocation memory for dimensions */
CurrentData[id].Dimensions = (int *)malloc((DimNumbers-1)*sizeof(int));
xdr_int(&xdrs,&LastPacketFlag); /* Last Paket Indicatort*/
xdr_int(&xdrs,&CurrentData[id].VarNumber); /* Variables number */
if(LastPacketFlag > 2)
{
LastPacketFlag = OK;
CurrentData[id].VarNumber = 0;
free(buf);
return(REPLYRECERR);
}
/*----- Decoding dimensions array -------------------*/
xdr_array(&xdrs, (caddr_t *)&(CurrentData[id].Dimensions),
&count,
(u_int )DimNumbers-1,
unixlen[DD_INT],
(xdrproc_t)xdr_int);
count = 1;
for(i=0; i < DimNumbers-1;i++)
{
count = count*CurrentData[id].Dimensions[i];
}
MaxSize = count;
}
else
{
if(type != NOVAR)
{
free(buf);
return(REPLYRECERR);
}
else
{
CurrentData[id].VarNumber = 0;
}
}
/*---------- Decoding variables -------------------------*/
if(CurrentData[id].VarNumber != 0) CurrentData[id].Variables = (void *)malloc(CurrentData[id].VarNumber*sizeof(void *));
else CurrentData[id].Variables = NULL;
for(i = 0; i < CurrentData[id].VarNumber; i++)
{
CurrentData[id].Variables[i] = (void *)malloc(unixlen[type]*MaxSize);
count = MaxSize;
xdr_array(&xdrs,(caddr_t *)(&(CurrentData[id].Variables[i])),&count,MaxSize,unixlen[type], (xdrproc_t)ConvFunc[type]);
}
/* Free all */
free(buf);
xdr_destroy(&xdrs);
}
*data = CurrentData;
return(LastPacketFlag);
}
/*===================================================================*/
/*-------------------------- DD_CLOSE --------------------------------*/
/*
*
* VarID - entry point of ID table
* Returns 1 if OK or negative err
* Close Socket if no VarID more ( this information supplyed by server )
*/
int DD_Close(int VarID)
{
static caddr_t buf = NULL;
static XDR xdrs;
static int op = CLOSEINSREQ;
int RemainedID; /* Server returns number of remained ID */
int cc,i;
/* Allocatein memory and stream */
buf = (caddr_t)malloc(REQUESTLENGTH);
xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);
/* Encoding the request */
xdr_int(&xdrs,&op);
xdr_int(&xdrs,&VarID);
/* Send request */
if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
{
perror("DD_Close:");
free(buf);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buf);
xdr_destroy(&xdrs);
/* Get reply header */
buf = (caddr_t)malloc(REPLYLENGTH);
/*
i = 0;
while(((cc = recv(SocketID,buf,REPLYLENGTH,0)) < 0) && (i < TRY)) i++;
if(cc < 0)
*/
if((cc = ReceiveFromNet(buf, REPLYLENGTH)) < 0)
{
perror("DD_Close:");
free(buf);
return(REPLYRECERR);
}
xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
xdr_int(&xdrs,&RemainedID);
if(RemainedID< 0)
{
free(buf);
xdr_destroy(&xdrs);
return(RemainedID);
}
free(buf);
xdr_destroy(&xdrs);
if(RemainedID == 0)
{
shutdown(SocketID,2);
close(SocketID);
SocketID = -1;
/*
UserHost[0] = '\0';
UserName[0] = '\0';
*/
}
return(1);
}
/*------------------------------------------------------------------------------*/
/*=============== DATABASE UPDATE FUNCTIONS ====================================*/
/*------------------ DD_AddDataSet() -------------------------------------------*/
int DD_AddDataSet(char *DataSetName, char *RemSetID, char *BaseName)
{
static caddr_t buf = NULL;
static XDR xdrs;
static int op = ADDVIREQ;
int Err; /* Server returns OK or NOEXEC */
int cc,i,length;
int CloseFlag = 0;
/* If there is no connection, try to get it */
if(SocketID < 0)
{
if((SocketID = GetSocket()) < 0) return(NOCONNECTION);
CloseFlag = 1; // To close Connection at the end
}
/* Allocatein memory and stream */
buf = (caddr_t)malloc(REQUESTLENGTH);
xdrmem_create(&xdrs, buf, REQUESTLENGTH, XDR_ENCODE);
/* Encoding the request */
xdr_int(&xdrs,&op);
/* Coding DataSetName */
if((length = strlen(DataSetName)) > MAXSETLENGTH) length = MAXSETLENGTH;
xdr_string(&xdrs, &DataSetName, length);
/* Coding RemSetID */
if((length = strlen(RemSetID)) > MAXVARLENGTH) length = MAXVARLENGTH;
xdr_string(&xdrs, &RemSetID, length);
/* Coding BaseName */
if((length = strlen(BaseName)) > MAXVARLENGTH) length = MAXVARLENGTH;
xdr_string(&xdrs, &BaseName, length);
/* Send request */
if((cc = send(SocketID,buf,REQUESTLENGTH,0)) < 0)
{
perror("DD_AddDataSet:");
free(buf);
xdr_destroy(&xdrs);
return(REQSENDERR);
}
free(buf);
xdr_destroy(&xdrs);
/* Get reply header */
buf = (caddr_t)malloc(REPLYLENGTH);
if((cc = ReceiveFromNet(buf, REPLYLENGTH)) < 0)
{
perror("DD_AddDataSet:");
free(buf);
return(REPLYRECERR);
}
xdrmem_create(&xdrs, buf, REPLYLENGTH, XDR_DECODE);
xdr_int(&xdrs,&Err);
if(CloseFlag) // Close everything if function established connection
{
shutdown(SocketID,2);
close(SocketID);
SocketID = -1;
/* Session with DD Server was closed, but User Session continues !!!!! */
/* UserHost[0] = '\0';
UserName[0] = '\0'; */
}
return Err;
}
/*-----------------------------------------------------------------------------*/
/*---------------------- DD GET VERSION ---------------------------------------*/
char *DD_GetVersion()
/*
* return static pointer to internal string with the DDLIB-SERVER package version
*/
{
static char Version[] = VERSION;
return (char *)Version;
}
/*------------------------- End Library -------------------------------*/