andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** File: prlayer.c andre@0: ** Description: Routines for handling pushable protocol modules on sockets. andre@0: */ andre@0: andre@0: #include "primpl.h" andre@0: #include "prerror.h" andre@0: #include "prmem.h" andre@0: #include "prlock.h" andre@0: #include "prlog.h" andre@0: #include "prio.h" andre@0: andre@0: #include /* for memset() */ andre@0: static PRStatus _PR_DestroyIOLayer(PRFileDesc *stack); andre@0: andre@0: void PR_CALLBACK pl_FDDestructor(PRFileDesc *fd) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: if (NULL != fd->lower) fd->lower->higher = fd->higher; andre@0: if (NULL != fd->higher) fd->higher->lower = fd->lower; andre@0: PR_DELETE(fd); andre@0: } andre@0: andre@0: /* andre@0: ** Default methods that just call down to the next fd. andre@0: */ andre@0: static PRStatus PR_CALLBACK pl_TopClose (PRFileDesc *fd) andre@0: { andre@0: PRFileDesc *top, *lower; andre@0: PRStatus rv; andre@0: andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: PR_ASSERT(fd->secret == NULL); andre@0: PR_ASSERT(fd->methods->file_type == PR_DESC_LAYERED); andre@0: andre@0: if (PR_IO_LAYER_HEAD == fd->identity) { andre@0: /* andre@0: * new style stack; close all the layers, before deleting the andre@0: * stack head andre@0: */ andre@0: rv = fd->lower->methods->close(fd->lower); andre@0: _PR_DestroyIOLayer(fd); andre@0: return rv; andre@0: } else if ((fd->higher) && (PR_IO_LAYER_HEAD == fd->higher->identity)) { andre@0: /* andre@0: * lower layers of new style stack andre@0: */ andre@0: lower = fd->lower; andre@0: /* andre@0: * pop and cleanup current layer andre@0: */ andre@0: top = PR_PopIOLayer(fd->higher, PR_TOP_IO_LAYER); andre@0: top->dtor(top); andre@0: /* andre@0: * then call lower layer andre@0: */ andre@0: return (lower->methods->close(lower)); andre@0: } else { andre@0: /* old style stack */ andre@0: top = PR_PopIOLayer(fd, PR_TOP_IO_LAYER); andre@0: top->dtor(top); andre@0: return (fd->methods->close)(fd); andre@0: } andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefRead (PRFileDesc *fd, void *buf, PRInt32 amount) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->read)(fd->lower, buf, amount); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefWrite ( andre@0: PRFileDesc *fd, const void *buf, PRInt32 amount) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->write)(fd->lower, buf, amount); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefAvailable (PRFileDesc *fd) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->available)(fd->lower); andre@0: } andre@0: andre@0: static PRInt64 PR_CALLBACK pl_DefAvailable64 (PRFileDesc *fd) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->available64)(fd->lower); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefFsync (PRFileDesc *fd) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->fsync)(fd->lower); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefSeek ( andre@0: PRFileDesc *fd, PRInt32 offset, PRSeekWhence how) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->seek)(fd->lower, offset, how); andre@0: } andre@0: andre@0: static PRInt64 PR_CALLBACK pl_DefSeek64 ( andre@0: PRFileDesc *fd, PRInt64 offset, PRSeekWhence how) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->seek64)(fd->lower, offset, how); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefFileInfo (PRFileDesc *fd, PRFileInfo *info) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->fileInfo)(fd->lower, info); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefFileInfo64 (PRFileDesc *fd, PRFileInfo64 *info) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->fileInfo64)(fd->lower, info); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefWritev (PRFileDesc *fd, const PRIOVec *iov, andre@0: PRInt32 size, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->writev)(fd->lower, iov, size, timeout); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefConnect ( andre@0: PRFileDesc *fd, const PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->connect)(fd->lower, addr, timeout); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefConnectcontinue ( andre@0: PRFileDesc *fd, PRInt16 out_flags) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->connectcontinue)(fd->lower, out_flags); andre@0: } andre@0: andre@0: static PRFileDesc* PR_CALLBACK pl_TopAccept ( andre@0: PRFileDesc *fd, PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PRStatus rv; andre@0: PRFileDesc *newfd, *layer = fd; andre@0: PRFileDesc *newstack; andre@0: PRBool newstyle_stack = PR_FALSE; andre@0: andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: /* test for new style stack */ andre@0: while (NULL != layer->higher) andre@0: layer = layer->higher; andre@0: newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; andre@0: newstack = PR_NEW(PRFileDesc); andre@0: if (NULL == newstack) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: *newstack = *fd; /* make a copy of the accepting layer */ andre@0: andre@0: newfd = (fd->lower->methods->accept)(fd->lower, addr, timeout); andre@0: if (NULL == newfd) andre@0: { andre@0: PR_DELETE(newstack); andre@0: return NULL; andre@0: } andre@0: andre@0: if (newstyle_stack) { andre@0: newstack->lower = newfd; andre@0: newfd->higher = newstack; andre@0: return newstack; andre@0: } else { andre@0: /* this PR_PushIOLayer call cannot fail */ andre@0: rv = PR_PushIOLayer(newfd, PR_TOP_IO_LAYER, newstack); andre@0: PR_ASSERT(PR_SUCCESS == rv); andre@0: return newfd; /* that's it */ andre@0: } andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefBind (PRFileDesc *fd, const PRNetAddr *addr) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->bind)(fd->lower, addr); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefListen (PRFileDesc *fd, PRIntn backlog) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->listen)(fd->lower, backlog); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefShutdown (PRFileDesc *fd, PRIntn how) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->shutdown)(fd->lower, how); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefRecv ( andre@0: PRFileDesc *fd, void *buf, PRInt32 amount, andre@0: PRIntn flags, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->recv)( andre@0: fd->lower, buf, amount, flags, timeout); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefSend ( andre@0: PRFileDesc *fd, const void *buf, andre@0: PRInt32 amount, PRIntn flags, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->send)(fd->lower, buf, amount, flags, timeout); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefRecvfrom ( andre@0: PRFileDesc *fd, void *buf, PRInt32 amount, andre@0: PRIntn flags, PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->recvfrom)( andre@0: fd->lower, buf, amount, flags, addr, timeout); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefSendto ( andre@0: PRFileDesc *fd, const void *buf, PRInt32 amount, PRIntn flags, andre@0: const PRNetAddr *addr, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->sendto)( andre@0: fd->lower, buf, amount, flags, addr, timeout); andre@0: } andre@0: andre@0: static PRInt16 PR_CALLBACK pl_DefPoll ( andre@0: PRFileDesc *fd, PRInt16 in_flags, PRInt16 *out_flags) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->poll)(fd->lower, in_flags, out_flags); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefAcceptread ( andre@0: PRFileDesc *sd, PRFileDesc **nd, PRNetAddr **raddr, void *buf, andre@0: PRInt32 amount, PRIntervalTime t) andre@0: { andre@0: PRInt32 nbytes; andre@0: PRStatus rv; andre@0: PRFileDesc *newstack; andre@0: PRFileDesc *layer = sd; andre@0: PRBool newstyle_stack = PR_FALSE; andre@0: andre@0: PR_ASSERT(sd != NULL); andre@0: PR_ASSERT(sd->lower != NULL); andre@0: andre@0: /* test for new style stack */ andre@0: while (NULL != layer->higher) andre@0: layer = layer->higher; andre@0: newstyle_stack = (PR_IO_LAYER_HEAD == layer->identity) ? PR_TRUE : PR_FALSE; andre@0: newstack = PR_NEW(PRFileDesc); andre@0: if (NULL == newstack) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return -1; andre@0: } andre@0: *newstack = *sd; /* make a copy of the accepting layer */ andre@0: andre@0: nbytes = sd->lower->methods->acceptread( andre@0: sd->lower, nd, raddr, buf, amount, t); andre@0: if (-1 == nbytes) andre@0: { andre@0: PR_DELETE(newstack); andre@0: return nbytes; andre@0: } andre@0: if (newstyle_stack) { andre@0: newstack->lower = *nd; andre@0: (*nd)->higher = newstack; andre@0: *nd = newstack; andre@0: return nbytes; andre@0: } else { andre@0: /* this PR_PushIOLayer call cannot fail */ andre@0: rv = PR_PushIOLayer(*nd, PR_TOP_IO_LAYER, newstack); andre@0: PR_ASSERT(PR_SUCCESS == rv); andre@0: return nbytes; andre@0: } andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefTransmitfile ( andre@0: PRFileDesc *sd, PRFileDesc *fd, const void *headers, PRInt32 hlen, andre@0: PRTransmitFileFlags flags, PRIntervalTime t) andre@0: { andre@0: PR_ASSERT(sd != NULL); andre@0: PR_ASSERT(sd->lower != NULL); andre@0: andre@0: return sd->lower->methods->transmitfile( andre@0: sd->lower, fd, headers, hlen, flags, t); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefGetsockname (PRFileDesc *fd, PRNetAddr *addr) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->getsockname)(fd->lower, addr); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefGetpeername (PRFileDesc *fd, PRNetAddr *addr) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->getpeername)(fd->lower, addr); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefGetsocketoption ( andre@0: PRFileDesc *fd, PRSocketOptionData *data) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->getsocketoption)(fd->lower, data); andre@0: } andre@0: andre@0: static PRStatus PR_CALLBACK pl_DefSetsocketoption ( andre@0: PRFileDesc *fd, const PRSocketOptionData *data) andre@0: { andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(fd->lower != NULL); andre@0: andre@0: return (fd->lower->methods->setsocketoption)(fd->lower, data); andre@0: } andre@0: andre@0: static PRInt32 PR_CALLBACK pl_DefSendfile ( andre@0: PRFileDesc *sd, PRSendFileData *sfd, andre@0: PRTransmitFileFlags flags, PRIntervalTime timeout) andre@0: { andre@0: PR_ASSERT(sd != NULL); andre@0: PR_ASSERT(sd->lower != NULL); andre@0: andre@0: return sd->lower->methods->sendfile( andre@0: sd->lower, sfd, flags, timeout); andre@0: } andre@0: andre@0: /* Methods for the top of the stack. Just call down to the next fd. */ andre@0: static PRIOMethods pl_methods = { andre@0: PR_DESC_LAYERED, andre@0: pl_TopClose, andre@0: pl_DefRead, andre@0: pl_DefWrite, andre@0: pl_DefAvailable, andre@0: pl_DefAvailable64, andre@0: pl_DefFsync, andre@0: pl_DefSeek, andre@0: pl_DefSeek64, andre@0: pl_DefFileInfo, andre@0: pl_DefFileInfo64, andre@0: pl_DefWritev, andre@0: pl_DefConnect, andre@0: pl_TopAccept, andre@0: pl_DefBind, andre@0: pl_DefListen, andre@0: pl_DefShutdown, andre@0: pl_DefRecv, andre@0: pl_DefSend, andre@0: pl_DefRecvfrom, andre@0: pl_DefSendto, andre@0: pl_DefPoll, andre@0: pl_DefAcceptread, andre@0: pl_DefTransmitfile, andre@0: pl_DefGetsockname, andre@0: pl_DefGetpeername, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: pl_DefGetsocketoption, andre@0: pl_DefSetsocketoption, andre@0: pl_DefSendfile, andre@0: pl_DefConnectcontinue, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt, andre@0: (PRReservedFN)_PR_InvalidInt andre@0: }; andre@0: andre@0: PR_IMPLEMENT(const PRIOMethods*) PR_GetDefaultIOMethods(void) andre@0: { andre@0: return &pl_methods; andre@0: } /* PR_GetDefaultIOMethods */ andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_CreateIOLayerStub( andre@0: PRDescIdentity ident, const PRIOMethods *methods) andre@0: { andre@0: PRFileDesc *fd = NULL; andre@0: PR_ASSERT((PR_NSPR_IO_LAYER != ident) && (PR_TOP_IO_LAYER != ident)); andre@0: if ((PR_NSPR_IO_LAYER == ident) || (PR_TOP_IO_LAYER == ident)) andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: else andre@0: { andre@0: fd = PR_NEWZAP(PRFileDesc); andre@0: if (NULL == fd) andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: else andre@0: { andre@0: fd->methods = methods; andre@0: fd->dtor = pl_FDDestructor; andre@0: fd->identity = ident; andre@0: } andre@0: } andre@0: return fd; andre@0: } /* PR_CreateIOLayerStub */ andre@0: andre@0: /* andre@0: * PR_CreateIOLayer andre@0: * Create a new style stack, where the stack top is a dummy header. andre@0: * Unlike the old style stacks, the contents of the stack head andre@0: * are not modified when a layer is pushed onto or popped from a new andre@0: * style stack. andre@0: */ andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_CreateIOLayer(PRFileDesc *top) andre@0: { andre@0: PRFileDesc *fd = NULL; andre@0: andre@0: fd = PR_NEWZAP(PRFileDesc); andre@0: if (NULL == fd) andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: else andre@0: { andre@0: fd->methods = &pl_methods; andre@0: fd->dtor = pl_FDDestructor; andre@0: fd->identity = PR_IO_LAYER_HEAD; andre@0: fd->higher = NULL; andre@0: fd->lower = top; andre@0: top->higher = fd; andre@0: top->lower = NULL; andre@0: } andre@0: return fd; andre@0: } /* PR_CreateIOLayer */ andre@0: andre@0: /* andre@0: * _PR_DestroyIOLayer andre@0: * Delete the stack head of a new style stack. andre@0: */ andre@0: andre@0: static PRStatus _PR_DestroyIOLayer(PRFileDesc *stack) andre@0: { andre@0: if (NULL == stack) andre@0: return PR_FAILURE; andre@0: else { andre@0: PR_DELETE(stack); andre@0: return PR_SUCCESS; andre@0: } andre@0: } /* _PR_DestroyIOLayer */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_PushIOLayer( andre@0: PRFileDesc *stack, PRDescIdentity id, PRFileDesc *fd) andre@0: { andre@0: PRFileDesc *insert = PR_GetIdentitiesLayer(stack, id); andre@0: andre@0: PR_ASSERT(fd != NULL); andre@0: PR_ASSERT(stack != NULL); andre@0: PR_ASSERT(insert != NULL); andre@0: PR_ASSERT(PR_IO_LAYER_HEAD != id); andre@0: if ((NULL == stack) || (NULL == fd) || (NULL == insert)) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: andre@0: if (stack == insert) andre@0: { andre@0: /* going on top of the stack */ andre@0: /* old-style stack */ andre@0: PRFileDesc copy = *stack; andre@0: *stack = *fd; andre@0: *fd = copy; andre@0: fd->higher = stack; andre@0: if (fd->lower) andre@0: { andre@0: PR_ASSERT(fd->lower->higher == stack); andre@0: fd->lower->higher = fd; andre@0: } andre@0: stack->lower = fd; andre@0: stack->higher = NULL; andre@0: } else { andre@0: /* andre@0: * going somewhere in the middle of the stack for both old and new andre@0: * style stacks, or going on top of stack for new style stack andre@0: */ andre@0: fd->lower = insert; andre@0: fd->higher = insert->higher; andre@0: andre@0: insert->higher->lower = fd; andre@0: insert->higher = fd; andre@0: } andre@0: andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_PopIOLayer(PRFileDesc *stack, PRDescIdentity id) andre@0: { andre@0: PRFileDesc *extract = PR_GetIdentitiesLayer(stack, id); andre@0: andre@0: PR_ASSERT(0 != id); andre@0: PR_ASSERT(NULL != stack); andre@0: PR_ASSERT(NULL != extract); andre@0: if ((NULL == stack) || (0 == id) || (NULL == extract)) andre@0: { andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return NULL; andre@0: } andre@0: andre@0: if (extract == stack) { andre@0: /* popping top layer of the stack */ andre@0: /* old style stack */ andre@0: PRFileDesc copy = *stack; andre@0: extract = stack->lower; andre@0: *stack = *extract; andre@0: *extract = copy; andre@0: stack->higher = NULL; andre@0: if (stack->lower) { andre@0: PR_ASSERT(stack->lower->higher == extract); andre@0: stack->lower->higher = stack; andre@0: } andre@0: } else if ((PR_IO_LAYER_HEAD == stack->identity) && andre@0: (extract == stack->lower) && (extract->lower == NULL)) { andre@0: /* andre@0: * new style stack andre@0: * popping the only layer in the stack; delete the stack too andre@0: */ andre@0: stack->lower = NULL; andre@0: _PR_DestroyIOLayer(stack); andre@0: } else { andre@0: /* for both kinds of stacks */ andre@0: extract->lower->higher = extract->higher; andre@0: extract->higher->lower = extract->lower; andre@0: } andre@0: extract->higher = extract->lower = NULL; andre@0: return extract; andre@0: } /* PR_PopIOLayer */ andre@0: andre@0: #define ID_CACHE_INCREMENT 16 andre@0: typedef struct _PRIdentity_cache andre@0: { andre@0: PRLock *ml; andre@0: char **name; andre@0: PRIntn length; andre@0: PRDescIdentity ident; andre@0: } _PRIdentity_cache; andre@0: andre@0: static _PRIdentity_cache identity_cache; andre@0: andre@0: PR_IMPLEMENT(PRDescIdentity) PR_GetUniqueIdentity(const char *layer_name) andre@0: { andre@0: PRDescIdentity identity, length; andre@0: char **names = NULL, *name = NULL, **old = NULL; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: PR_ASSERT((PRDescIdentity)0x7fff > identity_cache.ident); andre@0: andre@0: if (NULL != layer_name) andre@0: { andre@0: name = (char*)PR_Malloc(strlen(layer_name) + 1); andre@0: if (NULL == name) andre@0: { andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return PR_INVALID_IO_LAYER; andre@0: } andre@0: strcpy(name, layer_name); andre@0: } andre@0: andre@0: /* this initial code runs unsafe */ andre@0: retry: andre@0: PR_ASSERT(NULL == names); andre@0: /* andre@0: * In the initial round, both identity_cache.ident and andre@0: * identity_cache.length are 0, so (identity_cache.ident + 1) is greater andre@0: * than length. In later rounds, identity_cache.ident is always less andre@0: * than length, so (identity_cache.ident + 1) can be equal to but cannot andre@0: * be greater than length. andre@0: */ andre@0: length = identity_cache.length; andre@0: if ((identity_cache.ident + 1) >= length) andre@0: { andre@0: length += ID_CACHE_INCREMENT; andre@0: names = (char**)PR_CALLOC(length * sizeof(char*)); andre@0: if (NULL == names) andre@0: { andre@0: if (NULL != name) PR_DELETE(name); andre@0: PR_SetError(PR_OUT_OF_MEMORY_ERROR, 0); andre@0: return PR_INVALID_IO_LAYER; andre@0: } andre@0: } andre@0: andre@0: /* now we get serious about thread safety */ andre@0: PR_Lock(identity_cache.ml); andre@0: PR_ASSERT(identity_cache.length == 0 || andre@0: identity_cache.ident < identity_cache.length); andre@0: identity = identity_cache.ident + 1; andre@0: if (identity >= identity_cache.length) /* there's no room */ andre@0: { andre@0: /* we have to do something - hopefully it's already done */ andre@0: if ((NULL != names) && (identity < length)) andre@0: { andre@0: /* what we did is still okay */ andre@0: memcpy( andre@0: names, identity_cache.name, andre@0: identity_cache.length * sizeof(char*)); andre@0: old = identity_cache.name; andre@0: identity_cache.name = names; andre@0: identity_cache.length = length; andre@0: names = NULL; andre@0: } andre@0: else andre@0: { andre@0: PR_Unlock(identity_cache.ml); andre@0: if (NULL != names) PR_DELETE(names); andre@0: goto retry; andre@0: } andre@0: } andre@0: if (NULL != name) /* there's a name to be stored */ andre@0: { andre@0: identity_cache.name[identity] = name; andre@0: } andre@0: identity_cache.ident = identity; andre@0: PR_ASSERT(identity_cache.ident < identity_cache.length); andre@0: PR_Unlock(identity_cache.ml); andre@0: andre@0: if (NULL != old) PR_DELETE(old); andre@0: if (NULL != names) PR_DELETE(names); andre@0: andre@0: return identity; andre@0: } /* PR_GetUniqueIdentity */ andre@0: andre@0: PR_IMPLEMENT(const char*) PR_GetNameForIdentity(PRDescIdentity ident) andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: if (PR_TOP_IO_LAYER == ident) return NULL; andre@0: andre@0: PR_ASSERT(ident <= identity_cache.ident); andre@0: return (ident > identity_cache.ident) ? NULL : identity_cache.name[ident]; andre@0: } /* PR_GetNameForIdentity */ andre@0: andre@0: PR_IMPLEMENT(PRDescIdentity) PR_GetLayersIdentity(PRFileDesc* fd) andre@0: { andre@0: PR_ASSERT(NULL != fd); andre@0: if (PR_IO_LAYER_HEAD == fd->identity) { andre@0: PR_ASSERT(NULL != fd->lower); andre@0: return fd->lower->identity; andre@0: } else andre@0: return fd->identity; andre@0: } /* PR_GetLayersIdentity */ andre@0: andre@0: PR_IMPLEMENT(PRFileDesc*) PR_GetIdentitiesLayer(PRFileDesc* fd, PRDescIdentity id) andre@0: { andre@0: PRFileDesc *layer = fd; andre@0: andre@0: if (PR_TOP_IO_LAYER == id) { andre@0: if (PR_IO_LAYER_HEAD == fd->identity) andre@0: return fd->lower; andre@0: else andre@0: return fd; andre@0: } andre@0: andre@0: for (layer = fd; layer != NULL; layer = layer->lower) andre@0: { andre@0: if (id == layer->identity) return layer; andre@0: } andre@0: for (layer = fd; layer != NULL; layer = layer->higher) andre@0: { andre@0: if (id == layer->identity) return layer; andre@0: } andre@0: return NULL; andre@0: } /* PR_GetIdentitiesLayer */ andre@0: andre@0: void _PR_InitLayerCache(void) andre@0: { andre@0: memset(&identity_cache, 0, sizeof(identity_cache)); andre@0: identity_cache.ml = PR_NewLock(); andre@0: PR_ASSERT(NULL != identity_cache.ml); andre@0: } /* _PR_InitLayerCache */ andre@0: andre@0: void _PR_CleanupLayerCache(void) andre@0: { andre@0: if (identity_cache.ml) andre@0: { andre@0: PR_DestroyLock(identity_cache.ml); andre@0: identity_cache.ml = NULL; andre@0: } andre@0: andre@0: if (identity_cache.name) andre@0: { andre@0: PRDescIdentity ident; andre@0: andre@0: for (ident = 0; ident <= identity_cache.ident; ident++) andre@0: PR_DELETE(identity_cache.name[ident]); andre@0: andre@0: PR_DELETE(identity_cache.name); andre@0: } andre@0: } /* _PR_CleanupLayerCache */ andre@0: andre@0: /* prlayer.c */