PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/client/pub_client.c

https://bitbucket.org/klkucan/mosqnet
C | 696 lines | 633 code | 33 blank | 30 comment | 174 complexity | dc541952a440e9667e705a48f1de850a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. /*
  2. Copyright (c) 2009-2012 Roger Light <roger@atchoo.org>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. 1. Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. 2. Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. 3. Neither the name of mosquitto nor the names of its
  12. contributors may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  18. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  19. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  20. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  22. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #ifndef WIN32
  32. #include <unistd.h>
  33. #else
  34. #include <process.h>
  35. #include <winsock2.h>
  36. #define snprintf sprintf_s
  37. #endif
  38. #include <mosquitto.h>
  39. #define MSGMODE_NONE 0
  40. #define MSGMODE_CMD 1
  41. #define MSGMODE_STDIN_LINE 2
  42. #define MSGMODE_STDIN_FILE 3
  43. #define MSGMODE_FILE 4
  44. #define MSGMODE_NULL 5
  45. #define STATUS_CONNECTING 0
  46. #define STATUS_CONNACK_RECVD 1
  47. /* Global variables for use in callbacks. See sub_client.c for an example of
  48. * using a struct to hold variables for use in callbacks. */
  49. static char *topic = NULL;
  50. static char *message = NULL;
  51. static long msglen = 0;
  52. static int qos = 0;
  53. static int retain = 0;
  54. static int mode = MSGMODE_NONE;
  55. static int status = STATUS_CONNECTING;
  56. static int mid_sent = 0;
  57. static bool connected = true;
  58. static char *username = NULL;
  59. static char *password = NULL;
  60. static bool disconnect_sent = false;
  61. static bool quiet = false;
  62. void my_connect_callback(struct mosquitto *mosq, void *obj, int result)
  63. {
  64. int rc = MOSQ_ERR_SUCCESS;
  65. if(!result){
  66. switch(mode){
  67. case MSGMODE_CMD:
  68. case MSGMODE_FILE:
  69. case MSGMODE_STDIN_FILE:
  70. rc = mosquitto_publish(mosq, &mid_sent, topic, msglen, message, qos, retain);
  71. break;
  72. case MSGMODE_NULL:
  73. rc = mosquitto_publish(mosq, &mid_sent, topic, 0, NULL, qos, retain);
  74. break;
  75. case MSGMODE_STDIN_LINE:
  76. status = STATUS_CONNACK_RECVD;
  77. break;
  78. }
  79. if(rc){
  80. if(!quiet){
  81. switch(rc){
  82. case MOSQ_ERR_INVAL:
  83. fprintf(stderr, "Error: Invalid input. Does your topic contain '+' or '#'?\n");
  84. break;
  85. case MOSQ_ERR_NOMEM:
  86. fprintf(stderr, "Error: Out of memory when trying to publish message.\n");
  87. break;
  88. case MOSQ_ERR_NO_CONN:
  89. fprintf(stderr, "Error: Client not connected when trying to publish.\n");
  90. break;
  91. case MOSQ_ERR_PROTOCOL:
  92. fprintf(stderr, "Error: Protocol error when communicating with broker.\n");
  93. break;
  94. case MOSQ_ERR_PAYLOAD_SIZE:
  95. fprintf(stderr, "Error: Message payload is too large.\n");
  96. break;
  97. }
  98. }
  99. mosquitto_disconnect(mosq);
  100. }
  101. }else{
  102. if(result && !quiet){
  103. fprintf(stderr, "%s\n", mosquitto_connack_string(result));
  104. }
  105. }
  106. }
  107. void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc)
  108. {
  109. connected = false;
  110. }
  111. void my_publish_callback(struct mosquitto *mosq, void *obj, int mid)
  112. {
  113. if(mode != MSGMODE_STDIN_LINE && disconnect_sent == false){
  114. mosquitto_disconnect(mosq);
  115. disconnect_sent = true;
  116. }
  117. }
  118. void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
  119. {
  120. printf("%s\n", str);
  121. }
  122. int load_stdin(void)
  123. {
  124. long pos = 0, rlen;
  125. char buf[1024];
  126. mode = MSGMODE_STDIN_FILE;
  127. while(!feof(stdin)){
  128. rlen = fread(buf, 1, 1024, stdin);
  129. message = realloc(message, pos+rlen);
  130. if(!message){
  131. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  132. return 1;
  133. }
  134. memcpy(&(message[pos]), buf, rlen);
  135. pos += rlen;
  136. }
  137. msglen = pos;
  138. if(!msglen){
  139. if(!quiet) fprintf(stderr, "Error: Zero length input.\n");
  140. return 1;
  141. }
  142. return 0;
  143. }
  144. int load_file(const char *filename)
  145. {
  146. long pos, rlen;
  147. FILE *fptr = NULL;
  148. fptr = fopen(filename, "rb");
  149. if(!fptr){
  150. if(!quiet) fprintf(stderr, "Error: Unable to open file \"%s\".\n", filename);
  151. return 1;
  152. }
  153. mode = MSGMODE_FILE;
  154. fseek(fptr, 0, SEEK_END);
  155. msglen = ftell(fptr);
  156. if(msglen > 268435455){
  157. fclose(fptr);
  158. if(!quiet) fprintf(stderr, "Error: File \"%s\" is too large (>268,435,455 bytes).\n", filename);
  159. return 1;
  160. }
  161. if(msglen == 0){
  162. fclose(fptr);
  163. if(!quiet) fprintf(stderr, "Error: File \"%s\" is empty.\n", filename);
  164. return 1;
  165. }
  166. fseek(fptr, 0, SEEK_SET);
  167. message = malloc(msglen);
  168. if(!message){
  169. fclose(fptr);
  170. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  171. return 1;
  172. }
  173. pos = 0;
  174. while(pos < msglen){
  175. rlen = fread(&(message[pos]), sizeof(char), msglen-pos, fptr);
  176. pos += rlen;
  177. }
  178. fclose(fptr);
  179. return 0;
  180. }
  181. void print_usage(void)
  182. {
  183. printf("mosquitto_pub is a simple mqtt client that will publish a message on a single topic and exit.\n\n");
  184. printf("Usage: mosquitto_pub [-h host] [-p port] [-q qos] [-r] {-f file | -l | -n | -m message} -t topic\n");
  185. printf(" [-i id] [-I id_prefix]\n");
  186. printf(" [-d] [--quiet]\n");
  187. printf(" [-u username [-P password]]\n");
  188. printf(" [--will-topic [--will-payload payload] [--will-qos qos] [--will-retain]]\n");
  189. #ifdef WITH_TLS
  190. printf(" [{--cafile file | --capath dir} [--cert file] [--key file]]\n");
  191. #ifdef WITH_TLS_PSK
  192. printf(" [--psk hex-key --psk-identity identity]\n");
  193. #endif
  194. #endif
  195. printf(" mosquitto_pub --help\n\n");
  196. printf(" -d : enable debug messages.\n");
  197. printf(" -f : send the contents of a file as the message.\n");
  198. printf(" -h : mqtt host to connect to. Defaults to localhost.\n");
  199. printf(" -i : id to use for this client. Defaults to mosquitto_pub_ appended with the process id.\n");
  200. printf(" -I : define the client id as id_prefix appended with the process id. Useful for when the\n");
  201. printf(" broker is using the clientid_prefixes option.\n");
  202. printf(" -l : read messages from stdin, sending a separate message for each line.\n");
  203. printf(" -m : message payload to send.\n");
  204. printf(" -n : send a null (zero length) message.\n");
  205. printf(" -p : network port to connect to. Defaults to 1883.\n");
  206. printf(" -q : quality of service level to use for all messages. Defaults to 0.\n");
  207. printf(" -r : message should be retained.\n");
  208. printf(" -s : read message from stdin, sending the entire input as a message.\n");
  209. printf(" -t : mqtt topic to publish to.\n");
  210. printf(" -u : provide a username (requires MQTT 3.1 broker)\n");
  211. printf(" -P : provide a password (requires MQTT 3.1 broker)\n");
  212. printf(" --help : display this message.\n");
  213. printf(" --quiet : don't print error messages.\n");
  214. printf(" --will-payload : payload for the client Will, which is sent by the broker in case of\n");
  215. printf(" unexpected disconnection. If not given and will-topic is set, a zero\n");
  216. printf(" length message will be sent.\n");
  217. printf(" --will-qos : QoS level for the client Will.\n");
  218. printf(" --will-retain : if given, make the client Will retained.\n");
  219. printf(" --will-topic : the topic on which to publish the client Will.\n");
  220. #ifdef WITH_TLS
  221. printf(" --cafile : path to a file containing trusted CA certificates to enable encrypted\n");
  222. printf(" communication.\n");
  223. printf(" --capath : path to a directory containing trusted CA certificates to enable encrypted\n");
  224. printf(" communication.\n");
  225. printf(" --cert : client certificate for authentication, if required by server.\n");
  226. printf(" --key : client private key for authentication, if required by server.\n");
  227. #ifdef WITH_TLS_PSK
  228. printf(" --psk : pre-shared-key in hexadecimal (no leading 0x) to enable TLS-PSK mode.\n");
  229. printf(" --psk-identity : client identity string for TLS-PSK mode.\n");
  230. #endif
  231. #endif
  232. printf("\nSee http://mosquitto.org/ for more information.\n\n");
  233. }
  234. int main(int argc, char *argv[])
  235. {
  236. char *id = NULL;
  237. char *id_prefix = NULL;
  238. int i;
  239. char *host = "localhost";
  240. int port = 1883;
  241. int keepalive = 60;
  242. #ifndef WIN32
  243. int opt;
  244. #endif
  245. char buf[1024];
  246. bool debug = false;
  247. struct mosquitto *mosq = NULL;
  248. int rc;
  249. int rc2;
  250. char hostname[256];
  251. char err[1024];
  252. int len;
  253. char *will_payload = NULL;
  254. long will_payloadlen = 0;
  255. int will_qos = 0;
  256. bool will_retain = false;
  257. char *will_topic = NULL;
  258. char *cafile = NULL;
  259. char *capath = NULL;
  260. char *certfile = NULL;
  261. char *keyfile = NULL;
  262. char *psk = NULL;
  263. char *psk_identity = NULL;
  264. for(i=1; i<argc; i++){
  265. if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){
  266. if(i==argc-1){
  267. fprintf(stderr, "Error: -p argument given but no port specified.\n\n");
  268. print_usage();
  269. return 1;
  270. }else{
  271. port = atoi(argv[i+1]);
  272. if(port<1 || port>65535){
  273. fprintf(stderr, "Error: Invalid port given: %d\n", port);
  274. print_usage();
  275. return 1;
  276. }
  277. }
  278. i++;
  279. }else if(!strcmp(argv[i], "--cafile")){
  280. if(i==argc-1){
  281. fprintf(stderr, "Error: --cafile argument given but no file specified.\n\n");
  282. print_usage();
  283. return 1;
  284. }else{
  285. cafile = argv[i+1];
  286. }
  287. i++;
  288. }else if(!strcmp(argv[i], "--capath")){
  289. if(i==argc-1){
  290. fprintf(stderr, "Error: --capath argument given but no directory specified.\n\n");
  291. print_usage();
  292. return 1;
  293. }else{
  294. capath = argv[i+1];
  295. }
  296. i++;
  297. }else if(!strcmp(argv[i], "--cert")){
  298. if(i==argc-1){
  299. fprintf(stderr, "Error: --cert argument given but no file specified.\n\n");
  300. print_usage();
  301. return 1;
  302. }else{
  303. certfile = argv[i+1];
  304. }
  305. i++;
  306. }else if(!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")){
  307. debug = true;
  308. }else if(!strcmp(argv[i], "-f") || !strcmp(argv[i], "--file")){
  309. if(mode != MSGMODE_NONE){
  310. fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n");
  311. print_usage();
  312. return 1;
  313. }else if(i==argc-1){
  314. fprintf(stderr, "Error: -f argument given but no file specified.\n\n");
  315. print_usage();
  316. return 1;
  317. }else{
  318. if(load_file(argv[i+1])) return 1;
  319. }
  320. i++;
  321. }else if(!strcmp(argv[i], "--help")){
  322. print_usage();
  323. return 0;
  324. }else if(!strcmp(argv[i], "-h") || !strcmp(argv[i], "--host")){
  325. if(i==argc-1){
  326. fprintf(stderr, "Error: -h argument given but no host specified.\n\n");
  327. print_usage();
  328. return 1;
  329. }else{
  330. host = argv[i+1];
  331. }
  332. i++;
  333. }else if(!strcmp(argv[i], "-i") || !strcmp(argv[i], "--id")){
  334. if(id_prefix){
  335. fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n");
  336. print_usage();
  337. return 1;
  338. }
  339. if(i==argc-1){
  340. fprintf(stderr, "Error: -i argument given but no id specified.\n\n");
  341. print_usage();
  342. return 1;
  343. }else{
  344. id = argv[i+1];
  345. }
  346. i++;
  347. }else if(!strcmp(argv[i], "-I") || !strcmp(argv[i], "--id-prefix")){
  348. if(id){
  349. fprintf(stderr, "Error: -i and -I argument cannot be used together.\n\n");
  350. print_usage();
  351. return 1;
  352. }
  353. if(i==argc-1){
  354. fprintf(stderr, "Error: -I argument given but no id prefix specified.\n\n");
  355. print_usage();
  356. return 1;
  357. }else{
  358. id_prefix = argv[i+1];
  359. }
  360. i++;
  361. }else if(!strcmp(argv[i], "--key")){
  362. if(i==argc-1){
  363. fprintf(stderr, "Error: --key argument given but no file specified.\n\n");
  364. print_usage();
  365. return 1;
  366. }else{
  367. keyfile = argv[i+1];
  368. }
  369. i++;
  370. }else if(!strcmp(argv[i], "-l") || !strcmp(argv[i], "--stdin-line")){
  371. if(mode != MSGMODE_NONE){
  372. fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n");
  373. print_usage();
  374. return 1;
  375. }else{
  376. mode = MSGMODE_STDIN_LINE;
  377. #ifndef WIN32
  378. opt = fcntl(fileno(stdin), F_GETFL, 0);
  379. if(opt == -1 || fcntl(fileno(stdin), F_SETFL, opt | O_NONBLOCK) == -1){
  380. fprintf(stderr, "Error: Unable to set stdin to non-blocking.\n");
  381. return 1;
  382. }
  383. #endif
  384. }
  385. }else if(!strcmp(argv[i], "-m") || !strcmp(argv[i], "--message")){
  386. if(mode != MSGMODE_NONE){
  387. fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n");
  388. print_usage();
  389. return 1;
  390. }else if(i==argc-1){
  391. fprintf(stderr, "Error: -m argument given but no message specified.\n\n");
  392. print_usage();
  393. return 1;
  394. }else{
  395. message = argv[i+1];
  396. msglen = strlen(message);
  397. mode = MSGMODE_CMD;
  398. }
  399. i++;
  400. }else if(!strcmp(argv[i], "-n") || !strcmp(argv[i], "--null-message")){
  401. if(mode != MSGMODE_NONE){
  402. fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n");
  403. print_usage();
  404. return 1;
  405. }else{
  406. mode = MSGMODE_NULL;
  407. }
  408. }else if(!strcmp(argv[i], "--psk")){
  409. if(i==argc-1){
  410. fprintf(stderr, "Error: --psk argument given but no key specified.\n\n");
  411. print_usage();
  412. return 1;
  413. }else{
  414. psk = argv[i+1];
  415. }
  416. i++;
  417. }else if(!strcmp(argv[i], "--psk-identity")){
  418. if(i==argc-1){
  419. fprintf(stderr, "Error: --psk-identity argument given but no identity specified.\n\n");
  420. print_usage();
  421. return 1;
  422. }else{
  423. psk_identity = argv[i+1];
  424. }
  425. i++;
  426. }else if(!strcmp(argv[i], "-q") || !strcmp(argv[i], "--qos")){
  427. if(i==argc-1){
  428. fprintf(stderr, "Error: -q argument given but no QoS specified.\n\n");
  429. print_usage();
  430. return 1;
  431. }else{
  432. qos = atoi(argv[i+1]);
  433. if(qos<0 || qos>2){
  434. fprintf(stderr, "Error: Invalid QoS given: %d\n", qos);
  435. print_usage();
  436. return 1;
  437. }
  438. }
  439. i++;
  440. }else if(!strcmp(argv[i], "--quiet")){
  441. quiet = true;
  442. }else if(!strcmp(argv[i], "-r") || !strcmp(argv[i], "--retain")){
  443. retain = 1;
  444. }else if(!strcmp(argv[i], "-s") || !strcmp(argv[i], "--stdin-file")){
  445. if(mode != MSGMODE_NONE){
  446. fprintf(stderr, "Error: Only one type of message can be sent at once.\n\n");
  447. print_usage();
  448. return 1;
  449. }else{
  450. if(load_stdin()) return 1;
  451. }
  452. }else if(!strcmp(argv[i], "-t") || !strcmp(argv[i], "--topic")){
  453. if(i==argc-1){
  454. fprintf(stderr, "Error: -t argument given but no topic specified.\n\n");
  455. print_usage();
  456. return 1;
  457. }else{
  458. topic = argv[i+1];
  459. }
  460. i++;
  461. }else if(!strcmp(argv[i], "-u") || !strcmp(argv[i], "--username")){
  462. if(i==argc-1){
  463. fprintf(stderr, "Error: -u argument given but no username specified.\n\n");
  464. print_usage();
  465. return 1;
  466. }else{
  467. username = argv[i+1];
  468. }
  469. i++;
  470. }else if(!strcmp(argv[i], "-P") || !strcmp(argv[i], "--pw")){
  471. if(i==argc-1){
  472. fprintf(stderr, "Error: -P argument given but no password specified.\n\n");
  473. print_usage();
  474. return 1;
  475. }else{
  476. password = argv[i+1];
  477. }
  478. i++;
  479. }else if(!strcmp(argv[i], "--will-payload")){
  480. if(i==argc-1){
  481. fprintf(stderr, "Error: --will-payload argument given but no will payload specified.\n\n");
  482. print_usage();
  483. return 1;
  484. }else{
  485. will_payload = argv[i+1];
  486. will_payloadlen = strlen(will_payload);
  487. }
  488. i++;
  489. }else if(!strcmp(argv[i], "--will-qos")){
  490. if(i==argc-1){
  491. fprintf(stderr, "Error: --will-qos argument given but no will QoS specified.\n\n");
  492. print_usage();
  493. return 1;
  494. }else{
  495. will_qos = atoi(argv[i+1]);
  496. if(will_qos < 0 || will_qos > 2){
  497. fprintf(stderr, "Error: Invalid will QoS %d.\n\n", will_qos);
  498. return 1;
  499. }
  500. }
  501. i++;
  502. }else if(!strcmp(argv[i], "--will-retain")){
  503. will_retain = true;
  504. }else if(!strcmp(argv[i], "--will-topic")){
  505. if(i==argc-1){
  506. fprintf(stderr, "Error: --will-topic argument given but no will topic specified.\n\n");
  507. print_usage();
  508. return 1;
  509. }else{
  510. will_topic = argv[i+1];
  511. }
  512. i++;
  513. }else{
  514. fprintf(stderr, "Error: Unknown option '%s'.\n",argv[i]);
  515. print_usage();
  516. return 1;
  517. }
  518. }
  519. if(!topic || mode == MSGMODE_NONE){
  520. fprintf(stderr, "Error: Both topic and message must be supplied.\n");
  521. print_usage();
  522. return 1;
  523. }
  524. if(will_payload && !will_topic){
  525. fprintf(stderr, "Error: Will payload given, but no will topic given.\n");
  526. print_usage();
  527. return 1;
  528. }
  529. if(will_retain && !will_topic){
  530. fprintf(stderr, "Error: Will retain given, but no will topic given.\n");
  531. print_usage();
  532. return 1;
  533. }
  534. if(password && !username){
  535. if(!quiet) fprintf(stderr, "Warning: Not using password since username not set.\n");
  536. }
  537. if((certfile && !keyfile) || (keyfile && !certfile)){
  538. fprintf(stderr, "Error: Both certfile and keyfile must be provided if one of them is.\n");
  539. print_usage();
  540. return 1;
  541. }
  542. if((cafile || capath) && psk){
  543. if(!quiet) fprintf(stderr, "Error: Only one of --psk or --cafile/--capath may be used at once.\n");
  544. return 1;
  545. }
  546. if(psk && !psk_identity){
  547. if(!quiet) fprintf(stderr, "Error: --psk-identity required if --psk used.\n");
  548. return 1;
  549. }
  550. mosquitto_lib_init();
  551. if(id_prefix){
  552. id = malloc(strlen(id_prefix)+10);
  553. if(!id){
  554. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  555. mosquitto_lib_cleanup();
  556. return 1;
  557. }
  558. snprintf(id, strlen(id_prefix)+10, "%s%d", id_prefix, getpid());
  559. }else if(!id){
  560. hostname[0] = '\0';
  561. gethostname(hostname, 256);
  562. hostname[255] = '\0';
  563. len = strlen("mosqpub/-") + 6 + strlen(hostname);
  564. id = malloc(len);
  565. if(!id){
  566. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  567. mosquitto_lib_cleanup();
  568. return 1;
  569. }
  570. snprintf(id, len, "mosqpub/%d-%s", getpid(), hostname);
  571. id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0';
  572. snprintf(id, MOSQ_MQTT_ID_MAX_LENGTH, "mosqpub/%d-%s", getpid(), hostname);
  573. id[MOSQ_MQTT_ID_MAX_LENGTH] = '\0';
  574. }
  575. mosq = mosquitto_new(id, true, NULL);
  576. if(!mosq){
  577. switch(errno){
  578. case ENOMEM:
  579. if(!quiet) fprintf(stderr, "Error: Out of memory.\n");
  580. break;
  581. case EINVAL:
  582. if(!quiet) fprintf(stderr, "Error: Invalid id.\n");
  583. break;
  584. }
  585. mosquitto_lib_cleanup();
  586. return 1;
  587. }
  588. if(debug){
  589. mosquitto_log_callback_set(mosq, my_log_callback);
  590. }
  591. if(will_topic && mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)){
  592. if(!quiet) fprintf(stderr, "Error: Problem setting will.\n");
  593. mosquitto_lib_cleanup();
  594. return 1;
  595. }
  596. if(username && mosquitto_username_pw_set(mosq, username, password)){
  597. if(!quiet) fprintf(stderr, "Error: Problem setting username and password.\n");
  598. mosquitto_lib_cleanup();
  599. return 1;
  600. }
  601. if((cafile || capath) && mosquitto_tls_set(mosq, cafile, capath, certfile, keyfile, NULL)){
  602. if(!quiet) fprintf(stderr, "Error: Problem setting TLS options.\n");
  603. mosquitto_lib_cleanup();
  604. return 1;
  605. }
  606. if(psk && mosquitto_tls_psk_set(mosq, psk, psk_identity, NULL)){
  607. if(!quiet) fprintf(stderr, "Error: Problem setting TLS-PSK options.\n");
  608. mosquitto_lib_cleanup();
  609. return 1;
  610. }
  611. mosquitto_connect_callback_set(mosq, my_connect_callback);
  612. mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);
  613. mosquitto_publish_callback_set(mosq, my_publish_callback);
  614. rc = mosquitto_connect(mosq, host, port, keepalive);
  615. if(rc){
  616. if(!quiet){
  617. if(rc == MOSQ_ERR_ERRNO){
  618. #ifndef WIN32
  619. strerror_r(errno, err, 1024);
  620. #else
  621. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errno, 0, (LPTSTR)&err, 1024, NULL);
  622. #endif
  623. fprintf(stderr, "Error: %s\n", err);
  624. }else{
  625. fprintf(stderr, "Unable to connect (%d).\n", rc);
  626. }
  627. }
  628. mosquitto_lib_cleanup();
  629. return rc;
  630. }
  631. do{
  632. if(mode == MSGMODE_STDIN_LINE && status == STATUS_CONNACK_RECVD){
  633. if(fgets(buf, 1024, stdin)){
  634. buf[strlen(buf)-1] = '\0';
  635. rc2 = mosquitto_publish(mosq, &mid_sent, topic, strlen(buf), buf, qos, retain);
  636. if(rc2){
  637. if(!quiet) fprintf(stderr, "Error: Publish returned %d, disconnecting.\n", rc2);
  638. mosquitto_disconnect(mosq);
  639. }
  640. }else if(feof(stdin) && disconnect_sent == false){
  641. mosquitto_disconnect(mosq);
  642. disconnect_sent = true;
  643. }
  644. }
  645. rc = mosquitto_loop(mosq, -1, 1);
  646. }while(rc == MOSQ_ERR_SUCCESS && connected);
  647. if(message && mode == MSGMODE_FILE){
  648. free(message);
  649. }
  650. mosquitto_destroy(mosq);
  651. mosquitto_lib_cleanup();
  652. if(rc){
  653. if(rc == MOSQ_ERR_ERRNO){
  654. fprintf(stderr, "Error: %s\n", strerror(errno));
  655. }else{
  656. fprintf(stderr, "Error: %s\n", mosquitto_strerror(rc));
  657. }
  658. }
  659. return rc;
  660. }