X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..ce4df22d851ab9ba93374415363809aba1f43b59:/projects/rlm_stg/stg_client.cpp?ds=sidebyside diff --git a/projects/rlm_stg/stg_client.cpp b/projects/rlm_stg/stg_client.cpp index 7be0a55e..834f19c4 100644 --- a/projects/rlm_stg/stg_client.cpp +++ b/projects/rlm_stg/stg_client.cpp @@ -29,115 +29,77 @@ #include #include #include // close + +#include #include +#include + #include "stg_client.h" using namespace std; +void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password); +void Encrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8); +void Decrypt(BLOWFISH_CTX * ctx, char * dst, const char * src, int len8); + //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -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) + : localPort(lp), + password(pass), + framedIP(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); } //----------------------------------------------------------------------------- -int STG_CLIENT::Start() +uint32_t STG_CLIENT::GetFramedIP() const { -InitEncrypt(); - -return PrepareNet(); +return framedIP; } //----------------------------------------------------------------------------- -int STG_CLIENT::Stop() +int STG_CLIENT::PrepareNet() { -FinalizeNet(); +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; + } + } return 0; } //----------------------------------------------------------------------------- @@ -150,9 +112,9 @@ int STG_CLIENT::Send(const RAD_PACKET & packet) { char buf[RAD_MAX_PACKET_LEN]; -Encrypt(buf, (char *)&packet, sizeof(RAD_PACKET) / 8); +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"; @@ -165,16 +127,17 @@ int STG_CLIENT::RecvData(RAD_PACKET * packet) 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(&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; } @@ -298,23 +261,34 @@ if (packet.packetType != RAD_ACCEPT_PACKET) return 0; } //----------------------------------------------------------------------------- -void STG_CLIENT::Encrypt(char * dst, const char * src, int len8) +inline +void Encrypt(BLOWFISH_CTX * ctx, 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_Encrypt(&ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); + Blowfish_Encrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); } -//----------------------------------------------------------------------------- -void STG_CLIENT::Decrypt(char * dst, const char * src, int len8) +//----------------------------------------------------------------------------- +inline +void Decrypt(BLOWFISH_CTX * ctx, 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)); + Blowfish_Decrypt(ctx, (uint32_t *)(dst + i*8), (uint32_t *)(dst + i*8 + 4)); +} +//----------------------------------------------------------------------------- +inline +void InitEncrypt(BLOWFISH_CTX * ctx, const std::string & password) +{ +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); } //-----------------------------------------------------------------------------