]> git.stg.codes - stg.git/blob - projects/sgauthstress/proto.h
Merge branch 'stg-2.409-radius'
[stg.git] / projects / sgauthstress / proto.h
1 #ifndef __PROTO_H__
2 #define __PROTO_H__
3
4 #ifndef LINUX
5 #include <netinet/in_systm.h>
6 #endif
7 #include <netinet/ip.h>
8 #include <pthread.h>
9 #include <poll.h>
10
11 #include <string>
12 #include <list>
13 #include <vector>
14 #include <map>
15
16 #include "stg/os_int.h"
17 #include "stg/blowfish.h"
18
19 #include "user.h"
20
21 class PROTO;
22
23 typedef bool (PROTO::*PacketProcessor)(const void *, USER *);
24
25 class PROTO {
26     public:
27         PROTO(const std::string & server,
28               uint16_t port,
29               uint16_t localPort,
30               int timeout = 1);
31         ~PROTO();
32
33         bool Start();
34         bool Stop();
35
36         const std::string GetStrError() const { return errorStr; }
37
38         void AddUser(const USER & user, bool connect = false);
39
40         bool Connect(uint32_t ip);
41         bool Disconnect(uint32_t ip);
42
43         size_t UserCount() const { return users.size(); }
44     private:
45         BLOWFISH_CTX ctx;
46         struct sockaddr_in localAddr;
47         struct sockaddr_in serverAddr;
48         int timeout;
49
50         std::list<std::pair<uint32_t, USER> > users;
51         std::vector<struct pollfd> pollFds;
52
53         bool running;
54         bool stopped;
55
56         pthread_t tid;
57         pthread_mutex_t mutex;
58
59         std::string errorStr;
60
61         std::map<std::string, PacketProcessor> processors;
62
63         static void * Runner(void * data);
64
65         void Run();
66         void CheckTimeouts();
67         bool RecvPacket();
68         bool SendPacket(const void * buffer, size_t length, USER * user);
69         bool HandlePacket(const char * buffer, size_t length, USER * user);
70
71         bool CONN_SYN_ACK_Proc(const void * buffer, USER * user);
72         bool ALIVE_SYN_Proc(const void * buffer, USER * user);
73         bool DISCONN_SYN_ACK_Proc(const void * buffer, USER * user);
74         bool FIN_Proc(const void * buffer, USER * user);
75         bool INFO_Proc(const void * buffer, USER * user);
76         bool ERR_Proc(const void * buffer, USER * user);
77
78         bool Send_CONN_SYN(USER * user);
79         bool Send_CONN_ACK(USER * user);
80         bool Send_DISCONN_SYN(USER * user);
81         bool Send_DISCONN_ACK(USER * user);
82         bool Send_ALIVE_ACK(USER * user);
83
84         bool RealConnect(USER * user);
85         bool RealDisconnect(USER * user);
86 };
87
88 #endif