void * mmRealloc( void* adr, size_t newsize ) { size_t oldsize = *((size_t*)(adr)-1); void* newadr = mmAlloc( newsize ); memcpy( newadr, adr, (oldsize < newsize) ? oldsize : newsize ); mmFree( adr ); return newadr; }
static void prng_slow_init(PRNG *p) /* This fails silently and must be fixed. */ { YSHA1_CTX* ctx = NULL; MMPTR mmctx = MM_NULL; BYTE* bigbuf = NULL; MMPTR mmbigbuf = MM_NULL; BYTE* buf = NULL; MMPTR mmbuf = MM_NULL; DWORD polllength; mmbigbuf = mmMalloc(SPLEN); if(mmbigbuf == MM_NULL) {goto cleanup_slow_init;} bigbuf = (BYTE*)mmGetPtr(mmbigbuf); mmbuf = mmMalloc(20); if(mmbuf == MM_NULL) {goto cleanup_slow_init;} buf = (BYTE*)mmGetPtr(mmbuf); mmctx = mmMalloc(sizeof(YSHA1_CTX)); if(mmctx == MM_NULL) {goto cleanup_slow_init;} ctx = (YSHA1_CTX*)mmGetPtr(mmctx); /* Initialize the secret state. */ /* Init entropy pool */ YSHA1Init(&p->pool); /* Init output generator */ polllength = prng_slow_poll(bigbuf,SPLEN); YSHA1Init(ctx); YSHA1Update(ctx,bigbuf,polllength); YSHA1Final(buf,ctx); prng_make_new_state(&p->outstate, buf); cleanup_slow_init: mmFree(mmctx); mmFree(mmbigbuf); mmFree(mmbuf); return; }
void mmSABListDestory() { if(NULL == goHead) return; mmSABirdFly *t; t = goHead; while(goHead) { goHead = goHead->next; mmFree(t); t = goHead; } goHead = NULL; t = NULL; }
static void dchild6( DREC *pdr, CHILD *pc ) { int rc; // Get the lock to synchronize with parent SemPend( hDSem6, SEM_FOREVER ); SemPost( hDSem6 ); // This function returns "1" if the socket is still open, // and "0" is the socket has been closed. rc = pdr->pCb( pc->s, pdr->Argument ); // Close the socket if we need to // We do this before we get the lock so if the socket // uses LINGER, we don't hold everyone up if( rc && pc->closeSock ) fdClose( pc->s ); // Get our lock SemPend( hDSem6, SEM_FOREVER ); // Close the socket session (if open) if( pc->hTask ) fdCloseSession( pc->hTask ); // Remove our record from the DREC if( pc->pNext ) pc->pNext->pPrev = pc->pPrev; if( !pc->pPrev ) pdr->pC = pc->pNext; else pc->pPrev->pNext = pc->pNext; pdr->TasksSpawned--; // Free our record mmFree( pc ); // Kick the parent thread if( hDTask6 ) fdSelectAbort( hDTask6 ); // Release the lock SemPost( hDSem6 ); TaskExit(); }
/*-------------------------------------------------------------------- */ int efss_fclose( EFSS_FILE *stream ) { FILEPTR *pf = (FILEPTR *)stream; FILEHEADER *pfh = pf->pfh; llEnter(); /* Zap type for debug */ pf->Type = 0; mmFree( pf ); pfh->RefCount--; if( !pfh->RefCount && (pfh->Flags & EFSFLG_DESTROY_PENDING) ) efss_internal_remove( pfh ); llExit(); return(0); }
// ************************************************************************************** static inline int_least32_t ConStrToIPN( uint_least8_t *str, IPN *pIPN ) { uint_least8_t *buffer; struct in_addr in1; int_least32_t retcode = 0; // If the string is an IP, we're done if( inet_aton( (char *) str, &in1 ) ) { *pIPN = in1.s_addr; return(1); } // All the DNS functions need a scrap buffer buffer = mmAlloc( 512 ); if( buffer ) { // We can treat buffer as a HOSTENT structure after // DNSGetHostByXxx calls HOSTENT *phe = (HOSTENT *)buffer; retcode = DNSGetHostByName( (char *) str, buffer, 512 ); if( !retcode && phe->h_addrcnt ) { #ifndef _INCLUDE_IPv6_CODE *pIPN = phe->h_addr[0]; #else pIPN = (IPN *)phe->h_addr_list[0]; #endif retcode = 1; } else retcode = 0; mmFree( buffer ); } return( retcode ); }
/*-------------------------------------------------------------------- */ static void efss_internal_remove( FILEHEADER *pfh ) { FILEHEADER *pfhTmp; FILEHEADER *pfhPrev; pfhTmp = pRoot; pfhPrev = 0; while( pfhTmp ) { if( pfhTmp == pfh ) break; else { pfhPrev = pfhTmp; pfhTmp = pfhTmp->pNext; } } if( !pfhTmp ) return; /* Remove */ /* pfh is entry to remove */ if( !pfhPrev ) pRoot = pfh->pNext; else pfhPrev->pNext = pfh->pNext; /* Zap type for debug */ pfh->Type = 0; if( pfh->pMemMgrCallback ) (pfh->pMemMgrCallback)( pfh->MemMgrArg ); mmFree( pfh ); }
/* Set up the PRNG */ prng_error_status prngInitialize(PrngRef *prng) { UINT i; comp_error_status resp; prng_error_status retval = PRNG_ERR_LOW_MEMORY; MMPTR mmp; PRNG *p; mmInit(); #if MUTEX_ENABLE /* Create the mutex */ /* NOTE: on return the mutex should bve held, since our caller (prngInitialize) * will release it. */ if(mutexCreatorId!=0) {return PRNG_ERR_REINIT;} Statmutex = CreateMutex(NULL,TRUE,NULL); if(Statmutex == NULL) {mutexCreatorId = 0; return PRNG_ERR_MUTEX;} DuplicateHandle(GetCurrentProcess(),Statmutex,GetCurrentProcess(),&mutex,SYNCHRONIZE,FALSE,0); mutexCreatorId = GetCurrentProcessId(); #endif /* MUTEX_ENABLE */ /* Assign memory */ mmp = mmMalloc(sizeof(PRNG)); if(mmp==MM_NULL) { goto cleanup_init; } else { p = (PRNG*)mmGetPtr(mmp); memset(p, 0, sizeof(PRNG)); } /* Initialize Variables */ for(i=0;i<TOTAL_SOURCES;i++) { p->poolSize[i] = 0; p->poolEstBits[i] = 0; } #ifdef WIN_NT /* Setup security on the registry so that remote users cannot predict the slow pool */ prng_set_NT_security(); #endif /* Initialize the secret state. */ /* FIXME - might want to make this an option here and have the caller * do it after we return....? */ YSHA1Init(&p->pool); #if SLOW_POLL_ENABLE prng_slow_init(p); /* Does a slow poll and then calls prng_make_state(...) */ #else /* NULL init */ prng_do_SHA1(&p->outstate); prng_make_new_state(&p->outstate, p->outstate.out); #endif /* SLOW_POLL_ENABLE */ /* Initialize compression routines */ for(i=0;i<COMP_SOURCES;i++) { resp = comp_init((p->comp_state)+i); if(resp!=COMP_SUCCESS) {retval = PRNG_ERR_COMPRESSION; goto cleanup_init;} } p->ready = PRNG_READY; *prng = (PrngRef)p; return PRNG_SUCCESS; cleanup_init: /* Program failed on one of the mmmallocs */ mmFree(mmp); mmp = MM_NULL; #if MUTEX_ENABLE CloseHandle(Statmutex); Statmutex = NULL; mutexCreatorId = 0; #endif return retval; /* default PRNG_ERR_LOW_MEMORY */ }
static void daemon6() { int i,closeSock; struct sockaddr_in6 sin1; SOCKET tsock; CHILD* pc; // Enter our lock SemPend( hDSem6, SEM_FOREVER ); for(;;) { // // Create any socket that needs to be created // for( i=0; i<DAEMON_MAXRECORD; i++ ) { if( drec6[i].Type && drec6[i].s == INVALID_SOCKET ) { // Create UDP or TCP as needed if( drec6[i].Type == SOCK_DGRAM ) drec6[i].s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); else drec6[i].s = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); // If the socket was created, bind it if( drec6[i].s != INVALID_SOCKET ) { // Bind to the specified Server port bzero( &sin1, sizeof(struct sockaddr_in) ); sin1.sin6_family = AF_INET6; memcpy((void *)&sin1.sin6_addr,(void *)&drec6[i].LocalAddress, sizeof(struct in6_addr)); sin1.sin6_port = htons(drec6[i].LocalPort); if( bind( drec6[i].s,(struct sockaddr *)&sin1, sizeof(sin1) ) < 0 ) { fdClose( drec6[i].s ); drec6[i].s = INVALID_SOCKET; } } // If the socket is bound and TCP, start listening if( drec6[i].s != INVALID_SOCKET && drec6[i].Type != SOCK_DGRAM ) { if( listen( drec6[i].s, drec6[i].MaxSpawn ) < 0 ) { fdClose( drec6[i].s ); drec6[i].s = INVALID_SOCKET; } } } // Update the fdPoll array pollitem6[i].fd = drec6[i].s; if( drec6[i].Type && drec6[i].TasksSpawned < drec6[i].MaxSpawn ) pollitem6[i].eventsRequested = POLLIN; else pollitem6[i].eventsRequested = 0; } // Leave our lock SemPost( hDSem6 ); // Poll with a timeout of 10 second - to try and catch // synchronization error if( fdPoll( pollitem6, DAEMON_MAXRECORD, 10000 ) == SOCKET_ERROR ) break; // Enter our lock SemPend( hDSem6, SEM_FOREVER ); // // Spawn tasks for any active sockets // for( i=0; i<DAEMON_MAXRECORD; i++ ) { // If no poll results or the drec6 has been freed, skip it if( !pollitem6[i].eventsDetected || !drec6[i].Type ) continue; // If the socket is invalid, close it if( pollitem6[i].eventsDetected & POLLNVAL ) { fdClose( drec6[i].s); drec6[i].s = INVALID_SOCKET; continue; } if( pollitem6[i].eventsDetected & POLLIN ) { if( drec6[i].Type == SOCK_DGRAM ) { tsock = drec6[i].s; closeSock = 0; } else { tsock = accept( drec6[i].s, 0, 0 ); closeSock = 1; } if( tsock != INVALID_SOCKET ) { // Create a record to track this task pc = mmAlloc( sizeof(CHILD) ); if( !pc ) goto spawnComplete; // Create the task pc->hTask = TaskCreate( dchild6, "dchild6", drec6[i].Priority, drec6[i].StackSize, (UINT32)&drec6[i], (UINT32)pc, 0); if( !pc->hTask ) { mmFree( pc ); goto spawnComplete; } // Open a socket session for the child task fdOpenSession( pc->hTask ); // Fill in the rest of the child record pc->closeSock = closeSock; pc->s = tsock; // Now we won't close the socket here closeSock = 0; // Link this record onto the DREC drec6[i].TasksSpawned++; pc->pPrev = 0; pc->pNext = drec6[i].pC; drec6[i].pC = pc; if( pc->pNext ) pc->pNext->pPrev = pc; spawnComplete: // If there was an error, we may need to close the socket if( closeSock ) fdClose( tsock ); } } } } TaskExit(); }
// int NtTftpSend() // // Send a file using TFTP // // Return Conditions: // // In the following cases, FileSize is set to the actual file size: // // 1 - If file was sucessfully transferred // 0 - ?? // -1 - failed // // In the following cases, FileSize is set to the actual number of // bytes copied. // // <0 - Error // TFTPERROR_ERRORCODE: TFTP server error code. The error code // is written to pErrorCode, and an error message is // written to FileBuffer. The length of the error message // is written to FileSize. // int NtTftpSendLocal( UINT32 TftpIP, char *szFileName, char *FileBuffer, UINT32 *FileSize, UINT16 *pErrorCode ) { TFTP *pTftp; int rc; // Return Code // Quick parameter validation if( !szFileName || !FileSize || (*FileSize != 0 && !FileBuffer) ) return( TFTPERROR_BADPARAM ); // Malloc Parameter Structure if( !(pTftp = mmAlloc( sizeof(TFTP) )) ) return( TFTPERROR_RESOURCES ); // Initialize parameters to "NULL" bzero( pTftp, sizeof(TFTP) ); pTftp->Sock = INVALID_SOCKET; // Malloc Packet Data Buffer if( !(pTftp->PacketBuffer = mmAlloc( DATA_SIZE )) ) { rc = TFTPERROR_RESOURCES; goto ABORT2; } // store IP in network byte order pTftp->PeerAddress = TftpIP; // Setup initial socket rc = tftpSocketSetup( pTftp ); if( rc < 0 ) goto ABORT2; // Socket now registered and available for use. Get the data pTftp->szFileName = szFileName; pTftp->Buffer = FileBuffer; pTftp->BufferSize = *FileSize; // Do not read more than can fit in file pTftp->BufferUsed = *FileSize; // PUT the requested file rc = tftpPutFile( pTftp ); if( rc < 0 ) { // ERROR CONDITION // Set the "FileSize" to the actual number of bytes copied //*FileSize = pTftp->BufferUsed; // If the ErrorCode pointer is valid, copy it if( pErrorCode ) *pErrorCode = pTftp->ErrorCode; } else { // SUCCESS CONDITION // Set the "FileSize" to the file size (regardless of bytes copied) //*FileSize = pTftp->FileSize; } ABORT2: if( pTftp->Sock != INVALID_SOCKET ) fdClose( pTftp->Sock ); if( pTftp->PacketBuffer ) mmFree( pTftp->PacketBuffer ); mmFree( pTftp ); return(rc); }
/*------------------------------------------------------------------------- */ int httpsClientProcess( SOCKET Sock, WOLFSSL *ssl) { HTTPS_MSG *pMsg = 0; int nMethod; int rc = 0; int length,i,realmidx; int (*PostFunction)(WOLFSSL *, int, unsigned char *) = 0; int fCGI = 0; int termstrSizeRemaining = sizeof(pMsg->termstr); int parseSizeRemaining = sizeof(pMsg->parsestr); int requestSizeRemaining = sizeof(pMsg->RequestedFile); struct timeval to; struct linger lgr; /* Init the socket parameters */ lgr.l_onoff = 1; lgr.l_linger = 5; rc = setsockopt((int)Sock, SOL_SOCKET, SO_LINGER, &lgr, sizeof( lgr )); if (rc < 0) { goto SHUTDOWN; } /* Configure our socket timeout to be 10 seconds */ to.tv_sec = 20; to.tv_usec = 0; rc = setsockopt( (int)Sock, SOL_SOCKET, SO_SNDTIMEO, &to, sizeof( to ) ); if (rc < 0) { goto SHUTDOWN; } rc = setsockopt( (int)Sock, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof( to ) ); if (rc < 0) { goto SHUTDOWN; } /* Start the HTTP message processing */ pMsg = mmAlloc( sizeof(HTTPS_MSG) ); if( !pMsg ) goto SHUTDOWN; /* Init the message record */ pMsg->Sock = Sock; pMsg->ssl = ssl; pMsg->parsed = 0; pMsg->unparsed = 0; pMsg->flagreadall = 0; pMsg->length = 0; pMsg->PostContentLength = 0; pMsg->URIArgs = 0; pMsg->username[0] = 0; pMsg->password[0] = 0; if (strlen(CRLF) < termstrSizeRemaining) { strcpy(pMsg->termstr, CRLF ); termstrSizeRemaining = termstrSizeRemaining - strlen(CRLF); } else { /* Error constructing message, return failure */ goto SHUTDOWN; } if (strlen(CRLF) < termstrSizeRemaining) { strcat(pMsg->termstr, CRLF ); termstrSizeRemaining = termstrSizeRemaining - strlen(CRLF); } else { /* Error constructing message, return failure */ goto SHUTDOWN; } if (strlen(SPACE) < parseSizeRemaining) { strcpy(pMsg->parsestr, SPACE); parseSizeRemaining = parseSizeRemaining - strlen(SPACE); } else { /* Error constructing message, return failure */ goto SHUTDOWN; } /* Read the method */ rc = httpsParseRecv( pMsg ); if( rc<=0 ) { https400(ssl); goto SHUTDOWN; } nMethod = httpsExtractTag( pMsg->request ); /* Get URI */ rc = httpsParseRecv( pMsg ); if( rc<=0 ) { https400(ssl); goto SHUTDOWN; } strcpy( pMsg->URI, pMsg->request ); /* Setup for CRLF delimitor (CRLFCRLF is still terminator) */ if (strlen(CRLF) < parseSizeRemaining) { strcpy(pMsg->parsestr, CRLF); parseSizeRemaining = parseSizeRemaining - strlen(CRLF); } else { /* Error constructing message, return failure */ goto SHUTDOWN; } /* Scan all tags for tags we're interested in */ for(i=0;;i=1) { int nTag; rc = httpsParseRecv( pMsg ); if( rc < 0 ) { https400(ssl); goto SHUTDOWN; } if( rc == 0 ) break; /* A null tag after a tag is a CRLFCRLF */ if( i && !pMsg->request[0] ) break; /* Extract the tag */ nTag = httpsExtractTag( pMsg->request ); /* Process all the tag types we care about */ if( nTag == TAG_CLEN ) pMsg->PostContentLength = atoi(pMsg->request + 16); if( nTag == TAG_AUTH ) httpsGetAuthParams( pMsg, (unsigned char *)pMsg->request + 21 ); } /* Process the URI into RequestedFile */ length = strlen(pMsg->URI); /* Scan URI for a '?' and terminate at that point */ for(i=0;i<length;i++) if( pMsg->URI[i] == '?' ) { pMsg->URI[i] = 0; pMsg->URIArgs = pMsg->URI + i + 1; break; } /* Set the new string length (if any) */ length = i; /* Assume success from this point on */ rc = 1; /* If URI terminates with a '/', then add index.html, UNLESS */ /* length is 1, in which case replace '/' with index.html */ /* If URI does not terminate with a '/', use it as is */ /* Note: We always remove the leading '/' */ if( !length || (length == 1 && pMsg->URI[0] == '/') ) { if (strlen(DEFAULT_NAME) < requestSizeRemaining) { strcpy(pMsg->RequestedFile, DEFAULT_NAME); requestSizeRemaining = requestSizeRemaining - strlen(DEFAULT_NAME); } else { /* Error constructing message, return failure */ goto SHUTDOWN; } } else { if (strlen(pMsg->URI + 1) < requestSizeRemaining) { strcpy(pMsg->RequestedFile, pMsg->URI + 1); requestSizeRemaining = requestSizeRemaining - strlen(pMsg->URI + 1); if (pMsg->URI[length - 1] == '/') { if (strlen(DEFAULT_NAME) < requestSizeRemaining) { strcat(pMsg->RequestedFile, DEFAULT_NAME); requestSizeRemaining = requestSizeRemaining - strlen(DEFAULT_NAME); } else { /* Error constructing message, return failure */ goto SHUTDOWN; } } } else { /* Error constructing message, return failure */ goto SHUTDOWN; } } /* Authenticate the request */ i=efss_filecheck(pMsg->RequestedFile,pMsg->username,pMsg->password,&realmidx); /* Act on filecheck status code */ if( i & EFS_FC_NOTFOUND ) { https404(ssl); goto SHUTDOWN; } if( i & EFS_FC_AUTHFAILED ) { httpsAuthenticationReq( ssl, realmidx ); goto SHUTDOWN; } if( i & EFS_FC_NOTALLOWED ) { https405(ssl); goto SHUTDOWN; } if( i & EFS_FC_EXECUTE ) { fCGI = 1; PostFunction = (int(*)(WOLFSSL *, int, unsigned char *)) efss_loadfunction(pMsg->RequestedFile); } /* Execute based on the method and fCGI flag */ switch (nMethod) { case TAG_GET: if( !fCGI ) { httpsSendFullResponse( ssl, HTTP_OK, pMsg->RequestedFile ); } else { if( !PostFunction ) { https404(ssl); /* function does not exist */ } else { /* Now call Post function using the "get" syntax */ if( !PostFunction( ssl, 0, (unsigned char *)pMsg->URIArgs ) ) { rc = 0; goto SHUTDOWN_postclose; } } } break; case TAG_POST: if( !fCGI ) https405(ssl); /* file is not a CGI */ else if( !PostFunction ) https404(ssl); /* function does not exist */ /* Now call Post function using "post" syntax */ else if( !PostFunction( ssl, pMsg->PostContentLength, (unsigned char *)pMsg->URIArgs ) ) { rc = 0; goto SHUTDOWN_postclose; } break; default: https501( ssl ); /* Not Implemented */ break; } SHUTDOWN: #if 0 /* Close socket on error */ if( rc <= 0 ) fdClose( Sock ); #endif SHUTDOWN_postclose: /* Free buffer if we allocated one */ if( pMsg ) mmFree( pMsg ); if( rc < 0 ) rc = 0; return( rc ); }