DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Untracked file

Line Code
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
/*
 * Functions that emulate PR_AcceptRead and PR_TransmitFile for SSL sockets.
 * Each Layered NSPR protocol (like SSL) must unfortunately contain its 
 * own implementation of these functions.  This code was taken from NSPR.
 *
 * 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 the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 *
 * $Id: emulate.c,v 1.5 2002/02/22 04:23:22 wtc%netscape.com Exp $
 */

#include "nspr.h"

#if defined( XP_UNIX ) || defined( XP_BEOS )
#include <fcntl.h>
#endif
#if defined(WIN32)
#include <windef.h>
#include <winbase.h>
#endif
#include <string.h>

#define AMASK 7	/* mask for alignment of PRNetAddr */

/*
 * _PR_EmulateAcceptRead
 *
 *  Accept an incoming connection on sd, set *nd to point to the
 *  newly accepted socket, read 'amount' bytes from the accepted
 *  socket.
 *
 *  buf is a buffer of length = amount + (2 * sizeof(PRNetAddr)) + 32
 *  *raddr points to the PRNetAddr of the accepted connection upon
 *  return
 *
 *  return number of bytes read or -1 on error
 *
 */
PRInt32 
ssl_EmulateAcceptRead(	PRFileDesc *   sd, 
			PRFileDesc **  nd,
			PRNetAddr **   raddr, 
			void *         buf, 
			PRInt32        amount, 
			PRIntervalTime timeout)
{
    PRFileDesc *   newsockfd;
    PRInt32        rv;
    PRNetAddr      remote;

    if (!(newsockfd = PR_Accept(sd, &remote, PR_INTERVAL_NO_TIMEOUT))) {
	return -1;
    }

    rv = PR_Recv(newsockfd, buf, amount, 0, timeout);
    if (rv >= 0) {
	ptrdiff_t pNetAddr = (((ptrdiff_t)buf) + amount + AMASK) & ~AMASK;

	*nd = newsockfd;
	*raddr = (PRNetAddr *)pNetAddr;
	memcpy((void *)pNetAddr, &remote, sizeof(PRNetAddr));
	return rv;
    }

    PR_Close(newsockfd);
    return -1;
}


#if !defined( XP_UNIX ) && !defined( WIN32 ) && !defined( XP_BEOS )
/*
 * _PR_EmulateTransmitFile
 *
 *  Send file fd across socket sd. If headers is non-NULL, 'hlen'
 *  bytes of headers is sent before sending the file.
 *
 *  PR_TRANSMITFILE_CLOSE_SOCKET flag - close socket after sending file
 *
 *  return number of bytes sent or -1 on error
 *
 */
#define _TRANSMITFILE_BUFSIZE	(16 * 1024)

PRInt32 
ssl_EmulateTransmitFile(    PRFileDesc *        sd, 
			    PRFileDesc *        fd,
			    const void *        headers, 
			    PRInt32             hlen, 
			    PRTransmitFileFlags flags,
			    PRIntervalTime      timeout)
{
    char *  buf 	= NULL;
    PRInt32 count 	= 0;
    PRInt32 rlen;
    PRInt32 rv;

    buf = PR_MALLOC(_TRANSMITFILE_BUFSIZE);
    if (buf == NULL) {
	PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
	return -1;
    }

    /*
     * send headers, first
     */
    while (hlen) {
	rv =  PR_Send(sd, headers, hlen, 0, timeout);
	if (rv < 0) {
	    /* PR_Send() has invoked PR_SetError(). */
	    rv = -1;
	    goto done;
	} 
	count += rv;
	headers = (const void*) ((const char*)headers + rv);
	hlen -= rv;
    }
    /*
     * send file, next
     */
    while ((rlen = PR_Read(fd, buf, _TRANSMITFILE_BUFSIZE)) > 0) {
	while (rlen) {
	    char *bufptr = buf;

	    rv =  PR_Send(sd, bufptr, rlen,0,PR_INTERVAL_NO_TIMEOUT);
	    if (rv < 0) {
		/* PR_Send() has invoked PR_SetError(). */
		rv = -1;
		goto done;
	    } 
	    count += rv;
	    bufptr = ((char*)bufptr + rv);
	    rlen -= rv;
	}
    }
    if (rlen == 0) {
	/*
	 * end-of-file
	 */
	if (flags & PR_TRANSMITFILE_CLOSE_SOCKET)
	    PR_Close(sd);
	rv = count;
    } else {
	PR_ASSERT(rlen < 0);
	/* PR_Read() has invoked PR_SetError(). */
	rv = -1;
    }

done:
    if (buf)
	PR_DELETE(buf);
    return rv;
}
#else

