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