Back to index

lightning-sunbird  0.9+nobinonly
Functions | Variables
tcp.c File Reference
#include "lber.h"
#include "ldap.h"
#include "tcp.h"
#include <Devices.h>
#include <Events.h>
#include <Memory.h>
#include <Errors.h>
#include <Gestalt.h>

Go to the source code of this file.

Functions

void bzero ()
pascal void setdoneflag ()
pascal void EventHandler ()
pascal void DNREventHandler ()
OSStatus tcp_init (void)
Boolean tcp_have_opentransport (void)
tcpstreamtcpopen (unsigned char *buf, long buflen)
InetPort tcpconnect (tcpstream *tsp, InetHost addr, InetPort port)
short tcpclose (tcpstream *tsp)
short tcpselect (tcpstream *tsp, struct timeval *timeout)
short tcpreadready (tcpstream *tsp)
short tcpwriteready (tcpstream *tsp)
long tcpread (tcpstream *tsp, UInt8 timeout, unsigned char *rbuf, unsigned short rbuflen, DialogPtr dlp)
long tcpwrite (tcpstream *tsp, unsigned char *wbuf, unsigned short wbuflen)
short tcpgetpeername (tcpstream *tsp, InetHost *addrp, InetPort *portp)
static void bzero (char *p, unsigned long len)
short gethostinfobyname (char *host, InetHostInfo *hip)
short gethostinfobyaddr (InetHost addr, InetHostInfo *hip)
short ipaddr2str (InetHost ipaddr, char *addrstr)
static pascal void EventHandler (void *contextPtr, OTEventCode event, OTResult result, void *)
static pascal void DNREventHandler (void *contextPtr, OTEventCode event, OTResult result, void *)

Variables

static Boolean gHaveOT = false
static OTNotifyUPP sEventHandlerUPP
static EventRecord dummyEvent

Function Documentation

void bzero ( )

Here is the caller graph for this function:

static void bzero ( char *  p,
unsigned long  len 
) [static]

Definition at line 473 of file tcp.c.

{
       unsigned long i;
       
       for ( i = 0; i < len; ++i ) {
              *p++ = '\0';
       }
}
pascal void DNREventHandler ( )

Here is the caller graph for this function:

static pascal void DNREventHandler ( void contextPtr,
OTEventCode  event,
OTResult  result,
void  
) [static]

Definition at line 655 of file tcp.c.

{
       switch(event)
       {
              // OTInetStringToAddress Complete  %%%
        case T_DNRSTRINGTOADDRCOMPLETE:
        // OTInetAddressToName Complete          %%%    
        case T_DNRADDRTONAMECOMPLETE:
              {
              int *done = (void *)contextPtr;
              if ( result == noErr )
                            *done = 1;
                     else
                            *done = 2;
                     }
                     break;
                     
              default:
                     break;
       }
}

Here is the call graph for this function:

pascal void EventHandler ( )

Here is the caller graph for this function:

static pascal void EventHandler ( void contextPtr,
OTEventCode  event,
OTResult  result,
void  
) [static]

Definition at line 619 of file tcp.c.

{
       tcpstream *tsp       = (tcpstream *)contextPtr;
       Boolean enteredNotifier = OTEnterNotifier(tsp->tcps_ep);
       
       switch(event)
       {
              // OTConnect callback                            %%%
              case T_CONNECT:      
                     if ( result == noErr )      {                           
                            tsp->tcps_connected = true;
                     }
                     break;

              case T_ORDREL:
              case T_DISCONNECT:
                     tsp->tcps_terminated = true;
                     break;
              case T_DATA:
              case T_EXDATA:
                     tsp->tcps_data += 1;
                     break;
              default:
                     break;
       }
       
       if (enteredNotifier) {
              OTLeaveNotifier(tsp->tcps_ep);
       }
       
       return;
}

Here is the call graph for this function:

short gethostinfobyaddr ( InetHost  addr,
InetHostInfo *  hip 
)

Definition at line 544 of file tcp.c.

{
       
       char                 done = 0;
       OSStatus             err;
       unsigned long time;
       InetSvcRef           inetsvc;             /* Internet services reference */
       unsigned long cur_time, end_time;
       int                         timeout = 20;
       static int           sNotifiedWithResult2;
       static int*   sNpointer2;
       OTNotifyUPP          DNREventHandlerUPP;

       if ( gHaveOT ) {
              // Get an Internet Services reference
              inetsvc = OTOpenInternetServices( kDefaultInternetServicesPath, 0, &err );
              if ( !inetsvc || err != kOTNoError ) {
                     if ( err == kOTNoError ) {
                            err = -1;
                     }
                     inetsvc = nil;
              }
              
              if ( !err ) {
                     // set to async mode                      %%%
                     sNotifiedWithResult2 = 0;
                     sNpointer2 = &sNotifiedWithResult2;
                     DNREventHandlerUPP = NewOTNotifyUPP(DNREventHandler);
                     err = OTInstallNotifier(inetsvc, DNREventHandlerUPP, (void*)sNpointer2);
                     err = OTSetAsynchronous( inetsvc );

                     err = OTInetAddressToName( inetsvc, addr, hip->name );
                     if ( err == kOTNoError ) {
                            GetDateTime( &cur_time );
                            end_time = cur_time + timeout;
                            while(( timeout <= 0 || end_time > cur_time ) && (sNotifiedWithResult2 == 0)) {
                                   GetDateTime( &cur_time );
                                   (void)WaitNextEvent(nullEvent, &dummyEvent, 1, NULL);
                            }
                            if ( sNotifiedWithResult2 != 1 )
                                   err = -1;

                            DisposeOTNotifyUPP(DNREventHandlerUPP);
                     }                           
              }
              
              if ( inetsvc ) {
                     OTCloseProvider( inetsvc );
              }
       }
       return( err );
}

Here is the call graph for this function:

short gethostinfobyname ( char *  host,
InetHostInfo *  hip 
)

Definition at line 487 of file tcp.c.

                                                   {
       char                 done = 0;
       OSStatus             err;
       unsigned long time;
       InetSvcRef           inetsvc;             /* Internet services reference */
       unsigned long cur_time, end_time;
       int                         timeout = 20;
       static int           sNotifiedWithResult;
       static int*   sNpointer;
       OTNotifyUPP          DNREventHandlerUPP;
       
       if ( gHaveOT ) {
       
              // Get an Internet Services reference
              inetsvc = OTOpenInternetServices( kDefaultInternetServicesPath, 0, &err );
              if ( !inetsvc || err != kOTNoError ) {
                     if ( err == kOTNoError ) {
                            err = -1;
                     }
                     inetsvc = nil;
              }
              
              if ( !err ) {
                     // set to async mode                      %%%
                     sNotifiedWithResult = 0;
                     sNpointer = &sNotifiedWithResult;
                     DNREventHandlerUPP = NewOTNotifyUPP(DNREventHandler);
                     err = OTInstallNotifier(inetsvc, DNREventHandlerUPP, (void*)sNpointer);
                     err = OTSetAsynchronous( inetsvc );

                     err = OTInetStringToAddress( inetsvc, host, hip );
                     if ( err == kOTNoError ) {
                            GetDateTime( &cur_time );
                            end_time = cur_time + timeout;
                            while(( timeout <= 0 || end_time > cur_time ) && (sNotifiedWithResult == 0)) {
                                   GetDateTime( &cur_time );
                                   (void)WaitNextEvent(nullEvent, &dummyEvent, 1, NULL);
                            }
                            if ( sNotifiedWithResult != 1 )
                                   err = -1;
                     }                           
                     
                     DisposeOTNotifyUPP(DNREventHandlerUPP);
              }
              
              if ( inetsvc ) {
                     OTCloseProvider(inetsvc);
              }
       }
       
       return( err );
}

Here is the call graph for this function:

Here is the caller graph for this function:

short ipaddr2str ( InetHost  ipaddr,
char *  addrstr 
)

Definition at line 602 of file tcp.c.

