Note: We no longer publish the latest version of our code here. We primarily use a kumc-bmi github organization. The heron ETL repository, in particular, is not public. Peers in the informatics community should see MultiSiteDev for details on requesting access.

source: webrtc/talk/p2p/base/relayport.cc @ 0:4bda6873e34c

pub_scrub_3792 tip
Last change on this file since 0:4bda6873e34c was 0:4bda6873e34c, checked in by Michael Prittie <mprittie@…>, 6 years ago

Scrubbed password for publication.

File size: 27.7 KB
Line 
1/*
2 * libjingle
3 * Copyright 2004--2005, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/asyncpacketsocket.h"
29#include "talk/base/helpers.h"
30#include "talk/base/logging.h"
31#include "talk/p2p/base/relayport.h"
32
33namespace cricket {
34
35static const uint32 kMessageConnectTimeout = 1;
36static const int kKeepAliveDelay           = 10 * 60 * 1000;
37static const int kRetryTimeout             = 50 * 1000;  // ICE says 50 secs
38// How long to wait for a socket to connect to remote host in milliseconds
39// before trying another connection.
40static const int kSoftConnectTimeoutMs     = 3 * 1000;
41
42// Handles a connection to one address/port/protocol combination for a
43// particular RelayEntry.
44class RelayConnection : public sigslot::has_slots<> {
45 public:
46  RelayConnection(const ProtocolAddress* protocol_address,
47                  talk_base::AsyncPacketSocket* socket,
48                  talk_base::Thread* thread);
49  ~RelayConnection();
50  talk_base::AsyncPacketSocket* socket() const { return socket_; }
51
52  const ProtocolAddress* protocol_address() {
53    return protocol_address_;
54  }
55
56  talk_base::SocketAddress GetAddress() const {
57    return protocol_address_->address;
58  }
59
60  ProtocolType GetProtocol() const {
61    return protocol_address_->proto;
62  }
63
64  int SetSocketOption(talk_base::Socket::Option opt, int value);
65
66  // Validates a response to a STUN allocate request.
67  bool CheckResponse(StunMessage* msg);
68
69  // Sends data to the relay server.
70  int Send(const void* pv, size_t cb, talk_base::DiffServCodePoint dscp);
71
72  // Sends a STUN allocate request message to the relay server.
73  void SendAllocateRequest(RelayEntry* entry, int delay);
74
75  // Return the latest error generated by the socket.
76  int GetError() { return socket_->GetError(); }
77
78  // Called on behalf of a StunRequest to write data to the socket.  This is
79  // already STUN intended for the server, so no wrapping is necessary.
80  void OnSendPacket(const void* data, size_t size, StunRequest* req);
81
82 private:
83  talk_base::AsyncPacketSocket* socket_;
84  const ProtocolAddress* protocol_address_;
85  StunRequestManager *request_manager_;
86};
87
88// Manages a number of connections to the relayserver, one for each
89// available protocol. We aim to use each connection for only a
90// specific destination address so that we can avoid wrapping every
91// packet in a STUN send / data indication.
92class RelayEntry : public talk_base::MessageHandler,
93                   public sigslot::has_slots<> {
94 public:
95  RelayEntry(RelayPort* port, const talk_base::SocketAddress& ext_addr);
96  ~RelayEntry();
97
98  RelayPort* port() { return port_; }
99
100  const talk_base::SocketAddress& address() const { return ext_addr_; }
101  void set_address(const talk_base::SocketAddress& addr) { ext_addr_ = addr; }
102
103  bool connected() const { return connected_; }
104  bool locked() const { return locked_; }
105
106  // Returns the last error on the socket of this entry.
107  int GetError();
108
109  // Returns the most preferred connection of the given
110  // ones. Connections are rated based on protocol in the order of:
111  // UDP, TCP and SSLTCP, where UDP is the most preferred protocol
112  static RelayConnection* GetBestConnection(RelayConnection* conn1,
113                                            RelayConnection* conn2);
114
115  // Sends the STUN requests to the server to initiate this connection.
116  void Connect();
117
118  // Called when this entry becomes connected.  The address given is the one
119  // exposed to the outside world on the relay server.
120  void OnConnect(const talk_base::SocketAddress& mapped_addr,
121                 RelayConnection* socket);
122
123  // Sends a packet to the given destination address using the socket of this
124  // entry.  This will wrap the packet in STUN if necessary.
125  int SendTo(const void* data, size_t size,
126             const talk_base::SocketAddress& addr,
127             talk_base::DiffServCodePoint dscp);
128
129  // Schedules a keep-alive allocate request.
130  void ScheduleKeepAlive();
131
132  void SetServerIndex(size_t sindex) { server_index_ = sindex; }
133
134  // Sets this option on the socket of each connection.
135  int SetSocketOption(talk_base::Socket::Option opt, int value);
136
137  size_t ServerIndex() const { return server_index_; }
138
139  // Try a different server address
140  void HandleConnectFailure(talk_base::AsyncPacketSocket* socket);
141
142  // Implementation of the MessageHandler Interface.
143  virtual void OnMessage(talk_base::Message *pmsg);
144
145 private:
146  RelayPort* port_;
147  talk_base::SocketAddress ext_addr_;
148  size_t server_index_;
149  bool connected_;
150  bool locked_;
151  RelayConnection* current_connection_;
152
153  // Called when a TCP connection is established or fails
154  void OnSocketConnect(talk_base::AsyncPacketSocket* socket);
155  void OnSocketClose(talk_base::AsyncPacketSocket* socket, int error);
156
157  // Called when a packet is received on this socket.
158  void OnReadPacket(talk_base::AsyncPacketSocket* socket,
159                    const char* data, size_t size,
160                    const talk_base::SocketAddress& remote_addr);
161
162  // Called when the socket is currently able to send.
163  void OnReadyToSend(talk_base::AsyncPacketSocket* socket);
164
165  // Sends the given data on the socket to the server with no wrapping.  This
166  // returns the number of bytes written or -1 if an error occurred.
167  int SendPacket(const void* data, size_t size,
168                 talk_base::DiffServCodePoint dscp);
169};
170
171// Handles an allocate request for a particular RelayEntry.
172class AllocateRequest : public StunRequest {
173 public:
174  AllocateRequest(RelayEntry* entry, RelayConnection* connection);
175  virtual ~AllocateRequest() {}
176
177  virtual void Prepare(StunMessage* request);
178
179  virtual int GetNextDelay();
180
181  virtual void OnResponse(StunMessage* response);
182  virtual void OnErrorResponse(StunMessage* response);
183  virtual void OnTimeout();
184
185 private:
186  RelayEntry* entry_;
187  RelayConnection* connection_;
188  uint32 start_time_;
189};
190
191RelayPort::RelayPort(
192    talk_base::Thread* thread, talk_base::PacketSocketFactory* factory,
193    talk_base::Network* network, const talk_base::IPAddress& ip,
194    int min_port, int max_port, const std::string& username,
195    const std::string& password)
196    : Port(thread, RELAY_PORT_TYPE, factory, network, ip, min_port, max_port,
197           username, password),
198      ready_(false),
199      error_(0) {
200  entries_.push_back(
201      new RelayEntry(this, talk_base::SocketAddress()));
202  // TODO: set local preference value for TCP based candidates.
203}
204
205RelayPort::~RelayPort() {
206  for (size_t i = 0; i < entries_.size(); ++i)
207    delete entries_[i];
208  thread()->Clear(this);
209}
210
211void RelayPort::AddServerAddress(const ProtocolAddress& addr) {
212  // Since HTTP proxies usually only allow 443,
213  // let's up the priority on PROTO_SSLTCP
214  if (addr.proto == PROTO_SSLTCP &&
215      (proxy().type == talk_base::PROXY_HTTPS ||
216       proxy().type == talk_base::PROXY_UNKNOWN)) {
217    server_addr_.push_front(addr);
218  } else {
219    server_addr_.push_back(addr);
220  }
221}
222
223void RelayPort::AddExternalAddress(const ProtocolAddress& addr) {
224  std::string proto_name = ProtoToString(addr.proto);
225  for (std::vector<ProtocolAddress>::iterator it = external_addr_.begin();
226       it != external_addr_.end(); ++it) {
227    if ((it->address == addr.address) && (it->proto == addr.proto)) {
228      LOG(INFO) << "Redundant relay address: " << proto_name
229                << " @ " << addr.address.ToSensitiveString();
230      return;
231    }
232  }
233  external_addr_.push_back(addr);
234}
235
236void RelayPort::SetReady() {
237  if (!ready_) {
238    std::vector<ProtocolAddress>::iterator iter;
239    for (iter = external_addr_.begin();
240         iter != external_addr_.end(); ++iter) {
241      std::string proto_name = ProtoToString(iter->proto);
242      AddAddress(iter->address, iter->address, proto_name,
243                 RELAY_PORT_TYPE, ICE_TYPE_PREFERENCE_RELAY, false);
244    }
245    ready_ = true;
246    SignalPortComplete(this);
247  }
248}
249
250const ProtocolAddress * RelayPort::ServerAddress(size_t index) const {
251  if (index < server_addr_.size())
252    return &server_addr_[index];
253  return NULL;
254}
255
256bool RelayPort::HasMagicCookie(const char* data, size_t size) {
257  if (size < 24 + sizeof(TURN_MAGIC_COOKIE_VALUE)) {
258    return false;
259  } else {
260    return 0 == std::memcmp(data + 24, TURN_MAGIC_COOKIE_VALUE,
261                            sizeof(TURN_MAGIC_COOKIE_VALUE));
262  }
263}
264
265void RelayPort::PrepareAddress() {
266  // We initiate a connect on the first entry.  If this completes, it will fill
267  // in the server address as the address of this port.
268  ASSERT(entries_.size() == 1);
269  entries_[0]->Connect();
270  ready_ = false;
271}
272
273Connection* RelayPort::CreateConnection(const Candidate& address,
274                                        CandidateOrigin origin) {
275  // We only create conns to non-udp sockets if they are incoming on this port
276  if ((address.protocol() != UDP_PROTOCOL_NAME) &&
277      (origin != ORIGIN_THIS_PORT)) {
278    return 0;
279  }
280
281  // We don't support loopback on relays
282  if (address.type() == Type()) {
283    return 0;
284  }
285
286  if (!IsCompatibleAddress(address.address())) {
287    return 0;
288  }
289
290  size_t index = 0;
291  for (size_t i = 0; i < Candidates().size(); ++i) {
292    const Candidate& local = Candidates()[i];
293    if (local.protocol() == address.protocol()) {
294      index = i;
295      break;
296    }
297  }
298
299  Connection * conn = new ProxyConnection(this, index, address);
300  AddConnection(conn);
301  return conn;
302}
303
304int RelayPort::SendTo(const void* data, size_t size,
305                      const talk_base::SocketAddress& addr,
306                      talk_base::DiffServCodePoint dscp,
307                      bool payload) {
308  // Try to find an entry for this specific address.  Note that the first entry
309  // created was not given an address initially, so it can be set to the first
310  // address that comes along.
311  RelayEntry* entry = 0;
312
313  for (size_t i = 0; i < entries_.size(); ++i) {
314    if (entries_[i]->address().IsNil() && payload) {
315      entry = entries_[i];
316      entry->set_address(addr);
317      break;
318    } else if (entries_[i]->address() == addr) {
319      entry = entries_[i];
320      break;
321    }
322  }
323
324  // If we did not find one, then we make a new one.  This will not be useable
325  // until it becomes connected, however.
326  if (!entry && payload) {
327    entry = new RelayEntry(this, addr);
328    if (!entries_.empty()) {
329      entry->SetServerIndex(entries_[0]->ServerIndex());
330    }
331    entry->Connect();
332    entries_.push_back(entry);
333  }
334
335  // If the entry is connected, then we can send on it (though wrapping may
336  // still be necessary).  Otherwise, we can't yet use this connection, so we
337  // default to the first one.
338  if (!entry || !entry->connected()) {
339    ASSERT(!entries_.empty());
340    entry = entries_[0];
341    if (!entry->connected()) {
342      error_ = EWOULDBLOCK;
343      return SOCKET_ERROR;
344    }
345  }
346
347  // Send the actual contents to the server using the usual mechanism.
348  int sent = entry->SendTo(data, size, addr, dscp);
349  if (sent <= 0) {
350    ASSERT(sent < 0);
351    error_ = entry->GetError();
352    return SOCKET_ERROR;
353  }
354  // The caller of the function is expecting the number of user data bytes,
355  // rather than the size of the packet.
356  return static_cast<int>(size);
357}
358
359int RelayPort::SetOption(talk_base::Socket::Option opt, int value) {
360  int result = 0;
361  // DSCP option is not passed to the socket.
362  // TODO(mallinath) - After we have the support on socket,
363  // remove this specialization.
364  if (opt == talk_base::Socket::OPT_DSCP) {
365    SetDefaultDscpValue(static_cast<talk_base::DiffServCodePoint>(value));
366    return result;
367  }
368
369  for (size_t i = 0; i < entries_.size(); ++i) {
370    if (entries_[i]->SetSocketOption(opt, value) < 0) {
371      result = -1;
372      error_ = entries_[i]->GetError();
373    }
374  }
375  options_.push_back(OptionValue(opt, value));
376  return result;
377}
378
379int RelayPort::GetOption(talk_base::Socket::Option opt, int* value) {
380  std::vector<OptionValue>::iterator it;
381  for (it = options_.begin(); it < options_.end(); ++it) {
382    if (it->first == opt) {
383      *value = it->second;
384      return 0;
385    }
386  }
387  return SOCKET_ERROR;
388}
389
390int RelayPort::GetError() {
391  return error_;
392}
393
394void RelayPort::OnReadPacket(
395    const char* data, size_t size,
396    const talk_base::SocketAddress& remote_addr, ProtocolType proto) {
397  if (Connection* conn = GetConnection(remote_addr)) {
398    conn->OnReadPacket(data, size);
399  } else {
400    Port::OnReadPacket(data, size, remote_addr, proto);
401  }
402}
403
404RelayConnection::RelayConnection(const ProtocolAddress* protocol_address,
405                                 talk_base::AsyncPacketSocket* socket,
406                                 talk_base::Thread* thread)
407    : socket_(socket),
408      protocol_address_(protocol_address) {
409  request_manager_ = new StunRequestManager(thread);
410  request_manager_->SignalSendPacket.connect(this,
411                                             &RelayConnection::OnSendPacket);
412}
413
414RelayConnection::~RelayConnection() {
415  delete request_manager_;
416  delete socket_;
417}
418
419int RelayConnection::SetSocketOption(talk_base::Socket::Option opt,
420                                     int value) {
421  if (socket_) {
422    return socket_->SetOption(opt, value);
423  }
424  return 0;
425}
426
427bool RelayConnection::CheckResponse(StunMessage* msg) {
428  return request_manager_->CheckResponse(msg);
429}
430
431void RelayConnection::OnSendPacket(const void* data, size_t size,
432                                   StunRequest* req) {
433  // TODO(mallinath) Find a way to get DSCP value from Port.
434  int sent = socket_->SendTo(
435      data, size, GetAddress(), talk_base::DSCP_NO_CHANGE);
436  if (sent <= 0) {
437    LOG(LS_VERBOSE) << "OnSendPacket: failed sending to " << GetAddress() <<
438        std::strerror(socket_->GetError());
439    ASSERT(sent < 0);
440  }
441}
442
443int RelayConnection::Send(const void* pv, size_t cb,
444                          talk_base::DiffServCodePoint dscp) {
445  return socket_->SendTo(pv, cb, GetAddress(), dscp);
446}
447
448void RelayConnection::SendAllocateRequest(RelayEntry* entry, int delay) {
449  request_manager_->SendDelayed(new AllocateRequest(entry, this), delay);
450}
451
452RelayEntry::RelayEntry(RelayPort* port,
453                       const talk_base::SocketAddress& ext_addr)
454    : port_(port), ext_addr_(ext_addr),
455      server_index_(0), connected_(false), locked_(false),
456      current_connection_(NULL) {
457}
458
459RelayEntry::~RelayEntry() {
460  // Remove all RelayConnections and dispose sockets.
461  delete current_connection_;
462  current_connection_ = NULL;
463}
464
465void RelayEntry::Connect() {
466  // If we're already connected, return.
467  if (connected_)
468    return;
469
470  // If we've exhausted all options, bail out.
471  const ProtocolAddress* ra = port()->ServerAddress(server_index_);
472  if (!ra) {
473    LOG(LS_WARNING) << "No more relay addresses left to try";
474    return;
475  }
476
477  // Remove any previous connection.
478  if (current_connection_) {
479    port()->thread()->Dispose(current_connection_);
480    current_connection_ = NULL;
481  }
482
483  // Try to set up our new socket.
484  LOG(LS_INFO) << "Connecting to relay via " << ProtoToString(ra->proto) <<
485      " @ " << ra->address.ToSensitiveString();
486
487  talk_base::AsyncPacketSocket* socket = NULL;
488
489  if (ra->proto == PROTO_UDP) {
490    // UDP sockets are simple.
491    socket = port_->socket_factory()->CreateUdpSocket(
492        talk_base::SocketAddress(port_->ip(), 0),
493        port_->min_port(), port_->max_port());
494  } else if (ra->proto == PROTO_TCP || ra->proto == PROTO_SSLTCP) {
495    int opts = (ra->proto == PROTO_SSLTCP) ?
496     talk_base::PacketSocketFactory::OPT_SSLTCP : 0;
497    socket = port_->socket_factory()->CreateClientTcpSocket(
498        talk_base::SocketAddress(port_->ip(), 0), ra->address,
499        port_->proxy(), port_->user_agent(), opts);
500  } else {
501    LOG(LS_WARNING) << "Unknown protocol (" << ra->proto << ")";
502  }
503
504  if (!socket) {
505    LOG(LS_WARNING) << "Socket creation failed";
506  }
507
508  // If we failed to get a socket, move on to the next protocol.
509  if (!socket) {
510    port()->thread()->Post(this, kMessageConnectTimeout);
511    return;
512  }
513
514  // Otherwise, create the new connection and configure any socket options.
515  socket->SignalReadPacket.connect(this, &RelayEntry::OnReadPacket);
516  socket->SignalReadyToSend.connect(this, &RelayEntry::OnReadyToSend);
517  current_connection_ = new RelayConnection(ra, socket, port()->thread());
518  for (size_t i = 0; i < port_->options().size(); ++i) {
519    current_connection_->SetSocketOption(port_->options()[i].first,
520                                         port_->options()[i].second);
521  }
522
523  // If we're trying UDP, start binding requests.
524  // If we're trying TCP, wait for connection with a fixed timeout.
525  if ((ra->proto == PROTO_TCP) || (ra->proto == PROTO_SSLTCP)) {
526    socket->SignalClose.connect(this, &RelayEntry::OnSocketClose);
527    socket->SignalConnect.connect(this, &RelayEntry::OnSocketConnect);
528    port()->thread()->PostDelayed(kSoftConnectTimeoutMs, this,
529                                  kMessageConnectTimeout);
530  } else {
531    current_connection_->SendAllocateRequest(this, 0);
532  }
533}
534
535int RelayEntry::GetError() {
536  if (current_connection_ != NULL) {
537    return current_connection_->GetError();
538  }
539  return 0;
540}
541
542RelayConnection* RelayEntry::GetBestConnection(RelayConnection* conn1,
543                                               RelayConnection* conn2) {
544  return conn1->GetProtocol() <= conn2->GetProtocol() ? conn1 : conn2;
545}
546
547void RelayEntry::OnConnect(const talk_base::SocketAddress& mapped_addr,
548                           RelayConnection* connection) {
549  // We are connected, notify our parent.
550  ProtocolType proto = PROTO_UDP;
551  LOG(INFO) << "Relay allocate succeeded: " << ProtoToString(proto)
552            << " @ " << mapped_addr.ToSensitiveString();
553  connected_ = true;
554
555  // In case of Gturn related address is set to null socket address.
556  // This is due to mapped address stun attribute is used for allocated
557  // address.
558  port_->set_related_address(talk_base::SocketAddress());
559  port_->AddExternalAddress(ProtocolAddress(mapped_addr, proto));
560  port_->SetReady();
561}
562
563int RelayEntry::SendTo(const void* data, size_t size,
564                       const talk_base::SocketAddress& addr,
565                       talk_base::DiffServCodePoint dscp) {
566  // If this connection is locked to the address given, then we can send the
567  // packet with no wrapper.
568  if (locked_ && (ext_addr_ == addr))
569    return SendPacket(data, size, dscp);
570
571  // Otherwise, we must wrap the given data in a STUN SEND request so that we
572  // can communicate the destination address to the server.
573  //
574  // Note that we do not use a StunRequest here.  This is because there is
575  // likely no reason to resend this packet. If it is late, we just drop it.
576  // The next send to this address will try again.
577
578  RelayMessage request;
579  request.SetType(STUN_SEND_REQUEST);
580
581  StunByteStringAttribute* magic_cookie_attr =
582      StunAttribute::CreateByteString(STUN_ATTR_MAGIC_COOKIE);
583  magic_cookie_attr->CopyBytes(TURN_MAGIC_COOKIE_VALUE,
584                               sizeof(TURN_MAGIC_COOKIE_VALUE));
585  VERIFY(request.AddAttribute(magic_cookie_attr));
586
587  StunByteStringAttribute* username_attr =
588      StunAttribute::CreateByteString(STUN_ATTR_USERNAME);
589  username_attr->CopyBytes(port_->username_fragment().c_str(),
590                           port_->username_fragment().size());
591  VERIFY(request.AddAttribute(username_attr));
592
593  StunAddressAttribute* addr_attr =
594      StunAttribute::CreateAddress(STUN_ATTR_DESTINATION_ADDRESS);
595  addr_attr->SetIP(addr.ipaddr());
596  addr_attr->SetPort(addr.port());
597  VERIFY(request.AddAttribute(addr_attr));
598
599  // Attempt to lock
600  if (ext_addr_ == addr) {
601    StunUInt32Attribute* options_attr =
602      StunAttribute::CreateUInt32(STUN_ATTR_OPTIONS);
603    options_attr->SetValue(0x1);
604    VERIFY(request.AddAttribute(options_attr));
605  }
606
607  StunByteStringAttribute* data_attr =
608      StunAttribute::CreateByteString(STUN_ATTR_DATA);
609  data_attr->CopyBytes(data, size);
610  VERIFY(request.AddAttribute(data_attr));
611
612  // TODO: compute the HMAC.
613
614  talk_base::ByteBuffer buf;
615  request.Write(&buf);
616
617  return SendPacket(buf.Data(), buf.Length(), dscp);
618}
619
620void RelayEntry::ScheduleKeepAlive() {
621  if (current_connection_) {
622    current_connection_->SendAllocateRequest(this, kKeepAliveDelay);
623  }
624}
625
626int RelayEntry::SetSocketOption(talk_base::Socket::Option opt, int value) {
627  // Set the option on all available sockets.
628  int socket_error = 0;
629  if (current_connection_) {
630    socket_error = current_connection_->SetSocketOption(opt, value);
631  }
632  return socket_error;
633}
634
635void RelayEntry::HandleConnectFailure(
636    talk_base::AsyncPacketSocket* socket) {
637  // Make sure it's the current connection that has failed, it might
638  // be an old socked that has not yet been disposed.
639  if (!socket ||
640      (current_connection_ && socket == current_connection_->socket())) {
641    if (current_connection_)
642      port()->SignalConnectFailure(current_connection_->protocol_address());
643
644    // Try to connect to the next server address.
645    server_index_ += 1;
646    Connect();
647  }
648}
649
650void RelayEntry::OnMessage(talk_base::Message *pmsg) {
651  ASSERT(pmsg->message_id == kMessageConnectTimeout);
652  if (current_connection_) {
653    const ProtocolAddress* ra = current_connection_->protocol_address();
654    LOG(LS_WARNING) << "Relay " << ra->proto << " connection to " <<
655        ra->address << " timed out";
656
657    // Currently we connect to each server address in sequence. If we
658    // have more addresses to try, treat this is an error and move on to
659    // the next address, otherwise give this connection more time and
660    // await the real timeout.
661    //
662    // TODO: Connect to servers in parallel to speed up connect time
663    // and to avoid giving up too early.
664    port_->SignalSoftTimeout(ra);
665    HandleConnectFailure(current_connection_->socket());
666  } else {
667    HandleConnectFailure(NULL);
668  }
669}
670
671void RelayEntry::OnSocketConnect(talk_base::AsyncPacketSocket* socket) {
672  LOG(INFO) << "relay tcp connected to " <<
673      socket->GetRemoteAddress().ToSensitiveString();
674  if (current_connection_ != NULL) {
675    current_connection_->SendAllocateRequest(this, 0);
676  }
677}
678
679void RelayEntry::OnSocketClose(talk_base::AsyncPacketSocket* socket,
680                               int error) {
681  PLOG(LERROR, error) << "Relay connection failed: socket closed";
682  HandleConnectFailure(socket);
683}
684
685void RelayEntry::OnReadPacket(talk_base::AsyncPacketSocket* socket,
686                              const char* data, size_t size,
687                              const talk_base::SocketAddress& remote_addr) {
688  // ASSERT(remote_addr == port_->server_addr());
689  // TODO: are we worried about this?
690
691  if (current_connection_ == NULL || socket != current_connection_->socket()) {
692    // This packet comes from an unknown address.
693    LOG(WARNING) << "Dropping packet: unknown address";
694    return;
695  }
696
697  // If the magic cookie is not present, then this is an unwrapped packet sent
698  // by the server,  The actual remote address is the one we recorded.
699  if (!port_->HasMagicCookie(data, size)) {
700    if (locked_) {
701      port_->OnReadPacket(data, size, ext_addr_, PROTO_UDP);
702    } else {
703      LOG(WARNING) << "Dropping packet: entry not locked";
704    }
705    return;
706  }
707
708  talk_base::ByteBuffer buf(data, size);
709  RelayMessage msg;
710  if (!msg.Read(&buf)) {
711    LOG(INFO) << "Incoming packet was not STUN";
712    return;
713  }
714
715  // The incoming packet should be a STUN ALLOCATE response, SEND response, or
716  // DATA indication.
717  if (current_connection_->CheckResponse(&msg)) {
718    return;
719  } else if (msg.type() == STUN_SEND_RESPONSE) {
720    if (const StunUInt32Attribute* options_attr =
721        msg.GetUInt32(STUN_ATTR_OPTIONS)) {
722      if (options_attr->value() & 0x1) {
723        locked_ = true;
724      }
725    }
726    return;
727  } else if (msg.type() != STUN_DATA_INDICATION) {
728    LOG(INFO) << "Received BAD stun type from server: " << msg.type();
729    return;
730  }
731
732  // This must be a data indication.
733
734  const StunAddressAttribute* addr_attr =
735      msg.GetAddress(STUN_ATTR_SOURCE_ADDRESS2);
736  if (!addr_attr) {
737    LOG(INFO) << "Data indication has no source address";
738    return;
739  } else if (addr_attr->family() != 1) {
740    LOG(INFO) << "Source address has bad family";
741    return;
742  }
743
744  talk_base::SocketAddress remote_addr2(addr_attr->ipaddr(), addr_attr->port());
745
746  const StunByteStringAttribute* data_attr = msg.GetByteString(STUN_ATTR_DATA);
747  if (!data_attr) {
748    LOG(INFO) << "Data indication has no data";
749    return;
750  }
751
752  // Process the actual data and remote address in the normal manner.
753  port_->OnReadPacket(data_attr->bytes(), data_attr->length(), remote_addr2,
754                      PROTO_UDP);
755}
756
757void RelayEntry::OnReadyToSend(talk_base::AsyncPacketSocket* socket) {
758  if (connected()) {
759    port_->OnReadyToSend();
760  }
761}
762
763int RelayEntry::SendPacket(const void* data, size_t size,
764                           talk_base::DiffServCodePoint dscp) {
765  int sent = 0;
766  if (current_connection_) {
767    // We are connected, no need to send packets anywere else than to
768    // the current connection.
769    sent = current_connection_->Send(data, size, dscp);
770  }
771  return sent;
772}
773
774AllocateRequest::AllocateRequest(RelayEntry* entry,
775                                 RelayConnection* connection)
776    : StunRequest(new RelayMessage()),
777      entry_(entry),
778      connection_(connection) {
779  start_time_ = talk_base::Time();
780}
781
782void AllocateRequest::Prepare(StunMessage* request) {
783  request->SetType(STUN_ALLOCATE_REQUEST);
784
785  StunByteStringAttribute* username_attr =
786      StunAttribute::CreateByteString(STUN_ATTR_USERNAME);
787  username_attr->CopyBytes(
788      entry_->port()->username_fragment().c_str(),
789      entry_->port()->username_fragment().size());
790  VERIFY(request->AddAttribute(username_attr));
791}
792
793int AllocateRequest::GetNextDelay() {
794  int delay = 100 * talk_base::_max(1 << count_, 2);
795  count_ += 1;
796  if (count_ == 5)
797    timeout_ = true;
798  return delay;
799}
800
801void AllocateRequest::OnResponse(StunMessage* response) {
802  const StunAddressAttribute* addr_attr =
803      response->GetAddress(STUN_ATTR_MAPPED_ADDRESS);
804  if (!addr_attr) {
805    LOG(INFO) << "Allocate response missing mapped address.";
806  } else if (addr_attr->family() != 1) {
807    LOG(INFO) << "Mapped address has bad family";
808  } else {
809    talk_base::SocketAddress addr(addr_attr->ipaddr(), addr_attr->port());
810    entry_->OnConnect(addr, connection_);
811  }
812
813  // We will do a keep-alive regardless of whether this request suceeds.
814  // This should have almost no impact on network usage.
815  entry_->ScheduleKeepAlive();
816}
817
818void AllocateRequest::OnErrorResponse(StunMessage* response) {
819  const StunErrorCodeAttribute* attr = response->GetErrorCode();
820  if (!attr) {
821    LOG(INFO) << "Bad allocate response error code";
822  } else {
823    LOG(INFO) << "Allocate error response:"
824              << " code=" << attr->code()
825              << " reason='" << attr->reason() << "'";
826  }
827
828  if (talk_base::TimeSince(start_time_) <= kRetryTimeout)
829    entry_->ScheduleKeepAlive();
830}
831
832void AllocateRequest::OnTimeout() {
833  LOG(INFO) << "Allocate request timed out";
834  entry_->HandleConnectFailure(connection_->socket());
835}
836
837}  // namespace cricket
Note: See TracBrowser for help on using the repository browser.