Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions
tcp.h File Reference
#include <Dialogs.h>
#include <OpenTransport.h>
#include <OpenTptInternet.h>
#include <utime.h>
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  tcpstream

Defines

#define TCP_BUFSIZ   8192
#define TCP_IS_TERMINATED(tsp)   (tsp)->tcps_terminated

Typedefs

typedef struct tcpstream tcpstream
typedef struct tcpstreamtcpstreamptr

Functions

OSStatus tcp_init (void)
Boolean tcp_have_opentransport (void)
tcpstreamtcpopen (unsigned char *buf, long buflen)
InetPort tcpconnect (tcpstream *s, InetHost addr, InetPort port)
short tcpclose (tcpstream *s)
long tcpread (tcpstream *s, UInt8 timeout, unsigned char *rbuf, unsigned short rbuflen, DialogPtr dlp)
long tcpwrite (tcpstream *s, unsigned char *wbuf, unsigned short wbuflen)
short tcpselect (tcpstream *s, struct timeval *timeout)
short tcpreadready (tcpstream *tsp)
short tcpwriteready (tcpstream *tsp)
short tcpgetpeername (tcpstream *tsp, InetHost *addrp, InetPort *portp)
short gethostinfobyname (char *host, InetHostInfo *hip)
short gethostinfobyaddr (InetHost addr, InetHostInfo *hip)
short ipaddr2str (InetHost ipaddr, char *addrstr)

Class Documentation

struct tcpstream

Definition at line 57 of file tcp.h.

Class Members
short drefnum
unsigned char * tcps_buffer
Boolean tcps_connected
short tcps_data
EndpointRef tcps_ep
InetHost tcps_remoteaddr
InetPort tcps_remoteport
Boolean tcps_terminated

Define Documentation

#define TCP_BUFSIZ   8192

Definition at line 54 of file tcp.h.

#define TCP_IS_TERMINATED (   tsp)    (tsp)->tcps_terminated

Definition at line 68 of file tcp.h.


Typedef Documentation

typedef struct tcpstream tcpstream
typedef struct tcpstream * tcpstreamptr

Function Documentation

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:

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 s)

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 s,
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 s,
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 s,
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 s,
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: