pub_shared.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (c) 2009-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #include "config.h"
  15. #include <errno.h>
  16. #include <fcntl.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifndef WIN32
  21. #include <time.h>
  22. #else
  23. #include <process.h>
  24. #include <winsock2.h>
  25. #define snprintf sprintf_s
  26. #endif
  27. #include <mosquitto.h>
  28. #include <mqtt_protocol.h>
  29. #include "client_shared.h"
  30. #include "pub_shared.h"
  31. /* Global variables for use in callbacks. See sub_client.c for an example of
  32. * using a struct to hold variables for use in callbacks. */
  33. int mid_sent = -1;
  34. struct mosq_config cfg;
  35. void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
  36. {
  37. UNUSED(mosq);
  38. UNUSED(obj);
  39. UNUSED(level);
  40. printf("%s\n", str);
  41. }
  42. int load_stdin(void)
  43. {
  44. size_t pos = 0, rlen;
  45. char buf[1024];
  46. char *aux_message = NULL;
  47. cfg.pub_mode = MSGMODE_STDIN_FILE;
  48. while(!feof(stdin)){
  49. rlen = fread(buf, 1, 1024, stdin);
  50. aux_message = realloc(cfg.message, pos+rlen);
  51. if(!aux_message){
  52. err_printf(&cfg, "Error: Out of memory.\n");
  53. free(cfg.message);
  54. return 1;
  55. } else
  56. {
  57. cfg.message = aux_message;
  58. }
  59. memcpy(&(cfg.message[pos]), buf, rlen);
  60. pos += rlen;
  61. }
  62. if(pos > MQTT_MAX_PAYLOAD){
  63. err_printf(&cfg, "Error: Message length must be less than %u bytes.\n\n", MQTT_MAX_PAYLOAD);
  64. free(cfg.message);
  65. return 1;
  66. }
  67. cfg.msglen = (int )pos;
  68. if(!cfg.msglen){
  69. err_printf(&cfg, "Error: Zero length input.\n");
  70. return 1;
  71. }
  72. return 0;
  73. }
  74. int load_file(const char *filename)
  75. {
  76. size_t pos, rlen;
  77. FILE *fptr = NULL;
  78. long flen;
  79. fptr = fopen(filename, "rb");
  80. if(!fptr){
  81. err_printf(&cfg, "Error: Unable to open file \"%s\".\n", filename);
  82. return 1;
  83. }
  84. cfg.pub_mode = MSGMODE_FILE;
  85. fseek(fptr, 0, SEEK_END);
  86. flen = ftell(fptr);
  87. if(flen > MQTT_MAX_PAYLOAD){
  88. fclose(fptr);
  89. err_printf(&cfg, "Error: File must be less than %u bytes.\n\n", MQTT_MAX_PAYLOAD);
  90. free(cfg.message);
  91. return 1;
  92. }else if(flen == 0){
  93. fclose(fptr);
  94. cfg.message = NULL;
  95. cfg.msglen = 0;
  96. return 0;
  97. }else if(flen < 0){
  98. fclose(fptr);
  99. err_printf(&cfg, "Error: Unable to determine size of file \"%s\".\n", filename);
  100. return 1;
  101. }
  102. cfg.msglen = (int )flen;
  103. fseek(fptr, 0, SEEK_SET);
  104. cfg.message = malloc((size_t )cfg.msglen);
  105. if(!cfg.message){
  106. fclose(fptr);
  107. err_printf(&cfg, "Error: Out of memory.\n");
  108. return 1;
  109. }
  110. pos = 0;
  111. while(pos < (size_t)cfg.msglen){
  112. rlen = fread(&(cfg.message[pos]), sizeof(char), (size_t )cfg.msglen-pos, fptr);
  113. pos += rlen;
  114. }
  115. fclose(fptr);
  116. return 0;
  117. }