Добавлено: сохранение списка серверов

This commit is contained in:
Alexander Zhirov 2022-07-15 17:16:58 +03:00
parent a36ac928a0
commit f69847de79
7 changed files with 286 additions and 16 deletions

28
address.c Normal file
View File

@ -0,0 +1,28 @@
/*
* address.c
*
* Created on: 15 июл. 2022 г.
* Author: alexander
*/
#include "address.h"
#include <netdb.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
char *getHostIP(char *dnsName)
{
char *result = NULL;
struct hostent *he = gethostbyname(dnsName);
if (he)
{
char *ip = inet_ntoa(*(struct in_addr*)he->h_addr);
result = (char *)malloc(sizeof(char) * strlen(ip));
strcpy(result, ip);
}
return result;
}

13
address.h Normal file
View File

@ -0,0 +1,13 @@
/*
* address.h
*
* Created on: 15 июл. 2022 г.
* Author: alexander
*/
#ifndef ADDRESS_H_
#define ADDRESS_H_
char *getHostIP(char *dnsName);
#endif /* ADDRESS_H_ */

View File

@ -15,6 +15,7 @@
void settingsLoad() void settingsLoad()
{ {
dbGetHostsList();
if (!dbLoadData()) if (!dbLoadData())
{ {
addParameterKey(PARAMETER_XFREERDP, "xfreerdp", true, true, NULL, NULL); addParameterKey(PARAMETER_XFREERDP, "xfreerdp", true, true, NULL, NULL);

190
db.c
View File

@ -63,6 +63,8 @@ static int dbLoad(void *NotUsed, int argc, char **argv, char **azColName)
addParameterValue(atoi(parameters[0]), atoi(parameters[4]), parameters[5], atoi(parameters[6])); addParameterValue(atoi(parameters[0]), atoi(parameters[4]), parameters[5], atoi(parameters[6]));
} }
free(parameters);
return 0; return 0;
} }
@ -96,6 +98,75 @@ bool dbLoadData()
return true; return true;
} }
static int dbCreateHostsList(void *answer, int argc, char **argv, char **azColName)
{
Hosts *hosts = *(Hosts **)answer;
Host *host = (Host*) malloc(sizeof(Host));
host->data = (char **) malloc(sizeof(char*) * argc);
host->size = argc;
for (int i = 0; i < argc; i++)
{
host->data[i] = (char *) malloc(sizeof(char) * strlen(argv[i]));
strcpy(host->data[i], argv[i]);
}
Host **tmp = hosts->host;
hosts->host = (Host**) malloc(sizeof(Host*) * ++hosts->size);
for (size_t i = 0; i < hosts->size - 1; ++i)
{
hosts->host[i] = tmp[i];
}
if (tmp)
{
free(tmp);
}
hosts->host[hosts->size - 1] = host;
return 0;
}
Hosts *dbGetHostsList()
{
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return false;
}
char *err_msg = 0;
char *sql = "SELECT ROW_NUMBER () OVER (ORDER BY `hosts`.`ip`) `item`, `hosts`.`dns` as `dns`, `hosts`.`set` as `set` FROM `hosts` as `hosts`";
Hosts *hosts = (Hosts *) malloc(sizeof(Hosts));
hosts->size = 0;
hosts->host = NULL;
if (sqlite3_exec(db, sql, dbCreateHostsList, &hosts, &err_msg) != SQLITE_OK)
{
fprintf(stderr, "Ошибка выполнения запроса: %s\n", err_msg);
sqlite3_free(err_msg);
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return hosts;
}
void dbFreeHosts(Hosts *hosts)
{
for (size_t i = 0; i < hosts->size; ++i)
{
for (size_t j = 0; j < hosts->host[i]->size; ++j)
{
free(hosts->host[i]->data[j]);
}
free(hosts->host[i]);
}
free(hosts);
}
bool dbWriteParameter(Parameter name, bool set) bool dbWriteParameter(Parameter name, bool set)
{ {
sqlite3 *db = dbGetBase("freerdp.db"); sqlite3 *db = dbGetBase("freerdp.db");
@ -169,7 +240,116 @@ bool dbWriteValue(Value name, bool set)
return true; return true;
} }
bool dbSetValueCurrent(Value name, char *current) int dbAddServer(char *ip, char *dns)
{
int result = 0;
if (!(ip && strlen(ip) && dns && strlen(dns)))
return -1;
sqlite3 *db = dbGetBase("freerdp.db");
if (!db)
{
return -2;
}
sqlite3_stmt *res;
char *sql = "SELECT * FROM `hosts` WHERE `ip` = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, ip, -1, 0);
}
else
{
sqlite3_finalize(res);
sqlite3_close(db);
return -3;
}
if (sqlite3_step(res) == SQLITE_ROW)
{
int id = atoi((const char *)sqlite3_column_text(res, 0));
sqlite3_finalize(res);
sql = "UPDATE `hosts` set `set` = 0";
if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK)
{
sql = "UPDATE `hosts` set `dns` = ?, `set` = 1 where id = ?";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, dns, -1, 0);
sqlite3_bind_int(res, 2, id);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 5;
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 4;
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 3;
}
}
else
{
result = -5;
}
}
else if(sqlite3_step(res) == SQLITE_DONE)
{
sqlite3_finalize(res);
sql = "UPDATE `hosts` set `set` = 0";
if (sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK)
{
sql = "INSERT INTO `hosts` (`ip`, `dns`, `set`) VALUES (?, ?, 1)";
if (sqlite3_prepare_v2(db, sql, -1, &res, 0) == SQLITE_OK)
{
sqlite3_bind_text(res, 1, ip, -1, 0);
sqlite3_bind_text(res, 2, dns, -1, 0);
}
if (sqlite3_step(res) == SQLITE_BUSY)
{
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 2;
}
if (sqlite3_step(res) == SQLITE_ERROR)
{
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 1;
}
if (sqlite3_step(res) == SQLITE_DONE)
{
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", dns ? "установлено" : "очищено", ip);
result = 0;
}
}
else
{
result = -6;
}
}
else
{
result = -4;
}
sqlite3_finalize(res);
sqlite3_close(db);
return result;
}
bool dbSetUserNameCurrent(char *current)
{ {
sqlite3 *db = dbGetBase("freerdp.db"); sqlite3 *db = dbGetBase("freerdp.db");
if (!db) if (!db)
@ -185,21 +365,21 @@ bool dbSetValueCurrent(Value name, char *current)
{ {
sqlite3_bind_text(res, 1, text, -1, 0); sqlite3_bind_text(res, 1, text, -1, 0);
sqlite3_bind_int(res, 2, text ? true : false); sqlite3_bind_int(res, 2, text ? true : false);
sqlite3_bind_int(res, 3, name); sqlite3_bind_int(res, 3, VALUE_USERNAME);
} }
if (sqlite3_step(res) == SQLITE_BUSY) if (sqlite3_step(res) == SQLITE_BUSY)
{ {
fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(name)->key); fprintf(stderr, "[ЗАНЯТО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(VALUE_USERNAME)->key);
} }
if (sqlite3_step(res) == SQLITE_ERROR) if (sqlite3_step(res) == SQLITE_ERROR)
{ {
fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(name)->key); fprintf(stderr, "[ОШИБКА] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(VALUE_USERNAME)->key);
} }
if (sqlite3_step(res) == SQLITE_DONE) if (sqlite3_step(res) == SQLITE_DONE)
{ {
fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(name)->key); fprintf(stdout, "[УСПЕШНО] %s - \"%s\"\n", text ? "установлено" : "очищено", getParameter(VALUE_USERNAME)->key);
} }
sqlite3_finalize(res); sqlite3_finalize(res);

17
db.h
View File

@ -11,9 +11,24 @@
#include "parameter.h" #include "parameter.h"
#include "value.h" #include "value.h"
typedef struct Host
{
char **data;
size_t size;
} Host;
typedef struct Hosts
{
Host **host;
size_t size;
} Hosts;
bool dbLoadData(); bool dbLoadData();
Hosts *dbGetHostsList();
void dbFreeHosts(Hosts *hosts);
bool dbWriteParameter(Parameter name, bool set); bool dbWriteParameter(Parameter name, bool set);
bool dbWriteValue(Value name, bool set); bool dbWriteValue(Value name, bool set);
bool dbSetValueCurrent(Value name, char *current); bool dbSetUserNameCurrent(char *current);
int dbAddServer(char *ip, char *dns);
#endif /* DB_H_ */ #endif /* DB_H_ */

Binary file not shown.

53
gui.c
View File

@ -14,6 +14,7 @@
#include "node_settings.h" #include "node_settings.h"
#include "images.h" #include "images.h"
#include "arguments.h" #include "arguments.h"
#include "address.h"
#include "db.h" #include "db.h"
@ -24,10 +25,13 @@ static int guiExit(Ihandle *self)
static int guiConnect(Ihandle *self) static int guiConnect(Ihandle *self)
{ {
setParameterValue(PARAMETER_SERVER, VALUE_SERVER, IupGetAttribute(IupGetDialogChild(self, "SERVER"), "LINEVALUE")); char *result = getHostIP(IupGetAttribute(IupGetDialogChild(self, "SERVER"), "VALUE"));
dbSetValueCurrent(VALUE_SERVER, IupGetAttribute(IupGetDialogChild(self, "SERVER"), "LINEVALUE"));
if (!result)
return IUP_DEFAULT;
setParameterValue(PARAMETER_SERVER, VALUE_SERVER, result);
setParameterValue(PARAMETER_USERNAME, VALUE_USERNAME, IupGetAttribute(IupGetDialogChild(self, "USER"), "LINEVALUE")); setParameterValue(PARAMETER_USERNAME, VALUE_USERNAME, IupGetAttribute(IupGetDialogChild(self, "USER"), "LINEVALUE"));
dbSetValueCurrent(VALUE_USERNAME, IupGetAttribute(IupGetDialogChild(self, "USER"), "LINEVALUE"));
setParameterValue(PARAMETER_PASSWORD, VALUE_PASSWORD, IupGetAttribute(IupGetDialogChild(self, "PASSWORD"), "LINEVALUE")); setParameterValue(PARAMETER_PASSWORD, VALUE_PASSWORD, IupGetAttribute(IupGetDialogChild(self, "PASSWORD"), "LINEVALUE"));
Arguments args; Arguments args;
@ -39,9 +43,40 @@ static int guiConnect(Ihandle *self)
printf("\n"); printf("\n");
freeArguments(&args); freeArguments(&args);
if (!dbAddServer(result, IupGetAttribute(IupGetDialogChild(self, "SERVER"), "VALUE")))
{
IupSetAttribute(IupGetDialogChild(self, "SERVER"), "APPENDITEM", IupGetAttribute(IupGetDialogChild(self, "SERVER"), "VALUE"));
}
// if (free_rdp_connect(args.argc, args.argv) != XF_EXIT_DNS_NAME_NOT_FOUND)
// {
// dbSetValueCurrent(VALUE_SERVER, IupGetAttribute(IupGetDialogChild(self, "SERVER"), "LINEVALUE"));
dbSetUserNameCurrent(IupGetAttribute(IupGetDialogChild(self, "USER"), "LINEVALUE"));
// }
return IUP_DEFAULT; return IUP_DEFAULT;
} }
static void createHostsList(Ihandle *iupList)
{
Hosts *hosts = dbGetHostsList();
char *indexItem = NULL;
char *serverName = NULL;
for (size_t i = 0; i < hosts->size; ++i)
{
Host *host = hosts->host[i];
indexItem = (char *)malloc(sizeof(char) * strlen(host->data[0]));
strcpy(indexItem, host->data[0]);
serverName = (char *)malloc(sizeof(char) * strlen(host->data[1]));
strcpy(serverName, host->data[1]);
IupSetAttribute(iupList, indexItem, serverName);
if (atoi(host->data[2]))
IupSetAttribute(iupList, "VALUE", serverName);
}
dbFreeHosts(hosts);
}
Ihandle* guiStart() Ihandle* guiStart()
{ {
Ihandle *dlg; Ihandle *dlg;
@ -63,16 +98,16 @@ Ihandle* guiStart()
labelServer = IupLabel("Компьютер:"); labelServer = IupLabel("Компьютер:");
IupSetAttribute(labelServer, "SIZE", "80x16"); IupSetAttribute(labelServer, "SIZE", "80x16");
// IupSetAttribute(labelServer, "FONT", "Helvetica, 14");
IupSetAttribute(labelServer, "ALIGNMENT", "ARIGHT:ACENTER"); IupSetAttribute(labelServer, "ALIGNMENT", "ARIGHT:ACENTER");
IupSetAttribute(labelServer, "PADDING", "5"); IupSetAttribute(labelServer, "PADDING", "5");
inputServer = IupText(NULL);
inputServer = IupList(NULL);
IupSetAttribute(inputServer, "NAME", "SERVER"); IupSetAttribute(inputServer, "NAME", "SERVER");
IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера (обязательно)"); IupSetAttribute(inputServer, "TIP", "IP-адрес или имя удаленного сервера (обязательно)");
IupSetAttribute(inputServer, "VALUE", getSetValueCurrent(PARAMETER_SERVER)); createHostsList(inputServer);
// IupSetAttribute(inputServer, "BORDER", "NO");
IupSetAttribute(inputServer, "SIZE", "100"); IupSetAttribute(inputServer, "SIZE", "100");
// IupSetAttribute(inputServer, "PADDING", "10x"); IupSetAttribute(inputServer, "EDITBOX", "YES");
IupSetAttribute(inputServer, "DROPDOWN", "YES");
hBoxServer = IupHbox(labelServer, inputServer, NULL); hBoxServer = IupHbox(labelServer, inputServer, NULL);
IupSetAttribute(hBoxServer, "MARGIN", "10x10"); IupSetAttribute(hBoxServer, "MARGIN", "10x10");
@ -85,7 +120,6 @@ Ihandle* guiStart()
IupSetAttribute(inputUser, "TIP", "<домен>\\<имя пользователя> (обязательно)"); IupSetAttribute(inputUser, "TIP", "<домен>\\<имя пользователя> (обязательно)");
IupSetAttribute(inputUser, "VALUE", getSetValueCurrent(PARAMETER_USERNAME)); IupSetAttribute(inputUser, "VALUE", getSetValueCurrent(PARAMETER_USERNAME));
IupSetAttribute(inputUser, "SIZE", "100"); IupSetAttribute(inputUser, "SIZE", "100");
// IupSetAttribute(inputUser, "PADDING", "10x10");
hBoxUser = IupHbox(labelUser, inputUser, NULL); hBoxUser = IupHbox(labelUser, inputUser, NULL);
IupSetAttribute(hBoxUser, "MARGIN", "10x0"); IupSetAttribute(hBoxUser, "MARGIN", "10x0");
@ -95,7 +129,6 @@ Ihandle* guiStart()
IupSetAttribute(labelPassword, "PADDING", "5"); IupSetAttribute(labelPassword, "PADDING", "5");
inputPassword = IupText(NULL); inputPassword = IupText(NULL);
IupSetAttribute(inputPassword, "SIZE", "100"); IupSetAttribute(inputPassword, "SIZE", "100");
// IupSetAttribute(inputPassword, "PADDING", "10x10");
IupSetAttribute(inputPassword, "NAME", "PASSWORD"); IupSetAttribute(inputPassword, "NAME", "PASSWORD");
IupSetAttribute(inputPassword, "TIP", "Пароль пользователя (необязательно)"); IupSetAttribute(inputPassword, "TIP", "Пароль пользователя (необязательно)");
IupSetAttribute(inputPassword, "VALUE", getSetValueCurrent(PARAMETER_PASSWORD)); IupSetAttribute(inputPassword, "VALUE", getSetValueCurrent(PARAMETER_PASSWORD));