7 #include <crypto++/filters.h>
8 #include <crypto++/hex.h>
9 #include <crypto++/randpool.h>
10 #include <crypto++/files.h>
12 #include <crypto++/rsa.h>
13 #include <crypto++/des.h>
14 #include <crypto++/sha.h>
16 using namespace CryptoPP;
29 #include <jvoiplib/jvoipsession.h>
30 #include <jvoiplib/jvoiprtptransmission.h>
33 #define VOIP_PORT 5000
43 using namespace VSFileSystem;
46 void CheckVOIPError(
int val )
50 string error = JVOIPGetErrorString( val );
51 cerr<<
"!!! JVOIP ERROR : "<<error<<endl;
56 #ifdef NETCOMM_PORTAUDIO
57 void CheckPAError( PaError err,
const char *fun )
62 cerr<<
"!!! PORTAUDIO ERROR in "<<fun<<
": "<<Pa_GetErrorText( err )<<endl;
63 if (err == paHostError)
64 cerr<<
"!!! PA HOST ERROR : "<<Pa_GetHostError()<<
" - "<<Pa_GetErrorText( Pa_GetHostError() )<<endl;
70 void Pa_DisplayInfo( PaDeviceID
id )
72 const PaDeviceInfo *info = Pa_GetDeviceInfo(
id );
74 cout<<
"PORTAUDIO Device "<<
id<<
"---------------"<<endl;
75 cout<<
"\tName : "<<info->name<<endl;
76 cout<<
"\tStructure version : "<<info->structVersion<<endl;
77 cout<<
"\tmaxInputChannels : "<<info->maxInputChannels<<endl;
78 cout<<
"\tmaxOutputChannels : "<<info->maxOutputChannels<<endl;
79 cout<<
"\tnativeSampleFormats : ";
80 switch (info->nativeSampleFormats)
83 cout<<
"paFloat32"<<endl;
86 cout<<
"paInt16"<<endl;
89 cout<<
"paInt32"<<endl;
92 cout<<
"paInt24"<<endl;
95 cout<<
"paPackedInt24"<<endl;
101 cout<<
"paUInt8"<<endl;
103 case (paCustomFormat):
104 cout<<
"paCustomFormat"<<endl;
107 cout<<
"\tnumSampleRates : "<<info->numSampleRates<<endl;
108 for (
int i = 0;
i < info->numSampleRates;
i++)
109 cout<<
"\t\tRate "<<i<<
" = "<<info->sampleRates[i];
112 int Pa_RecordCallback(
void *inputBuffer,
114 unsigned long framesPerBuffer,
119 unsigned short *in = (
unsigned short*) inputBuffer;
121 if (Pa_GetCPULoad( netcomm->instream ) < MAX_PA_CPU_LOAD && in) {
122 netcomm->audio_inlength =
sizeof (
unsigned short)*framesPerBuffer;
123 cerr<<
"Input BUFFER SIZE = "<<netcomm->audio_inlength<<endl;
124 memset( netcomm->audio_inbuffer, 0,
MAXBUFFER );
126 memcpy( netcomm->audio_inbuffer, in, netcomm->audio_inlength );
131 int Pa_PlayCallback(
void *inputBuffer,
void *outputBuffer,
unsigned long framesPerBuffer, PaTimestamp outTime,
void *userdata )
134 unsigned short *out = (
unsigned short*) outputBuffer;
136 if (Pa_GetCPULoad( netcomm->outstream ) < MAX_PA_CPU_LOAD && netcomm->audio_outbuffer)
137 memcpy( out, netcomm->audio_outbuffer,
sizeof (
unsigned short)*framesPerBuffer );
143 void NetworkCommunication::private_init()
145 this->active =
false;
146 this->max_messages = 25;
147 this->method = ClientBroadcast;
152 this->session = NULL;
154 this->rtpparams = NULL;
157 #ifdef NETCOMM_PORTAUDIO
158 this->indev = paNoDevice;
159 this->outdev = paNoDevice;
160 this->devinfo = NULL;
163 #ifdef NETCOMM_WEBCAM
178 #ifdef NETCOMM_WEBCAM
182 if (Webcam->Init() == -1) {
185 cerr<<
"!!! NO WEBCAM SUPPORT !!!"<<endl;
188 _sock_set.addDownloadManager( _downloader );
190 _sock_set.addDownloadManager( _downloadServer );
194 #ifdef NETCOMM_PORTAUDIO
197 CheckPAError( Pa_Initialize(),
"Pa_Initialize" );
200 int nbdev = Pa_CountDevices();
201 for (
int i = 0;
i < nbdev;
i++)
206 this->indev = Pa_GetDefaultInputDeviceID();
207 this->outdev = Pa_GetDefaultOutputDeviceID();
209 this->sample_rate = 11025;
210 this->audio_inlength = 0;
214 crypto_method = cryptomethod;
220 bool create_keys =
false;
224 if (err1 >
Ok || err2 >
Ok) {
243 #ifdef NETCOMM_WEBCAM
255 #ifdef NETCOMM_PORTAUDIO
266 assert( nb < 65536 );
267 this->max_messages = nb;
272 this->commClients.push_back( clt );
275 if (*(this->webcamClient) && clt->webcam) {
277 assert( (*cltit) == clt );
278 this->webcamClient = cltit;
281 CheckVOIPError( this->session->AddDestination( ntohl( clt->cltadr.inaddr() ),
VOIP_PORT ) );
287 this->commClients.remove( clt );
289 CheckVOIPError( this->session->AddDestination( ntohl( clt->cltadr.inaddr() ),
VOIP_PORT ) );
304 if (this->message_history.size() == this->max_messages)
305 this->message_history.pop_front();
306 this->message_history.push_back( message );
308 if (method == ServerUnicast) {
312 encrypted = this->EncryptBuffer( message.c_str(), message.length() );
321 }
else if (method == ClientBroadcast) {
324 for (it = commClients.begin(); it != commClients.end(); it++) {
327 encrypted = this->EncryptBuffer( message.c_str(), message.length() );
352 if (method == ServerUnicast) {
358 encrypted = this->EncryptBufer( this->audio_inbuffer, this->audio_inlength );
367 }
else if (method == ClientBroadcast) {
370 for (it = commClients.begin(); it != commClients.end(); it++) {
371 if ( (*it)->portaudio ) {
374 encrypted = this->EncryptBufer( this->audio_inbuffer, this->audio_inlength );
397 #ifdef NETCOMM_PORTAUDIO
404 decrypted = DecryptBuffer( sndbuffer, length );
405 memcpy( audio_outbuffer, decrypted.c_str(), decrypted.length() );
408 memcpy( audio_outbuffer, sndbuffer, length );
416 if (this->message_history.size() == this->max_messages)
417 this->message_history.pop_front();
421 decrypted = DecryptBuffer( message.c_str(), message.length() );
426 this->message_history.push_back( message );
470 this->session =
new JVOIPSession;
471 this->session->SetSampleInterval( 100 );
473 this->params =
new JVOIPSessionParams;
474 this->rtpparams =
new JVOIPRTPTransmissionParams;
476 CheckVOIPError( this->session->Create( *(this->params) ) );
479 #ifdef NETCOMM_PORTAUDIO
482 CheckPAError( Pa_OpenStream( &this->instream,
483 this->indev, 1, paInt16, NULL,
484 paNoDevice, 0, paInt16, NULL,
485 this->sample_rate, 256, 0, paNoFlag,
486 Pa_RecordCallback, (
void*)
this ),
"Pa_OpenStream(instream)" );
489 CheckPAError( Pa_OpenStream( &this->outstream,
490 paNoDevice, 0, paInt16, NULL,
491 this->outdev, 1, paInt16, NULL,
492 this->sample_rate, 256, 0, paNoFlag,
493 Pa_PlayCallback, (
void*)
this ),
"Pa_OpenStream(outstream)" );
495 CheckPAError( Pa_StartStream( this->instream ),
"Pa_StartStream(instream)" );
497 CheckPAError( Pa_StartStream( this->outstream ),
"Pa_StartStream(outstream)" );
501 #ifdef NETCOMM_WEBCAM
503 this->Webcam->StartCapture();
507 this->freq = frequency;
513 this->active =
false;
514 #ifdef NETCOMM_PORTAUDIO
516 CheckPAError( Pa_StopStream( this->instream ),
"Pa_StopStream(instream)" );
517 CheckPAError( Pa_StopStream( this->outstream ),
"Pa_StopStream(outstream)" );
519 CheckPAError( Pa_CloseStream( this->instream ),
"Pa_CloseStream(instream)" );
520 CheckPAError( Pa_CloseStream( this->outstream ),
"Pa_CloseStream(outstream)" );
524 CheckVOIPError( this->session->Destroy() );
526 delete this->session;
530 delete this->rtpparams;
532 #ifdef NETCOMM_WEBCAM
534 this->Webcam->EndCapture();
548 delete this->session;
552 delete this->rtpparams;
554 #ifdef NETCOMM_PORTAUDIO
556 CheckPAError( Pa_Terminate(),
"Pa_Terminate" );
558 #ifdef NETCOMM_WEBCAM
560 this->Webcam->Shutdown();
573 #ifdef NETCOMM_WEBCAM
575 return Webcam->jpeg_buffer;
583 #ifdef NETCOMM_WEBCAM
584 if ( (*webcamClient) && bufitem ) {
585 if ( !bufitem->done() ) {
589 length = bufitem->getSize();
590 wshot =
new char[bufitem->getSize()+1];
591 memcpy( wshot, bufitem->getBuffer().get(), bufitem->getSize() );
592 wshot[bufitem->getSize()] = 0;
593 _downloader->addItem( bufitem );
602 #ifdef NETCOMM_WEBCAM
613 #ifdef NETCOMM_WEBCAM
615 this->SwitchWebcam();
621 #ifdef NETCOMM_WEBCAM
627 if ( (*this->webcamClient) )
630 it = commClients.begin();
631 for (; it != commClients.end() && !found; it++)
636 for (it = commClients.begin(), found =
false; it != webcamClient && !found; it++)
648 _downloader->addItem( bufitem );
665 void NetworkCommunication::GenerateKey()
668 if (crypto_method ==
"rsa") {
671 randPool.Put( (byte*) this->seed.c_str(), this->seed.length() );
673 RSAES_OAEP_SHA_Decryptor priv( randPool, this->key_length );
674 HexEncoder privFile(
new FileSink( privKeyFilename.c_str() ) );
675 HexEncoder privstr(
new StringSink( privkey ) );
676 priv.DEREncode( privFile );
677 privFile.MessageEnd();
679 RSAES_OAEP_SHA_Encryptor pub( priv );
680 HexEncoder pubFile(
new FileSink( pubKeyFilename.c_str() ) );
681 HexEncoder pubstr(
new StringSink( pubkey ) );
682 pub.DEREncode( pubFile );
683 pubFile.MessageEnd();
684 }
else if (crypto_method ==
"3des") {
687 static DES_EDE3_Encryption des_encryptor( (byte*) privkey.c_str(), 24 );
688 static DES_EDE3_Decryption des_decryptor( (byte*) privkey.c_str(), 24 );
695 string NetworkCommunication::EncryptBuffer(
const char *
buffer,
unsigned int length )
699 if (crypto_method ==
"rsa") {
700 StringSource pubstr( pubkey,
true,
new HexDecoder );
701 RSAES_OAEP_SHA_Encryptor pub( pubstr );
703 randPool.Put( (byte*) seed.c_str(), seed.length() );
705 StringSource( buffer,
true,
new PK_EncryptorFilter( randPool, pub,
new HexEncoder(
new StringSink( result ) ) ) );
706 }
else if (crypto_method ==
"3des") {
747 string NetworkCommunication::DecryptBuffer(
const char *buffer,
unsigned int length )
751 if (crypto_method ==
"rsa") {
752 StringSource privstr( privkey,
true,
new HexDecoder );
753 RSAES_OAEP_SHA_Decryptor priv( privstr );
756 StringSource( buffer,
true,
new HexDecoder(
new PK_DecryptorFilter( priv,
new StringSink( result ) ) ) );
758 StringSource( buffer,
true,
new HexDecoder(
new PK_DecryptorFilter( randPool, priv,
new StringSink( result ) ) ) );
760 }
else if (crypto_method ==
"3des") {}
else {}