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
|
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998-1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright (c) 1990-92 Regents of the University of Michigan.
* All rights reserved.
*/
/*
* tcp.c -- TCP communication related routines
*/
#include "lber.h"
#include "ldap.h"
#include "tcp.h"
#include <Devices.h>
#include <Events.h> /* to get SystemTask() */
#include <Memory.h>
#include <Errors.h>
#include <Gestalt.h>
/*
* local prototypes
*/
#ifdef NEEDPROTOS
void bzero( char *buf, unsigned long count );
pascal void setdoneflag( struct hostInfo *hip, char *donep );
pascal void EventHandler(void * contextPtr, OTEventCode event, OTResult result, void * /* cookie */);
pascal void DNREventHandler(void * contextPtr, OTEventCode event, OTResult result, void * /* cookie */);
#else /* NEEDPROTOS */
void bzero();
pascal void setdoneflag();
pascal void EventHandler();
pascal void DNREventHandler();
#endif /* NEEDPROTOS */
static Boolean gHaveOT = false;
static OTNotifyUPP sEventHandlerUPP;
static EventRecord dummyEvent;
/*
* Initialize the tcp module. This mainly consists of seeing if we have
* Open Transport and initializing it and setting our global saying so.
*/
OSStatus
tcp_init( void )
{
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;
}
Boolean
tcp_have_opentransport( void )
{
return( gHaveOT );
}
/*
* open and return an pointer to a TCP stream (return NULL if error)
* "buf" is a buffer for receives of length "buflen."
*/
tcpstream *
tcpopen( unsigned char * buf, long buflen ) {
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 );
}
/*
* connect to remote host at IP address "addr", TCP port "port"
* return local port assigned, 0 if error
*/
InetPort
tcpconnect( tcpstream * tsp, InetHost addr, InetPort port ) {
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;
}
}
}
/*
* close and release a TCP stream
* returns 0 if no error, -1 if any error occurs
*/
short
tcpclose( tcpstream * tsp ) {
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 );
}
/*
* wait for new data to arrive
*
* if "timeout" is NULL, wait until data arrives or connection goes away
* if "timeout" is a pointer to a zero'ed struct, poll
* else wait for "timeout->tv_sec + timeout->tv_usec" seconds
*/
short
tcpselect( tcpstream * tsp, struct timeval * timeout )
{
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 );
}
short
tcpreadready( tcpstream *tsp )
{
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 );
}
short
tcpwriteready( tcpstream *tsp )
{
if (gHaveOT) {
if ( tsp->tcps_ep == NULL ) {
return( -1 );
}
}
return ( tsp->tcps_terminated ? -1 : 1 );
}
/*
* read up to "rbuflen" bytes into "rbuf" from a connected TCP stream
* wait up to "timeout" seconds for data (if timeout == 0, wait "forever")
*/
long
tcpread( tcpstream * tsp, UInt8 timeout, unsigned char * rbuf,
unsigned short rbuflen, DialogPtr dlp ) {
#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 );
}
}
/*
* send TCP data
* "sp" is the stream to write to
* "wbuf" is "wbuflen" bytes of data to send
* returns < 0 if error, number of bytes written if no error
*/
long
tcpwrite( tcpstream * tsp, unsigned char * wbuf, unsigned short wbuflen ) {
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
tcpgetpeername( tcpstream *tsp, InetHost *addrp, InetPort *portp ) {
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 );
}
/*
* bzero -- set "len" bytes starting at "p" to zero
*/
static void
bzero( char *p, unsigned long len )
{
unsigned long i;
for ( i = 0; i < len; ++i ) {
*p++ = '\0';
}
}
/*
* return a hostInfo structure for "host" (return != noErr if error)
*/
short
gethostinfobyname( char *host, InetHostInfo *hip ) {
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 );
}
/*
* return a hostInfo structure for "addr" (return != noErr if error)
*/
short
gethostinfobyaddr( InetHost addr, InetHostInfo *hip )
{
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 );
}
/*
* return a ASCII equivalent of ipaddr. addrstr must be at large enough to hold the
* result which is of the form "AAA.BBB.CCC.DDD" and can be a maximum of 16 bytes in size
*/
short
ipaddr2str( InetHost ipaddr, char *addrstr )
{
OSStatus err;
if ( gHaveOT ) {
OTInetHostToString( ipaddr, addrstr );
err = kOTNoError;
}
return( err );
}
/*******************************************************************************
** EventHandler
********************************************************************************/
static pascal void EventHandler(void * contextPtr, OTEventCode event, OTResult result, void * /* cookie */)
{
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;
}
/*******************************************************************************
** Simplified EventHandler for DNR events
********************************************************************************/
static pascal void DNREventHandler(void * contextPtr, OTEventCode event, OTResult result, void * /* cookie */)
{
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;
}
}
|