gloox  0.9.9.12
connectiontcpserver.cpp
1 /*
2  Copyright (c) 2004-2008 by Jakob Schroeter <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 "connectiontcpserver.h"
18 #include "connectiontcpclient.h"
19 #include "connectionhandler.h"
20 #include "dns.h"
21 #include "logsink.h"
22 #include "mutex.h"
23 #include "mutexguard.h"
24 
25 #ifdef __MINGW32__
26 # include <winsock.h>
27 #endif
28 
29 #if !defined( _WIN32 ) && !defined( _WIN32_WCE )
30 # include <netinet/in.h>
31 # include <arpa/nameser.h>
32 # include <resolv.h>
33 # include <netdb.h>
34 # include <arpa/inet.h>
35 # include <sys/socket.h>
36 # include <sys/un.h>
37 # include <sys/select.h>
38 # include <unistd.h>
39 #endif
40 
41 #ifdef _WIN32
42 # include <winsock.h>
43 #elif defined( _WIN32_WCE )
44 # include <winsock2.h>
45 #endif
46 
47 #include <cstdlib>
48 #include <string>
49 
50 #ifndef _WIN32_WCE
51 # include <sys/types.h>
52 # include <sstream>
53 #endif
54 
55 namespace gloox
56 {
57 
59  const std::string& ip, int port )
60  : ConnectionTCPBase( 0, logInstance, ip, port ),
61  m_connectionHandler( ch )
62  {
63  }
64 
66  {
67  }
68 
70  {
71  return new ConnectionTCPServer( m_connectionHandler, m_logInstance, m_server, m_port );
72  }
73 
75  {
76  MutexGuard mg( &m_sendMutex );
77 
78  if( m_socket >= 0 || m_state > StateDisconnected )
79  return ConnNoError;
80 
81  m_state = StateConnecting;
82 
83  if( m_socket < 0 )
84  m_socket = DNS::getSocket();
85 
86  if( m_socket < 0 )
87  return ConnIoError;
88 
89  struct sockaddr_in local;
90  local.sin_family = AF_INET;
91  local.sin_port = htons( m_port );
92  local.sin_addr.s_addr = m_server.empty() ? INADDR_ANY : inet_addr( m_server.c_str() );
93  memset( &(local.sin_zero), '\0', 8 );
94 
95  if( bind( m_socket, (struct sockaddr*)&local, sizeof( struct sockaddr ) ) < 0 )
96  return ConnIoError;
97 
98  if( listen( m_socket, 10 ) < 0 )
99  return ConnIoError;
100 
101  m_cancel = false;
102  return ConnNoError;
103  }
104 
106  {
107  m_recvMutex.lock();
108 
109  if( m_cancel || m_socket < 0 || !m_connectionHandler )
110  {
111  m_recvMutex.unlock();
112  return ConnNotConnected;
113  }
114 
115  if( !dataAvailable( timeout ) )
116  {
117  m_recvMutex.unlock();
118  return ConnNoError;
119  }
120 
121  struct sockaddr_in they;
122  int sin_size = sizeof( struct sockaddr_in );
123 #ifdef _WIN32
124  int newfd = accept( m_socket, (struct sockaddr*)&they, &sin_size );
125 #else
126  int newfd = accept( m_socket, (struct sockaddr*)&they, (socklen_t*)&sin_size );
127 #endif
128 
129  m_recvMutex.unlock();
130 
131  ConnectionTCPClient* conn = new ConnectionTCPClient( m_logInstance, inet_ntoa( they.sin_addr ),
132  ntohs( they.sin_port ) );
133  conn->setSocket( newfd );
134  m_connectionHandler->handleIncomingConnection( conn );
135 
136  return ConnNoError;
137  }
138 
139 }