Annotation of libaitmqtt/src/conn.c, revision 1.1.1.1.2.2
1.1 misho 1: /*************************************************************************
2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.1.1.1.2.2! misho 6: * $Id: conn.c,v 1.1.1.1.2.1 2012/04/07 21:00:52 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.1.1.1.2.1 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.1 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: /*
50: * mqtt_msgCONNECT() Create CONNECT message
51: *
52: * @buf = Message buffer
53: * @csConnID = ConnectID
54: * @kasec = Keep alive timeout
55: * @csUser = Username if !=NULL
56: * @csPass = Password for Username, only if csUser is set
57: * @csWillTopic = Will Topic if !=NULL Will Flags set into message
58: * @csWillMessage = Will Message, may be NULL
59: * @ClrSess = Clear Session subscriptions after disconnect
60: * @WillQOS = Will QOS if csWillTopic is set
61: * @WillRetain = Will Retain Will Message if csWillTopic is set
62: * return: -1 error or >-1 message size for send
63: */
64: int
65: mqtt_msgCONNECT(mqtt_msg_t * __restrict buf, const char *csConnID,
66: u_short kasec, const char *csUser, const char *csPass,
67: const char *csWillTopic, const char *csWillMessage,
68: u_char ClrSess, u_char WillQOS, u_char WillRetain)
69: {
70: int siz = 0;
71: struct mqtthdr *hdr;
72: mqtthdr_var_t *var, *cid, *topic, *wmsg, *user, *pass;
73: mqtthdr_protover_t *proto;
74: mqtthdr_connflgs_t *flags;
75: mqtt_len_t *ka;
76:
77: if (!buf || !csConnID)
78: return -1;
79: if (strlen(csConnID) > 23) {
1.1.1.1.2.1 misho 80: mqtt_SetErr(EINVAL, "Invalid argument ConnID is too long (max 23 bytes)");
1.1 misho 81: return -1;
82: }
83: if (csUser && strlen(csUser) > 12) {
1.1.1.1.2.1 misho 84: mqtt_SetErr(EINVAL, "Invalid argument Username is too long (max 12 bytes)");
1.1 misho 85: return -1;
86: }
87: if (csPass && strlen(csPass) > 12) {
1.1.1.1.2.1 misho 88: mqtt_SetErr(EINVAL, "Invalid argument Password is too long (max 12 bytes)");
1.1 misho 89: return -1;
90: }
91: if (WillQOS > MQTT_QOS_EXACTLY) {
1.1.1.1.2.1 misho 92: mqtt_SetErr(EINVAL, "Invalid argument WillQOS - unknown QOS value");
1.1 misho 93: return -1;
94: }
95:
96: if (mqtt_msgRealloc(buf, BUFSIZ) == -1)
97: return -1;
98: else {
99: hdr = (struct mqtthdr *) (buf->msg_base + siz);
100: siz += sizeof(struct mqtthdr);
101: var = (mqtthdr_var_t*) (buf->msg_base + siz);
102: siz += 8;
103: proto = buf->msg_base + siz;
104: siz++;
105: flags = (mqtthdr_connflgs_t*) (buf->msg_base + siz);
106: siz++;
107: ka = (mqtt_len_t*) (buf->msg_base + siz);
108: siz += sizeof(mqtt_len_t);
109: }
110:
111: /* fixed header */
112: MQTTHDR_MSGINIT(hdr);
113: hdr->mqtt_msg.type = MQTT_TYPE_CONNECT;
114: *hdr->mqtt_len = 0;
115:
116: /* variable header */
117: var->var_sb.sb.l = 6;
118: memcpy(var->var_data, MQTT_CONN_STR, 6);
119:
120: *proto = MQTT_PROTO_VER;
121:
122: /* CONNECT header */
123: flags->clean_sess = ClrSess ? 1 : 0;
124: if (csUser && *csUser) {
125: flags->username = 1;
126: flags->password = csPass ? 1 : 0;
127: } else {
128: flags->username = 0;
129: flags->password = 0;
130: }
131: if (csWillTopic && *csWillTopic) {
132: flags->will_flg = 1;
133: flags->will_qos = WillQOS;
134: flags->will_retain = WillRetain ? 1 : 0;
135: } else {
136: flags->will_flg = 0;
137: flags->will_qos = 0;
138: flags->will_retain = 0;
139: }
140:
141: ka->val = kasec ? htons(kasec) : htons(MQTT_KEEPALIVE);
142:
143: /* ConnID */
144: cid = (mqtthdr_var_t*) (buf->msg_base + siz);
145: cid->var_sb.val = htons(strlen(csConnID));
146: siz += MQTTHDR_VAR_SIZEOF(cid);
147: memcpy(cid->var_data, csConnID, ntohs(cid->var_sb.val));
148:
149: /* If Will Flags setup */
150: if (csWillTopic && *csWillTopic) {
151: topic = (mqtthdr_var_t*) (buf->msg_base + siz);
152: topic->var_sb.val = htons(strlen(csWillTopic));
153: memcpy(topic->var_data, csWillTopic, ntohs(topic->var_sb.val));
154: siz += MQTTHDR_VAR_SIZEOF(topic);
155:
156: wmsg = (mqtthdr_var_t*) (buf->msg_base + siz);
157: if (csWillMessage && *csWillMessage) {
158: wmsg->var_sb.val = htons(strlen(csWillMessage));
159: memcpy(wmsg->var_data, csWillMessage, ntohs(wmsg->var_sb.val));
160: } else
161: wmsg->var_sb.val = 0;
162: siz += MQTTHDR_VAR_SIZEOF(wmsg);
163: }
164:
165: /* If defined Username & Password */
166: if (csUser && *csUser) {
167: user = (mqtthdr_var_t*) (buf->msg_base + siz);
168: user->var_sb.val = htons(strlen(csUser));
169: memcpy(user->var_data, csUser, ntohs(user->var_sb.val));
170: siz += MQTTHDR_VAR_SIZEOF(user);
171:
172: if (csPass && *csPass) {
173: pass = (mqtthdr_var_t*) (buf->msg_base + siz);
174: pass->var_sb.val = htons(strlen(csPass));
175: memcpy(pass->var_data, csPass, ntohs(pass->var_sb.val));
176: siz += MQTTHDR_VAR_SIZEOF(pass);
177: }
178: }
179:
180: *hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr));
181: return siz;
182: }
183:
184: /*
185: * mqtt_msgCONNACK() Create CONNACK message
186: *
187: * @buf = Message buffer
188: * @retcode = Return code
189: * return: -1 error or >-1 message size for send
190: */
191: int
192: mqtt_msgCONNACK(mqtt_msg_t * __restrict buf, u_char retcode)
193: {
194: int siz = 0;
195: struct mqtthdr *hdr;
196: mqtthdr_connack_t *ack;
197:
198: if (!buf)
199: return -1;
200: if (retcode > MQTT_RETCODE_DENIED) {
1.1.1.1.2.1 misho 201: mqtt_SetErr(EINVAL, "Invalid retcode");
1.1 misho 202: return -1;
203: }
204:
205: if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr) + sizeof(mqtthdr_connack_t)) == -1)
206: return -1;
207: else {
208: hdr = (struct mqtthdr *) (buf->msg_base + siz);
209: siz += sizeof(struct mqtthdr);
210: ack = (mqtthdr_connack_t*) (buf->msg_base + siz);
211: siz += sizeof(mqtthdr_connack_t);
212: }
213:
214: /* fixed header */
215: MQTTHDR_MSGINIT(hdr);
216: hdr->mqtt_msg.type = MQTT_TYPE_CONNACK;
217: *hdr->mqtt_len = sizeof(mqtthdr_connack_t);
218:
219: /* CONNACK header */
220: ack->reserved = 0;
221: ack->retcode = retcode;
222:
223: return siz;
224: }
225:
226: static int
227: _mqtt_msgSIMPLE_(mqtt_msg_t * __restrict buf, u_char cmd)
228: {
229: int siz = 0;
230: struct mqtthdr *hdr;
231:
232: if (!buf)
233: return -1;
234:
235: if (mqtt_msgRealloc(buf, sizeof(struct mqtthdr)) == -1)
236: return -1;
237: else {
238: hdr = (struct mqtthdr *) (buf->msg_base + siz);
239: siz += sizeof(struct mqtthdr);
240: }
241:
242: /* fixed header */
243: MQTTHDR_MSGINIT(hdr);
244: hdr->mqtt_msg.type = cmd;
245: *hdr->mqtt_len = 0;
246:
247: return siz;
248: }
249:
250: /*
251: * mqtt_msgPINGREQ() Create PINGREQ message
252: *
253: * @buf = Message buffer
254: * return: -1 error or >-1 message size for send
255: */
256: int
257: mqtt_msgPINGREQ(mqtt_msg_t * __restrict buf)
258: {
259: return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_PINGREQ);
260: }
261:
262: /*
263: * mqtt_msgPINGRESP() Create PINGRESP message
264: *
265: * @buf = Message buffer
266: * return: -1 error or >-1 message size for send
267: */
268: int
269: mqtt_msgPINGRESP(mqtt_msg_t * __restrict buf)
270: {
271: return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_PINGRESP);
272: }
273:
274: /*
275: * mqtt_msgDISCONNECT() Create DISCONNECT message
276: *
277: * @buf = Message buffer
278: * return: -1 error or >-1 message size for send
279: */
280: int
281: mqtt_msgDISCONNECT(mqtt_msg_t * __restrict buf)
282: {
283: return _mqtt_msgSIMPLE_(buf, MQTT_TYPE_DISCONNECT);
284: }
285:
286: /* ============= decode ============ */
287:
288: /*
289: * mqtt_readCONNECT() Read elements from CONNECT message
290: *
291: * @buf = Message buffer
292: * @kasec = Keep Alive in seconds for current connection
293: * @psConnID = ConnectID
294: * @connLen = ConnectID length
295: * @psUser = Username if !=NULL
296: * @userLen = Username length
297: * @psPass = Password for Username, only if csUser is set
298: * @passLen = Password length
299: * @psWillTopic = Will Topic if !=NULL Will Flags set into message and must be free()
300: * @psWillMessage = Will Message, may be NULL if !NULL must be free() after use!
301: * return: .reserved == 1 is error or == 0 connection flags & msg ok
302: */
303: mqtthdr_connack_t
304: mqtt_readCONNECT(mqtt_msg_t * __restrict buf, u_short *kasec, char * __restrict psConnID, int connLen,
305: char * __restrict psUser, int userLen, char * __restrict psPass, int passLen,
306: char ** __restrict psWillTopic, char ** __restrict psWillMessage)
307: {
308: mqtthdr_connflgs_t flg = { MQTT_CONNFLGS_INIT };
309: mqtthdr_connack_t cack = { 1, MQTT_RETCODE_DENIED };
310: struct mqtthdr *hdr;
311: mqtthdr_var_t *var;
312: mqtt_len_t *ka;
313: mqtthdr_protover_t *proto;
314: int len, ret;
315: caddr_t pos;
316:
317: if (!buf || !buf->msg_base || !buf->msg_len || !psConnID || !connLen)
318: return cack;
319:
320: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNECT, &ret, &len);
321: if (!hdr)
322: return cack;
323: if (len < 12) {
1.1.1.1.2.1 misho 324: mqtt_SetErr(EINVAL, "Short message length %d", len);
1.1 misho 325: return cack;
326: } else {
327: pos = buf->msg_base + ret + 1;
328: var = (mqtthdr_var_t*) pos;
329: }
330: /* check init string & protocol */
331: if (var->var_sb.sb.l != 6 || strncmp((char*) var->var_data, MQTT_CONN_STR, 6)) {
1.1.1.1.2.1 misho 332: mqtt_SetErr(EINVAL, "Invalid init string %.6s(%d)",
1.1 misho 333: var->var_data, var->var_sb.sb.l);
334: cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL;
335: return cack;
336: } else {
337: pos += var->var_sb.sb.l + sizeof(mqtt_len_t);
338: proto = (mqtthdr_protover_t*) pos;
339: }
340: if (*proto != MQTT_PROTO_VER) {
1.1.1.1.2.1 misho 341: mqtt_SetErr(EINVAL, "Invalid protocol version %d", *pos);
1.1 misho 342: cack.retcode = MQTT_RETCODE_REFUSE_VER;
343: return cack;
344: } else
345: pos++;
346: flg = *(mqtthdr_connflgs_t*) pos;
347: pos++;
348: ka = (mqtt_len_t*) pos;
349: *kasec = ntohs(ka->val);
350: pos += sizeof(mqtt_len_t);
351:
352: len -= pos - (caddr_t) var;
353:
354: /* get ConnID */
355: var = (mqtthdr_var_t*) pos;
356: len -= MQTTHDR_VAR_SIZEOF(var);
357: if (len < 0 || var->var_sb.sb.l > 23) {
1.1.1.1.2.1 misho 358: mqtt_SetErr(EINVAL, "Unexpected EOM at Connection ID %d", len);
1.1 misho 359: cack.retcode = MQTT_RETCODE_REFUSE_ID;
360: return cack;
361: } else {
362: memset(psConnID, 0, connLen--);
363: memcpy(psConnID, var->var_data, ntohs(var->var_sb.val) > connLen ? connLen : ntohs(var->var_sb.val));
364: pos += MQTTHDR_VAR_SIZEOF(var);
365: }
366:
367: /* get Willz */
368: if (flg.will_flg) {
369: var = (mqtthdr_var_t*) pos;
370: len -= MQTTHDR_VAR_SIZEOF(var);
371: if (len < 0) {
1.1.1.1.2.1 misho 372: mqtt_SetErr(EINVAL, "Unexpected EOM at Will Topic %d", len);
1.1 misho 373: cack.retcode = MQTT_RETCODE_REFUSE_ID;
374: return cack;
375: } else {
376: if (psWillTopic) {
377: *psWillTopic = malloc(ntohs(var->var_sb.val) + 1);
378: if (!*psWillTopic) {
379: LOGERR;
380: cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL;
381: return cack;
382: } else
383: memset(*psWillTopic, 0, ntohs(var->var_sb.val) + 1);
384: memcpy(*psWillTopic, var->var_data, ntohs(var->var_sb.val));
385: }
386: pos += MQTTHDR_VAR_SIZEOF(var);
387: }
388:
389: var = (mqtthdr_var_t*) pos;
390: len -= MQTTHDR_VAR_SIZEOF(var);
391: if (len < 0) {
1.1.1.1.2.1 misho 392: mqtt_SetErr(EINVAL, "Unexpected EOM at Will Message %d", len);
1.1 misho 393: cack.retcode = MQTT_RETCODE_REFUSE_ID;
394: return cack;
395: } else {
396: if (psWillMessage) {
397: *psWillMessage = malloc(ntohs(var->var_sb.val) + 1);
398: if (!*psWillMessage) {
399: LOGERR;
400: cack.retcode = MQTT_RETCODE_REFUSE_UNAVAIL;
401: return cack;
402: } else
403: memset(*psWillMessage, 0, ntohs(var->var_sb.val) + 1);
404: memcpy(*psWillMessage, var->var_data, ntohs(var->var_sb.val));
405: }
406: pos += MQTTHDR_VAR_SIZEOF(var);
407: }
408: }
409:
410: /* get User/Pass */
411: if (flg.username) {
412: var = (mqtthdr_var_t*) pos;
413: len -= MQTTHDR_VAR_SIZEOF(var);
414: if (len < 0 || var->var_sb.sb.l > 12) {
1.1.1.1.2.1 misho 415: mqtt_SetErr(EINVAL, "Unexpected EOM at Username %d", len);
1.1 misho 416: cack.retcode = MQTT_RETCODE_REFUSE_USERPASS;
417: return cack;
418: } else {
419: if (psUser && userLen) {
420: memset(psUser, 0, userLen--);
421: memcpy(psUser, var->var_data,
422: ntohs(var->var_sb.val) > userLen ? userLen : ntohs(var->var_sb.val));
423: }
424: pos += MQTTHDR_VAR_SIZEOF(var);
425: }
426: }
427: if (flg.password) {
428: var = (mqtthdr_var_t*) pos;
429: len -= MQTTHDR_VAR_SIZEOF(var);
430: if (len < 0 || var->var_sb.sb.l > 12) {
1.1.1.1.2.1 misho 431: mqtt_SetErr(EINVAL, "Unexpected EOM at Password %d", len);
1.1 misho 432: cack.retcode = MQTT_RETCODE_REFUSE_USERPASS;
433: return cack;
434: } else {
435: if (psPass && passLen) {
436: memset(psPass, 0, passLen--);
437: memcpy(psPass, var->var_data,
438: ntohs(var->var_sb.val) > passLen ? passLen : ntohs(var->var_sb.val));
439: }
440: pos += MQTTHDR_VAR_SIZEOF(var);
441: }
442: }
443:
444: flg.reserved = 0;
445: cack.reserved = flg.flags;
446: cack.retcode = MQTT_RETCODE_ACCEPTED;
447: return cack;
448: }
449:
450: /*
451: * mqtt_readCONNACK() Read CONNACK message
452: *
453: * @buf = Message buffer
454: * return: -1 error or >-1 CONNECT message return code
455: */
456: u_char
457: mqtt_readCONNACK(mqtt_msg_t * __restrict buf)
458: {
459: int len, ret;
460: struct mqtthdr *hdr;
461: mqtthdr_connack_t *ack;
462: caddr_t pos;
463:
464: if (!buf || !buf->msg_base || !buf->msg_len)
465: return (u_char) -1;
466:
467: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_CONNACK, &ret, &len);
468: if (!hdr)
469: return (u_char) -1;
470: if (len < sizeof(mqtthdr_connack_t)) {
1.1.1.1.2.1 misho 471: mqtt_SetErr(EINVAL, "Short message length %d", len);
1.1 misho 472: return (u_char) -1;
473: } else {
474: pos = buf->msg_base + ret + 1;
475: ack = (mqtthdr_connack_t*) pos;
476: }
477:
478: if (ack->retcode > MQTT_RETCODE_DENIED) {
1.1.1.1.2.1 misho 479: mqtt_SetErr(EINVAL, "Invalid retcode %u", ack->retcode);
1.1 misho 480: return (u_char) -1;
481: }
482:
483: return ack->retcode;
484: }
485:
486: /*
487: * mqtt_readDISCONNECT() Read DISCONNECT message
488: *
489: * @buf = Message buffer
490: * return: -1 error, 0 ok, >0 undefined result
491: */
492: int
493: mqtt_readDISCONNECT(mqtt_msg_t * __restrict buf)
494: {
495: int len, ret;
496: struct mqtthdr *hdr;
497:
498: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_DISCONNECT, &ret, &len);
499: if (!hdr || ret != 1)
500: return -1;
501:
502: return len;
503: }
504:
505: /*
506: * mqtt_readPINGREQ() Read PINGREQ message
507: *
508: * @buf = Message buffer
509: * return: -1 error, 0 ok, >0 undefined result
510: */
511: int
512: mqtt_readPINGREQ(mqtt_msg_t * __restrict buf)
513: {
514: int len, ret;
515: struct mqtthdr *hdr;
516:
517: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PINGREQ, &ret, &len);
518: if (!hdr || ret != 1)
519: return -1;
520:
521: return len;
522: }
523:
524: /*
525: * mqtt_readPINGRESP() Read PINGRESP message
526: *
527: * @buf = Message buffer
528: * return: -1 error, 0 ok, >0 undefined result
529: */
530: int
531: mqtt_readPINGRESP(mqtt_msg_t * __restrict buf)
532: {
533: int len, ret;
534: struct mqtthdr *hdr;
535:
536: hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PINGRESP, &ret, &len);
537: if (!hdr || ret != 1)
538: return -1;
539:
540: return len;
541: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>