]> git.stg.codes - stg.git/blob - projects/rscriptd/listener.h
Merge branch 'stg-2.409-radius'
[stg.git] / projects / rscriptd / listener.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include <pthread.h>
23
24 #include <string>
25 #include <vector>
26 #include <list>
27 #include <functional>
28
29 #include "stg/os_int.h"
30 #include "stg/blowfish.h"
31 #include "stg/rs_packets.h"
32 #include "stg/logger.h"
33
34 struct UserData
35 {
36     std::string params;
37     std::string login;
38     uint32_t    ip;
39     uint32_t    id;
40 };
41
42 struct PendingData : public UserData
43 {
44     enum {CONNECT, ALIVE, DISCONNECT} type;
45 };
46
47 struct AliveData : public UserData
48 {
49     explicit AliveData(const UserData & data)
50         : UserData(data),
51           lastAlive(time(NULL))
52     {};
53     bool operator<(const std::string & rvalue) const { return login < rvalue; };
54     time_t      lastAlive;
55 };
56
57 class IsNotTimedOut : public std::unary_function<const AliveData &, bool> {
58     public:
59         explicit IsNotTimedOut(double to) : timeout(to), now(time(NULL)) {}
60         bool operator()(const AliveData & data) const
61         {
62             return difftime(now, data.lastAlive) < timeout;
63         }
64     private:
65         double timeout;
66         time_t now;
67 };
68
69 class LISTENER
70 {
71 public:
72                         LISTENER();
73                         ~LISTENER(){};
74
75     void                SetPort(uint16_t p) { port = p; };
76     void                SetPassword(const std::string & p);
77     void                SetUserTimeout(int t) { userTimeout = t; };
78     void                SetScriptOnConnect(const std::string & script) { scriptOnConnect = script; };
79     void                SetScriptOnDisconnect(const std::string & script) { scriptOnDisconnect = script; };
80
81     bool                Start();
82     bool                Stop();
83     bool                IsRunning() const { return !receiverStopped && !processorStopped; };
84
85     const std::string & GetStrError() const { return errorStr; };
86     const std::string & GetVersion() const { return version; };
87
88 private:
89     // Threading stuff
90     static void *       Run(void * self);
91     static void *       RunProcessor(void * self);
92     void                Runner();
93     void                ProcessorRunner();
94     // Networking stuff
95     bool                PrepareNet();
96     bool                FinalizeNet();
97     bool                RecvPacket();
98     // Parsing stuff
99     bool                CheckHeader(const RS::PACKET_HEADER & header) const;
100     bool                GetParams(char * buffer, UserData & data);
101     // Processing stuff
102     void                ProcessPending();
103     void                ProcessTimeouts();
104     bool                Disconnect(const UserData & data) const;
105     bool                Connect(const UserData & data) const;
106
107     BLOWFISH_CTX        ctxS;
108     STG_LOGGER &        WriteServLog;
109
110     mutable std::string errorStr;
111     std::string         scriptOnConnect;
112     std::string         scriptOnDisconnect;
113     std::string         password;
114     uint16_t            port;
115
116     bool                running;
117     bool                receiverStopped;
118     bool                processorStopped;
119     std::vector<AliveData> users;
120     std::list<PendingData> pending;
121     int                 userTimeout;
122
123     pthread_t           receiverThread;
124     pthread_t           processorThread;
125     pthread_mutex_t     mutex;
126
127     int                 listenSocket;
128
129     std::string         version;
130
131     friend class DisconnectUser;
132 };
133
134 class DisconnectUser : public std::unary_function<const UserData &, void> {
135     public:
136         explicit DisconnectUser(LISTENER & l) : listener(l) {};
137         void operator()(const UserData & data)
138         {
139             listener.Disconnect(data);
140         };
141     private:
142         LISTENER & listener;
143 };
144 //-----------------------------------------------------------------------------