OsSocket* SipTlsServer::buildClientSocket(int hostPort, const char* hostAddress, const char* localIp, bool& existingSocketReused) { OsSocket* socket; socket = new OsSSLConnectionSocket(hostPort, hostAddress); socket->makeBlocking(); existingSocketReused = false; return(socket); }
/** * Open datagram socket and send a few bytes. * * NOTE: This can/will fail if /etc/hosts defines localhost as ::1 (as * opposed to 127.0.0.1). */ void testWriteMsg() { OsSocket* s = new OsDatagramSocket(8020, "localhost"); const char* msg = "hello\n"; int len = strlen(msg); int bytesWritten = s->write(msg, len); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes", bytesWritten, len); s->close(); delete s; }
/** * Start a client and server and send 2 messages over TCP thru them * * NOTE: This can/will fail if /etc/hosts defines localhost as ::1 (as * opposed to 127.0.0.1). */ void testWriteAndAcceptMsg() { // Create/Verify Sockets OsServerSocket* server = new OsServerSocket(50, 8021); KNOWN_BUG("This can fail if there is a port conflict on the test system", "XECS-1924"); CPPUNIT_ASSERT_MESSAGE("server socket failure", server->isOk()); OsSocket* client = new OsConnectionSocket(8021, "localhost"); CPPUNIT_ASSERT_MESSAGE("client socket failure", client->isOk()); OsSocket* serverClient = server->accept(1000); CPPUNIT_ASSERT_MESSAGE("socket server failed to accept connection", serverClient != NULL); // Begin read/write test const char* msg = "hello\n"; int len = strlen(msg) + 1; // +1 for NULL int bytesWritten = client->write(msg, len); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes", bytesWritten, len); char recvBuf[1024]; int bytesRead = serverClient->read(recvBuf, sizeof(recvBuf) - 1); CPPUNIT_ASSERT_EQUAL_MESSAGE("read correct number of bytes", len, bytesRead); ASSERT_STR_EQUAL_MESSAGE("message same as was sent", msg, recvBuf); const char *resp = "bye"; len = strlen(resp) + 1; // +1 for NULL bytesWritten = serverClient->write(resp, len); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes on 2nd msg", len, bytesWritten); bytesRead = client->read(recvBuf, sizeof(recvBuf) - 1); CPPUNIT_ASSERT_EQUAL_MESSAGE("read correct number of bytes on 2nd msg", len, bytesRead); CPPUNIT_ASSERT_EQUAL_MESSAGE("write correct number of bytes on 2nd msg", len, bytesWritten); ASSERT_STR_EQUAL_MESSAGE("2nd message same as was sent", resp, recvBuf); serverClient->close(); client->close(); server->close(); delete client; delete server; }
OsSocket* SipTlsServer::buildClientSocket(int hostPort, const char* hostAddress, const char* localIp) { OsSocket* socket = NULL; #ifdef SIP_TLS # ifdef SIP_TLS_NSS socket = new OsTLSClientConnectionSocket(hostPort, hostAddress, mCertNickname, mCertPassword, mDbLocation, 0, localIp, mSipUserAgent); # else socket = new OsSSLConnectionSocket(hostPort, hostAddress); # endif #else // Create the socket in non-blocking mode so it does not block // while conecting socket = new OsConnectionSocket(hostPort, hostAddress, FALSE, localIp); #endif if (socket) { socket->makeBlocking(); } return(socket); }
int SipLSendCommand::execute(int argc, char* argv[]) { int commandStatus = CommandProcessor::COMMAND_FAILED; UtlString messageBuffer; char buffer[1025]; int bufferSize = 1024; int charsRead; printf("send command with %d arguments\n", argc); if(argc != 5) { UtlString usage; getUsage(argv[0], &usage); printf("%s", usage.data()); } else { UtlString protocol(argv[2]); protocol.toUpper(); UtlString hostAddress(argv[3]); int hostPort = atoi(argv[4]); FILE* sipMessageFile = fopen(argv[1], "r"); if(sipMessageFile && hostPort > 0 && !hostAddress.isNull() && (protocol.compareTo("TCP") == 0 || protocol.compareTo("UDP") == 0)) { //printf("opened file: \"%s\"\n", argv[1]); do { charsRead = fread(buffer, 1, bufferSize, sipMessageFile); if(charsRead > 0) { messageBuffer.append(buffer, charsRead); } } while(charsRead); fclose(sipMessageFile); OsSocket* writeSocket = NULL; if(protocol.compareTo("TCP") == 0) writeSocket = new OsConnectionSocket(hostPort ,hostAddress); else if(protocol.compareTo("UDP") == 0) writeSocket = new OsDatagramSocket(hostPort ,hostAddress); //printf("Read file contents:\n%s\n====END====\n", messageBuffer.data()); //SipMessage message(messageBuffer.data()); int bytesSent; if((bytesSent = writeSocket->write(messageBuffer.data(), messageBuffer.length())) > 0) { commandStatus = CommandProcessor::COMMAND_SUCCESS; } else { printf("Failed to send SIP message"); } printf("Send message with %d bytes\n", bytesSent); } else if(!sipMessageFile) { printf("send file: \"%s\" does not exist\n", argv[1]); commandStatus = CommandProcessor::COMMAND_FAILED; } else if(hostPort <= 0) { printf("Invalid destination port: %s\n", argv[4]); commandStatus = CommandProcessor::COMMAND_FAILED; } else if(hostAddress.isNull()) { printf("Invalid destination address: %s\n", argv[3]); commandStatus = CommandProcessor::COMMAND_FAILED; } else if(protocol.compareTo("TCP") && protocol.compareTo("UDP")) { printf("Invalid protocol: %s\n", argv[2]); commandStatus = CommandProcessor::COMMAND_FAILED; } } return(commandStatus); }
SipClient* SipProtocolServerBase::createClient(const char* hostAddress, int hostPort, const char* localIp) { UtlString remoteHostAddr; UtlBoolean clientStarted = FALSE; mClientLock.acquireWrite(); SipClient* client = getClient(hostAddress, hostPort, localIp); if(! client) { # if TEST_CLIENT_CREATION OsSysLog::add(FAC_SIP, PRI_DEBUG, "SipProtocolServerBase::createClient( %s, %d )", hostAddress, hostPort); # endif if(!portIsValid(hostPort)) { hostPort = mDefaultPort; # if TEST_CLIENT_CREATION OsSysLog::add(FAC_SIP, PRI_DEBUG, "SipProtocolServerBase::createClient port defaulting to %d", hostPort); # endif } OsTime time; OsDateTime::getCurTimeSinceBoot(time); long beforeSecs = time.seconds(); OsSocket* clientSocket = buildClientSocket(hostPort, hostAddress, localIp); OsDateTime::getCurTimeSinceBoot(time); long afterSecs = time.seconds(); if(afterSecs - beforeSecs > 1) { OsSysLog::add(FAC_SIP, PRI_WARNING, "SIP %s socket create for %s:%d took %d seconds", mProtocolString.data(), hostAddress, hostPort, (int)(afterSecs - beforeSecs)); } UtlBoolean isOk = clientSocket->isOk(); int writeWait = 3000; // mSec UtlBoolean isReadyToWrite = clientSocket->isReadyToWrite(writeWait); if(!isReadyToWrite) { OsSysLog::add(FAC_SIP, PRI_WARNING, "SIP %s socket %s:%d not ready for writing after %d seconds", mProtocolString.data(), hostAddress, hostPort, (int) (writeWait/1000)); } if(isOk && isReadyToWrite) { #ifdef TEST osPrintf("Socket OK, creating client\n"); #endif client = new SipClient(clientSocket) ; if (client && mSipUserAgent->getUseRport() && clientSocket->getIpProtocol() == OsSocket::UDP) { client->setSharedSocket(TRUE) ; } #ifdef TEST osPrintf("Created client\n"); #endif if(mSipUserAgent) { client->setUserAgent(mSipUserAgent); } if (clientSocket->getIpProtocol() != OsSocket::UDP) { //osPrintf("starting client\n"); clientStarted = client->start(); if(!clientStarted) { osPrintf("SIP %s client failed to start\n", mProtocolString.data()); } } OsSysLog::add(FAC_SIP, PRI_DEBUG, "Sip%sServer::createClient client: %p %s -> %s:%d", mProtocolString.data(), client, localIp, hostAddress, hostPort); mClientList.push(client); } // The socket failed to be connected else { if(clientSocket) { if (!mSipUserAgent->getUseRport() || (clientSocket->getIpProtocol() == OsSocket::TCP)) { delete clientSocket; } clientSocket = NULL; } OsSysLog::add(FAC_SIP, PRI_WARNING, "Sip%sServer::createClient client %p Failed to create socket %s -> %s:%d", mProtocolString.data(), this, localIp, hostAddress, hostPort); } } int isBusy = FALSE; if(client) { isBusy = client->isInUseForWrite(); if(!isBusy) client->markInUseForWrite(); } mClientLock.releaseWrite(); if(client && isBusy) { if(!waitForClientToWrite(client)) client = NULL; } return(client); }
main(int argc, char* argv[]) #endif { PT_TEST_BEGIN; OsSocket* s = new OsDatagramSocket(8020, "localhost"); char* msg = "hello\n"; int len = strlen(msg); int bytesWritten = s->write(msg, len); char recvBuf[RECV_BUF_LEN]; char eMsg[100]; char* resp; OsServerSocket* server = NULL; OsSocket* client = NULL; OsSocket* serverClient = NULL; int bytesRead; printf("wrote UDP %d of %d bytes\n", bytesWritten, len); PT_TEST_ASSERT(len == bytesWritten, "OsDatagramSOcket::write failed to write correct number of bytes", PT_TEST_CONTINUE); s->close(); delete s; server = new OsServerSocket(50, 8080); client = new OsConnectionSocket(8080, "localhost"); serverClient = server->accept(); PT_TEST_ASSERT(serverClient != NULL, "socket server failed to accept connection", PT_TEST_ABORT); bytesWritten = client->write(msg, len); sprintf(eMsg, "wrote TCP %d of %d bytes", bytesWritten, len); PT_TEST_ASSERT(len == bytesWritten, eMsg, PT_TEST_CONTINUE); bytesRead = serverClient->read(recvBuf, RECV_BUF_LEN - 1); if(bytesRead > 0) { recvBuf[bytesRead] = '\0'; printf("received %d of %d butes: \"%s\"\n", bytesRead, len, recvBuf); } PT_TEST_ASSERT(len == bytesRead, eMsg, PT_TEST_CONTINUE); PT_TEST_ASSERT(strcmp(msg, recvBuf) == 0, "message is not the same as was sent", PT_TEST_CONTINUE); resp = "bye"; len = strlen(resp); bytesWritten = serverClient->write(resp, len); sprintf(eMsg, "wrote TCP %d of %d bytes", bytesWritten, len); PT_TEST_ASSERT(len == bytesWritten, eMsg, PT_TEST_CONTINUE); bytesRead = client->read(recvBuf, RECV_BUF_LEN - 1); if(bytesRead > 0) { recvBuf[bytesRead] = '\0'; printf("received %d of %d butes: \"%s\"\n", bytesRead, len, recvBuf); } PT_TEST_ASSERT(len == bytesRead, eMsg, PT_TEST_CONTINUE); PT_TEST_ASSERT(strcmp(resp, recvBuf) == 0, "response is not the same as was sent", PT_TEST_CONTINUE); serverClient->close(); client->close(); server->close(); delete serverClient; delete client; delete server; // Note: no semicolon PT_TEST_END }