]> git.stg.codes - stg.git/blob - projects/sgauthstress/user.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / sgauthstress / user.cpp
1 #include <sys/types.h>
2 #include <sys/socket.h>
3 #include <netinet/in.h>
4 #include <unistd.h> // close
5
6 #include <cstring>
7 #include <cerrno>
8 #include <stdexcept>
9
10 #include "user.h"
11 #include "stg/ia_packets.h"
12 #include "stg/common.h"
13
14 USER::USER(const std::string & l,
15            const std::string & pwd,
16            uint32_t i)
17     : login(l),
18       password(pwd),
19       ip(i),
20       aliveTimeout(5),
21       userTimeout(60),
22       phase(1),
23       phaseChangeTime(0),
24       rnd(0),
25       sock(-1)
26 {
27 unsigned char key[IA_PASSWD_LEN];
28 memset(key, 0, IA_PASSWD_LEN);
29 strncpy((char *)key, password.c_str(), IA_PASSWD_LEN);
30 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
31 }
32
33 USER::USER(const USER & rvalue)
34     : login(rvalue.login),
35       password(rvalue.password),
36       ip(rvalue.ip),
37       aliveTimeout(rvalue.aliveTimeout),
38       userTimeout(rvalue.userTimeout),
39       phase(1),
40       phaseChangeTime(0),
41       rnd(0),
42       sock(-1)
43 {
44 unsigned char key[IA_PASSWD_LEN];
45 memset(key, 0, IA_PASSWD_LEN);
46 strncpy((char *)key, password.c_str(), IA_PASSWD_LEN);
47 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
48 }
49
50 USER::~USER()
51 {
52 if (sock > 0)
53     close(sock);
54 }
55
56 USER & USER::operator=(const USER & rvalue)
57 {
58 login = rvalue.login;
59 password = rvalue.password;
60 ip = rvalue.ip;
61 aliveTimeout = rvalue.aliveTimeout;
62 userTimeout = rvalue.userTimeout;
63 phase = 1;
64 phaseChangeTime = 0;
65 rnd = 0;
66 sock = -1;
67
68 unsigned char key[IA_PASSWD_LEN];
69 memset(key, 0, IA_PASSWD_LEN);
70 strncpy((char *)key, password.c_str(), IA_PASSWD_LEN);
71 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
72
73 return *this;
74 }
75
76 bool USER::InitNetwork()
77 {
78 sock = socket(AF_INET, SOCK_DGRAM, 0);
79
80 if (sock < 0)
81     {
82     throw std::runtime_error(std::string("USER::USER() - socket creation error: '") + strerror(errno) + "', ip: " + inet_ntostring(ip) + ", login: " + login);
83     }
84
85 struct sockaddr_in addr;
86
87 addr.sin_family = AF_INET;
88 addr.sin_addr.s_addr = ip;
89 addr.sin_port = htons(5554); // :(
90
91 int res = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
92 if (res == -1)
93     {
94     throw std::runtime_error(std::string("USER::USER() - bind error: '") + strerror(errno) + "', ip: " + inet_ntostring(ip) + ", login: " + login);
95     }
96
97 return true;
98 }