PageRenderTime 57ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/qwaqvm/platforms/Cross/third-party/resiprocate-src-1.6/resip/stack/test/limpc.cxx

http://openqwaq.googlecode.com/
C++ | 1121 lines | 906 code | 126 blank | 89 comment | 144 complexity | 76ff542fd271fb625347a294e18c3357 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, LGPL-2.0, BSD-3-Clause
  1. #if defined(HAVE_CONFIG_HXX)
  2. #include "resip/stack/config.hxx"
  3. #endif
  4. #include <cstring>
  5. #include <cassert>
  6. #include <stdio.h>
  7. #include <signal.h>
  8. //#define USE_CURSES
  9. #ifdef USE_CURSES
  10. #include <ncurses.h>
  11. #else
  12. #include <iostream>
  13. #include <cstdio>
  14. #ifdef WIN32
  15. #include <io.h>
  16. #else
  17. #include <unistd.h>
  18. #endif
  19. typedef void WINDOW;
  20. // !ah! Really ought to check for ncurses and be a bit better behaved than this.
  21. #if !defined(TRUE)
  22. #define TRUE true
  23. #endif
  24. #if !defined(FALSE)
  25. #define FALSE false
  26. #endif
  27. char ACS_HLINE=1;
  28. char ACS_VLINE=2;
  29. WINDOW* stdscr=0;
  30. WINDOW* newwin(...) { return NULL; };
  31. void waddstr(WINDOW*, const char* text) { std::clog << text; };
  32. char getch()
  33. {
  34. char buf[1];
  35. int r = read(fileno(stdin),&buf,1);
  36. if ( r ==1 )
  37. {
  38. return buf[0];
  39. }
  40. return 0;
  41. };
  42. void werase(WINDOW*) {};
  43. void wrefresh(...) {};
  44. void mvhline(...) {};
  45. void refresh(...) {};
  46. void getmaxyx(...) {};
  47. void clearok(...) {};
  48. void waddch(...) {};
  49. void initscr(...) {};
  50. void cbreak(...) {};
  51. void noecho(...) {};
  52. void nonl(...) {};
  53. void intrflush(...) {};
  54. void keypad(...) {};
  55. void scrollok(...) {};
  56. void wmove(...) {};
  57. void mvvline(...) {};
  58. #endif
  59. #ifndef WIN32
  60. #include <sys/time.h>
  61. #include <sys/types.h>
  62. #include <unistd.h>
  63. #include <stdlib.h>
  64. #endif
  65. #include "rutil/Socket.hxx"
  66. #include "rutil/Logger.hxx"
  67. #include "resip/stack/SipStack.hxx"
  68. #include "resip/stack/Uri.hxx"
  69. #include "resip/stack/TuIM.hxx"
  70. #ifdef USE_SSL
  71. #include "resip/stack/ssl/Security.hxx"
  72. #endif
  73. static int myMain(int argc, char* argv[]);
  74. using namespace resip;
  75. using namespace std;
  76. #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
  77. static WINDOW* commandWin=0;
  78. static WINDOW* textWin=0;
  79. static WINDOW* statusWin=0;
  80. static TuIM* tuIM;
  81. static Uri dest;
  82. void
  83. displayPres()
  84. {
  85. werase(statusWin);
  86. for( int i=0; i<tuIM->getNumBuddies();i++)
  87. {
  88. Uri uri = tuIM->getBuddyUri(i);
  89. Data status;
  90. bool online = tuIM->getBuddyStatus(i,&status);
  91. const char* stat = (online)?"online":"offline";
  92. waddstr(statusWin,uri.getAor().c_str());
  93. waddstr(statusWin," ");
  94. waddstr(statusWin,stat);
  95. waddstr(statusWin," ");
  96. waddstr(statusWin,status.c_str());
  97. waddstr(statusWin,"\n");
  98. }
  99. wrefresh(statusWin);
  100. }
  101. class TestCallback: public TuIM::Callback
  102. {
  103. public:
  104. virtual void presenceUpdate(const Uri& dest, bool open, const Data& status );
  105. virtual void receivedPage( const Data& msg, const Uri& from ,
  106. const Data& signedBy, SignatureStatus sigStatus,
  107. bool wasEncryped );
  108. virtual void sendPageFailed( const Uri& dest,int respNumber );
  109. virtual void registrationFailed(const resip::Uri&, int respNumber);
  110. virtual void registrationWorked(const Uri& dest );
  111. virtual void receivePageFailed(const Uri& sender);
  112. };
  113. void
  114. TestCallback::presenceUpdate(const Uri& from, bool open, const Data& status )
  115. {
  116. const char* stat = (open)?"online":"offline";
  117. //cout << from << " set presence to " << stat << " " << status.c_str() << endl;
  118. waddstr(textWin,"Status: ");
  119. waddstr(textWin, from.getAor().c_str());
  120. waddstr(textWin," is ");
  121. waddstr(textWin,stat);
  122. waddstr(textWin," ");
  123. waddstr(textWin,status.c_str());
  124. waddstr(textWin,"\n");
  125. wrefresh(textWin);
  126. displayPres();
  127. }
  128. void
  129. TestCallback::receivedPage( const Data& msg, const Uri& from,
  130. const Data& signedBy, SignatureStatus sigStatus,
  131. bool wasEncryped )
  132. {
  133. //DebugLog(<< "In TestPageCallback");
  134. if ( dest != from )
  135. {
  136. dest = from;
  137. //cerr << "Set destination to <" << *mDest << ">" << endl;
  138. waddstr(textWin,"Set destination to ");
  139. waddstr(textWin, Data::from(dest).c_str());
  140. waddstr(textWin,"\n");
  141. }
  142. //cout << from;
  143. waddstr(textWin,"From: ");
  144. waddstr(textWin,from.getAor().c_str());
  145. if ( !wasEncryped )
  146. {
  147. //cout << " -NOT SECURE- ";
  148. waddstr(textWin," -NOT SECURE-");
  149. }
  150. else
  151. {
  152. waddstr(textWin," -secure-");
  153. }
  154. switch ( sigStatus )
  155. {
  156. case SignatureSelfSigned:
  157. //cout << " -self signed signature (bad)- ";
  158. waddstr(textWin,"bad signature");
  159. break;
  160. case SignatureIsBad:
  161. //cout << " -bad signature- ";
  162. waddstr(textWin,"bad signature");
  163. break;
  164. case SignatureNone:
  165. //cout << " -no signature- ";
  166. waddstr(textWin,"no signature");
  167. break;
  168. case SignatureTrusted:
  169. //cout << " <signed " << signedBy << " > ";
  170. waddstr(textWin,"signed ");
  171. waddstr(textWin,signedBy.c_str());
  172. break;
  173. case SignatureCATrusted:
  174. //cout << " <ca signed " << signedBy << " > ";
  175. waddstr(textWin,"ca signed " );
  176. waddstr(textWin,signedBy.c_str());
  177. break;
  178. case SignatureNotTrusted:
  179. //cout << " <signed " << signedBy << " NOT TRUSTED > ";
  180. waddstr(textWin,"untrusted signature ");
  181. waddstr(textWin,signedBy.c_str());
  182. break;
  183. }
  184. //cout << " says:" << endl;
  185. //cout << msg.escaped() << endl;
  186. waddstr(textWin, " says: ");
  187. waddstr(textWin, msg.escaped().c_str() );
  188. waddstr(textWin, "\n");
  189. wrefresh(textWin);
  190. }
  191. void
  192. TestCallback::sendPageFailed( const Uri& target, int respNum )
  193. {
  194. //InfoLog(<< "In TestErrCallback");
  195. // cerr << "Message to " << dest << " failed" << endl;
  196. Data num(respNum);
  197. waddstr(textWin,"Message to ");
  198. waddstr(textWin, Data::from(target).c_str());
  199. waddstr(textWin," failed (");
  200. waddstr(textWin,num.c_str());
  201. waddstr(textWin," response)\n");
  202. wrefresh(textWin);
  203. }
  204. void
  205. TestCallback::receivePageFailed( const Uri& target )
  206. {
  207. //InfoLog(<< "In TestErrCallback");
  208. // cerr << "Message to " << dest << " failed" << endl;
  209. waddstr(textWin,"Can not understand messager from ");
  210. waddstr(textWin, Data::from(target).c_str());
  211. waddstr(textWin,"\n");
  212. wrefresh(textWin);
  213. }
  214. void
  215. TestCallback::registrationFailed(const resip::Uri& target, int respNum )
  216. {
  217. Data num(respNum);
  218. waddstr(textWin,"Registration to ");
  219. waddstr(textWin, Data::from(target).c_str());
  220. waddstr(textWin," failed (");
  221. waddstr(textWin,num.c_str());
  222. waddstr(textWin," response)\n");
  223. wrefresh(textWin);
  224. }
  225. void
  226. TestCallback::registrationWorked(const resip::Uri& target)
  227. {
  228. waddstr(textWin,"Registration to ");
  229. waddstr(textWin, Data::from(target).c_str());
  230. waddstr(textWin," worked");
  231. wrefresh(textWin);
  232. }
  233. bool
  234. processStdin( Uri* dest, bool sign, bool encryp )
  235. {
  236. static unsigned int num=0;
  237. static char buf[1024];
  238. char c = getch();
  239. if ( c == 0 )
  240. {
  241. return true;
  242. }
  243. if ( c == '\f' )
  244. {
  245. clearok(textWin,TRUE);
  246. clearok(statusWin,TRUE);
  247. clearok(commandWin,TRUE);
  248. assert( num < sizeof(buf) );
  249. buf[num] = 0;
  250. werase(commandWin);
  251. waddstr(commandWin,buf);
  252. wrefresh(textWin);
  253. wrefresh(statusWin);
  254. wrefresh(commandWin);
  255. return true;
  256. }
  257. #if 0
  258. char junk[6];
  259. junk[0]=' ';
  260. junk[1]='0'+(c/100);
  261. junk[2]='0'+((c/10)%10);
  262. junk[3]='0'+(c%10);
  263. junk[4]=' ';
  264. junk[5]=0;
  265. waddstr(commandWin,junk);
  266. #endif
  267. if ( (c == '\a') || (c == '\b') || (c == 4 ) || (c == 0x7F) )
  268. {
  269. if ( num > 0 )
  270. {
  271. num--;
  272. }
  273. buf[num]=0;
  274. werase(commandWin);
  275. waddstr(commandWin,buf);
  276. wrefresh(commandWin);
  277. return true;
  278. }
  279. if ( (c == '\r') || (c == '\n') || (num+2>=sizeof(buf)) )
  280. {
  281. buf[num] =0;
  282. if ( (num>3) && (!strncmp("to:",buf,3)) )
  283. {
  284. buf[num] = 0;
  285. *dest = Uri(Data(buf+3));
  286. //cerr << "Set destination to <" << *dest << ">";
  287. waddstr(textWin,"Set destination to ");
  288. waddstr(textWin, Data::from(*dest).c_str());
  289. waddstr(textWin,"\n");
  290. wrefresh(textWin);
  291. }
  292. else if ( (num>4) && (!strncmp("add:",buf,4)) )
  293. {
  294. buf[num] = 0;
  295. Uri uri(Data(buf+4));
  296. //cerr << "Subscribing to buddy <" << uri << ">";
  297. waddstr(textWin, "Subscribing to ");
  298. waddstr(textWin, Data::from(uri).c_str());
  299. waddstr(textWin, "\n");
  300. wrefresh(textWin);
  301. tuIM->addBuddy( uri, Data::Empty );
  302. displayPres();
  303. }
  304. else if ( (num>=7) && (!strncmp("status:",buf,7)) )
  305. {
  306. buf[num] = 0;
  307. Data stat(buf+7);
  308. //cerr << "setting presence status to <" << stat << ">";
  309. waddstr(textWin,"Set presece status to <");
  310. waddstr(textWin,stat.c_str());
  311. waddstr(textWin,">\n");
  312. wrefresh(textWin);
  313. tuIM->setMyPresence( !stat.empty(), stat );
  314. }
  315. else if ( (num==1) && (!strncmp(".",buf,1)) )
  316. {
  317. //DebugLog( << "Got a period - end program" );
  318. return false;
  319. }
  320. else
  321. {
  322. if ( num >= 1 )
  323. {
  324. assert( num < sizeof(buf) );
  325. buf[num] = 0;
  326. Data text(buf);
  327. Data destValue = dest->getAor();
  328. DebugLog( << "Destination is " << destValue );
  329. Data encFor = Data::Empty;
  330. if (encryp)
  331. {
  332. encFor = dest->getAorNoPort();
  333. }
  334. DebugLog( << "Destination encrypt for is " << encFor );
  335. if ( tuIM->haveCerts(sign,encFor) )
  336. {
  337. waddstr(textWin,"To: ");
  338. waddstr(textWin,destValue.c_str());
  339. waddstr(textWin," ");
  340. waddstr(textWin,text.c_str());
  341. waddstr(textWin,"\n");
  342. wrefresh(textWin);
  343. tuIM->sendPage( text , *dest, sign , encFor );
  344. }
  345. else
  346. {
  347. waddstr(textWin,"Don't have aproperate certificates to sign and encrypt a message to ");
  348. waddstr(textWin,destValue.c_str());
  349. waddstr(textWin,"\n");
  350. wrefresh(textWin);
  351. }
  352. }
  353. }
  354. num = 0;
  355. werase(commandWin);
  356. wrefresh(commandWin);
  357. }
  358. else
  359. {
  360. buf[num++] = c;
  361. assert( num < sizeof(buf) );
  362. waddch(commandWin,c);
  363. wrefresh(commandWin);
  364. }
  365. return true;
  366. }
  367. int
  368. main(int argc, char* argv[])
  369. {
  370. #ifndef _WIN32
  371. if ( signal( SIGPIPE, SIG_IGN) == SIG_ERR)
  372. {
  373. cerr << "Couldn't install signal handler for SIGPIPE" << endl;
  374. exit(-1);
  375. }
  376. #endif
  377. int r;
  378. try
  379. {
  380. r = myMain( argc, argv );
  381. }
  382. catch( ... )
  383. {
  384. ErrLog( << "Got a exception passed all the way to the top of limp" );
  385. exit(-1);
  386. }
  387. return r;
  388. }
  389. static int
  390. myMain(int argc, char* argv[])
  391. {
  392. Log::initialize(Log::Cerr, Log::Err, argv[0]);
  393. Log::setLevel(Log::Warning);
  394. InfoLog(<<"Test Driver for IM Starting");
  395. int port = 5060;
  396. int tlsPort = 0;
  397. int dtlsPort = 0;
  398. Uri aor;
  399. bool haveAor=false;
  400. dest = Uri("sip:nobody@example.com");
  401. Data aorPassword;
  402. Uri contact("sip:user@");
  403. bool haveContact=false;
  404. Uri outbound;
  405. bool noRegister = false;
  406. Data tlsDomain = Data::Empty;
  407. Data sendMsg = Data::Empty;
  408. int numAdd=0;
  409. Data addList[100];
  410. int numPub=0;
  411. Data pubList[100];
  412. bool encryp=false;
  413. bool sign=false;
  414. Data key("password");
  415. bool useTls = true;
  416. bool noTls = false;
  417. bool noTcp = false;
  418. bool prefUdp = false;
  419. bool prefTls = false;
  420. bool prefDtls = false;
  421. bool prefTcp = false;
  422. bool noUdp = false;
  423. bool noV6 = false;
  424. bool noV4 = false;
  425. bool genUserCert = false;
  426. for ( int i=1; i<argc; i++)
  427. {
  428. if (!strcmp(argv[i],"-vv"))
  429. {
  430. Log::setLevel(Log::Stack);
  431. }
  432. else if (!strcmp(argv[i],"-v"))
  433. {
  434. Log::setLevel(Log::Info);
  435. }
  436. else if (!strcmp(argv[i],"-encrypt"))
  437. {
  438. encryp = true;
  439. }
  440. else if (!strcmp(argv[i],"-genUserCert"))
  441. {
  442. genUserCert = true;
  443. }
  444. else if (!strcmp(argv[i],"-noRegister"))
  445. {
  446. noRegister = true;
  447. }
  448. else if (!strcmp(argv[i],"-sign"))
  449. {
  450. sign = true;
  451. }
  452. else if (!strcmp(argv[i],"-tlsDomain"))
  453. {
  454. i++;
  455. assert( i<argc );
  456. tlsDomain = Data(argv[i]);
  457. }
  458. else if (!strcmp(argv[i],"-ssl"))
  459. {
  460. useTls = false;
  461. }
  462. else if (!strcmp(argv[i],"-noTcp"))
  463. {
  464. noTcp = true;
  465. }
  466. else if (!strcmp(argv[i],"-noTls"))
  467. {
  468. noTls = true;
  469. }
  470. else if (!strcmp(argv[i],"-noUdp"))
  471. {
  472. noUdp = true;
  473. }
  474. else if (!strcmp(argv[i],"-prefTcp"))
  475. {
  476. prefTcp = true;
  477. }
  478. else if (!strcmp(argv[i],"-prefTls"))
  479. {
  480. prefTls = true;
  481. }
  482. else if (!strcmp(argv[i],"-prefUdp"))
  483. {
  484. prefUdp = true;
  485. }
  486. else if (!strcmp(argv[i],"-noV6"))
  487. {
  488. noV6 = true;
  489. }
  490. else if (!strcmp(argv[i],"-noV4"))
  491. {
  492. noV4 = true;
  493. }
  494. else if (!strcmp(argv[i],"-port"))
  495. {
  496. i++;
  497. assert( i<argc );
  498. port = atoi( argv[i] );
  499. }
  500. else if (!strcmp(argv[i],"-tlsPort"))
  501. {
  502. i++;
  503. assert( i<argc );
  504. tlsPort = atoi( argv[i] );
  505. }
  506. else if (!strcmp(argv[i],"-dtlsPort"))
  507. {
  508. i++;
  509. assert( i<argc );
  510. dtlsPort = atoi( argv[i] );
  511. }
  512. else if (!strcmp(argv[i],"-aor"))
  513. {
  514. i++;
  515. assert( i<argc );
  516. try
  517. {
  518. aor = Uri(Data(argv[i]));
  519. }
  520. catch (...)
  521. {
  522. ErrLog( <<"AOR URI is not valid - must start with sip: ");
  523. exit(-1);
  524. }
  525. haveAor=true;
  526. }
  527. else if (!strcmp(argv[i],"-outbound"))
  528. {
  529. i++;
  530. assert( i<argc );
  531. try
  532. {
  533. outbound = Uri(Data(argv[i]));
  534. }
  535. catch (...)
  536. {
  537. ErrLog( <<"Outbound URI is not valid - must start with sip: ");
  538. exit(-1);
  539. }
  540. }
  541. else if (!strcmp(argv[i],"-send"))
  542. {
  543. i++;
  544. assert( i<argc );
  545. sendMsg = Data(argv[i]);
  546. }
  547. else if (!strcmp(argv[i],"-contact"))
  548. {
  549. i++;
  550. assert( i<argc );
  551. try
  552. {
  553. contact = Uri(Data(argv[i]));
  554. }
  555. catch (...)
  556. {
  557. ErrLog( <<"Contact URI is not valid - must start with sip: ");
  558. exit(-1);
  559. }
  560. haveContact=true;
  561. }
  562. else if (!strcmp(argv[i],"-add"))
  563. {
  564. i++;
  565. assert( i<argc );
  566. addList[numAdd++] = Data(argv[i]);
  567. assert( numAdd < 100 );
  568. try
  569. {
  570. // CJ TODO FIX
  571. //Uri uri( Data(argv[i]) );
  572. }
  573. catch (...)
  574. {
  575. ErrLog( <<"URI in -add is not valid - must start with sip: ");
  576. exit(-1);
  577. }
  578. }
  579. else if (!strcmp(argv[i],"-pub"))
  580. {
  581. i++;
  582. assert( i<argc );
  583. pubList[numPub++] = Data(argv[i]);
  584. assert( numPub < 100 );
  585. try
  586. {
  587. // CJ TODO FIX
  588. //Uri uri(Data(argv[i]));
  589. }
  590. catch (...)
  591. {
  592. ErrLog( <<"Pub URI is not valid - must start with sip: ");
  593. exit(-1);
  594. }
  595. }
  596. else if (!strcmp(argv[i],"-aorPassword"))
  597. {
  598. i++;
  599. assert( i<argc );
  600. aorPassword = Data(argv[i]);
  601. }
  602. else if (!strcmp(argv[i],"-to"))
  603. {
  604. i++;
  605. assert( i<argc );
  606. try
  607. {
  608. dest = Uri(Data(argv[i]));
  609. }
  610. catch (...)
  611. {
  612. ErrLog( <<"To URI is not valid - must start with sip: ");
  613. exit(-1);
  614. }
  615. }
  616. else if (!strcmp(argv[i],"-key"))
  617. {
  618. i++;
  619. assert( i<argc );
  620. key = Data(argv[i]);
  621. }
  622. else
  623. {
  624. clog <<"Bad command line opion: " << argv[i] << endl;
  625. clog <<"options are: " << endl
  626. << "\t [-v] [-vv] [-tls] [-port 5060] [-tlsport 5061]" << endl
  627. << "\t [-aor sip:alice@example.com] [-aorPassword password]" << endl
  628. << "\t [-to sip:friend@example.com] [-add sip:buddy@example.com]" << endl
  629. << "\t [-sign] [-encrypt] [-key secret]" << endl
  630. << "\t [-contact sip:me@example.com] " << endl
  631. << "\t [-outbound \"sip:example.com;lr\"] " << endl
  632. << "\t [-noRegister] " << endl
  633. << "\t [-pub sip:foo.com] " << endl
  634. << "\t [-tlsDomain foo.com] " << endl
  635. << "\t [-send myMessage] " << endl;
  636. clog << endl
  637. << " -v is verbose" << endl
  638. << " -vv is very verbose" << endl
  639. << " -noV6 don't use IPv6" << endl
  640. << " -noV4 don't use IPv4" << endl
  641. << " -noUdp don't use UDP" << endl
  642. << " -noTcp don't use TCP" << endl
  643. << " -noTls don't use TLS" << endl
  644. << " -prefUdp prefer UDP" << endl
  645. << " -prefTcp prefer TCP" << endl
  646. << " -prefTls prefer TLS" << endl
  647. << " -port sets the UDP and TCP port to listen on" << endl
  648. << " -tlsPort sets the port to listen for TLS on" << endl
  649. << " -tlsDomain domainName - sets tls and dtls to act as tls server instead of client" << endl
  650. << " -ssl - use ssl instead of tls" << endl
  651. << " -aor sets the proxy and user name to register with" << endl
  652. << " -aorPassword sets the password to use for registration" << endl
  653. << " -noRegister causes it not to register - by default the AOR is registered" << endl
  654. << " -to sets initial location to send messages to" << endl
  655. << " -outbound sets the outbound proxy" << endl
  656. << " -add adds a budy who's presence will be monitored" << endl
  657. << " -pub adds a State Agent to send publishes too" << endl
  658. << " -sign signs message you send and -encryp encrypt them " << endl
  659. << " -send takes a string (needs to be quoted if it has spaces) "
  660. << "and sends it as an IM " << endl
  661. << "\t(You need PKI certs for this to work)" << endl
  662. << " -key allows you to enter a secret used to load your private key."<< endl
  663. << " If you set the secret to - the system will querry you for it."<< endl
  664. << " -contact overrides your SIP contact - can be used for NAT games" << endl
  665. << "\t there can be many -add " << endl
  666. << " -genUserCert - generate a new user cert" << endl
  667. << " " << endl
  668. << "Examples" << endl
  669. << "An example command line for a user with account name alice at example.com is:" << endl
  670. << "\t" << argv[0] << " -aor \"alice@example.com\" -aorPassword \"secret\"" << endl
  671. << "to watch the presence of bob and charlie add" << endl
  672. << "\t-add \"sip:bob@bilboxi.com\" -add \"charlie@example.com\" " << endl
  673. << "If Alice was behind a NAT that had a public address of 1.2.3.4 and had forwarded" << endl
  674. << "port 5070 on this NAT to the machine Alice was using, then the following " << endl
  675. << "options would be added" << endl
  676. << "\t-contact \"sip:alice@1.2.3.4:5070\" -port 5070" << endl
  677. << "" << endl
  678. << endl;
  679. exit(1);
  680. }
  681. }
  682. //InfoLog( << "Using port " << port );
  683. #ifdef USE_SSL
  684. InfoLog( << "Setting up Security" );
  685. Security* security=NULL;
  686. try
  687. {
  688. char cert_dir[ 1024 ] ;
  689. char *home_dir = getenv( "HOME" ) ;
  690. cert_dir[ 0 ] = '\0' ;
  691. ::strcat( cert_dir, home_dir ) ;
  692. ::strcat( cert_dir, "/.sipCerts/" ) ;
  693. security = new Security( cert_dir ) ;
  694. // ::free( home_dir ) ; // CJ TODO mem leak
  695. }
  696. catch( ... )
  697. {
  698. security = NULL;
  699. ErrLog( << "Got a exception setting up Security" );
  700. }
  701. SipStack sipStack( security );
  702. #else
  703. SipStack sipStack( false /*multihtread*/ );
  704. #endif
  705. if ( key == Data("-") )
  706. {
  707. clog << "Please enter password to use to load your private key: ";
  708. char buf[1024];
  709. cin.get(buf,1024);
  710. key = Data(buf);
  711. InfoLog( << "Certificate key set to <" << key << ">" );
  712. }
  713. #ifdef USE_SSL
  714. try
  715. {
  716. Security* security = sipStack.getSecurity();
  717. assert(security != 0);
  718. }
  719. catch( ... )
  720. {
  721. ErrLog( << "Got an exception creating security object " );
  722. }
  723. try
  724. {
  725. assert(security != 0);
  726. security->preload();
  727. }
  728. catch( ... )
  729. {
  730. ErrLog( << "Got a exception pre loading certificates" );
  731. }
  732. if (genUserCert)
  733. {
  734. assert( security );
  735. security->generateUserCert(aor.getAor());
  736. }
  737. #endif
  738. DebugLog( << "About to add the transports " );
  739. if (port!=0)
  740. {
  741. if ( noUdp != true )
  742. {
  743. if (!noV4) sipStack.addTransport(UDP, port, V4);
  744. #ifdef USE_IPV6
  745. if (!noV6) sipStack.addTransport(UDP, port, V6);
  746. #endif
  747. }
  748. if ( noTcp != true )
  749. {
  750. if (!noV4) sipStack.addTransport(TCP, port, V4);
  751. #ifdef USE_IPV6
  752. if (!noV6) sipStack.addTransport(TCP, port, V6);
  753. #endif
  754. }
  755. }
  756. #if USE_SSL
  757. if ( tlsPort != 0 )
  758. {
  759. if ( noTls != true )
  760. {
  761. if (!noV4)
  762. {
  763. sipStack.addTransport(TLS, tlsPort, V4, StunDisabled, Data::Empty, tlsDomain );
  764. }
  765. //if (!noV6) sipStack.addTlsTransport(tlsPort,Data::Empty,Data::Empty,Data::Empty,V6);
  766. }
  767. }
  768. #if USE_DTLS
  769. if ( dtlsPort != 0 )
  770. {
  771. if ( noTls != true )
  772. {
  773. if (!noV4)
  774. {
  775. sipStack.addTransport(DTLS, dtlsPort, V4, StunDisabled, Data::Empty, tlsDomain );
  776. }
  777. }
  778. }
  779. #endif
  780. #endif
  781. DebugLog( << "Done adding the transports " );
  782. if (!haveContact)
  783. {
  784. // contact.port() = port;
  785. // contact.host() = sipStack.getHostname();
  786. }
  787. if ( haveAor )
  788. {
  789. if (!haveContact)
  790. {
  791. contact.user() = aor.user();
  792. #if USE_SSL
  793. if ( aor.scheme() == "sips" )
  794. {
  795. contact.scheme() = aor.scheme();
  796. //contact.port() = tlsPort;
  797. }
  798. #endif
  799. }
  800. }
  801. else
  802. {
  803. aor.port() = port;
  804. aor.host() = sipStack.getHostname();
  805. aor.user() = Data("user");
  806. }
  807. InfoLog( << "aor is " << aor );
  808. InfoLog( << "contact is " << contact );
  809. TestCallback callback;
  810. tuIM = new TuIM(&sipStack,aor,contact,&callback);
  811. Data name("SIPimp.org/0.2.5 (curses)");
  812. tuIM->setUAName( name );
  813. if ( !outbound.host().empty() )
  814. {
  815. tuIM->setOutboundProxy( outbound );
  816. }
  817. // setup prefered outbound transport
  818. if ( prefUdp )
  819. {
  820. tuIM->setDefaultProtocol( UDP );
  821. }
  822. if ( prefTcp )
  823. {
  824. tuIM->setDefaultProtocol( TCP );
  825. }
  826. if ( prefTls )
  827. {
  828. tuIM->setDefaultProtocol( TLS );
  829. }
  830. if ( prefDtls )
  831. {
  832. tuIM->setDefaultProtocol( DTLS );
  833. }
  834. if ( haveAor )
  835. {
  836. if ( !noRegister )
  837. {
  838. tuIM->registerAor( aor, aorPassword );
  839. }
  840. }
  841. initscr();
  842. cbreak();
  843. noecho();
  844. nonl();
  845. intrflush(stdscr, FALSE);
  846. keypad(stdscr, TRUE);
  847. int rows=0;
  848. int cols=0;
  849. getmaxyx(stdscr,rows,cols); /* get the number of rows and columns */
  850. commandWin = newwin(2,cols,rows-2,0);
  851. scrollok(commandWin, TRUE);
  852. wmove(commandWin,0,0);
  853. textWin = newwin(rows-3,cols*3/4,0,0);
  854. scrollok(textWin, TRUE);
  855. wmove(textWin,0,0);
  856. statusWin = newwin(rows-3,cols-(cols*3/4)-1,0,1+cols*3/4);
  857. scrollok(statusWin, FALSE);
  858. wmove(statusWin,0,0);
  859. mvhline(rows-3,0,ACS_HLINE,cols);
  860. mvvline(0,(cols*3/4),ACS_VLINE,rows-3);
  861. refresh();
  862. for ( int i=0; i<numAdd; i++ )
  863. {
  864. Uri uri(addList[i]);
  865. tuIM->addBuddy( uri, Data::Empty );
  866. }
  867. for ( int i=0; i<numPub; i++ )
  868. {
  869. Uri uri(pubList[i]);
  870. tuIM->addStateAgent( uri );
  871. }
  872. displayPres();
  873. waddstr(textWin,"Use -help on the command line to view options\n");
  874. waddstr(textWin,"To set where your messages will get sent type\n");
  875. waddstr(textWin," to: sip:alice@example.com \n");
  876. waddstr(textWin,"To monitores someeone presence type\n");
  877. waddstr(textWin," add: sip:buddy@example.com \n");
  878. waddstr(textWin,"To change you online status type\n");
  879. waddstr(textWin," status: in meeting\n");
  880. waddstr(textWin,"To set yourself to offline type\n");
  881. waddstr(textWin," status:\n");
  882. waddstr(textWin,"To exit type a single period\n");
  883. waddstr(textWin,"\n");
  884. wrefresh(textWin);
  885. if ( !sendMsg.empty() )
  886. {
  887. tuIM->sendPage( sendMsg , dest, sign , (encryp) ?
  888. (dest.getAorNoPort()) : (Data::Empty) );
  889. }
  890. while (1)
  891. {
  892. FdSet fdset;
  893. sipStack.buildFdSet(fdset);
  894. int time = sipStack.getTimeTillNextProcessMS();
  895. fdset.setRead( fileno(stdin) );
  896. //cerr << time << endl;
  897. int err = fdset.selectMilliSeconds( time );
  898. if ( err == -1 )
  899. {
  900. int e = errno;
  901. switch (e)
  902. {
  903. case 0:
  904. break;
  905. default:
  906. //InfoLog(<< "Error " << e << " " << strerror(e) << " in select");
  907. break;
  908. }
  909. }
  910. if ( err == 0 )
  911. {
  912. //cerr << "select timed out" << endl;
  913. }
  914. if ( err > 0 )
  915. {
  916. //cerr << "select has " << err << " fd ready" << endl;
  917. }
  918. ////InfoLog(<< "Select returned");
  919. if ( fdset.readyToRead( fileno(stdin) ) )
  920. {
  921. bool keepGoing = processStdin(&dest,sign,encryp);
  922. if (!keepGoing)
  923. {
  924. break;
  925. }
  926. }
  927. // //DebugLog ( << "Try TO PROCESS " );
  928. try
  929. {
  930. sipStack.process(fdset);
  931. }
  932. catch (...)
  933. {
  934. ErrLog( << "Got a exception from sipStack::process" );
  935. }
  936. try
  937. {
  938. tuIM->process();
  939. }
  940. catch (...)
  941. {
  942. ErrLog( << "Got a exception passed from TuIM::process" );
  943. }
  944. }
  945. return 0;
  946. }
  947. /* ====================================================================
  948. * The Vovida Software License, Version 1.0
  949. *
  950. * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
  951. *
  952. * Redistribution and use in source and binary forms, with or without
  953. * modification, are permitted provided that the following conditions
  954. * are met:
  955. *
  956. * 1. Redistributions of source code must retain the above copyright
  957. * notice, this list of conditions and the following disclaimer.
  958. *
  959. * 2. Redistributions in binary form must reproduce the above copyright
  960. * notice, this list of conditions and the following disclaimer in
  961. * the documentation and/or other materials provided with the
  962. * distribution.
  963. *
  964. * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  965. * and "Vovida Open Communication Application Library (VOCAL)" must
  966. * not be used to endorse or promote products derived from this
  967. * software without prior written permission. For written
  968. * permission, please contact vocal@vovida.org.
  969. *
  970. * 4. Products derived from this software may not be called "VOCAL", nor
  971. * may "VOCAL" appear in their name, without prior written
  972. * permission of Vovida Networks, Inc.
  973. *
  974. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  975. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  976. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  977. * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
  978. * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  979. * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  980. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  981. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  982. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  983. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  984. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  985. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  986. * DAMAGE.
  987. *
  988. * ====================================================================
  989. *
  990. * This software consists of voluntary contributions made by Vovida
  991. * Networks, Inc. and many individuals on behalf of Vovida Networks,
  992. * Inc. For more information on Vovida Networks, Inc., please see
  993. * <http://www.vovida.org/>.
  994. *
  995. */