PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/openslp/slptool/slptool.c

#
C | 607 lines | 484 code | 68 blank | 55 comment | 143 complexity | cea0d93a9273bb66e29c484d6114d655 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. unsigned int lt = 0;
  235. if (cmdline->time) {
  236. lt = atoi(cmdline->time);
  237. }
  238. if (strncasecmp(cmdline->cmdparam1, "service:", 8) == 0)
  239. len = 8;
  240. s = strchr(cmdline->cmdparam1 + len, ':');
  241. if (!s)
  242. {
  243. printf("Invalid URL: %s\n", cmdline->cmdparam1);
  244. return;
  245. }
  246. len = s - cmdline->cmdparam1;
  247. strncpy(srvtype, cmdline->cmdparam1, len);
  248. srvtype[len] = 0;
  249. /* Clear property (if set), otherwise the register function is quite useless */
  250. SLPSetProperty("net.slp.watchRegistrationPID", 0);
  251. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  252. {
  253. if (!lt || lt > SLP_LIFETIME_MAXIMUM)
  254. result = SLPReg(hslp, cmdline->cmdparam1, SLP_LIFETIME_MAXIMUM, srvtype,
  255. cmdline->cmdparam2, SLP_TRUE, mySLPRegReport, 0);
  256. else
  257. result = SLPReg(hslp, cmdline->cmdparam1, (unsigned short)lt, srvtype,
  258. cmdline->cmdparam2, SLP_TRUE, mySLPRegReport, 0);
  259. if (result != SLP_OK)
  260. printf("errorcode: %i\n", result);
  261. SLPClose(hslp);
  262. }
  263. }
  264. void Deregister(SLPToolCommandLine * cmdline)
  265. {
  266. SLPError result;
  267. SLPHandle hslp;
  268. if (SLPOpen(cmdline->lang, SLP_FALSE, &hslp) == SLP_OK)
  269. {
  270. result = SLPDereg(hslp, cmdline->cmdparam1, mySLPRegReport, 0);
  271. if (result != SLP_OK)
  272. printf("errorcode: %i\n", result);
  273. SLPClose(hslp);
  274. }
  275. }
  276. void PrintVersion(SLPToolCommandLine * cmdline)
  277. {
  278. (void)cmdline;
  279. printf("slptool version = %s\n", SLP_VERSION);
  280. printf("libslp version = %s\n", SLPGetProperty("net.slp.OpenSLPVersion"));
  281. printf("libslp configuration file = %s\n",
  282. SLPGetProperty("net.slp.OpenSLPConfigFile"));
  283. }
  284. void GetProperty(SLPToolCommandLine * cmdline)
  285. {
  286. const char * propertyValue;
  287. propertyValue = SLPGetProperty(cmdline->cmdparam1);
  288. printf("%s = %s\n", cmdline->cmdparam1,
  289. propertyValue == 0 ? "" : propertyValue);
  290. }
  291. /* Returns Zero on success. Non-zero on error. */
  292. int ParseCommandLine(int argc, char * argv[], SLPToolCommandLine * cmdline)
  293. {
  294. int i;
  295. if (argc < 2)
  296. return 1; /* not enough arguments */
  297. for (i = 1; i < argc; i++)
  298. {
  299. if (strcasecmp(argv[i], "-v") == 0
  300. || strcasecmp(argv[i], "--version") == 0)
  301. {
  302. if (i < argc)
  303. {
  304. cmdline->cmd = PRINT_VERSION;
  305. return 0;
  306. }
  307. else
  308. return 1;
  309. }
  310. else if (strcasecmp(argv[i], "-s") == 0
  311. || strcasecmp(argv[i], "--scopes") == 0)
  312. {
  313. i++;
  314. if (i < argc)
  315. cmdline->scopes = argv[i];
  316. else
  317. return 1;
  318. }
  319. else if (strcasecmp(argv[i], "-l") == 0
  320. || strcasecmp(argv[i], "--language") == 0)
  321. {
  322. i++;
  323. if (i < argc)
  324. cmdline->lang = argv[i];
  325. else
  326. return 1;
  327. }
  328. else if (strcasecmp(argv[i], "-t") == 0
  329. || strcasecmp(argv[i], "--time") == 0)
  330. {
  331. i++;
  332. if (i < argc)
  333. cmdline->time = argv[i];
  334. else
  335. return 1;
  336. }
  337. #ifndef MI_NOT_SUPPORTED
  338. else if (strcasecmp(argv[i], "-i") == 0
  339. || strcasecmp(argv[i], "--interfaces") == 0)
  340. {
  341. if (cmdline->unicastifc != 0)
  342. {
  343. printf("slptool: Can't use -i and -u together.\n");
  344. return -1;
  345. }
  346. i++;
  347. if (i < argc)
  348. cmdline->interfaces = argv[i];
  349. else
  350. return 1;
  351. }
  352. #endif
  353. #ifndef UNICAST_NOT_SUPPORTED
  354. else if (strcasecmp(argv[i], "-u") == 0
  355. || strcasecmp(argv[i], "--unicastifc") == 0)
  356. {
  357. if (cmdline->interfaces != 0)
  358. {
  359. printf("slptool: Can't use -i and -u together.\n");
  360. return -1;
  361. }
  362. i++;
  363. if (i < argc)
  364. cmdline->unicastifc = argv[i];
  365. else
  366. return 1;
  367. }
  368. #endif
  369. else if (strcasecmp(argv[i], "findsrvs") == 0)
  370. {
  371. cmdline->cmd = FINDSRVS;
  372. /* service type */
  373. i++;
  374. if (i < argc)
  375. cmdline->cmdparam1 = argv[i];
  376. else
  377. return 1;
  378. /* (optional) filter */
  379. i++;
  380. if (i < argc)
  381. cmdline->cmdparam2 = argv[i];
  382. break;
  383. }
  384. else if (strcasecmp(argv[i], "findattrs") == 0)
  385. {
  386. cmdline->cmd = FINDATTRS;
  387. /* url or service type */
  388. i++;
  389. if (i < argc)
  390. cmdline->cmdparam1 = argv[i];
  391. else
  392. return 1;
  393. /* (optional) attrids */
  394. i++;
  395. if (i < argc)
  396. cmdline->cmdparam2 = argv[i];
  397. }
  398. else if (strcasecmp(argv[i], "findsrvtypes") == 0)
  399. {
  400. cmdline->cmd = FINDSRVTYPES;
  401. /* (optional) naming authority */
  402. i++;
  403. if (i < argc)
  404. cmdline->cmdparam1 = argv[i];
  405. }
  406. else if (strcasecmp(argv[i], "findscopes") == 0)
  407. cmdline->cmd = FINDSCOPES;
  408. else if (strcasecmp(argv[i], "register") == 0)
  409. {
  410. cmdline->cmd = REGISTER;
  411. /* url */
  412. i++;
  413. if (i < argc)
  414. cmdline->cmdparam1 = argv[i];
  415. else
  416. return 1;
  417. /* Optional attrids */
  418. i++;
  419. if (i < argc)
  420. cmdline->cmdparam2 = argv[i];
  421. else
  422. cmdline->cmdparam2 = cmdline->cmdparam1
  423. + strlen(cmdline->cmdparam1);
  424. break;
  425. }
  426. else if (strcasecmp(argv[i], "deregister") == 0)
  427. {
  428. cmdline->cmd = DEREGISTER;
  429. /* url */
  430. i++;
  431. if (i < argc)
  432. cmdline->cmdparam1 = argv[i];
  433. else
  434. return 1;
  435. }
  436. else if (strcasecmp(argv[i], "getproperty") == 0)
  437. {
  438. cmdline->cmd = GETPROPERTY;
  439. i++;
  440. if (i < argc)
  441. cmdline->cmdparam1 = argv[i];
  442. else
  443. return 1;
  444. }
  445. else
  446. return 1;
  447. }
  448. return 0;
  449. }
  450. void DisplayUsage()
  451. {
  452. printf("Usage: slptool [options] command-and-arguments \n");
  453. printf(" options may be:\n");
  454. printf(" -v (or --version) displays the versions of slptool and OpenSLP.\n");
  455. printf(" -s (or --scope) followed by a comma-separated list of scopes.\n");
  456. printf(" -l (or --language) followed by a language tag.\n");
  457. printf(" -t (or --time) followed by a lifetime tag.\n");
  458. #ifndef MI_NOT_SUPPORTED
  459. printf(" -i (or --interfaces) followed by a comma-separated list of interfaces.\n");
  460. #endif /* MI_NOT_SUPPORTED */
  461. #ifndef UNICAST_NOT_SUPPORTED
  462. printf(" -u (or --unicastifc) followed by a single interface.\n");
  463. #endif
  464. printf("\n");
  465. printf(" command-and-arguments may be:\n");
  466. printf(" findsrvs service-type [filter]\n");
  467. printf(" findattrs url [attrids]\n");
  468. printf(" findsrvtypes [authority]\n");
  469. printf(" findscopes\n");
  470. printf(" register url [attrs]\n");
  471. printf(" deregister url\n");
  472. printf(" getproperty propertyname\n");
  473. printf("\n");
  474. printf("Examples:\n");
  475. printf(" slptool register service:myserv.x://myhost.com \"(attr1=val1),(attr2=val2)\"\n");
  476. printf(" slptool findsrvs service:myserv.x\n");
  477. printf(" slptool findsrvs service:myserv.x \"(attr1=val1)\"\n");
  478. #ifndef MI_NOT_SUPPORTED
  479. printf(" slptool -i 10.77.13.240,192.168.250.240 findsrvs service:myserv.x\n");
  480. #endif /* MI_NOT_SUPPORTED */
  481. #ifndef UNICAST_NOT_SUPPORTED
  482. printf(" slptool -u 10.77.13.237 findsrvs service:myserv.x \"(attr1=val1)\"\n");
  483. #endif
  484. printf(" slptool findattrs service:myserv.x://myhost.com\n");
  485. printf(" slptool findattrs service:myserv.x://myhost.com attr1\n");
  486. #ifndef MI_NOT_SUPPORTED
  487. printf(" slptool -i 10.77.13.243 findattrs service:myserv.x://myhost.com attr1\n");
  488. #endif /* MI_NOT_SUPPORTED */
  489. #ifndef UNICAST_NOT_SUPPORTED
  490. printf(" slptool -u 10.77.13.237 findattrs service:myserv.x://myhost.com attr1 \n");
  491. #endif
  492. printf(" slptool deregister service:myserv.x://myhost.com\n");
  493. printf(" slptool getproperty net.slp.useScopes\n");
  494. }
  495. int main(int argc, char * argv[])
  496. {
  497. int result;
  498. SLPToolCommandLine cmdline;
  499. /* zero out the cmdline */
  500. memset(&cmdline, 0, sizeof(cmdline));
  501. /* Parse the command line */
  502. if (ParseCommandLine(argc, argv, &cmdline) == 0)
  503. switch (cmdline.cmd)
  504. {
  505. case FINDSRVS:
  506. FindSrvs(&cmdline);
  507. break;
  508. case FINDATTRS:
  509. FindAttrs(&cmdline);
  510. break;
  511. case FINDSRVTYPES:
  512. FindSrvTypes(&cmdline);
  513. break;
  514. case FINDSCOPES:
  515. FindScopes(&cmdline);
  516. break;
  517. case GETPROPERTY:
  518. GetProperty(&cmdline);
  519. break;
  520. case REGISTER:
  521. Register(&cmdline);
  522. break;
  523. case DEREGISTER:
  524. Deregister(&cmdline);
  525. break;
  526. case PRINT_VERSION:
  527. PrintVersion(&cmdline);
  528. break;
  529. case DUMMY:
  530. break;
  531. }
  532. else
  533. {
  534. DisplayUsage();
  535. result = 1;
  536. }
  537. return 0;
  538. }
  539. /*=========================================================================*/