Annotation of libaitmqtt/src/aitmqtt.c, revision 1.1.1.1
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 $
6: * $Id: aitsched.c,v 1.5 2012/01/24 21:59:47 misho Exp $
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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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: #pragma GCC visibility push(hidden)
50:
51: int mqtt_Errno;
52: char mqtt_Error[STRSIZ];
53:
54: #pragma GCC visibility pop
55:
56: //
57: // Error maintenance functions ...
58: //
59:
60: // mqtt_GetErrno() Get error code of last operation
61: inline int
62: mqtt_GetErrno()
63: {
64: return mqtt_Errno;
65: }
66:
67: // mqtt_GetError() Get error text of last operation
68: inline const char *
69: mqtt_GetError()
70: {
71: return mqtt_Error;
72: }
73:
74: // mqtt_SetErr() Set error to variables for internal use!!!
75: inline void
76: mqtt_SetErr(int eno, char *estr, ...)
77: {
78: va_list lst;
79:
80: mqtt_Errno = eno;
81: memset(mqtt_Error, 0, sizeof mqtt_Error);
82: va_start(lst, estr);
83: vsnprintf(mqtt_Error, sizeof mqtt_Error, estr, lst);
84: va_end(lst);
85: }
86:
87: #pragma GCC visibility push(hidden)
88: // _mqtt_readHEADER() read fixed header from MQTT message
89: inline struct mqtthdr *
90: _mqtt_readHEADER(mqtt_msg_t * __restrict buf, u_char cmd, int *bytes, int *len)
91: {
92: struct mqtthdr *hdr;
93:
94: if (!buf || !buf->msg_base || !buf->msg_len)
95: return NULL;
96:
97: hdr = (struct mqtthdr*) buf->msg_base;
98: if (hdr->mqtt_msg.type != cmd) {
99: mqtt_SetErr(EINVAL, "Error:: wrong command #%d should be %d",
100: hdr->mqtt_msg.type, cmd);
101: return NULL;
102: }
103:
104: *len = mqtt_decodeLen(hdr->mqtt_len, bytes);
105: return hdr;
106: }
107: #pragma GCC visibility pop
108:
109: // ----------------------------------------------------------
110:
111: /*
112: * mqtt_msgFree() Free MQTT message
113: *
114: * @msg = Message buffer
115: * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc()
116: * return: none
117: */
118: inline void
119: mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all)
120: {
121: if (msg && *msg) {
122: if ((*msg)->msg_base) {
123: free((*msg)->msg_base);
124: (*msg)->msg_base = NULL;
125: }
126: if (all) {
127: free(*msg);
128: *msg = NULL;
129: } else
130: (*msg)->msg_len ^= (*msg)->msg_len;
131: }
132: }
133:
134: /*
135: * mqtt_msgAlloc() Allocate memory for MQTT Message
136: *
137: * @len = >0 Allocate buffer with length
138: * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0
139: */
140: inline mqtt_msg_t *
141: mqtt_msgAlloc(u_short len)
142: {
143: mqtt_msg_t *m = NULL;
144:
145: m = malloc(sizeof(mqtt_msg_t));
146: if (!m) {
147: LOGERR;
148: return NULL;
149: } else
150: memset(m, 0, sizeof(mqtt_msg_t));
151:
152: if (len) {
153: m->msg_len = len;
154: m->msg_base = malloc(m->msg_len);
155: if (!m->msg_base) {
156: LOGERR;
157: free(m);
158: return NULL;
159: } else
160: memset(m->msg_base, 0, m->msg_len);
161: }
162:
163: return m;
164: }
165:
166: /*
167: * mqtt_msgRealloc() Reallocate MQTT message buffer
168: *
169: * @msg = MQTT message
170: * @len = new length
171: * return: -1 error or >-1 old buffer length
172: */
173: inline int
174: mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short len)
175: {
176: void *p = NULL;
177: int ret = 0;
178:
179: if (!msg)
180: return -1;
181:
182: if (len == msg->msg_len)
183: return len;
184:
185: p = realloc(msg->msg_base, len);
186: if (!p) {
187: LOGERR;
188: return -1;
189: }
190:
191: ret = msg->msg_len;
192: msg->msg_len = len;
193: msg->msg_base = p;
194:
195: return ret;
196: }
197:
198: /*
199: * mqtt_encodeLen() Encode number to MQTT length field
200: *
201: * @num = number for encode
202: * return: -1 error or >-1 length
203: */
204: inline u_int
205: mqtt_encodeLen(u_int num)
206: {
207: register u_int dig, i;
208: u_int ret = 0;
209:
210: if (num > 268435455)
211: return (u_int) -1;
212:
213: for (i = 0; i < sizeof ret && num > 0; i++) {
214: dig = num % 0x80;
215: num /= 0x80;
216: if (num > 0)
217: dig |= 0x80;
218:
219: *((u_char*) &ret + i) = (u_char) dig;
220: }
221:
222: return ret;
223: }
224:
225: /*
226: * mqtt_decodeLen() Decode length from MQTT packet
227: *
228: * @len = length from MQTT header
229: * @n = sizeof bytes, if !=NULL
230: * return: -1 error, >-1 length of message
231: */
232: inline u_int
233: mqtt_decodeLen(void * __restrict len, int * __restrict n)
234: {
235: register u_int i, dig, mul;
236: u_int ret = 0;
237: u_char *p = (u_char*) len;
238:
239: if (!len)
240: return (u_int) -1;
241:
242: for (mul = 1, i = 0; i < sizeof ret; i++, mul *= 0x80) {
243: dig = p[i];
244: ret += (dig & 0x7f) * mul;
245:
246: if (!(dig & 0x80))
247: break;
248: }
249:
250: if (n)
251: *n = (char) (i & 0x7f) + 1;
252: return ret;
253: }
254:
255: /*
256: * mqtt_sizeLen Return sizeof len field
257: *
258: * @len = length
259: * return: -1 error, >-1 sizeof len in bytes
260: */
261: inline char
262: mqtt_sizeLen(u_int len)
263: {
264: register char i;
265: u_char *p = (u_char*) &len;
266:
267: if (len > 0xffffff7f)
268: return -1;
269:
270: for (i = 0; i < sizeof len; i++)
271: if (!(*(p + i) & 0x80))
272: break;
273:
274: return ++i;
275: }
276:
277: /*
278: * mqtt_str2sub Create MQTT subscribe variable from string(s)
279: *
280: * @csStr = strings
281: * @strnum = number of strings elements
282: * @qoses = QoS elements applied to subscribe variable,
283: * count of elements must be equal with csStr elements
284: * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub()
285: */
286: inline mqtt_subscr_t *
287: mqtt_str2sub(const char **csStr, u_short strnum, u_char *qoses)
288: {
289: mqtt_subscr_t *v;
290: register int i, items;
291: const char **strs;
292:
293: if (!csStr)
294: return NULL;
295: for (items = 0, strs = csStr; *strs; items++, strs++)
296: if (strnum && items >= strnum) {
297: items = strnum;
298: break;
299: }
300:
301: if (!(v = malloc((items + 1) * sizeof(mqtt_subscr_t)))) {
302: LOGERR;
303: return NULL;
304: } else
305: memset(v, 0, (items + 1) * sizeof(mqtt_subscr_t));
306:
307: for (i = 0; i < items; i++) {
308: v[i].sub_topic.msg_len = strlen(csStr[i]);
309: v[i].sub_topic.msg_base = (u_char*) strdup(csStr[i]);
310: if (qoses && qoses[i] < MQTT_QOS_RESERVED)
311: v[i].sub_ret = qoses[i];
312: }
313:
314: return v;
315: }
316:
317: /*
318: * mqtt_subFree() Free array from subscribe variables
319: *
320: * @subs = Subscribe variables
321: * return: none
322: */
323: inline void
324: mqtt_subFree(mqtt_subscr_t ** __restrict subs)
325: {
326: mqtt_subscr_t *v;
327:
328: if (!subs)
329: return;
330:
331: for (v = *subs; v->sub_topic.msg_base; v++) {
332: free(v->sub_topic.msg_base);
333: v->sub_topic.msg_base = NULL;
334: v->sub_topic.msg_len = 0;
335:
336: if (v->sub_value.msg_base) {
337: free(v->sub_value.msg_base);
338: v->sub_value.msg_base = NULL;
339: v->sub_value.msg_len = 0;
340: }
341: }
342:
343: free(*subs);
344: *subs = NULL;
345: }
346:
347: /*
348: * mqtt_subAlloc() Create array from subscribe variables
349: *
350: * @num = Number of elements
351: * return: NULL error or subscribe array, after use must call mqtt_subFree()
352: */
353: inline mqtt_subscr_t *
354: mqtt_subAlloc(u_short num)
355: {
356: mqtt_subscr_t *s = NULL;
357:
358: s = malloc((num + 1) * sizeof(mqtt_subscr_t));
359: if (!s) {
360: LOGERR;
361: return NULL;
362: } else
363: memset(s, 0, (num + 1) * sizeof(mqtt_subscr_t));
364:
365: return s;
366: }
367:
368: /*
369: * mqtt_subRealloc() Reallocate array from subscribe variables
370: *
371: * @subs = Subscribe array
372: * @num = Number of elements
373: * return: NULL error or subscribe array, after use must call mqtt_subFree()
374: */
375: inline mqtt_subscr_t *
376: mqtt_subRealloc(mqtt_subscr_t * __restrict subs, u_short num)
377: {
378: mqtt_subscr_t *s = NULL;
379:
380: s = realloc(subs, (num + 1) * sizeof(mqtt_subscr_t));
381: if (!s) {
382: LOGERR;
383: return NULL;
384: }
385:
386: return s;
387: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>