kamcmd.c 50 KB

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