- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
32 GPop::ServerPeer::ServerPeer( GNet::Server::PeerInfo peer_info , Server & server , Store & store ,
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
72 void GPop::ServerPeer::onSendComplete()
73 {
74 m_protocol.resume() ; // calls back to protocolSend()
75 }
76
77 bool GPop::ServerPeer::securityEnabled() const
78 {
79 GSsl::Library * ssl = GSsl::Library::instance() ;
80 bool enabled = ssl != NULL && ssl->enabled(true) ;
81 G_DEBUG( "ServerPeer::securityEnabled: ssl library " << (enabled?"enabled":"disabled") ) ;
82 return enabled ;
83 }
84
85 void GPop::ServerPeer::securityStart()
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
105 GPop::Server::~Server()
106 {
107 // early cleanup -- not really required
108 serverCleanup() ; // base class
109 }
110
111 void GPop::Server::report() const
112 {
113 serverReport( "pop" ) ; // base class implementation
114 G_LOG_S( "GPop::Server: pop authentication secrets from \"" << m_secrets.path() << "\"" ) ;
115 }
116
117 GNet::ServerPeer * GPop::Server::newPeer( GNet::Server::PeerInfo peer_info )
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
146 GPop::Server::Config::Config() :
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
G_LOG_S
#define G_LOG_S(expr)
Definition: glog.h:103
GSsl::Library::instance
static Library * instance()
Returns a pointer to a library object, if any.
Definition: gssl_openssl.cpp:254
GNet::ServerPeer
An abstract base class for the GNet::Server's connection to a remote client.
Definition: gserver.h:191
gssl.h
GPop::Secrets
A simple interface to a store of secrets as used in authentication.
Definition: gpopsecrets.h:44
GNet
Network classes.
glog.h
GPop::ServerPeer::onReceive
virtual bool onReceive(const std::string &)
Final override from GNet::BufferedServerPeer.
Definition: gpopserver.cpp:56
G::Strings
std::list< std::string > Strings
A std::list of std::strings.
Definition: gstrings.h:39
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::Local::isLocal
static bool isLocal(const Address &)
Returns true if the given address appears to be local.
Definition: glocal.cpp:82
GPop::Server::report
void report() const
Generates helpful diagnostics after construction.
Definition: gpopserver.cpp:111
GNet::Server::PeerInfo::m_address
Address m_address
Definition: gserver.h:94
GPop::ServerPeer::onSendComplete
virtual void onSendComplete()
Final override from GNet::BufferedServerPeer.
Definition: gpopserver.cpp:72
GPop::Server::Config
A structure containing GPop::Server configuration parameters.
Definition: gpopserver.h:101
GPop::ServerProtocol::init
void init()
Starts the protocol.
Definition: gpopserverprotocol.cpp:71
GPop::Store
A message store.
Definition: gpopstore.h:46
GPop::ServerPeer::protocolSend
virtual bool protocolSend(const std::string &line, size_t)
Final override from GPop::ServerProtocol::Sender.
Definition: gpopserver.cpp:67
GPop::ServerPeer::securityEnabled
virtual bool securityEnabled() const
Final override from GPop::ServerProtocol::Security.
Definition: gpopserver.cpp:77
GPop::Server::Config::Config
Config()
Definition: gpopserver.cpp:146
GPop::Server
A POP server class.
Definition: gpopserver.h:96
GPop::ServerProtocolText
A default implementation for the ServerProtocol::Text interface.
Definition: gpopserverprotocol.h:212
GNet::Address::displayString
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.
Definition: gaddress_ipv4.cpp:581
gassert.h
GPop::ServerProtocol::Text
An interface used by ServerProtocol to provide response text strings.
Definition: gpopserverprotocol.h:62
GPop::ServerPeer::securityStart
virtual void securityStart()
Final override from GPop::ServerProtocol::Security.
Definition: gpopserver.cpp:85
GPop::Server::~Server
virtual ~Server()
Destructor.
Definition: gpopserver.cpp:105
G_DEBUG
#define G_DEBUG(expr)
Definition: glog.h:95
gpop.h
GPop::ServerProtocol::Config
A structure containing configuration parameters for ServerProtocol. NOT USED.
Definition: gpopserverprotocol.h:72
GPop::Server::newPeer
GNet::ServerPeer * newPeer(GNet::Server::PeerInfo)
From MultiServer.
Definition: gpopserver.cpp:117
glocal.h
GPop::ServerPeer::ServerPeer
ServerPeer(GNet::Server::PeerInfo, Server &, Store &, const Secrets &, std::auto_ptr< ServerProtocol::Text > ptext, ServerProtocol::Config)
Constructor.
Definition: gpopserver.cpp:32
GSsl::Library
A RAII class for initialising the underlying ssl library.
Definition: gssl.h:147
GPop::ServerPeer::onSecure
virtual void onSecure(const std::string &)
Final override from GNet::SocketProtocolSink.
Definition: gpopserver.cpp:90
GSsl::Library::enabled
bool enabled(bool for_serving=false) const
Returns true if this is a real and enabled ssl library.
Definition: gssl_openssl.cpp:259
GPop::ServerPeer
Represents a connection from a POP client.
Definition: gpopserver.h:50
gpopserver.h
gmemory.h
GNet::Server::PeerInfo
A structure used in GNet::Server::newPeer().
Definition: gserver.h:91
GPop::Server::Server
Server(Store &store, const Secrets &, Config)
Constructor. The 'secrets' reference is kept.
Definition: gpopserver.cpp:97
GPop::ServerPeer::onDelete
virtual void onDelete(const std::string &)
Final override from GNet::ServerPeer.
Definition: gpopserver.cpp:50
G_WARNING
#define G_WARNING(expr)
Definition: glog.h:107
gdebug.h