~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/libjingle/talk/base/basicpacketsocketfactory.cc

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * libjingle
 
3
 * Copyright 2011, 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/basicpacketsocketfactory.h"
 
29
 
 
30
#include "talk/base/asyncudpsocket.h"
 
31
#include "talk/base/asynctcpsocket.h"
 
32
#include "talk/base/logging.h"
 
33
#include "talk/base/socketadapters.h"
 
34
#include "talk/base/thread.h"
 
35
 
 
36
namespace talk_base {
 
37
 
 
38
BasicPacketSocketFactory::BasicPacketSocketFactory()
 
39
    : thread_(Thread::Current()),
 
40
      socket_factory_(NULL) {
 
41
}
 
42
 
 
43
BasicPacketSocketFactory::BasicPacketSocketFactory(Thread* thread)
 
44
    : thread_(thread),
 
45
      socket_factory_(NULL) {
 
46
}
 
47
 
 
48
BasicPacketSocketFactory::BasicPacketSocketFactory(
 
49
    SocketFactory* socket_factory)
 
50
    : thread_(NULL),
 
51
      socket_factory_(socket_factory) {
 
52
}
 
53
 
 
54
BasicPacketSocketFactory::~BasicPacketSocketFactory() {
 
55
}
 
56
 
 
57
AsyncPacketSocket* BasicPacketSocketFactory::CreateUdpSocket(
 
58
    const SocketAddress& address, int min_port, int max_port) {
 
59
  // UDP sockets are simple.
 
60
  talk_base::AsyncSocket* socket =
 
61
      socket_factory()->CreateAsyncSocket(SOCK_DGRAM);
 
62
  if (!socket) {
 
63
    return NULL;
 
64
  }
 
65
  if (BindSocket(socket, address, min_port, max_port) < 0) {
 
66
    LOG(LS_ERROR) << "UDP bind failed with error "
 
67
                    << socket->GetError();
 
68
    delete socket;
 
69
    return NULL;
 
70
  }
 
71
  return new talk_base::AsyncUDPSocket(socket);
 
72
}
 
73
 
 
74
AsyncPacketSocket* BasicPacketSocketFactory::CreateServerTcpSocket(
 
75
    const SocketAddress& local_address, int min_port, int max_port, bool ssl) {
 
76
  talk_base::AsyncSocket* socket =
 
77
      socket_factory()->CreateAsyncSocket(SOCK_STREAM);
 
78
  if (!socket) {
 
79
    return NULL;
 
80
  }
 
81
 
 
82
  if (BindSocket(socket, local_address, min_port, max_port) < 0) {
 
83
    LOG(LS_ERROR) << "TCP bind failed with error "
 
84
                  << socket->GetError();
 
85
    delete socket;
 
86
    return NULL;
 
87
  }
 
88
 
 
89
  // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
 
90
  if (ssl) {
 
91
    socket = new talk_base::AsyncSSLSocket(socket);
 
92
  }
 
93
 
 
94
  // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
 
95
  // See http://go/gtalktcpnodelayexperiment
 
96
  socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
 
97
 
 
98
  return new talk_base::AsyncTCPSocket(socket, true);
 
99
}
 
100
 
 
101
AsyncPacketSocket* BasicPacketSocketFactory::CreateClientTcpSocket(
 
102
    const SocketAddress& local_address, const SocketAddress& remote_address,
 
103
    const ProxyInfo& proxy_info, const std::string& user_agent, bool ssl) {
 
104
  talk_base::AsyncSocket* socket =
 
105
      socket_factory()->CreateAsyncSocket(SOCK_STREAM);
 
106
  if (!socket) {
 
107
    return NULL;
 
108
  }
 
109
 
 
110
  if (BindSocket(socket, local_address, 0, 0) < 0) {
 
111
    LOG(LS_ERROR) << "TCP bind failed with error "
 
112
                  << socket->GetError();
 
113
    delete socket;
 
114
    return NULL;
 
115
  }
 
116
 
 
117
  // If using a proxy, wrap the socket in a proxy socket.
 
118
  if (proxy_info.type == talk_base::PROXY_SOCKS5) {
 
119
    socket = new talk_base::AsyncSocksProxySocket(
 
120
        socket, proxy_info.address, proxy_info.username, proxy_info.password);
 
121
  } else if (proxy_info.type == talk_base::PROXY_HTTPS) {
 
122
    socket = new talk_base::AsyncHttpsProxySocket(
 
123
        socket, user_agent, proxy_info.address,
 
124
        proxy_info.username, proxy_info.password);
 
125
  }
 
126
 
 
127
  // If using SSLTCP, wrap the TCP socket in a pseudo-SSL socket.
 
128
  if (ssl) {
 
129
    socket = new talk_base::AsyncSSLSocket(socket);
 
130
  }
 
131
 
 
132
  if (socket->Connect(remote_address) < 0) {
 
133
    LOG(LS_ERROR) << "TCP connect failed with error "
 
134
                  << socket->GetError();
 
135
    delete socket;
 
136
    return NULL;
 
137
  }
 
138
 
 
139
  // Finally, wrap that socket in a TCP packet socket.
 
140
  talk_base::AsyncTCPSocket* tcp_socket =
 
141
      new talk_base::AsyncTCPSocket(socket, false);
 
142
 
 
143
  // Set TCP_NODELAY (via OPT_NODELAY) for improved performance.
 
144
  // See http://go/gtalktcpnodelayexperiment
 
145
  tcp_socket->SetOption(talk_base::Socket::OPT_NODELAY, 1);
 
146
 
 
147
  return tcp_socket;
 
148
}
 
149
 
 
150
int BasicPacketSocketFactory::BindSocket(
 
151
    AsyncSocket* socket, const SocketAddress& local_address,
 
152
    int min_port, int max_port) {
 
153
  int ret = -1;
 
154
  if (min_port == 0 && max_port == 0) {
 
155
    // If there's no port range, let the OS pick a port for us.
 
156
    ret = socket->Bind(local_address);
 
157
  } else {
 
158
    // Otherwise, try to find a port in the provided range.
 
159
    for (int port = min_port; ret < 0 && port <= max_port; ++port) {
 
160
      ret = socket->Bind(talk_base::SocketAddress(local_address.ipaddr(),
 
161
                                                  port));
 
162
    }
 
163
  }
 
164
  return ret;
 
165
}
 
166
 
 
167
SocketFactory* BasicPacketSocketFactory::socket_factory() {
 
168
  if (thread_) {
 
169
    ASSERT(thread_ == Thread::Current());
 
170
    return thread_->socketserver();
 
171
  } else {
 
172
    return socket_factory_;
 
173
  }
 
174
}
 
175
 
 
176
}  // namespace talk_base