-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcdrserver.cpp
More file actions
1630 lines (1465 loc) · 69.7 KB
/
Copy pathcdrserver.cpp
File metadata and controls
1630 lines (1465 loc) · 69.7 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
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003-2006 MySQL AB
All rights reserved. Use is subject to license terms.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* **************************************************************** */
/* */
/* S E R V . T C P */
/* * This is an example program that demonstrates the use of */
/* stream sockets as an IPC mechanism. This contains the server, */
/* and is intended to operate in conjunction with the client */
/* program found in client.tcp. Together, these two programs */
/* demonstrate many of the features of sockets, as well as good */
/* conventions for using these features. */
/* * This program provides a service called "example". In order for*/
/* it to function, an entry for it needs to exist in the */
/* ./etc/services file. The port address for this service can be */
/* any port number that is likely to be unused, such as 22375, */
/* for example. The host on which the client will be running */
/* must also have the same entry (same port number) in its */
/* ./etc/services file. */
/* **************************************************************** */
#include <ndb_global.h>
/******** NDB INCLUDE ******/
#include <NdbApi.hpp>
/***************************/
/*#include <sys/shm.h>*/
#include <pthread.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <netinet/in.h>
#include <signal.h>
#include <netdb.h>
#include <time.h>
#include <synch.h>
#include <sched.h>
extern "C" {
#include "utv.h"
#include "vcdrfunc.h"
#include "bcd.h"
}
#ifndef TESTLEV
#define TESTLEV
#endif
//#define DEBUG
//#define MYDEBUG
//#define SETDBG
//#define ops_before_exe 64
#define MAXOPSEXEC 1024
/* Used in nanosleep */
/**** NDB ********/
static int bTestPassed;
void create_table(Ndb* pMyNdb);
void error_handler(const char* errorText);
/*****************/
static struct timespec tmspec1;
static int server(long int);
/* Function for initiating the cdr-area and make it clean for ongoing calls */
static int s; /* connected socket descriptor */
static int ls; /* listen socket descriptor */
static struct hostent *hp; /* pointer to host info for remote host */
static struct servent *sp; /* pointer to service information */
struct linger linger; /* allow a lingering, graceful close; */
/* used when setting SO_LINGER */
static struct sockaddr_in myaddr_in; /* for local socket address */
static struct sockaddr_in peeraddr_in; /* for peer socket address */
static FILE *fi; /* Log output */
static char temp[600]="";
static int ops_before_exe = 1; /* Number of operations per execute, default is 1,
but it can be changed with the -o parameter. */
/*----------------------------------------------------------------------
M A I N
* This routine starts the server. It forks, leaving the child
to do all the work, so it does not have to be run in the
background. It sets up the listen socket, and for each incoming
connection, it forks a child process to process the data. It
will loop forever, until killed by a signal.
----------------------------------------------------------------------*/
/****** NDB *******/
static char *tableName = "VWTABLE";
/******************/
#include <iostream>
using namespace std;
int main(int argc, const char** argv)
{
ndb_init();
/******** NDB ***********/
/*
Ndb MyNdb( "TEST_DB" );
int tTableId;
*/
/************************/
char tmpbuf[400];
/* Loop and status variables */
int i,j,found;
/* Used by the server */
int addrlen;
/* return code used with functions */
int rc;
i = 1;
while (argc > 1)
{
if (strcmp(argv[i], "-o") == 0)
{
ops_before_exe = atoi(argv[i+1]);
if ((ops_before_exe < 1) || (ops_before_exe > MAXOPSEXEC))
{
cout << "Number of operations per execute must be at least 1, and at most " << MAXOPSEXEC << endl;
exit(1);
}
}
else
{
cout << "Invalid parameter!" << endl << "Look in cdrserver.C for more info." << endl;
exit(1);
}
argc -= 2;
i = i + 2;
}
/* Setup log handling */
logname(temp,"Cdrserver","Mother","");
puts(temp);
fi=fopen(temp,"w");
if (fi == NULL)
{
perror(argv[0]);
exit(EXIT_FAILURE);
}
m2log(fi,"Initiation of program");
/***** NDB ******/
/*
MyNdb.init();
if (MyNdb.waitUntilReady(30) != 0)
{
puts("Not ready");
exit(-1);
}
tTableId = MyNdb.getTable()->openTable(tableName);
if (tTableId == -1)
{
printf("%d: Creating table",getpid());
create_table(&MyNdb);
}
else printf("%d: Table already create",getpid());
*/
/****************/
/* clear out address structures */
memset ((char *)&myaddr_in, 0, sizeof(struct sockaddr_in));
memset ((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in));
m2log(fi,"Socket setup starting");
/* Set up address structure for the listen socket. */
myaddr_in.sin_family = AF_INET;
/* The server should listen on the wildcard address, */
/* rather than its own internet address. This is */
/* generally good practice for servers, because on */
/* systems which are connected to more than one */
/* network at once will be able to have one server */
/* listening on all networks at once. Even when the */
/* host is connected to only one network, this is good */
/* practice, because it makes the server program more */
/* portable. */
myaddr_in.sin_addr.s_addr = INADDR_ANY;
/* Find the information for the "cdrserver" server */
/* in order to get the needed port number. */
sp = getservbyname ("cdrserver", "tcp");
if (sp == NULL) {
m2log(fi,"Service cdrserver not found in /etc/services");
m2log(fi,"Terminating.");
exit(EXIT_FAILURE);
}
myaddr_in.sin_port = sp->s_port;
/* Create the listen socket.i */
ls = socket (AF_INET, SOCK_STREAM, 0);
if (ls == -1) {
m2log(fi,"Unable to create socket");
m2log(fi,"Terminating.");
exit(EXIT_FAILURE);
}
printf("Socket created\n");
printf("Wait..........\n");
/* Bind the listen address to the socket. */
if (bind(ls,(struct sockaddr*)&myaddr_in, sizeof(struct sockaddr_in)) == -1) {
m2log(fi,"Unable to bind address");
m2log(fi,"Terminating.");
exit(EXIT_FAILURE);
}
/* Initiate the listen on the socket so remote users */
/* can connect. The listen backlog is set to 5, which */
/* is the largest currently supported. */
if (listen(ls, 5) == -1) {
m2log(fi,"Unable to listen on socket");
m2log(fi,"Terminating.");
exit(EXIT_FAILURE);
}
/* Now, all the initialization of the server is */
/* complete, and any user errors will have already */
/* been detected. Now we can fork the daemon and */
/* return to the user. We need to do a setpgrp */
/* so that the daemon will no longer be associated */
/* with the user's control terminal. This is done */
/* before the fork, so that the child will not be */
/* a process group leader. Otherwise, if the child */
/* were to open a terminal, it would become associated */
/* with that terminal as its control terminal. It is */
/* always best for the parent to do the setpgrp. */
m2log(fi,"Socket setup completed");
m2log(fi,"Start server");
setpgrp();
/* Initiate the tmspec struct for use with nanosleep() */
tmspec1.tv_sec = 0;
tmspec1.tv_nsec = 1;
printf("Waiting for client to connect.........\n");
printf("Done\n");
switch (fork()) {
case -1: /* Unable to fork, for some reason. */
m2log(fi,"Failed to start server");
m2log(fi,"Terminating.");
fclose(fi);
perror(argv[0]);
fprintf(stderr, "%s: unable to fork daemon\n", argv[0]);
exit(EXIT_FAILURE);
break;
case 0: /* The child process (daemon) comes here. */
m2log(fi,"Server started");
/* Close stdin and stderr so that they will not */
/* be kept open. Stdout is assumed to have been */
/* redirected to some logging file, or /dev/null. */
/* From now on, the daemon will not report any */
/* error messages. This daemon will loop forever, */
/* waiting for connections and forking a child */
/* server to handle each one. */
close((int)stdin);
close((int)stderr);
/* Set SIGCLD to SIG_IGN, in order to prevent */
/* the accumulation of zombies as each child */
/* terminates. This means the daemon does not */
/* have to make wait calls to clean them up. */
signal(SIGCLD, SIG_IGN);
for(EVER) {
if ((checkchangelog(fi,temp))==0)
m2log(fi,"Waiting for connection");
/* Note that addrlen is passed as a pointer */
/* so that the accept call can return the */
/* size of the returned address. */
addrlen = sizeof(struct sockaddr_in);
/* This call will block until a new */
/* connection arrives. Then, it will */
/* return the address of the connecting */
/* peer, and a new socket descriptor, s, */
/* for that connection. */
s = accept(ls,(struct sockaddr*) &peeraddr_in, &addrlen);
#ifdef MYDEBUG
puts("accepted");
#endif
if ((checkchangelog(fi,temp))==0)
m2log(fi,"Connection attempt from a client");
if ((checkchangelog(fi,temp))==0)
m2log(fi,"Start communication server");
if ( s == -1) exit(EXIT_FAILURE);
switch (fork()) {
case -1: /* Can't fork, just exit. */
if ((checkchangelog(fi,temp))==0)
m2log(fi,"Start communication server failed.");
exit(EXIT_FAILURE);
break;
case 0: /* Child process comes here. */
/* Get clients adress and save it in the info area */
/* Keep track of how many times the client connects to the server */
printf("Connect attempt from client %u\n",peeraddr_in.sin_addr.s_addr);
server(peeraddr_in.sin_addr.s_addr);
exit(EXIT_FAILURE);
break;
default: /* Daemon process comes here. */
/* The daemon needs to remember */
/* to close the new accept socket */
/* after forking the child. This */
/* prevents the daemon from running */
/* out of file descriptor space. It */
/* also means that when the server */
/* closes the socket, that it will */
/* allow the socket to be destroyed */
/* since it will be the last close. */
close(s);
break;
}
}
default: /* Parent process comes here. */
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
/*----------------------------------------------------------------------
S E R V E R
* This is the actual server routine that the daemon forks to
handle each individual connection. Its purpose is to receive
the request packets from the remote client, process them,
and return the results to the client. It will also write some
logging information to stdout.
----------------------------------------------------------------------*/
server(long int servernum)
{
/******** NDB ***********/
Ndb MyNdb( "TEST_DB" );
int tTableId;
NdbConnection *MyTransaction;
NdbOperation *MyOperation;
int check;
int c1 = 0;
int c2 = 0;
int c3 = 0;
int c4 = 0;
int act_index = 0;
/************************/
register unsigned int reqcnt; /* keeps count of number of requests */
register unsigned int i; /* Loop counters */
register int x;
register short done; /* Loop variable */
short int found;
/* The server index number */
int thisServer;
/* Variables used to keep track of some statistics */
time_t ourtime;
time_t tmptime;
int tmpvalue;
long int tmptransfer;
long int transfer;
int ops = 0;
/* Variables used by the server */
char buf[400]; /* This example uses 10 byte messages. */
char *inet_ntoa();
char *hostname; /* points to the remote host's name string */
int len;
int rcvbuf_size;
long ctid;
unsigned char uc;
/* Variables used by the logging facilitiy */
char msg[600];
char crap[600];
char lognamn[600];
FILE *log;
/* scheduling parameter for pthread */
struct sched_param param1,param2,param3;
/* Header information */
/* cdrtype not used */
/*short cdrtype; */ /* 1 CDR Typ */
short cdrlen; /* 2 CDR recored length in bytes excluding CDR type */
short cdrsubtype; /* 1 CDR subtype */
unsigned int cdrid; /* 8 CDR unique number of each call */
unsigned int cdrtime; /* 4 CDR Time in seconds */
short cdrmillisec; /* 2 CDR Milliseconds */
short cdrstatus; /* 1 CDR For future use */
short cdrequipeid; /* 1 CDR Equipment id */
int cdrreserved1; /* 4 CDR For future use */
/* Defined or calculated for each record */
int cdrrestlen; /* Unprocessed data left in record in bytes */
/* Gemensamma datatyper */
unsigned short parmtype_prev; /* 1 Parameter type */
unsigned short parmtype; /* 1 Parameter type */
unsigned short parmlen; /* 1 Parameter type */
int rc; /* return code for functions */
/* Attribute object used with threads */
pthread_attr_t attr1;
pthread_attr_t attr2;
pthread_attr_t attr3;
struct cdr_record *tmpcdrptr,*ftest;
void *dat;
int error_from_client = 0;
/* Konstanter */
const int headerlen = 24; /* Length of header record */
parmtype_prev = 99;
reqcnt = 0;
/* Close the listen socket inherited from the daemon. */
close(ls);
printf("Use the readinfo program to get information about server status\n\n");
if((checkchangelog(fi,temp))==0)
c2log(fi,"Communication server started");
/* Look up the host information for the remote host */
/* that we have connected with. Its internet address */
/* was returned by the accept call, in the main */
/* daemon loop above. */
hp=gethostbyaddr((char *) &peeraddr_in.sin_addr,sizeof(struct in_addr),peeraddr_in.sin_family);
if (hp == NULL) {
/* The information is unavailable for the remote */
/* host. Just format its internet address to be */
/* printed out in the logging information. The */
/* address will be shown in "internet dot format". */
/*
hostname = inet_ntoa(peeraddr_in.sin_addr);
*/
sprintf(hostname,"Test");
logname(lognamn,"Cdrserver","Child",hostname);
}
else {
hostname = hp->h_name; /* point to host's name */
logname(lognamn,"Cdrserver","Child",hostname);
}
log=fopen(lognamn,"w");
if (log == NULL)
{
perror(hostname);
exit(EXIT_FAILURE);
}
n2log(log,"Setup in progress");
/* Log a startup message. */
/* The port number must be converted first to host byte */
/* order before printing. On most hosts, this is not */
/* necessary, but the ntohs() call is included here so */
/* that this program could easily be ported to a host */
/* that does require it. */
BaseString::snprintf(msg,sizeof(msg),"Startup from %s port %u",hostname,ntohs(peeraddr_in.sin_port));
if ((checkchangelog(fi,temp))==0)
c2log(fi,msg);
n2log(log,msg);
BaseString::snprintf(msg,sizeof(msg),"For further information, see log(%s)",lognamn);
if ((checkchangelog(fi,temp))==0)
c2log(fi,msg);
/* Set the socket for a lingering, graceful close. */
/* This will cause a final close of this socket to wait until */
/* all * data sent on it has been received by the remote host. */
linger.l_onoff =1;
linger.l_linger =0;
if (setsockopt(s, SOL_SOCKET, SO_LINGER,(const char*)&linger,sizeof(linger)) == -1) {
BaseString::snprintf(msg,sizeof(msg),"Setting SO_LINGER, l_onoff=%d, l_linger=%d",linger.l_onoff,linger.l_linger);
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
goto errout;
}
/* Set the socket for a lingering, graceful close. */
/* This will cause a final close of this socket to wait until all * data sent */
/* on it has been received by the remote host. */
rcvbuf_size=64*1024;
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF,(const char*) &rcvbuf_size,sizeof(rcvbuf_size)) == -1) {
BaseString::snprintf(msg,sizeof(msg),"Setting SO_RCVBUF = %d",rcvbuf_size);
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
goto errout;
}
/* Set nodelay on socket */
n2log(log,"Port setup complete");
/* Go into a loop, receiving requests from the remote */
/* client. After the client has sent the last request, */
/* it will do a shutdown for sending, which will cause */
/* an end-of-file condition to appear on this end of the */
/* connection. After all of the client's requests have */
/* been received, the next recv call will return zero */
/* bytes, signalling an end-of-file condition. This is */
/* how the server will know that no more requests will */
/* follow, and the loop will be exited. */
n2log(log,"Setup completed");
/* Fetch the process id for the server */
/* Inititate the variables used for counting transfer rates and rec/sec */
tmpvalue = 0;
tmptime = 0;
tmptransfer = 0;
transfer = 0;
printf("Client %s connected\nStarting to process the data\n\n",hostname);
tmpcdrptr = (struct cdr_record*)malloc(sizeof(struct cdr_record));
/***** NDB ******/
MyNdb.init();
if (MyNdb.waitUntilReady(30) != 0)
{
puts("Not ready");
exit(-1);
}
tTableId = MyNdb.getTable()->openTable(tableName);
if (tTableId == -1)
{
printf("%d: Creating table",getpid());
create_table(&MyNdb);
}
else printf("%d: Table already created",getpid());
/****************/
while (len = recv(s,buf,headerlen,MSG_WAITALL)) {
if (len == -1) {
snprintf(msg,sizeof(msg),"Error from recv");
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
goto errout; /* error from recv */
}
/* The reason this while loop exists is that there */
/* is a remote possibility of the above recv returning */
/* less than 10 bytes. This is because a recv returns */
/* as soon as there is some data, and will not wait for */
/* all of the requested data to arrive. Since 10 bytes */
/* is relatively small compared to the allowed TCP */
/* packet sizes, a partial receive is unlikely. If */
/* this example had used 2048 bytes requests instead, */
/* a partial receive would be far more likely. */
/* This loop will keep receiving until all 10 bytes */
/* have been received, thus guaranteeing that the */
/* next recv at the top of the loop will start at */
/* the begining of the next request. */
for (;len < headerlen;) {
x = recv(s,buf,(headerlen-len),0);
if (x == -1) {
snprintf(msg,sizeof(msg),"Error from recv");
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
goto errout; /* error from recv */
}
len=len+x;
}
if (ops == 0) {
MyTransaction = MyNdb.startTransaction();
if (MyTransaction == NULL)
error_handler(MyNdb.getNdbErrorString());
}//if
MyOperation = MyTransaction->getNdbOperation(tableName);
if (MyOperation == NULL)
error_handler(MyTransaction->getNdbErrorString());
/*------------------------------------------------------*/
/* Parse header of CDR records */
/*------------------------------------------------------*/
/*------------------------------------------------------*/
/* 1. Type of cdr */
/*------------------------------------------------------*/
/* Not used for the moment
cdrtype=(char)buf[0];
*/
/*------------------------------------------------------*/
/* 2. Total length of CDR */
/*------------------------------------------------------*/
swab(buf+1,buf+1,2);
memcpy(&cdrlen,buf+1,2);
/*------------------------------------------------------*/
/* 3. Partial type of CDR */
/*------------------------------------------------------*/
cdrsubtype=(char)buf[3];
switch (cdrsubtype)
{
case 0:
c1++;
tmpcdrptr->CallAttemptState = 1;
check = MyOperation->insertTuple();
break;
case 1:
c2++;
tmpcdrptr->CallAttemptState = 2;
check = MyOperation->updateTuple();
break;
case 2:
c3++;
tmpcdrptr->CallAttemptState = 3;
check = MyOperation->deleteTuple();
break;
case 3:
c4++;
tmpcdrptr->CallAttemptState = 4;
check = MyOperation->deleteTuple();
break;
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
}
/*cdrsubtype=(cdrsubtype << 24) >> 24;*/
/*------------------------------------------------------*/
/* 4. ID number */
/*------------------------------------------------------*/
/*swab(buf+4,buf+4,4);*/ /* ABCD -> BADC */
/*
swab(buf+4,buf+4,4);
swab(buf+5,buf+5,2);
swab(buf+6,buf+6,2);
swab(buf+4,buf+4,2);
swab(buf+5,buf+5,2);
*/
memcpy(&cdrid,buf+4,4);
tmpcdrptr->CallIdentificationNumber = cdrid;
#ifdef SETDBG
puts("CIN");
#endif
check = MyOperation->equal("CIN",(char*)&cdrid);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
#ifdef SETDBG
puts("CAS");
#endif
if (cdrsubtype < 2)
{
check = MyOperation->setValue("CAS",(char*)&cdrsubtype);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
}
/*------------------------------------------------------*/
/* 5. Time stamp */
/*------------------------------------------------------*/
swab(buf+12,buf+12,4);
swab(buf+13,buf+13,2);
swab(buf+14,buf+14,2);
swab(buf+12,buf+12,2);
swab(buf+13,buf+13,2);
memcpy(&cdrtime,buf+12,4);
switch (cdrsubtype)
{
case 0:
#ifdef SETDBG
puts("START_TIME");
#endif
check = MyOperation->setValue("START_TIME",(char*)&cdrtime);
break;
case 1:
#ifdef SETDBG
puts("Start1");
#endif
check = MyOperation->setValue("StartOfCharge",(char*)&cdrtime);
break;
case 2:
#ifdef SETDBG
puts("Start2");
#endif
/*
check = MyOperation->setValue("StopOfCharge",(char*)&cdrtime);
*/
check = 0;
break;
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
}
/*------------------------------------------------------*/
/* 6. Milliseconds */
/*------------------------------------------------------*/
/* Not used by application
swab(buf+16,buf+16,2);
memcpy(&cdrmillisec,buf+16,2);
*/
/*------------------------------------------------------*/
/* 7. CDR status reserverd for future use */
/*------------------------------------------------------*/
/* Not used by application
memcpy(&cdrstatus,buf+18,1);
*/
/*------------------------------------------------------*/
/* 8. CDR equipe id, number of sending equipement */
/*------------------------------------------------------*/
/* Not used by application
memcpy(&cdrequipeid,buf+19,1);
*/
/*cdrequipeid=(cdrequipeid << 24) >> 24;*/
/*------------------------------------------------------*/
/* 9. CDR reserverd for furter use */
/*------------------------------------------------------*/
/* Not used by applikation
swab(buf+20,buf+20,4);
swab(buf+21,buf+21,2);
swab(buf+22,buf+22,2);
swab(buf+20,buf+20,2);
swab(buf+21,buf+21,2);
memcpy(&cdrreserved1,buf+20,4);
*/
/*------------------------------------------------------*/
/* calculate length of datapart in record */
/* Formula recordlength-headerlen-1 */
/*------------------------------------------------------*/
cdrrestlen=cdrlen-(headerlen-1);
/*------------------------------------------------------*/
/* Finished with header */
/*------------------------------------------------------*/
/* Read remaining cdr data into buffer for furter */
/* handling. */
/*------------------------------------------------------*/
len = recv(s,buf,cdrrestlen,MSG_WAITALL);
if (len == -1) {
snprintf(msg,sizeof(msg),"Error from recv");
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
goto errout; /* error from recv */
}
for (;len<cdrrestlen;) {
x = recv(s,buf,len-cdrrestlen,0);
if (x == -1) {
snprintf(msg,sizeof(msg),"Error from recv");
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
goto errout; /* error from recv */
}
len=len+x;
}
done=FALSE;
/* Count the transfer/sec */
tmptransfer += cdrlen;
if (cdrsubtype > 1)
{
#ifdef SETDBG
puts("Going to execute");
#endif
ops++;
if (ops == ops_before_exe) {
ops = 0;
check = MyTransaction->execute(Commit, CommitAsMuchAsPossible);
if ((check == -1) && (MyTransaction->getNdbError() != 0))
error_handler(MyTransaction->getNdbErrorString());
MyNdb.closeTransaction(MyTransaction);
#ifdef SETDBG
puts("Transaction closed");
#endif
}//if
reqcnt++;
continue;
}
for (x=0;x<=cdrrestlen && !done && cdrrestlen > 1;) {
uc=buf[x];
parmtype=uc;
/*parmtype=(parmtype << 24) >> 24;*/ /* Modified in sun worked in hp */
parmlen = buf[x+1];
/*parmlen =(parmlen << 24) >> 24;*/
x+=2;
switch (parmtype) {
case 4: /* Called party number */
bcd_decode2(parmlen,&buf[x],crap);
tmpcdrptr->BSubscriberNumberLength = (char)parmlen;
strcpy(tmpcdrptr->BSubscriberNumber,crap);
tmpcdrptr->BSubscriberNumber[parmlen] = '\0';
x=x+(parmlen/2);
if (parmlen % 2) x++;
tmpcdrptr->USED_FIELDS |= B_BSubscriberNumber;
#ifdef SETDBG
puts("BNumber");
#endif
check = MyOperation->setValue("BNumber",(char*)&tmpcdrptr->BSubscriberNumber);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 9: /* Calling Partys cataegory */
if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
else tmpcdrptr->ACategory=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_ACategory;
#ifdef SETDBG
puts("ACategory");
#endif
check = MyOperation->setValue("ACategory",(char*)&tmpcdrptr->ACategory);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 10: /* Calling Party Number */
bcd_decode2(parmlen,&buf[x],crap);
tmpcdrptr->ASubscriberNumberLength = (char)parmlen;
strcpy(tmpcdrptr->ASubscriberNumber,crap);
tmpcdrptr->ASubscriberNumber[parmlen] = '\0';
x=x+(parmlen/2);
if (parmlen % 2) x++;
tmpcdrptr->USED_FIELDS |= B_ASubscriberNumber;
#ifdef SETDBG
puts("ANumber");
#endif
check = MyOperation->setValue("ANumber",(char*)&tmpcdrptr->ASubscriberNumber);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 11: /* Redirecting number */
bcd_decode2(parmlen,&buf[x],crap);
strcpy(tmpcdrptr->RedirectingNumber,crap);
x=x+(parmlen/2);
if (parmlen % 2) x++;
tmpcdrptr->USED_FIELDS |= B_RedirectingNumber;
#ifdef SETDBG
puts("RNumber");
#endif
check = MyOperation->setValue("RNumber",(char*)&tmpcdrptr->RedirectingNumber);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 17: /* Called partys category */
if (parmlen != 1) printf("ERROR: Called partys category has wrong length %d\n",parmlen);
else tmpcdrptr->EndOfSelectionInformation=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_EndOfSelectionInformation;
#ifdef SETDBG
puts("EndOfSelInf");
#endif
check = MyOperation->setValue("EndOfSelInf",(char*)&tmpcdrptr->EndOfSelectionInformation);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 18: /* Release reason */
if (parmlen != 1) printf("ERROR: Release reason has wrong length %d\n",parmlen);
else tmpcdrptr->CauseCode=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_CauseCode;
#ifdef SETDBG
puts("CauseCode");
#endif
check = MyOperation->setValue("CauseCode",(char*)&tmpcdrptr->CauseCode);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 19: /* Redirection information */
switch (parmlen) {
case 1:
tmpcdrptr->ReroutingIndicator= (char)buf[x];
tmpcdrptr->USED_FIELDS |= B_ReroutingIndicator;
break;
case 2:
swab(buf+x,buf+x,2);
tmpcdrptr->ReroutingIndicator= buf[x];
tmpcdrptr->USED_FIELDS |= B_ReroutingIndicator;
break;
default :
BaseString::snprintf(msg,sizeof(msg),"ERROR: Redirection information has wrong length %d\n",parmlen);
if ((checkchangelog(log,lognamn))==0)
n2log(log,msg);
break;
#ifdef SETDBG
puts("RI");
#endif
check = MyOperation->setValue("RI",(char*)&tmpcdrptr->ReroutingIndicator);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
}
x+=parmlen;
break;
case 32: /* User to user information */
if (parmlen != 1) printf("ERROR: User to User information has wrong length %d\n",parmlen);
else tmpcdrptr->UserToUserInformation=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_UserToUserInformation;
#ifdef SETDBG
puts("UserToUserInf");
#endif
check = MyOperation->setValue("UserToUserInf",(char*)&tmpcdrptr->UserToUserInformation);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 40: /* Original called number */
bcd_decode2(parmlen,&buf[x],crap);
strcpy(tmpcdrptr->OriginalCalledNumber,crap);
x=x+(parmlen/2);
if (parmlen % 2) x++;
tmpcdrptr->USED_FIELDS |= B_OriginalCalledNumber;
#ifdef SETDBG
puts("ONumber");
#endif
check = MyOperation->setValue("ONumber",(char*)&tmpcdrptr->OriginalCalledNumber);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 42: /* User to user indicator */
if (parmlen != 1) printf("ERROR: User to User indicator has wrong length %d\n",parmlen);
else tmpcdrptr->UserToUserIndicatior=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_UserToUserIndicatior;
#ifdef SETDBG
puts("UserToUserInd");
#endif
check = MyOperation->setValue("UserToUserInd",(char*)&tmpcdrptr->UserToUserIndicatior);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 63: /* Location number */
bcd_decode2(parmlen,&buf[x],crap);
strcpy(tmpcdrptr->LocationCode,crap);
x=x+(parmlen/2);
if (parmlen % 2) x++;
tmpcdrptr->USED_FIELDS |= B_LocationCode;
#ifdef SETDBG
puts("LocationCode");
#endif
check = MyOperation->setValue("LocationCode",(char*)&tmpcdrptr->LocationCode);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 240: /* Calling Partys cataegory */
if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
else tmpcdrptr->NetworkIndicator=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_NetworkIndicator;
#ifdef SETDBG
puts("NIndicator");
#endif
check = MyOperation->setValue("NIndicator",(char*)&tmpcdrptr->NetworkIndicator);
if (check == -1)
error_handler(MyTransaction->getNdbErrorString());
break;
case 241: /* Calling Partys cataegory */
if (parmlen != 1) printf("ERROR: Calling partys category has wrong length %d\n",parmlen);
else tmpcdrptr->TonASubscriberNumber=(char)buf[x];
x+=parmlen;
tmpcdrptr->USED_FIELDS |= B_TonASubscriberNumber;
#ifdef SETDBG
puts("TonANumber");
#endif