DiskLoc DummyExtentManager::allocateExtent( OperationContext* txn, bool capped, int size, bool enforceQuota ) { size = quantizeExtentSize( size ); ExtentInfo info; info.data = static_cast<char*>( mongoMalloc( size ) ); info.length = size; DiskLoc loc( _extents.size(), 0 ); _extents.push_back( info ); Extent* e = getExtent( loc, false ); e->magic = Extent::extentSignature; e->myLoc = loc; e->xnext.Null(); e->xprev.Null(); e->length = size; e->firstRecord.Null(); e->lastRecord.Null(); return loc; }
WindowsCommandLine::WindowsCommandLine(int argc, wchar_t* argvW[], wchar_t* envpW[]) : _argv(NULL), _envp(NULL) { // Construct UTF-8 copy of arguments vector<string> utf8args; vector<size_t> utf8argLength; size_t blockSize = argc * sizeof(char*); size_t blockPtr = blockSize; for (int i = 0; i < argc; ++i) { utf8args.push_back( toUtf8String(argvW[i]) ); size_t argLength = utf8args[i].length() + 1; utf8argLength.push_back(argLength); blockSize += argLength; } _argv = static_cast<char**>(mongoMalloc(blockSize)); for (int i = 0; i < argc; ++i) { _argv[i] = reinterpret_cast<char*>(_argv) + blockPtr; strcpy_s(_argv[i], utf8argLength[i], utf8args[i].c_str()); blockPtr += utf8argLength[i]; } // Construct UTF-8 copy of environment strings size_t envCount = 0; wchar_t** envpWptr = &envpW[0]; while (*envpWptr++) { ++envCount; } vector<string> utf8envs; vector<size_t> utf8envLength; blockSize = (envCount + 1) * sizeof(char*); blockPtr = blockSize; for (size_t i = 0; i < envCount; ++i) { utf8envs.push_back( toUtf8String(envpW[i]) ); size_t envLength = utf8envs[i].length() + 1; utf8envLength.push_back(envLength); blockSize += envLength; } _envp = static_cast<char**>(mongoMalloc(blockSize)); size_t i; for (i = 0; i < envCount; ++i) { _envp[i] = reinterpret_cast<char*>(_envp) + blockPtr; strcpy_s(_envp[i], utf8envLength[i], utf8envs[i].c_str()); blockPtr += utf8envLength[i]; } _envp[i] = NULL; }
void NetworkInterfaceASIO::_recvMessageBody(AsyncOp* op) { // TODO: This error code should be more meaningful. std::error_code ec; // validate message length int len = op->header()->constView().getMessageLength(); if (len == 542393671) { LOG(3) << "attempt to access MongoDB over HTTP on the native driver port."; return _networkErrorCallback(op, ec); } else if (len == -1) { // TODO: An endian check is run after the client connects, we should // set that we've received the client's handshake LOG(3) << "Endian check received from client"; return _networkErrorCallback(op, ec); } else if (static_cast<size_t>(len) < sizeof(MSGHEADER::Value) || static_cast<size_t>(len) > MaxMessageSizeBytes) { warning() << "recv(): message len " << len << " is invalid. " << "Min " << sizeof(MSGHEADER::Value) << " Max: " << MaxMessageSizeBytes; return _networkErrorCallback(op, ec); } // validate response id uint32_t expectedId = op->toSend()->header().getId(); uint32_t actualId = op->header()->constView().getResponseTo(); if (actualId != expectedId) { LOG(3) << "got wrong response:" << " expected response id: " << expectedId << ", got response id: " << actualId; return _networkErrorCallback(op, ec); } int z = (len + 1023) & 0xfffffc00; invariant(z >= len); op->toRecv()->setData(reinterpret_cast<char*>(mongoMalloc(z)), true); MsgData::View mdView = op->toRecv()->buf(); // copy header data into master buffer int headerLen = sizeof(MSGHEADER::Value); memcpy(mdView.view2ptr(), op->header(), headerLen); int bodyLength = len - headerLen; invariant(bodyLength >= 0); // receive remaining data into md->data asio::async_read(op->connection()->sock(), asio::buffer(mdView.data(), bodyLength), [this, op, mdView](asio::error_code ec, size_t bytes) { if (op->canceled()) { return _completeOperation(op, kCanceledStatus); } if (ec) { LOG(3) << "error receiving message body"; return _networkErrorCallback(op, ec); } return _completedWriteCallback(op); }); }
void run() { if( 0 && debug ) { try { LogFile f("logfile_test"); void *p = mongoMalloc(16384); char *buf = (char*) p; buf += 4095; buf = (char*) (((size_t)buf)&(~0xfff)); memset(buf, 'z', 8192); buf[8190] = '\n'; buf[8191] = 'B'; buf[0] = 'A'; f.synchronousAppend(buf, 8192); f.synchronousAppend(buf, 8192); free(p); } catch(DBException& e ) { log() << "logfile.cpp test failed : " << e.what() << endl; throw; } } }
bool MessagingPort::recv(Message& m) { try { #ifdef MONGO_CONFIG_SSL again: #endif // mmm( log() << "* recv() sock:" << this->sock << endl; ) MSGHEADER::Value header; int headerLen = sizeof(MSGHEADER::Value); psock->recv((char*)&header, headerLen); int len = header.constView().getMessageLength(); if (len == 542393671) { // an http GET string msg = "It looks like you are trying to access MongoDB over HTTP on the native driver " "port.\n"; LOG(psock->getLogLevel()) << msg; std::stringstream ss; ss << "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type: " "text/plain\r\nContent-Length: " << msg.size() << "\r\n\r\n" << msg; string s = ss.str(); send(s.c_str(), s.size(), "http"); return false; } // If responseTo is not 0 or -1 for first packet assume SSL else if (psock->isAwaitingHandshake()) { #ifndef MONGO_CONFIG_SSL if (header.constView().getResponseTo() != 0 && header.constView().getResponseTo() != -1) { uasserted(17133, "SSL handshake requested, SSL feature not available in this build"); } #else if (header.constView().getResponseTo() != 0 && header.constView().getResponseTo() != -1) { uassert(17132, "SSL handshake received but server is started without SSL support", sslGlobalParams.sslMode.load() != SSLParams::SSLMode_disabled); setX509SubjectName( psock->doSSLHandshake(reinterpret_cast<const char*>(&header), sizeof(header))); psock->setHandshakeReceived(); goto again; } uassert(17189, "The server is configured to only allow SSL connections", sslGlobalParams.sslMode.load() != SSLParams::SSLMode_requireSSL); #endif // MONGO_CONFIG_SSL } if (static_cast<size_t>(len) < sizeof(MSGHEADER::Value) || static_cast<size_t>(len) > MaxMessageSizeBytes) { LOG(0) << "recv(): message len " << len << " is invalid. " << "Min " << sizeof(MSGHEADER::Value) << " Max: " << MaxMessageSizeBytes; return false; } psock->setHandshakeReceived(); int z = (len + 1023) & 0xfffffc00; verify(z >= len); MsgData::View md = reinterpret_cast<char*>(mongoMalloc(z)); ScopeGuard guard = MakeGuard(free, md.view2ptr()); verify(md.view2ptr()); memcpy(md.view2ptr(), &header, headerLen); int left = len - headerLen; psock->recv(md.data(), left); guard.Dismiss(); m.setData(md.view2ptr(), true); return true; } catch (const SocketException& e) { logger::LogSeverity severity = psock->getLogLevel(); if (!e.shouldPrint()) severity = severity.lessSevere(); LOG(severity) << "SocketException: remote: " << remote() << " error: " << e; m.reset(); return false; } }
BSONObj BSONObj::copy() const { char* storage = static_cast<char*>(mongoMalloc(sizeof(Holder) + objsize())); memcpy(storage + sizeof(Holder), objdata(), objsize()); return BSONObj::takeOwnership(storage); }