Annotation of libaitmqtt/src/srvside.c, revision 1.3.10.1
1.2 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.3.10.1! misho 6: * $Id: srvside.c,v 1.3 2012/06/29 13:54:48 misho Exp $
1.2 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.3.10.1! misho 15: Copyright 2004 - 2022
1.2 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: /*
1.3 misho 50: * mqtt_srv_cliInit() Init MQTT server side support for clients
1.2 misho 51: *
52: * @sock = Client socket from accept()
53: * @timeout = timeout
1.3 misho 54: * @nb = Non block socket
1.2 misho 55: * return: NULL error or !=NULL allocated server handle
56: */
57: mqtt_srv_t *
1.3.10.1! misho 58: mqtt_srv_cliInit(int sock, u_short timeout, int nb)
1.2 misho 59: {
60: mqtt_srv_t *srv = NULL;
1.3 misho 61: struct timeval tv = { timeout, 0 };
1.3.10.1! misho 62: #ifdef MQTT_SET_CLIBUF
! 63: int n = MQTT_SET_CLIBUF;
! 64: #endif
1.2 misho 65:
1.3.10.1! misho 66: srv = e_malloc(sizeof(mqtt_srv_t));
1.2 misho 67: if (!srv) {
68: LOGERR;
69: return srv;
70: } else
71: memset(srv, 0, sizeof(mqtt_srv_t));
72:
1.3 misho 73: /* set options of client socket */
1.3.10.1! misho 74: #ifdef MQTT_SET_CLIBUF
1.3 misho 75: setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n);
76: setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n);
1.3.10.1! misho 77: #endif
1.3 misho 78: setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv);
79: setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv);
80: ioctl(sock, FIONBIO, nb);
81:
1.2 misho 82: srv->sock = sock;
83: srv->timeout = timeout;
84:
85: return srv;
86: }
87:
88: /*
1.3 misho 89: * mqtt_srv_cliFini() Finish MQTT server side support for clients
1.2 misho 90: *
91: * @psrv = Server handle
92: * return: none
93: */
94: void
1.3 misho 95: mqtt_srv_cliFini(mqtt_srv_t ** __restrict psrv)
1.2 misho 96: {
97: if (psrv && *psrv) {
1.3.10.1! misho 98: e_free(*psrv);
1.2 misho 99: *psrv = NULL;
100: }
101: }
102:
103: /*
1.3 misho 104: * mqtt_srv_cliDispatch() MQTT server dispatcher for clients
1.2 misho 105: *
106: * @srv = Server handle
107: * @rlen = Ready bytes to parse
108: * @arg = Argument pass to command callback
109: * return: -1 error or 0 ok
110: */
111: int
1.3 misho 112: mqtt_srv_cliDispatch(mqtt_srv_t * __restrict srv, int rlen, void *arg)
1.2 misho 113: {
114: struct mqtthdr *hdr;
115:
1.3.10.1! misho 116: if (!srv || !srv->buf || !srv->buf->msg_base || !srv->buf->msg_len)
1.2 misho 117: return -1;
118: if (srv->buf->msg_len < sizeof(struct mqtthdr) || rlen < sizeof(struct mqtthdr)) {
119: mqtt_SetErr(EINVAL, "Message is too short ...");
120: return -1;
121: }
122: hdr = (struct mqtthdr*) srv->buf->msg_base;
123: if (hdr->mqtt_msg.type > MQTT_TYPE_MAX) {
124: mqtt_SetErr(ENOSYS, "Message type %d not implemented", hdr->mqtt_msg.type);
125: return -1;
126: }
127: if (srv->cmds[hdr->mqtt_msg.type])
128: return srv->cmds[hdr->mqtt_msg.type]((void*) srv, rlen, arg);
129:
130: mqtt_SetErr(ENOTSUP, "Unsupported message type %d", hdr->mqtt_msg.type);
131: return -1;
132: }
1.3 misho 133:
134:
135: /*
136: * mqtt_srv_Create() - Create server socket
137: *
138: * @sa = Server bind address
139: * @salen = Server struct sockaddr size
140: * return: -1 error or >-1 server socket
141: */
142: int
143: mqtt_srv_Create(struct sockaddr * __restrict sa, int salen)
144: {
145: int s = -1, n = 1;
146:
147: if (!sa)
148: return -1;
149:
150: s = socket(sa->sa_family, SOCK_STREAM, 0);
151: if (s == -1) {
152: LOGERR;
153: return -1;
154: }
155: if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
156: LOGERR;
157: close(s);
158: return -1;
159: }
160: if (bind(s, sa, salen) == -1) {
161: LOGERR;
162: close(s);
163: return -1;
164: }
165:
166: return s;
167: }
168:
169: /*
170: * mqtt_srv_Destroy() - Close server socket
171: *
172: * @sock = Server socket
173: * return: -1 error or 0 ok
174: */
175: int
176: mqtt_srv_Destroy(int sock)
177: {
178: shutdown(sock, SHUT_RDWR);
179: return close(sock);
180: }
181:
182: /*
183: * mqtt_srv_Listen() - Listen server socket
184: *
185: * @sock = Server socket
186: * @maxconn = max number of pending connections
187: * @nb = Non block socket
188: * return: -1 error or 0 ok
189: */
190: int
191: mqtt_srv_Listen(int sock, int maxconn, int nb)
192: {
193: if (!maxconn)
194: maxconn = SOMAXCONN;
195:
196: if (listen(sock, maxconn) == -1) {
197: LOGERR;
198: return -1;
199: } else
200: ioctl(sock, FIONBIO, nb);
201:
202: return 0;
203: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>