/*-------------------------------------------------------------------- */ void efss_createfilecb( char *name, unsigned int length, unsigned char *pData, EFSFUN pllDestroyFun, unsigned int MemMgrArg ) { FILEHEADER *pfh; /* Allocate the file header structure */ pfh = mmAlloc( sizeof(FILEHEADER) ); if( !pfh ) return; pfh->Type = HTYPE_EFSFILEHEADER; /* Record the filename, size, and data pointer */ strncpy( pfh->Filename, name, EFSS_FILENAME_MAX-1 ); pfh->Filename[EFSS_FILENAME_MAX-1] = 0; pfh->Length = length; pfh->RefCount = 0; pfh->pData = pData; pfh->Flags = 0; pfh->pMemMgrCallback = pllDestroyFun; pfh->MemMgrArg = MemMgrArg; llEnter(); /* Put it in our list */ pfh->pNext = pRoot; pRoot = pfh; llExit(); }
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 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 ); }
/*-------------------------------------------------------------------- */ EFSS_FILE *efss_fopen( char *name, char *mode ) { FILEHEADER *pfh; FILEPTR *pf; /* Verify the read mode */ if( stricmp( mode, "rb" ) ) return(0); llEnter(); pfh = efss_findfile( name ); if( !pfh ) { llExit(); return(0); } /* Allocate the file header structure */ pf = mmAlloc( sizeof(FILEPTR) ); if( !pf ) { llExit(); return(0); } pf->Type = HTYPE_EFSFILEPOINTER; pf->pfh = pfh; pf->Pos = 0; pfh->RefCount++; llExit(); return( pf ); }
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 ); }