kamcmd.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /*
  2. * Copyright (C) 2006 iptelorg GmbH
  3. *
  4. * This file is part of kamcmd, a free cli tool for Kamailio SIP server.
  5. *
  6. * kamailio is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the kamailio software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * kamailio is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. /*
  26. * send commands using binrpc
  27. *
  28. */
  29. #include <stdlib.h> /* exit, abort */
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #include <ctype.h> /* isprint */
  35. #include <sys/socket.h>
  36. #include <sys/un.h> /* unix sock*/
  37. #include <netinet/in.h> /* udp sock */
  38. #include <sys/uio.h> /* writev */
  39. #include <netdb.h> /* gethostbyname */
  40. #include <fcntl.h>
  41. #include <time.h> /* time */
  42. #include <sys/time.h>
  43. #ifdef USE_READLINE
  44. #include <readline/readline.h>
  45. #include <readline/history.h>
  46. #define USE_CFG_VARS /* cfg group and vars completion */
  47. #define USE_COUNTERS /* counters/statistics completion */
  48. #endif
  49. #include "parse_listen_id.h"
  50. #include "license.h"
  51. #include "../../src/modules/ctl/ctl_defaults.h" /* default socket & port */
  52. #include "../../src/modules/ctl/binrpc.h"
  53. #include "../../src/modules/ctl/binrpc.c" /* ugly hack */
  54. #ifndef NAME
  55. #define NAME "kamcmd"
  56. #endif
  57. #ifndef VERSION
  58. #define VERSION "1.5"
  59. #endif
  60. #define IOVEC_CNT 20
  61. #define MAX_LINE_SIZE 16 * 1024 /* for non readline mode */
  62. #define MAX_REPLY_SIZE 128 * 1024
  63. #define MAX_BODY_SIZE 128 * 1024
  64. #define MAX_BINRPC_ARGS 256
  65. #ifndef UNIX_PATH_MAX
  66. #define UNIX_PATH_MAX 104
  67. #endif
  68. static char version[] = NAME " " VERSION;
  69. #ifdef VERSION_NODATE
  70. static char compiled[] = "";
  71. #else
  72. #ifdef VERSION_DATE
  73. static char compiled[] = VERSION_DATE;
  74. #else
  75. static char compiled[] = __TIME__ " " __DATE__;
  76. #endif
  77. #endif
  78. static char help_msg[] = "\
  79. Usage: " NAME " [options][-s address] [ cmd ]\n\
  80. Options:\n\
  81. -s address unix socket name or host name to send the commands on\n\
  82. -R name force reply socket name, for the unix datagram socket mode\n\
  83. -D dir create the reply socket in the directory <dir> if no reply \n\
  84. socket is forced (-R) and a unix datagram socket is selected\n\
  85. as the transport\n\
  86. -f format print the result using format. Format is a string containing\n\
  87. %v at the places where values read from the reply should be\n\
  88. substituted. To print '%v', escape it using '%': %%v.\n\
  89. -v Verbose \n\
  90. -V Version number\n\
  91. -h This help message\n\
  92. address:\n\
  93. [proto:]name[:port] where proto is one of tcp, udp, unixs or unixd\n\
  94. e.g.: tcp:localhost:2049 , unixs:/tmp/kamailio_ctl\n\
  95. cmd:\n\
  96. method [arg1 [arg2...]]\n\
  97. arg:\n\
  98. string or number; to force a number to be interpreted as string \n\
  99. prefix it by \"s:\", e.g. s:1\n\
  100. Examples:\n\
  101. " NAME " -s unixs:/tmp/" NAME "_ctl system.listMethods\n\
  102. " NAME " -f \"pid: %v desc: %v\\n\" -s udp:localhost:2047 core.ps \n\
  103. " NAME " ps # uses default ctl socket \n\
  104. " NAME " # enters interactive mode on the default socket \n\
  105. " NAME " -s tcp:localhost # interactive mode, default port \n\
  106. ";
  107. int verbose = 0;
  108. char *reply_socket = 0; /* unix datagram reply socket name */
  109. char *sock_dir = 0; /* same as above, but only the directory */
  110. char *unix_socket = 0;
  111. struct sockaddr_un mysun;
  112. int quit; /* used only in interactive mode */
  113. struct binrpc_val *rpc_array;
  114. int rpc_no = 0;
  115. #ifdef USE_CFG_VARS
  116. struct binrpc_val *cfg_vars_array;
  117. int cfg_vars_no;
  118. struct cfg_var_grp
  119. {
  120. struct cfg_var_grp *next;
  121. str grp_name; /**< group name */
  122. str *var_names; /**< str array, null terminated */
  123. int var_no;
  124. };
  125. struct cfg_var_grp *cfg_grp_lst; /** cfg groups list, allong with var names*/
  126. struct cfg_var_grp *crt_cfg_grp;
  127. #endif /* USE_CFG_VARS */
  128. #ifdef USE_COUNTERS
  129. struct binrpc_val *cnt_grps_array; /* response array */
  130. int cnt_grps_no; /* number of response records */
  131. struct cnt_var_grp
  132. {
  133. struct cnt_var_grp *next;
  134. str grp_name;
  135. str *var_names; /**< str array (null terminated strings)*/
  136. int var_no;
  137. struct binrpc_val *cnt_vars_array; /* var_name will point here */
  138. int cnt_vars_no; /* cnt_vars_array size (no. of response records) */
  139. };
  140. struct cnt_var_grp *cnt_grp_lst; /* counters groups list, allong with vars */
  141. struct cnt_var_grp *crt_cnt_grp;
  142. #endif /* USE_COUNTERS */
  143. #define IOV_SET(vect, str) \
  144. do { \
  145. (vect).iov_base = (str); \
  146. (vect).iov_len = strlen((str)); \
  147. } while(0)
  148. #define INT2STR_MAX_LEN (19 + 1 + 1) /* 2^64~= 16*10^18 => 19+1 digits + \0 */
  149. /* returns a pointer to a static buffer containing l in asciiz & sets len */
  150. static inline char *int2str(unsigned int l, int *len)
  151. {
  152. static char r[INT2STR_MAX_LEN];
  153. int i;
  154. i = INT2STR_MAX_LEN - 2;
  155. r[INT2STR_MAX_LEN - 1] = 0; /* null terminate */
  156. do {
  157. r[i] = l % 10 + '0';
  158. i--;
  159. l /= 10;
  160. } while(l && (i >= 0));
  161. if(l && (i < 0)) {
  162. fprintf(stderr, "BUG: int2str: overflow\n");
  163. }
  164. if(len)
  165. *len = (INT2STR_MAX_LEN - 2) - i;
  166. return &r[i + 1];
  167. }
  168. static char *trim_ws(char *l)
  169. {
  170. char *ret;
  171. for(; *l && ((*l == ' ') || (*l == '\t') || (*l == '\n') || (*l == '\r'));
  172. l++)
  173. ;
  174. ret = l;
  175. if(*ret == 0)
  176. return ret;
  177. for(l = l + strlen(l) - 1;
  178. (l > ret)
  179. && ((*l == ' ') || (*l == '\t') || (*l == '\n') || (*l == '\r'));
  180. l--)
  181. ;
  182. *(l + 1) = 0;
  183. return ret;
  184. }
  185. int gen_cookie()
  186. {
  187. return rand();
  188. }
  189. struct binrpc_cmd
  190. {
  191. char *method;
  192. int argc;
  193. struct binrpc_val argv[MAX_BINRPC_ARGS];
  194. };
  195. struct cmd_alias
  196. {
  197. char *name;
  198. char *method;
  199. char *format; /* reply print format */
  200. };
  201. struct kamcmd_builtin
  202. {
  203. char *name;
  204. int (*f)(int, struct binrpc_cmd *);
  205. char *doc;
  206. };
  207. static int kamcmd_help(int s, struct binrpc_cmd *cmd);
  208. static int kamcmd_ver(int s, struct binrpc_cmd *cmd);
  209. static int kamcmd_quit(int s, struct binrpc_cmd *cmd);
  210. static int kamcmd_warranty(int s, struct binrpc_cmd *cmd);
  211. static struct cmd_alias cmd_aliases[] = {{"ps", "core.ps", "%v\t%v\n"},
  212. {"psx", "core.psx", 0}, {"list", "system.listMethods", 0},
  213. {"ls", "system.listMethods", 0}, {"ver", "core.version", 0},
  214. {"version", "core.version", 0},
  215. {"who", "ctl.who", "[%v] %v: %v %v -> %v %v\n"},
  216. {"listen", "ctl.listen", "[%v] %v: %v %v\n"},
  217. {"dns_mem_info", "dns.mem_info", "%v / %v\n"},
  218. {"dns_debug", "dns.debug",
  219. "%v (%v): size=%v ref=%v expire=%vs last=%vs ago f=%v\n"},
  220. {"dns_debug_all", "dns.debug_all",
  221. "%v (%v) [%v]: size=%v ref=%v expire=%vs last=%vs ago f=%v\n"
  222. "\t\t%v:%v expire=%vs f=%v\n"},
  223. {"dst_blocklist_mem_info", "dst_blocklist.mem_info", "%v / %v\n"},
  224. {"dst_blocklist_debug", "dst_blocklist.debug",
  225. "%v:%v:%v expire:%v flags: %v\n"},
  226. {0, 0, 0}};
  227. static struct kamcmd_builtin builtins[] = {{"?", kamcmd_help, "help"},
  228. {"help", kamcmd_help, "displays help for a command"},
  229. {"version", kamcmd_ver, "displays " NAME "version"},
  230. {"quit", kamcmd_quit, "exits " NAME},
  231. {"exit", kamcmd_quit, "exits " NAME},
  232. {"warranty", kamcmd_warranty, "displays " NAME "'s warranty info"},
  233. {"license", kamcmd_warranty, "displays " NAME "'s license"}, {0, 0}};
  234. #ifdef USE_READLINE
  235. enum complete_states
  236. {
  237. COMPLETE_INIT,
  238. COMPLETE_CMD_NAME,
  239. #ifdef USE_CFG_VARS
  240. COMPLETE_CFG_GRP,
  241. COMPLETE_CFG_VAR,
  242. #endif /* USE_CFG_VARS */
  243. #ifdef USE_COUNTERS
  244. COMPLETE_CNT_GRP,
  245. COMPLETE_CNT_VAR,
  246. #endif /* USE_COUNTERS */
  247. COMPLETE_NOTHING
  248. };
  249. /* instead of rl_attempted_completion_over which is not present in
  250. some readline emulations, use attempted_completion_state */
  251. static enum complete_states attempted_completion_state;
  252. static int crt_param_no;
  253. /* commands for which we complete the params to other method names */
  254. char *complete_params_methods[] = {
  255. "?", "h", "help", "system.methodSignature", "system.methodHelp", 0};
  256. #ifdef USE_CFG_VARS
  257. /* commands for which we complete the first param with a cfg var grp*/
  258. char *complete_params_cfg_var[] = {"cfg.get", "cfg.help", "cfg.set_delayed_int",
  259. "cfg.set_delayed_string", "cfg.set_now_int", "cfg.set_now_string", 0};
  260. #endif /* USE_CFG_VARS */
  261. #ifdef USE_COUNTERS
  262. /* commands for which we complete the first param with a counter group */
  263. char *complete_param1_counter_grp[] = {"cnt.get", "cnt.get_raw",
  264. "cnt.grp_get_all", "cnt.reset", "cnt.var_list", "cnt.help", 0};
  265. /* commands for which we completed the 2nd param with a counter name */
  266. char *complete_param2_counter_name[] = {
  267. "cnt.get", "cnt.get_raw", "cnt.reset", "cnt.help", 0};
  268. #endif /* USE_COUNTERS */
  269. #endif /* USE_READLINE */
  270. static int parse_arg(struct binrpc_val *v, char *arg)
  271. {
  272. int i;
  273. double f;
  274. char *tmp;
  275. int len;
  276. i = strtol(arg, &tmp, 10);
  277. if((tmp == 0) || (*tmp)) {
  278. f = strtod(arg, &tmp);
  279. if((tmp == 0) || (*tmp)) {
  280. /* not an int or a float => string */
  281. len = strlen(arg);
  282. if((len >= 2) && (arg[0] == 's') && (arg[1] == ':')) {
  283. tmp = &arg[2];
  284. len -= 2;
  285. } else {
  286. tmp = arg;
  287. }
  288. v->type = BINRPC_T_STR;
  289. v->u.strval.s = tmp;
  290. v->u.strval.len = len;
  291. } else { /* float */
  292. v->type = BINRPC_T_DOUBLE;
  293. v->u.fval = f;
  294. }
  295. } else { /* int */
  296. v->type = BINRPC_T_INT;
  297. v->u.intval = i;
  298. }
  299. return 0;
  300. }
  301. static int parse_cmd(struct binrpc_cmd *cmd, char **argv, int count)
  302. {
  303. int r;
  304. cmd->method = argv[0];
  305. if((count - 1) > MAX_BINRPC_ARGS) {
  306. fprintf(stderr, "ERROR: too many args %d, only %d allowed\n", count - 1,
  307. MAX_BINRPC_ARGS);
  308. return -1;
  309. }
  310. for(r = 1; r < count; r++) {
  311. if(parse_arg(&cmd->argv[r - 1], argv[r]) < 0)
  312. return -1;
  313. }
  314. cmd->argc = r - 1;
  315. return 0;
  316. }
  317. void print_binrpc_val(struct binrpc_val *v, int ident)
  318. {
  319. int r;
  320. if((v->type == BINRPC_T_STRUCT) && !v->u.end)
  321. ident--; /* fix to have struct beg. idented differently */
  322. for(r = 0; r < ident; r++)
  323. putchar(' ');
  324. if(v->name.s) {
  325. printf("%.*s: ", v->name.len, v->name.s);
  326. }
  327. switch(v->type) {
  328. case BINRPC_T_INT:
  329. printf("%d", v->u.intval);
  330. break;
  331. case BINRPC_T_STR:
  332. case BINRPC_T_BYTES:
  333. printf("%.*s", v->u.strval.len, v->u.strval.s);
  334. break;
  335. case BINRPC_T_ARRAY:
  336. printf("%c", (v->u.end) ? ']' : '[');
  337. break;
  338. case BINRPC_T_STRUCT:
  339. printf("%c", (v->u.end) ? '}' : '{');
  340. break;
  341. case BINRPC_T_DOUBLE:
  342. if(v->u.fval == (double)((long long int)v->u.fval)) {
  343. printf("%lld", (long long int)v->u.fval);
  344. } else {
  345. printf("%f", v->u.fval);
  346. }
  347. break;
  348. default:
  349. printf("ERROR: unknown type %d\n", v->type);
  350. };
  351. }
  352. /* opens, and connects on a STREAM unix socket
  353. * returns socket fd or -1 on error */
  354. int connect_unix_sock(char *name, int type)
  355. {
  356. struct sockaddr_un ifsun;
  357. int s;
  358. int len;
  359. int ret;
  360. int retries;
  361. retries = 0;
  362. s = -1;
  363. memset(&ifsun, 0, sizeof(struct sockaddr_un));
  364. len = strlen(name);
  365. if(len > UNIX_PATH_MAX) {
  366. fprintf(stderr,
  367. "ERROR: connect_unix_sock: name too long "
  368. "(%d > %d): %s\n",
  369. len, UNIX_PATH_MAX, name);
  370. goto error;
  371. }
  372. ifsun.sun_family = AF_UNIX;
  373. memcpy(ifsun.sun_path, name, len);
  374. #ifdef HAVE_SOCKADDR_SA_LEN
  375. ifsun.sun_len = len;
  376. #endif
  377. s = socket(PF_UNIX, type, 0);
  378. if(s == -1) {
  379. fprintf(stderr,
  380. "ERROR: connect_unix_sock: cannot create unix socket"
  381. " %s: %s [%d]\n",
  382. name, strerror(errno), errno);
  383. goto error;
  384. }
  385. if(type == SOCK_DGRAM) {
  386. /* we must bind so that we can receive replies */
  387. if(reply_socket == 0) {
  388. if(sock_dir == 0)
  389. sock_dir = "/tmp";
  390. retry:
  391. ret = snprintf(mysun.sun_path, UNIX_PATH_MAX, "%s/" NAME "_%d",
  392. sock_dir, rand());
  393. if((ret < 0) || (ret >= UNIX_PATH_MAX)) {
  394. fprintf(stderr, "ERROR: buffer overflow while trying to"
  395. "generate unix datagram socket name");
  396. goto error;
  397. }
  398. } else {
  399. if(strlen(reply_socket) > UNIX_PATH_MAX) {
  400. fprintf(stderr,
  401. "ERROR: buffer overflow while trying to"
  402. "use the provided unix datagram socket name (%s)",
  403. reply_socket);
  404. goto error;
  405. }
  406. strcpy(mysun.sun_path, reply_socket);
  407. }
  408. mysun.sun_family = AF_UNIX;
  409. if(bind(s, (struct sockaddr *)&mysun, sizeof(mysun)) == -1) {
  410. if(errno == EADDRINUSE && (reply_socket == 0) && (retries < 10)) {
  411. retries++;
  412. /* try another one */
  413. goto retry;
  414. }
  415. fprintf(stderr,
  416. "ERROR: could not bind the unix socket to"
  417. " %s: %s (%d)\n",
  418. mysun.sun_path, strerror(errno), errno);
  419. goto error;
  420. }
  421. unix_socket = mysun.sun_path;
  422. }
  423. if(connect(s, (struct sockaddr *)&ifsun, sizeof(ifsun)) == -1) {
  424. fprintf(stderr, "ERROR: connect_unix_sock: connect(%s): %s [%d]\n",
  425. name, strerror(errno), errno);
  426. goto error;
  427. }
  428. return s;
  429. error:
  430. if(s != -1)
  431. close(s);
  432. return -1;
  433. }
  434. int connect_tcpudp_socket(char *address, int port, int type)
  435. {
  436. struct sockaddr_in addr;
  437. struct hostent *he;
  438. int sock;
  439. sock = -1;
  440. /* resolve destination */
  441. he = gethostbyname(address);
  442. if(he == 0) {
  443. fprintf(stderr, "ERROR: could not resolve %s\n", address);
  444. goto error;
  445. }
  446. /* open socket*/
  447. addr.sin_family = he->h_addrtype;
  448. addr.sin_port = htons(port);
  449. memcpy(&addr.sin_addr.s_addr, he->h_addr_list[0], he->h_length);
  450. sock = socket(he->h_addrtype, type, 0);
  451. if(sock == -1) {
  452. fprintf(stderr, "ERROR: socket: %s\n", strerror(errno));
  453. goto error;
  454. }
  455. if(connect(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr)) != 0) {
  456. fprintf(stderr, "ERROR: connect: %s\n", strerror(errno));
  457. goto error;
  458. }
  459. return sock;
  460. error:
  461. if(sock != -1)
  462. close(sock);
  463. return -1;
  464. }
  465. static void hexdump(unsigned char *buf, int len, int ascii)
  466. {
  467. int r, i;
  468. /* dump it in hex */
  469. for(r = 0; r < len; r++) {
  470. if((r) && ((r % 16) == 0)) {
  471. if(ascii) {
  472. putchar(' ');
  473. for(i = r - 16; i < r; i++) {
  474. if(isprint(buf[i]))
  475. putchar(buf[i]);
  476. else
  477. putchar('.');
  478. }
  479. }
  480. putchar('\n');
  481. }
  482. printf("%02x ", buf[r]);
  483. };
  484. if(ascii) {
  485. for(i = r; i % 16; i++)
  486. printf(" ");
  487. putchar(' ');
  488. for(i = 16 * (r / 16); i < r; i++) {
  489. if(isprint(buf[i]))
  490. putchar(buf[i]);
  491. else
  492. putchar('.');
  493. }
  494. }
  495. putchar('\n');
  496. }
  497. /* returns: -1 on error, number of bytes written on success */
  498. static int send_binrpc_cmd(int s, struct binrpc_cmd *cmd, int cookie)
  499. {
  500. struct iovec v[IOVEC_CNT];
  501. int r;
  502. unsigned char msg_body[MAX_BODY_SIZE];
  503. unsigned char msg_hdr[BINRPC_MAX_HDR_SIZE];
  504. struct binrpc_pkt body;
  505. int ret;
  506. int n;
  507. ret = binrpc_init_pkt(&body, msg_body, MAX_BODY_SIZE);
  508. if(ret < 0)
  509. goto binrpc_err;
  510. ret = binrpc_addstr(&body, cmd->method, strlen(cmd->method));
  511. if(ret < 0)
  512. goto binrpc_err;
  513. for(r = 0; r < cmd->argc; r++) {
  514. switch(cmd->argv[r].type) {
  515. case BINRPC_T_STR:
  516. ret = binrpc_addstr(&body, cmd->argv[r].u.strval.s,
  517. cmd->argv[r].u.strval.len);
  518. break;
  519. case BINRPC_T_INT:
  520. ret = binrpc_addint(&body, cmd->argv[r].u.intval);
  521. break;
  522. case BINRPC_T_DOUBLE:
  523. ret = binrpc_adddouble(&body, cmd->argv[r].u.fval);
  524. break;
  525. default:
  526. fprintf(stderr, "ERROR: unsupported type %d\n",
  527. cmd->argv[r].type);
  528. }
  529. if(ret < 0)
  530. goto binrpc_err;
  531. }
  532. ret = binrpc_build_hdr(BINRPC_REQ, binrpc_pkt_len(&body), cookie, msg_hdr,
  533. BINRPC_MAX_HDR_SIZE);
  534. if(ret < 0)
  535. goto binrpc_err;
  536. v[0].iov_base = msg_hdr;
  537. v[0].iov_len = ret;
  538. v[1].iov_base = msg_body;
  539. v[1].iov_len = binrpc_pkt_len(&body);
  540. write_again:
  541. if((n = writev(s, v, 2)) < 0) {
  542. if(errno == EINTR)
  543. goto write_again;
  544. goto error_send;
  545. }
  546. return n;
  547. error_send:
  548. return -1;
  549. binrpc_err:
  550. return -2;
  551. }
  552. static int binrpc_errno = 0;
  553. /* reads the whole reply
  554. * returns < 0 on error, reply size on success + initializes in_pkt
  555. * if ret==-2 (parse error), sets binrpc_errno to the binrpc error
  556. * error returns: -1 - read error (check errno)
  557. * -2 - binrpc parse error (check binrpc_errno)
  558. * -3 - cookie error (the cookied doesn't match)
  559. * -4 - message too big */
  560. static int get_reply(int s, unsigned char *reply_buf, int max_reply_size,
  561. int cookie, struct binrpc_parse_ctx *in_pkt, unsigned char **body)
  562. {
  563. unsigned char *crt;
  564. unsigned char *hdr_end;
  565. unsigned char *msg_end;
  566. int n;
  567. int ret;
  568. hdr_end = crt = reply_buf;
  569. msg_end = reply_buf + max_reply_size;
  570. binrpc_errno = 0;
  571. do {
  572. n = read(s, crt, (int)(msg_end - crt));
  573. if(n <= 0) {
  574. if(errno == EINTR)
  575. continue;
  576. goto error_read;
  577. }
  578. if(verbose >= 3) {
  579. /* dump it in hex */
  580. printf("received %d bytes in reply (@offset %d):\n", n,
  581. (int)(crt - reply_buf));
  582. hexdump(crt, n, 1);
  583. }
  584. crt += n;
  585. /* parse header if not parsed yet */
  586. if(hdr_end == reply_buf) {
  587. hdr_end = binrpc_parse_init(in_pkt, reply_buf, n, &ret);
  588. if(ret < 0) {
  589. if(ret == E_BINRPC_MORE_DATA)
  590. continue;
  591. goto error_parse;
  592. }
  593. if(verbose > 1) {
  594. printf("new packet: type %02x, len %d, cookie %02x\n",
  595. in_pkt->type, in_pkt->tlen, in_pkt->cookie);
  596. }
  597. if(in_pkt->cookie != cookie) {
  598. fprintf(stderr,
  599. "bad reply, cookie doesn't match: sent %02x "
  600. "and received %02x\n",
  601. cookie, in_pkt->cookie);
  602. goto error;
  603. }
  604. msg_end = hdr_end + in_pkt->tlen;
  605. if((int)(msg_end - reply_buf) > max_reply_size) {
  606. /* reading the rest from the socket */
  607. struct timeval timeout_save;
  608. unsigned sizeoft = sizeof(timeout_save);
  609. if(getsockopt(
  610. s, SOL_SOCKET, SO_RCVTIMEO, &timeout_save, &sizeoft)
  611. == 0) {
  612. struct timeval timeout;
  613. timeout.tv_sec = 1;
  614. timeout.tv_usec = 0;
  615. if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
  616. sizeof(timeout))
  617. == 0) {
  618. while(read(s, reply_buf, max_reply_size) > 0)
  619. ;
  620. setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
  621. (char *)&timeout_save, sizeof(timeout_save));
  622. }
  623. }
  624. goto error_toolong;
  625. }
  626. }
  627. } while(crt < msg_end);
  628. *body = hdr_end;
  629. return (int)(msg_end - reply_buf);
  630. error_read:
  631. return -1;
  632. error_parse:
  633. binrpc_errno = ret;
  634. return -2;
  635. error:
  636. return -3;
  637. error_toolong:
  638. return -4;
  639. }
  640. /* returns a malloced copy of str, with all the escapes ('\') resolved */
  641. static char *str_escape(char *str)
  642. {
  643. char *n;
  644. char *ret;
  645. ret = n = malloc(strlen(str) + 1);
  646. if(n == 0)
  647. goto end;
  648. for(; *str; str++) {
  649. *n = *str;
  650. if(*str == '\\') {
  651. switch(*(str + 1)) {
  652. case 'n':
  653. *n = '\n';
  654. str++;
  655. break;
  656. case 'r':
  657. *n = '\r';
  658. str++;
  659. break;
  660. case 't':
  661. *n = '\t';
  662. str++;
  663. break;
  664. case '\\':
  665. str++;
  666. break;
  667. }
  668. }
  669. n++;
  670. }
  671. *n = *str; /* terminating 0 */
  672. end:
  673. return ret;
  674. }
  675. /* parses strings like "bla bla %v 10%% %v\n test=%v",
  676. * and stops at each %v, returning a pointer after the %v, setting *size
  677. * to the string length (not including %v) and *type to the corresponding
  678. * BINRPC type (for now only BINRPC_T_ALL).
  679. * To escape a '%', use "%%", and check for type==-1 (which means skip a call
  680. * again parse_fmt).
  681. * Usage:
  682. * n="test: %v,%v,%v\n";
  683. * while(*n){
  684. * s=n;
  685. * n=parse_fmt(n, &type, &size);
  686. * printf("%.*s", size, s);
  687. * if (type==-1)
  688. * continue;
  689. * else
  690. * printf("now we should get & print an object of type %d\n", type)
  691. * }
  692. */
  693. static char *parse_fmt(char *fmt, int *type, int *size)
  694. {
  695. char *s;
  696. s = fmt;
  697. do {
  698. for(; *fmt && *fmt != '%'; fmt++)
  699. ;
  700. if(*fmt == '%') {
  701. switch(*(fmt + 1)) {
  702. case 'v':
  703. *type = BINRPC_T_ALL;
  704. *size = (int)(fmt - s);
  705. return (fmt + 2);
  706. break;
  707. case '%':
  708. /* escaped % */
  709. *size = (int)(fmt - s) + 1;
  710. *type = -1; /* skip */
  711. return (fmt + 2);
  712. break;
  713. }
  714. }
  715. } while(*fmt);
  716. *type = -1; /* no value */
  717. *size = (fmt - s);
  718. return fmt;
  719. }
  720. static int print_body(struct binrpc_parse_ctx *in_pkt, unsigned char *body,
  721. int size, char *fmt)
  722. {
  723. unsigned char *p;
  724. unsigned char *end;
  725. struct binrpc_val val;
  726. int ret;
  727. int rec;
  728. char *f;
  729. char *s;
  730. int f_size;
  731. int fmt_has_values;
  732. p = body;
  733. end = p + size;
  734. rec = 0;
  735. f = fmt;
  736. fmt_has_values = 0;
  737. /* read body */
  738. while(p < end) {
  739. if(f) {
  740. do {
  741. if(*f == 0)
  742. f = fmt; /* reset */
  743. s = f;
  744. f = parse_fmt(f, &val.type, &f_size);
  745. printf("%.*s", f_size, s);
  746. if(val.type != -1) {
  747. fmt_has_values = 1;
  748. goto read_value;
  749. }
  750. } while(*f || fmt_has_values);
  751. val.type = BINRPC_T_ALL;
  752. } else {
  753. val.type = BINRPC_T_ALL;
  754. }
  755. read_value:
  756. val.name.s = 0;
  757. val.name.len = 0;
  758. p = binrpc_read_record(in_pkt, p, end, &val, 1, &ret);
  759. if(ret < 0) {
  760. if(fmt)
  761. putchar('\n');
  762. /*if (ret==E_BINRPC_MORE_DATA)
  763. goto error_read_again;*/
  764. if(ret == E_BINRPC_EOP) {
  765. printf("end of message detected\n");
  766. break;
  767. }
  768. fprintf(stderr,
  769. "ERROR:: while parsing the record %d,"
  770. " @%d: %02x : %s\n",
  771. rec, in_pkt->offset, *p, binrpc_error(ret));
  772. goto error;
  773. }
  774. rec++;
  775. if(fmt) {
  776. print_binrpc_val(&val, 0);
  777. } else {
  778. print_binrpc_val(&val, in_pkt->in_struct + in_pkt->in_array);
  779. putchar('\n');
  780. }
  781. }
  782. if(fmt && *f) {
  783. /* print the rest, with empty values */
  784. while(*f) {
  785. s = f;
  786. f = parse_fmt(f, &val.type, &f_size);
  787. printf("%.*s", f_size, s);
  788. }
  789. }
  790. return 0;
  791. error:
  792. return -1;
  793. /*error_read_again:
  794. fprintf(stderr, "ERROR: more data needed\n");
  795. return -2;
  796. */
  797. }
  798. static int print_fault(
  799. struct binrpc_parse_ctx *in_pkt, unsigned char *body, int size)
  800. {
  801. printf("error: ");
  802. return print_body(in_pkt, body, size, "%v - %v\n");
  803. }
  804. static int run_binrpc_cmd(int s, struct binrpc_cmd *cmd, char *fmt)
  805. {
  806. int cookie;
  807. unsigned char reply_buf[MAX_REPLY_SIZE];
  808. unsigned char *msg_body;
  809. struct binrpc_parse_ctx in_pkt;
  810. int ret;
  811. cookie = gen_cookie();
  812. if((ret = send_binrpc_cmd(s, cmd, cookie)) < 0) {
  813. if(ret == -1)
  814. goto error_send;
  815. else
  816. goto binrpc_err;
  817. }
  818. /* read reply */
  819. memset(&in_pkt, 0, sizeof(in_pkt));
  820. if((ret = get_reply(
  821. s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt, &msg_body))
  822. < 0) {
  823. switch(ret) {
  824. case -1:
  825. goto error_read;
  826. case -2:
  827. goto error_parse;
  828. case -3:
  829. goto error_cookie;
  830. case -4:
  831. goto error_toobig;
  832. }
  833. goto error;
  834. }
  835. switch(in_pkt.type) {
  836. case BINRPC_FAULT:
  837. if(print_fault(&in_pkt, msg_body, in_pkt.tlen) < 0) {
  838. goto error;
  839. }
  840. break;
  841. case BINRPC_REPL:
  842. if(print_body(&in_pkt, msg_body, in_pkt.tlen, fmt) < 0) {
  843. goto error;
  844. }
  845. break;
  846. default:
  847. fprintf(stderr, "ERROR: not a reply\n");
  848. goto error;
  849. }
  850. if(verbose)
  851. printf(".\n");
  852. /* normal exit */
  853. return 0;
  854. binrpc_err:
  855. fprintf(stderr, "ERROR while building the packet: %s\n", binrpc_error(ret));
  856. goto error;
  857. error_parse:
  858. fprintf(stderr, "ERROR while parsing the reply: %s\n",
  859. binrpc_error(binrpc_errno));
  860. goto error;
  861. error_cookie:
  862. fprintf(stderr, "ERROR: cookie does not match\n");
  863. goto error;
  864. error_toobig:
  865. fprintf(stderr, "ERROR: reply too big\n");
  866. goto error;
  867. error_send:
  868. fprintf(stderr, "ERROR: send packet failed: %s (%d)\n", strerror(errno),
  869. errno);
  870. goto error;
  871. error_read:
  872. fprintf(stderr, "ERROR: read reply failed: %s (%d)\n", strerror(errno),
  873. errno);
  874. goto error;
  875. error:
  876. return -1;
  877. }
  878. static int parse_line(struct binrpc_cmd *cmd, char *line)
  879. {
  880. char *p;
  881. int count;
  882. cmd->method = strtok(line, " \t");
  883. if(cmd->method == 0)
  884. goto error_no_method;
  885. count = 0;
  886. for(p = strtok(0, " \t"); p; p = strtok(0, " \t")) {
  887. if(count >= MAX_BINRPC_ARGS)
  888. goto error_too_many;
  889. if(parse_arg(&cmd->argv[count], p) < 0) {
  890. goto error_arg;
  891. }
  892. count++;
  893. }
  894. cmd->argc = count;
  895. return 0;
  896. error_no_method:
  897. printf("ERROR: no method name\n");
  898. return -1;
  899. error_too_many:
  900. printf("ERROR: too many arguments (%d), no more than %d allowed\n", count,
  901. MAX_BINRPC_ARGS);
  902. return -1;
  903. error_arg:
  904. printf("ERROR: bad argument %d: %s\n", count + 1, p);
  905. return -1;
  906. }
  907. /* resolves builtin aliases */
  908. static void fix_cmd(struct binrpc_cmd *cmd, char **format)
  909. {
  910. int r;
  911. for(r = 0; cmd_aliases[r].name; r++) {
  912. if(strcmp(cmd_aliases[r].name, cmd->method) == 0) {
  913. cmd->method = cmd_aliases[r].method;
  914. if(*format == 0)
  915. *format = cmd_aliases[r].format;
  916. break;
  917. }
  918. }
  919. }
  920. /* intercept builtin commands, returns 1 if intercepted, 0 if not, <0 on error
  921. */
  922. static int run_builtins(int s, struct binrpc_cmd *cmd)
  923. {
  924. int r;
  925. int ret;
  926. for(r = 0; builtins[r].name; r++) {
  927. if(strcmp(builtins[r].name, cmd->method) == 0) {
  928. ret = builtins[r].f(s, cmd);
  929. return (ret < 0) ? ret : 1;
  930. }
  931. }
  932. return 0;
  933. }
  934. /* runs command from cmd */
  935. inline static int run_cmd(int s, struct binrpc_cmd *cmd, char *format)
  936. {
  937. int ret;
  938. char *fmt;
  939. fmt = format;
  940. fix_cmd(cmd, &fmt);
  941. if(!(ret = run_builtins(s, cmd))) {
  942. ret = run_binrpc_cmd(s, cmd, fmt);
  943. }
  944. return (ret > 0) ? 0 : ret;
  945. }
  946. /* runs a command represented in line */
  947. inline static int run_line(int s, char *l, char *format)
  948. {
  949. struct binrpc_cmd cmd;
  950. int ret;
  951. if((ret = parse_line(&cmd, l)) == 0) {
  952. return run_cmd(s, &cmd, format);
  953. }
  954. return ret;
  955. }
  956. static void free_rpc_array(struct binrpc_val *a, int size)
  957. {
  958. int r;
  959. for(r = 0; r < size; r++) {
  960. if(a[r].name.s)
  961. free(a[r].name.s);
  962. if((a[r].type == BINRPC_T_STR || a[r].type == BINRPC_T_BYTES)
  963. && a[r].u.strval.s) {
  964. free(a[r].u.strval.s);
  965. }
  966. }
  967. free(a);
  968. }
  969. /* parse the body into a malloc allocated, binrpc_val array */
  970. static struct binrpc_val *parse_reply_body(int *records,
  971. struct binrpc_parse_ctx *in_pkt, unsigned char *body, int size)
  972. {
  973. struct binrpc_val *a;
  974. struct binrpc_val *t;
  975. unsigned char *p;
  976. unsigned char *end;
  977. struct binrpc_val val;
  978. int ret;
  979. int rec;
  980. rec = 0;
  981. if(*records == 0) {
  982. *records = 100; /* start with a reasonable size */
  983. };
  984. a = malloc(*records * sizeof(struct binrpc_val));
  985. if(a == 0)
  986. goto error_mem;
  987. p = body;
  988. end = p + size;
  989. /* read body */
  990. while(p < end) {
  991. val.type = BINRPC_T_ALL;
  992. val.name.s = 0;
  993. val.name.len = 0;
  994. p = binrpc_read_record(in_pkt, p, end, &val, 1, &ret);
  995. if(ret < 0) {
  996. if(ret == E_BINRPC_EOP) {
  997. printf("end of message detected\n");
  998. break;
  999. }
  1000. fprintf(stderr,
  1001. "ERROR: while parsing the record %d,"
  1002. " @%d: %02x : %s\n",
  1003. rec, in_pkt->offset, *p, binrpc_error(ret));
  1004. goto error;
  1005. }
  1006. if(rec >= *records) {
  1007. t = realloc(a, *records * sizeof(struct binrpc_val) * 2);
  1008. if(t == 0)
  1009. goto error_mem;
  1010. a = t;
  1011. *records *= 2;
  1012. }
  1013. a[rec] = val;
  1014. if(val.name.s) {
  1015. if((a[rec].name.s = malloc(val.name.len + 1)) == 0)
  1016. goto error_mem;
  1017. memcpy(a[rec].name.s, val.name.s, val.name.len);
  1018. a[rec].name.s[val.name.len + 1] = 0; /* 0-term */
  1019. }
  1020. if(val.u.strval.s) {
  1021. if(val.type == BINRPC_T_STR) {
  1022. if((a[rec].u.strval.s = malloc(val.u.strval.len + 1)) == 0)
  1023. goto error_mem;
  1024. memcpy(a[rec].u.strval.s, val.u.strval.s, val.u.strval.len);
  1025. a[rec].u.strval.s[val.u.strval.len] = 0; /* 0-term */
  1026. } else if(val.type == BINRPC_T_BYTES) {
  1027. if((a[rec].u.strval.s = malloc(val.u.strval.len)) == 0)
  1028. goto error_mem;
  1029. memcpy(a[rec].u.strval.s, val.u.strval.s, val.u.strval.len);
  1030. }
  1031. }
  1032. rec++;
  1033. }
  1034. if(rec && (rec < *records)) {
  1035. a = realloc(a, rec * sizeof(struct binrpc_val));
  1036. }
  1037. *records = rec;
  1038. return a;
  1039. error_mem:
  1040. fprintf(stderr, "ERROR: parse_reply_body: out of memory\n");
  1041. error:
  1042. if(a) {
  1043. free_rpc_array(a, rec);
  1044. }
  1045. *records = 0;
  1046. return 0;
  1047. }
  1048. static int get_kamcmd_list(int s)
  1049. {
  1050. struct binrpc_cmd cmd;
  1051. int cookie;
  1052. unsigned char reply_buf[MAX_REPLY_SIZE];
  1053. unsigned char *msg_body;
  1054. struct binrpc_parse_ctx in_pkt;
  1055. int ret;
  1056. cmd.method = "system.listMethods";
  1057. cmd.argc = 0;
  1058. cookie = gen_cookie();
  1059. if((ret = send_binrpc_cmd(s, &cmd, cookie)) < 0) {
  1060. if(ret == -1)
  1061. goto error_send;
  1062. else
  1063. goto binrpc_err;
  1064. }
  1065. /* read reply */
  1066. memset(&in_pkt, 0, sizeof(in_pkt));
  1067. if((ret = get_reply(
  1068. s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt, &msg_body))
  1069. < 0) {
  1070. goto error;
  1071. }
  1072. switch(in_pkt.type) {
  1073. case BINRPC_FAULT:
  1074. if(print_fault(&in_pkt, msg_body, in_pkt.tlen) < 0) {
  1075. goto error;
  1076. }
  1077. break;
  1078. case BINRPC_REPL:
  1079. rpc_no = 100; /* default cmd list */
  1080. if((rpc_array = parse_reply_body(
  1081. &rpc_no, &in_pkt, msg_body, in_pkt.tlen))
  1082. == 0)
  1083. goto error;
  1084. break;
  1085. default:
  1086. fprintf(stderr, "ERROR: not a reply\n");
  1087. goto error;
  1088. }
  1089. return 0;
  1090. binrpc_err:
  1091. error_send:
  1092. error:
  1093. return -1;
  1094. }
  1095. #if defined(USE_CFG_VARS) || defined(USE_COUNTERS)
  1096. /** check if cmd is a rpc command.
  1097. * Quick check (using the internal rpc_array) if cmd is a valid rpc command.
  1098. * @param cmd - null terminated ascii string
  1099. * @return 1 on success, 0 on failure.
  1100. */
  1101. static int is_rpc_cmd(char *cmd)
  1102. {
  1103. int r;
  1104. int cmd_len;
  1105. cmd_len = strlen(cmd);
  1106. for(r = 0; r < rpc_no; r++) {
  1107. if((rpc_array[r].type == BINRPC_T_STR)
  1108. && (rpc_array[r].u.strval.len == cmd_len)
  1109. && (strncmp(cmd, rpc_array[r].u.strval.s, cmd_len) == 0))
  1110. return 1;
  1111. }
  1112. return 0;
  1113. }
  1114. #endif /* USE_CFG_VARS */
  1115. #ifdef USE_CFG_VARS
  1116. /* retrieve the cfg vars and group list */
  1117. static int get_cfgvars_list(int s)
  1118. {
  1119. struct binrpc_cmd cmd;
  1120. int cookie;
  1121. unsigned char reply_buf[MAX_REPLY_SIZE];
  1122. unsigned char *msg_body;
  1123. struct binrpc_parse_ctx in_pkt;
  1124. struct cfg_var_grp *grp;
  1125. struct cfg_var_grp *last_grp;
  1126. char *p;
  1127. char *end;
  1128. str grp_name;
  1129. str var_name;
  1130. int r;
  1131. int ret;
  1132. cmd.method = "cfg.list";
  1133. cmd.argc = 0;
  1134. if(!is_rpc_cmd(cmd.method))
  1135. goto error;
  1136. cookie = gen_cookie();
  1137. if((ret = send_binrpc_cmd(s, &cmd, cookie)) < 0) {
  1138. if(ret == -1)
  1139. goto error_send;
  1140. else
  1141. goto binrpc_err;
  1142. }
  1143. /* read reply */
  1144. memset(&in_pkt, 0, sizeof(in_pkt));
  1145. if((ret = get_reply(
  1146. s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt, &msg_body))
  1147. < 0) {
  1148. goto error;
  1149. }
  1150. switch(in_pkt.type) {
  1151. case BINRPC_FAULT:
  1152. if(print_fault(&in_pkt, msg_body, in_pkt.tlen) < 0) {
  1153. goto error;
  1154. }
  1155. break;
  1156. case BINRPC_REPL:
  1157. cfg_vars_no = 100; /* default cmd list */
  1158. if((cfg_vars_array = parse_reply_body(
  1159. &cfg_vars_no, &in_pkt, msg_body, in_pkt.tlen))
  1160. == 0)
  1161. goto error;
  1162. break;
  1163. default:
  1164. fprintf(stderr, "ERROR: not a reply\n");
  1165. goto error;
  1166. }
  1167. /* get the config groups */
  1168. last_grp = 0;
  1169. for(r = 0; r < cfg_vars_no; r++) {
  1170. grp_name.s = 0;
  1171. grp_name.len = 0;
  1172. if(cfg_vars_array[r].type != BINRPC_T_STR)
  1173. continue;
  1174. grp_name.s = cfg_vars_array[r].u.strval.s;
  1175. end = cfg_vars_array[r].u.strval.len + grp_name.s;
  1176. /* parse <grp>: <var_name>*/
  1177. for(p = grp_name.s; p < end; p++) {
  1178. if(*p == ':') {
  1179. grp_name.len = (int)(long)(p - grp_name.s);
  1180. break;
  1181. }
  1182. }
  1183. for(grp = cfg_grp_lst; grp; grp = grp->next) {
  1184. if(grp->grp_name.len == grp_name.len
  1185. && memcmp(grp->grp_name.s, grp_name.s, grp_name.len) == 0) {
  1186. break; /* found */
  1187. }
  1188. }
  1189. if(grp == 0) {
  1190. /* not found => create a new one */
  1191. grp = malloc(sizeof(*grp));
  1192. if(grp == 0)
  1193. goto error_mem;
  1194. memset(grp, 0, sizeof(*grp));
  1195. grp->grp_name = grp_name;
  1196. if(last_grp) {
  1197. last_grp->next = grp;
  1198. last_grp = grp;
  1199. } else {
  1200. cfg_grp_lst = grp;
  1201. last_grp = cfg_grp_lst;
  1202. }
  1203. }
  1204. grp->var_no++;
  1205. }
  1206. /* alloc the var arrays per group */
  1207. for(grp = cfg_grp_lst; grp; grp = grp->next) {
  1208. grp->var_names = malloc(sizeof(str) * grp->var_no);
  1209. if(grp->var_names == 0)
  1210. goto error_mem;
  1211. memset(grp->var_names, 0, sizeof(str) * grp->var_no);
  1212. grp->var_no = 0;
  1213. }
  1214. /* reparse to get the var names per group */
  1215. for(r = 0; r < cfg_vars_no; r++) {
  1216. grp_name.s = 0;
  1217. grp_name.len = 0;
  1218. var_name.s = 0;
  1219. var_name.len = 0;
  1220. if(cfg_vars_array[r].type != BINRPC_T_STR)
  1221. continue;
  1222. grp_name.s = cfg_vars_array[r].u.strval.s;
  1223. end = cfg_vars_array[r].u.strval.len + grp_name.s;
  1224. /* parse <grp>: <var_name>*/
  1225. for(p = grp_name.s; p < end; p++) {
  1226. if(*p == ':') {
  1227. grp_name.len = (int)(long)(p - grp_name.s);
  1228. p++;
  1229. for(; p < end && *p == ' '; p++)
  1230. ;
  1231. var_name.s = p;
  1232. var_name.len = (int)(long)(end - p);
  1233. if(var_name.len == 0)
  1234. break;
  1235. for(grp = cfg_grp_lst; grp; grp = grp->next) {
  1236. if(grp->grp_name.len == grp_name.len
  1237. && memcmp(grp->grp_name.s, grp_name.s, grp_name.len)
  1238. == 0) {
  1239. /* add var */
  1240. grp->var_names[grp->var_no] = var_name;
  1241. grp->var_no++;
  1242. }
  1243. }
  1244. break;
  1245. }
  1246. }
  1247. }
  1248. return 0;
  1249. binrpc_err:
  1250. error_send:
  1251. error:
  1252. error_mem:
  1253. return -1;
  1254. }
  1255. void free_cfg_grp_lst()
  1256. {
  1257. struct cfg_var_grp *grp;
  1258. struct cfg_var_grp *last;
  1259. grp = cfg_grp_lst;
  1260. while(grp) {
  1261. last = grp;
  1262. grp = grp->next;
  1263. if(last->var_names)
  1264. free(last->var_names);
  1265. free(last);
  1266. }
  1267. cfg_grp_lst = 0;
  1268. }
  1269. #endif /* USE_CFG_VARS */
  1270. #ifdef USE_COUNTERS
  1271. /* retrieve the counters names and group list */
  1272. static int get_counters_list(int s)
  1273. {
  1274. struct binrpc_cmd cmd;
  1275. int cookie;
  1276. unsigned char reply_buf[MAX_REPLY_SIZE];
  1277. unsigned char *msg_body;
  1278. struct binrpc_parse_ctx in_pkt;
  1279. struct cnt_var_grp *grp;
  1280. struct cnt_var_grp *last_grp;
  1281. str grp_name;
  1282. str var_name;
  1283. int r;
  1284. int ret;
  1285. cmd.method = "cnt.grps_list";
  1286. cmd.argc = 0;
  1287. if(!is_rpc_cmd(cmd.method))
  1288. goto error;
  1289. cookie = gen_cookie();
  1290. if((ret = send_binrpc_cmd(s, &cmd, cookie)) < 0) {
  1291. if(ret == -1)
  1292. goto error_send;
  1293. else
  1294. goto binrpc_err;
  1295. }
  1296. /* read reply */
  1297. memset(&in_pkt, 0, sizeof(in_pkt));
  1298. if((ret = get_reply(
  1299. s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt, &msg_body))
  1300. < 0) {
  1301. goto error;
  1302. }
  1303. switch(in_pkt.type) {
  1304. case BINRPC_FAULT:
  1305. if(print_fault(&in_pkt, msg_body, in_pkt.tlen) < 0) {
  1306. goto error;
  1307. }
  1308. break;
  1309. case BINRPC_REPL:
  1310. cnt_grps_no = 20; /* default counter list */
  1311. if((cnt_grps_array = parse_reply_body(
  1312. &cnt_grps_no, &in_pkt, msg_body, in_pkt.tlen))
  1313. == 0)
  1314. goto error;
  1315. break;
  1316. default:
  1317. fprintf(stderr, "ERROR: not a reply\n");
  1318. goto error;
  1319. }
  1320. /* get the config groups */
  1321. last_grp = 0;
  1322. for(r = 0; r < cnt_grps_no; r++) {
  1323. grp_name.s = 0;
  1324. grp_name.len = 0;
  1325. if(cnt_grps_array[r].type != BINRPC_T_STR)
  1326. continue;
  1327. grp_name = cnt_grps_array[r].u.strval;
  1328. /* check for duplicates */
  1329. for(grp = cnt_grp_lst; grp; grp = grp->next) {
  1330. if(grp->grp_name.len == grp_name.len
  1331. && memcmp(grp->grp_name.s, grp_name.s, grp_name.len) == 0) {
  1332. break; /* found */
  1333. }
  1334. }
  1335. if(grp == 0) {
  1336. /* not found => create a new one */
  1337. grp = malloc(sizeof(*grp));
  1338. if(grp == 0)
  1339. goto error_mem;
  1340. memset(grp, 0, sizeof(*grp));
  1341. grp->grp_name = grp_name;
  1342. if(last_grp) {
  1343. last_grp->next = grp;
  1344. last_grp = grp;
  1345. } else {
  1346. cnt_grp_lst = grp;
  1347. last_grp = cnt_grp_lst;
  1348. }
  1349. }
  1350. }
  1351. /* gets vars per group */
  1352. for(grp = cnt_grp_lst; grp; grp = grp->next) {
  1353. cmd.method = "cnt.var_list";
  1354. cmd.argv[0].type = BINRPC_T_STR;
  1355. cmd.argv[0].u.strval = grp->grp_name;
  1356. cmd.argc = 1;
  1357. if(!is_rpc_cmd(cmd.method))
  1358. goto error;
  1359. cookie = gen_cookie();
  1360. if((ret = send_binrpc_cmd(s, &cmd, cookie)) < 0) {
  1361. if(ret == -1)
  1362. goto error_send;
  1363. else
  1364. goto binrpc_err;
  1365. }
  1366. /* read reply */
  1367. memset(&in_pkt, 0, sizeof(in_pkt));
  1368. if((ret = get_reply(
  1369. s, reply_buf, MAX_REPLY_SIZE, cookie, &in_pkt, &msg_body))
  1370. < 0) {
  1371. goto error;
  1372. }
  1373. switch(in_pkt.type) {
  1374. case BINRPC_FAULT:
  1375. if(print_fault(&in_pkt, msg_body, in_pkt.tlen) < 0) {
  1376. goto error;
  1377. }
  1378. break;
  1379. case BINRPC_REPL:
  1380. grp->cnt_vars_no = 100; /* default counter list */
  1381. if((grp->cnt_vars_array = parse_reply_body(
  1382. &grp->cnt_vars_no, &in_pkt, msg_body, in_pkt.tlen))
  1383. == 0)
  1384. goto error;
  1385. break;
  1386. default:
  1387. fprintf(stderr, "ERROR: not a reply\n");
  1388. goto error;
  1389. }
  1390. grp->var_no = 0;
  1391. grp->var_names = malloc(sizeof(str) * grp->cnt_vars_no);
  1392. if(grp->var_names == 0)
  1393. goto error_mem;
  1394. memset(grp->var_names, 0, sizeof(str) * grp->cnt_vars_no);
  1395. for(r = 0; r < grp->cnt_vars_no; r++) {
  1396. if(grp->cnt_vars_array[r].type != BINRPC_T_STR)
  1397. continue;
  1398. var_name = grp->cnt_vars_array[r].u.strval;
  1399. grp->var_names[grp->var_no] = var_name;
  1400. grp->var_no++;
  1401. }
  1402. }
  1403. return 0;
  1404. binrpc_err:
  1405. error_send:
  1406. error:
  1407. error_mem:
  1408. return -1;
  1409. }
  1410. void free_cnt_grp_lst()
  1411. {
  1412. struct cnt_var_grp *grp;
  1413. struct cnt_var_grp *last;
  1414. grp = cnt_grp_lst;
  1415. while(grp) {
  1416. last = grp;
  1417. grp = grp->next;
  1418. if(last->cnt_vars_array)
  1419. free_rpc_array(last->cnt_vars_array, last->cnt_vars_no);
  1420. free(last);
  1421. }
  1422. cnt_grp_lst = 0;
  1423. }
  1424. #endif /* USE_COUNTERS */
  1425. static void print_formatting(char *prefix, char *format, char *suffix)
  1426. {
  1427. if(format) {
  1428. printf("%s", prefix);
  1429. for(; *format; format++) {
  1430. switch(*format) {
  1431. case '\t':
  1432. printf("\\t");
  1433. break;
  1434. case '\n':
  1435. printf("\\n");
  1436. break;
  1437. case '\r':
  1438. printf("\\r");
  1439. break;
  1440. default:
  1441. putchar(*format);
  1442. }
  1443. }
  1444. printf("%s", suffix);
  1445. }
  1446. }
  1447. static int kamcmd_help(int s, struct binrpc_cmd *cmd)
  1448. {
  1449. int r;
  1450. if(cmd->argc && (cmd->argv[0].type == BINRPC_T_STR)) {
  1451. /* if it has args, try command help */
  1452. for(r = 0; cmd_aliases[r].name; r++) {
  1453. if(strcmp(cmd->argv[0].u.strval.s, cmd_aliases[r].name) == 0) {
  1454. printf("%s is an alias for %s", cmd->argv[0].u.strval.s,
  1455. cmd_aliases[r].method);
  1456. print_formatting(" with reply formatting: \"",
  1457. cmd_aliases[r].format, "\"");
  1458. putchar('\n');
  1459. return 0;
  1460. }
  1461. }
  1462. for(r = 0; builtins[r].name; r++) {
  1463. if(strcmp(cmd->argv[0].u.strval.s, builtins[r].name) == 0) {
  1464. printf("builtin command: %s\n",
  1465. builtins[r].doc ? builtins[r].doc : "undocumented");
  1466. return 0;
  1467. }
  1468. }
  1469. cmd->method = "system.methodHelp";
  1470. if(run_binrpc_cmd(s, cmd, 0) < 0) {
  1471. printf("error: no such command %s\n", cmd->argv[0].u.strval.s);
  1472. }
  1473. return 0;
  1474. }
  1475. if(rpc_no == 0) {
  1476. if(get_kamcmd_list(s) < 0)
  1477. goto error;
  1478. }
  1479. for(r = 0; r < rpc_no; r++) {
  1480. if(rpc_array[r].type == BINRPC_T_STR) {
  1481. printf("%s\n", rpc_array[r].u.strval.s);
  1482. }
  1483. }
  1484. for(r = 0; cmd_aliases[r].name; r++) {
  1485. printf("alias: %s\n", cmd_aliases[r].name);
  1486. }
  1487. for(r = 0; builtins[r].name; r++) {
  1488. printf("builtin: %s\n", builtins[r].name);
  1489. }
  1490. return 0;
  1491. error:
  1492. return -1;
  1493. }
  1494. static int kamcmd_ver(int s, struct binrpc_cmd *cmd)
  1495. {
  1496. printf("%s\n", version);
  1497. printf("%s compiled on %s \n", __FILE__, compiled);
  1498. #ifdef USE_READLINE
  1499. printf("interactive mode command completion support\n");
  1500. #endif
  1501. return 0;
  1502. }
  1503. static int kamcmd_quit(int s, struct binrpc_cmd *cmd)
  1504. {
  1505. quit = 1;
  1506. return 0;
  1507. }
  1508. static int kamcmd_warranty(int s, struct binrpc_cmd *cmd)
  1509. {
  1510. printf("%s %s\n", NAME, VERSION);
  1511. printf("%s\n", COPYRIGHT);
  1512. printf("\n%s\n", LICENSE);
  1513. return 0;
  1514. }
  1515. #ifdef USE_READLINE
  1516. /* readline command generator */
  1517. static char *kamcmd_generator(const char *text, int state)
  1518. {
  1519. static int idx;
  1520. static int list; /* aliases, builtins, rpc_array */
  1521. static int len;
  1522. char *name;
  1523. #ifdef USE_CFG_VARS
  1524. static struct cfg_var_grp *grp;
  1525. #endif
  1526. #ifdef USE_COUNTERS
  1527. static struct cnt_var_grp *cnt_grp;
  1528. #endif
  1529. switch(attempted_completion_state) {
  1530. case COMPLETE_INIT:
  1531. case COMPLETE_NOTHING:
  1532. return 0;
  1533. case COMPLETE_CMD_NAME:
  1534. if(state == 0) {
  1535. /* init */
  1536. idx = list = 0;
  1537. len = strlen(text);
  1538. }
  1539. /* return next partial match */
  1540. switch(list) {
  1541. case 0: /* aliases*/
  1542. while((name = cmd_aliases[idx].name)) {
  1543. idx++;
  1544. if(strncmp(name, text, len) == 0)
  1545. return strdup(name);
  1546. }
  1547. list++;
  1548. idx = 0;
  1549. /* no break */
  1550. case 1: /* builtins */
  1551. while((name = builtins[idx].name)) {
  1552. idx++;
  1553. if(strncmp(name, text, len) == 0)
  1554. return strdup(name);
  1555. }
  1556. list++;
  1557. idx = 0;
  1558. /* no break */
  1559. case 2: /* rpc_array */
  1560. while(idx < rpc_no) {
  1561. if(rpc_array[idx].type == BINRPC_T_STR) {
  1562. name = rpc_array[idx].u.strval.s;
  1563. idx++;
  1564. if(strncmp(name, text, len) == 0)
  1565. return strdup(name);
  1566. } else {
  1567. idx++;
  1568. }
  1569. }
  1570. }
  1571. break;
  1572. #ifdef USE_CFG_VARS
  1573. case COMPLETE_CFG_GRP:
  1574. if(state == 0) {
  1575. /* init */
  1576. len = strlen(text);
  1577. grp = cfg_grp_lst;
  1578. } else {
  1579. grp = grp->next;
  1580. }
  1581. for(; grp; grp = grp->next) {
  1582. if(len <= grp->grp_name.len
  1583. && memcmp(text, grp->grp_name.s, len) == 0) {
  1584. /* zero-term copy of the grp name */
  1585. name = malloc(grp->grp_name.len + 1);
  1586. if(name) {
  1587. memcpy(name, grp->grp_name.s, grp->grp_name.len);
  1588. name[grp->grp_name.len] = 0;
  1589. }
  1590. return name;
  1591. }
  1592. }
  1593. break;
  1594. case COMPLETE_CFG_VAR:
  1595. if(state == 0) {
  1596. /* init */
  1597. len = strlen(text);
  1598. idx = 0;
  1599. }
  1600. while(idx < crt_cfg_grp->var_no) {
  1601. if(len <= crt_cfg_grp->var_names[idx].len
  1602. && memcmp(text, crt_cfg_grp->var_names[idx].s, len)
  1603. == 0) {
  1604. /* zero-term copy of the var name */
  1605. name = malloc(crt_cfg_grp->var_names[idx].len + 1);
  1606. if(name) {
  1607. memcpy(name, crt_cfg_grp->var_names[idx].s,
  1608. crt_cfg_grp->var_names[idx].len);
  1609. name[crt_cfg_grp->var_names[idx].len] = 0;
  1610. }
  1611. idx++;
  1612. return name;
  1613. }
  1614. idx++;
  1615. }
  1616. break;
  1617. #endif /* USE_CFG_VARS */
  1618. #ifdef USE_COUNTERS
  1619. case COMPLETE_CNT_GRP:
  1620. if(state == 0) {
  1621. /* init */
  1622. len = strlen(text);
  1623. cnt_grp = cnt_grp_lst;
  1624. } else {
  1625. cnt_grp = cnt_grp->next;
  1626. }
  1627. for(; cnt_grp; cnt_grp = cnt_grp->next) {
  1628. if(len <= cnt_grp->grp_name.len
  1629. && memcmp(text, cnt_grp->grp_name.s, len) == 0) {
  1630. /* zero-term copy of the cnt_grp name */
  1631. name = malloc(cnt_grp->grp_name.len + 1);
  1632. if(name) {
  1633. memcpy(name, cnt_grp->grp_name.s,
  1634. cnt_grp->grp_name.len);
  1635. name[cnt_grp->grp_name.len] = 0;
  1636. }
  1637. return name;
  1638. }
  1639. }
  1640. break;
  1641. case COMPLETE_CNT_VAR:
  1642. if(state == 0) {
  1643. /* init */
  1644. len = strlen(text);
  1645. idx = 0;
  1646. }
  1647. while(idx < crt_cnt_grp->var_no) {
  1648. if(len <= crt_cnt_grp->var_names[idx].len
  1649. && memcmp(text, crt_cnt_grp->var_names[idx].s, len)
  1650. == 0) {
  1651. /* zero-term copy of the var name */
  1652. name = malloc(crt_cnt_grp->var_names[idx].len + 1);
  1653. if(name) {
  1654. memcpy(name, crt_cnt_grp->var_names[idx].s,
  1655. crt_cnt_grp->var_names[idx].len);
  1656. name[crt_cnt_grp->var_names[idx].len] = 0;
  1657. }
  1658. idx++;
  1659. return name;
  1660. }
  1661. idx++;
  1662. }
  1663. break;
  1664. #endif /* USE_COUNTERS */
  1665. }
  1666. /* no matches */
  1667. return 0;
  1668. }
  1669. char **kamcmd_completion(const char *text, int start, int end)
  1670. {
  1671. int i, j;
  1672. int cmd_start, cmd_end, cmd_len;
  1673. int whitespace;
  1674. #ifdef USE_CFG_VARS
  1675. struct cfg_var_grp *grp;
  1676. static int grp_start;
  1677. int grp_len;
  1678. #endif /* USE_CFG_VARS */
  1679. #ifdef USE_COUNTERS
  1680. struct cnt_var_grp *cnt_grp;
  1681. static int cnt_grp_start;
  1682. int cnt_grp_len;
  1683. #endif /* USE_COUNTERS */
  1684. crt_param_no = 0;
  1685. /* skip over whitespace at the beginning */
  1686. for(j = 0; (j < start)
  1687. && (rl_line_buffer[j] == ' ' || rl_line_buffer[j] == '\t');
  1688. j++)
  1689. ;
  1690. cmd_start = j;
  1691. if(start == cmd_start) {
  1692. /* complete cmd name at beginning */
  1693. attempted_completion_state = COMPLETE_CMD_NAME;
  1694. #ifdef USE_CFG_VARS
  1695. grp_start = 0;
  1696. #endif /* USE_CFG_VARS */
  1697. #ifdef USE_COUNTERS
  1698. cnt_grp_start = 0;
  1699. #endif /* USE_COUNTERS */
  1700. } else { /* or if this is a command for which we complete the parameters */
  1701. /* find first whitespace after command name*/
  1702. for(; (j < start) && (rl_line_buffer[j] != ' ')
  1703. && (rl_line_buffer[j] != '\t');
  1704. j++)
  1705. ;
  1706. cmd_end = j;
  1707. cmd_len = cmd_end - cmd_start;
  1708. /* count params before the current one */
  1709. whitespace = 1;
  1710. for(; j < start; j++) {
  1711. if(rl_line_buffer[j] != ' ' && rl_line_buffer[j] != '\t') {
  1712. if(whitespace)
  1713. crt_param_no++;
  1714. whitespace = 0;
  1715. } else
  1716. whitespace = 1;
  1717. }
  1718. crt_param_no++;
  1719. if(crt_param_no == 1) {
  1720. for(i = 0; complete_params_methods[i]; i++) {
  1721. if((cmd_len == strlen(complete_params_methods[i]))
  1722. && (strncmp(&rl_line_buffer[cmd_start],
  1723. complete_params_methods[i], cmd_len)
  1724. == 0)) {
  1725. attempted_completion_state = COMPLETE_CMD_NAME;
  1726. goto end;
  1727. }
  1728. }
  1729. #ifdef USE_CFG_VARS
  1730. /* try complete_param*_cfg_grp */
  1731. for(i = 0; complete_params_cfg_var[i]; i++) {
  1732. if((cmd_len == strlen(complete_params_cfg_var[i]))
  1733. && (strncmp(&rl_line_buffer[cmd_start],
  1734. complete_params_cfg_var[i], cmd_len)
  1735. == 0)) {
  1736. attempted_completion_state = COMPLETE_CFG_GRP;
  1737. grp_start = start;
  1738. goto end;
  1739. }
  1740. }
  1741. #endif /* USE_CFG_VARS */
  1742. #ifdef USE_COUNTERS
  1743. /* try complete_param*_cfg_grp */
  1744. for(i = 0; complete_param1_counter_grp[i]; i++) {
  1745. if((cmd_len == strlen(complete_param1_counter_grp[i]))
  1746. && (strncmp(&rl_line_buffer[cmd_start],
  1747. complete_param1_counter_grp[i], cmd_len)
  1748. == 0)) {
  1749. attempted_completion_state = COMPLETE_CNT_GRP;
  1750. cnt_grp_start = start;
  1751. goto end;
  1752. }
  1753. }
  1754. #endif /* USE_COUNTERS */
  1755. } else if(crt_param_no == 2) {
  1756. #ifdef USE_CFG_VARS
  1757. /* see if we complete cfg. var names for this command */
  1758. for(i = 0; complete_params_cfg_var[i]; i++) {
  1759. if((cmd_len == strlen(complete_params_cfg_var[i]))
  1760. && (strncmp(&rl_line_buffer[cmd_start],
  1761. complete_params_cfg_var[i], cmd_len)
  1762. == 0)) {
  1763. /* get the group name: */
  1764. /* find grp_start */
  1765. for(j = cmd_end; (j < start)
  1766. && ((rl_line_buffer[j] == ' ')
  1767. || (rl_line_buffer[j] == '\t'));
  1768. j++)
  1769. ;
  1770. grp_start = j;
  1771. /* find group end / grp_len*/
  1772. for(j = grp_start; (j < start) && (rl_line_buffer[j] != ' ')
  1773. && (rl_line_buffer[j] != '\t');
  1774. j++)
  1775. ;
  1776. grp_len = j - grp_start;
  1777. for(grp = cfg_grp_lst; grp; grp = grp->next) {
  1778. if(grp_len == grp->grp_name.len
  1779. && memcmp(&rl_line_buffer[grp_start],
  1780. grp->grp_name.s, grp_len)
  1781. == 0) {
  1782. attempted_completion_state = COMPLETE_CFG_VAR;
  1783. crt_cfg_grp = grp;
  1784. goto end;
  1785. }
  1786. }
  1787. }
  1788. }
  1789. #endif /* USE_CFG_VARS */
  1790. #ifdef USE_COUNTERS
  1791. /* see if we complete counter names for this command */
  1792. for(i = 0; complete_param2_counter_name[i]; i++) {
  1793. if((cmd_len == strlen(complete_param2_counter_name[i]))
  1794. && (strncmp(&rl_line_buffer[cmd_start],
  1795. complete_param2_counter_name[i], cmd_len)
  1796. == 0)) {
  1797. /* get the group name: */
  1798. /* find grp_start */
  1799. for(j = cmd_end; (j < start)
  1800. && ((rl_line_buffer[j] == ' ')
  1801. || (rl_line_buffer[j] == '\t'));
  1802. j++)
  1803. ;
  1804. cnt_grp_start = j;
  1805. /* find group end / cnt_grp_len*/
  1806. for(j = cnt_grp_start;
  1807. (j < start) && (rl_line_buffer[j] != ' ')
  1808. && (rl_line_buffer[j] != '\t');
  1809. j++)
  1810. ;
  1811. cnt_grp_len = j - cnt_grp_start;
  1812. for(cnt_grp = cnt_grp_lst; cnt_grp;
  1813. cnt_grp = cnt_grp->next) {
  1814. if(cnt_grp_len == cnt_grp->grp_name.len
  1815. && memcmp(&rl_line_buffer[cnt_grp_start],
  1816. cnt_grp->grp_name.s, cnt_grp_len)
  1817. == 0) {
  1818. attempted_completion_state = COMPLETE_CNT_VAR;
  1819. crt_cnt_grp = cnt_grp;
  1820. goto end;
  1821. }
  1822. }
  1823. }
  1824. }
  1825. #endif /* COUNTERS */
  1826. }
  1827. attempted_completion_state = COMPLETE_NOTHING;
  1828. }
  1829. end:
  1830. return 0; /* let readline call kamcmd_generator */
  1831. }
  1832. #endif /* USE_READLINE */
  1833. /* on exit cleanup */
  1834. static void cleanup()
  1835. {
  1836. if(unix_socket) {
  1837. if(unlink(unix_socket) < 0) {
  1838. fprintf(stderr, "ERROR: failed to delete %s: %s\n", unix_socket,
  1839. strerror(errno));
  1840. }
  1841. }
  1842. }
  1843. int main(int argc, char **argv)
  1844. {
  1845. int c;
  1846. char *sock_name;
  1847. int sock_type;
  1848. int s;
  1849. struct binrpc_cmd cmd;
  1850. struct id_list *sock_id;
  1851. char *format;
  1852. char *line;
  1853. char *l;
  1854. quit = 0;
  1855. format = 0;
  1856. line = 0;
  1857. s = -1;
  1858. sock_name = 0;
  1859. sock_type = UNIXS_SOCK;
  1860. opterr = 0;
  1861. while((c = getopt(argc, argv, "UVhs:D:R:vf:")) != -1) {
  1862. switch(c) {
  1863. case 'V':
  1864. printf("version: %s\n", version);
  1865. printf("%s compiled on %s \n", __FILE__, compiled);
  1866. exit(0);
  1867. break;
  1868. case 'h':
  1869. printf("version: %s\n", version);
  1870. printf("%s", help_msg);
  1871. exit(0);
  1872. break;
  1873. case 's':
  1874. sock_name = optarg;
  1875. break;
  1876. case 'R':
  1877. reply_socket = optarg;
  1878. break;
  1879. case 'D':
  1880. sock_dir = optarg;
  1881. break;
  1882. case 'U':
  1883. sock_type = UDP_SOCK;
  1884. break;
  1885. case 'v':
  1886. verbose++;
  1887. break;
  1888. case 'f':
  1889. format = str_escape(optarg);
  1890. if(format == 0) {
  1891. fprintf(stderr, "ERROR: memory allocation failure\n");
  1892. goto error;
  1893. }
  1894. break;
  1895. case '?':
  1896. if(isprint(optopt))
  1897. fprintf(stderr, "Unknown option `-%c'.\n", optopt);
  1898. else
  1899. fprintf(stderr, "Unknown option character `\\x%x'.\n",
  1900. optopt);
  1901. goto error;
  1902. case ':':
  1903. fprintf(stderr, "Option `-%c' requires an argument.\n", optopt);
  1904. goto error;
  1905. default:
  1906. abort();
  1907. }
  1908. }
  1909. if(sock_name == 0) {
  1910. sock_name = DEFAULT_CTL_SOCKET;
  1911. }
  1912. /* init the random number generator */
  1913. srand(getpid() + time(0)); /* we don't need very strong random numbers */
  1914. if(sock_name == 0) {
  1915. fprintf(stderr, "ERROR: no server socket address specified\n");
  1916. goto error;
  1917. }
  1918. sock_id = parse_listen_id(sock_name, strlen(sock_name), sock_type);
  1919. if(sock_id == 0) {
  1920. fprintf(stderr, "ERROR: error parsing server socket address %s\n",
  1921. sock_name);
  1922. goto error;
  1923. }
  1924. switch(sock_id->proto) {
  1925. case UDP_SOCK:
  1926. case TCP_SOCK:
  1927. if(sock_id->port == 0) {
  1928. sock_id->port = DEFAULT_CTL_PORT;
  1929. /*
  1930. fprintf(stderr, "ERROR: no port specified: %s:<port>\n",
  1931. sock_name);
  1932. goto error;
  1933. */
  1934. }
  1935. if((s = connect_tcpudp_socket(sock_id->name, sock_id->port,
  1936. (sock_id->proto == UDP_SOCK) ? SOCK_DGRAM
  1937. : SOCK_STREAM))
  1938. < 0) {
  1939. goto error;
  1940. }
  1941. break;
  1942. case UNIXS_SOCK:
  1943. case UNIXD_SOCK:
  1944. if((s = connect_unix_sock(sock_id->name,
  1945. (sock_id->proto == UNIXD_SOCK) ? SOCK_DGRAM
  1946. : SOCK_STREAM))
  1947. < 0)
  1948. goto error;
  1949. break;
  1950. case UNKNOWN_SOCK:
  1951. fprintf(stderr, "ERROR: Bad socket type for %s\n", sock_name);
  1952. goto error;
  1953. }
  1954. free(sock_id->buf); /* not needed anymore */
  1955. free(sock_id); /* not needed anymore */
  1956. sock_id = 0;
  1957. if(optind >= argc) {
  1958. /*fprintf(stderr, "ERROR: no command specified\n");
  1959. goto error; */
  1960. } else {
  1961. if(parse_cmd(&cmd, &argv[optind], argc - optind) < 0)
  1962. goto error;
  1963. if(run_cmd(s, &cmd, format) < 0)
  1964. goto error;
  1965. goto end;
  1966. }
  1967. /* interactive mode */
  1968. if(get_kamcmd_list(s) == 0) {
  1969. #ifdef USE_CFG_VARS
  1970. get_cfgvars_list(s);
  1971. #endif /* USE_CFG_VARS */
  1972. #ifdef USE_COUNTERS
  1973. get_counters_list(s);
  1974. #endif /* USE_COUNTERS */
  1975. }
  1976. /* banners */
  1977. printf("%s %s\n", NAME, VERSION);
  1978. printf("%s\n", COPYRIGHT);
  1979. printf("%s\n", DISCLAIMER);
  1980. #ifdef USE_READLINE
  1981. /* initialize readline */
  1982. /* allow conditional parsing of the ~/.inputrc file*/
  1983. rl_readline_name = NAME;
  1984. rl_completion_entry_function = kamcmd_generator;
  1985. rl_attempted_completion_function = kamcmd_completion;
  1986. while(!quit) {
  1987. line = readline(NAME "> ");
  1988. if(line == 0) /* EOF */
  1989. break;
  1990. l = trim_ws(line); /* trim whitespace */
  1991. if(*l) {
  1992. add_history(l);
  1993. run_line(s, l, format);
  1994. }
  1995. free(line);
  1996. line = 0;
  1997. }
  1998. #else
  1999. line = malloc(MAX_LINE_SIZE);
  2000. if(line == 0) {
  2001. fprintf(stderr, "memory allocation error\n");
  2002. goto error;
  2003. }
  2004. printf(NAME "> ");
  2005. fflush(stdout); /* prompt */
  2006. while(!quit && fgets(line, MAX_LINE_SIZE, stdin)) {
  2007. l = trim_ws(line);
  2008. if(*l) {
  2009. run_line(s, l, format);
  2010. }
  2011. printf(NAME "> ");
  2012. fflush(stdout); /* prompt */
  2013. };
  2014. free(line);
  2015. line = 0;
  2016. #endif /* USE_READLINE */
  2017. end:
  2018. /* normal exit */
  2019. if(line)
  2020. free(line);
  2021. if(format)
  2022. free(format);
  2023. if(rpc_array)
  2024. free_rpc_array(rpc_array, rpc_no);
  2025. #ifdef USE_CFG_VARS
  2026. if(cfg_grp_lst)
  2027. free_cfg_grp_lst();
  2028. if(cfg_vars_array) {
  2029. free_rpc_array(cfg_vars_array, cfg_vars_no);
  2030. cfg_vars_array = 0;
  2031. cfg_vars_no = 0;
  2032. }
  2033. #endif /* USE_CFG_VARS */
  2034. #ifdef USE_COUNTERS
  2035. if(cnt_grp_lst)
  2036. free_cnt_grp_lst();
  2037. if(cnt_grps_array) {
  2038. free_rpc_array(cnt_grps_array, cnt_grps_no);
  2039. cnt_grps_array = 0;
  2040. cnt_grps_no = 0;
  2041. }
  2042. #endif /* USE_COUNTERS */
  2043. cleanup();
  2044. exit(0);
  2045. error:
  2046. if(line)
  2047. free(line);
  2048. if(format)
  2049. free(format);
  2050. if(rpc_array)
  2051. free_rpc_array(rpc_array, rpc_no);
  2052. #ifdef USE_CFG_VARS
  2053. if(cfg_grp_lst)
  2054. free_cfg_grp_lst();
  2055. if(cfg_vars_array) {
  2056. free_rpc_array(cfg_vars_array, cfg_vars_no);
  2057. cfg_vars_array = 0;
  2058. cfg_vars_no = 0;
  2059. }
  2060. #endif /* USE_CFG_VARS */
  2061. #ifdef USE_COUNTERS
  2062. if(cnt_grp_lst)
  2063. free_cnt_grp_lst();
  2064. if(cnt_grps_array) {
  2065. free_rpc_array(cnt_grps_array, cnt_grps_no);
  2066. cnt_grps_array = 0;
  2067. cnt_grps_no = 0;
  2068. }
  2069. #endif /* USE_COUNTERS */
  2070. cleanup();
  2071. exit(-1);
  2072. }