2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
24 $Date: 2008/01/05 12:11:34 $
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
41 #define REQ_STR_LEN (300)
42 char fileName[FN_LEN];
45 //int ParseReply(void * data, SLIST * ans);
46 int ParseReply(void * data, list<string> * ans);
48 struct option long_options[] = {
49 {"server", 1, 0, 's'}, //Server
50 {"port", 1, 0, 'p'}, //Port
51 {"admin", 1, 0, 'a'}, //Admin
52 {"admin_pass", 1, 0, 'w'}, //passWord
53 {"file", 1, 0, 'f'}, //File
54 {"strreq", 1, 0, 'r'}, //String request
57 //-----------------------------------------------------------------------------
58 int CheckLogin(const char * login)
60 for (int i = 0; i < (int)strlen(login); i++)
62 if (!(( login[i] >= 'a' && login[i] <= 'z')
63 || (login[i] >= 'A' && login[i] <= 'Z')
64 || (login[i] >= '0' && login[i] <= '9')
73 //-----------------------------------------------------------------------------
74 short int ParseServerPort(const char * p)
77 if (str2x(p, port) != 0)
79 printf("Incorresct server port %s\n", p);
80 exit(NETWORK_ERR_CODE);
84 //-----------------------------------------------------------------------------
85 char * ParseAdminLogin(char * adm)
89 printf("Incorrect admin login %s\n", adm);
90 exit(PARAMETER_PARSING_ERR_CODE);
94 //-----------------------------------------------------------------------------
95 char * ParsePassword(char * pass)
97 if (strlen(pass) >= ADM_PASSWD_LEN)
99 printf("Password too big %s\n", pass);
100 exit(PARAMETER_PARSING_ERR_CODE);
105 //-----------------------------------------------------------------------------
106 void CreateRequest(REQUEST * req, char * r)
111 if (!req->strReq.res_empty())
113 sprintf(str, "%s", req->strReq.const_data().c_str());
120 f = fopen(fileName, "rt");
123 printf("Can't open request file\n");
124 exit(PARAMETER_PARSING_ERR_CODE);
127 char ts[REQ_STR_LEN];
128 while (fgets(ts, REQ_STR_LEN, f))
130 strncat(r, ts, REQ_STR_LEN);
135 //-----------------------------------------------------------------------------
136 int Process(REQUEST * r)
138 char errorMsg[MAX_ERR_STR_LEN];
143 nt.SetServer(r->server.const_data().c_str());
144 nt.SetServerPort(r->port);
145 nt.SetLogin(r->admLogin.const_data().c_str());
146 nt.SetPassword(r->admPasswd.const_data().c_str());
147 nt.SetRxCallback(NULL, ParseReply);
149 CreateRequest(r, str);
151 if ((ret = nt.Connect()) != st_ok)
153 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
154 printf("%s", errorMsg);
157 if ((ret = nt.Transact(str)) != st_ok)
159 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
160 printf("%s", errorMsg);
163 if ((ret = nt.Disconnect()) != st_ok)
165 strncpy(errorMsg, nt.GetError(), MAX_ERR_STR_LEN);
166 printf("%s", errorMsg);
170 printf("<!-- Ok -->\n");
173 //-----------------------------------------------------------------------------
174 int CheckParameters(REQUEST * req)
176 int a = !req->admLogin.res_empty()
177 && !req->admPasswd.res_empty()
178 && !req->server.res_empty()
179 && !req->port.res_empty();
181 int b = !req->fileReq.res_empty()
182 || !req->strReq.res_empty();
186 //-----------------------------------------------------------------------------
189 printf("Sgconf version: 1.05.9_XML\n\n");
191 printf("Use: sgconf -s <server> -p <port> -a <admin> -w <admin_pass> -r <request_string>\n");
192 printf("Use: sgconf -s <server> -p <port> -a <admin> -w <admin_pass> -f <request_file>\n\n");
194 printf("Request file or string content:\n\n");
196 printf(" <GetServerInfo/>\n\n");
198 printf(" <GetTariffs/>\n");
199 printf(" <AddTariff name=\"NEW_TARIFF\"/>\n");
200 printf(" <DelTariff name=\"DELETED_TARIFF\"/>\n\n");
202 printf(" <SetTariff name=\"TARIFF\"/>\n");
203 printf(" <Time[0...9] value=\"HH:MM-HH:MM\"/> Day-Night time for each DIR\n");
204 printf(" <PriceDayA value=\"PriceDayA0/PriceDayA1/PriceDayA2/PriceDayA3/PriceDayA4/PriceDayA5/PriceDayA6/PriceDayA7/PriceDayA8/PriceDayA9\"/>\n");
205 printf(" <PriceDayB value=\"PriceDayB0/PriceDayB1/PriceDayB2/PriceDayB3/PriceDayB4/PriceDayB5/PriceDayB6/PriceDayB7/PriceDayB8/PriceDayB9\"/>\n");
206 printf(" <PriceNightA value=\"PriceNightA0/PriceNightA1/PriceNightA2/PriceNightA3/PriceNightA4/PriceNightA5/PriceNightA6/PriceNightA7/PriceNightA8/PriceNightA9\"/>\n");
207 printf(" <PriceNightB value=\"PriceNightB0/PriceNightB1/PriceNightB2/PriceNightB3/PriceNightB4/PriceNightB5/PriceNightB6/PriceNightB7/PriceNightB8/PriceNightB9\"/>\n");
208 printf(" <SinglePrice value=\"SinglePrice0/SinglePrice1/SinglePrice2/SinglePrice3/SinglePrice4/SinglePrice5/SinglePrice6/SinglePrice7/SinglePrice8/SinglePrice9\"/>\n");
209 printf(" <NoDiscount value=\"NoDiscount0/NoDiscount1/NoDiscount2/NoDiscount3/NoDiscount4/NoDiscount5/NoDiscount6/NoDiscount7/NoDiscount8/NoDiscount9\"/>\n");
210 printf(" <Threshold value=\"NEW_Threshold\"/>\n");
211 printf(" <Fee value=\"NEW_Fee\"/>\n");
212 printf(" <PassiveCost value=\"NEW_PassiveCost\"/>\n");
213 printf(" <Free value=\"NEW_Free\"/>\n");
214 printf(" <TraffType value=\"NEW_TraffType\"/> New TraffType value: [up|down|up+down|max]\n");
215 printf(" </SetTariff/>\n\n");
217 printf(" <GetAdmins/>\n");
218 printf(" <AddAdmin login=\"LOGIN\"/>\n");
219 printf(" <DelAdmin login=\"LOGIN\"/>\n");
220 printf(" <ChgAdmin login=\"LOGIN\" priv=\"NEW_PRIV\" password=\"NEW_PASSWORD\"/>\n\n");
222 printf(" <GetUsers/>\n");
223 printf(" <GetUser login=\"LOGIN\"/>\n");
224 printf(" <AddUser login=\"LOGIN\"/>\n");
225 printf(" <DelUser login=\"LOGIN\"/>\n");
226 printf(" <CheckUser login=\"LOGIN\" password=\"PASSWORD\"/> Checking login and password in database. Return Ok or Err.\n\n");
228 printf(" <SetUser>\n");
229 printf(" <login value=\"LOGIN\" />\n");
230 printf(" <ip value=\"NEW_IP\" />\n");
231 printf(" <password value=\"NEW_Password\" />\n");
232 printf(" <tariff [ delayed | now ]=\"NEW_Tariff\" /> delayed - change tariff from 1st day of new month; now - change tariff NOW.\n");
233 printf(" <group value=\"NEW_Group\" /> Encode12() -> value\n");
234 printf(" <name value=\"NEW_RealName\" /> Encode12() -> value\n");
235 printf(" <address value=\"NEW_Address\" /> Encode12() -> value\n");
236 printf(" <phone value=\"NEW_Phone\" /> Encode12() -> value\n");
237 printf(" <email value=\"NEW_Email\" /> Encode12() -> value\n");
238 printf(" <note value=\"NEW_Note\" /> Encode12() -> value\n");
239 printf(" <userdata[0...9] value=\"NEW_Userdata[0...9]\" /> Encode12() -> value\n");
240 printf(" <cash [ add | set ]=\"Cash\" msg=\"MESSAGE\" /> add - add money on account; set - set money on account; Message - message for log\n");
241 printf(" <credit value=\"NEW_Credit\" />\n");
242 printf(" <CreditExpire value=\"NEW_CreditExpire\" />\n");
243 printf(" <freemb value=\"NEW_FreeMB\" />\n");
244 printf(" <aonline value=\"AlwaysOnline\" /> 1 - turn ON AlwaysOnline; 0 - turn OFF AlwaysOnline\n");
245 printf(" <down value=\"Down\" /> 1 - turn ON Down; 0 - turn OFF Down\n");
246 printf(" <passive value=\"Passive\" /> 1 - turn ON Passive; 0 - turn OFF Passive\n");
247 printf(" <traff MU[0...9]=\"NEW_MU[0...9]\" MD[0...9]=\"NEW_MD[0...9]\" /> MU[0...9] - Set upload traffic value; MU[0...9] - Set download traffic value; \n");
248 printf(" </SetUser>\n\n");
250 printf(" <Message login=\"LOGIN\" msgver=\"1\" msgtype=\"1\" repeat=\"0\" repeatperiod=\"0\" showtime=\"0\" text=\"MESSAGE\" />\n");
252 //---------------------------------------------------------------------------
253 int main (int argc, char **argv)
256 //int digit_optind = 0;
261 //int this_option_optind = optind ? optind : 1;
262 int option_index = -1;
264 c = getopt_long(argc, argv, "s:p:a:w:f:r:", long_options, &option_index);
275 req.port = ParseServerPort(optarg);
280 req.admLogin = ParseAdminLogin(optarg);
283 case 'w': //admin password
284 req.admPasswd = ParsePassword(optarg);
288 strcpy(fileName,optarg);
292 case 'r': //string request
297 //printf ("Unknown option \n");
301 printf ("?? getopt returned character code 0%o ??\n", c);
307 printf ("non-option ARGV-elements: ");
308 while (optind < argc)
309 printf ("%s ", argv[optind++]);
311 exit(PARAMETER_PARSING_ERR_CODE);
314 if (CheckParameters(&req) == 0)
316 //printf("Parameter needed\n");
318 exit(PARAMETER_PARSING_ERR_CODE);
325 //-----------------------------------------------------------------------------