-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathptp.cpp
More file actions
3113 lines (2699 loc) · 114 KB
/
ptp.cpp
File metadata and controls
3113 lines (2699 loc) · 114 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
/* CMS REPLACEMENT HISTORY, Element PTP.CPP*/
/* *13 1-OCT-2003 08:57:50 RD2JK "Change silent shutdown"*/
/* *12 25-SEP-2003 12:33:57 RD2JK ""*/
/* *11 15-SEP-2003 14:26:16 RD2JK "Reverted back to CRTL"*/
/* *10 15-SEP-2003 12:11:44 RD2JK "Replaced CRTL file i/o for windows with win32 api calls"*/
/* *9 28-AUG-2003 14:21:20 RD2JK ""*/
/* *8 28-AUG-2003 12:09:34 RD2JK "Fix in putgetdata"*/
/* *7 22-AUG-2003 11:50:25 RD2JK ""*/
/* *6 22-AUG-2003 10:41:10 RD2JK "Fix order for sendsafe"*/
/* *5 18-AUG-2003 09:29:58 RD2JK ""*/
/* *4 1-AUG-2003 11:57:52 RD2JK ""*/
/* *3 29-JUL-2003 10:14:10 RD2JK ""*/
/* *2 28-JUL-2003 16:15:35 RD2JK ""*/
/* *1 25-JUL-2003 15:57:47 RD2JK "POINTTPOINT API SOURCE FILE"*/
/* CMS REPLACEMENT HISTORY, Element PTP.CPP*/
/**************************************************************
* Point to Point API
*
* Written : James Kyburz 13-Feb-2002
* Function : API for sending and receiveing TCP/IP
* stream data.
* includes TCP/IP server ( gatekeeper )
* Version : 1.0
* Modifications :
*
*************************************************************/
/******************************************
* zTime *
* internal routine for setting time as *
* year-month-day_hour:minutes:seconds *
* returns privateVoid *
******************************************/
privateVoid zTime(
char *timeBuf, /* storage */
const char maxSize ) /* Size of storage */
{
TM *t;
time_t sec;
( void ) time( &sec );
t = localtime( &sec );
/* Assign the formatted time string to the callee */
strftime( timeBuf, maxSize - 0x01, "%Y-%m-%d_%H:%M:%S", t );
}
#if W_I_N && !PTP_IMANAGER
/******************************************
* zCleanupWinSocks *
* Cleanup windows sockets *
* returns void *
******************************************/
static void zCleanupWinSock( )
{
WSACleanup();
}
#endif
/******************************************
* zPTPLog *
* Write information to disk or console *
* Information can be errors or data you *
* always want to log. *
* returns privateVoid *
******************************************/
/* Do not create function if calling from COM as will
call a COM component there to log */
#if !PTP_IMANAGER
privateVoid zPTPLog(
FILE *fp, /* file or console */
const char outLevel, /* level of output */
const char *funcName,/* callee function */
const int type, /* type of information i.e error/informational */
const int code, /* code to be used for error code if needed */
const char *fmt, /* format argument for vprintf routines */
... ) /* varied arguments */
{
va_list ap; /* called arguments */
char timeBuf[0x50];
char message[PTP_MAX_LOG_MESSAGE_SIZE];
char *logLevel[] = { "-E-",
"-I-",
"-W-",
"-D-",
"-S-",
"" };
/* if level of output is 0 or pointer (fp) is not set log nothing.
if the level of output is not 3 (all output).
if level is 1 only report errors,
if level is 2 only report informational + errors,
if level is 3 report everything.
*/
if ( !outLevel || !fp )
{
return;
}
if ( outLevel == 0x01 && type != 0x00 )
{
return;
}
if ( outLevel == 0x02 && type != 0x00 && type != 0x01 )
{
return;
}
va_start( ap, fmt );
/* check if type is normal text with no type (plain text)*/
if ( type != PTP_LOG_LEVEL_NOTHING )
{
/* Timestamp the log entry */
zTime( timeBuf, sizeof( timeBuf ) );
fprintf( fp, "\n%s", timeBuf );
fprintf( fp, " - " );
message[vsprintf( message, fmt, ap )] = '\x00';
/* write message to log file */
fprintf( fp, "%s%s%s, system msg:%s, errno:%d\n", funcName,
logLevel[type], message, strerror( PTP_ERROR ), PTP_ERROR );
}
else
{
/* write message to log file */
vfprintf( fp, fmt, ap );
}
/* flush file if the pointer is not pointing to console device */
if( fp != PTP_CONSOLE && fp )
{
fflush( fp );
}
va_end( ap );
}
#endif
/******************************************
* zCleanup *
* internal routine for cleaning up a *
* socket device using close and shutdown *
* returns privateVoid *
******************************************/
privateVoid zCleanup(
FILE *fp, /* file or console for logging */
const char outLevel, /* level of output */
const char shut, /* 1=perform shutdown() */
PTP_SOCKET *sockFd, /* connected socket channel */
const char gatekeeper ) /* is the socket the gatekeeper in PTP_Server */
{
/* if no connection (socket descriptor is 0) then do nothing */
if( !*sockFd )
{
return;
}
/* does the calle want to shutdown the socket ? */
if (shut)
{
/* See if connection already shutdown by checking PTP_ERROR
do not shutdown connection if error is anything
this can happen when the connection dies before call to
this function
*/
if ( PTP_ERROR != PTPECONNRESET )
{
/* shutdown both directions of the connection */
if ( shutdown( *sockFd, PTP_SD_BOTH ) < 0x00 )
{
zPTPLog(
fp, outLevel, "zCleanup",
PTP_LOG_LEVEL_INFORMATION, 0x00,
"%s %d\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"Failed to shutdown connection on channel", *sockFd,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__
);
}
}
}
/* close the socket (freeing the device) */
#if W_I_N
if ( closesocket( *sockFd ) < 0x00 )
#else
if ( close( *sockFd ) < 0x00 )
#endif
{
zPTPLog(
fp, outLevel, "zCleanup",
PTP_LOG_LEVEL_INFORMATION, 0x00,
"%s %d\n/Source: %s V%s , Compiled: %s %s Line: %d\n",
"Failed to close connection", *sockFd,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__
);
}
/* reset the channel number to 0 */
*sockFd = 0x00;
}
/******************************************
* zCreateHeader *
* internal routine for assigning the *
* header values used by both *
* client/server to send header to *
* gatekeeper *
* returns PTP_EXIT_SUCCESS *
* PTP_EXIT_FAILURE *
******************************************/
privateRtn zCreateHeader(
union ptpHeader *details, /* the header block */
const long buffSize, /* to assign to data size in header */
const uchar funk, /* client or server type */
const long encrypt, /* 1=encryted data will be sent */
const long language ) /* language of client/server data */
{
char fmt[10];
double max;
/* Check the buffer size to send does not exceed the character
representation in bytes in the header */
if ( buffSize >
( max = (pow( 10, (sizeof( details->u.cSize ) - 0x01) ) - 0x01) ) )
{
zPTPLog( PTP_CONSOLE, 0x03, "zCreateHeader", PTP_LOG_LEVEL_ERROR,
0x00,
"%s %d %s %d %s %d\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"Message max size exceeded, max =",
( int ) max,
",buffer size=",
buffSize,
",function=",
funk,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
return PTP_EXIT_FAILURE;
}
sprintf( fmt, "%%%d.%dd", sizeof ( details->u.cSize ) - 0x01,
sizeof ( details->u.cSize ) - 0x01);
/* size in ascii bytes */
sprintf( details->u.cSize, fmt, buffSize );
/* assign all header values */
details->u.function = funk;
details->u.ptpIdentifier = sizeof ( details->cHeader );
details->u.encrypt = ( uchar ) encrypt;
details->u.language = ( uchar ) language;
return PTP_EXIT_SUCCESS;
}
/******************************************
* zAddQ *
* internal routine for creating a link *
* in a linked list and setting default *
* values *
* note - only called from PTP_Server *
* returns a pointer to the new link *
******************************************/
PTP_MESSAGE *zAddQ(
PTP_MESSAGE **lastQ, /* last link in list */
PTP_SOCKET channel ) /* messages connected socket channel */
{
PTP_MESSAGE *prevQ;
PTP_MESSAGE *que; /* pointer used to create new link */
que = ( PTP_MESSAGE * ) malloc( sizeof( PTP_MESSAGE ) );
if ( !que ) /* no space available ? */
{
return NULL;
}
/* if there was a link before make it to point to the new
link (as it is now the last link), and set the last links
prevQ to it.
*/
if ( *lastQ )
{
prevQ = *lastQ;
prevQ->nextQ = que;
que->prevQ = prevQ;
}
/* no previous link */
else
{
que->prevQ = NULL;
}
/* set the last link the newly created link */
*lastQ = que;
/* set default values */
que->nextQ = NULL;
que->pairQ = NULL;
que->type = PTP_NONE;
que->status = PTP_IDLE;
que->bytesReceived = 0x00;
que->deleteFlag = 0x00;
que->validated = 0x00;
que->processedQuestion = 0x00;
que->noSize = 0x00;
que->channel = channel;
return que;
}
/******************************************
* zDelQ *
* internal routine for deleting a link *
* in a linked list *
* note - only called from PTP_Server *
* resets last link if needed *
* returns privateVoid *
******************************************/
privateVoid zDelQ(
PTP_MESSAGE **lastQ,
PTP_MESSAGE *que )
{
if ( !que )
{
return;
}
if ( que->prevQ )
{
if ( que->nextQ )
{
que->prevQ->nextQ = que->nextQ;
que->nextQ->prevQ = que->prevQ;
}
else
{
*lastQ = que->prevQ;
que->prevQ->nextQ = NULL;
}
}
else
{
if ( que->nextQ )
{
que->nextQ->prevQ = NULL;
}
else
{
*lastQ = NULL;
}
}
if ( que->pairQ )
{
que->pairQ->pairQ = NULL;
que->pairQ = NULL;
}
free( que );
}
/******************************************
* zGetParam *
* gets the value of a keyword contained *
* in a file must use format as example *
* example line :- *
* TCP/IP port '1234' *
* TCP/IP port is the keyword and 1234 is *
* the return value *
* note: only called from zConfig *
* returns PTP_EXIT_SUCCESS *
* PTP_EXIT_FAILURE *
******************************************/
privateRtn zGetParam(
FILE *stream, /* point to log output */
FILE *fp, /* config file pointer */
char *keyword, /* keyword */
char *result, /* result */
const uchar length ) /* max length 1 - 255 of result */
{
char inparam[PTP_PARAM_LINE + 0x01];
char *pek = inparam, *pek2;
/* keep reading a line until ! is not the first
character (comment character)
*/
while( fgets( inparam, PTP_PARAM_LINE, fp) != NULL)
{
if ( ( *pek + 0x00 ) != '!' )
{
break;
}
}
/* if the keyword does not exist return error */
if ( strstr( pek, keyword ) != NULL )
{
/* find ' in line */
pek = strstr( pek, "'" );
if ( pek )
{
/* find next ' in line */
pek2 = strstr( pek + 0x01, "'" );
if ( pek2 )
{
*( pek2 + 0x00 ) = '\0';
/* copy value inbetween two 's */
strcpy( keyword, pek + 0x01 );
return PTP_EXIT_SUCCESS;
}
}
}
zPTPLog( stream, 0x03, "zGetParam", PTP_LOG_LEVEL_ERROR,
0x00,
"%s %s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"SERVER config - invalid keyword",
keyword,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
return PTP_EXIT_FAILURE;
}
/******************************************
* zConfig *
* opens a config file calling zGetParam *
* with each given argument. The result *
* of the found keyword in the config *
* file is then written to the same *
* address. *
* Because this routine is called during *
* the startup of PTP_Server a temporary *
* log file PTP_STARTUP_LOG is opened to *
* record any failures, on success the *
* file is deleted. *
* *
* note: only called from PTP_Server and *
* PTP_Shutdown *
* returns PTP_EXIT_SUCCESS *
* PTP_EXIT_FAILURE *
******************************************/
privateRtn zConfig(
const char argc, /* number of arguments */
const char *filename, /* filename of config file */
... ) /* ... varied argument declaration */
{
va_list ap; /* varied arguments */
FILE *fp; /* config file */
FILE *stream; /* log output */
char *keyword; /* keyword
note the result will be written to the
same address
*/
char tmpFil[0xFF]; /* Temporary filename to report failures */
time_t ticks;
long retCode = PTP_INTERFACE_SUCCESS;
register i; /* register variable to loop through arguments */
ticks = time( ( time_t * ) NULL );
/* create a filename that is unique */
sprintf( tmpFil, "%s%ld%08o.TXT\0", PTP_STARTUP_LOG,
( long ) ticks, getpid() );
/* open temporary log file */
stream = fopen( tmpFil, "a+" );
if ( NULL != filename && NULL != stream )
{
fprintf( stream, "%s= %s", "Config file", filename );
}
/* do no processing if filename or config file not given */
if (NULL == filename)
{
zPTPLog( stream, 0x03, "zConfig", PTP_LOG_LEVEL_ERROR,
0x00,
"%s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"SERVER- no config file found in call to zConfig"
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
return PTP_EXIT_FAILURE;
}
/* open the config file for reading checking for any errors */
if( !(fp = fopen( filename, "r" )) )
{
zPTPLog( stream, 0x03, "zConfig", PTP_LOG_LEVEL_ERROR,
0x00,
"%s %s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"SERVER - failed to open configuration file",
filename,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
if ( stream )
{
fclose( stream );
return PTP_EXIT_FAILURE;
}
}
/* start variable argument processing */
va_start( ap, filename );
/* for argc times call GetParam with keyword from arg */
for( i=0x00; i<argc; i++ )
{
/* get called argument */
keyword = ( char * ) va_arg( ap, char * );
if ( !keyword ) continue;
/* if keyword not found report error */
if( (zGetParam( stream, fp, keyword, keyword,
PTP_PARAM_LINE )) == PTP_EXIT_FAILURE )
{
retCode = PTP_INTERFACE_FAILURE;
break;
}
}
/* close log file */
if ( fp )
{
fclose( fp );
}
/* end variable argument processing */
va_end( ap );
/* close temporary log file and remove if no errors occured */
if ( stream )
{
fclose( stream );
if ( retCode == PTP_INTERFACE_SUCCESS )
{
( void ) remove( tmpFil );
}
}
return retCode ==
PTP_INTERFACE_SUCCESS ? PTP_EXIT_SUCCESS : PTP_EXIT_FAILURE;
}
/******************************************
* zGetServAddr *
* gets the ip address of a host using *
* DNS lookup *
* note: only called from PTP_Server and *
* zConnect, will hang if the host *
* does not exists in DSN as the lookup *
* will go out to the internet and will *
* timeout after a few minutes. *
* returns PTP_EXIT_SUCCESS *
* PTP_EXIT_FAILURE *
******************************************/
privateRtn zGetServAddr(
void *addrptr,
const char * buf )
{
INADDR val; /* sock in structure */
HOSTENT *host; /* host entity structure */
val.s_addr = inet_addr( buf ); /* use internet format ip address */
/* if the host name was an ip address return success */
if ( ( int ) val.s_addr != ( int ) INADDR_NONE )
{
memcpy( addrptr, &val, sizeof( INADDR ) );
return PTP_EXIT_SUCCESS;
}
/* DNS lookup where the host must exist in the database */
if ( host = gethostbyname( buf ) )
{
memcpy( addrptr, host->h_addr, sizeof( INADDR ) );
}
else
{
/* host not found */
return PTP_EXIT_FAILURE;
}
return PTP_EXIT_SUCCESS;
}
/******************************************
* zConnect *
* creates a socket *
* and attempts to connect to a IP server *
* note: only called from PTP_Connect *
* if the connect fails the failover host *
* will be used *
* retCode PTP_INTERFACE_SUCESS *
* PTP_INTERFACE_FAILURE *
* PTP_INTERFACE_TIMEOUT *
* returns PTP_EXIT_SUCCESS *
* PTP_EXIT_FAILURE *
******************************************/
privateRtn zConnect(
long *retCode, /* address of return value */
const char *host, /* host to connect to */
const long *port, /* port to connect to */
const char *failoverHost, /* failover host to connect to */
const long *failoverPort, /* failover port to connect to */
PTP_SOCKET *sockFd, /* channel that will be connected */
const long *timeout, /* timeout */
long *failed ) /* failover count */
{
ISOCKET clientAddr; /* client address used for connection */
PTP_ADDRLEN clientAddrLen = sizeof ( clientAddr );
register int rounds;
ulong block = 0x01;
long ackSize = 0x01;
fd_set writeFds;
struct timeval tv,*pTv = NULL;
char ack[2];
const char *currentHost;
const long *currentPort;
/* if a socket exists return success as already
connected
*/
if ( getpeername( *sockFd,
(struct sockaddr *) &clientAddr,
&clientAddrLen ) )
{
*retCode = PTP_INTERFACE_FAILURE;
for ( rounds = 0x00; rounds < 0x02; rounds++ )
{
/* An error occured so cleanup socket if exists (*sockFd!=0)*/
if ( (*sockFd != 0x00 ) && (*retCode != PTP_INTERFACE_SUCCESS) )
{
zCleanup ( PTP_CONSOLE, 0x03, 0x00, sockFd,
0x00 );
}
currentHost = *failed == 0x00 ? host : failoverHost;
currentPort = *failed == 0x00 ? port : failoverPort;
/* if end point information not supplied go back to for check
making sure to set failed back
*/
if ( !currentHost )
{
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
if ( !currentPort )
{
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
if ( !*currentHost )
{
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
if ( !*currentPort )
{
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
*retCode = PTP_INTERFACE_SUCCESS;
clientAddr.sin_family = AF_INET;
clientAddr.sin_port = htons( ( ushort ) *currentPort );
/* get ip address of host return error if not found */
if( zGetServAddr( &clientAddr.sin_addr, currentHost ) )
{
zPTPLog( PTP_CONSOLE, 0x03, "zConnect",
PTP_LOG_LEVEL_ERROR, 0x00,
"%s %s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"DSN lookup failed for host",
currentHost,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
*retCode = PTP_INTERFACE_FAILURE;
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
/* create socket for outbound connection report any errors */
if ( (*sockFd = socket( AF_INET, SOCK_STREAM, 0x00)) <= 0x00 )
{
zPTPLog( PTP_CONSOLE, 0x03, "zConnect",
PTP_LOG_LEVEL_ERROR, 0x00,
"%s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"Outbound socket creation failed",
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
*retCode = PTP_INTERFACE_FAILURE;
return PTP_EXIT_FAILURE;
}
/* set socket to nonblocking so as to not wait for the connect
call to complete. the checking is done in the select statement
to check if the socket is writeable ( here timeout applies )
and by using getpeername to check if the connect has completed
*/
PTP_IOCTL( *sockFd, FIONBIO, ( ulong * ) &block );
/*
* Connect socket 1 to addr.
*/
( void ) connect( *sockFd, (struct sockaddr *)&clientAddr,
sizeof( clientAddr ) );
/* If no timeout exists or is less than 3 set the wait time in
select to max 3 seconds otherwise use the timeout sent */
tv.tv_sec = (*timeout < 0x02 && *timeout ) ? *timeout : 0x03;
tv.tv_usec = 0x00;
pTv = &tv;
FD_ZERO( &writeFds );
FD_SET( *sockFd, &writeFds );
if ( (select ( *sockFd + 0x01,
( fd_set *) NULL,
&writeFds,
( fd_set *) NULL,
pTv )) <= 0 )
{
zPTPLog(
PTP_CONSOLE, 0x03, "zConnect",
PTP_LOG_LEVEL_ERROR, 0x00,
"%s %s:%d\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"Connection timed out on",
currentHost,
*currentPort,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__
);
*retCode = PTP_INTERFACE_TIMEOUT;
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
/* check if connected */
else if ( getpeername( *sockFd,
(struct sockaddr *) &clientAddr,
&clientAddrLen ) )
{
zPTPLog(
PTP_CONSOLE, 0x03, "zConnect",
PTP_LOG_LEVEL_ERROR, 0x00,
"%s %s:%d\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"Connection to server failed on",
currentHost,
*currentPort,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__
);
*retCode = PTP_INTERFACE_FAILURE;
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
/* Turn off non blocking socket */
block = 0x00;
PTP_IOCTL( *sockFd, FIONBIO, ( ulong * ) &block );
/* Connected was succesfull, now get ack from server, this is
done after memory and security checks
*/
if ( *retCode == PTP_INTERFACE_SUCCESS )
{
/* If receive of ack failed return error (at end of function)
do not cleanup socket on error as this is done in this
function */
if ( zPutGetStream( retCode, PTP_CONSOLE, sockFd, ack,
&ackSize, timeout,
0x00, PTP_RECEIVE, "zConnect" ) )
{
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
/* receive worked so now check contents of ack received from
gatekeeper if not Y then return error
*/
else if ( ack[0] != PTP_ACK )
{
*retCode = PTP_INTERFACE_FAILURE;
zPTPLog(
PTP_CONSOLE, 0x03, "zConnect", PTP_LOG_LEVEL_INFORMATION,
0x00,
"%s %s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
"failed to get connect ack from ",
currentHost,
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__
);
*failed = *failed == 0x00 ? 0x01 : 0x00;
continue;
}
break;
/* now we are connected ! */
} /* end if */
} /* end for */
} /* getpeername != 0x00 */
/* A connection already exists so do nothing */
else /* getpeername == 0x00 */
{
*retCode = PTP_INTERFACE_SUCCESS;
}
/* if an error occured so cleanup socket if exists (*sockFd!=0)*/
if ( (*sockFd != 0x00 ) && (*retCode != PTP_INTERFACE_SUCCESS) )
{
zCleanup ( PTP_CONSOLE, 0x03, 0x00, sockFd, 0x00 );
}
return *retCode ==
PTP_INTERFACE_SUCCESS ? PTP_EXIT_SUCCESS : PTP_EXIT_FAILURE;
}
/******************************************
* zPutGetStream *
* reads or writes to/from a connected *
* socket. *
* This routines calls zPutGetData until *
* all bytes are either written or read *
* retCode PTP_INTERFACE_SUCESS *
* PTP_INTERFACE_FAILURE *
* PTP_INTERFACE_TIMEOUT *
* returns PTP_EXIT_SUCCESS *
* PTP_EXIT_FAILURE *
******************************************/
privateRtn zPutGetStream(
long *retCode, /* return value */
FILE *fp, /* where to log */
PTP_SOCKET *sockFd, /* connected socket channel */
char *buffer, /* data to be sent/received */
long *buffSize, /* buffer size */
const long *timeout, /* timeout */
const long clearCallOnError, /* 1=call zCleanup on errors, 0=do not */
const SEND_TYPE sType, /* PTP_SEND / PTP_RECEIVE ? */
const char *caller ) /* calling function */
{
char *sendReceive[] = { "zPutGetStream/send",
"zPutGetStream/recv" };
struct timeval tv;
long bytes, totalBytes,
localBuffSize = *buffSize;
/* clear error */
PTP_SET_ERROR( 0x00 );
/* if no connection return error */
if( *sockFd <= 0x00 )
{
zPTPLog( fp, 0x03, sendReceive[sType],
PTP_LOG_LEVEL_ERROR, 0x00,
"[%s]%s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
caller,
"invalid socket descriptor ",
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
*retCode = PTP_INTERFACE_FAILURE;
return PTP_EXIT_FAILURE;
}
/* if buffSize = 0 then return error */
if( !*buffSize)
{
zPTPLog( fp, 0x03, sendReceive[sType],
PTP_LOG_LEVEL_ERROR, 0x00,
"[%s]%s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
caller,
"invalid size",
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
if ( clearCallOnError )
{
zCleanup ( fp, 0x03, 0x01, sockFd, 0x00 );
}
*retCode = PTP_INTERFACE_FAILURE;
return PTP_EXIT_FAILURE;
}
tv.tv_sec = *timeout;
tv.tv_usec = 0x00;
for( totalBytes = 0x00, bytes = 0x01;
localBuffSize && bytes;
totalBytes+= bytes, buffer+=bytes )
{
bytes = zPutGetData( retCode,
fp,
sockFd,
buffer,
&localBuffSize,
tv.tv_sec ? &tv : NULL,
clearCallOnError,
sType,
caller );
/* once data has started to be transmitted ignore the senders
timeout and use 2 seconds, this is so if there are any
delays in the middle of communication the function will
return instead of hanging
*/
tv.tv_sec = 0x02;
}
if ( sType == PTP_RECEIVE )
{
*buffer = '\x00';
*buffSize = totalBytes;
}
return *retCode == PTP_INTERFACE_SUCCESS ?
PTP_EXIT_SUCCESS : PTP_EXIT_FAILURE;
}
/******************************************
* zPutGetData *
* reads or writes to/from a connected *
* socket. *
* retCode PTP_INTERFACE_SUCESS *
* PTP_INTERFACE_FAILURE *
* PTP_INTERFACE_TIMEOUT *
* returns bytes written or read *
* PTP_EXIT_FAILURE *
******************************************/
long zPutGetData(
long *retCode, /* return value */
FILE *fp, /* where to log */
PTP_SOCKET *sockFd, /* connected socket channel */
char *buffer, /* data to be sent/received */
long *buffSize, /* buffer size */
const struct timeval *pTv, /* timeout */
const long clearCallOnError, /* 1=call zCleanup on errors, 0=do not */
const SEND_TYPE sType, /* PTP_SEND / PTP_RECEIVE ? */
const char *caller ) /* calling function */
{
char *sendReceive[] = { "zPutGetStream/send",
"zPutGetStream/recv" };
int bytes = 0x00, rc;
int sendReceiveSize;
fd_set readFds, writeFds, rSet;
ISOCKET clientAddr; /* client address used for connection */
PTP_ADDRLEN clientAddrLen = sizeof ( clientAddr );
/* get connection details, if not connected return */
if ( getpeername( *sockFd,
(struct sockaddr *) &clientAddr,
&clientAddrLen ) )
{
zPTPLog( fp, 0x03, sendReceive[sType],
PTP_LOG_LEVEL_ERROR, 0x00,
"[%s]%s\nSource: %s V%s , Compiled: %s %s Line: %d\n",
caller,
"invalid socket descriptor ",
__FILE__, PTP_VER, __DATE__, __TIME__, __LINE__ );
*retCode = PTP_INTERFACE_FAILURE;
if ( clearCallOnError )
{
zCleanup ( fp, 0x03, 0x01, sockFd, 0x00 );
}
return 0x00; /* no bytes written or read */
}