0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <string.h>
0038 #include <errno.h>
0039 #include <sys/time.h>
0040
0041 #include "tsdef.h"
0042 #include "tsconf.h"
0043 #include "tsext.h"
0044
0045 #if (client==PC)
0046 #define WSOCKETS_DLL
0047 #include <ctype.h>
0048 #include <limits.h>
0049 #include <sys/socket.h>
0050
0051 #include <netinet\in.h>
0052
0053 #include <netdb.h>
0054
0055 #include <sock_err.h>
0056 #include <windows.h>
0057 #include <toolhelp.h>
0058
0059 #elif (client==MAC)
0060 #include <CTBUtilities.h>
0061 #include <CommResources.h>
0062 #include <Connections.h>
0063 #include <unix.h>
0064
0065 #elif (client == INTEL)
0066 #include <rmx_c.h>
0067 #include <rmx_err.h>
0068 #include <rmx_def.h>
0069 #include <sys/types.h>
0070 #include <sys/socket.h>
0071 #include <netinet/in.h>
0072 #include <arpa/inet.h>
0073 #include <netdb.h>
0074 #include <string.h>
0075 #include <strings.h>
0076
0077 #else
0078
0079 #include <netdb.h>
0080
0081 #include <sys/types.h>
0082 #include <sys/socket.h>
0083
0084 #include <netinet/in.h>
0085
0086 #endif
0087
0088 #include "tshote.h"
0089 #include "tsmes.h"
0090 #include "tstype.h"
0091 #include "functs.h"
0092
0093 void *Memset (void *, int, Longint);
0094 extern void *MemAlloc (Longint);
0095 extern void FreeMem (void *);
0096
0097 #if (client == INTEL)
0098 #define MAX_TRY 4
0099 #endif
0100
0101 int compress_on;
0102 int Socket_save;
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121 Longint FAR_PASCAL
0122 Emission (
0123 #if (client == MAC)
0124 ConnHandle nsock,
0125 #else
0126 int nsock,
0127 #endif
0128 char *parametre,
0129 Longint longbuffer,
0130 Longint code_param)
0131
0132 {
0133 #if (client ==MAC)
0134 Longint longenvoye, i;
0135 OSErr anErr;
0136
0137 i = 0;
0138 while (i < longbuffer)
0139 {
0140 longenvoye = longbuffer - i;
0141
0142 (void) CMIdle (nsock);
0143 if ((anErr = CMWrite (nsock, parametre + i, &longenvoye, cmData, 0, NULL, 1, 0))
0144 != noErr)
0145 {
0146 Fin_Communication (nsock);
0147 return (Gestion_Erreur (erreur_emission, code_param));
0148 }
0149 i = i + longenvoye;
0150 }
0151 #elif (client==PC)
0152
0153 short int longbuff;
0154 Longint i;
0155
0156 i = 0;
0157 while (longbuffer > 0)
0158 {
0159 if (longbuffer >= (Longint) SHRT_MAX)
0160 {
0161 longbuff = SHRT_MAX;
0162 longbuffer = longbuffer - (Longint) SHRT_MAX;
0163 }
0164 else
0165 {
0166 longbuff = (short int) longbuffer;
0167 longbuffer = 0;
0168 }
0169 if (send (nsock, parametre + i, longbuff, 0) != longbuff)
0170 {
0171 Fin_Communication (nsock);
0172 return (Gestion_Erreur (erreur_emission, code_param));
0173 }
0174 i = i + longbuff;
0175 }
0176 #elif (client == INTEL)
0177 Longint i, j;
0178 int max_try;
0179
0180 i = 0;
0181 while (i < longbuffer)
0182 {
0183 max_try = MAX_TRY;
0184 do
0185 {
0186 j = send (nsock, parametre + i, longbuffer - i, 0);
0187 if (j == -1)
0188 {
0189 max_try--;
0190 if ((errno != EWOULDBLOCK) || (max_try < 0))
0191 {
0192 printf ("TSLIB : send errno = %d \n", errno);
0193 Fin_Communication (nsock);
0194 return (Gestion_Erreur (erreur_emission, code_param));
0195 }
0196 }
0197 else
0198 {
0199 i = i + j;
0200 break;
0201 }
0202 }
0203 while (max_try >= 0);
0204 }
0205 #else
0206 if (send (nsock, parametre, longbuffer, 0) != longbuffer)
0207 {
0208 Fin_Communication (nsock);
0209 return (Gestion_Erreur (erreur_emission, code_param));
0210 }
0211 #endif
0212 return ok;
0213 }
0214
0215 #if ((client!=PC) && (client !=MAC))
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230 Longint FAR_PASCAL
0231 Fin_Emission (int nsock)
0232 {
0233 if (shutdown (nsock, 1) == -1)
0234 {
0235 Fin_Communication (nsock);
0236 return erreur_fin_emission;
0237 }
0238
0239 return ok;
0240 }
0241 #endif
0242
0243
0244 #if (client == PC)
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 Longint FAR_PASCAL
0264 ReceptionHuge (
0265 int nsock,
0266 char HUGE * parametre,
0267 Longint longbuffer,
0268 Longint code_param)
0269 {
0270 Longint i;
0271 int j;
0272 short longbuff;
0273 char far *para;
0274 short nbytes = 16384;
0275
0276 if (longbuffer > 0)
0277 {
0278 i = 0;
0279 para = (char *) MemAlloc (nbytes);
0280 while (i < longbuffer)
0281 {
0282
0283 if ((longbuffer - i) > nbytes)
0284 longbuff = nbytes;
0285 else
0286 longbuff = (short int) (longbuffer - i);
0287
0288 j = recv (nsock, para, longbuff, 0);
0289 if ((j == -1) || (j == 0))
0290 {
0291 Fin_Communication (nsock);
0292 return (Gestion_Erreur (erreur_reception, code_param));
0293 }
0294 hmemcpy (parametre + i, para, longbuff);
0295 i += (Longint) j;
0296 }
0297 FreeMem (para);
0298 }
0299
0300 return ok;
0301 }
0302 #endif
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320 Longint FAR_PASCAL
0321 Reception (
0322 #if (client == MAC)
0323 ConnHandle nsock,
0324 #else
0325 int nsock,
0326 #endif
0327 char *parametre,
0328 Longint longbuffer,
0329 Longint code_param)
0330 {
0331 #if (client ==MAC)
0332 Longint longenvoye;
0333 Longint i;
0334 CMFlags cmflags;
0335 OSErr anErr;
0336 CMBufferSizes datasizes;
0337 CMStatFlags statflags;
0338 unsigned Longint startticks;
0339
0340 if (longbuffer > 0)
0341 {
0342 i = 0;
0343 while (i < longbuffer)
0344 {
0345 longenvoye = longbuffer - i;
0346
0347 startticks = TickCount ();
0348 do
0349 {
0350 (void) CMIdle (nsock);
0351 anErr = CMStatus (nsock, datasizes, &statflags);
0352 }
0353 while ((anErr == noErr) && (datasizes[cmDataIn] == 0) &&
0354 (((unsigned Longint) TickCount () - startticks) < delai));
0355
0356 cmflags = 0;
0357 if ((anErr == noErr) && (datasizes[cmDataIn] != 0))
0358 {
0359 anErr = CMRead (nsock, parametre + i, &longenvoye, cmData, 0, NULL, 1, &cmflags);
0360 if (anErr != noErr)
0361 {
0362 Fin_Communication (nsock);
0363 return (Gestion_Erreur (erreur_reception, code_param));
0364 }
0365 else
0366 i = i + longenvoye;
0367 }
0368 else
0369 {
0370 Fin_Communication (nsock);
0371 return (Gestion_Erreur (erreur_reception, code_param));
0372 }
0373 }
0374 }
0375
0376 #elif (client == PC)
0377 Longint i;
0378 int j;
0379 short longbuff;
0380
0381 if (longbuffer > 0)
0382 {
0383 i = 0;
0384 while (i < longbuffer)
0385 {
0386
0387 if ((longbuffer - i) > (Longint) SHRT_MAX)
0388 longbuff = SHRT_MAX;
0389 else
0390 longbuff = (short int) (longbuffer - i);
0391
0392 j = recv (nsock, (parametre + i), longbuff, 0);
0393 if ((j == -1) || (j == 0))
0394 {
0395 Fin_Communication (nsock);
0396 return (Gestion_Erreur (erreur_reception, code_param));
0397 }
0398 i += (Longint) j;
0399 }
0400 }
0401
0402 #elif (client == INTEL)
0403 Longint i, j;
0404 int max_try;
0405 if (longbuffer > 0)
0406 {
0407 i = 0;
0408 while (i < longbuffer)
0409 {
0410 max_try = MAX_TRY;
0411 do
0412 {
0413 j = recv (nsock, parametre + i, longbuffer - i, 0);
0414 if (j == -1)
0415 {
0416 max_try--;
0417 if ((max_try < 0) || (errno != EWOULDBLOCK))
0418 {
0419 printf ("TSLIB : receive errno = %d \n", errno);
0420 Fin_Communication (nsock);
0421 return (Gestion_Erreur (erreur_reception, code_param));
0422 }
0423 }
0424 else
0425 {
0426 i = i + j;
0427 break;
0428 }
0429 }
0430 while (max_try >= 0);
0431 }
0432 }
0433 #else
0434 Longint i, j;
0435 if (longbuffer > 0)
0436 {
0437 i = 0;
0438 while (i < longbuffer)
0439 {
0440 again:
0441 j = recv (nsock, parametre + i, longbuffer - i, 0);
0442 if ((j == -1) || (j == 0))
0443 {
0444 if (j < 0)
0445 printf ("receive errno = %d \n", errno);
0446 if (errno == EINTR)
0447 {
0448 errno = 0;
0449 goto again;
0450 }
0451 Fin_Communication (nsock);
0452 return (erreur_reception);
0453 }
0454 i += j;
0455 }
0456 }
0457 #endif
0458 return ok;
0459 }
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476 #if (client != MAC)
0477
0478 Longint FAR_PASCAL
0479 Debut_Communication (int *NumSocket, int ind_serv)
0480
0481
0482
0483 {
0484 int LongAdr;
0485 Longint Resultat;
0486 char CodeClient;
0487
0488 struct hostent *Serveur = NULL;
0489 struct servent *Service;
0490
0491 struct sockaddr_in AdrSockDistante;
0492
0493 short nb_tent = 0, port;
0494 int one = 1;
0495 char *tslib_comp, *tslib_port;
0496 struct linger l_inger;
0497
0498 char hostpart[256] = {0};
0499 char portpart[256] = {0};
0500 char *host;
0501
0502 if (ind_serv == 1)
0503 {
0504 host = getenv ("TSLIB_SERVER");
0505 if (host)
0506 {
0507 sscanf (host, "%[^:]:%s", hostpart, portpart);
0508 if (strlen (portpart) == 0)
0509 strcpy (portpart, "5880");
0510
0511
0512 }
0513 else
0514 {
0515 strcpy (hostpart, hote2);
0516 strcpy (portpart, "5880");
0517 }
0518 }
0519 else if (ind_serv == 0)
0520 {
0521 host = getenv ("TSLIB_SERVER_IR");
0522 if (host)
0523 {
0524 sscanf (host, "%[^:]:%s", hostpart, portpart);
0525 if (strlen (portpart) == 0)
0526 strcpy (portpart, "5880");
0527
0528
0529 }
0530 else
0531 {
0532 strcpy (hostpart, hote1);
0533 strcpy (portpart, "5880");
0534 }
0535 }
0536
0537 compress_on = 0;
0538 tslib_comp = getenv ("TSLIB_COMPRESS");
0539 if (tslib_comp)
0540 if (!strcmp (tslib_comp, "TRUE"))
0541 compress_on = 1;
0542
0543 Serveur = gethostbyname (hostpart);
0544
0545 if (Serveur == NULL)
0546 {
0547 LongAdr = inet_addr (hostpart);
0548 if (LongAdr != 0xffffffff)
0549 Serveur = gethostbyaddr ((void *) &LongAdr,
0550 (int) sizeof (LongAdr), AF_INET);
0551 }
0552 if (Serveur == NULL)
0553 {
0554 printf ("Erreur : %s inconnu\n", hostpart);
0555 return (erreur_hote);
0556 }
0557
0558 Resultat = ok;
0559
0560 tslib_port = getenv ("TSLIB_PORT");
0561 if (tslib_port)
0562 {
0563 sscanf (tslib_port, "%s", portpart);
0564 printf (" Warning !! Connecting to Tslib port : %s \n", portpart);
0565 }
0566
0567 try_again:
0568 *NumSocket = socket (AF_INET, SOCK_STREAM, 0);
0569 if (*NumSocket == -1)
0570 return (erreur_socket);
0571
0572 AdrSockDistante.sin_port = htons ((short) atoi (portpart));
0573 AdrSockDistante.sin_family = AF_INET;
0574 memcpy (&AdrSockDistante.sin_addr, Serveur->h_addr_list[0],
0575 Serveur->h_length);
0576
0577 if (connect (*NumSocket, (struct sockaddr *) &AdrSockDistante,
0578 sizeof (AdrSockDistante)) == -1)
0579 {
0580 #if (client == INTEL)
0581 shutdown (*NumSocket, 2);
0582 #else
0583 close (*NumSocket);
0584 #endif
0585 nb_tent++;
0586 if (nb_tent > 4)
0587 return (erreur_connexion);
0588 sleep (1);
0589 goto try_again;
0590 }
0591
0592 Socket_save = *NumSocket;
0593 setsockopt (*NumSocket, SOL_SOCKET, SO_KEEPALIVE, (char *) &one,
0594 (int) sizeof (one));
0595
0596
0597
0598
0599
0600
0601
0602
0603 CodeClient = client;
0604 if ((Resultat = Emission (*NumSocket, &CodeClient, taille_char, p_codeclient))
0605 != ok)
0606 return (Resultat);
0607
0608 return Resultat;
0609 }
0610
0611 #else
0612 Longint
0613 Debut_Communication (ConnHandle * NumSocket, int ind_base)
0614 {
0615
0616 ConnHandle sock;
0617 OSErr anErr;
0618 short int procID;
0619 Longint refcon;
0620 unsigned Longint startticks;
0621 CMRecFlags recflags;
0622 CMBufferSizes desiredsizes, datasizes;
0623 CMStatFlags statflags;
0624 CMStatFlags prevstatflags = -1;
0625 CMFlags cmflags;
0626 Str255 toolname;
0627 Longint Resultat;
0628 char CodeClient;
0629
0630 Resultat = ok;
0631
0632 desiredsizes[cmDataIn] = 0;
0633 desiredsizes[cmCntlIn] = 0;
0634 desiredsizes[cmAttnIn] = 0;
0635 desiredsizes[cmRsrvIn] = 0;
0636 desiredsizes[cmDataOut] = 0;
0637 desiredsizes[cmCntlOut] = 0;
0638 desiredsizes[cmAttnOut] = 0;
0639 desiredsizes[cmRsrvOut] = 0;
0640
0641 recflags = 0;
0642 refcon = 0;
0643
0644 (void) InitCRM ();
0645 (void) InitCTBUtilities ();
0646 (void) InitCM ();
0647
0648 Strcpy (toolname, glob_toolname);
0649 procID = CMGetProcID (toolname);
0650 if (procID != -1)
0651 {
0652 sock = NULL;
0653 if ((sock = CMNew (procID, recflags, desiredsizes, refcon, (Longint) NULL))
0654 == NULL)
0655 return erreur_cree_enr_conn;
0656 *NumSocket = sock;
0657 }
0658 else
0659 return erreur_outil_connexion;
0660
0661 if ((anErr = CMSetConfig (*NumSocket, glob_config)) != noErr)
0662 return erreur_config_conn;
0663
0664 if ((anErr = CMOpen (*NumSocket, 0, NULL, 1)) != noErr)
0665 return erreur_connexion;
0666
0667
0668
0669 CodeClient = client;
0670 if ((Resultat = Emission (*NumSocket, &CodeClient, taille_char, p_codeclient))
0671 != ok)
0672 return (Resultat);
0673
0674 return Resultat;
0675 }
0676
0677 #endif
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691 void
0692 Fin_Communication (
0693 #if (client == MAC)
0694 ConnHandle nsock
0695 #else
0696 int nsock
0697 #endif
0698 )
0699 {
0700 #if (client==PC)
0701 close_socket (nsock);
0702 #elif (client==MAC)
0703 CMDispose (nsock);
0704 #elif (client == INTEL)
0705 shutdown (nsock, 2);
0706 #else
0707 close (nsock);
0708 #endif
0709 }
0710
0711 void *
0712 Memset (void *dst, int c, Longint n)
0713 {
0714 if (n != 0)
0715 {
0716 char *d = dst;
0717 do
0718 *d++ = c;
0719 while (--n != 0);
0720 }
0721 return (dst);
0722 }
0723
0724 int
0725 ReceptionYB (int nsock, char *szBuffer, int len)
0726 {
0727 int ret;
0728
0729 if ((ret = recv (nsock, szBuffer, len, 0)) <= 0)
0730 return 0;
0731
0732 return ret;
0733 }