PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/snapshot_20100419/openslp/slptool/slptool.c

#
C | 585 lines | 465 code | 66 blank | 54 comment | 134 complexity | e3141d6a2262f3071e9fe58693551fe3 MD5 | raw file
Possible License(s): BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*-------------------------------------------------------------------------
  2. * Copyright (C) 2000 Caldera Systems, Inc
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * Neither the name of Caldera Systems nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * `AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE CALDERA
  24. * SYSTEMS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  28. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *-------------------------------------------------------------------------*/
  32. /** OpenSLP command line User Agent wrapper.
  33. *
  34. * This file contains code that provide command line access to all OpenSLP
  35. * User Agent library functionality.
  36. *
  37. * @file slptool.c
  38. * @author Matthew Peterson, John Calcote (jcalcote@novell.com)
  39. * @attention Please submit patches to http://www.openslp.org
  40. * @ingroup SlpToolCode
  41. */
  42. #include "slptool.h"
  43. #ifndef _WIN32
  44. # ifndef HAVE_STRCASECMP
  45. static int strncasecmp(const char * s1, const char * s2, size_t len)
  46. {
  47. while (len && *s1 && *s2 && ((*s1 ^ *s2) & ~0x20) == 0)
  48. len--, s1++, s2++;
  49. return len? (int)(*(unsigned char *)s1 - *(unsigned char *)s2): 0;
  50. }
  51. static int strcasecmp(const char * s1, const char * s2)
  52. {
  53. while (*s1 && *s2 && ((*s1 ^ *s2) & ~0x20) == 0)
  54. s1++, s2++;
  55. return (int)(*(unsigned char *)s1 - *(unsigned char *)s2);
  56. }
  57. # endif
  58. #endif
  59. static SLPBoolean mySrvTypeCallback(SLPHandle hslp,
  60. const char * srvtypes, SLPError errcode, void * cookie)
  61. {
  62. char * cpy;
  63. char * slider1;
  64. char * slider2;
  65. (void)hslp;
  66. (void)cookie;
  67. if (errcode == SLP_OK && *srvtypes)
  68. {
  69. cpy = strdup(srvtypes);
  70. if (cpy)
  71. {
  72. slider1 = slider2 = cpy;
  73. slider1 = strchr(slider2, ',');
  74. while (slider1)
  75. {
  76. *slider1 = 0;
  77. printf("%s\n", slider2);
  78. slider1 ++;
  79. slider2 = slider1;
  80. slider1 = strchr(slider2, ',');
  81. }
  82. /* print the final itam */
  83. printf("%s\n", slider2);
  84. free(cpy);
  85. }
  86. }
  87. return SLP_TRUE;
  88. }
  89. void FindSrvTypes(SLPToolCommandLine * cmdline)
  90. {
  91. SLPError result;
  92. SLPHandle hslp;
  93. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  94. {
  95. #ifndef UNICAST_NOT_SUPPORTED
  96. if (cmdline->unicastifc && (result = SLPAssociateIP(hslp,
  97. cmdline->unicastifc)) != SLP_OK)
  98. {
  99. printf("errorcode: %i\n", result);
  100. SLPClose(hslp);
  101. return;
  102. }
  103. #endif
  104. #ifndef MI_NOT_SUPPORTED
  105. if (cmdline->interfaces && (result = SLPAssociateIFList(hslp,
  106. cmdline->interfaces)) != SLP_OK)
  107. {
  108. printf("errorcode: %i\n", result);
  109. SLPClose(hslp);
  110. return;
  111. }
  112. #endif
  113. if (cmdline->cmdparam1)
  114. result = SLPFindSrvTypes(hslp, cmdline->cmdparam1, cmdline->scopes,
  115. mySrvTypeCallback, 0);
  116. else
  117. result = SLPFindSrvTypes(hslp, "*", cmdline->scopes,
  118. mySrvTypeCallback, 0);
  119. if (result != SLP_OK)
  120. printf("errorcode: %i\n", result);
  121. SLPClose(hslp);
  122. }
  123. }
  124. static SLPBoolean myAttrCallback(SLPHandle hslp,
  125. const char * attrlist, SLPError errcode, void * cookie)
  126. {
  127. (void)hslp;
  128. (void)cookie;
  129. if (errcode == SLP_OK)
  130. printf("%s\n", attrlist);
  131. return SLP_TRUE;
  132. }
  133. void FindAttrs(SLPToolCommandLine * cmdline)
  134. {
  135. SLPError result;
  136. SLPHandle hslp;
  137. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  138. {
  139. #ifndef UNICAST_NOT_SUPPORTED
  140. if (cmdline->unicastifc && (result = SLPAssociateIP(hslp,
  141. cmdline->unicastifc)) != SLP_OK)
  142. {
  143. printf("errorcode: %i\n", result);
  144. SLPClose(hslp);
  145. return;
  146. }
  147. #endif
  148. #ifndef MI_NOT_SUPPORTED
  149. if (cmdline->interfaces && (result = SLPAssociateIFList(hslp,
  150. cmdline->interfaces)) != SLP_OK)
  151. {
  152. printf("errorcode: %i\n", result);
  153. SLPClose(hslp);
  154. return;
  155. }
  156. #endif
  157. result = SLPFindAttrs(hslp, cmdline->cmdparam1, cmdline->scopes,
  158. cmdline->cmdparam2, myAttrCallback, 0);
  159. if (result != SLP_OK)
  160. printf("errorcode: %i\n", result);
  161. SLPClose(hslp);
  162. }
  163. }
  164. static SLPBoolean mySrvUrlCallback(SLPHandle hslp, const char * srvurl,
  165. unsigned short lifetime, SLPError errcode, void * cookie)
  166. {
  167. (void)hslp;
  168. (void)cookie;
  169. if (errcode == SLP_OK)
  170. printf("%s,%i\n", srvurl, lifetime);
  171. return SLP_TRUE;
  172. }
  173. void FindSrvs(SLPToolCommandLine * cmdline)
  174. {
  175. SLPError result;
  176. SLPHandle hslp;
  177. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  178. {
  179. #ifndef UNICAST_NOT_SUPPORTED
  180. if (cmdline->unicastifc && (result = SLPAssociateIP(hslp,
  181. cmdline->unicastifc)) != SLP_OK)
  182. {
  183. printf("errorcode: %i\n", result);
  184. SLPClose(hslp);
  185. return;
  186. }
  187. #endif
  188. #ifndef MI_NOT_SUPPORTED
  189. if (cmdline->interfaces && (result = SLPAssociateIFList(hslp,
  190. cmdline->interfaces)) != SLP_OK)
  191. {
  192. printf("errorcode: %i\n", result);
  193. SLPClose(hslp);
  194. return;
  195. }
  196. #endif
  197. result = SLPFindSrvs(hslp, cmdline->cmdparam1, cmdline->scopes,
  198. cmdline->cmdparam2, mySrvUrlCallback, 0);
  199. if (result != SLP_OK)
  200. printf("errorcode: %i\n", result);
  201. SLPClose(hslp);
  202. }
  203. }
  204. void FindScopes(SLPToolCommandLine * cmdline)
  205. {
  206. SLPError result;
  207. SLPHandle hslp;
  208. char * scopes;
  209. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  210. {
  211. result = SLPFindScopes(hslp, &scopes);
  212. if (result == SLP_OK)
  213. {
  214. printf("%s\n", scopes);
  215. SLPFree(scopes);
  216. }
  217. SLPClose(hslp);
  218. }
  219. }
  220. static void mySLPRegReport(SLPHandle hslp, SLPError errcode,
  221. void * cookie)
  222. {
  223. (void)hslp;
  224. (void)cookie;
  225. if (errcode)
  226. printf("(de)registration errorcode %d\n", errcode);
  227. }
  228. void Register(SLPToolCommandLine * cmdline)
  229. {
  230. SLPError result;
  231. SLPHandle hslp;
  232. char srvtype[80] = "", * s;
  233. size_t len = 0;
  234. if (strncasecmp(cmdline->cmdparam1, "service:", 8) == 0)
  235. len = 8;
  236. s = strchr(cmdline->cmdparam1 + len, ':');
  237. if (!s)
  238. {
  239. printf("Invalid URL: %s\n", cmdline->cmdparam1);
  240. return;
  241. }
  242. len = s - cmdline->cmdparam1;
  243. strncpy(srvtype, cmdline->cmdparam1, len);
  244. srvtype[len] = 0;
  245. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  246. {
  247. result = SLPReg(hslp, cmdline->cmdparam1, SLP_LIFETIME_DEFAULT, srvtype,
  248. cmdline->cmdparam2, SLP_TRUE, mySLPRegReport, 0);
  249. if (result != SLP_OK)
  250. printf("errorcode: %i\n", result);
  251. SLPClose(hslp);
  252. }
  253. }
  254. void Deregister(SLPToolCommandLine * cmdline)
  255. {
  256. SLPError result;
  257. SLPHandle hslp;
  258. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  259. {
  260. result = SLPDereg(hslp, cmdline->cmdparam1, mySLPRegReport, 0);
  261. if (result != SLP_OK)
  262. printf("errorcode: %i\n", result);
  263. SLPClose(hslp);
  264. }
  265. }
  266. void PrintVersion(SLPToolCommandLine * cmdline)
  267. {
  268. (void)cmdline;
  269. printf("slptool version = %s\n", SLP_VERSION);
  270. printf("libslp version = %s\n", SLPGetProperty("net.slp.OpenSLPVersion"));
  271. printf("libslp configuration file = %s\n",
  272. SLPGetProperty("net.slp.OpenSLPConfigFile"));
  273. }
  274. void GetProperty(SLPToolCommandLine * cmdline)
  275. {
  276. const char * propertyValue;
  277. propertyValue = SLPGetProperty(cmdline->cmdparam1);
  278. printf("%s = %s\n", cmdline->cmdparam1,
  279. propertyValue == 0 ? "" : propertyValue);
  280. }
  281. /* Returns Zero on success. Non-zero on error. */
  282. int ParseCommandLine(int argc, char * argv[], SLPToolCommandLine * cmdline)
  283. {
  284. int i;
  285. if (argc < 2)
  286. return 1; /* not enough arguments */
  287. for (i = 1; i < argc; i++)
  288. {
  289. if (strcasecmp(argv[i], "-v") == 0
  290. || strcasecmp(argv[i], "--version") == 0)
  291. {
  292. if (i < argc)
  293. {
  294. cmdline->cmd = PRINT_VERSION;
  295. return 0;
  296. }
  297. else
  298. return 1;
  299. }
  300. else if (strcasecmp(argv[i], "-s") == 0
  301. || strcasecmp(argv[i], "--scopes") == 0)
  302. {
  303. i++;
  304. if (i < argc)
  305. cmdline->scopes = argv[i];
  306. else
  307. return 1;
  308. }
  309. else if (strcasecmp(argv[i], "-l") == 0
  310. || strcasecmp(argv[i], "--language") == 0)
  311. {
  312. i++;
  313. if (i < argc)
  314. cmdline->lang = argv[i];
  315. else
  316. return 1;
  317. }
  318. #ifndef MI_NOT_SUPPORTED
  319. else if (strcasecmp(argv[i], "-i") == 0
  320. || strcasecmp(argv[i], "--interfaces") == 0)
  321. {
  322. if (cmdline->unicastifc != 0)
  323. {
  324. printf("slptool: Can't use -i and -u together.\n");
  325. return -1;
  326. }
  327. i++;
  328. if (i < argc)
  329. cmdline->interfaces = argv[i];
  330. else
  331. return 1;
  332. }
  333. #endif
  334. #ifndef UNICAST_NOT_SUPPORTED
  335. else if (strcasecmp(argv[i], "-u") == 0
  336. || strcasecmp(argv[i], "--unicastifc") == 0)
  337. {
  338. if (cmdline->interfaces != 0)
  339. {
  340. printf("slptool: Can't use -i and -u together.\n");
  341. return -1;
  342. }
  343. i++;
  344. if (i < argc)
  345. cmdline->unicastifc = argv[i];
  346. else
  347. return 1;
  348. }
  349. #endif
  350. else if (strcasecmp(argv[i], "findsrvs") == 0)
  351. {
  352. cmdline->cmd = FINDSRVS;
  353. /* service type */
  354. i++;
  355. if (i < argc)
  356. cmdline->cmdparam1 = argv[i];
  357. else
  358. return 1;
  359. /* (optional) filter */
  360. i++;
  361. if (i < argc)
  362. cmdline->cmdparam2 = argv[i];
  363. break;
  364. }
  365. else if (strcasecmp(argv[i], "findattrs") == 0)
  366. {
  367. cmdline->cmd = FINDATTRS;
  368. /* url or service type */
  369. i++;
  370. if (i < argc)
  371. cmdline->cmdparam1 = argv[i];
  372. else
  373. return 1;
  374. /* (optional) attrids */
  375. i++;
  376. if (i < argc)
  377. cmdline->cmdparam2 = argv[i];
  378. }
  379. else if (strcasecmp(argv[i], "findsrvtypes") == 0)
  380. {
  381. cmdline->cmd = FINDSRVTYPES;
  382. /* (optional) naming authority */
  383. i++;
  384. if (i < argc)
  385. cmdline->cmdparam1 = argv[i];
  386. }
  387. else if (strcasecmp(argv[i], "findscopes") == 0)
  388. cmdline->cmd = FINDSCOPES;
  389. else if (strcasecmp(argv[i], "register") == 0)
  390. {
  391. cmdline->cmd = REGISTER;
  392. /* url */
  393. i++;
  394. if (i < argc)
  395. cmdline->cmdparam1 = argv[i];
  396. else
  397. return 1;
  398. /* Optional attrids */
  399. i++;
  400. if (i < argc)
  401. cmdline->cmdparam2 = argv[i];
  402. else
  403. cmdline->cmdparam2 = cmdline->cmdparam1
  404. + strlen(cmdline->cmdparam1);
  405. break;
  406. }
  407. else if (strcasecmp(argv[i], "deregister") == 0)
  408. {
  409. cmdline->cmd = DEREGISTER;
  410. /* url */
  411. i++;
  412. if (i < argc)
  413. cmdline->cmdparam1 = argv[i];
  414. else
  415. return 1;
  416. }
  417. else if (strcasecmp(argv[i], "getproperty") == 0)
  418. {
  419. cmdline->cmd = GETPROPERTY;
  420. i++;
  421. if (i < argc)
  422. cmdline->cmdparam1 = argv[i];
  423. else
  424. return 1;
  425. }
  426. else
  427. return 1;
  428. }
  429. return 0;
  430. }
  431. void DisplayUsage()
  432. {
  433. printf("Usage: slptool [options] command-and-arguments \n");
  434. printf(" options may be:\n");
  435. printf(" -v (or --version) displays the versions of slptool and OpenSLP.\n");
  436. printf(" -s (or --scope) followed by a comma-separated list of scopes.\n");
  437. printf(" -l (or --language) followed by a language tag.\n");
  438. #ifndef MI_NOT_SUPPORTED
  439. printf(" -i (or --interfaces) followed by a comma-separated list of interfaces.\n");
  440. #endif /* MI_NOT_SUPPORTED */
  441. #ifndef UNICAST_NOT_SUPPORTED
  442. printf(" -u (or --unicastifc) followed by a single interface.\n");
  443. #endif
  444. printf("\n");
  445. printf(" command-and-arguments may be:\n");
  446. printf(" findsrvs service-type [filter]\n");
  447. printf(" findattrs url [attrids]\n");
  448. printf(" findsrvtypes [authority]\n");
  449. printf(" findscopes\n");
  450. printf(" register url [attrs]\n");
  451. printf(" deregister url\n");
  452. printf(" getproperty propertyname\n");
  453. printf("\n");
  454. printf("Examples:\n");
  455. printf(" slptool register service:myserv.x://myhost.com \"(attr1=val1),(attr2=val2)\"\n");
  456. printf(" slptool findsrvs service:myserv.x\n");
  457. printf(" slptool findsrvs service:myserv.x \"(attr1=val1)\"\n");
  458. #ifndef MI_NOT_SUPPORTED
  459. printf(" slptool -i 10.77.13.240,192.168.250.240 findsrvs service:myserv.x\n");
  460. #endif /* MI_NOT_SUPPORTED */
  461. #ifndef UNICAST_NOT_SUPPORTED
  462. printf(" slptool -u 10.77.13.237 findsrvs service:myserv.x \"(attr1=val1)\"\n");
  463. #endif
  464. printf(" slptool findattrs service:myserv.x://myhost.com\n");
  465. printf(" slptool findattrs service:myserv.x://myhost.com attr1\n");
  466. #ifndef MI_NOT_SUPPORTED
  467. printf(" slptool -i 10.77.13.243 findattrs service:myserv.x://myhost.com attr1\n");
  468. #endif /* MI_NOT_SUPPORTED */
  469. #ifndef UNICAST_NOT_SUPPORTED
  470. printf(" slptool -u 10.77.13.237 findattrs service:myserv.x://myhost.com attr1 \n");
  471. #endif
  472. printf(" slptool deregister service:myserv.x://myhost.com\n");
  473. printf(" slptool getproperty net.slp.useScopes\n");
  474. }
  475. int main(int argc, char * argv[])
  476. {
  477. int result;
  478. SLPToolCommandLine cmdline;
  479. /* zero out the cmdline */
  480. memset(&cmdline, 0, sizeof(cmdline));
  481. /* Parse the command line */
  482. if (ParseCommandLine(argc, argv, &cmdline) == 0)
  483. switch (cmdline.cmd)
  484. {
  485. case FINDSRVS:
  486. FindSrvs(&cmdline);
  487. break;
  488. case FINDATTRS:
  489. FindAttrs(&cmdline);
  490. break;
  491. case FINDSRVTYPES:
  492. FindSrvTypes(&cmdline);
  493. break;
  494. case FINDSCOPES:
  495. FindScopes(&cmdline);
  496. break;
  497. case GETPROPERTY:
  498. GetProperty(&cmdline);
  499. break;
  500. case REGISTER:
  501. Register(&cmdline);
  502. break;
  503. case DEREGISTER:
  504. Deregister(&cmdline);
  505. break;
  506. case PRINT_VERSION:
  507. PrintVersion(&cmdline);
  508. break;
  509. case DUMMY:
  510. break;
  511. }
  512. else
  513. {
  514. DisplayUsage();
  515. result = 1;
  516. }
  517. return 0;
  518. }
  519. /*=========================================================================*/