]> git.stg.codes - stg.git/blobdiff - projects/rlm_stg/stg_client.cpp
Fix trailing whitespaces.
[stg.git] / projects / rlm_stg / stg_client.cpp
index 7be0a55e159ebd1c53da2f9f03d3ff63816899ce..284c1e2bc6d22fff515a9a504165b9259b5d9572 100644 (file)
 #include <netdb.h>
 #include <sys/types.h>
 #include <unistd.h> // close
+
+#include <cerrno>
 #include <cstring>
+#include <vector>
+#include <utility>
+
+#include <stdexcept>
 
 #include "stg_client.h"
 
-using namespace std;
+typedef std::vector<std::pair<std::string, std::string> > PAIRS;
 
 //-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-//-----------------------------------------------------------------------------
-STG_CLIENT::STG_CLIENT()
-    : port(0),
-      localPort(0),
-      sock(0)
-{
-}
-//-----------------------------------------------------------------------------
-STG_CLIENT::~STG_CLIENT()
-{
-}
-//-----------------------------------------------------------------------------
-void STG_CLIENT::SetServer(const string & host)
-{
-STG_CLIENT::host = host;
-}
-//-----------------------------------------------------------------------------
-void STG_CLIENT::SetPort(uint16_t port)
-{
-STG_CLIENT::port = port;
-}
-//-----------------------------------------------------------------------------
-void STG_CLIENT::SetLocalPort(uint16_t port)
-{
-STG_CLIENT::localPort = port;
-}
-//-----------------------------------------------------------------------------
-void STG_CLIENT::SetPassword(const string & password)
-{
-STG_CLIENT::password = password;
-}
-//-----------------------------------------------------------------------------
-uint32_t STG_CLIENT::GetFramedIP() const
-{
-return framedIP;
-}
-//-----------------------------------------------------------------------------
-void STG_CLIENT::InitEncrypt()
-{
-unsigned char keyL[RAD_PASSWORD_LEN];
-memset(keyL, 0, RAD_PASSWORD_LEN);
-strncpy((char *)keyL, password.c_str(), RAD_PASSWORD_LEN);
-Blowfish_Init(&ctx, keyL, RAD_PASSWORD_LEN);
-}
-//-----------------------------------------------------------------------------
-int STG_CLIENT::PrepareNet()
+
+STG_CLIENT::STG_CLIENT(const std::string & host, uint16_t port, uint16_t lp, const std::string & pass)
+    : password(pass),
+      framedIP(0)
 {
-sock = socket(AF_INET, SOCK_DGRAM, 0);
+/*sock = socket(AF_INET, SOCK_DGRAM, 0);
 if (sock == -1)
     {
-    errorStr = "Socket create error";
-    return -1;
+    std::string message = strerror(errno);
+    message = "Socket create error: '" + message + "'";
+    throw std::runtime_error(message);
     }
 
 struct hostent * he = NULL;
 he = gethostbyname(host.c_str());
 if (he == NULL)
     {
-    errorStr = "gethostbyname error";
-    return -1;
-    }
-
-if (localPort != 0)
-    {
-    struct sockaddr_in localAddr;
-    localAddr.sin_family = AF_INET;
-    localAddr.sin_port = htons(localPort);
-    localAddr.sin_addr.s_addr = inet_addr("0.0.0.0");;
-
-    if (bind(sock, (struct sockaddr *)&localAddr, sizeof(localAddr)))
-        {
-        errorStr = "Bind failed";
-        return -1;
-        }
+    throw std::runtime_error("gethostbyname error");
     }
 
 outerAddr.sin_family = AF_INET;
 outerAddr.sin_port = htons(port);
 outerAddr.sin_addr.s_addr = *(uint32_t *)he->h_addr;
 
-outerAddrLen = sizeof(struct sockaddr_in);
+InitEncrypt(&ctx, password);
 
-return 0;
+PrepareNet();*/
 }
-//-----------------------------------------------------------------------------
-void STG_CLIENT::FinalizeNet()
+
+STG_CLIENT::~STG_CLIENT()
 {
-close(sock);
+/*close(sock);*/
 }
