/src/haproxy/include/haproxy/mqtt.h
Line | Count | Source |
1 | | /* |
2 | | * include/haproxt/mqtt.h |
3 | | * This file contains structure declarations for MQTT protocol. |
4 | | * |
5 | | * Copyright 2020 Baptiste Assmann <bedis9@gmail.com> |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or |
8 | | * modify it under the terms of the GNU Lesser General Public |
9 | | * License as published by the Free Software Foundation, version 2.1 |
10 | | * exclusively. |
11 | | * |
12 | | * This library is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | | * Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public |
18 | | * License along with this library; if not, write to the Free Software |
19 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | | */ |
21 | | |
22 | | #ifndef _HAPROXY_MQTT_H |
23 | | #define _HAPROXY_MQTT_H |
24 | | |
25 | | #include <import/ist.h> |
26 | | |
27 | | #include <haproxy/mqtt-t.h> |
28 | | #include <haproxy/tools.h> |
29 | | |
30 | | /* expected flags for control packets */ |
31 | | extern uint8_t mqtt_cpt_flags[MQTT_CPT_ENTRIES]; |
32 | | |
33 | | /* MQTT field string names */ |
34 | | extern const struct ist mqtt_fields_string[MQTT_FN_ENTRIES]; |
35 | | |
36 | | /* list of supported capturable field names for each MQTT control packet type */ |
37 | | extern const uint64_t mqtt_fields_per_packet[MQTT_CPT_ENTRIES]; |
38 | | |
39 | | int mqtt_validate_message(const struct ist msg, struct mqtt_pkt *mpkt); |
40 | | struct ist mqtt_field_value(const struct ist msg, int type, int fieldname_id); |
41 | | |
42 | | /* |
43 | | * Return a MQTT packet type ID based found in <str>. |
44 | | * <str> can be a number or a string and returned value will always be the numeric value. |
45 | | * |
46 | | * If <str> can't be translated into an ID, then MQTT_CPT_INVALID (0) is returned. |
47 | | */ |
48 | | static inline int mqtt_typeid(struct ist str) |
49 | 0 | { |
50 | 0 | int id; |
51 | |
|
52 | 0 | id = strl2ui(str.ptr, istlen(str)); |
53 | 0 | if ((id >= MQTT_CPT_CONNECT) && (id < MQTT_CPT_ENTRIES)) |
54 | 0 | return id; |
55 | | |
56 | 0 | else if (isteqi(str, ist("CONNECT")) != 0) |
57 | 0 | return MQTT_CPT_CONNECT; |
58 | 0 | else if (isteqi(str, ist("CONNACK")) != 0) |
59 | 0 | return MQTT_CPT_CONNACK; |
60 | 0 | else if (isteqi(str, ist("PUBLISH")) != 0) |
61 | 0 | return MQTT_CPT_PUBLISH; |
62 | 0 | else if (isteqi(str, ist("PUBACK")) != 0) |
63 | 0 | return MQTT_CPT_PUBACK; |
64 | 0 | else if (isteqi(str, ist("PUBREC")) != 0) |
65 | 0 | return MQTT_CPT_PUBREC; |
66 | 0 | else if (isteqi(str, ist("PUBREL")) != 0) |
67 | 0 | return MQTT_CPT_PUBREL; |
68 | 0 | else if (isteqi(str, ist("PUBCOMP")) != 0) |
69 | 0 | return MQTT_CPT_PUBCOMP; |
70 | 0 | else if (isteqi(str, ist("SUBSCRIBE")) != 0) |
71 | 0 | return MQTT_CPT_SUBSCRIBE; |
72 | 0 | else if (isteqi(str, ist("SUBACK")) != 0) |
73 | 0 | return MQTT_CPT_SUBACK; |
74 | 0 | else if (isteqi(str, ist("UNSUBSCRIBE")) != 0) |
75 | 0 | return MQTT_CPT_UNSUBSCRIBE; |
76 | 0 | else if (isteqi(str, ist("UNSUBACK")) != 0) |
77 | 0 | return MQTT_CPT_UNSUBACK; |
78 | 0 | else if (isteqi(str, ist("PINGREQ")) != 0) |
79 | 0 | return MQTT_CPT_PINGREQ; |
80 | 0 | else if (isteqi(str, ist("PINGRESP")) != 0) |
81 | 0 | return MQTT_CPT_PINGRESP; |
82 | 0 | else if (isteqi(str, ist("DISCONNECT")) != 0) |
83 | 0 | return MQTT_CPT_DISCONNECT; |
84 | 0 | else if (isteqi(str, ist("AUTH")) != 0) |
85 | 0 | return MQTT_CPT_AUTH; |
86 | | |
87 | 0 | return MQTT_CPT_INVALID; |
88 | 0 | } Unexecuted instantiation: sample.c:mqtt_typeid Unexecuted instantiation: mqtt.c:mqtt_typeid |
89 | | |
90 | | /* |
91 | | * validate that <str> is a field that can be extracted from a <type> MQTT packet |
92 | | * |
93 | | * return the field name ID (MQTT_FN_*) if a match is found, MQTT_FN_INVALID (0) otherwise. |
94 | | */ |
95 | | static inline int mqtt_check_type_fieldname(int type, struct ist str) |
96 | 0 | { |
97 | 0 | int i, id = MQTT_FN_INVALID; |
98 | |
|
99 | 0 | for (i = 0; i < MQTT_FN_ENTRIES; i++) { |
100 | 0 | if (isteqi(str, mqtt_fields_string[i])) { |
101 | 0 | if (mqtt_fields_per_packet[type] & (1ULL << i)) |
102 | 0 | id = i; |
103 | 0 | break; |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | 0 | return id; |
108 | |
|
109 | 0 | } Unexecuted instantiation: sample.c:mqtt_check_type_fieldname Unexecuted instantiation: mqtt.c:mqtt_check_type_fieldname |
110 | | |
111 | | #endif /* _HAPROXY_MQTT_H */ |
112 | | |
113 | | /* |
114 | | * Local variables: |
115 | | * c-indent-level: 8 |
116 | | * c-basic-offset: 8 |
117 | | * End: |
118 | | */ |