gloox 1.0.24
connectiontcpserver.cpp
1/*
2 Copyright (c) 2004-2019 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
65namespace 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
85 {
86 util::MutexGuard mg( &m_sendMutex );
87
88 if( m_socket >= 0 || m_state > StateDisconnected )
89 return ConnNoError;
90
92
93 if( m_socket < 0 )
94 m_socket = DNS::getSocket( m_logInstance );
95
96 if( m_socket < 0 )
97 return ConnIoError;
98
99#ifdef HAVE_SETSOCKOPT
100 int buf = 0;
101#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
102 int bufbytes = sizeof( int );
103#else
104 socklen_t bufbytes = sizeof( int );
105#endif
106 if( ( getsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<char*>( &buf ), &bufbytes ) != -1 ) && ( m_bufsize > buf ) )
107 setsockopt( m_socket, SOL_SOCKET, SO_RCVBUF, reinterpret_cast<const char*>( &m_bufsize ), sizeof( m_bufsize ) );
108
109 if( ( getsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<char*>( &buf ), &bufbytes ) != -1 ) && ( m_bufsize > buf ) )
110 setsockopt( m_socket, SOL_SOCKET, SO_SNDBUF, reinterpret_cast<const char*>( &m_bufsize ), sizeof( m_bufsize ) );
111#endif
112
113 int status = 0;
114 int err = 0;
115 struct addrinfo hints;
116 struct addrinfo *res;
117
118 memset( &hints, 0, sizeof hints );
119 hints.ai_family = AF_UNSPEC;
120 hints.ai_socktype = SOCK_STREAM;
121 hints.ai_flags = AI_PASSIVE;
122 status = getaddrinfo( m_server.empty() ? 0 : m_server.c_str(), util::int2string( m_port ).c_str(), &hints, &res );
123 if( status != 0 )
124 {
125 err = errno;
126 std::string message = "getaddrinfo() for " + ( m_server.empty() ? std::string( "*" ) : m_server )
127 + " (" + util::int2string( m_port ) + ") failed. "
128#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
129 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
130#else
131 + strerror( err ) + " (errno: " + util::int2string( err ) + ")";
132#endif
133 m_logInstance.dbg( LogAreaClassConnectionTCPServer, message );
134
135 DNS::closeSocket( m_socket, m_logInstance );
136 return ConnIoError;
137 }
138
139 if( bind( m_socket, res->ai_addr, res->ai_addrlen ) < 0 )
140 {
141 err = errno;
142 std::string message = "bind() to " + ( m_server.empty() ? std::string( "*" ) : m_server )
143 + " (" + /*inet_ntoa( local.sin_addr ) + ":" +*/ util::int2string( m_port ) + ") failed. "
144#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
145 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
146#else
147 + strerror( err ) + " (errno: " + util::int2string( err ) + ")";
148#endif
149 m_logInstance.dbg( LogAreaClassConnectionTCPServer, message );
150
151 DNS::closeSocket( m_socket, m_logInstance );
152 return ConnIoError;
153 }
154
155 if( listen( m_socket, 10 ) < 0 )
156 {
157 err = errno;
158 std::string message = "listen() on " + ( m_server.empty() ? std::string( "*" ) : m_server )
159 + " (" + /*inet_ntoa( local.sin_addr ) +*/ ":" + util::int2string( m_port ) + ") failed. "
160#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
161 "WSAGetLastError: " + util::int2string( ::WSAGetLastError() );
162#else
163 + strerror( err ) + " (errno: " + util::int2string( err ) + ")";
164#endif
165 m_logInstance.dbg( LogAreaClassConnectionTCPServer, message );
166
167 DNS::closeSocket( m_socket, m_logInstance );
168 return ConnIoError;
169 }
170
171 m_cancel = false;
172 return ConnNoError;
173 }
174
176 {
177 m_recvMutex.lock();
178
179 if( m_cancel || m_socket < 0 || !m_connectionHandler )
180 {
181 m_recvMutex.unlock();
182 return ConnNotConnected;
183 }
184
185 if( !dataAvailable( timeout ) )
186 {
187 m_recvMutex.unlock();
188 return ConnNoError;
189 }
190
191 struct sockaddr_storage they;
192 int addr_size = sizeof( struct sockaddr_storage );
193#if defined( _WIN32 ) && !defined( __SYMBIAN32__ )
194 int newfd = static_cast<int>( accept( static_cast<SOCKET>( m_socket ), reinterpret_cast<struct sockaddr*>( &they ), &addr_size ) );
195#else
196 int newfd = accept( m_socket, reinterpret_cast<struct sockaddr*>( &they ), reinterpret_cast<socklen_t*>( &addr_size ) );
197#endif
198
199 m_recvMutex.unlock();
200
201 char buffer[INET6_ADDRSTRLEN];
202 char portstr[NI_MAXSERV];
203 int err = getnameinfo( reinterpret_cast<struct sockaddr*>( &they ), addr_size, buffer, sizeof( buffer ),
204 portstr, sizeof( portstr ), NI_NUMERICHOST | NI_NUMERICSERV );
205 if( err )
206 return ConnIoError;
207
208 ConnectionTCPClient* conn = new ConnectionTCPClient( m_logInstance, buffer,
209 atoi( portstr ) );
210 conn->setSocket( newfd );
211 m_connectionHandler->handleIncomingConnection( this, conn );
212
213 return ConnNoError;
214 }
215
216}
An abstract base class for a connection.
ConnectionState m_state
This is an abstract base class to receive incoming connection attempts. Do not confuse this with Conn...
virtual void handleIncomingConnection(ConnectionBase *server, ConnectionBase *connection)=0
This is a base class for a simple TCP connection.
This is an implementation of a simple TCP connection.
ConnectionTCPServer(ConnectionHandler *ch, const LogSink &logInstance, const std::string &ip, int port)
virtual ConnectionError recv(int timeout=-1)
virtual ConnectionBase * newInstance() const
virtual ConnectionError connect()
static int getSocket(const LogSink &logInstance)
Definition: dns.cpp:339
static void closeSocket(int fd, const LogSink &logInstance)
Definition: dns.cpp:518
An implementation of log sink and source.
Definition: logsink.h:39
void dbg(LogArea area, const std::string &message) const
Definition: logsink.h:66
A simple implementation of a mutex guard.
Definition: mutexguard.h:32
The namespace for the gloox library.
Definition: adhoc.cpp:28
ConnectionError
Definition: gloox.h:684
@ ConnNotConnected
Definition: gloox.h:715
@ ConnNoError
Definition: gloox.h:685
@ ConnIoError
Definition: gloox.h:696
@ LogAreaClassConnectionTCPServer
Definition: gloox.h:1063
@ StateDisconnected
Definition: gloox.h:642
@ StateConnecting
Definition: gloox.h:643