#define TRANSMITFILE_MMAP_CHUNK (256 * 1024)

/*
 * _PR_UnixTransmitFile
 *
 *  Send file fd across socket sd. If headers is non-NULL, 'hlen'
 *  bytes of headers is sent before sending the file.
 *
 *  PR_TRANSMITFILE_CLOSE_SOCKET flag - close socket after sending file
 *
 *  return number of bytes sent or -1 on error
 *
 */

PRInt32 
ssl_EmulateTransmitFile(    PRFileDesc *        sd, 
			    PRFileDesc *        fd,
			    const void *        headers, 
			    PRInt32             hlen, 
			    PRTransmitFileFlags flags,
			    PRIntervalTime      timeout)
{
    void *            addr = NULL;
    PRFileMap *       mapHandle = NULL;
    PRInt32           count     = 0;
    PRInt32           index     = 0;
    PRInt32           len	= 0;
    PRInt32           rv;
    struct PRFileInfo info;
    struct PRIOVec    iov[2];

    /* Get file size */
    if (PR_SUCCESS != PR_GetOpenFileInfo(fd, &info)) {
	count = -1;
	goto done;
    }
    if (hlen) {
	iov[index].iov_base = (char *) headers;
	iov[index].iov_len  = hlen;
	index++;
    }
    if (info.size > 0) {
	mapHandle = PR_CreateFileMap(fd, info.size, PR_PROT_READONLY);
	if (mapHandle == NULL) {
	    count = -1;
	    goto done;
	}
	/*
	 * If the file is large, mmap and send the file in chunks so as
	 * to not consume too much virtual address space
	 */
	len = PR_MIN(info.size , TRANSMITFILE_MMAP_CHUNK );
	/*
	 * Map in (part of) file. Take care of zero-length files.
	 */
	if (len) {
	    addr = PR_MemMap(mapHandle, 0, len);
	    if (addr == NULL) {
		count = -1;
		goto done;
	    }
	}
	iov[index].iov_base = (char*)addr;
	iov[index].iov_len = len;
	index++;
    }
    if (!index)
    	goto done;
    rv = PR_Writev(sd, iov, index, timeout);
    if (len) {
	PR_MemUnmap(addr, len);
    }
    if (rv >= 0) {
	PR_ASSERT(rv == hlen + len);
	info.size -= len;
	count     += rv;
    } else {
	count = -1;
	goto done;
    }
    /*
     * send remaining bytes of the file, if any
     */
    len = PR_MIN(info.size , TRANSMITFILE_MMAP_CHUNK );
    while (len > 0) {
	/*
	 * Map in (part of) file
	 */
	PR_ASSERT((count - hlen) % TRANSMITFILE_MMAP_CHUNK == 0);
	addr = PR_MemMap(mapHandle, count - hlen, len);
	if (addr == NULL) {
	    count = -1;
	    goto done;
	}
	rv =  PR_Send(sd, addr, len, 0, timeout);
	PR_MemUnmap(addr, len);
	if (rv >= 0) {
	    PR_ASSERT(rv == len);
	    info.size -= rv;
	    count     += rv;
	    len = PR_MIN(info.size , TRANSMITFILE_MMAP_CHUNK );
	} else {
	    count = -1;
	    goto done;
	}
    }
done:
    if ((count >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET))
	PR_Close(sd);
    if (mapHandle != NULL)
    	PR_CloseFileMap(mapHandle);
    return count;
}
#endif  /* XP_UNIX || WIN32 || XP_BEOS */




#if !defined( XP_UNIX ) && !defined( WIN32 ) && !defined( XP_BEOS )
/*
 * _PR_EmulateSendFile
 *
 *  Send file sfd->fd across socket sd. The header and trailer buffers
 *  specified in the 'sfd' argument are sent before and after the file,
 *  respectively.
 *
 *  PR_TRANSMITFILE_CLOSE_SOCKET flag - close socket after sending file
 *
 *  return number of bytes sent or -1 on error
 *
 */

