sercmd.c 54 KB

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