⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sipclient.cpp

📁 KphoneSI (kpsi) is a SIP (Session Initiation Protocol) user agent for Linux, with which you can in
💻 CPP
📖 第 1 页 / 共 4 页
字号:
	} else {		msg->setTimeTick( 500 ); // T1	}	msg->incrSendCount();	// Error in reguest uri	if( msg->getRequestUri().reqUri().contains( ' ' ) ) {		QString s = msg->getRequestUri().reqUri();		while( s.contains( ' ' ) ) {			s.remove( s.find( ' ' ), 1 );		}		msg->setRequestUri( SipUri( s ) );	}	// Announce that we're sending a message	if( debug > 0)  printf("=====SipClient: <<<<<\n send Request  ");	if( ( debug == 1) || (debug >= 3) ) {	    printf( "at: %s.%03d\n", QTime::currentTime().toString().latin1(), QTime::currentTime().msec());	    printf( "%s",msg->message().data() );	    printf( "<<<<<\n");	}	// Send the message	TCPMessageSocket *tcpSocket = 0;	if( isTcpSocket() ) {		bool createTcpSocket = true;		TCPMessageSocketIterator it( getTcpSocketList() );		if( useExplicitProxy ) {			for (it.toFirst(); it.current(); ++it) {				tcpSocket = it.current();				if( tcpSocket->cmpSocket( proxy, proxyport ) ) {					createTcpSocket = false;					break;				}			}			if( createTcpSocket ) {				tcpSocket = new TCPMessageSocket;				if( !tcpSocket->setHostnamePort( proxy, proxyport ) ) {					return false;				}				if( tcpSocket->connect( proxyport ) == -1 ) {					delete tcpSocket;					tcpSocket = 0;					return false;				} else {					if(debug>0) {					    printf( "=====SipClient:sending TCP to '%s:%d'\n ",	proxy.latin1(), proxyport );					}					tcpSockets.append( tcpSocket );				}			}		} else {			// send to the host/port in the request uri			QString sendtoaddr;			if( msg->getRequestUri().hasMaddrParam() ) {				sendtoaddr = msg->getRequestUri().getMaddrParam();			} else {				sendtoaddr = msg->getRequestUri().getHostname();			}			sendtoaddr = getSipProxySrv( sendtoaddr );						if( sendtoaddr.contains( ':' ) ) {				unsigned int port = sendtoaddr.mid( sendtoaddr.find( ':' ) + 1 ).toUInt();				msg->getRequestUri().setPortNumber( port );				sendtoaddr = sendtoaddr.left( sendtoaddr.find( ':' ) );			}			if(debug>0) printf( "=====SipClient: Sending TCP to '%s:%d'\n ", sendtoaddr.latin1(),					msg->getRequestUri().getPortNumber() );						for (it.toFirst(); it.current(); ++it) {				tcpSocket = it.current();				if( tcpSocket->cmpSocket( sendtoaddr, msg->getRequestUri().getPortNumber() ) ) {					createTcpSocket = false;					break;				}			}			if( createTcpSocket ) {				tcpSocket = new TCPMessageSocket;				if( !tcpSocket->setHostnamePort( sendtoaddr, msg->getRequestUri().getPortNumber() ) ) {					return false;				}				if( tcpSocket->connect( msg->getRequestUri().getPortNumber() ) == -1 ) {					delete tcpSocket;					tcpSocket = 0;					return false;				} else {					tcpSockets.append( tcpSocket );				}			}		}		if( tcpSocket != 0 ) {			const QString m = msg->message().utf8();			if( tcpSocket->send( m.data(), m.length() ) == -1 ) {			tcpSockets.remove( tcpSocket );			tcpSocket = 0;			}		}	}		if( (tcpSocket == 0) && (!isTcpSocket()) ) {		UDPMessageSocket* s;		UDPMessageSocket sendsocket;		if (symmetricmode) {			s = &listener;		} else {			s = &sendsocket;	}		// Choose destination		if( useExplicitProxy ) {			if( !s->setHostname( proxy ) ) { return false; }			if( debug>0 ) {				printf( "to '%s:%d' (UDP)\n", proxy.latin1(), proxyport );			}			s->connect( proxyport );		} else {			// send to the host/port in the request uri			QString sendtoaddr;			unsigned int sendtoport = msg->getRequestUri().getPortNumber();			if( msg->getRequestUri().hasMaddrParam() ) {				sendtoaddr = msg->getRequestUri().getMaddrParam();			} else {				SipUri route( msg->getHeaderData( SipHeader::Route ) );				if( route.uri().contains( ";lr" ) ) {					sendtoaddr = route.getHostname();					sendtoport = route.getPortNumber();				} else {					sendtoaddr = msg->getRequestUri().getHostname();					sendtoaddr = getSipProxySrv( sendtoaddr );					if( sendtoaddr.contains( ':' ) ) {						unsigned int port =							sendtoaddr.mid( sendtoaddr.find( ':' ) + 1 ).toUInt();						msg->getRequestUri().setPortNumber( port );						sendtoaddr = sendtoaddr.left( sendtoaddr.find( ':' ) );					}						sendtoport = msg->getRequestUri().getPortNumber();				}			}			if(debug>0) {			    printf( "=====SipCient Sending UDP to '%s:%d' ", sendtoaddr.latin1(), sendtoport );			}			if( !s->setHostname( sendtoaddr ) ) { return false; }			s->connect( sendtoport );		}		const QString m = msg->message().utf8();		s->send( m.data(), m.length() );	}	return true;}void SipClient::setSymmetricMode( bool newmode){	symmetricmode = newmode;}void SipClient::sendResponse( SipMessage *msg, bool contact ){	MessageSocket *outsocket = 0;	SipVia topvia;	QString sendaddr;	bool isTCP=false;		// Calculate content length	msg->insertHeader( SipHeader::Content_Length, QString::number( msg->messageBody().utf8().length() ) );	// Advertise shamelesslysipvialist	msg->insertHeader( SipHeader::User_Agent, getUserAgent() );	// If this rquest requires the contact header, add it	if( contact ) {		msg->getContactList().addToHead( contacturi );	}	// Indicate what methods we allow if this is an answer to an OPTIONS	if( msg->getHeaderData( SipHeader::CSeq ).contains( "OPTIONS" ) ) {		msg->insertHeader( SipHeader::Allow,			"INVITE, OPTIONS, ACK, BYE, MSG, CANCEL, MESSAGE, SUBSCRIBE, NOTIFY, INFO, REFER" );	}	// Use via to tell us where to send it and how	topvia = msg->getViaList().getTopmostVia();	switch( topvia.getTransport() ) {		case SipVia::UDP:			if(debug>0 ) printf( "=====SipClient:<<<<<\n Sending UDP Response " );			if (symmetricmode) {				outsocket = &listener;			} else {				outsocket = new UDPMessageSocket;			}			break;		case SipVia::TCP:			isTCP=true;			if( debug>0 ) printf( "=====SipClient: <<<<<\n Sending TCP Response" );			outsocket = new TCPMessageSocket;			break;		case SipVia::TLS:			if(debug>0) printf( "=====SipClient: TLS in top via, not supported (full TLS support not implemented)\n" );			break;		case SipVia::BadTransport:			if(debug>0) printf( "=====SipClient: Bad transport on incoming Via\n" );			break;	}	// If transport was bad, no use sending	if( !outsocket ) return;	// maddr, received, sentby	if( topvia.hasMaddrParam() ) {		sendaddr = topvia.getMaddrParam();	} else if( topvia.hasReceivedParam() ) {		sendaddr = topvia.getReceivedParam();	} else {		sendaddr = topvia.getHostname();	}	// Announce where we're sending	if(debug>0) {	printf( "to '%s' port %d ", sendaddr.utf8().data(), topvia.getPortNumber() );	}		if( !outsocket->setHostname( sendaddr.utf8().data() ) ) {		if (outsocket != &listener) {		if(debug>0) printf("=====sending outsocket deleted\n");			delete outsocket;		}		return;	}	//in case of success we queue our send-socket to delete it later	if(isTCP) {	    if( outsocket->connect( topvia.getPortNumber() ) == -1 ) {		delete outsocket;		outsocket = 0;		return;	    } else {		sndSockets.append( (TCPMessageSocket *)outsocket );	    }	} else {	outsocket->connect( topvia.getPortNumber() );	}	// Announce what we're sending	if( debug > 0)  printf("=====SipClient:<<<<<\n sendMessage ");	if( ( debug == 1) || (debug >= 3) ) {	    printf( " at: %s.%03d\n",QTime::currentTime().toString().latin1(), QTime::currentTime().msec());	    printf( "%s<<<<<\n",msg->message().data() );	}	// Send it	const QString m = msg->message().utf8();	outsocket->send( m.data(), m.length() );		//clean UDP sockets immediately but TCPSockets deferred, as some proxies dislike too early FIN	if ((outsocket != &listener) && !isTCP) {		delete outsocket;	}	if(isTCP && (sndSockets.count() >3)) {	sndSockets.removeFirst();	}}void SipClient::sendRaw( SipMessage *msg ){	if( isTcpSocket() ) {		return;	}	// Announce that we're sending	if( debug >0 )  printf("=====SipClient:<<<<<\n sendRaw ");	if( ( debug == 1) || (debug >= 3) ) {	    printf( "at %s.%03d\n", QTime::currentTime().toString().latin1(), QTime::currentTime().msec() );	    printf( "%s<<<<<\n",msg->message().data() );	}		UDPMessageSocket *sendsocket;	if (symmetricmode) {		sendsocket = &listener;	} else {		sendsocket = new UDPMessageSocket;	}	// Choose destination	if( ( msg->getType() != SipMessage::Response ) && useExplicitProxy ) {		if( !sendsocket->setHostname( proxy ) ) {			if (sendsocket != &listener) {				delete sendsocket;			}			return; 		}		sendsocket->connect( proxyport );	} else {		// Send to whatever is in the request uri		QString sendtoaddr;		if( msg->getRequestUri().hasMaddrParam() ) {			sendtoaddr = msg->getRequestUri().getMaddrParam();		} else {			sendtoaddr = msg->getRequestUri().getHostname();		}				if( !sendsocket->setHostname( sendtoaddr ) ) { 			if (sendsocket != &listener) {				delete sendsocket;			}			return; 		}		sendsocket->connect( msg->getRequestUri().getPortNumber() );	}	const QString m = msg->message().utf8();	sendsocket->send( m.data(), m.length() );	if (sendsocket != &listener) {		delete sendsocket;	}}void SipClient::addCall( SipCall *call ){	if( !calls.contains( call ) ) {		calls.append( call );	}	callListUpdated();}void SipClient::callTypeUpdated( void ){	callListUpdated();}void SipClient::deleteCall( SipCall *call ){	calls.remove( call );	callListUpdated();}void SipClient::setExplicitProxyMode( bool eproxy ){	useExplicitProxy = eproxy;}void SipClient::setExplicitProxyAddress( const QString &newproxy ){	if( newproxy.contains( ':' ) ) {		proxy = newproxy.left( newproxy.find( ':' ) );		proxyport = newproxy.mid( newproxy.find( ':' ) + 1 ).toUInt();	} else {		proxy = newproxy;		proxyport = 5060;	}}QString SipClient::getExplicitProxyAddress( void ){	QString uri = "<sip:" + proxy;	if( proxyport != 5060 ) {		uri += ":" + QString::number( proxyport, 10 );	}	uri += ";lr>";	return uri;}void SipClient::setUser( SipUser *u ){	user = u;}void SipClient::sendQuickResponse( SipMessage *origmessage, const SipStatus &status,	const QString &body, const MimeContentType &bodytype ){if( ( debug == 1) || (debug >= 3) ) printf("=====SipClient:<<<<<\n sendQuickResponse:\n");	MessageSocket *outsocket = 0;	SipMessage *msg = new SipMessage;	SipVia topvia;	QString sendaddr;	msg->setType( SipMessage::Response );	msg->setStatus( status );	bool isTCP=false;	// Copy via list exactly	msg->setViaList( origmessage->getViaList() );	msg->insertHeader( SipHeader::From, origmessage->getHeaderData( SipHeader::From ) );	msg->insertHeader( SipHeader::To, origmessage->getHeaderData( SipHeader::To ) );	msg->insertHeader( SipHeader::CSeq, origmessage->getHeaderData( SipHeader::CSeq ) );	msg->insertHeader( SipHeader::Call_ID, origmessage->getHeaderData( SipHeader::Call_ID ) );	if( origmessage->hasHeader( SipHeader::Require ) ) {		msg->insertHeader( SipHeader::Unsupported, origmessage->getHeaderData( SipHeader::Require ) );	}	if( ( status.getCode() >= 300 ) && ( status.getCode() < 400 ) ) {		msg->getContactList().addToHead( forwarduri );	}	if( status.getCode() == 501 ) {		msg->insertHeader( SipHeader::Allow,			"INVITE, OPTIONS, ACK, BYE, MSG, CANCEL, MESSAGE, SUBSCRIBE, NOTIFY, INFO, REFER" );	}	if( bodytype != MimeContentType::null ) {		msg->insertHeader( SipHeader::Content_Type, bodytype.type() );	}	msg->setBody( body );	// Calculate content length	msg->insertHeader( SipHeader::Content_Length, QString::number( msg->messageBody().utf8().length() ) );	// Advertise shamelessly	msg->insertHeader( SipHeader::User_Agent, getUserAgent() );	// Use via to tell us where to send it and how	topvia = msg->getViaList().getTopmostVia();	switch( topvia.getTransport() ) {		case SipVia::UDP:			if( ( debug == 1) || (debug >= 3) ) printf( "UDP " );			if (symmetricmode) {				outsocket = &listener;			} else {				outsocket = new UDPMessageSocket;			}			break;		case SipVia::TCP:			if( ( debug == 1) || (debug >= 3) ) printf( "TCP ");			isTCP=true;			outsocket = new TCPMessageSocket;			break;		case SipVia::TLS:			if(debug>0) printf( "=====SipClient: TLS in top via, not supported (full TLS support not implemented)\n" );			break;		case SipVia::BadTransport:			if(debug>0) printf( "=====SipClient: Bad transport on incoming Via\n" );			break;	}	// If transport is bad, no use sending	if( !outsocket ) {		delete msg;		return;	}	// maddr, received, sentby	if( topvia.hasMaddrParam() ) {		sendaddr = topvia.getMaddrParam();	} else if( topvia.hasReceivedParam() ) {		sendaddr = topvia.getReceivedParam();	} else {		sendaddr = topvia.getHostname();	}	// Announce where we're sending	if(debug>0) printf( "to '%s' port %d  ",			sendaddr.latin1(), topvia.getPortNumber() );	if( !outsocket->setHostname( sendaddr.utf8().data() ) ) {		delete msg;		if (outsocket != &listener) {			perror("--HOPPLA6");			delete outsocket;		}		return;	}	//in case of success we queue our send-socket to delete it later	if(isTCP) {	    if( outsocket->connect( topvia.getPortNumber() ) == -1 ) {		delete outsocket;		outsocket = 0;		return;	    } else {		sndSockets.append( (TCPMessageSocket *)outsocket ); 	    }	} else {	outsocket->connect( topvia.getPortNumber() );	}	// Announce what we're sending		if( ( debug == 1) || (debug >= 3) ) {		printf( "at  %s.%03d\n",QTime::currentTime().toString().latin1(), QTime::currentTime().msec() );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -