You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

715 lines
21 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #define _CRT_SECURE_NO_DEPRECATE 1
  2. #include <time.h>
  3. #if defined(POSIX)
  4. #include <unistd.h>
  5. #endif
  6. #include <iomanip>
  7. #include <iostream>
  8. #include <string>
  9. #if HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif // HAVE_CONFIG_H
  12. #include "talk/base/sslconfig.h" // For SSL_USE_*
  13. #if SSL_USE_OPENSSL
  14. #define USE_SSL_TUNNEL
  15. #endif
  16. #include "talk/base/basicdefs.h"
  17. #include "talk/base/common.h"
  18. #include "talk/base/helpers.h"
  19. #include "talk/base/logging.h"
  20. #include "talk/base/ssladapter.h"
  21. #include "talk/base/stringutils.h"
  22. #include "talk/base/thread.h"
  23. #include "talk/p2p/base/sessionmanager.h"
  24. #include "talk/p2p/client/autoportallocator.h"
  25. #include "talk/p2p/client/sessionmanagertask.h"
  26. #include "talk/xmpp/xmppengine.h"
  27. #ifdef USE_SSL_TUNNEL
  28. #include "talk/session/tunnel/securetunnelsessionclient.h"
  29. #endif
  30. #include "talk/session/tunnel/tunnelsessionclient.h"
  31. #include "talk/xmpp/xmppclient.h"
  32. #include "talk/xmpp/xmppclientsettings.h"
  33. #include "talk/xmpp/xmpppump.h"
  34. #include "talk/xmpp/xmppsocket.h"
  35. #ifndef MAX_PATH
  36. #define MAX_PATH 256
  37. #endif
  38. #if defined(_MSC_VER) && (_MSC_VER < 1400)
  39. // The following are necessary to properly link when compiling STL without
  40. // /EHsc, otherwise known as C++ exceptions.
  41. void __cdecl std::_Throw(const std::exception &) {}
  42. std::_Prhand std::_Raise_handler = 0;
  43. #endif
  44. enum {
  45. MSG_LOGIN_COMPLETE = 1,
  46. MSG_LOGIN_FAILED,
  47. MSG_DONE,
  48. };
  49. buzz::Jid gUserJid;
  50. talk_base::InsecureCryptStringImpl gUserPass;
  51. std::string gXmppHost = "talk.google.com";
  52. int gXmppPort = 5222;
  53. buzz::TlsOptions gXmppUseTls = buzz::TLS_REQUIRED;
  54. class DebugLog : public sigslot::has_slots<> {
  55. public:
  56. DebugLog() :
  57. debug_input_buf_(NULL), debug_input_len_(0), debug_input_alloc_(0),
  58. debug_output_buf_(NULL), debug_output_len_(0), debug_output_alloc_(0),
  59. censor_password_(false)
  60. {}
  61. char * debug_input_buf_;
  62. int debug_input_len_;
  63. int debug_input_alloc_;
  64. char * debug_output_buf_;
  65. int debug_output_len_;
  66. int debug_output_alloc_;
  67. bool censor_password_;
  68. void Input(const char * data, int len) {
  69. if (debug_input_len_ + len > debug_input_alloc_) {
  70. char * old_buf = debug_input_buf_;
  71. debug_input_alloc_ = 4096;
  72. while (debug_input_alloc_ < debug_input_len_ + len) {
  73. debug_input_alloc_ *= 2;
  74. }
  75. debug_input_buf_ = new char[debug_input_alloc_];
  76. memcpy(debug_input_buf_, old_buf, debug_input_len_);
  77. delete[] old_buf;
  78. }
  79. memcpy(debug_input_buf_ + debug_input_len_, data, len);
  80. debug_input_len_ += len;
  81. DebugPrint(debug_input_buf_, &debug_input_len_, false);
  82. }
  83. void Output(const char * data, int len) {
  84. if (debug_output_len_ + len > debug_output_alloc_) {
  85. char * old_buf = debug_output_buf_;
  86. debug_output_alloc_ = 4096;
  87. while (debug_output_alloc_ < debug_output_len_ + len) {
  88. debug_output_alloc_ *= 2;
  89. }
  90. debug_output_buf_ = new char[debug_output_alloc_];
  91. memcpy(debug_output_buf_, old_buf, debug_output_len_);
  92. delete[] old_buf;
  93. }
  94. memcpy(debug_output_buf_ + debug_output_len_, data, len);
  95. debug_output_len_ += len;
  96. DebugPrint(debug_output_buf_, &debug_output_len_, true);
  97. }
  98. static bool
  99. IsAuthTag(const char * str, size_t len) {
  100. if (str[0] == '<' && str[1] == 'a' &&
  101. str[2] == 'u' &&
  102. str[3] == 't' &&
  103. str[4] == 'h' &&
  104. str[5] <= ' ') {
  105. std::string tag(str, len);
  106. if (tag.find("mechanism") != std::string::npos)
  107. return true;
  108. }
  109. return false;
  110. }
  111. void
  112. DebugPrint(char * buf, int * plen, bool output) {
  113. int len = *plen;
  114. if (len > 0) {
  115. time_t tim = time(NULL);
  116. struct tm * now = localtime(&tim);
  117. char *time_string = asctime(now);
  118. if (time_string) {
  119. size_t time_len = strlen(time_string);
  120. if (time_len > 0) {
  121. time_string[time_len-1] = 0; // trim off terminating \n
  122. }
  123. }
  124. LOG(INFO) << (output ? "SEND >>>>>>>>>>>>>>>>>>>>>>>>>" : "RECV <<<<<<<<<<<<<<<<<<<<<<<<<")
  125. << " : " << time_string;
  126. bool indent;
  127. int start = 0, nest = 3;
  128. for (int i = 0; i < len; i += 1) {
  129. if (buf[i] == '>') {
  130. if ((i > 0) && (buf[i-1] == '/')) {
  131. indent = false;
  132. } else if ((start + 1 < len) && (buf[start + 1] == '/')) {
  133. indent = false;
  134. nest -= 2;
  135. } else {
  136. indent = true;
  137. }
  138. // Output a tag
  139. LOG(INFO) << std::setw(nest) << " " << std::string(buf + start, i + 1 - start);
  140. if (indent)
  141. nest += 2;
  142. // Note if it's a PLAIN auth tag
  143. if (IsAuthTag(buf + start, i + 1 - start)) {
  144. censor_password_ = true;
  145. }
  146. // incr
  147. start = i + 1;
  148. }
  149. if (buf[i] == '<' && start < i) {
  150. if (censor_password_) {
  151. LOG(INFO) << std::setw(nest) << " " << "## TEXT REMOVED ##";
  152. censor_password_ = false;
  153. }
  154. else {
  155. LOG(INFO) << std::setw(nest) << " " << std::string(buf + start, i - start);
  156. }
  157. start = i;
  158. }
  159. }
  160. len = len - start;
  161. memcpy(buf, buf + start, len);
  162. *plen = len;
  163. }
  164. }
  165. };
  166. static DebugLog debug_log_;
  167. // Prints out a usage message then exits.
  168. void Usage() {
  169. std::cerr << "Usage:" << std::endl;
  170. std::cerr << " pcp [options] <my_jid> (server mode)" << std::endl;
  171. std::cerr << " pcp [options] <my_jid> <src_file> <dst_full_jid>:<dst_file> (client sending)" << std::endl;
  172. std::cerr << " pcp [options] <my_jid> <src_full_jid>:<src_file> <dst_file> (client rcv'ing)" << std::endl;
  173. std::cerr << " --verbose" << std::endl;
  174. std::cerr << " --xmpp-host=<host>" << std::endl;
  175. std::cerr << " --xmpp-port=<port>" << std::endl;
  176. std::cerr << " --xmpp-use-tls=(true|false)" << std::endl;
  177. exit(1);
  178. }
  179. // Prints out an error message, a usage message, then exits.
  180. void Error(const std::string& msg) {
  181. std::cerr << "error: " << msg << std::endl;
  182. std::cerr << std::endl;
  183. Usage();
  184. }
  185. void FatalError(const std::string& msg) {
  186. std::cerr << "error: " << msg << std::endl;
  187. std::cerr << std::endl;
  188. exit(1);
  189. }
  190. // Determines whether the given string is an option. If so, the name and
  191. // value are appended to the given strings.
  192. bool ParseArg(const char* arg, std::string* name, std::string* value) {
  193. if (strncmp(arg, "--", 2) != 0)
  194. return false;
  195. const char* eq = strchr(arg + 2, '=');
  196. if (eq) {
  197. if (name)
  198. name->append(arg + 2, eq);
  199. if (value)
  200. value->append(eq + 1, arg + strlen(arg));
  201. } else {
  202. if (name)
  203. name->append(arg + 2, arg + strlen(arg));
  204. if (value)
  205. value->clear();
  206. }
  207. return true;
  208. }
  209. int ParseIntArg(const std::string& name, const std::string& value) {
  210. char* end;
  211. long val = strtol(value.c_str(), &end, 10);
  212. if (*end != '\0')
  213. Error(std::string("value of option ") + name + " must be an integer");
  214. return static_cast<int>(val);
  215. }
  216. #ifdef WIN32
  217. #pragma warning(push)
  218. // disable "unreachable code" warning b/c it varies between dbg and opt
  219. #pragma warning(disable: 4702)
  220. #endif
  221. bool ParseBoolArg(const std::string& name, const std::string& value) {
  222. if (value == "true")
  223. return true;
  224. else if (value == "false")
  225. return false;
  226. else {
  227. Error(std::string("value of option ") + name + " must be true or false");
  228. return false;
  229. }
  230. }
  231. #ifdef WIN32
  232. #pragma warning(pop)
  233. #endif
  234. void ParseFileArg(const char* arg, buzz::Jid* jid, std::string* file) {
  235. const char* sep = strchr(arg, ':');
  236. if (!sep) {
  237. *file = arg;
  238. } else {
  239. buzz::Jid jid_arg(std::string(arg, sep-arg));
  240. if (jid_arg.IsBare())
  241. Error("A full JID is required for the source or destination arguments.");
  242. *jid = jid_arg;
  243. *file = std::string(sep+1);
  244. }
  245. }
  246. void SetConsoleEcho(bool on) {
  247. #ifdef WIN32
  248. HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
  249. if ((hIn == INVALID_HANDLE_VALUE) || (hIn == NULL))
  250. return;
  251. DWORD mode;
  252. if (!GetConsoleMode(hIn, &mode))
  253. return;
  254. if (on) {
  255. mode = mode | ENABLE_ECHO_INPUT;
  256. } else {
  257. mode = mode & ~ENABLE_ECHO_INPUT;
  258. }
  259. SetConsoleMode(hIn, mode);
  260. #else
  261. int re;
  262. if (on)
  263. re = system("stty echo");
  264. else
  265. re = system("stty -echo");
  266. if (-1 == re)
  267. return;
  268. #endif
  269. }
  270. // Fills in a settings object with the values from the arguments.
  271. buzz::XmppClientSettings LoginSettings() {
  272. buzz::XmppClientSettings xcs;
  273. xcs.set_user(gUserJid.node());
  274. xcs.set_host(gUserJid.domain());
  275. xcs.set_resource("pcp");
  276. xcs.set_pass(talk_base::CryptString(gUserPass));
  277. talk_base::SocketAddress server(gXmppHost, gXmppPort);
  278. xcs.set_server(server);
  279. xcs.set_use_tls(gXmppUseTls);
  280. return xcs;
  281. }
  282. // Runs the current thread until a message with the given ID is seen.
  283. uint32 Loop(const std::vector<uint32>& ids) {
  284. talk_base::Message msg;
  285. while (talk_base::Thread::Current()->Get(&msg)) {
  286. if (msg.phandler == NULL) {
  287. if (std::find(ids.begin(), ids.end(), msg.message_id) != ids.end())
  288. return msg.message_id;
  289. std::cout << "orphaned message: " << msg.message_id;
  290. continue;
  291. }
  292. talk_base::Thread::Current()->Dispatch(&msg);
  293. }
  294. return 0;
  295. }
  296. #ifdef WIN32
  297. #pragma warning(disable:4355)
  298. #endif
  299. class CustomXmppPump : public buzz::XmppPumpNotify, public buzz::XmppPump {
  300. public:
  301. CustomXmppPump() : XmppPump(this), server_(false) { }
  302. void Serve(cricket::TunnelSessionClient* client) {
  303. client->SignalIncomingTunnel.connect(this,
  304. &CustomXmppPump::OnIncomingTunnel);
  305. server_ = true;
  306. }
  307. void OnStateChange(buzz::XmppEngine::State state) {
  308. switch (state) {
  309. case buzz::XmppEngine::STATE_START:
  310. std::cout << "connecting..." << std::endl;
  311. break;
  312. case buzz::XmppEngine::STATE_OPENING:
  313. std::cout << "logging in..." << std::endl;
  314. break;
  315. case buzz::XmppEngine::STATE_OPEN:
  316. std::cout << "logged in..." << std::endl;
  317. talk_base::Thread::Current()->Post(NULL, MSG_LOGIN_COMPLETE);
  318. break;
  319. case buzz::XmppEngine::STATE_CLOSED:
  320. std::cout << "logged out..." << std::endl;
  321. talk_base::Thread::Current()->Post(NULL, MSG_LOGIN_FAILED);
  322. break;
  323. }
  324. }
  325. void OnIncomingTunnel(cricket::TunnelSessionClient* client, buzz::Jid jid,
  326. std::string description, cricket::Session* session) {
  327. std::cout << "IncomingTunnel from " << jid.Str()
  328. << ": " << description << std::endl;
  329. if (!server_ || file_) {
  330. client->DeclineTunnel(session);
  331. return;
  332. }
  333. std::string filename;
  334. bool send;
  335. if (strncmp(description.c_str(), "send:", 5) == 0) {
  336. send = true;
  337. } else if (strncmp(description.c_str(), "recv:", 5) == 0) {
  338. send = false;
  339. } else {
  340. client->DeclineTunnel(session);
  341. return;
  342. }
  343. filename = description.substr(5);
  344. talk_base::StreamInterface* stream = client->AcceptTunnel(session);
  345. if (!ProcessStream(stream, filename, send))
  346. talk_base::Thread::Current()->Post(NULL, MSG_DONE);
  347. // TODO: There is a potential memory leak, however, since the PCP
  348. // app doesn't work right now, I can't verify the fix actually works, so
  349. // comment out the following line until we fix the PCP app.
  350. // delete stream;
  351. }
  352. bool ProcessStream(talk_base::StreamInterface* stream,
  353. const std::string& filename, bool send) {
  354. ASSERT(file_);
  355. sending_ = send;
  356. file_.reset(new talk_base::FileStream);
  357. buffer_len_ = 0;
  358. int err;
  359. if (!file_->Open(filename.c_str(), sending_ ? "rb" : "wb", &err)) {
  360. std::cerr << "Error opening <" << filename << ">: "
  361. << std::strerror(err) << std::endl;
  362. return false;
  363. }
  364. stream->SignalEvent.connect(this, &CustomXmppPump::OnStreamEvent);
  365. if (stream->GetState() == talk_base::SS_CLOSED) {
  366. std::cerr << "Failed to establish P2P tunnel" << std::endl;
  367. return false;
  368. }
  369. if (stream->GetState() == talk_base::SS_OPEN) {
  370. OnStreamEvent(stream,
  371. talk_base::SE_OPEN | talk_base::SE_READ | talk_base::SE_WRITE, 0);
  372. }
  373. return true;
  374. }
  375. void OnStreamEvent(talk_base::StreamInterface* stream, int events,
  376. int error) {
  377. if (events & talk_base::SE_CLOSE) {
  378. if (error == 0) {
  379. std::cout << "Tunnel closed normally" << std::endl;
  380. } else {
  381. std::cout << "Tunnel closed with error: " << error << std::endl;
  382. }
  383. Cleanup(stream);
  384. return;
  385. }
  386. if (events & talk_base::SE_OPEN) {
  387. std::cout << "Tunnel connected" << std::endl;
  388. }
  389. talk_base::StreamResult result;
  390. size_t count;
  391. if (sending_ && (events & talk_base::SE_WRITE)) {
  392. LOG(LS_VERBOSE) << "Tunnel SE_WRITE";
  393. while (true) {
  394. size_t write_pos = 0;
  395. while (write_pos < buffer_len_) {
  396. result = stream->Write(buffer_ + write_pos, buffer_len_ - write_pos,
  397. &count, &error);
  398. if (result == talk_base::SR_SUCCESS) {
  399. write_pos += count;
  400. continue;
  401. }
  402. if (result == talk_base::SR_BLOCK) {
  403. buffer_len_ -= write_pos;
  404. memmove(buffer_, buffer_ + write_pos, buffer_len_);
  405. LOG(LS_VERBOSE) << "Tunnel write block";
  406. return;
  407. }
  408. if (result == talk_base::SR_EOS) {
  409. std::cout << "Tunnel closed unexpectedly on write" << std::endl;
  410. } else {
  411. std::cout << "Tunnel write error: " << error << std::endl;
  412. }
  413. Cleanup(stream);
  414. return;
  415. }
  416. buffer_len_ = 0;
  417. while (buffer_len_ < sizeof(buffer_)) {
  418. result = file_->Read(buffer_ + buffer_len_,
  419. sizeof(buffer_) - buffer_len_,
  420. &count, &error);
  421. if (result == talk_base::SR_SUCCESS) {
  422. buffer_len_ += count;
  423. continue;
  424. }
  425. if (result == talk_base::SR_EOS) {
  426. if (buffer_len_ > 0)
  427. break;
  428. std::cout << "End of file" << std::endl;
  429. // A hack until we have friendly shutdown
  430. Cleanup(stream, true);
  431. return;
  432. } else if (result == talk_base::SR_BLOCK) {
  433. std::cout << "File blocked unexpectedly on read" << std::endl;
  434. } else {
  435. std::cout << "File read error: " << error << std::endl;
  436. }
  437. Cleanup(stream);
  438. return;
  439. }
  440. }
  441. }
  442. if (!sending_ && (events & talk_base::SE_READ)) {
  443. LOG(LS_VERBOSE) << "Tunnel SE_READ";
  444. while (true) {
  445. buffer_len_ = 0;
  446. while (buffer_len_ < sizeof(buffer_)) {
  447. result = stream->Read(buffer_ + buffer_len_,
  448. sizeof(buffer_) - buffer_len_,
  449. &count, &error);
  450. if (result == talk_base::SR_SUCCESS) {
  451. buffer_len_ += count;
  452. continue;
  453. }
  454. if (result == talk_base::SR_BLOCK) {
  455. if (buffer_len_ > 0)
  456. break;
  457. LOG(LS_VERBOSE) << "Tunnel read block";
  458. return;
  459. }
  460. if (result == talk_base::SR_EOS) {
  461. std::cout << "Tunnel closed unexpectedly on read" << std::endl;
  462. } else {
  463. std::cout << "Tunnel read error: " << error << std::endl;
  464. }
  465. Cleanup(stream);
  466. return;
  467. }
  468. size_t write_pos = 0;
  469. while (write_pos < buffer_len_) {
  470. result = file_->Write(buffer_ + write_pos, buffer_len_ - write_pos,
  471. &count, &error);
  472. if (result == talk_base::SR_SUCCESS) {
  473. write_pos += count;
  474. continue;
  475. }
  476. if (result == talk_base::SR_EOS) {
  477. std::cout << "File closed unexpectedly on write" << std::endl;
  478. } else if (result == talk_base::SR_BLOCK) {
  479. std::cout << "File blocked unexpectedly on write" << std::endl;
  480. } else {
  481. std::cout << "File write error: " << error << std::endl;
  482. }
  483. Cleanup(stream);
  484. return;
  485. }
  486. }
  487. }
  488. }
  489. void Cleanup(talk_base::StreamInterface* stream, bool delay = false) {
  490. LOG(LS_VERBOSE) << "Closing";
  491. stream->Close();
  492. file_.reset();
  493. if (!server_) {
  494. if (delay)
  495. talk_base::Thread::Current()->PostDelayed(2000, NULL, MSG_DONE);
  496. else
  497. talk_base::Thread::Current()->Post(NULL, MSG_DONE);
  498. }
  499. }
  500. private:
  501. bool server_, sending_;
  502. talk_base::scoped_ptr<talk_base::FileStream> file_;
  503. char buffer_[1024 * 64];
  504. size_t buffer_len_;
  505. };
  506. int main(int argc, char **argv) {
  507. talk_base::LogMessage::LogThreads();
  508. talk_base::LogMessage::LogTimestamps();
  509. // TODO: Default the username to the current users's name.
  510. // Parse the arguments.
  511. int index = 1;
  512. while (index < argc) {
  513. std::string name, value;
  514. if (!ParseArg(argv[index], &name, &value))
  515. break;
  516. if (name == "help") {
  517. Usage();
  518. } else if (name == "verbose") {
  519. talk_base::LogMessage::LogToDebug(talk_base::LS_VERBOSE);
  520. } else if (name == "xmpp-host") {
  521. gXmppHost = value;
  522. } else if (name == "xmpp-port") {
  523. gXmppPort = ParseIntArg(name, value);
  524. } else if (name == "xmpp-use-tls") {
  525. gXmppUseTls = ParseBoolArg(name, value)?
  526. buzz::TLS_REQUIRED : buzz::TLS_DISABLED;
  527. } else {
  528. Error(std::string("unknown option: ") + name);
  529. }
  530. index += 1;
  531. }
  532. if (index >= argc)
  533. Error("bad arguments");
  534. gUserJid = buzz::Jid(argv[index++]);
  535. if (!gUserJid.IsValid())
  536. Error("bad arguments");
  537. char path[MAX_PATH];
  538. #if WIN32
  539. GetCurrentDirectoryA(MAX_PATH, path);
  540. #else
  541. if (NULL == getcwd(path, MAX_PATH))
  542. Error("Unable to get current path");
  543. #endif
  544. std::cout << "Directory: " << std::string(path) << std::endl;
  545. buzz::Jid gSrcJid;
  546. buzz::Jid gDstJid;
  547. std::string gSrcFile;
  548. std::string gDstFile;
  549. bool as_server = true;
  550. if (index + 2 == argc) {
  551. ParseFileArg(argv[index], &gSrcJid, &gSrcFile);
  552. ParseFileArg(argv[index+1], &gDstJid, &gDstFile);
  553. if(gSrcJid.Str().empty() == gDstJid.Str().empty())
  554. Error("Exactly one of source JID or destination JID must be empty.");
  555. as_server = false;
  556. } else if (index != argc) {
  557. Error("bad arguments");
  558. }
  559. std::cout << "Password: ";
  560. SetConsoleEcho(false);
  561. std::cin >> gUserPass.password();
  562. SetConsoleEcho(true);
  563. std::cout << std::endl;
  564. talk_base::InitializeSSL();
  565. // Log in.
  566. CustomXmppPump pump;
  567. pump.client()->SignalLogInput.connect(&debug_log_, &DebugLog::Input);
  568. pump.client()->SignalLogOutput.connect(&debug_log_, &DebugLog::Output);
  569. pump.DoLogin(LoginSettings(), new buzz::XmppSocket(gXmppUseTls), 0);
  570. //new XmppAuth());
  571. // Wait until login succeeds.
  572. std::vector<uint32> ids;
  573. ids.push_back(MSG_LOGIN_COMPLETE);
  574. ids.push_back(MSG_LOGIN_FAILED);
  575. if (MSG_LOGIN_FAILED == Loop(ids))
  576. FatalError("Failed to connect");
  577. {
  578. talk_base::scoped_ptr<buzz::XmlElement> presence(
  579. new buzz::XmlElement(buzz::QN_PRESENCE));
  580. presence->AddElement(new buzz::XmlElement(buzz::QN_PRIORITY));
  581. presence->AddText("-1", 1);
  582. pump.SendStanza(presence.get());
  583. }
  584. std::string user_jid_str = pump.client()->jid().Str();
  585. std::cout << "Logged in as " << user_jid_str << std::endl;
  586. // Prepare the random number generator.
  587. talk_base::InitRandom(user_jid_str.c_str(), user_jid_str.size());
  588. // Create the P2P session manager.
  589. talk_base::BasicNetworkManager network_manager;
  590. AutoPortAllocator allocator(&network_manager, "pcp_agent");
  591. allocator.SetXmppClient(pump.client());
  592. cricket::SessionManager session_manager(&allocator);
  593. #ifdef USE_SSL_TUNNEL
  594. cricket::SecureTunnelSessionClient session_client(pump.client()->jid(),
  595. &session_manager);
  596. if (!session_client.GenerateIdentity())
  597. FatalError("Failed to generate SSL identity");
  598. #else // !USE_SSL_TUNNEL
  599. cricket::TunnelSessionClient session_client(pump.client()->jid(),
  600. &session_manager);
  601. #endif // USE_SSL_TUNNEL
  602. cricket::SessionManagerTask *receiver =
  603. new cricket::SessionManagerTask(pump.client(), &session_manager);
  604. receiver->EnableOutgoingMessages();
  605. receiver->Start();
  606. bool success = true;
  607. // Establish the appropriate connection.
  608. if (as_server) {
  609. pump.Serve(&session_client);
  610. } else {
  611. talk_base::StreamInterface* stream = NULL;
  612. std::string filename;
  613. bool sending;
  614. if (gSrcJid.Str().empty()) {
  615. std::string message("recv:");
  616. message.append(gDstFile);
  617. stream = session_client.CreateTunnel(gDstJid, message);
  618. filename = gSrcFile;
  619. sending = true;
  620. } else {
  621. std::string message("send:");
  622. message.append(gSrcFile);
  623. stream = session_client.CreateTunnel(gSrcJid, message);
  624. filename = gDstFile;
  625. sending = false;
  626. }
  627. success = pump.ProcessStream(stream, filename, sending);
  628. }
  629. if (success) {
  630. // Wait until the copy is done.
  631. ids.clear();
  632. ids.push_back(MSG_DONE);
  633. ids.push_back(MSG_LOGIN_FAILED);
  634. Loop(ids);
  635. }
  636. // Log out.
  637. pump.DoDisconnect();
  638. return 0;
  639. }