gloox  1.0.17
connectiontcpserver.cpp
1 /*
2  Copyright (c) 2004-2016 by Jakob Schröter <js@camaya.net>
3  This file is part of the gloox library. http://camaya.net/gloox
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 
15 #include "gloox.h"
16 
17 #include "config.h"
18 
19 #include "connectiontcpserver.h"
20 #include "connectiontcpclient.h"
21 #include "connectionhandler.h"
22 #include "dns.h"
23 #include "logsink.h"
24 #include "mutex.h"
25 #include "mutexguard.h"
26 #include "util.h"
27 
28 #ifdef __MINGW32__
29 # include <winsock2.h>
30 # include <ws2tcpip.h>
31 #endif
32 
33 #if ( !defined( _WIN32 ) && !defined( _WIN32_WCE ) ) || defined( __SYMBIAN32__ )
34 # include <netinet/in.h>
35 # include <arpa/nameser.h>
36 # include <resolv.h>
37 # include <netdb.h>
38 # include <arpa/inet.h>
39 # include <sys/socket.h>
40 # include <sys/un.h>
41 # include <sys/select.h>
42 # include <unistd.h>
43 # include <errno.h>
44 #endif
45 
46 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
47 # include <winsock2.h>
48 # include <ws2tcpip.h>
49 #elif defined( _WIN32_WCE )
50 # include <winsock2.h>
51 #endif
52 
53 #include <cstdlib>
54 #include <string>
55 
56 #ifndef _WIN32_WCE
57 # include <sys/types.h>
58 #endif
59 
60 // remove for 1.1
61 #ifndef INVALID_SOCKET
62 # define INVALID_SOCKET -1
63 #endif
64 
65 namespace gloox
66 {
67 
69  const std::string& ip, int port )
70  : ConnectionTCPBase( 0, logInstance, ip, port ),
71  m_connectionHandler( ch )
72  {
73  }
74 
76  {
77  }
78 
80  {
81  return new ConnectionTCPServer( m_connectionHandler, m_logInstance, m_server, m_port );
82  }
83 
84  // remove for 1.1
85  int ConnectionTCPServer::getSocket( int af, int socktype, int proto, const LogSink& logInstance )
86  {
87 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
88  SOCKET fd;
89 #else
90  int fd;
91 #endif
92  if( ( fd = ::socket( af, socktype, proto ) ) == INVALID_SOCKET )
93  {
94  std::string message = "getSocket( "
95  + util::int2string( af ) + ", "
96  + util::int2string( socktype ) + ", "
97  + util::int2string( proto )
98  + " ) failed. "
99 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
100  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
101 #else
102  "errno: " + util::int2string( errno ) + ": " + strerror( errno );
103 #endif
104  logInstance.dbg( LogAreaClassDns, message );
105 
106 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
107  if( WSACleanup() != 0 )
108  {
109  logInstance.dbg( LogAreaClassDns, "WSACleanup() failed. WSAGetLastError: "
110  + util::int2string( ::WSAGetLastError() ) );
111  }
112 #endif
113 
114  return -ConnConnectionRefused;
115  }
116 
117 #ifdef HAVE_SETSOCKOPT
118  int timeout = 5000;
119  int reuseaddr = 1;
120  setsockopt( fd, SOL_SOCKET, SO_SNDTIMEO, (char*)&timeout, sizeof( timeout ) );
121  setsockopt( fd, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseaddr, sizeof( reuseaddr ) );
122 #endif
123 
124  return (int)fd;
125  }
126 
128  {
129  util::MutexGuard mg( &m_sendMutex );
130 
131  if( m_socket >= 0 || m_state > StateDisconnected )
132  return ConnNoError;
133 
135 
136  if( m_socket < 0 )
137  m_socket = DNS::getSocket( m_logInstance );
138 
139  if( m_socket < 0 )
140  return ConnIoError;
141 
142 #ifdef HAVE_SETSOCKOPT
143  int buf = 0;
144 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
145  int bufbytes = sizeof( int );
146 #else
147  socklen_t bufbytes = sizeof( int );
148 #endif
149  if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (char*)&buf, &bufbytes ) != -1 ) &&
150  ( m_bufsize > buf ) )
151  setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, (char*)&m_bufsize, sizeof( m_bufsize ) );
152 
153  if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (char*)&buf, &bufbytes ) != -1 ) &&
154  ( m_bufsize > buf ) )
155  setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, (char*)&m_bufsize, sizeof( m_bufsize ) );
156 #endif
157 
158  int status = 0;
159  struct addrinfo hints;
160  struct addrinfo *res;
161 
162  memset( &hints, 0, sizeof hints );
163  hints.ai_family = AF_UNSPEC;
164  hints.ai_socktype = SOCK_STREAM;
165  hints.ai_flags = AI_PASSIVE;
166  status = getaddrinfo( m_server.c_str(), util::int2string( m_port ).c_str(), &hints, &res );
167  if( status != 0 )
168  {
169  std::string message = "getaddrinfo() for " + ( m_server.empty() ? std::string( "*" ) : m_server )
170  + " (" + util::int2string( m_port ) + ") failed. "
171 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
172  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
173 #else
174  "errno: " + util::int2string( errno );
175 #endif
176  return ConnIoError;
177  }
178 
179  m_socket = ::socket( res->ai_family, res->ai_socktype, res->ai_protocol );
180 
181  if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
182  {
183  std::string message = "bind() to " + ( m_server.empty() ? std::string( "*" ) : m_server )
184  + " (" + /*inet_ntoa( local.sin_addr ) + ":" +*/ util::int2string( m_port ) + ") failed. "
185 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
186  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
187 #else
188  "errno: " + util::int2string( errno );
189 #endif
190  m_logInstance.dbg( LogAreaClassConnectionTCPServer, message );
191 
192  return ConnIoError;
193  }
194 
195  if( listen( m_socket, 10 ) < 0 )
196  {
197  std::string message = "listen on " + ( m_server.empty() ? std::string( "*" ) : m_server )
198  + " (" + /*inet_ntoa( local.sin_addr ) +*/ ":" + util::int2string( m_port ) + ") failed. "
199 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
200  "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
201 #else
202  "errno: " + util::int2string( errno );
203 #endif
204  m_logInstance.dbg( LogAreaClassConnectionTCPServer, message );
205 
206  return ConnIoError;
207  }
208 
209  m_cancel = false;
210  return ConnNoError;
211  }
212 
214  {
215  m_recvMutex.lock();
216 
217  if( m_cancel || m_socket < 0 || !m_connectionHandler )
218  {
219  m_recvMutex.unlock();
220  return ConnNotConnected;
221  }
222 
223  if( !dataAvailable( timeout ) )
224  {
225  m_recvMutex.unlock();
226  return ConnNoError;
227  }
228 
229  struct sockaddr_storage they;
230  int addr_size = sizeof( struct sockaddr_storage );
231 #if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
232  int newfd = static_cast<int>( accept( static_cast<SOCKET>( m_socket ), (struct sockaddr*)&they, &addr_size ) );
233 #else
234  int newfd = accept( m_socket, (struct sockaddr*)&they, (socklen_t*)&addr_size );
235 #endif
236 
237  m_recvMutex.unlock();
238 
239  char buffer[INET6_ADDRSTRLEN];
240  char portstr[NI_MAXSERV];
241  int err = getnameinfo( (struct sockaddr*)&they, addr_size, buffer, sizeof( buffer ),
242  portstr, sizeof( portstr ), NI_NUMERICHOST | NI_NUMERICSERV );
243  if( !err )
244  return ConnIoError;
245 
246  ConnectionTCPClient* conn = new ConnectionTCPClient( m_logInstance, buffer,
247  atoi( portstr ) );
248  conn->setSocket( newfd );
249  m_connectionHandler->handleIncomingConnection( this, conn );
250 
251  return ConnNoError;
252  }
253 
254 }
An abstract base class for a connection.
A simple implementation of a mutex guard.
Definition: mutexguard.h:31
ConnectionError
Definition: gloox.h:679
virtual ConnectionError recv(int timeout=-1)
virtual void handleIncomingConnection(ConnectionBase *server, ConnectionBase *connection)=0
This is an implementation of a simple TCP connection.
This is a base class for a simple TCP connection.
virtual ConnectionError connect()
This is an abstract base class to receive incoming connection attempts. Do not confuse this with Conn...
The namespace for the gloox library.
Definition: adhoc.cpp:27
virtual ConnectionBase * newInstance() const
void dbg(LogArea area, const std::string &message) const
Definition: logsink.h:66
ConnectionState m_state
static int getSocket(const LogSink &logInstance)
Definition: dns.cpp:335
ConnectionTCPServer(ConnectionHandler *ch, const LogSink &logInstance, const std::string &ip, int port)
An implementation of log sink and source.
Definition: logsink.h:38