X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4ab6759cf046b076e454cbf01bc4b299e4e63794..afb00c045e0ea58e39c0d7a7921bedbf26d1860d:/projects/sgconf_xml/main.cpp diff --git a/projects/sgconf_xml/main.cpp b/projects/sgconf_xml/main.cpp new file mode 100644 index 00000000..751a7537 --- /dev/null +++ b/projects/sgconf_xml/main.cpp @@ -0,0 +1,326 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Boris Mikhailenko + */ + +/* +$Revision: 1.2 $ +$Author: nobunaga $ +$Date: 2008/01/05 12:11:34 $ +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "request.h" +#include "common.h" +#include "netunit.h" + +#define FN_LEN (512) +#define REQ_STR_LEN (300) +char fileName[FN_LEN]; +char strReq[2048]; + +//int ParseReply(void * data, SLIST * ans); +int ParseReply(void * data, list * ans); + +struct option long_options[] = { +{"server", 1, 0, 's'}, //Server +{"port", 1, 0, 'p'}, //Port +{"admin", 1, 0, 'a'}, //Admin +{"admin_pass", 1, 0, 'w'}, //passWord +{"file", 1, 0, 'f'}, //File +{"strreq", 1, 0, 'r'}, //String request +{0, 0, 0, 0}}; + +//----------------------------------------------------------------------------- +int CheckLogin(const char * login) +{ +for (int i = 0; i < (int)strlen(login); i++) + { + if (!(( login[i] >= 'a' && login[i] <= 'z') + || (login[i] >= 'A' && login[i] <= 'Z') + || (login[i] >= '0' && login[i] <= '9') + || login[i] == '_' + || login[i] == '-')) + { + return 1; + } + } +return 0; +} +//----------------------------------------------------------------------------- +short int ParseServerPort(const char * p) +{ +int port; +if (str2x(p, port) != 0) + { + printf("Incorresct server port %s\n", p); + exit(NETWORK_ERR_CODE); + } +return(short)port; +} +//----------------------------------------------------------------------------- +char * ParseAdminLogin(char * adm) +{ +if (CheckLogin(adm)) + { + printf("Incorrect admin login %s\n", adm); + exit(PARAMETER_PARSING_ERR_CODE); + } +return adm; +} +//----------------------------------------------------------------------------- +char * ParsePassword(char * pass) +{ +if (strlen(pass) >= ADM_PASSWD_LEN) + { + printf("Password too big %s\n", pass); + exit(PARAMETER_PARSING_ERR_CODE); + } + +return pass; +} +//----------------------------------------------------------------------------- +void CreateRequest(REQUEST * req, char * r) +{ +char str[10024]; +r[0] = 0; + +if (!req->strReq.res_empty()) + { + sprintf(str, "%s", req->strReq.const_data().c_str()); + strcat(r, str); + return; + } else + { + FILE *f; + f = NULL; + f = fopen(fileName, "rt"); + if (!f) + { + printf("Can't open request file\n"); + exit(PARAMETER_PARSING_ERR_CODE); + } + + char ts[REQ_STR_LEN]; + while (fgets(ts, REQ_STR_LEN, f)) + { + strncat(r, ts, REQ_STR_LEN); + } + fclose(f); + } +} +//----------------------------------------------------------------------------- +int Process(REQUEST * r) +{ +char errorMsg[MAX_ERR_STR_LEN]; +int ret; +char str[2048]; + +NETTRANSACT nt; +nt.SetServer(r->server.const_data().c_str()); +nt.SetServerPort(r->port); +nt.SetLogin(r->admLogin.const_data().c_str()); +nt.SetPassword(r->admPasswd.const_data().c_str()); +nt.SetRxCallback(NULL, ParseReply); + +CreateRequest(r, str); + +if ((ret = nt.Connect()) != st_ok) + { + strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + printf("%s", errorMsg); + return ret; + } +if ((ret = nt.Transact(str)) != st_ok) + { + strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + printf("%s", errorMsg); + return ret; + } +if ((ret = nt.Disconnect()) != st_ok) + { + strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN); + printf("%s", errorMsg); + return ret; + } + +printf("\n"); +return 0; +} +//----------------------------------------------------------------------------- +int CheckParameters(REQUEST * req) +{ +int a = !req->admLogin.res_empty() + && !req->admPasswd.res_empty() + && !req->server.res_empty() + && !req->port.res_empty(); + +int b = !req->fileReq.res_empty() + || !req->strReq.res_empty(); + +return a && b; +} +//----------------------------------------------------------------------------- +void Usage() +{ +printf("Sgconf version: 1.05.9_XML\n\n"); + +printf("Use: sgconf -s -p -a -w -r \n"); +printf("Use: sgconf -s -p -a -w -f \n\n"); + +printf("Request file or string content:\n\n"); + +printf(" \n\n"); + +printf(" \n"); +printf(" \n"); +printf(" \n\n"); + +printf(" \n"); +printf(" Day-Night time for each DIR\n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" New TraffType value: [up|down|up+down|max]\n"); +printf(" \n\n"); + +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n\n"); + +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" Checking login and password in database. Return Ok or Err.\n\n"); + +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" delayed - change tariff from 1st day of new month; now - change tariff NOW.\n"); +printf(" Encode12() -> value\n"); +printf(" Encode12() -> value\n"); +printf("
Encode12() -> value\n"); +printf(" Encode12() -> value\n"); +printf(" Encode12() -> value\n"); +printf(" Encode12() -> value\n"); +printf(" Encode12() -> value\n"); +printf(" add - add money on account; set - set money on account; Message - message for log\n"); +printf(" \n"); +printf(" \n"); +printf(" \n"); +printf(" 1 - turn ON AlwaysOnline; 0 - turn OFF AlwaysOnline\n"); +printf(" 1 - turn ON Down; 0 - turn OFF Down\n"); +printf(" 1 - turn ON Passive; 0 - turn OFF Passive\n"); +printf(" MU[0...9] - Set upload traffic value; MU[0...9] - Set download traffic value; \n"); +printf(" \n\n"); + +printf(" \n"); +} +//--------------------------------------------------------------------------- +int main (int argc, char **argv) +{ +int c; +//int digit_optind = 0; +REQUEST req; + +while (1) + { + //int this_option_optind = optind ? optind : 1; + int option_index = -1; + + c = getopt_long(argc, argv, "s:p:a:w:f:r:", long_options, &option_index); + if (c == -1) + break; + + switch (c) + { + case 's': //server + req.server = optarg; + break; + + case 'p': //port + req.port = ParseServerPort(optarg); + //req.portReq = 1; + break; + + case 'a': //admin + req.admLogin = ParseAdminLogin(optarg); + break; + + case 'w': //admin password + req.admPasswd = ParsePassword(optarg); + break; + + case 'f': //file + strcpy(fileName,optarg); + req.fileReq = 1; + break; + + case 'r': //string request + req.strReq = optarg; + break; + + case '?': + //printf ("Unknown option \n"); + break; + + default: + printf ("?? getopt returned character code 0%o ??\n", c); + } + } + +if (optind < argc) + { + printf ("non-option ARGV-elements: "); + while (optind < argc) + printf ("%s ", argv[optind++]); + printf ("\n"); + exit(PARAMETER_PARSING_ERR_CODE); + } + +if (CheckParameters(&req) == 0) + { + //printf("Parameter needed\n"); + Usage(); + exit(PARAMETER_PARSING_ERR_CODE); + } + +Process(&req); + +return 0; +} +//----------------------------------------------------------------------------- +