client_props.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. Copyright (c) 2018-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 <unistd.h>
  22. #include <strings.h>
  23. #else
  24. #include <process.h>
  25. #include <winsock2.h>
  26. #define snprintf sprintf_s
  27. #define strncasecmp _strnicmp
  28. #endif
  29. #include "mosquitto.h"
  30. #include "mqtt_protocol.h"
  31. #include "client_shared.h"
  32. enum prop_type
  33. {
  34. PROP_TYPE_BYTE,
  35. PROP_TYPE_INT16,
  36. PROP_TYPE_INT32,
  37. PROP_TYPE_BINARY,
  38. PROP_TYPE_STRING,
  39. PROP_TYPE_STRING_PAIR
  40. };
  41. /* This parses property inputs. It should work for any command type, but is limited at the moment.
  42. *
  43. * Format:
  44. *
  45. * command property value
  46. * command property key value
  47. *
  48. * Example:
  49. *
  50. * publish message-expiry-interval 32
  51. * connect user-property key value
  52. */
  53. int cfg_parse_property(struct mosq_config *cfg, int argc, char *argv[], int *idx)
  54. {
  55. char *cmdname = NULL, *propname = NULL;
  56. char *key = NULL, *value = NULL;
  57. int cmd, identifier, type;
  58. mosquitto_property **proplist;
  59. int rc;
  60. long tmpl;
  61. size_t szt;
  62. /* idx now points to "command" */
  63. if((*idx)+2 > argc-1){
  64. /* Not enough args */
  65. fprintf(stderr, "Error: --property argument given but not enough arguments specified.\n\n");
  66. return MOSQ_ERR_INVAL;
  67. }
  68. cmdname = argv[*idx];
  69. if(mosquitto_string_to_command(cmdname, &cmd)){
  70. fprintf(stderr, "Error: Invalid command given in --property argument.\n\n");
  71. return MOSQ_ERR_INVAL;
  72. }
  73. propname = argv[(*idx)+1];
  74. if(mosquitto_string_to_property_info(propname, &identifier, &type)){
  75. fprintf(stderr, "Error: Invalid property name given in --property argument.\n\n");
  76. return MOSQ_ERR_INVAL;
  77. }
  78. if(mosquitto_property_check_command(cmd, identifier)){
  79. fprintf(stderr, "Error: %s property not allowed for %s in --property argument.\n\n", propname, cmdname);
  80. return MOSQ_ERR_INVAL;
  81. }
  82. if(identifier == MQTT_PROP_USER_PROPERTY){
  83. if((*idx)+3 > argc-1){
  84. /* Not enough args */
  85. fprintf(stderr, "Error: --property argument given but not enough arguments specified.\n\n");
  86. return MOSQ_ERR_INVAL;
  87. }
  88. key = argv[(*idx)+2];
  89. value = argv[(*idx)+3];
  90. (*idx) += 3;
  91. }else{
  92. value = argv[(*idx)+2];
  93. (*idx) += 2;
  94. }
  95. switch(cmd){
  96. case CMD_CONNECT:
  97. proplist = &cfg->connect_props;
  98. break;
  99. case CMD_PUBLISH:
  100. if(identifier == MQTT_PROP_TOPIC_ALIAS){
  101. cfg->have_topic_alias = true;
  102. }
  103. if(identifier == MQTT_PROP_SUBSCRIPTION_IDENTIFIER){
  104. fprintf(stderr, "Error: %s property not supported for %s in --property argument.\n\n", propname, cmdname);
  105. return MOSQ_ERR_INVAL;
  106. }
  107. proplist = &cfg->publish_props;
  108. break;
  109. case CMD_SUBSCRIBE:
  110. if(identifier != MQTT_PROP_SUBSCRIPTION_IDENTIFIER && identifier != MQTT_PROP_USER_PROPERTY){
  111. fprintf(stderr, "Error: %s property not supported for %s in --property argument.\n\n", propname, cmdname);
  112. return MOSQ_ERR_NOT_SUPPORTED;
  113. }
  114. proplist = &cfg->subscribe_props;
  115. break;
  116. case CMD_UNSUBSCRIBE:
  117. proplist = &cfg->unsubscribe_props;
  118. break;
  119. case CMD_DISCONNECT:
  120. proplist = &cfg->disconnect_props;
  121. break;
  122. case CMD_AUTH:
  123. fprintf(stderr, "Error: %s property not supported for %s in --property argument.\n\n", propname, cmdname);
  124. return MOSQ_ERR_NOT_SUPPORTED;
  125. case CMD_WILL:
  126. proplist = &cfg->will_props;
  127. break;
  128. case CMD_PUBACK:
  129. case CMD_PUBREC:
  130. case CMD_PUBREL:
  131. case CMD_PUBCOMP:
  132. case CMD_SUBACK:
  133. case CMD_UNSUBACK:
  134. fprintf(stderr, "Error: %s property not supported for %s in --property argument.\n\n", propname, cmdname);
  135. return MOSQ_ERR_NOT_SUPPORTED;
  136. default:
  137. return MOSQ_ERR_INVAL;
  138. }
  139. switch(type){
  140. case MQTT_PROP_TYPE_BYTE:
  141. tmpl = atol(value);
  142. if(tmpl < 0 || tmpl > UINT8_MAX){
  143. fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname);
  144. return MOSQ_ERR_INVAL;
  145. }
  146. rc = mosquitto_property_add_byte(proplist, identifier, (uint8_t )tmpl);
  147. break;
  148. case MQTT_PROP_TYPE_INT16:
  149. tmpl = atol(value);
  150. if(tmpl < 0 || tmpl > UINT16_MAX){
  151. fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname);
  152. return MOSQ_ERR_INVAL;
  153. }
  154. rc = mosquitto_property_add_int16(proplist, identifier, (uint16_t )tmpl);
  155. break;
  156. case MQTT_PROP_TYPE_INT32:
  157. tmpl = atol(value);
  158. if(tmpl < 0 || tmpl > UINT32_MAX){
  159. fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname);
  160. return MOSQ_ERR_INVAL;
  161. }
  162. rc = mosquitto_property_add_int32(proplist, identifier, (uint32_t )tmpl);
  163. break;
  164. case MQTT_PROP_TYPE_VARINT:
  165. tmpl = atol(value);
  166. if(tmpl < 0 || tmpl > UINT32_MAX){
  167. fprintf(stderr, "Error: Property value (%ld) out of range for property %s.\n\n", tmpl, propname);
  168. return MOSQ_ERR_INVAL;
  169. }
  170. rc = mosquitto_property_add_varint(proplist, identifier, (uint32_t )tmpl);
  171. break;
  172. case MQTT_PROP_TYPE_BINARY:
  173. szt = strlen(value);
  174. if(szt > UINT16_MAX){
  175. fprintf(stderr, "Error: Property value too long for property %s.\n\n", propname);
  176. return MOSQ_ERR_INVAL;
  177. }
  178. rc = mosquitto_property_add_binary(proplist, identifier, value, (uint16_t )szt);
  179. break;
  180. case MQTT_PROP_TYPE_STRING:
  181. rc = mosquitto_property_add_string(proplist, identifier, value);
  182. break;
  183. case MQTT_PROP_TYPE_STRING_PAIR:
  184. rc = mosquitto_property_add_string_pair(proplist, identifier, key, value);
  185. break;
  186. default:
  187. return MOSQ_ERR_INVAL;
  188. }
  189. if(rc){
  190. fprintf(stderr, "Error adding property %s %d\n", propname, type);
  191. return rc;
  192. }
  193. return MOSQ_ERR_SUCCESS;
  194. }