Annotation of libaitio/src/sock.c, revision 1.4.4.4
1.2 misho 1: /*************************************************************************
2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.4.4.4 ! misho 6: * $Id: sock.c,v 1.4.4.3 2013/11/21 15:01:22 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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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:
1.4.4.4 ! misho 49: static void *
! 50: io_closeClient(sched_task_t *task)
! 51: {
! 52: sock_cli_t *cli = (sock_cli_t*) TASK_ARG(task);
! 53: sock_t *s = (sock_t*) cli->cli_parent;
! 54: int sock = (int) TASK_DATLEN(task);
! 55:
! 56: pthread_mutex_lock(&s->sock_mtx);
! 57: TAILQ_REMOVE(&s->sock_cli, cli, cli_node);
! 58: pthread_mutex_unlock(&s->sock_mtx);
! 59:
! 60: if (s->sock_type == SOCK_STREAM) {
! 61: shutdown(sock, SHUT_RDWR);
! 62: close(sock);
! 63: }
! 64: AIT_FREE_VAL(&cli->cli_buf);
! 65:
! 66: e_free(cli);
! 67: taskExit(task, NULL);
! 68: }
! 69:
! 70: static void *
! 71: io_acceptClient(sched_task_t *task)
! 72: {
! 73: int c, rlen;
! 74: sockaddr_t sa;
! 75: socklen_t salen = sizeof sa.ss;
! 76: sock_cli_t *cli = NULL;
! 77: sock_t *s = (sock_t*) TASK_ARG(task);
! 78:
! 79: if (s->sock_type == SOCK_STREAM) {
! 80: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
! 81: LOGERR;
! 82: goto end;
! 83: }
! 84: } else {
! 85: if ((rlen = recvfrom(TASK_FD(task),
! 86: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
! 87: MSG_PEEK, &sa.sa, &salen)) == -1) {
! 88: LOGERR;
! 89: goto end;
! 90: } else
! 91: c = TASK_FD(task);
! 92: }
! 93:
! 94: cli = e_malloc(sizeof(sock_cli_t));
! 95: if (!cli) {
! 96: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
! 97: if (s->sock_type == SOCK_STREAM)
! 98: close(c);
! 99: goto end;
! 100: } else {
! 101: memset(cli, 0, sizeof(sock_cli_t));
! 102: pthread_mutex_lock(&s->sock_mtx);
! 103: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
! 104: pthread_mutex_unlock(&s->sock_mtx);
! 105: }
! 106:
! 107: cli->cli_parent = TASK_ARG(task);
! 108: cli->cli_fd = c;
! 109: cli->cli_func = TASK_DATA(task);
! 110: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
! 111: AIT_SET_BUFSIZ(&cli->cli_buf, 0, AIT_LEN(&s->sock_buf));
! 112:
! 113: schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0);
! 114: ioUpdTimerSocket(cli);
! 115: end:
! 116: schedReadSelf(task);
! 117: taskExit(task, NULL);
! 118: }
! 119:
! 120:
1.2 misho 121: /*
122: * ioInitSocket() - Init socket and allocate resources
123: *
124: * @role = Socket role
125: * @type = Socket type
126: * @proto = Socket protocol
127: * @addr = Bind to address
128: * @port = Bind to port
129: * @buflen = Socket buffer, optional if =0 == BUFSIZ
130: * return: NULL error or !=NULL created socket
131: */
132: sock_t *
133: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
134: {
135: sock_t *s = NULL;
136: int n = 1;
137:
138: if (!addr)
139: return NULL;
140:
141: s = e_malloc(sizeof(sock_t));
142: if (!s) {
143: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
144: return NULL;
145: } else
146: memset(s, 0, sizeof(sock_t));
147:
1.3 misho 148: TAILQ_INIT(&s->sock_cli);
149:
1.2 misho 150: s->sock_role = role;
151: s->sock_type = type;
152: s->sock_proto = proto;
153: if (!e_gethostbyname(addr, port, &s->sock_addr)) {
154: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
155: e_free(s);
156: return NULL;
157: } else {
158: buflen = buflen ? buflen : BUFSIZ;
1.4.4.1 misho 159: buflen = E_ALIGN(buflen, 2); /* align buflen length */
1.2 misho 160: AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
161: }
162:
163: s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
164: if (s->sock_fd == -1) {
165: LOGERR;
166: AIT_FREE_VAL(&s->sock_buf);
167: e_free(s);
168: return NULL;
169: }
170: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
171: LOGERR;
172: AIT_FREE_VAL(&s->sock_buf);
173: e_free(s);
174: return NULL;
175: }
176: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
177: LOGERR;
178: AIT_FREE_VAL(&s->sock_buf);
179: e_free(s);
180: return NULL;
181: }
182: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
183: LOGERR;
184: AIT_FREE_VAL(&s->sock_buf);
185: e_free(s);
186: return NULL;
187: }
188: if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
189: LOGERR;
190: AIT_FREE_VAL(&s->sock_buf);
191: e_free(s);
192: return NULL;
193: }
194:
1.4.4.1 misho 195: s->sock_root = schedBegin();
196: if (!s->sock_root) {
197: io_SetErr(sched_GetErrno(), "%s", sched_GetError());
198: AIT_FREE_VAL(&s->sock_buf);
199: e_free(s);
200: return NULL;
201: }
202:
1.3 misho 203: pthread_mutex_init(&s->sock_mtx, NULL);
1.2 misho 204: return s;
205: }
206:
207: /*
208: * ioCloseSocket() - Close socket and free resources
209: *
210: * @s = Socket
211: * return: none
212: */
213: void
214: ioCloseSocket(sock_t ** __restrict s)
215: {
1.4.4.4 ! misho 216: sock_cli_t *cli;
1.3 misho 217:
1.2 misho 218: if (s && *s) {
1.3 misho 219: pthread_mutex_lock(&(*s)->sock_mtx);
220: while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
221: TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
1.4.4.4 ! misho 222:
! 223: schedCancelby((*s)->sock_root, taskTIMER, CRITERIA_DATLEN,
! 224: (void*) cli->cli_fd, NULL);
! 225: schedCancelby((*s)->sock_root, taskMAX, CRITERIA_FD,
! 226: (void*) cli->cli_fd, NULL);
! 227:
! 228: if ((*s)->sock_type == SOCK_STREAM) {
! 229: shutdown(cli->cli_fd, SHUT_RDWR);
! 230: close(cli->cli_fd);
! 231: }
1.3 misho 232: AIT_FREE_VAL(&cli->cli_buf);
233: e_free(cli);
234: }
235: pthread_mutex_unlock(&(*s)->sock_mtx);
236:
1.2 misho 237: shutdown((*s)->sock_fd, SHUT_RDWR);
238: close((*s)->sock_fd);
239:
240: AIT_FREE_VAL(&(*s)->sock_buf);
1.3 misho 241:
1.4.4.1 misho 242: schedEnd(&(*s)->sock_root);
243:
1.3 misho 244: pthread_mutex_destroy(&(*s)->sock_mtx);
1.2 misho 245: e_free(*s);
246: *s = NULL;
247: }
248: }
249:
250: /*
251: * ioUpSocket() - Setup socket for use
252: *
253: * @s = Socket
254: * @arg = Server role = listen backlog queue and Client role = peer address
1.4.4.1 misho 255: * @timeout = Socket timeout in sec (default -1 infinit)
1.2 misho 256: * return: -1 error or 0 ok
257: */
258: int
1.4.4.1 misho 259: ioUpSocket(sock_t * __restrict s, void *arg, int timeout)
1.2 misho 260: {
261: int ret = 0;
262: sockaddr_t *peer = (sockaddr_t*) arg;
263: uintptr_t backlog = (uintptr_t) arg;
264:
265: if (!s || !arg)
266: return -1;
1.4.4.1 misho 267: else {
268: s->sock_timeout.tv_sec = timeout;
269: s->sock_timeout.tv_nsec = (timeout < 1) ? timeout : 0;
270: schedPolling(s->sock_root, &s->sock_timeout, NULL);
271: }
1.2 misho 272:
273: switch (s->sock_role) {
274: case IO_SOCK_ROLE_CLIENT:
275: memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
276:
277: if (connect(s->sock_fd, &s->sock_peer.sa,
278: s->sock_peer.sa.sa_len) == -1) {
279: LOGERR;
280: return -1;
281: }
282: break;
283: case IO_SOCK_ROLE_SERVER:
284: if (s->sock_type == SOCK_STREAM) {
285: s->sock_backq = backlog;
286:
287: if (listen(s->sock_fd, s->sock_backq) == -1) {
288: LOGERR;
289: return -1;
290: }
291: }
292: break;
293: default:
294: io_SetErr(EINVAL, "Unsupported socket type");
295: return -1;
296: }
297:
298: fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
299: return ret;
300: }
1.3 misho 301:
302: /*
1.4.4.2 misho 303: * ioUpdTimerSocket() - Update timeout of socket
1.3 misho 304: *
1.4.4.2 misho 305: * @c = Client socket
306: * return: none
1.3 misho 307: */
1.4.4.2 misho 308: void
309: ioUpdTimerSocket(sock_cli_t * __restrict c)
1.3 misho 310: {
1.4.4.2 misho 311: sock_t *s;
1.3 misho 312:
1.4.4.2 misho 313: if (!c)
314: return;
315: else
316: s = c->cli_parent;
1.3 misho 317:
1.4.4.2 misho 318: schedCancelby(s->sock_root, taskTIMER, CRITERIA_DATLEN, (void*) c->cli_fd, NULL);
319: schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, c->cli_fd);
320: }
1.3 misho 321:
1.4.4.2 misho 322: /*
323: * ioLoopSocket() - Start socket scheduler
324: *
325: * @s = Socket
326: * @rcb = Read callback
327: * return: -1 error or return result from scheduler
328: */
329: int
330: ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb)
331: {
332: schedRead(s->sock_root, io_acceptClient, s, s->sock_fd, rcb, 0);
333: return schedRun(s->sock_root, &s->sock_kill);
1.3 misho 334: }
1.4.4.3 misho 335:
336: /*
337: * ioCloseClient() - Close client socket
338: *
339: * @c = Client socket
340: * return: 0 ok or !=0 error
341: */
342: int
343: ioCloseClient(sock_cli_t * __restrict c)
344: {
345: sock_t *s;
346:
347: if (!c)
348: return -1;
349: else
350: s = c->cli_parent;
351:
352: return !schedEvent(s->sock_root, io_closeClient, c, 0, NULL, c->cli_fd);
353: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>