PRInt32 
ssl_EmulateSendFile(PRFileDesc *sd, PRSendFileData *sfd,
                    PRTransmitFileFlags flags, PRIntervalTime timeout)
{
    char *       buf = NULL;
    const void * buffer;
    PRInt32      rv;
    PRInt32      count = 0;
    PRInt32      rlen;
    PRInt32      buflen;
    PRInt32      sendbytes;
    PRInt32      readbytes;

#define _SENDFILE_BUFSIZE   (16 * 1024)

    buf = (char*)PR_MALLOC(_SENDFILE_BUFSIZE);
    if (buf == NULL) {
        PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0);
        return -1;
    }

    /*
     * send header, first
     */
    buflen = sfd->hlen;
    buffer = sfd->header;
    while (buflen) {
        rv =  PR_Send(sd, buffer, buflen, 0, timeout);
        if (rv < 0) {
            /* PR_Send() has invoked PR_SetError(). */
            rv = -1;
            goto done;
        } else {
            count += rv;
            buffer = (const void*) ((const char*)buffer + rv);
            buflen -= rv;
        }
    }
    /*
     * send file, next
     */

    if (PR_Seek(sfd->fd, sfd->file_offset, PR_SEEK_SET) < 0) {
        rv = -1;
        goto done;
    }
    sendbytes = sfd->file_nbytes;
    if (sendbytes == 0) {
        /* send entire file */
        while ((rlen = PR_Read(sfd->fd, buf, _SENDFILE_BUFSIZE)) > 0) {
            while (rlen) {
                char *bufptr = buf;

                rv =  PR_Send(sd, bufptr, rlen, 0, timeout);
                if (rv < 0) {
                    /* PR_Send() has invoked PR_SetError(). */
                    rv = -1;
                    goto done;
                } else {
                    count += rv;
                    bufptr = ((char*)bufptr + rv);
                    rlen -= rv;
                }
            }
        }
        if (rlen < 0) {
            /* PR_Read() has invoked PR_SetError(). */
            rv = -1;
            goto done;
        }
    } else {
        readbytes = PR_MIN(sendbytes, _SENDFILE_BUFSIZE);
        while (readbytes && ((rlen = PR_Read(sfd->fd, buf, readbytes)) > 0)) {
            while (rlen) {
                char *bufptr = buf;

                rv =  PR_Send(sd, bufptr, rlen, 0, timeout);
                if (rv < 0) {
                    /* PR_Send() has invoked PR_SetError(). */
                    rv = -1;
                    goto done;
                } else {
                    count += rv;
                    sendbytes -= rv;
                    bufptr = ((char*)bufptr + rv);
                    rlen -= rv;
                }
            }
	    readbytes = PR_MIN(sendbytes, _SENDFILE_BUFSIZE);
        }
        if (rlen < 0) {
            /* PR_Read() has invoked PR_SetError(). */
            rv = -1;
            goto done;
        } else if (sendbytes != 0) {
            /*
             * there are fewer bytes in file to send than specified
             */
	    PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
            rv = -1;
            goto done;
        }
    }
    /*
     * send trailer, last
     */
    buflen = sfd->tlen;
    buffer = sfd->trailer;
    while (buflen) {
        rv =  PR_Send(sd, buffer, buflen, 0, timeout);
        if (rv < 0) {
            /* PR_Send() has invoked PR_SetError(). */
            rv = -1;
            goto done;
        } else {
            count += rv;
            buffer = (const void*) ((const char*)buffer + rv);
            buflen -= rv;
        }
    }
    rv = count;

done:
    if (buf)
        PR_DELETE(buf);
    if ((rv >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET))
	PR_Close(sd);
    return rv;
}

#else /* UNIX, NT, and BEOS handled below */

/*
 * _PR_UnixSendFile
 *
 *    Send file sfd->fd across socket sd. If header/trailer are specified
 *    they are sent before and after the file, respectively.
 *
 *    PR_TRANSMITFILE_CLOSE_SOCKET flag - close socket after sending file
 *
 *    return number of bytes sent or -1 on error
 *
 */
#define SENDFILE_MMAP_CHUNK    (256 * 1024)

