]> git.stg.codes - stg.git/blob - projects/sgauthstress/user.cpp
Check socket creation and socket 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       sock(-1)
24 {
25 unsigned char key[IA_PASSWD_LEN];
26 memset(key, 0, IA_PASSWD_LEN);
27 strncpy((char *)key, password.c_str(), IA_PASSWD_LEN);
28 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
29 }
30
31 USER::USER(const USER & rvalue)
32     : login(rvalue.login),
33       password(rvalue.password),
34       ip(rvalue.ip),
35       aliveTimeout(rvalue.aliveTimeout),
36       userTimeout(rvalue.userTimeout),
37       phase(1),
38       phaseChangeTime(0),
39       rnd(0),
40       sock(-1)
41 {
42 unsigned char key[IA_PASSWD_LEN];
43 memset(key, 0, IA_PASSWD_LEN);
44 strncpy((char *)key, password.c_str(), IA_PASSWD_LEN);
45 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
46 }
47
48 USER::~USER()
49 {
50 if (sock > 0)
51     close(sock);
52 }
53
54 const USER & USER::operator=(const USER & rvalue)
55 {
56 login = rvalue.login;
57 password = rvalue.password;
58 ip = rvalue.ip;
59 aliveTimeout = rvalue.aliveTimeout;
60 userTimeout = rvalue.userTimeout;
61 phase = 1;
62 phaseChangeTime = 0;
63 rnd = 0;
64 sock = -1;
65
66 unsigned char key[IA_PASSWD_LEN];
67 memset(key, 0, IA_PASSWD_LEN);
68 strncpy((char *)key, password.c_str(), IA_PASSWD_LEN);
69 Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
70
71 return *this;
72 }
73
74 bool USER::InitNetwork()
75 {
76 sock = socket(AF_INET, SOCK_DGRAM, 0);
77
78 if (sock < 0)
79     {
80     throw std::runtime_error(std::string("USER::USER() - socket creation error: '") + strerror(errno) + "', ip: " + inet_ntostring(ip) + ", login: " + login);
81     }
82
83 struct sockaddr_in addr;
84
85 addr.sin_family = AF_INET;
86 addr.sin_addr.s_addr = ip;
87 addr.sin_port = htons(5554); // :(
88
89 int res = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
90 if (res == -1)
91     {
92     throw std::runtime_error(std::string("USER::USER() - bind error: '") + strerror(errno) + "', ip: " + inet_ntostring(ip) + ", login: " + login);
93     }
94
95 return true;
96 }