]> git.stg.codes - stg.git/blobdiff - projects/sgauthstress/user.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / projects / sgauthstress / user.cpp
index b36da5e1f1b3928603767088d6bc359d88ee25de..944907fe8abf74221dd12e2b3acd076a71e0abf5 100644 (file)
@@ -1,5 +1,7 @@
+#include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
+#include <unistd.h> // close
 
 #include <cstring>
 #include <cerrno>
@@ -15,11 +17,12 @@ USER::USER(const std::string & l,
     : login(l),
       password(pwd),
       ip(i),
-      aliveTimeout(0),
-      userTimeout(0),
+      aliveTimeout(5),
+      userTimeout(60),
       phase(1),
       phaseChangeTime(0),
-      rnd(0)
+      rnd(0),
+      sock(-1)
 {
 unsigned char key[IA_PASSWD_LEN];
 memset(key, 0, IA_PASSWD_LEN);
@@ -35,7 +38,8 @@ USER::USER(const USER & rvalue)
       userTimeout(rvalue.userTimeout),
       phase(1),
       phaseChangeTime(0),
-      rnd(0)
+      rnd(0),
+      sock(-1)
 {
 unsigned char key[IA_PASSWD_LEN];
 memset(key, 0, IA_PASSWD_LEN);
@@ -45,10 +49,11 @@ Blowfish_Init(&ctx, key, IA_PASSWD_LEN);
 
 USER::~USER()
 {
-close(sock);
+if (sock > 0)
+    close(sock);
 }
 
-const USER & USER::operator=(const USER & rvalue)
+USER & USER::operator=(const USER & rvalue)
 {
 login = rvalue.login;
 password = rvalue.password;
@@ -58,6 +63,7 @@ userTimeout = rvalue.userTimeout;
 phase = 1;
 phaseChangeTime = 0;
 rnd = 0;
+sock = -1;
 
 unsigned char key[IA_PASSWD_LEN];
 memset(key, 0, IA_PASSWD_LEN);
@@ -71,6 +77,11 @@ bool USER::InitNetwork()
 {
 sock = socket(AF_INET, SOCK_DGRAM, 0);
 
+if (sock < 0)
+    {
+    throw std::runtime_error(std::string("USER::USER() - socket creation error: '") + strerror(errno) + "', ip: " + inet_ntostring(ip) + ", login: " + login);
+    }
+
 struct sockaddr_in addr;
 
 addr.sin_family = AF_INET;
@@ -82,4 +93,6 @@ if (res == -1)
     {
     throw std::runtime_error(std::string("USER::USER() - bind error: '") + strerror(errno) + "', ip: " + inet_ntostring(ip) + ", login: " + login);
     }
+
+return true;
 }