pubsub_opts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2018 IBM Corp.
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial contribution
  15. *******************************************************************************/
  16. #include "pubsub_opts.h"
  17. #include <string.h>
  18. #include <stdlib.h>
  19. int printVersionInfo(pubsub_opts_nameValue* info)
  20. {
  21. int rc = 0;
  22. printf("\nLibrary information:\n");
  23. while (info->name)
  24. {
  25. printf("%s: %s\n", info->name, info->value);
  26. info++;
  27. rc = 1; /* at least one value printed */
  28. }
  29. if (rc == 1)
  30. printf("\n");
  31. return rc;
  32. }
  33. void usage(struct pubsub_opts* opts, pubsub_opts_nameValue* name_values, const char* program_name)
  34. {
  35. printf("Eclipse Paho MQTT C %s\n", opts->publisher ? "publisher" : "subscriber");
  36. printVersionInfo(name_values);
  37. printf("Usage: %s [topicname] [-t topic] [-c connection] [-h host] [-p port]\n"
  38. " [-q qos] [-i clientid] [-u username] [-P password] [-k keepalive_timeout]\n"
  39. , program_name);
  40. printf(" [-V MQTT-version] [--quiet] [--trace trace-level]\n");
  41. if (opts->publisher)
  42. {
  43. printf(" [-r] [-n] [-m message] [-f filename]\n");
  44. printf(" [--maxdatalen len] [--message-expiry seconds] [--user-property name value]\n");
  45. }
  46. else
  47. printf(" [-R] [--no-delimiter]\n");
  48. printf(" [--will-topic topic] [--will-payload message] [--will-qos qos] [--will-retain]\n");
  49. printf(" [--cafile filename] [--capath dirname] [--cert filename] [--key filename]\n"
  50. " [--keypass string] [--ciphers string] [--insecure]");
  51. printf(
  52. "\n\n -t (--topic) : MQTT topic to %s to\n"
  53. " -c (--connection) : connection string, overrides host/port e.g wss://hostname:port/ws. Use this option\n"
  54. " rather than host/port to connect with TLS and/or web sockets. No default.\n"
  55. " -h (--host) : host to connect to. Default is %s.\n"
  56. " -p (--port) : network port to connect to. Default is %s.\n"
  57. " -q (--qos) : MQTT QoS to %s with (0, 1 or 2). Default is %d.\n"
  58. " -V (--MQTTversion) : MQTT version (31, 311, or 5). Default is 311.\n"
  59. " --quiet : do not print error messages.\n"
  60. " --trace : print internal trace (\"error\", \"min\", \"max\" or \"protocol\").\n",
  61. opts->publisher ? "publish" : "subscribe", opts->host, opts->port,
  62. opts->publisher ? "publish" : "subscribe", opts->qos);
  63. if (opts->publisher)
  64. {
  65. printf(" -r (--retained) : use MQTT retain option. Default is off.\n");
  66. printf(" -n (--null-message) : send 0-length message.\n");
  67. printf(" -m (--message) : the payload to send.\n");
  68. printf(" -f (--filename) : use the contents of the named file as the payload.\n");
  69. }
  70. printf(
  71. " -i (--clientid) : MQTT client id. Default is %s.\n"
  72. " -u (--username) : MQTT username. No default.\n"
  73. " -P (--password) : MQTT password. No default.\n"
  74. " -k (--keepalive) : MQTT keepalive timeout value. Default is %d seconds.\n"
  75. " --delimiter : delimiter string. Default is \\n.\n",
  76. opts->clientid, opts->keepalive);
  77. if (opts->publisher)
  78. {
  79. printf(" --maxdatalen : maximum length of data to read when publishing strings (default is %d)\n",
  80. opts->maxdatalen);
  81. printf(" --message-expiry : MQTT 5 only. Sets the message expiry property in seconds.\n");
  82. printf(" --user-property : MQTT 5 only. Sets a user property.\n");
  83. }
  84. else
  85. {
  86. printf(" --no-delimiter : do not use a delimiter string between messages.\n");
  87. printf(" -R (--no-retained) : do not print retained messages.\n");
  88. }
  89. printf(
  90. " --will-topic : will topic on connect. No default.\n"
  91. " --will-payload : will message. If the will topic is set, but not payload, a null message will be set.\n"
  92. " --will-retain : set the retained flag on the will message. The default is off.\n"
  93. " --will-qos : the will message QoS. The default is 0.\n"
  94. );
  95. printf(
  96. " --cafile : a filename for the TLS truststore.\n"
  97. " --capath : a directory name containing TLS trusted server certificates.\n"
  98. " --cert : a filename for the TLS keystore containing client certificates.\n"
  99. " --key : client private key file.\n"
  100. " --keypass : password for the client private key file.\n"
  101. " --ciphers : the list of cipher suites that the client will present to the server during\n"
  102. " the TLS handshake.\n"
  103. " --insecure : don't check that the server certificate common name matches the hostname.\n"
  104. " --psk : pre-shared-key in hexadecimal (no leading 0x) \n"
  105. " --psk-identity : client identity string for TLS-PSK mode.\n"
  106. );
  107. printf(
  108. " --http-proxy : HTTP proxy string.\n"
  109. " --https-proxy : HTTPS proxy string.\n"
  110. );
  111. printf("\nSee http://eclipse.org/paho for more information about the Eclipse Paho project.\n");
  112. exit(EXIT_FAILURE);
  113. }
  114. int getopts(int argc, char** argv, struct pubsub_opts* opts)
  115. {
  116. int count = 1;
  117. if (argv[1][0] != '-')
  118. {
  119. opts->topic = argv[1];
  120. count = 2;
  121. }
  122. while (count < argc)
  123. {
  124. if (strcmp(argv[count], "--verbose") == 0 || strcmp(argv[count], "-v") == 0)
  125. opts->verbose = 1;
  126. else if (strcmp(argv[count], "--quiet") == 0)
  127. opts->quiet = 1;
  128. else if (strcmp(argv[count], "--qos") == 0 || strcmp(argv[count], "-q") == 0)
  129. {
  130. if (++count < argc)
  131. {
  132. if (strcmp(argv[count], "0") == 0)
  133. opts->qos = 0;
  134. else if (strcmp(argv[count], "1") == 0)
  135. opts->qos = 1;
  136. else if (strcmp(argv[count], "2") == 0)
  137. opts->qos = 2;
  138. else
  139. return 1;
  140. }
  141. else
  142. return 1;
  143. }
  144. else if (strcmp(argv[count], "--connection") == 0 || strcmp(argv[count], "-c") == 0)
  145. {
  146. if (++count < argc)
  147. opts->connection = argv[count];
  148. else
  149. return 1;
  150. }
  151. else if (strcmp(argv[count], "--host") == 0 || strcmp(argv[count], "-h") == 0)
  152. {
  153. if (++count < argc)
  154. opts->host = argv[count];
  155. else
  156. return 1;
  157. }
  158. else if (strcmp(argv[count], "--port") == 0 || strcmp(argv[count], "-p") == 0)
  159. {
  160. if (++count < argc)
  161. opts->port = argv[count];
  162. else
  163. return 1;
  164. }
  165. else if (strcmp(argv[count], "--http-proxy") == 0)
  166. {
  167. if (++count < argc)
  168. opts->http_proxy = argv[count];
  169. else
  170. return 1;
  171. }
  172. else if (strcmp(argv[count], "--https-proxy") == 0)
  173. {
  174. if (++count < argc)
  175. opts->https_proxy = argv[count];
  176. else
  177. return 1;
  178. }
  179. else if (strcmp(argv[count], "--clientid") == 0 || strcmp(argv[count], "-i") == 0)
  180. {
  181. if (++count < argc)
  182. opts->clientid = argv[count];
  183. else
  184. return 1;
  185. }
  186. else if (strcmp(argv[count], "--username") == 0 || strcmp(argv[count], "-u") == 0)
  187. {
  188. if (++count < argc)
  189. opts->username = argv[count];
  190. else
  191. return 1;
  192. }
  193. else if (strcmp(argv[count], "--password") == 0 || strcmp(argv[count], "-P") == 0)
  194. {
  195. if (++count < argc)
  196. opts->password = argv[count];
  197. else
  198. return 1;
  199. }
  200. else if (strcmp(argv[count], "--maxdatalen") == 0)
  201. {
  202. if (++count < argc)
  203. opts->maxdatalen = atoi(argv[count]);
  204. else
  205. return 1;
  206. }
  207. else if (strcmp(argv[count], "--delimiter") == 0)
  208. {
  209. if (++count < argc)
  210. opts->delimiter = argv[count];
  211. else
  212. return 1;
  213. }
  214. else if (strcmp(argv[count], "--no-delimiter") == 0)
  215. opts->delimiter = NULL;
  216. else if (strcmp(argv[count], "--keepalive") == 0 || strcmp(argv[count], "-k") == 0)
  217. {
  218. if (++count < argc)
  219. opts->keepalive = atoi(argv[count]);
  220. else
  221. return 1;
  222. }
  223. else if (strcmp(argv[count], "--topic") == 0 || strcmp(argv[count], "-t") == 0)
  224. {
  225. if (++count < argc)
  226. opts->topic = argv[count];
  227. else
  228. return 1;
  229. }
  230. else if (strcmp(argv[count], "--will-topic") == 0)
  231. {
  232. if (++count < argc)
  233. opts->will_topic = argv[count];
  234. else
  235. return 1;
  236. }
  237. else if (strcmp(argv[count], "--will-payload") == 0)
  238. {
  239. if (++count < argc)
  240. opts->will_payload = argv[count];
  241. else
  242. return 1;
  243. }
  244. else if (strcmp(argv[count], "--will-qos") == 0)
  245. {
  246. if (++count < argc)
  247. opts->will_qos = atoi(argv[count]);
  248. else
  249. return 1;
  250. }
  251. else if (strcmp(argv[count], "--will-retain") == 0)
  252. {
  253. if (++count < argc)
  254. opts->will_retain = 1;
  255. else
  256. return 1;
  257. }
  258. else if (strcmp(argv[count], "--insecure") == 0)
  259. opts->insecure = 1;
  260. else if (strcmp(argv[count], "--capath") == 0)
  261. {
  262. if (++count < argc)
  263. opts->capath = argv[count];
  264. else
  265. return 1;
  266. }
  267. else if (strcmp(argv[count], "--cafile") == 0)
  268. {
  269. if (++count < argc)
  270. opts->cafile = argv[count];
  271. else
  272. return 1;
  273. }
  274. else if (strcmp(argv[count], "--cert") == 0)
  275. {
  276. if (++count < argc)
  277. opts->cert = argv[count];
  278. else
  279. return 1;
  280. }
  281. else if (strcmp(argv[count], "--key") == 0)
  282. {
  283. if (++count < argc)
  284. opts->key = argv[count];
  285. else
  286. return 1;
  287. }
  288. else if (strcmp(argv[count], "--keypass") == 0)
  289. {
  290. if (++count < argc)
  291. opts->keypass = argv[count];
  292. else
  293. return 1;
  294. }
  295. else if (strcmp(argv[count], "--ciphers") == 0)
  296. {
  297. if (++count < argc)
  298. opts->ciphers = argv[count];
  299. else
  300. return 1;
  301. }
  302. else if (strcmp(argv[count], "--psk") == 0)
  303. {
  304. if (++count < argc)
  305. opts->psk = argv[count];
  306. else
  307. return 1;
  308. }
  309. else if (strcmp(argv[count], "--psk-identity") == 0)
  310. {
  311. if (++count < argc)
  312. opts->psk_identity = argv[count];
  313. else
  314. return 1;
  315. }
  316. else if (strcmp(argv[count], "-V") == 0)
  317. {
  318. if (++count < argc)
  319. {
  320. if (strcmp(argv[count], "mqttv31") == 0 || strcmp(argv[count], "31") == 0)
  321. opts->MQTTVersion = MQTTVERSION_3_1;
  322. else if (strcmp(argv[count], "mqttv311") == 0 || strcmp(argv[count], "311") == 0)
  323. opts->MQTTVersion = MQTTVERSION_3_1_1;
  324. else if (strcmp(argv[count], "mqttv5") == 0 || strcmp(argv[count], "5") == 0)
  325. opts->MQTTVersion = MQTTVERSION_5;
  326. else
  327. return 1;
  328. }
  329. else
  330. return 1;
  331. }
  332. else if (strcmp(argv[count], "--trace") == 0)
  333. {
  334. if (++count < argc)
  335. {
  336. if (strcmp(argv[count], "error") == 0)
  337. opts->tracelevel = MQTTASYNC_TRACE_ERROR;
  338. else if (strcmp(argv[count], "protocol") == 0)
  339. opts->tracelevel = MQTTASYNC_TRACE_PROTOCOL;
  340. else if (strcmp(argv[count], "min") == 0 || strcmp(argv[count], "on") == 0)
  341. opts->tracelevel = MQTTASYNC_TRACE_MINIMUM;
  342. else if (strcmp(argv[count], "max") == 0)
  343. opts->tracelevel = MQTTASYNC_TRACE_MAXIMUM;
  344. else
  345. return 1;
  346. }
  347. else
  348. return 1;
  349. }
  350. else if (opts->publisher == 0)
  351. {
  352. if (strcmp(argv[count], "--no-retained") == 0 || strcmp(argv[count], "-R") == 0)
  353. opts->retained = 1;
  354. else
  355. {
  356. fprintf(stderr, "Unknown option %s\n", argv[count]);
  357. return 1;
  358. }
  359. }
  360. else if (opts->publisher == 1)
  361. {
  362. if (strcmp(argv[count], "--retained") == 0 || strcmp(argv[count], "-r") == 0)
  363. opts->retained = 1;
  364. else if (strcmp(argv[count], "--user-property") == 0)
  365. {
  366. if (count + 2 < argc)
  367. {
  368. opts->user_property.name = argv[++count];
  369. opts->user_property.value = argv[++count];
  370. }
  371. else
  372. return 1;
  373. }
  374. else if (strcmp(argv[count], "--message-expiry") == 0)
  375. {
  376. if (++count < argc)
  377. opts->message_expiry = atoi(argv[count]);
  378. else
  379. return 1;
  380. }
  381. else if (strcmp(argv[count], "-m") == 0 || strcmp(argv[count], "--message") == 0)
  382. {
  383. if (++count < argc)
  384. {
  385. opts->stdin_lines = 0;
  386. opts->message = argv[count];
  387. }
  388. else
  389. return 1;
  390. }
  391. else if (strcmp(argv[count], "-f") == 0 || strcmp(argv[count], "--filename") == 0)
  392. {
  393. if (++count < argc)
  394. {
  395. opts->stdin_lines = 0;
  396. opts->filename = argv[count];
  397. }
  398. else
  399. return 1;
  400. }
  401. else if (strcmp(argv[count], "-n") == 0 || strcmp(argv[count], "--null-message") == 0)
  402. {
  403. opts->stdin_lines = 0;
  404. opts->null_message = 1;
  405. }
  406. else
  407. {
  408. fprintf(stderr, "Unknown option %s\n", argv[count]);
  409. return 1;
  410. }
  411. }
  412. else
  413. {
  414. fprintf(stderr, "Unknown option %s\n", argv[count]);
  415. return 1;
  416. }
  417. count++;
  418. }
  419. if (opts->topic == NULL)
  420. return 1;
  421. return 0;
  422. }
  423. char* readfile(int* data_len, struct pubsub_opts* opts)
  424. {
  425. char* buffer = NULL;
  426. long filesize = 0L;
  427. FILE* infile = fopen(opts->filename, "rb");
  428. if (infile == NULL)
  429. {
  430. fprintf(stderr, "Can't open file %s\n", opts->filename);
  431. return NULL;
  432. }
  433. fseek(infile, 0, SEEK_END);
  434. filesize = ftell(infile);
  435. rewind(infile);
  436. buffer = malloc(sizeof(char)*filesize);
  437. if (buffer == NULL)
  438. {
  439. fprintf(stderr, "Can't allocate buffer to read file %s\n", opts->filename);
  440. fclose(infile);
  441. return NULL;
  442. }
  443. *data_len = (int)fread(buffer, 1, filesize, infile);
  444. if (*data_len != filesize)
  445. {
  446. fprintf(stderr, "%d bytes read of %ld expected for file %s\n", *data_len, filesize, opts->filename);
  447. fclose(infile);
  448. free(buffer);
  449. return NULL;
  450. }
  451. fclose(infile);
  452. return buffer;
  453. }
  454. void logProperties(MQTTProperties *props)
  455. {
  456. int i = 0;
  457. for (i = 0; i < props->count; ++i)
  458. {
  459. int id = props->array[i].identifier;
  460. const char* name = MQTTPropertyName(id);
  461. char* intformat = "Property name %s value %d\n";
  462. switch (MQTTProperty_getType(id))
  463. {
  464. case MQTTPROPERTY_TYPE_BYTE:
  465. printf(intformat, name, props->array[i].value.byte);
  466. break;
  467. case MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER:
  468. printf(intformat, name, props->array[i].value.integer2);
  469. break;
  470. case MQTTPROPERTY_TYPE_FOUR_BYTE_INTEGER:
  471. printf(intformat, name, props->array[i].value.integer4);
  472. break;
  473. case MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER:
  474. printf(intformat, name, props->array[i].value.integer4);
  475. break;
  476. case MQTTPROPERTY_TYPE_BINARY_DATA:
  477. case MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING:
  478. printf("Property name %s value len %.*s\n", name,
  479. props->array[i].value.data.len, props->array[i].value.data.data);
  480. break;
  481. case MQTTPROPERTY_TYPE_UTF_8_STRING_PAIR:
  482. printf("Property name %s key %.*s value %.*s\n", name,
  483. props->array[i].value.data.len, props->array[i].value.data.data,
  484. props->array[i].value.value.len, props->array[i].value.value.data);
  485. break;
  486. }
  487. }
  488. }