pubsub_opts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 v1.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * http://www.eclipse.org/legal/epl-v10.html
  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("\nSee http://eclipse.org/paho for more information about the Eclipse Paho project.\n");
  108. exit(EXIT_FAILURE);
  109. }
  110. int getopts(int argc, char** argv, struct pubsub_opts* opts)
  111. {
  112. int count = 1;
  113. if (argv[1][0] != '-')
  114. {
  115. opts->topic = argv[1];
  116. count = 2;
  117. }
  118. while (count < argc)
  119. {
  120. if (strcmp(argv[count], "--verbose") == 0 || strcmp(argv[count], "-v") == 0)
  121. opts->verbose = 1;
  122. else if (strcmp(argv[count], "--quiet") == 0)
  123. opts->quiet = 1;
  124. else if (strcmp(argv[count], "--qos") == 0 || strcmp(argv[count], "-q") == 0)
  125. {
  126. if (++count < argc)
  127. {
  128. if (strcmp(argv[count], "0") == 0)
  129. opts->qos = 0;
  130. else if (strcmp(argv[count], "1") == 0)
  131. opts->qos = 1;
  132. else if (strcmp(argv[count], "2") == 0)
  133. opts->qos = 2;
  134. else
  135. return 1;
  136. }
  137. else
  138. return 1;
  139. }
  140. else if (strcmp(argv[count], "--connection") == 0 || strcmp(argv[count], "-c") == 0)
  141. {
  142. if (++count < argc)
  143. opts->connection = argv[count];
  144. else
  145. return 1;
  146. }
  147. else if (strcmp(argv[count], "--host") == 0 || strcmp(argv[count], "-h") == 0)
  148. {
  149. if (++count < argc)
  150. opts->host = argv[count];
  151. else
  152. return 1;
  153. }
  154. else if (strcmp(argv[count], "--port") == 0 || strcmp(argv[count], "-p") == 0)
  155. {
  156. if (++count < argc)
  157. opts->port = argv[count];
  158. else
  159. return 1;
  160. }
  161. else if (strcmp(argv[count], "--clientid") == 0 || strcmp(argv[count], "-i") == 0)
  162. {
  163. if (++count < argc)
  164. opts->clientid = argv[count];
  165. else
  166. return 1;
  167. }
  168. else if (strcmp(argv[count], "--username") == 0 || strcmp(argv[count], "-u") == 0)
  169. {
  170. if (++count < argc)
  171. opts->username = argv[count];
  172. else
  173. return 1;
  174. }
  175. else if (strcmp(argv[count], "--password") == 0 || strcmp(argv[count], "-P") == 0)
  176. {
  177. if (++count < argc)
  178. opts->password = argv[count];
  179. else
  180. return 1;
  181. }
  182. else if (strcmp(argv[count], "--maxdatalen") == 0)
  183. {
  184. if (++count < argc)
  185. opts->maxdatalen = atoi(argv[count]);
  186. else
  187. return 1;
  188. }
  189. else if (strcmp(argv[count], "--delimiter") == 0)
  190. {
  191. if (++count < argc)
  192. opts->delimiter = argv[count];
  193. else
  194. return 1;
  195. }
  196. else if (strcmp(argv[count], "--no-delimiter") == 0)
  197. opts->delimiter = NULL;
  198. else if (strcmp(argv[count], "--keepalive") == 0 || strcmp(argv[count], "-k") == 0)
  199. {
  200. if (++count < argc)
  201. opts->keepalive = atoi(argv[count]);
  202. else
  203. return 1;
  204. }
  205. else if (strcmp(argv[count], "--topic") == 0 || strcmp(argv[count], "-t") == 0)
  206. {
  207. if (++count < argc)
  208. opts->topic = argv[count];
  209. else
  210. return 1;
  211. }
  212. else if (strcmp(argv[count], "--will-topic") == 0)
  213. {
  214. if (++count < argc)
  215. opts->will_topic = argv[count];
  216. else
  217. return 1;
  218. }
  219. else if (strcmp(argv[count], "--will-payload") == 0)
  220. {
  221. if (++count < argc)
  222. opts->will_payload = argv[count];
  223. else
  224. return 1;
  225. }
  226. else if (strcmp(argv[count], "--will-qos") == 0)
  227. {
  228. if (++count < argc)
  229. opts->will_qos = atoi(argv[count]);
  230. else
  231. return 1;
  232. }
  233. else if (strcmp(argv[count], "--will-retain") == 0)
  234. {
  235. if (++count < argc)
  236. opts->will_retain = 1;
  237. else
  238. return 1;
  239. }
  240. else if (strcmp(argv[count], "--insecure") == 0)
  241. opts->insecure = 1;
  242. else if (strcmp(argv[count], "--capath") == 0)
  243. {
  244. if (++count < argc)
  245. opts->capath = argv[count];
  246. else
  247. return 1;
  248. }
  249. else if (strcmp(argv[count], "--cafile") == 0)
  250. {
  251. if (++count < argc)
  252. opts->cafile = argv[count];
  253. else
  254. return 1;
  255. }
  256. else if (strcmp(argv[count], "--cert") == 0)
  257. {
  258. if (++count < argc)
  259. opts->cert = argv[count];
  260. else
  261. return 1;
  262. }
  263. else if (strcmp(argv[count], "--key") == 0)
  264. {
  265. if (++count < argc)
  266. opts->key = argv[count];
  267. else
  268. return 1;
  269. }
  270. else if (strcmp(argv[count], "--keypass") == 0)
  271. {
  272. if (++count < argc)
  273. opts->keypass = argv[count];
  274. else
  275. return 1;
  276. }
  277. else if (strcmp(argv[count], "--ciphers") == 0)
  278. {
  279. if (++count < argc)
  280. opts->ciphers = argv[count];
  281. else
  282. return 1;
  283. }
  284. else if (strcmp(argv[count], "--psk") == 0)
  285. {
  286. if (++count < argc)
  287. opts->psk = argv[count];
  288. else
  289. return 1;
  290. }
  291. else if (strcmp(argv[count], "--psk-identity") == 0)
  292. {
  293. if (++count < argc)
  294. opts->psk_identity = argv[count];
  295. else
  296. return 1;
  297. }
  298. else if (strcmp(argv[count], "-V") == 0)
  299. {
  300. if (++count < argc)
  301. {
  302. if (strcmp(argv[count], "mqttv31") == 0 || strcmp(argv[count], "31") == 0)
  303. opts->MQTTVersion = MQTTVERSION_3_1;
  304. else if (strcmp(argv[count], "mqttv311") == 0 || strcmp(argv[count], "311") == 0)
  305. opts->MQTTVersion = MQTTVERSION_3_1_1;
  306. else if (strcmp(argv[count], "mqttv5") == 0 || strcmp(argv[count], "5") == 0)
  307. opts->MQTTVersion = MQTTVERSION_5;
  308. else
  309. return 1;
  310. }
  311. else
  312. return 1;
  313. }
  314. else if (strcmp(argv[count], "--trace") == 0)
  315. {
  316. if (++count < argc)
  317. {
  318. if (strcmp(argv[count], "error") == 0)
  319. opts->tracelevel = MQTTASYNC_TRACE_ERROR;
  320. else if (strcmp(argv[count], "protocol") == 0)
  321. opts->tracelevel = MQTTASYNC_TRACE_PROTOCOL;
  322. else if (strcmp(argv[count], "min") == 0 || strcmp(argv[count], "on") == 0)
  323. opts->tracelevel = MQTTASYNC_TRACE_MINIMUM;
  324. else if (strcmp(argv[count], "max") == 0)
  325. opts->tracelevel = MQTTASYNC_TRACE_MAXIMUM;
  326. else
  327. return 1;
  328. }
  329. else
  330. return 1;
  331. }
  332. else if (opts->publisher == 0)
  333. {
  334. if (strcmp(argv[count], "--no-retained") == 0 || strcmp(argv[count], "-R") == 0)
  335. opts->retained = 1;
  336. else
  337. {
  338. fprintf(stderr, "Unknown option %s\n", argv[count]);
  339. return 1;
  340. }
  341. }
  342. else if (opts->publisher == 1)
  343. {
  344. if (strcmp(argv[count], "--retained") == 0 || strcmp(argv[count], "-r") == 0)
  345. opts->retained = 1;
  346. else if (strcmp(argv[count], "--user-property") == 0)
  347. {
  348. if (count + 2 < argc)
  349. {
  350. opts->user_property.name = argv[++count];
  351. opts->user_property.value = argv[++count];
  352. }
  353. else
  354. return 1;
  355. }
  356. else if (strcmp(argv[count], "--message-expiry") == 0)
  357. {
  358. if (++count < argc)
  359. opts->message_expiry = atoi(argv[count]);
  360. else
  361. return 1;
  362. }
  363. else if (strcmp(argv[count], "-m") == 0 || strcmp(argv[count], "--message") == 0)
  364. {
  365. if (++count < argc)
  366. {
  367. opts->stdin_lines = 0;
  368. opts->message = argv[count];
  369. }
  370. else
  371. return 1;
  372. }
  373. else if (strcmp(argv[count], "-f") == 0 || strcmp(argv[count], "--filename") == 0)
  374. {
  375. if (++count < argc)
  376. {
  377. opts->stdin_lines = 0;
  378. opts->filename = argv[count];
  379. }
  380. else
  381. return 1;
  382. }
  383. else if (strcmp(argv[count], "-n") == 0 || strcmp(argv[count], "--null-message") == 0)
  384. {
  385. opts->stdin_lines = 0;
  386. opts->null_message = 1;
  387. }
  388. else
  389. {
  390. fprintf(stderr, "Unknown option %s\n", argv[count]);
  391. return 1;
  392. }
  393. }
  394. else
  395. {
  396. fprintf(stderr, "Unknown option %s\n", argv[count]);
  397. return 1;
  398. }
  399. count++;
  400. }
  401. if (opts->topic == NULL)
  402. return 1;
  403. return 0;
  404. }
  405. char* readfile(int* data_len, struct pubsub_opts* opts)
  406. {
  407. char* buffer = NULL;
  408. long filesize = 0L;
  409. FILE* infile = fopen(opts->filename, "rb");
  410. if (infile == NULL)
  411. {
  412. fprintf(stderr, "Can't open file %s\n", opts->filename);
  413. return NULL;
  414. }
  415. fseek(infile, 0, SEEK_END);
  416. filesize = ftell(infile);
  417. rewind(infile);
  418. buffer = malloc(sizeof(char)*filesize);
  419. if (buffer == NULL)
  420. {
  421. fprintf(stderr, "Can't allocate buffer to read file %s\n", opts->filename);
  422. fclose(infile);
  423. return NULL;
  424. }
  425. *data_len = (int)fread(buffer, 1, filesize, infile);
  426. if (*data_len != filesize)
  427. {
  428. fprintf(stderr, "%d bytes read of %ld expected for file %s\n", *data_len, filesize, opts->filename);
  429. fclose(infile);
  430. free(buffer);
  431. return NULL;
  432. }
  433. fclose(infile);
  434. return buffer;
  435. }
  436. void logProperties(MQTTProperties *props)
  437. {
  438. int i = 0;
  439. for (i = 0; i < props->count; ++i)
  440. {
  441. int id = props->array[i].identifier;
  442. const char* name = MQTTPropertyName(id);
  443. char* intformat = "Property name %s value %d\n";
  444. switch (MQTTProperty_getType(id))
  445. {
  446. case MQTTPROPERTY_TYPE_BYTE:
  447. printf(intformat, name, props->array[i].value.byte);
  448. break;
  449. case MQTTPROPERTY_TYPE_TWO_BYTE_INTEGER:
  450. printf(intformat, name, props->array[i].value.integer2);
  451. break;
  452. case MQTTPROPERTY_TYPE_FOUR_BYTE_INTEGER:
  453. printf(intformat, name, props->array[i].value.integer4);
  454. break;
  455. case MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER:
  456. printf(intformat, name, props->array[i].value.integer4);
  457. break;
  458. case MQTTPROPERTY_TYPE_BINARY_DATA:
  459. case MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING:
  460. printf("Property name %s value len %.*s\n", name,
  461. props->array[i].value.data.len, props->array[i].value.data.data);
  462. break;
  463. case MQTTPROPERTY_TYPE_UTF_8_STRING_PAIR:
  464. printf("Property name %s key %.*s value %.*s\n", name,
  465. props->array[i].value.data.len, props->array[i].value.data.data,
  466. props->array[i].value.value.len, props->array[i].value.value.data);
  467. break;
  468. }
  469. }
  470. }