📄 sipprocessor.cpp
字号:
// Forward INVITE to callee string tempIP(calleeSocket->RemoteIPAddress().GetAddressString()); log->debug("Forwarding ACK Message to: IP = " + tempIP + ", port = ", calleeSocket->RemotePortNumber()); bytesTransferred = calleeSocket->SendDatagram(originalRequest.c_str(), originalRequest.size()); log->debug(" - sent bytes: ", bytesTransferred); */}void SipProcessor::sendServerError() { string response = "SERVER ERROR"; serverSocket->SendDatagram(response.c_str(), response.size(), *callerIPSource, callerPort); log->debug("SERVER ERROR sent to caller.");}void SipProcessor::sendRequestNotSupported() { string response = "REQUEST NOT SUPPORTED"; serverSocket->SendDatagram(response.c_str(), response.size(), *callerIPSource, callerPort); log->debug("REQUEST NOT SUPPORTED sent to caller.");}void SipProcessor::sendInvalidRequest() { string response = "INVALID REQUEST"; serverSocket->SendDatagram(response.c_str(), response.size(), *callerIPSource, callerPort); log->debug("INVALID REQUEST sent to caller.");}void SipProcessor::sendNotFound() { string response = "NOT FOUND"; serverSocket->SendDatagram(response.c_str(), response.size(), *callerIPSource, callerPort); log->debug("NOT FOUND sent to caller.");}const string SipProcessor::prepareInvite() { istringstream is(originalRequest); string line; // First iterate over the request, to find the <To: XXX> line // When found, seach in Kademlia network for real data // Then iterate over the request again, replace the old // To data with the one found in kademlia network for (getline(is, line); line != ""; getline(is, line)) { if (line.find("To:") == 0) { if (!splitContactData(line)) { log->debug("Error reading contact data from line: " + line); sendInvalidRequest(); return ""; } break; } } // Now search for data in kademlia Database* api = Database::getInstance(); string foundData = api->get(responseContact[0]); if (foundData == "") { log->debug("Name '" + responseContact[0] + "' not found in kademlia!"); sendNotFound(); return ""; } int index = foundData.find("@"); responseContact[0] = foundData.substr(0, index); // name responseContact[1] = foundData.substr(index + 1); // ip + port index = responseContact[1].find(":"); if (index == -1) { responseContact[2] = "5060"; // def port } else { responseContact[2] = responseContact[1].substr(index +1); // port responseContact[1] = responseContact[1].substr(0, index); // IP adr } // The data should be prepared. Now generate the response ostringstream response; is.str(originalRequest); // Copy needed lines from request to response int emptyLines = 0; for (getline(is, line); emptyLines < 2; getline(is, line)) { if (line == "") { // Count empty lines, after two of them we are done response << line << endl; emptyLines++; } else if (line.find(INVITE) == 0) { // Replace values in header response << INVITE << " sip:" << responseContact[0] << "@" << responseContact[1] << " SIP/2.0" << endl; } else if (line.find("To:") == 0) { // Replace values in To: field response << "To: <sip:" << responseContact[0] << "@" << responseContact[1] << ":" << responseContact[2] << ">" << endl; } else { // Else just copy the line response << line << endl; } } response << endl; return response.str();}const string SipProcessor::createTryingMessage() { istringstream is(originalRequest); ostringstream responseStream; string line; responseStream << SIP_HEADER_TRYING << endl; // Copy needed lines from request to response for (getline(is, line); line != ""; getline(is, line)) { if (line.find("Via:") == 0) { responseStream << line << endl; } else if (line.find("To:") == 0) { responseStream << line << endl; } else if (line.find("Contact:") == 0) { responseStream << line << endl; } else if (line.find("From:") == 0) { responseStream << line << endl; } else if (line.find("Call-ID:") == 0) { responseStream << line << endl; } else if (line.find("Server:") == 0) { responseStream << line << endl; } else if (line.find("Content-Length:") == 0) { responseStream << "Content-Length: 0" << endl << endl; } } // Everything ok -> return created message; return responseStream.str();}const string SipProcessor::createRedirectionMessage() { istringstream is(originalRequest); string line; string foundData = ""; // Firs search the for the TO field, and get data for (getline(is, line); line != ""; getline(is, line)) { if (line.find("To:") == 0) { if (!splitContactData(line)) { log->debug("Error reading contact data from line: " + line); sendInvalidRequest(); return ""; } // Now search for data in kademlia Database* api = Database::getInstance(); log->debug("Searching for " + responseContact[0]); foundData = api->get(responseContact[0]); log->debug("Found " + foundData); if (foundData == "") { log->debug("Name '" + responseContact[0] + "' not found in kademlia!"); sendNotFound(); return ""; } } } log->debug("Data found, prepare redirection"); istringstream is2(originalRequest); ostringstream responseStream; responseStream << SIP_HEADER_MOVED_TEMPORARILY << endl; for (getline(is2, line); line != ""; getline(is2, line)) { if (line.find("Via:") == 0) { string tempLine = "Via: SIP/2.0/UDP 80.108.236.241:5063;rport;branch=z9hG4bKowbwhxec"; responseStream << tempLine << endl; responseStream << line << endl; } else if (line.find("To:") == 0) { responseStream << "To: <sip:" + foundData + ">" << endl; } else if (line.find("From:") == 0) { responseStream << line << endl; } else if (line.find("Contact:") == 0) { responseStream << "Contact: <sip:" + foundData + ">" << endl; } else if (line.find("Call-ID:") == 0) { responseStream << line << endl; } else if (line.find("CSeq:") == 0) { responseStream << line << endl; } else if (line.find("Content-Length:") == 0) { responseStream << "Content-Length: 0" << endl << endl; } } // Everything ok -> return created message; log->debug("Exiting..."); return responseStream.str();}/** * Prepare contact data */bool SipProcessor::splitContactData(const string& cl) { string contactLine = cl; int index = contactLine.find("<sip:"); int index2 = contactLine.find(">"); if (index == -1 || index2 ==-1) { return false; } contactLine = contactLine.substr(index + 5, index2 - index - 5); index = contactLine.find("@"); if (index == -1) { return false; } string name = contactLine.substr(0, index); string ipAddress = contactLine.substr(index + 1); string port = ""; index = ipAddress.find(":"); if (index != -1) { port = ipAddress.substr(index+1); ipAddress = ipAddress.substr(0, index); } responseContact[0] = name; responseContact[1] = ipAddress; responseContact[2] = port; log->debug("PORT = " + port); return true;}/** * Gets current date and time in format prepared for SIP. */const string SipProcessor::getDateAndTime() { time_t rawtime; struct tm * timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); char* now = asctime(timeinfo); string nfdt(now); string fdt; fdt += nfdt.substr(0, 3) + ", "; // day in week fdt += nfdt.substr(8, 2) + " "; // date fdt += nfdt.substr(4, 3) + " "; // Month fdt += nfdt.substr(20, 4) + " "; // year fdt += nfdt.substr(11, 8) + " GMT"; return fdt;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -