15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2012, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <iostream>  // NOLINT
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/asyncudpsocket.h"
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/optionsfile.h"
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/thread.h"
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/stringencode.h"
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/p2p/base/basicpacketsocketfactory.h"
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/p2p/base/turnserver.h"
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgstatic const char kSoftware[] = "libjingle TurnServer";
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass TurnFileAuth : public cricket::TurnAuthInterface {
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  explicit TurnFileAuth(const std::string& path) : file_(path) {
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    file_.Load();
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetKey(const std::string& username, const std::string& realm,
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                      std::string* key) {
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    // File is stored as lines of <username>=<HA1>.
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    // Generate HA1 via "echo -n "<username>:<realm>:<password>" | md5sum"
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    std::string hex;
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    bool ret = file_.GetStringValue(username, &hex);
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    if (ret) {
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      char buf[32];
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      size_t len = talk_base::hex_decode(buf, sizeof(buf), hex);
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      *key = std::string(buf, len);
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    }
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return ret;
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private:
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::OptionsFile file_;
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgint main(int argc, char **argv) {
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (argc != 5) {
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    std::cerr << "usage: turnserver int-addr ext-ip realm auth-file"
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org              << std::endl;
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return 1;
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::SocketAddress int_addr;
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (!int_addr.FromString(argv[1])) {
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    std::cerr << "Unable to parse IP address: " << argv[1] << std::endl;
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return 1;
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::IPAddress ext_addr;
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (!IPFromString(argv[2], &ext_addr)) {
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    std::cerr << "Unable to parse IP address: " << argv[2] << std::endl;
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return 1;
785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::Thread* main = talk_base::Thread::Current();
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  talk_base::AsyncUDPSocket* int_socket =
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org      talk_base::AsyncUDPSocket::Create(main->socketserver(), int_addr);
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  if (!int_socket) {
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    std::cerr << "Failed to create a UDP socket bound at"
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org              << int_addr.ToString() << std::endl;
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org    return 1;
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  }
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  cricket::TurnServer server(main);
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  TurnFileAuth auth(argv[4]);
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  server.set_realm(argv[3]);
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  server.set_software(kSoftware);
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  server.set_auth_hook(&auth);
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  server.AddInternalSocket(int_socket, cricket::PROTO_UDP);
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  server.SetExternalSocketFactory(new talk_base::BasicPacketSocketFactory(),
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                  talk_base::SocketAddress(ext_addr, 0));
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  std::cout << "Listening internally at " << int_addr.ToString() << std::endl;
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  main->Run();
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  return 0;
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}
103