-//-----------------------------------------------------------------------------
-int STG_CLIENT::Start()
-{
-InitEncrypt();
 
-return PrepareNet();
-}
-//-----------------------------------------------------------------------------
-int STG_CLIENT::Stop()
+int STG_CLIENT::PrepareNet()
 {
-FinalizeNet();
-
 return 0;
 }
-//-----------------------------------------------------------------------------
-string STG_CLIENT::GetUserPassword() const
-{
-return userPassword;
-}
-//-----------------------------------------------------------------------------
+
 int STG_CLIENT::Send(const RAD_PACKET & packet)
 {
-char buf[RAD_MAX_PACKET_LEN];
-    
-Encrypt(buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
+/*char buf[RAD_MAX_PACKET_LEN];
+
+Encrypt(&ctx, buf, (char *)&packet, sizeof(RAD_PACKET) / 8);
 
-int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, outerAddrLen);
+int res = sendto(sock, buf, sizeof(RAD_PACKET), 0, (struct sockaddr *)&outerAddr, sizeof(outerAddr));
 
 if (res == -1)
     errorStr = "Error sending data";
 
-return res;
+return res;*/
 }
-//-----------------------------------------------------------------------------
+
 int STG_CLIENT::RecvData(RAD_PACKET * packet)
 {
-char buf[RAD_MAX_PACKET_LEN];
+/*char buf[RAD_MAX_PACKET_LEN];
 int res;
 
-outerAddrLen = sizeof(struct sockaddr_in);
+struct sockaddr_in addr;
+socklen_t len = sizeof(struct sockaddr_in);
 
-res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, (struct sockaddr *)&outerAddr, &outerAddrLen);
+res = recvfrom(sock, buf, RAD_MAX_PACKET_LEN, 0, reinterpret_cast<struct sockaddr *>(&addr), &len);
 if (res == -1)
     {
     errorStr = "Error receiving data";
     return -1;
     }
 
-Decrypt((char *)packet, buf, res / 8);
+Decrypt(&ctx, (char *)packet, buf, res / 8);
 
-return 0;
+return 0;*/
 }
-//-----------------------------------------------------------------------------
+
 int STG_CLIENT::Request(RAD_PACKET * packet, const std::string & login, const std::string & svc, uint8_t packetType)
 {
-int res;
+/*int res;
 
 memcpy((void *)&packet->magic, (void *)RAD_ID, RAD_MAGIC_LEN);
 packet->protoVer[0] = '0';
@@ -209,12 +145,14 @@ if (strncmp((char *)packet->magic, RAD_ID, RAD_MAGIC_LEN))
     return -1;
     }
 
-return 0;
+return 0;*/
 }
+
 //-----------------------------------------------------------------------------
-int STG_CLIENT::Authorize(const string & login, const string & svc)
+
+const STG_PAIRS * STG_CLIENT::Authorize(const std::string & login, const std::string & svc)
 {
-RAD_PACKET packet;
+/*RAD_PACKET packet;
 
 userPassword = "";
 
@@ -224,14 +162,17 @@ if (Request(&packet, login, svc, RAD_AUTZ_PACKET))
 if (packet.packetType != RAD_ACCEPT_PACKET)
     return -1;
 
-userPassword = (char *)packet.password;
+userPassword = (char *)packet.password;*/
 
-return 0;
+PAIRS pairs;
+pairs.push_back(std::make_pair("Cleartext-Password", userPassword));
+
+return ToSTGPairs(pairs);
 }
-//-----------------------------------------------------------------------------
-int STG_CLIENT::Authenticate(const string & login, const string & svc)
+
+const STG_PAIRS * STG_CLIENT::Authenticate(const std::string & login, const std::string & svc)
 {
-RAD_PACKET packet;
+/*RAD_PACKET packet;
 
 userPassword = "";
 
@@ -239,14 +180,16 @@ if (Request(&packet, login, svc, RAD_AUTH_PACKET))
     return -1;
 
 if (packet.packetType != RAD_ACCEPT_PACKET)
-    return -1;
+    return -1;*/
 
-return 0;
+PAIRS pairs;
+
+return ToSTGPairs(pairs);
 }
-//-----------------------------------------------------------------------------
-int STG_CLIENT::PostAuthenticate(const string & login, const string & svc)
+
+const STG_PAIRS * STG_CLIENT::PostAuth(const std::string & login, const std::string & svc)
 {
-RAD_PACKET packet;
+/*RAD_PACKET packet;
 
 userPassword = "";
 
@@ -259,14 +202,24 @@ if (packet.packetType != RAD_ACCEPT_PACKET)
 if (svc == "Framed-User")
     framedIP = packet.ip;
 else
-    framedIP = 0;
+    framedIP = 0;*/
 
-return 0;
+PAIRS pairs;
+pairs.push_back(std::make_pair("Framed-IP-Address", inet_ntostring(framedIP)));
+
+return ToSTGPairs(pairs);
 }
-//-----------------------------------------------------------------------------
-int STG_CLIENT::Account(const std::string & type, const string & login, const string & svc, const string & sessid)
+
+const STG_PAIRS * STG_CLIENT::PreAcct(const std::string & login, const std::String & service)
+{
+PAIRS pairs;
+
+return ToSTGPairs(pairs);
+}
+
+const STG_PAIRS * STG_CLIENT::Account(const std::string & type, const std::string & login, const std::string & svc, const std::string & sessid)
 {
-RAD_PACKET packet;
+/*RAD_PACKET packet;
 
 userPassword = "";
 strncpy((char *)packet.sessid, sessid.c_str(), RAD_SESSID_LEN);
@@ -293,28 +246,50 @@ else
     }
 
 if (packet.packetType != RAD_ACCEPT_PACKET)
-    return -1;
+    return -1;*/
 
-return 0;
+PAIRS pairs;
+
+return ToSTGPairs(pairs);
 }
+
 //-----------------------------------------------------------------------------
-void STG_CLIENT::Encrypt(char * dst, const char * src, int len8)         
+
+std::string STG_CLIENT_ST::m_host;
+uint16_t STG_CLIENT_ST::m_port(6666);
+std::string STG_CLIENT_ST::m_password;
+
+//-----------------------------------------------------------------------------
+
+STG_CLIENT * STG_CLIENT_ST::Get()
 {
-// len8 - длина в 8-ми байтовых блоках                                                   
-if (dst != src) 
-    memcpy(dst, src, len8 * 8);                                                          
-    
-for (int i = 0; i < len8; i++)
-    Blowfish_Encrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
+    static STG_CLIENT * stgClient = NULL;
+    if ( stgClient == NULL )
+        stgClient = new STG_CLIENT(m_host, m_port, m_password);
+    return stgClient;
 }
-//-----------------------------------------------------------------------------          
-void STG_CLIENT::Decrypt(char * dst, const char * src, int len8)
-{
-// len8 - длина в 8-ми байтовых блоках
-if (dst != src)
-    memcpy(dst, src, len8 * 8);
 
-for (int i = 0; i < len8; i++)
-    Blowfish_Decrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4));
+void STG_CLIENT_ST::Configure(const std::string & host, uint16_t port, const std::string & password)
+{
+    m_host = host;
+    m_port = port;
+    m_password = password;
 }
+
 //-----------------------------------------------------------------------------
+
+const STG_PAIR * ToSTGPairs(const PAIRS & source)
+{
+    STG_PAIR * pairs = new STG_PAIR[source.size() + 1];
+    for (size_t pos = 0; pos < source.size(); ++pos) {
+        bzero(pairs[pos].key, sizeof(STG_PAIR::key));
+        bzero(pairs[pos].value, sizeof(STG_PAIR::value));
+        strncpy(pairs[pos].key, source[pos].first.c_str(), sizeof(STG_PAIR::key));
+        strncpy(pairs[pos].value, source[pos].second.c_str(), sizeof(STG_PAIR::value));
+        ++pos;
+    }
+    bzero(pairs[sources.size()].key, sizeof(STG_PAIR::key));
+    bzero(pairs[sources.size()].value, sizeof(STG_PAIR::value));
+
+    return pairs;
+}