{
       OSStatus                           err;

       if ( gHaveOT ) {
       
              OTInetHostToString( ipaddr, addrstr );
              err = kOTNoError;
       
       }      
       return( err );
}

Here is the call graph for this function:

pascal void setdoneflag ( )

Definition at line 116 of file tcp.c.

{
       return( gHaveOT );
}

Definition at line 79 of file tcp.c.

{
       static Boolean       sInitialized = false;
       Boolean              hasOT;
       long                 gestaltResult;
       OSStatus             otErr = kOTNoError;
       OSErr                err;
       
       if ( sInitialized ) {
              return otErr;
       }
       
       err = Gestalt(gestaltOpenTpt, &gestaltResult);
       
       hasOT = (err == noErr) && ((gestaltResult & gestaltOpenTptPresentMask) != 0);
       hasOT = hasOT && ((gestaltResult & gestaltOpenTptTCPPresentMask) != 0);

       sInitialized = true;
       
       if ( hasOT ) {
#if TARGET_CARBON
              gHaveOT = ( InitOpenTransportInContext(kInitOTForApplicationMask, NULL) == noErr );
#else /* TARGET_CARBON */
              gHaveOT = ( InitOpenTransport() == noErr );
#endif /* TARGET_CARBON */

              sEventHandlerUPP = NewOTNotifyUPP(EventHandler);
              /* Somewhere we should probably do a DisposeOTNotifyUPP(sEventHandlerUPP) but since we only
                 create one once I'm not going to worry about a leak */

       }

       return otErr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

short tcpclose ( tcpstream tsp)

Definition at line 265 of file tcp.c.

                            {
       OSStatus             rc;
       
       if ( gHaveOT ) {
       
       
              if ( tsp->tcps_ep == NULL ) {
                     return( -1 );
              }
       
#ifdef notdef
              /*
               * if connected execute a close
               */
              if ( tcp->tcps_connected ) {
                     Call OTSndOrderlyDisconnect and wait for the other end to respond.  This requires
                     waiting for and reading any data that comes in in the meantime.  This code was ifdefed
                     out in the MacTCP so it is here too.
              }
#endif /* notdef */
              
              rc = OTSndDisconnect( tsp->tcps_ep, NULL );
       
              OTCloseProvider( tsp->tcps_ep );
              DisposePtr( (Ptr)tsp );
              
              if ( rc != 0 ) {
                     rc = -1;
              }

       }             
       return( rc );
}

Here is the call graph for this function:

Here is the caller graph for this function:

InetPort tcpconnect ( tcpstream tsp,
InetHost  addr,
InetPort  port 
)

Definition at line 185 of file tcp.c.

                                                            {
       OSStatus      err;
       struct InetAddress sndsin, rcvsin, retsin;
       TCall         sndcall, rcvcall;
       TBind         ret;
       TDiscon              localDiscon;
       unsigned long time, end_time;
       int           timeout = 45;                      /*     %%%    */

       if ( gHaveOT ) {
       
              if ( tsp->tcps_ep == NULL ) {
                     return( 0 );
              }
       
              bzero( (char *)&sndsin, sizeof( struct InetAddress ));
              bzero( (char *)&rcvsin, sizeof( struct InetAddress ));
              bzero( (char *)&retsin, sizeof( struct InetAddress ));
              bzero( (char *)&sndcall, sizeof( TCall ));
              bzero( (char *)&rcvcall, sizeof( TCall));
              bzero( (char *)&ret, sizeof( TBind ));
              
              // Bind TCP to an address and port.  We don't care which one, so we pass null for
              // the requested address.
              ret.addr.maxlen = sizeof( struct InetAddress );
              ret.addr.buf = (unsigned char *)&retsin;
              err = OTBind( tsp->tcps_ep, NULL, &ret );
              if ( err != kOTNoError ) {
                     return( 0 );
              }
       
       
              // set to async mode, then OTConnect             %%%
              err = OTSetAsynchronous( tsp->tcps_ep );

              OTInitInetAddress( &sndsin, port, addr );
              sndcall.addr.len = sizeof( struct InetAddress );
              sndcall.addr.buf = (UInt8 *)&sndsin;
              rcvcall.addr.maxlen = sizeof( struct InetAddress );
              rcvcall.addr.buf = (unsigned char *)&rcvsin;

              err = OTConnect( tsp->tcps_ep, &sndcall, nil );
              if ( (err != kOTNoDataErr) && (err != kOTNoError) )     
                     return 0;
       
              // wait for the OTConnect done            %%%    
              GetDateTime( &time );
              end_time = time + timeout;
              while(( timeout <= 0 || end_time > time ) && !tsp->tcps_connected &&
                            !tsp->tcps_terminated ) {
                     GetDateTime( &time );
                     (void)WaitNextEvent(nullEvent, &dummyEvent, 1, NULL);
              }
              
              if ( tsp->tcps_connected )
              {
                     err = OTRcvConnect( tsp->tcps_ep, &rcvcall );
                     if ( err == kOTNoError)
                     {
                            tsp->tcps_remoteport = rcvsin.fPort;
                            tsp->tcps_remoteaddr = rcvsin.fHost;
                     }
                     OTSetSynchronous( tsp->tcps_ep );  // set back to sync  %%%    
                     return( retsin.fPort );
              }
              else
              {
                     if ( tsp->tcps_terminated )
                            OTRcvDisconnect(tsp->tcps_ep, &localDiscon );
                     return 0;
              }      
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

short tcpgetpeername ( tcpstream tsp,
InetHost *  addrp,
InetPort *  portp 
)

Definition at line 446 of file tcp.c.

                                                                   {
       OSErr         err;

       if ( gHaveOT ) {
       
              if ( tsp->tcps_ep == NULL ) {
                     return( -1 );
              }
                     
              if ( addrp != NULL ) {
                     *addrp = tsp->tcps_remoteaddr;
              }
       
              if ( portp != NULL ) {
                     *portp = tsp->tcps_remoteport;
              }
              
       }

       return( kOTNoError );
}
tcpstream* tcpopen ( unsigned char *  buf,
long  buflen 
)

Definition at line 127 of file tcp.c.

                                            {
       OSStatus             err;
       tcpstream *          tsp;
       short                drefnum;
       TEndpointInfo info;

       if (nil == (tsp = (tcpstream *)NewPtrClear(sizeof(tcpstream)))) {
              return( nil );
       }
       
       if ( gHaveOT ) {
       
              //
              // Now create a TCP
              //
#if TARGET_CARBON
              tsp->tcps_ep = OTOpenEndpointInContext( OTCreateConfiguration( kTCPName ), 0, &info, &err, NULL );
#else /* TARGET_CARBON */
              tsp->tcps_ep = OTOpenEndpoint( OTCreateConfiguration( kTCPName ), 0, &info, &err );
#endif /* TARGET_CARBON */

              if ( !tsp->tcps_ep ) {
                     if ( err == kOTNoError ) {
                            err = -1;
                     }
              }
       
              if ( !err ) {
                     err = OTSetSynchronous( tsp->tcps_ep );
              }
       
              tsp->tcps_data = 0;
              tsp->tcps_terminated = tsp->tcps_connected = false;
              
              //
              // Install notifier we're going to use
              //     need to pass tsp to make it work          %%%
              //
              if ( !err ) {
                     err = OTInstallNotifier( tsp->tcps_ep, sEventHandlerUPP, tsp );
              }
       
              if ( err != kOTNoError ) {
                     if ( tsp->tcps_ep ) {
                            OTCloseProvider( tsp->tcps_ep );
                     }
                     DisposePtr( (Ptr)tsp );
                     tsp = nil;
              }
       }      
       return( tsp );
}

Here is the call graph for this function:

Here is the caller graph for this function:

long tcpread ( tcpstream tsp,
UInt8  timeout,
unsigned char *  rbuf,
unsigned short  rbuflen,
DialogPtr  dlp 
)

Definition at line 363 of file tcp.c.

                                                                                    {
#pragma unused (dlp)
       unsigned long time, end_time;
       OTFlags                     flags;
       OTResult             result;
       size_t                      dataAvail;
       
       if ( gHaveOT ) {
       
              if ( tsp->tcps_ep == NULL ) {
                     return( -1 );
              }
              
              // Try to read data.  Since we're in non-blocking mode, this will fail with kOTNoDataErr
              // if no data is available.
              result = OTRcv( tsp->tcps_ep, rbuf, rbuflen, &flags );
              if ( result == kOTNoDataErr ) {
                     // Nothing available, wait for some.  The ugly spin loop below is the way the old
                     // MacTCP code worked.  I should fix it, but I don't have time right now.
                     tsp->tcps_data = 0;
                     GetDateTime( &time );
                     end_time = time + timeout;
              
                     while (( timeout <= 0 || end_time > time ) && tsp->tcps_data < 1 && !tsp->tcps_terminated ) {
                            OTCountDataBytes( tsp->tcps_ep, &dataAvail );
                            if ( dataAvail > 0 ) {
                                   tsp->tcps_data = 1;
                            }
                            GetDateTime( &time );
                            (void)WaitNextEvent(nullEvent, &dummyEvent, 1, NULL);
                     }
                     
                     if ( tsp->tcps_data < 1 ) {
                            return( tsp->tcps_terminated ? -3 : -1 );
                     }
                     
                     // Should have data available now, try again.
                     result = OTRcv( tsp->tcps_ep, rbuf, rbuflen, &flags );
              }
              
              if ( result < 0 ) {
                     return( -1 );
              }
              
              OTCountDataBytes( tsp->tcps_ep, &dataAvail );
              if ( dataAvail == 0 ) {
                     tsp->tcps_data = 0;
              }

              return( result );
       
       }      
}
short tcpreadready ( tcpstream tsp)

Definition at line 328 of file tcp.c.

{
       size_t        dataAvail;

       if (gHaveOT) {
              if ( tsp->tcps_ep == NULL ) {
                     return( -1 );
              }
       
              OTCountDataBytes( tsp->tcps_ep, &dataAvail );
              tsp->tcps_data = ( dataAvail != 0 );
       }

       return ( tsp->tcps_terminated ? -1 : ( tsp->tcps_data < 1 ) ? 0 : 1 );
}

Here is the caller graph for this function:

short tcpselect ( tcpstream tsp,
struct timeval timeout 
)

Definition at line 308 of file tcp.c.

{
       unsigned long ticks, endticks;
       short rc;

       if ( timeout != NULL ) {
              endticks = 60 * timeout->tv_sec + ( 60 * timeout->tv_usec ) / 1000000 + TickCount();
       }
       ticks = 0;

       while (( rc = tcpreadready( tsp )) == 0 && ( timeout == NULL || ticks < endticks )) {
              (void)WaitNextEvent(nullEvent, &dummyEvent, 1, NULL);
              ticks = TickCount();
       }

       return ( rc );
}

Here is the call graph for this function:

long tcpwrite ( tcpstream tsp,
unsigned char *  wbuf,
unsigned short  wbuflen 
)

Definition at line 425 of file tcp.c.

                                                                          {
       OSErr         err;
       OTResult      result;
       
       if ( gHaveOT ) {
       
              if ( tsp->tcps_ep == NULL ) {
                     return( -1 );
              }
              
              OTSetBlocking( tsp->tcps_ep );
              result = OTSnd( tsp->tcps_ep, wbuf, wbuflen, 0 );
              OTSetNonBlocking( tsp->tcps_ep );
              return( result );
              
       }
}
short tcpwriteready ( tcpstream tsp)

Definition at line 346 of file tcp.c.

{
       if (gHaveOT) {
              if ( tsp->tcps_ep == NULL ) {
                     return( -1 );
              }
       }

       return ( tsp->tcps_terminated ? -1 : 1 );
}

Here is the caller graph for this function:


Variable Documentation

EventRecord dummyEvent [static]

Definition at line 71 of file tcp.c.

Boolean gHaveOT = false [static]

Definition at line 69 of file tcp.c.

OTNotifyUPP sEventHandlerUPP [static]

Definition at line 70 of file tcp.c.