Loading src/aes.cpp +78 −22 Changes for src/aes.cpp: 78 added lines, 22 removed lines. Original line number Diff line number Diff line Loading @@ -25,8 +25,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> // For memcpy or related functions if needed #include <iomanip> #include "aes.h" Loading Loading @@ -81,24 +83,69 @@ const uint8_t RCON[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; // Logarithm table: log[i] = power 'x' such that g^x = i (g=0x03) const uint8_t LOG_TABLE[256] = { 0x00, 0x00, 0x19, 0x01, 0x32, 0x02, 0x1a, 0xc6, 0x4b, 0xc7, 0x1b, 0x68, 0x33, 0xee, 0xdf, 0x03, 0x64, 0x04, 0xde, 0x34, 0x35, 0x6a, 0x1c, 0x48, 0x69, 0x76, 0x1d, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x56, 0x05, 0xd3, 0x36, 0x5e, 0x6b, 0x71, 0x06, 0x40, 0x07, 0x86, 0x6c, 0x93, 0xd0, 0x08, 0x88, 0x6d, 0x97, 0x37, 0x8d, 0x96, 0x38, 0x6f, 0xad, 0x09, 0xb3, 0x39, 0x24, 0x98, 0x0a, 0xa3, 0x0b, 0x8f, 0x6e, 0x45, 0x81, 0x41, 0x22, 0x78, 0x9c, 0x7a, 0x1e, 0x49, 0xc1, 0x42, 0x5f, 0x59, 0x0c, 0xb6, 0x43, 0x44, 0x7f, 0x27, 0x82, 0x23, 0x73, 0xb7, 0x4e, 0x83, 0x2b, 0x84, 0x7e, 0x21, 0xd2, 0xda, 0x85, 0xa6, 0x6f, 0x12, 0x87, 0xae, 0xf6, 0x99, 0x8c, 0x9a, 0x1f, 0x4a, 0x4c, 0xc4, 0xc2, 0xfb, 0xca, 0x1c, 0x3c, 0x20, 0x91, 0x65, 0x3c, 0x0d, 0x4f, 0x1c, 0x1c, 0x1c, 0x1c, 0x9d, 0x66, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x4d, 0x50, 0xe1, 0x8e, 0x0f, 0xec, 0x80, 0xc8, 0xaa, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x05, 0x51, 0x64, 0x49, 0x04, 0x2b, 0x2f, 0x3d, 0x9e, 0xa3, 0x6b, 0x3f, 0xa1, 0x4e, 0x40, 0x8a, 0x8b, 0x0c, 0x8c, 0x0f, 0xec, 0x80, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, }; // Antilogarithm table: alog[x] = g^x (g=0x03) const uint8_t ALOG_TABLE[256] = { 0x01, 0x03, 0x05, 0x0f, 0x11, 0x33, 0x55, 0xf, 0x41, 0xc3, 0xda, 0xbf, 0x25, 0x66, 0xaa, 0xe0, 0x47, 0xc1, 0xd8, 0xbb, 0x2f, 0x71, 0x93, 0xfd, 0x1d, 0x3b, 0x5d, 0xf7, 0x03, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b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x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1 }; // --- Helper Functions --- // Simple Galois Field multiplication (gmul) for MixColumns uint8_t aes::gmul(uint8_t a, uint8_t b) const{ uint8_t p = 0; uint8_t hi_bit_set; for (int i = 0; i < 8; i++) { if ((b & 1) == 1) { p ^= a; } hi_bit_set = (a & 0x80); a <<= 1; if (hi_bit_set) { a ^= 0x1b; /* XOR with 0x11b (Rijndael's irreducible polynomial) */ uint8_t aes::gmul(uint8_t a, uint8_t b) { if (a == 0 || b == 0) { return 0; } b >>= 1; // 1. Convert factors to their logarithms uint16_t log_a = LOG_TABLE[a]; uint16_t log_b = LOG_TABLE[b]; // 2. Add logarithms (this is the multiplication step) uint16_t sum = log_a + log_b; // 3. Modulo 255 reduction: (g^255 = g^0 = 1). Since log values only go up to 254. // The tables are only valid up to index 254 (0xFF). if (sum > 254) { sum = sum % 255; } return p; // 4. Convert back using antilogarithm table return ALOG_TABLE[sum]; } // RotWord (Cyclic shift left by 1 byte) Loading Loading @@ -192,19 +239,19 @@ aes::aes(const key128& key) { } // --- Encryption Implementation --- void aes::addRoundKey(block128& state, const block128& roundKey) const{ void aes::addRoundKey(block128& state, const block128& roundKey){ for (int i = 0; i < 16; ++i) { state[i] ^= roundKey[i]; } } void aes::subBytes(block128& state) const{ void aes::subBytes(block128& state){ for (int i = 0; i < 16; ++i) { state[i] = S_BOX[state[i]]; } } void aes::shiftRows(block128& state) const{ void aes::shiftRows(block128& state){ // The state is conceptually a 4x4 matrix, stored column-major: // [0] [4] [8] [12] // [1] [5] [9] [13] Loading Loading @@ -242,7 +289,7 @@ void aes::shiftRows(block128& state) const{ // In aes.cpp void aes::mixColumns(block128& state) const{ void aes::mixColumns(block128& state){ uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); Loading @@ -269,7 +316,7 @@ void aes::mixColumns(block128& state) const{ } } block128 aes::encrypt(const block128& plaintext) const { block128 aes::encrypt(const block128& plaintext){ block128 state = plaintext; const int NUM_ROUNDS = 10; Loading @@ -282,6 +329,15 @@ block128 aes::encrypt(const block128& plaintext) const { shiftRows(state); mixColumns(state); addRoundKey(state, m_roundKeys[i]); if (i == 1) { // Print state after Round 1 for comparison std::cout << "DEBUG: State after Round 1 (i=" << i << "): "; // Use a temporary print function here // print_block(state); // std::cout << std::endl; } for (const auto& byte : state) { std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)byte; } } // 3. Final Round (Round 10 - No MixColumns) Loading @@ -293,7 +349,7 @@ block128 aes::encrypt(const block128& plaintext) const { } // --- Decryption Implementation --- void aes::invSubBytes(block128& state) const{ void aes::invSubBytes(block128& state){ for (int i = 0; i < 16; ++i) { state[i] = INV_S_BOX[state[i]]; } Loading @@ -301,7 +357,7 @@ void aes::invSubBytes(block128& state) const{ // In aes.cpp void aes::invShiftRows(block128& state) const{ void aes::invShiftRows(block128& state){ // We use a temporary array because the shifts must be applied simultaneously. uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); Loading Loading @@ -333,7 +389,7 @@ void aes::invShiftRows(block128& state) const{ // In aes.cpp (Note: The function signature below is NON-const, // as it must modify the 'state' passed by reference) void aes::invMixColumns(block128& state) const { void aes::invMixColumns(block128& state){ uint8_t temp[16]; // Copy the original state to a temporary buffer before modification. // This is crucial because modifications to one column depend on the original values Loading Loading @@ -365,7 +421,7 @@ void aes::invMixColumns(block128& state) const { } } block128 aes::decrypt(const block128& ciphertext) const { block128 aes::decrypt(const block128& ciphertext){ block128 state = ciphertext; const int NUM_ROUNDS = 10; Loading src/aes.h +10 −10 Changes for src/aes.h: 10 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -56,18 +56,18 @@ private: uint32_t rotWord(uint32_t word); // Round operations void subBytes(block128& state) const; void shiftRows(block128& state) const; void mixColumns(block128& state) const; void addRoundKey(block128& state, const block128& roundKey) const; void subBytes(block128& state); void shiftRows(block128& state); void mixColumns(block128& state); void addRoundKey(block128& state, const block128& roundKey); // Inverse Round operations void invSubBytes(block128& state) const; void invShiftRows(block128& state) const; void invMixColumns(block128& state) const; void invSubBytes(block128& state); void invShiftRows(block128& state); void invMixColumns(block128& state); // Helper function for Galois Field multiplication in mixColumns uint8_t gmul(uint8_t a, uint8_t b) const; uint8_t gmul(uint8_t a, uint8_t b); public: /** Loading @@ -81,14 +81,14 @@ public: * @param plaintext The 16-byte input block. * @return The 16-byte ciphertext block. */ block128 encrypt(const block128& plaintext) const; block128 encrypt(const block128& plaintext); /** * @brief Decrypts a 128-bit block of ciphertext. * @param ciphertext The 16-byte input block. * @return The 16-byte plaintext block. */ block128 decrypt(const block128& ciphertext) const; block128 decrypt(const block128& ciphertext); }; } // namespace netplus src/event/kqueue.cpp +43 −44 Changes for src/event/kqueue.cpp: 43 added lines, 44 removed lines. Original line number Diff line number Diff line Loading @@ -172,43 +172,38 @@ namespace netplus { void ConnectEventHandler(int pos,const int tid,ULONG_PTR args) { NetException exception; while (true) { std::unique_ptr<con> ccon; evconnection->CreateConnection(ccon); if(!ccon){ char errstr[255]; strerror_r_netplus(errno,errstr,255); std::cerr << "ConnectEventHandler: Failed to accept connection or invalid socket: " << errstr << std::endl; return; } // 1. Allocate the socket object if(_ServerSocket->_Type==sockettype::TCP){ ccon->csock=std::make_unique<tcp>(-1); }else if(_ServerSocket->_Type==sockettype::UDP){ ccon->csock=std::make_unique<udp>(-1); }else if(_ServerSocket->_Type==sockettype::SSL){ // clt->connection->csock=std::make_unique<ssl>( -1); } } // ... handle other types ... if (!ccon->csock) { // The connection failed to establish or the socket is invalid. // Clean up and return, perhaps log an error. // 2. Attempt to accept the connection int accept_status = _ServerSocket->accept(ccon->csock); // Check accept status *before* checking ccon validity if (accept_status < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { // Queue drained. Exit the loop. break; } // Handle fatal accept errors char errstr[255]; strerror_r_netplus(errno,errstr,255); std::cerr << "ConnectEventHandler: Failed to accept connection or invalid socket: " << errstr << std::endl; std::cerr << "ConnectEventHandler: Failed to accept connection: " << errstr << std::endl; return; } _ServerSocket->accept(ccon->csock); // 3. Post-Accept setup (original logic) ccon->csock->setFlag(O_NONBLOCK,1); std::string ip; ccon->csock->getAddress(ip); std::cout << "Connected: " << ip << std::endl; // ... (logging, etc.) ccon->lasteventime = time(nullptr); // 4. Register client socket for reading (kqueue setup) struct kevent setevent {}; setevent.udata = ccon.get(); Loading @@ -217,36 +212,36 @@ namespace netplus { int estate = kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr); if ( estate < 0 ) { char errstr[255]; strerror_r_netplus(errno,errstr,255); exception[NetException::Error] << "ConnectEventHandler: can't add socket to kqueue: " << errstr; throw exception; // Fatal kqueue error, must close the accepted socket and throw // ... (error handling code, which must clean up ccon) // For now, let's assume the accepted socket is cleaned up by ccon's unique_ptr if released fails continue; // Skip to next accept attempt } // 5. Finalize connection (call event and insert into map) evconnection ->ConnectEvent(*ccon,tid,args); { std::lock_guard<std::mutex> clock(ccon->event_mutex),global_lock(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(ccon->csock->fd(),ccon.release()); } // Loop continues to accept the next connection from the queue } // end while(true) }; void ReadEventHandler(int pos,const int tid,ULONG_PTR args) { con *rcon = nullptr; { std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX); rcon = reinterpret_cast<con*>(_Events[pos].udata); } // Get pointer directly from udata. con *rcon = reinterpret_cast<con*>(_Events[pos].udata); if (!rcon) { return; } buffer buf(BLOCKSIZE); // Acquire connection-specific lock immediately std::lock_guard<std::mutex> lock_guard(rcon->event_mutex); if (!rcon || !rcon->csock) { return; } // Double check pointer validity under lock if (!rcon->csock) { return; } buffer buf(BLOCKSIZE); try{ Loading @@ -256,7 +251,11 @@ namespace netplus { if (rcvsize <= 0) { NetException e; if (rcvsize == 0) { e[NetException::Note] << "ReadEventHandler: EOF detected. Closing connection."; } else { e[NetException::Note] << "ReadEventHandler: Fatal socket error or eof detected. Closing connection."; } throw e; } Loading @@ -273,7 +272,12 @@ namespace netplus { }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ // Re-register for read or write if it was a non-fatal temporary error if(!rcon->SendData.empty()){ setpollEvents(rcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT); }else{ setpollEvents(rcon,EVFILT_READ,EV_ADD | EV_ONESHOT); } return; } throw e; Loading @@ -281,19 +285,14 @@ namespace netplus { }; void WriteEventHandler(int pos,const int tid,ULONG_PTR args) { con *wcon = nullptr; { std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX); wcon = reinterpret_cast<con*>(_Events[pos].udata); } con *wcon = reinterpret_cast<con*>(_Events[pos].udata); if (!wcon) { return; } std::lock_guard<std::mutex> lock_guard(wcon->event_mutex); if (!wcon->csock) { return; } try{ evconnection->ResponseEvent(*wcon,tid,args); Loading Loading
src/aes.cpp +78 −22 Changes for src/aes.cpp: 78 added lines, 22 removed lines. Original line number Diff line number Diff line Loading @@ -25,8 +25,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ #include <iostream> #include <algorithm> #include <cstring> // For memcpy or related functions if needed #include <iomanip> #include "aes.h" Loading Loading @@ -81,24 +83,69 @@ const uint8_t RCON[10] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 }; // Logarithm table: log[i] = power 'x' such that g^x = i (g=0x03) const uint8_t LOG_TABLE[256] = { 0x00, 0x00, 0x19, 0x01, 0x32, 0x02, 0x1a, 0xc6, 0x4b, 0xc7, 0x1b, 0x68, 0x33, 0xee, 0xdf, 0x03, 0x64, 0x04, 0xde, 0x34, 0x35, 0x6a, 0x1c, 0x48, 0x69, 0x76, 0x1d, 0xc5, 0x46, 0xd4, 0x47, 0x54, 0x56, 0x05, 0xd3, 0x36, 0x5e, 0x6b, 0x71, 0x06, 0x40, 0x07, 0x86, 0x6c, 0x93, 0xd0, 0x08, 0x88, 0x6d, 0x97, 0x37, 0x8d, 0x96, 0x38, 0x6f, 0xad, 0x09, 0xb3, 0x39, 0x24, 0x98, 0x0a, 0xa3, 0x0b, 0x8f, 0x6e, 0x45, 0x81, 0x41, 0x22, 0x78, 0x9c, 0x7a, 0x1e, 0x49, 0xc1, 0x42, 0x5f, 0x59, 0x0c, 0xb6, 0x43, 0x44, 0x7f, 0x27, 0x82, 0x23, 0x73, 0xb7, 0x4e, 0x83, 0x2b, 0x84, 0x7e, 0x21, 0xd2, 0xda, 0x85, 0xa6, 0x6f, 0x12, 0x87, 0xae, 0xf6, 0x99, 0x8c, 0x9a, 0x1f, 0x4a, 0x4c, 0xc4, 0xc2, 0xfb, 0xca, 0x1c, 0x3c, 0x20, 0x91, 0x65, 0x3c, 0x0d, 0x4f, 0x1c, 0x1c, 0x1c, 0x1c, 0x9d, 0x66, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x4d, 0x50, 0xe1, 0x8e, 0x0f, 0xec, 0x80, 0xc8, 0xaa, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x05, 0x51, 0x64, 0x49, 0x04, 0x2b, 0x2f, 0x3d, 0x9e, 0xa3, 0x6b, 0x3f, 0xa1, 0x4e, 0x40, 0x8a, 0x8b, 0x0c, 0x8c, 0x0f, 0xec, 0x80, 0x57, 0xfa, 0x3e, 0x13, 0x72, 0x6a, 0x93, 0x17, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0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x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1, 0x3f, 0x09, 0x1b, 0x3d, 0x5b, 0xf1 }; // --- Helper Functions --- // Simple Galois Field multiplication (gmul) for MixColumns uint8_t aes::gmul(uint8_t a, uint8_t b) const{ uint8_t p = 0; uint8_t hi_bit_set; for (int i = 0; i < 8; i++) { if ((b & 1) == 1) { p ^= a; } hi_bit_set = (a & 0x80); a <<= 1; if (hi_bit_set) { a ^= 0x1b; /* XOR with 0x11b (Rijndael's irreducible polynomial) */ uint8_t aes::gmul(uint8_t a, uint8_t b) { if (a == 0 || b == 0) { return 0; } b >>= 1; // 1. Convert factors to their logarithms uint16_t log_a = LOG_TABLE[a]; uint16_t log_b = LOG_TABLE[b]; // 2. Add logarithms (this is the multiplication step) uint16_t sum = log_a + log_b; // 3. Modulo 255 reduction: (g^255 = g^0 = 1). Since log values only go up to 254. // The tables are only valid up to index 254 (0xFF). if (sum > 254) { sum = sum % 255; } return p; // 4. Convert back using antilogarithm table return ALOG_TABLE[sum]; } // RotWord (Cyclic shift left by 1 byte) Loading Loading @@ -192,19 +239,19 @@ aes::aes(const key128& key) { } // --- Encryption Implementation --- void aes::addRoundKey(block128& state, const block128& roundKey) const{ void aes::addRoundKey(block128& state, const block128& roundKey){ for (int i = 0; i < 16; ++i) { state[i] ^= roundKey[i]; } } void aes::subBytes(block128& state) const{ void aes::subBytes(block128& state){ for (int i = 0; i < 16; ++i) { state[i] = S_BOX[state[i]]; } } void aes::shiftRows(block128& state) const{ void aes::shiftRows(block128& state){ // The state is conceptually a 4x4 matrix, stored column-major: // [0] [4] [8] [12] // [1] [5] [9] [13] Loading Loading @@ -242,7 +289,7 @@ void aes::shiftRows(block128& state) const{ // In aes.cpp void aes::mixColumns(block128& state) const{ void aes::mixColumns(block128& state){ uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); Loading @@ -269,7 +316,7 @@ void aes::mixColumns(block128& state) const{ } } block128 aes::encrypt(const block128& plaintext) const { block128 aes::encrypt(const block128& plaintext){ block128 state = plaintext; const int NUM_ROUNDS = 10; Loading @@ -282,6 +329,15 @@ block128 aes::encrypt(const block128& plaintext) const { shiftRows(state); mixColumns(state); addRoundKey(state, m_roundKeys[i]); if (i == 1) { // Print state after Round 1 for comparison std::cout << "DEBUG: State after Round 1 (i=" << i << "): "; // Use a temporary print function here // print_block(state); // std::cout << std::endl; } for (const auto& byte : state) { std::cout << std::hex << std::setw(2) << std::setfill('0') << (int)byte; } } // 3. Final Round (Round 10 - No MixColumns) Loading @@ -293,7 +349,7 @@ block128 aes::encrypt(const block128& plaintext) const { } // --- Decryption Implementation --- void aes::invSubBytes(block128& state) const{ void aes::invSubBytes(block128& state){ for (int i = 0; i < 16; ++i) { state[i] = INV_S_BOX[state[i]]; } Loading @@ -301,7 +357,7 @@ void aes::invSubBytes(block128& state) const{ // In aes.cpp void aes::invShiftRows(block128& state) const{ void aes::invShiftRows(block128& state){ // We use a temporary array because the shifts must be applied simultaneously. uint8_t temp[16]; std::copy(state.begin(), state.end(), temp); Loading Loading @@ -333,7 +389,7 @@ void aes::invShiftRows(block128& state) const{ // In aes.cpp (Note: The function signature below is NON-const, // as it must modify the 'state' passed by reference) void aes::invMixColumns(block128& state) const { void aes::invMixColumns(block128& state){ uint8_t temp[16]; // Copy the original state to a temporary buffer before modification. // This is crucial because modifications to one column depend on the original values Loading Loading @@ -365,7 +421,7 @@ void aes::invMixColumns(block128& state) const { } } block128 aes::decrypt(const block128& ciphertext) const { block128 aes::decrypt(const block128& ciphertext){ block128 state = ciphertext; const int NUM_ROUNDS = 10; Loading
src/aes.h +10 −10 Changes for src/aes.h: 10 added lines, 10 removed lines. Original line number Diff line number Diff line Loading @@ -56,18 +56,18 @@ private: uint32_t rotWord(uint32_t word); // Round operations void subBytes(block128& state) const; void shiftRows(block128& state) const; void mixColumns(block128& state) const; void addRoundKey(block128& state, const block128& roundKey) const; void subBytes(block128& state); void shiftRows(block128& state); void mixColumns(block128& state); void addRoundKey(block128& state, const block128& roundKey); // Inverse Round operations void invSubBytes(block128& state) const; void invShiftRows(block128& state) const; void invMixColumns(block128& state) const; void invSubBytes(block128& state); void invShiftRows(block128& state); void invMixColumns(block128& state); // Helper function for Galois Field multiplication in mixColumns uint8_t gmul(uint8_t a, uint8_t b) const; uint8_t gmul(uint8_t a, uint8_t b); public: /** Loading @@ -81,14 +81,14 @@ public: * @param plaintext The 16-byte input block. * @return The 16-byte ciphertext block. */ block128 encrypt(const block128& plaintext) const; block128 encrypt(const block128& plaintext); /** * @brief Decrypts a 128-bit block of ciphertext. * @param ciphertext The 16-byte input block. * @return The 16-byte plaintext block. */ block128 decrypt(const block128& ciphertext) const; block128 decrypt(const block128& ciphertext); }; } // namespace netplus
src/event/kqueue.cpp +43 −44 Changes for src/event/kqueue.cpp: 43 added lines, 44 removed lines. Original line number Diff line number Diff line Loading @@ -172,43 +172,38 @@ namespace netplus { void ConnectEventHandler(int pos,const int tid,ULONG_PTR args) { NetException exception; while (true) { std::unique_ptr<con> ccon; evconnection->CreateConnection(ccon); if(!ccon){ char errstr[255]; strerror_r_netplus(errno,errstr,255); std::cerr << "ConnectEventHandler: Failed to accept connection or invalid socket: " << errstr << std::endl; return; } // 1. Allocate the socket object if(_ServerSocket->_Type==sockettype::TCP){ ccon->csock=std::make_unique<tcp>(-1); }else if(_ServerSocket->_Type==sockettype::UDP){ ccon->csock=std::make_unique<udp>(-1); }else if(_ServerSocket->_Type==sockettype::SSL){ // clt->connection->csock=std::make_unique<ssl>( -1); } } // ... handle other types ... if (!ccon->csock) { // The connection failed to establish or the socket is invalid. // Clean up and return, perhaps log an error. // 2. Attempt to accept the connection int accept_status = _ServerSocket->accept(ccon->csock); // Check accept status *before* checking ccon validity if (accept_status < 0) { if (errno == EAGAIN || errno == EWOULDBLOCK) { // Queue drained. Exit the loop. break; } // Handle fatal accept errors char errstr[255]; strerror_r_netplus(errno,errstr,255); std::cerr << "ConnectEventHandler: Failed to accept connection or invalid socket: " << errstr << std::endl; std::cerr << "ConnectEventHandler: Failed to accept connection: " << errstr << std::endl; return; } _ServerSocket->accept(ccon->csock); // 3. Post-Accept setup (original logic) ccon->csock->setFlag(O_NONBLOCK,1); std::string ip; ccon->csock->getAddress(ip); std::cout << "Connected: " << ip << std::endl; // ... (logging, etc.) ccon->lasteventime = time(nullptr); // 4. Register client socket for reading (kqueue setup) struct kevent setevent {}; setevent.udata = ccon.get(); Loading @@ -217,36 +212,36 @@ namespace netplus { int estate = kevent(_pollFD, &setevent, 1, nullptr, 0, nullptr); if ( estate < 0 ) { char errstr[255]; strerror_r_netplus(errno,errstr,255); exception[NetException::Error] << "ConnectEventHandler: can't add socket to kqueue: " << errstr; throw exception; // Fatal kqueue error, must close the accepted socket and throw // ... (error handling code, which must clean up ccon) // For now, let's assume the accepted socket is cleaned up by ccon's unique_ptr if released fails continue; // Skip to next accept attempt } // 5. Finalize connection (call event and insert into map) evconnection ->ConnectEvent(*ccon,tid,args); { std::lock_guard<std::mutex> clock(ccon->event_mutex),global_lock(POLL_HANDLER_MUTEX); CONNECTIONS.emplace(ccon->csock->fd(),ccon.release()); } // Loop continues to accept the next connection from the queue } // end while(true) }; void ReadEventHandler(int pos,const int tid,ULONG_PTR args) { con *rcon = nullptr; { std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX); rcon = reinterpret_cast<con*>(_Events[pos].udata); } // Get pointer directly from udata. con *rcon = reinterpret_cast<con*>(_Events[pos].udata); if (!rcon) { return; } buffer buf(BLOCKSIZE); // Acquire connection-specific lock immediately std::lock_guard<std::mutex> lock_guard(rcon->event_mutex); if (!rcon || !rcon->csock) { return; } // Double check pointer validity under lock if (!rcon->csock) { return; } buffer buf(BLOCKSIZE); try{ Loading @@ -256,7 +251,11 @@ namespace netplus { if (rcvsize <= 0) { NetException e; if (rcvsize == 0) { e[NetException::Note] << "ReadEventHandler: EOF detected. Closing connection."; } else { e[NetException::Note] << "ReadEventHandler: Fatal socket error or eof detected. Closing connection."; } throw e; } Loading @@ -273,7 +272,12 @@ namespace netplus { }catch(NetException &e){ if(e.getErrorType()== NetException::Note){ // Re-register for read or write if it was a non-fatal temporary error if(!rcon->SendData.empty()){ setpollEvents(rcon,EVFILT_WRITE,EV_ADD | EV_ONESHOT); }else{ setpollEvents(rcon,EVFILT_READ,EV_ADD | EV_ONESHOT); } return; } throw e; Loading @@ -281,19 +285,14 @@ namespace netplus { }; void WriteEventHandler(int pos,const int tid,ULONG_PTR args) { con *wcon = nullptr; { std::lock_guard<std::mutex> global_lock(POLL_HANDLER_MUTEX); wcon = reinterpret_cast<con*>(_Events[pos].udata); } con *wcon = reinterpret_cast<con*>(_Events[pos].udata); if (!wcon) { return; } std::lock_guard<std::mutex> lock_guard(wcon->event_mutex); if (!wcon->csock) { return; } try{ evconnection->ResponseEvent(*wcon,tid,args); Loading