void Txc14::handleMessage(cMessage *msg) { // check_and_cast from omnetpp.h perform a dynamic cast or raise an error // during the simulation instead of crash. TicTocMsg14 *ttmsg = check_and_cast<TicTocMsg14 *>(msg); if (ttmsg->getDestination() == getIndex()) { // Message arrived. int hopcount = ttmsg->getHopCount(); EV << "Message " << ttmsg <<" arrived after " << hopcount << " messages.\n"; receivedPkgs++; delete ttmsg; bubble("ARRIVED, starting a new one."); // Generate another message. TicTocMsg14 *newmsg = generateMessage(); EV <<newmsg << endl; forwardMessage(newmsg); sentPkgs++; } else { // We need to forward the message. forwardMessage(ttmsg); } }
void handleClient(struct ClientNode *client) { ssize_t bytesRcvd; uint8_t *buf = malloc(MAX_PACKET_SIZE); if (buf == NULL) { perror("handleClient:malloc"); return; } if ((bytesRcvd = recv(client->clientSocket, buf, MAX_PACKET_SIZE-1, 0)) < 0) { perror("handleClient:recv"); free(buf); return; } // Check for a shutdown if (bytesRcvd == 0) { removeClient(client); } // Parse the buffer else { client->packetLength = bytesRcvd; client->packetData = (struct ChatHeader *)buf; // make sure the checksum is correct before parsing the packets if (in_cksum((uint16_t *)client->packetData, client->packetLength) == 0) { // Put the packet header fields in host order switch (client->packetData->flag) { case FLAG_INIT_REQ: registerHandle(client); break; case FLAG_MSG_REQ: case FLAG_MSG_ACK: forwardMessage(client); break; case FLAG_EXIT_REQ: clientExit(client); break; case FLAG_LIST_REQ: getClientCount(client); break; case FLAG_HNDL_REQ: clientNameRequest(client); break; case FLAG_INIT_ACK: case FLAG_INIT_ERR: case FLAG_MSG_ERR: case FLAG_EXIT_ACK: case FLAG_LIST_RESP: case FLAG_HNDL_RESP: default: break; } } client->packetData = NULL; client->packetLength = 0; } free(buf); }
void FifoReadCallback::readDataAvailable(size_t len) noexcept { try { readBuffer_.postallocate(len); // Process any pending packet headers. if (pendingHeader_) { if (readBuffer_.chainLength() < pendingHeader_->packetSize()) { return; } forwardMessage(pendingHeader_.value(), readBuffer_.split(pendingHeader_->packetSize())); pendingHeader_.clear(); } while (readBuffer_.chainLength() >= kHeaderMagicSize) { if (isMessageHeader(readBuffer_)) { if (readBuffer_.chainLength() < sizeof(MessageHeader)) { // Wait for more data return; } handleMessageHeader(parseMessageHeader(readBuffer_)); } if (readBuffer_.chainLength() < sizeof(PacketHeader)) { // Wait for more data return; } auto packetHeader = parsePacketHeader(readBuffer_); if (packetHeader.packetSize() > readBuffer_.chainLength()) { // Wait for more data. pendingHeader_.assign(std::move(packetHeader)); return; } forwardMessage(packetHeader, readBuffer_.split(packetHeader.packetSize())); } } catch (const std::exception& ex) { CHECK(false) << "Unexpected exception: " << ex.what(); } }
void Txc13::handleMessage(cMessage *msg) { // check_and_cast from omnetpp.h perform a dynamic cast or raise an error // during the simulation instead of crash. TicTocMsg13 *ttmsg = check_and_cast<TicTocMsg13 *>(msg); if (ttmsg->getDestination() == getIndex()) { // Message arrived. EV << "Message " << msg <<" arrived.\n"; bubble("ARRIVED, starting a new one."); delete ttmsg; // Generate another message. TicTocMsg13 *newmsg = generateMessage(); EV <<newmsg << endl; forwardMessage(newmsg); } else { // We need to forward the message. forwardMessage(ttmsg); } }
void serveClient(ClientDesc client) { SSL* ssl; ssl = client.ssl; Message req; req = receive(ssl); switch (req.getType()) { case Message::REGISTER_REQ : { registerClient(req, client); break; } case Message::TEXT_MSG : { forwardMessage(req, client); break; } case Message::BROADCAST_MSG : { broadcastTextMessageToAll(req, client); break; } case Message::CLIENT_QUIT_IND : { forgetAboutClient(client); broadcastInfoAboutQuitClient(client); break; } default: { break; } } }
ChatWindow *MainWindow::createChatWindow(Contact& contact, bool show) { // Just open one window at a time to avoid duplicates createWindowMutex.lock(); ChatWindow *chat; QString jid = contact.jid; if (contact.type == Contact::TypeGroup) Utilities::logData("Group"); else Utilities::logData("Contact"); if (!chatWindowList.contains(jid)) { Utilities::logData("There's no previous chat window"); if (contact.type == Contact::TypeContact) chat = new ChatWindow(&contact,this); else chat = new GroupWindow((Group *)&contact,this); chatWindowList.insert(jid,chat); if (!lastContactsList.contains(jid)) { // This is a new chat and it is not in the // open chats list // Create the open chats item ChatDisplayItem *item = new ChatDisplayItem(&contact); lastContactsList.insert(jid,item); model->appendRow(item); // This contact now has an open chat contact.hasOpenChat = true; // Set the last line logged in the item FMessage msg = chat->lastMessage(); item->updateData(msg); // Store this chat in the DB ConversationsDBEntry entry; entry.jid = jid; entry.muted = false; entry.muteExpireTimestamp = 0; chatsDB.createChat(entry); if (contact.type == Contact::TypeContact) emit subscribe(jid); } else { // This chat is in the open chats list // Configure mute settings ChatDisplayItem *item = lastContactsList.value(jid); if (item->muted) chat->setMute(item->muteExpireTimestamp); } chat->setAttribute(Qt::WA_DeleteOnClose); if (!show) Utilities::logData("Chat window will not be shown"); if (show) { Utilities::logData("Showing chat window"); qint64 startTime = QDateTime::currentMSecsSinceEpoch(); chat->show(); qint64 endTime = QDateTime::currentMSecsSinceEpoch() - startTime; Utilities::logData("Chat window showed " + QString::number(endTime) + " milliseconds."); } connect(chat,SIGNAL(sendMessage(FMessage)), this,SLOT(sendMessageFromChat(FMessage))); connect(chat,SIGNAL(destroyed(QObject *)), this,SLOT(deleteChat(QObject *))); connect(chat,SIGNAL(mute(QString,bool,qint64)), this,SLOT(mute(QString,bool,qint64))); connect(chat,SIGNAL(blockOrUnblockContact(QString,bool)), this,SLOT(blockOrUnblockContact(QString,bool))); connect(chat,SIGNAL(photoUpdate(QString,QString,bool)), this,SLOT(requestPhotoUpdate(QString,QString,bool))); connect(chat,SIGNAL(requestStatus(QString)), this,SLOT(requestContactStatus(QString))); connect(chat,SIGNAL(voiceNotePlayed(FMessage)), this,SLOT(sendVoiceNotePlayed(FMessage))); connect(chat,SIGNAL(messageRead(FMessage)), this,SLOT(sendMessageRead(FMessage))); connect(chat,SIGNAL(updateLastDir(int,QString)), this,SLOT(requestUpdateLastDir(int,QString))); connect(chat,SIGNAL(forwardMessage(FMessage)), this,SLOT(forwardMessage(FMessage))); if (contact.type == Contact::TypeGroup) { GroupWindow *groupChat = (GroupWindow *) chat; connect(groupChat,SIGNAL(changeSubject(QString,QString)), this,SLOT(sendSetGroupSubjectFromChat(QString,QString))); connect(groupChat,SIGNAL(requestLeaveGroup(QString)), this,SLOT(requestLeaveGroupFromChat(QString))); connect(groupChat,SIGNAL(getParticipants(QString)), this,SLOT(requestGetParticipants(QString))); } else emit queryLastOnline(jid); }
int32_t mail_responder(uint32_t socket) { #ifdef DEBUG printf("Entering MAIL subroutine\n"); #endif /*DEBUG*/ //Allocate general variables //Allocate primary buffers and counters char source_account_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t source_account_length = 0; char source_domain_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t source_domain_length = 0; char dest_account_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t dest_account_length = 0; char dest_domain_buffer[ROUTING_FIELD_SIZE] = {0}; int32_t dest_domain_length = 0; uint32_t version = 0; uint32_t attachment_count = 0; uint64_t message_length = 0; uint64_t log_length = 0; //Generate unique file from current time and current FD //Get time with nanosecond resolution (or so they say) struct timespec time_for_file; clock_gettime(CLOCK_REALTIME, &time_for_file); //Mix time with FD and hash //We are hashing two ints and a long, so char meat_and_potatoes[24] = {0}; //memset(meat_and_potatoes, 0, sizeof(meat_and_potatoes)); memcpy(meat_and_potatoes, &time_for_file.tv_sec, sizeof(time_for_file.tv_sec)); memcpy(meat_and_potatoes+sizeof(time_for_file.tv_sec), &socket, sizeof(socket)); memcpy(meat_and_potatoes+sizeof(time_for_file.tv_sec)+sizeof(socket), &time_for_file.tv_nsec, sizeof(time_for_file.tv_nsec)); unsigned char hash[64]; //64 bytes because hash has a fixed size output crypto_generichash(hash, sizeof(hash), (const unsigned char *)meat_and_potatoes, sizeof(meat_and_potatoes),NULL, 0); //Get file ready to write //TODO needs to be /mail/user/unique_file_name char unique_file_name[129] = {0}; //char base64_username[341] = {0}; char unique_file_location[522] = {0}; //TODO Need to check if user is part of this domain. If not the file location should be some temporary storage. //unique_file_name_length is not currently used. Should be fine. uint32_t unique_file_name_length = base64_encode((char *)hash, sizeof(hash), unique_file_name, sizeof(unique_file_name), (char *)filesystem_safe_base64_string, 64); if (unique_file_name_length<=0) { perror("base64_encode. unique_file_name failed to be created"); print_to_log("base64_encode failed to create a unique_file_name.", LOG_ERR); return -1; } //uint32_t base64_username_length = base64_encode((char *)dest_account_buffer, strlen(dest_account_buffer), base64_username, strlen(base64_username), (char *)filesystem_safe_base64_string, 64); //Read primary routing and processing information //First read in fixed length fields if (read_n_bytes(socket, (unsigned char *)&version, 4)!=4) { perror("read_n_bytes version"); print_to_log("Read error while reading crypto type", LOG_ERR); return -1; } if (read_n_bytes(socket, (unsigned char *)&attachment_count, 4)!=4) { perror("read_n_bytes attachment_count"); print_to_log("Read error while reading attachment count", LOG_ERR); return -1; } if (read_n_bytes(socket, (unsigned char *)&log_length, 8)!=8) { perror("read_n_bytes log_length"); print_to_log("Read error while reading message length", LOG_ERR); return -1; } if (read_n_bytes(socket, (unsigned char *)&message_length, 8)!=8) { perror("read_n_bytes message_length"); print_to_log("Read error while reading message length", LOG_ERR); return -1; } //Read in account and domain info if ((dest_account_length=read_until(socket, (unsigned char *)dest_account_buffer, sizeof(dest_account_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading dest_account_buffer", LOG_ERR); return -1; } if (snprintf(unique_file_location, sizeof(unique_file_location), "%s%s%s%s", "/mail/", dest_account_buffer, "/" , unique_file_name)<0) { perror("snprintf"); print_to_log("snprintf failed to create a new file string. Cannot write message out",LOG_ERR); return -1; } write_to_file((char *)&version, 4, unique_file_location); version = be32toh(version); write_to_file((char *)&attachment_count, 4, unique_file_location); attachment_count = be32toh(attachment_count); write_to_file((char *)&log_length, 8, unique_file_location); log_length = be64toh(log_length); write_to_file((char *)&message_length, 8, unique_file_location); message_length = be64toh(message_length); #ifdef DEBUG printf("Writing mail to %s\n", unique_file_location); printf("Log length = %ld\n", log_length); printf("Version = %d\n", version); printf("Attachment count = %d\n", attachment_count); printf("Message length = %ld\n", message_length); #endif /*DEBUG*/ write_to_file(dest_account_buffer, dest_account_length, unique_file_location); #ifdef DEBUG printf("dest_account_length = %d\n", dest_account_length); #endif /*DEBUG*/ if ((dest_domain_length=read_until(socket, (unsigned char *)dest_domain_buffer, sizeof(dest_domain_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading dest_domain_buffer", LOG_ERR); return -1; } write_to_file(dest_domain_buffer, dest_domain_length, unique_file_location); #ifdef DEBUG printf("dest_domain_length = %d\n", dest_domain_length); #endif /*DEBUG*/ //Check if this is the destination if((strcmp(dest_domain_buffer, home_domain)!=0)&&(forward==0)) { //Send delivery failure, close connection, clean up and return. unsigned char signature_of_DELIVERYFAILURE[crypto_sign_BYTES] = {0}; unsigned char write_buffer[sizeof(network_crypto_version)+sizeof(server_public_key)+sizeof(signature_of_DELIVERYFAILURE)+2]; crypto_sign_detached(signature_of_DELIVERYFAILURE, NULL, signature_of_DELIVERYFAILURE, sizeof(signature_of_DELIVERYFAILURE), server_private_key); memcpy(write_buffer, &network_crypto_version, sizeof(network_crypto_version)); memcpy(write_buffer+sizeof(network_crypto_version), cmtp_reply_DELIVERYFAILURE, sizeof(cmtp_reply_DELIVERYFAILURE)); memcpy(write_buffer+sizeof(network_crypto_version)+sizeof(cmtp_reply_DELIVERYFAILURE), &signature_of_DELIVERYFAILURE, sizeof(signature_of_DELIVERYFAILURE)); memcpy(write_buffer+sizeof(network_crypto_version)+sizeof(cmtp_reply_DELIVERYFAILURE)+sizeof(signature_of_DELIVERYFAILURE), &termination_char, sizeof(termination_char)); write(socket, write_buffer, sizeof(write_buffer)); } if ((source_account_length=read_until(socket, (unsigned char *)source_account_buffer, sizeof(source_account_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading source_account_buffer", LOG_ERR); return -1; } write_to_file(source_account_buffer, source_account_length, unique_file_location); #ifdef DEBUG printf("source_account_length = %d\n", source_account_length); #endif /*DEBUG*/ if ((source_domain_length=read_until(socket, (unsigned char *)source_domain_buffer, sizeof(source_domain_buffer), '\0'))<0) { perror("read_until"); print_to_log("Read error while reading source_domain_buffer", LOG_ERR); return -1; } write_to_file(source_domain_buffer, source_domain_length, unique_file_location); #ifdef DEBUG printf("source_domain_length = %d\n", source_domain_length); #endif /*DEBUG*/ //This completes the read of the header //uint64_t numeric_message_length = be64toh(*(uint64_t*)(&(message_length[0]))); //uint64_t numeric_log_length = be64toh(*(uint64_t*)(&(log_length[0]))); char temp_byte[1] = {0}; //Read log for (uint64_t i = 0; i<log_length; i++) { if (read(socket, temp_byte, 1)<1) { print_to_log("read error while reading message body", LOG_ERR); perror("read"); return -1; } write_to_file(temp_byte, 1, unique_file_location); } //Read message body for (uint64_t i = 0; i<message_length; i++) { if (read(socket, temp_byte, 1)<1) { print_to_log("read error while reading message body", LOG_ERR); perror("read"); return -1; } write_to_file(temp_byte, 1, unique_file_location); } #ifdef DEBUG printf("Message body finished. Moving to attachment handling.\n"); #endif /*DEBUG*/ //Read for attachment //uint32_t numeric_attachment_count = be32toh(*(uint32_t*)(&(attachment_count[0]))); temp_byte[0] = 0; for (uint64_t i = 0; i<attachment_count; i++) { if (read(socket, temp_byte, 1)<1) { print_to_log("read error while reading message body", LOG_ERR); perror("read"); return -1; } write_to_file(temp_byte, 1, unique_file_location); } #ifdef DEBUG printf("Mail destin for %s\n", dest_domain_buffer); printf("Server domain is %s\n", home_domain); #endif /*DEBUG*/ //Destination cases if ((memcmp(dest_domain_buffer, home_domain, dest_domain_length)==0)&&(memcmp(dest_account_buffer,"",1))==0) { #ifdef DEBUG printf("Devlivered mail is for server. Begin processing.\n"); #endif /*DEBUG*/ print_to_log("Mail has arrived for the server. Processing.",LOG_INFO); //Destination is this domain and for the server } else if ((memcmp(dest_domain_buffer, home_domain, dest_domain_length)==0)) { #ifdef DEBUG printf("Devlivered mail is for a user on this domain. Store.\n"); #endif /*DEBUG*/ print_to_log("Mail has arrived for a user on this domain. Storing.",LOG_INFO); //Destination is for a user at this domain } else { #ifdef DEBUG printf("Devlivered mail is not destined for this domain. Forward to %s\n", dest_domain_buffer); #endif /*DEBUG*/ print_to_log("Mail has arrived for another domain. Forwarding.",LOG_INFO); forwardMessage(unique_file_location, dest_domain_buffer); //Destination is on the web. Forward message. } #ifdef DEBUG printf("Mail section complete. Returning to mail loop.\n"); #endif /*DEBUG*/ return 0; }