PRInt32 
ssl_EmulateSendFile(PRFileDesc *sd, PRSendFileData *sfd,
                    PRTransmitFileFlags flags, PRIntervalTime timeout)
{
    void *            addr = NULL;
    PRFileMap *       mapHandle  	= NULL;
    PRInt32           count 		= 0;
    PRInt32           file_bytes;
    PRInt32           index 		= 0;
    PRInt32           len;
    PRInt32           rv;
    PRUint32          addr_offset;
    PRUint32          file_mmap_offset;
    PRUint32          mmap_len;
    PRUint32          pagesize;
    struct PRFileInfo info;
    struct PRIOVec    iov[3];

    /* Get file size */
    if (PR_SUCCESS != PR_GetOpenFileInfo(sfd->fd, &info)) {
	count = -1;
	goto done;
    }
    if (sfd->file_nbytes && 
		(info.size < (sfd->file_offset + sfd->file_nbytes))) {
        /*
         * there are fewer bytes in file to send than specified
         */
	PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
        count = -1;
        goto done;
    }
    if (sfd->file_nbytes)
        file_bytes = sfd->file_nbytes;
    else
        file_bytes = info.size - sfd->file_offset;

#if defined(WIN32)
    {
        SYSTEM_INFO sysinfo;
        GetSystemInfo(&sysinfo);
        pagesize = sysinfo.dwAllocationGranularity;
    }
#else
    pagesize = PR_GetPageSize();
#endif
    /*
     * If the file is large, mmap and send the file in chunks so as
     * to not consume too much virtual address space
     */
    if (!sfd->file_offset || !(sfd->file_offset & (pagesize - 1))) {
        /*
         * case 1: page-aligned file offset
         */
        mmap_len = PR_MIN(file_bytes, SENDFILE_MMAP_CHUNK);
        len      = mmap_len;
        file_mmap_offset = sfd->file_offset;
        addr_offset = 0;
    } else {
        /*
         * case 2: non page-aligned file offset
         */
        /* find previous page boundary */
        file_mmap_offset = (sfd->file_offset & ~(pagesize - 1));

        /* number of initial bytes to skip in mmap'd segment */
        addr_offset = sfd->file_offset - file_mmap_offset;
        PR_ASSERT(addr_offset > 0);
        mmap_len = PR_MIN(file_bytes + addr_offset, SENDFILE_MMAP_CHUNK);
        len      = mmap_len - addr_offset;
    }
    /*
     * OK I've convinced myself that length has to be possitive (file_bytes is 
     * negative or  SENDFILE_MMAP_CHUNK is less than pagesize). Just assert 
     * that this is the case so we catch problems in debug builds.
     */
    PR_ASSERT(len >= 0);

    /*
     * Map in (part of) file. Take care of zero-length files.
     */
    if (len > 0) {
	mapHandle = PR_CreateFileMap(sfd->fd, info.size, PR_PROT_READONLY);
	if (!mapHandle) {
	    count = -1;
	    goto done;
	}
	addr = PR_MemMap(mapHandle, file_mmap_offset, mmap_len);
        if (!addr) {
            count = -1;
            goto done;
        }
    }
    /*
     * send headers, first, followed by the file
     */
    if (sfd->hlen) {
        iov[index].iov_base = (char *) sfd->header;
        iov[index].iov_len  = sfd->hlen;
        index++;
    }
    if (len) {
        iov[index].iov_base = (char*)addr + addr_offset;
        iov[index].iov_len  = len;
        index++;
    }
    if ((file_bytes == len) && (sfd->tlen)) {
        /*
         * all file data is mapped in; send the trailer too
         */
        iov[index].iov_base = (char *) sfd->trailer;
        iov[index].iov_len  = sfd->tlen;
        index++;
    }
    rv = PR_Writev(sd, iov, index, timeout);
    if (len)
        PR_MemUnmap(addr, mmap_len);
    if (rv < 0) {
        count = -1;
        goto done;
    }

    PR_ASSERT(rv == sfd->hlen + len + ((len == file_bytes) ? sfd->tlen : 0));

    file_bytes -= len;
    count      += rv;
    if (!file_bytes)    /* header, file and trailer are sent */
	goto done;

    /*
     * send remaining bytes of the file, if any
     */
    len = PR_MIN(file_bytes, SENDFILE_MMAP_CHUNK);
    while (len > 0) {
	/*
	 * Map in (part of) file
	 */
	file_mmap_offset = sfd->file_offset + count - sfd->hlen;
	PR_ASSERT((file_mmap_offset % pagesize) == 0);

	addr = PR_MemMap(mapHandle, file_mmap_offset, len);
        if (!addr) {
            count = -1;
            goto done;
        }
	rv = PR_Send(sd, addr, len, 0, timeout);
	PR_MemUnmap(addr, len);
	if (rv < 0) {
	    count = -1;
	    goto done;
	}

	PR_ASSERT(rv == len);
	file_bytes -= rv;
	count      += rv;
	len = PR_MIN(file_bytes, SENDFILE_MMAP_CHUNK);
    }
    PR_ASSERT(0 == file_bytes);
    if (sfd->tlen) {
        rv =  PR_Send(sd, sfd->trailer, sfd->tlen, 0, timeout);
        if (rv >= 0) {
            PR_ASSERT(rv == sfd->tlen);
            count += rv;
        } else
            count = -1;
    }
done:
    if (mapHandle)
    	PR_CloseFileMap(mapHandle);
    if ((count >= 0) && (flags & PR_TRANSMITFILE_CLOSE_SOCKET))
	PR_Close(sd);
    return count;
}
#endif /* UNIX, NT, and BEOS */