int DTLSBio::sslRead(char* buf, int bufLen) { /// 1. Read available data from the external input (probably libnice) /// If packet is not a DTLS packet, return the read value, else goto 2 /// 2. Store (encrypted) read data into the input BIO using BIO_write /// 3. Call SSL_read to decrypt the data /// Returns return value of SSL_read /// if (!_pSSL) { OSS_LOG_ERROR("DTLSBio::sslRead - _pSSL is not set."); return 0; } if (!SSL_is_init_finished(_pSSL)) { OSS_LOG_ERROR("DTLSBio::sslRead - SSL Handshake is not yet completed."); return 0; } int ret = 0; char readBuf[DTLS_BIO_BUFFER_LEN]; ret = readDirect(readBuf, DTLS_BIO_BUFFER_LEN); if (ret > 0) { // // We have read some packets // if (DTLSSession::peek(readBuf) != DTLSSession::DTLS) { // // Not a DTLS packet. return the raw packet // if (ret > bufLen) { // // We have read a packet that is bigger than the buffer provided // we do not have any better option but to truncate // OSS_LOG_WARNING("DTLSBio::sslRead - buffer size is smaller than the packet in the buffer. Packet will be truncated!"); ret = bufLen; } memcpy(buf, readBuf, ret); return ret; } ret = BIO_write(_pInBIO, readBuf, ret); if (ret > 0) { // // Packets are written to the IN BIO. Read it back unencrypted. // ret = SSL_read(_pSSL, buf, bufLen); } } return ret; }
static void getBucket(EHTFILE * file, unsigned recordPos) { unsigned entry = recordPos % 512; unsigned bucketPos; if ( file->cache->tablePos[entry] == recordPos ) { if ( file->bucketNumber != file->cache->bucketPos[entry] ) { file->bucketNumber = file->cache->bucketPos[entry]; readDirect(file->bucket, file->bucketFile, file->bucketNumber); } } else { readDirect(&bucketPos, file->bucketIndex, recordPos); if ( bucketPos != file->bucketNumber ) { file->bucketNumber = bucketPos; readDirect(file->bucket, file->bucketFile, bucketPos); } file->cache->bucketPos[entry] = bucketPos; file->cache->tablePos[entry] = recordPos; } }
quint8 MBC3Mapper::readExRam(quint16 address) { if (m_ramEnable) { if (m_ramBank != -1) return readDirect(address); switch (m_clockRegister) { case 0x08: return m_latchSeconds; break; case 0x09: return m_latchMinutes; break; case 0x0a: return m_latchHours; break; case 0x0b: return m_latchDays; break; case 0x0c: return m_latchControl; break; } } return 0xff; }
static void doubleHashTable(EHTFILE * file) { unsigned i; if ( file->tableSize < 512 ) { /* update cache to reflect bucket index */ for ( i = 0; i < file->tableSize; ++i ) { file->cache->bucketPos[i + file->tableSize] = file->cache->bucketPos[i]; file->cache->tablePos[i + file->tableSize] = file->cache->tablePos[i]; writeDirect(&(file->cache->bucketPos[i]), file->bucketIndex, i + file->tableSize); } } else { unsigned bucketPos; for ( i = 0; i < file->tableSize; ++i ) { readDirect(&bucketPos, file->bucketIndex, i); writeDirect(&bucketPos, file->bucketIndex, i + file->tableSize); } } file->tableSize *= 2; writeHeader(NULL, file->bucketIndex); }
int DTLSBio::accept() { // // Read from the external source // int ret = 0; char readBuf[DTLS_BIO_BUFFER_LEN]; while (!SSL_is_init_finished(_pSSL)) { ret = readDirect(readBuf, DTLS_BIO_BUFFER_LEN); if (ret > 0) { // // We have read request from the client // Write it to the input BIO // ret = BIO_write(_pInBIO, readBuf, ret); if (ret > 0) { // // Packets are written to the IN BIO. call the handshake to process // the request // SSL_do_handshake(_pSSL); // // Read the response from the out BIO // ret = BIO_read(_pOutBIO, readBuf, DTLS_BIO_BUFFER_LEN); if (ret > 0) { // // We have read the encrypted data, write it to our external output // ret = writeDirect(readBuf, ret); if (ret <= 0) { OSS_LOG_ERROR("DTLSBio::accept - writeDirect returned " << ret); break; } } else { OSS_LOG_ERROR("DTLSBio::connect - BIO_read returned " << ret); break; } } else { OSS_LOG_ERROR("DTLSBio::accept - BIO_write returned " << ret); break; } } else { OSS_LOG_ERROR("DTLSBio::accept - readDirect returned " << ret); if (ret == 0) { // // No data returned by socket but is not an error. Retry // continue; } else { break; } } } if (SSL_is_init_finished(_pSSL)) return 1; else return 0; }
int DTLSBio::connect() { /// call the client handshake /// 1. Call SSL_do_handshake to start the handshake procedure /// 2. Read the ClientHello data from the output BIO and send to external output /// 3. Read the ServerHelloDone from external input and write it to the input BIO /// 4. Call SSL_do_handshake again to process ServerHelloDone /// 5. Repeat 2-4 to complete Certificate exchange /// if (SSL_is_init_finished(_pSSL)) { OSS_LOG_ERROR("DTLSBio::connect - SSL Handshake is already completed."); return 0; } // // Start the handshake // SSL_do_handshake(_pSSL); int ret = 0; char readBuf[DTLS_BIO_BUFFER_LEN]; int resendCount = 0; while (!SSL_is_init_finished(_pSSL)) { int pending = BIO_ctrl_pending(_pOutBIO); if (pending > 0) { ret = BIO_read(_pOutBIO, readBuf, DTLS_BIO_BUFFER_LEN); if (ret > 0) { // // We have read the encrypted data, write it to our external output // ret = writeDirect(readBuf, ret); if (ret <= 0) { OSS_LOG_ERROR("DTLSBio::connect - writeDirect returned " << ret); break; } } else { OSS_LOG_ERROR("DTLSBio::connect - BIO_read returned " << ret); break; } } // // Read the response from the server // ret = readDirect(readBuf, DTLS_BIO_BUFFER_LEN); if (ret > 0) { // // We have read the response from the server // Write it to the input BIO // ret = BIO_write(_pInBIO, readBuf, ret); if (ret > 0) { // // Packets are written to the IN BIO. call the handshake again to process // the response // SSL_do_handshake(_pSSL); } else { OSS_LOG_ERROR("DTLSBio::connect - BIO_write returned " << ret); break; } } else { OSS_LOG_ERROR("DTLSBio::connect - readDirect returned " << ret); if (ret < 0) { // // This is an error. Abort immediately // break; } // // We are not able to read any response. We will try to retransmit // struct timeval timeout; if (DTLSv1_get_timeout(_pSSL, &timeout)) { OSS::UInt64 timeout_value = timeout.tv_sec*1000 + timeout.tv_usec/1000; if (timeout_value > 0) { if (resendCount > MAX_RETRY_COUNT) { break; } else { continue; } } else { ++resendCount; DTLSv1_handle_timeout(_pSSL); continue; } } else { break; } } resendCount = 0; } if (SSL_is_init_finished(_pSSL)) return 1; else return 0; }