gpopserver.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 //
18 // gpopserver.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gpop.h"
23 #include "gpopserver.h"
24 #include "gssl.h"
25 #include "gmemory.h"
26 #include "glocal.h"
27 #include "glog.h"
28 #include "gdebug.h"
29 #include "gassert.h"
30 #include <string>
31 
33  const Secrets & secrets , std::auto_ptr<ServerProtocol::Text> ptext ,
34  ServerProtocol::Config protocol_config ) :
35  GNet::BufferedServerPeer(peer_info,crlf()) ,
36  m_server(server) ,
37  m_ptext(ptext) ,
38  m_protocol(*this,*this,store,secrets,*m_ptext.get(),peer_info.m_address,protocol_config)
39 {
40  G_LOG_S( "GPop::ServerPeer: pop connection from " << peer_info.m_address.displayString() ) ;
41  m_protocol.init() ;
42 }
43 
44 const std::string & GPop::ServerPeer::crlf()
45 {
46  static const std::string s( "\015\012" ) ;
47  return s ;
48 }
49 
50 void GPop::ServerPeer::onDelete( const std::string & reason )
51 {
52  G_LOG_S( "GPop::ServerPeer: pop connection closed: " << reason << (reason.empty()?"":": ")
53  << peerAddress().second.displayString() ) ;
54 }
55 
56 bool GPop::ServerPeer::onReceive( const std::string & line )
57 {
58  processLine( line ) ;
59  return true ;
60 }
61 
62 void GPop::ServerPeer::processLine( const std::string & line )
63 {
64  m_protocol.apply( line ) ;
65 }
66 
67 bool GPop::ServerPeer::protocolSend( const std::string & line , size_t offset )
68 {
69  return send( line , offset ) ; // BufferedServerPeer::send() -- see also GNet::Sender
70 }
71 
73 {
74  m_protocol.resume() ; // calls back to protocolSend()
75 }
76 
78 {
80  bool enabled = ssl != NULL && ssl->enabled(true) ;
81  G_DEBUG( "ServerPeer::securityEnabled: ssl library " << (enabled?"enabled":"disabled") ) ;
82  return enabled ;
83 }
84 
86 {
87  sslAccept() ; // base class
88 }
89 
90 void GPop::ServerPeer::onSecure( const std::string & certificate )
91 {
92  m_protocol.secure() ;
93 }
94 
95 // ===
96 
97 GPop::Server::Server( Store & store , const Secrets & secrets , Config config ) :
98  GNet::MultiServer( GNet::MultiServer::addressList(config.interfaces,config.port) , false ) ,
99  m_allow_remote(config.allow_remote) ,
100  m_store(store) ,
101  m_secrets(secrets)
102 {
103 }
104 
106 {
107  // early cleanup -- not really required
108  serverCleanup() ; // base class
109 }
110 
112 {
113  serverReport( "pop" ) ; // base class implementation
114  G_LOG_S( "GPop::Server: pop authentication secrets from \"" << m_secrets.path() << "\"" ) ;
115 }
116 
118 {
119  try
120  {
121  std::string reason ;
122  if( ! m_allow_remote && ! GNet::Local::isLocal(peer_info.m_address,reason) )
123  {
124  G_WARNING( "GPop::Server: configured to reject non-local pop connection: " << reason ) ;
125  return NULL ;
126  }
127 
128  std::auto_ptr<ServerProtocol::Text> ptext( newProtocolText(peer_info.m_address) ) ;
129  return new ServerPeer( peer_info , *this , m_store , m_secrets ,
130  ptext , ServerProtocol::Config() ) ;
131  }
132  catch( std::exception & e ) // newPeer()
133  {
134  G_WARNING( "GPop::Server: exception from new connection: " << e.what() ) ;
135  return NULL ;
136  }
137 }
138 
139 GPop::ServerProtocol::Text * GPop::Server::newProtocolText( GNet::Address peer_address ) const
140 {
141  return new ServerProtocolText( peer_address ) ;
142 }
143 
144 // ===
145 
147  allow_remote(false) ,
148  port(110)
149 {
150 }
151 
152 GPop::Server::Config::Config( bool allow_remote_ , unsigned int port_ , const G::Strings & interfaces_ ) :
153  allow_remote(allow_remote_) ,
154  port(port_) ,
155  interfaces(interfaces_)
156 {
157 }
158 
#define G_LOG_S(expr)
Definition: glog.h:103
static Library * instance()
Returns a pointer to a library object, if any.
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:191
A simple interface to a store of secrets as used in authentication.
Definition: gpopsecrets.h:44
Network classes.
virtual bool onReceive(const std::string &)
Final override from GNet::BufferedServerPeer.
Definition: gpopserver.cpp:56
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
static bool isLocal(const Address &)
Returns true if the given address appears to be local.
Definition: glocal.cpp:82
void report() const
Generates helpful diagnostics after construction.
Definition: gpopserver.cpp:111
virtual void onSendComplete()
Final override from GNet::BufferedServerPeer.
Definition: gpopserver.cpp:72
A structure containing GPop::Server configuration parameters.
Definition: gpopserver.h:101
void init()
Starts the protocol.
A message store.
Definition: gpopstore.h:46
virtual bool protocolSend(const std::string &line, size_t)
Final override from GPop::ServerProtocol::Sender.
Definition: gpopserver.cpp:67
virtual bool securityEnabled() const
Final override from GPop::ServerProtocol::Security.
Definition: gpopserver.cpp:77
A POP server class.
Definition: gpopserver.h:96
A default implementation for the ServerProtocol::Text interface.
std::string displayString(bool with_port=true, bool with_scope_id=false) const
Returns a string which represents the address for debugging and diagnostics purposes.
An interface used by ServerProtocol to provide response text strings.
virtual void securityStart()
Final override from GPop::ServerProtocol::Security.
Definition: gpopserver.cpp:85
virtual ~Server()
Destructor.
Definition: gpopserver.cpp:105
#define G_DEBUG(expr)
Definition: glog.h:95
A structure containing configuration parameters for ServerProtocol. NOT USED.
GNet::ServerPeer * newPeer(GNet::Server::PeerInfo)
From MultiServer.
Definition: gpopserver.cpp:117
ServerPeer(GNet::Server::PeerInfo, Server &, Store &, const Secrets &, std::auto_ptr< ServerProtocol::Text > ptext, ServerProtocol::Config)
Constructor.
Definition: gpopserver.cpp:32
A RAII class for initialising the underlying ssl library.
Definition: gssl.h:147
virtual void onSecure(const std::string &)
Final override from GNet::SocketProtocolSink.
Definition: gpopserver.cpp:90
bool enabled(bool for_serving=false) const
Returns true if this is a real and enabled ssl library.
Represents a connection from a POP client.
Definition: gpopserver.h:50
A structure used in GNet::Server::newPeer().
Definition: gserver.h:91
Server(Store &store, const Secrets &, Config)
Constructor. The 'secrets' reference is kept.
Definition: gpopserver.cpp:97
virtual void onDelete(const std::string &)
Final override from GNet::ServerPeer.
Definition: gpopserver.cpp:50
#define G_WARNING(expr)
Definition: glog.h:107