Annotation of mqtt/src/conn.c, revision 1.1.2.8
1.1.2.1 misho 1: #include "global.h"
2:
3:
4: /* ------------------------------------------------------------------- */
5:
6: /*
7: * mqtt_msgCONNECT() Create CONNECT message
8: *
9: * @buf = Message buffer
10: * @csConnID = ConnectID
11: * @csUser = Username if !=NULL
12: * @csPass = Password for Username, only if csUser is set
13: * @csWillTopic = Will Topic if !=NULL Will Flags set into message
14: * @csWillMessage = Will Message, may be NULL
15: * @ClrSess = Clear Session subscriptions after disconnect
16: * @WillQOS = Will QOS if csWillTopic is set
17: * @WillRetain = Will Retain Will Message if csWillTopic is set
18: * return: -1 error or >-1 message size for send
19: */
20: int
21: mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
22: const char *csUser, const char *csPass,
23: const char *csWillTopic, const char *csWillMessage,
1.1.2.3 misho 24: u_char ClrSess, u_char WillQOS, u_char WillRetain)
1.1.2.1 misho 25: {
26: int siz = 0;
27: struct mqtthdr *hdr;
28: mqtthdr_var_t *var, *cid, *topic, *wmsg, *user, *pass;
29: mqtthdr_protover_t *proto;
30: mqtthdr_connflgs_t *flags;
31: mqtt_v_t *ka;
32:
33: if (!buf || !csConnID)
34: return -1;
35: if (strlen(csConnID) > 23) {
36: mqtt_SetErr(EINVAL, "Error:: invalid argument ConnID is too long (max 23 bytes)");
37: return -1;
38: }
39: if (csUser && strlen(csUser) > 12) {
40: mqtt_SetErr(EINVAL, "Error:: invalid argument Username is too long (max 12 bytes)");
41: return -1;
42: }
43: if (csPass && strlen(csPass) > 12) {
44: mqtt_SetErr(EINVAL, "Error:: invalid argument Password is too long (max 12 bytes)");
45: return -1;
46: }
1.1.2.3 misho 47: if (WillQOS > MQTT_QOS_EXACTLY) {
1.1.2.1 misho 48: mqtt_SetErr(EINVAL, "Error:: invalid argument WillQOS - unknown QOS value");
49: return -1;
50: }
51:
52: if (mqtt_msgRealloc(buf, BUFSIZ) == -1)
53: return -1;
54: else {
55: hdr = (struct mqtthdr *) (buf->msg_base + siz);
1.1.2.2 misho 56: siz += sizeof(struct mqtthdr);
1.1.2.1 misho 57: var = (mqtthdr_var_t*) (buf->msg_base + siz);
58: siz += 8;
59: proto = buf->msg_base + siz;
60: siz++;
61: flags = (mqtthdr_connflgs_t*) (buf->msg_base + siz);
62: siz++;
63: ka = (mqtt_v_t*) (buf->msg_base + siz);
64: siz += sizeof(mqtt_v_t);
65: }
66:
67: /* fixed header */
1.1.2.4 misho 68: MQTTHDR_MSGINIT(hdr);
1.1.2.1 misho 69: hdr->mqtt_msg.type = MQTT_TYPE_CONNECT;
70: *hdr->mqtt_len = 0;
71:
72: /* variable header */
73: var->var_sb.sb.l = 6;
74: memcpy(var->var_data, MQTT_CONN_STR, 6);
75:
76: *proto = MQTT_PROTO_VER;
77:
78: /* CONNECT header */
79: flags->clean_sess = ClrSess ? 1 : 0;
80: if (csUser) {
81: flags->username = 1;
82: flags->password = csPass ? 1 : 0;
83: } else {
84: flags->username = 0;
85: flags->password = 0;
86: }
87: if (csWillTopic) {
88: flags->will_flg = 1;
89: flags->will_qos = WillQOS;
90: flags->will_retain = WillRetain ? 1 : 0;
91: } else {
92: flags->will_flg = 0;
93: flags->will_qos = 0;
94: flags->will_retain = 0;
95: }
96:
97: ka->sb.l = MQTT_KEEPALIVE;
98:
99: /* ConnID */
100: cid = (mqtthdr_var_t*) (buf->msg_base + siz);
101: cid->var_sb.val = htons(strlen(csConnID));
102: siz += MQTTHDR_VAR_SIZEOF(cid);
103: memcpy(cid->var_data, csConnID, ntohs(cid->var_sb.val));
104:
105: /* If Will Flags setup */
106: if (csWillTopic) {
107: topic = (mqtthdr_var_t*) (buf->msg_base + siz);
108: topic->var_sb.val = htons(strlen(csWillTopic));
109: memcpy(topic->var_data, csWillTopic, ntohs(topic->var_sb.val));
110: siz += MQTTHDR_VAR_SIZEOF(topic);
111:
112: wmsg = (mqtthdr_var_t*) (buf->msg_base + siz);
113: if (csWillMessage) {
114: wmsg->var_sb.val = htons(strlen(csWillMessage));
115: memcpy(wmsg->var_data, csWillMessage, ntohs(wmsg->var_sb.val));
116: } else
117: wmsg->var_sb.val = 0;
118: siz += MQTTHDR_VAR_SIZEOF(wmsg);
119: }
120:
121: /* If defined Username & Password */
122: if (csUser) {
123: user = (mqtthdr_var_t*) (buf->msg_base + siz);
124: user->var_sb.val = htons(strlen(csUser));
125: memcpy(user->var_data, csUser, ntohs(user->var_sb.val));
126: siz += MQTTHDR_VAR_SIZEOF(user);
127:
128: if (csPass) {
129: pass = (mqtthdr_var_t*) (buf->msg_base + siz);
130: pass->var_sb.val = htons(strlen(csPass));
131: memcpy(pass->var_data, csPass, ntohs(pass->var_sb.val));
132: siz += MQTTHDR_VAR_SIZEOF(pass);
133: }
134: }
135:
1.1.2.3 misho 136: *hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr));
1.1.2.1 misho 137: mqtt_msgRealloc(buf, siz);
138: return siz;
139: }
1.1.2.2 misho 140:
141: /*
142: * mqtt_msgCONNACK() Create CONNACK message
143: *
144: * @buf = Message buffer
145: * @retcode = Return code
146: * return: -1 error or >-1 message size for send
147: */
148: int
149: mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, u_char retcode)
150: {
151: int siz = 0;
152: struct mqtthdr *hdr;
153: mqtthdr_connack_t *ack;
154:
155: if (!buf)
156: return -1;
157: if (retcode > MQTT_RETCODE_DENIED) {
158: mqtt_SetErr(EINVAL, "Error:: invalid retcode");
159: return -1;
160: }
161:
162: if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtthdr_connack_t)) == -1)
163: return -1;
164: else {
165: hdr = (struct mqtthdr *) (buf->msg_base + siz);
166: siz += sizeof(struct mqtthdr);
167: ack = (mqtthdr_connack_t*) (buf->msg_base + siz);
168: siz += sizeof(mqtthdr_connack_t);
169: }
170:
171: /* fixed header */
1.1.2.4 misho 172: MQTTHDR_MSGINIT(hdr);
1.1.2.2 misho 173: hdr->mqtt_msg.type = MQTT_TYPE_CONNACK;
174: *hdr->mqtt_len = sizeof(mqtthdr_connack_t);
175:
176: /* CONNACK header */
177: ack->reserved = 0;
178: ack->retcode = retcode;
179:
180: return siz;
181: }
1.1.2.5 misho 182:
183: static int
184: _mqtt_msgSIMPLE_(mqtt_msg_t * __restrict buf, u_char cmd)
185: {
186: int siz = 0;
187: struct mqtthdr *hdr;
188:
189: if (!buf)
190: return -1;
191:
192: if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr)) == -1)
193: return -1;
194: else {
195: hdr = (struct mqtthdr *) (buf->msg_base + siz);
196: siz += sizeof(struct mqtthdr);
197: }
198:
199: /* fixed header */
200: MQTTHDR_MSGINIT(hdr);
201: hdr->mqtt_msg.type = cmd;
202: *hdr->mqtt_len = 0;
203:
204: return siz;
205: }
206:
207: /*
208: * mqtt_msgPINGREQ() Create PINGREQ message
209: *
210: * @buf = Message buffer
211: * return: -1 error or >-1 message size for send
212: */
213: int
214: mqtt_msgPINGREQ(mqtt_msg_t * __restrict buf)
215: {
216: return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_PINGREQ);
217: }
218:
219: /*
220: * mqtt_msgPINGRESP() Create PINGRESP message
221: *
222: * @buf = Message buffer
223: * return: -1 error or >-1 message size for send
224: */
225: int
226: mqtt_msgPINGRESP(mqtt_msg_t * __restrict buf)
227: {
228: return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_PINGRESP);
229: }
230:
231: /*
232: * mqtt_msgDISCONNECT() Create DISCONNECT message
233: *
234: * @buf = Message buffer
235: * return: -1 error or >-1 message size for send
236: */
237: int
238: mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf)
239: {
240: return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_DISCONNECT);
241: }
1.1.2.6 misho 242:
243: /* ============= decode ============ */
244:
245: /*
246: * mqtt_readCONNECT() Read elements from CONNECT message
247: *
248: * @buf = Message buffer
249: * @kasec = Keep Alive in seconds for current connection
250: * @psConnID = ConnectID
251: * @connLen = ConnectID length
252: * @psUser = Username if !=NULL
253: * @userLen = Username length
254: * @psPass = Password for Username, only if csUser is set
255: * @passLen = Password length
256: * @psWillTopic = Will Topic if !=NULL Will Flags set into message
257: * @topicLen = Will Topic length
258: * @psWillMessage = Will Message, may be NULL
259: * @msgLen = Will Message length
260: * return: .reserved == 1 is error or == 0 connection flags & msg ok
261: */
262: mqtthdr_connflgs_t
263: mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short *kasec, char * __restrict psConnID, int connLen,
264: char * __restrict psUser, int userLen, char * __restrict psPass, int passLen,
265: char * __restrict psWillTopic, int topicLen, char * __restrict psWillMessage, int msgLen)
266: {
267: mqtthdr_connflgs_t flg = MQTT_CONNFLGS_INIT;
268: struct mqtthdr *hdr;
269: mqtthdr_var_t *var;
270: mqtt_v_t *ka;
271: mqtthdr_protover_t *proto;
272: int len, ret;
273: caddr_t pos;
274:
275: if (!buf || !buf->msg_base || !buf->msg_len || !psConnID || !connLen)
276: return flg;
277:
1.1.2.8 ! misho 278: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNECT, &ret, &len);
! 279: if (!hdr)
1.1.2.6 misho 280: return flg;
281: if (len < 12) {
282: mqtt_SetErr(EINVAL, "Error:: short message length %d", len);
283: return flg;
284: } else {
285: pos = buf->msg_base + ret + 1;
286: var = (mqtthdr_var_t*) pos;
287: }
288: /* check init string & protocol */
289: if (var->var_sb.sb.l != 6 || strncmp(var->var_data, MQTT_CONN_STR, 6)) {
290: mqtt_SetErr(EINVAL, "Error:: invalid init string %.6s(%d)",
291: var->var_data, var->var_sb.sb.l);
292: return flg;
293: } else {
294: pos += var->var_sb.sb.l + sizeof(mqtt_v_t);
295: proto = (mqtthdr_protover_t*) pos;
296: }
297: if (*proto != MQTT_PROTO_VER) {
298: mqtt_SetErr(EINVAL, "Error:: invalid protocol version %d", *pos);
299: return flg;
300: } else
301: pos++;
302: flg = *(mqtthdr_connflgs_t*) pos;
303: pos++;
304: ka = (mqtt_v_t*) pos;
305: *kasec = ntohs(ka->val);
306: pos += sizeof(mqtt_v_t);
307:
308: len -= pos - (caddr_t) var;
309:
310: /* get ConnID */
311: var = (mqtthdr_var_t*) pos;
312: len -= MQTTHDR_VAR_SIZEOF(var);
313: if (len < 0) {
314: mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Connection ID %d", len);
315: flg.reserved = 1;
316: return flg;
317: } else {
318: memset(psConnID, 0, connLen--);
319: memcpy(psConnID, var->var_data, ntohs(var->var_sb.val) > connLen ? connLen : ntohs(var->var_sb.val));
320: pos += MQTTHDR_VAR_SIZEOF(var);
321: }
322:
323: /* get Willz */
324: if (flg.will_flg) {
325: var = (mqtthdr_var_t*) pos;
326: len -= MQTTHDR_VAR_SIZEOF(var);
327: if (len < 0) {
328: mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Will Topic %d", len);
329: flg.reserved = 1;
330: return flg;
331: } else {
332: if (psWillTopic && topicLen) {
333: memset(psWillTopic, 0, topicLen--);
334: memcpy(psWillTopic, var->var_data,
335: ntohs(var->var_sb.val) > topicLen ? topicLen : ntohs(var->var_sb.val));
336: }
337: pos += MQTTHDR_VAR_SIZEOF(var);
338: }
339:
340: var = (mqtthdr_var_t*) pos;
341: len -= MQTTHDR_VAR_SIZEOF(var);
342: if (len < 0) {
343: mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Will Message %d", len);
344: flg.reserved = 1;
345: return flg;
346: } else {
347: if (psWillMessage && msgLen) {
348: memset(psWillMessage, 0, msgLen--);
349: memcpy(psWillMessage, var->var_data,
350: ntohs(var->var_sb.val) > msgLen ? msgLen : ntohs(var->var_sb.val));
351: }
352: pos += MQTTHDR_VAR_SIZEOF(var);
353: }
354: }
355:
356: /* get User/Pass */
357: if (flg.username) {
358: var = (mqtthdr_var_t*) pos;
359: len -= MQTTHDR_VAR_SIZEOF(var);
360: if (len < 0) {
361: mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Username %d", len);
362: flg.reserved = 1;
363: return flg;
364: } else {
365: if (psUser && userLen) {
366: memset(psUser, 0, userLen--);
367: memcpy(psUser, var->var_data,
368: ntohs(var->var_sb.val) > userLen ? userLen : ntohs(var->var_sb.val));
369: }
370: pos += MQTTHDR_VAR_SIZEOF(var);
371: }
372: }
373: if (flg.password) {
374: var = (mqtthdr_var_t*) pos;
375: len -= MQTTHDR_VAR_SIZEOF(var);
376: if (len < 0) {
377: mqtt_SetErr(EINVAL, "Error:: unexpected EOM at Password %d", len);
378: flg.reserved = 1;
379: return flg;
380: } else {
381: if (psPass && passLen) {
382: memset(psPass, 0, passLen--);
383: memcpy(psPass, var->var_data,
384: ntohs(var->var_sb.val) > passLen ? passLen : ntohs(var->var_sb.val));
385: }
386: pos += MQTTHDR_VAR_SIZEOF(var);
387: }
388: }
389:
390: return flg;
391: }
1.1.2.7 misho 392:
393: /*
394: * mqtt_readCONNACK() Read CONNACK message
395: *
396: * @buf = Message buffer
397: * return: -1 error or >-1 CONNECT message return code
398: */
399: u_char
400: mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
401: {
402: int len, ret;
403: struct mqtthdr *hdr;
404: mqtthdr_connack_t *ack;
405: caddr_t pos;
406:
407: if (!buf || !buf->msg_base || !buf->msg_len)
408: return (u_char) -1;
409:
1.1.2.8 ! misho 410: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNACK, &ret, &len);
! 411: if (!hdr)
1.1.2.7 misho 412: return (u_char) -1;
413: if (len < sizeof(mqtthdr_connack_t)) {
414: mqtt_SetErr(EINVAL, "Error:: short message length %d", len);
415: return (u_char) -1;
416: } else {
417: pos = buf->msg_base + ret + 1;
418: ack = (mqtthdr_connack_t*) pos;
419: }
420:
421: if (ack->retcode > MQTT_RETCODE_DENIED) {
422: mqtt_SetErr(EINVAL, "Error:: invalid retcode %u", ack->retcode);
423: return (u_char) -1;
424: }
425:
426: return ack->retcode;
427: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>