Annotation of libaitio/src/sock.c, revision 1.4.4.10
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.10! misho 6: * $Id: sock.c,v 1.4.4.9 2013/11/22 09:24:58 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:
1.4.4.7 misho 60: schedCancelby(s->sock_root, taskTIMER, CRITERIA_DATLEN,
61: (void*) cli->cli_fd, NULL);
62: schedCancelby(s->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
63:
1.4.4.4 misho 64: if (s->sock_type == SOCK_STREAM) {
65: shutdown(sock, SHUT_RDWR);
66: close(sock);
67: }
1.4.4.5 misho 68: AIT_FREE_VAL(&cli->cli_buf[1]);
69: AIT_FREE_VAL(&cli->cli_buf[0]);
1.4.4.4 misho 70:
71: e_free(cli);
72: taskExit(task, NULL);
73: }
74:
75: static void *
76: io_acceptClient(sched_task_t *task)
77: {
78: int c, rlen;
79: sockaddr_t sa;
80: socklen_t salen = sizeof sa.ss;
81: sock_cli_t *cli = NULL;
82: sock_t *s = (sock_t*) TASK_ARG(task);
83:
84: if (s->sock_type == SOCK_STREAM) {
85: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
86: LOGERR;
87: goto end;
88: }
89: } else {
90: if ((rlen = recvfrom(TASK_FD(task),
91: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
92: MSG_PEEK, &sa.sa, &salen)) == -1) {
93: LOGERR;
94: goto end;
95: } else
96: c = TASK_FD(task);
97: }
98:
99: cli = e_malloc(sizeof(sock_cli_t));
100: if (!cli) {
101: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
102: if (s->sock_type == SOCK_STREAM)
103: close(c);
104: goto end;
105: } else {
106: memset(cli, 0, sizeof(sock_cli_t));
107: pthread_mutex_lock(&s->sock_mtx);
108: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
109: pthread_mutex_unlock(&s->sock_mtx);
110: }
111:
112: cli->cli_parent = TASK_ARG(task);
113: cli->cli_fd = c;
114: cli->cli_func = TASK_DATA(task);
115: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
1.4.4.5 misho 116: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
117: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
1.4.4.4 misho 118:
119: schedRead(TASK_ROOT(task), cli->cli_func, cli, cli->cli_fd, TASK_ARG(task), 0);
120: ioUpdTimerSocket(cli);
121: end:
122: schedReadSelf(task);
123: taskExit(task, NULL);
124: }
125:
1.4.4.7 misho 126: static void *
1.4.4.9 misho 127: io_txNet(sched_task_t *task)
128: {
129: int wlen;
130: sock_cli_t *cli = TASK_ARG(task);
131: sock_t *s = (sock_t*) cli->cli_parent;
132:
133: if (s->sock_type == SOCK_STREAM)
134: wlen = send(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task), 0);
135: else
136: wlen = sendto(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task), 0,
137: &cli->cli_addr.sa, cli->cli_addr.sa.sa_len);
138: if (wlen < 1)
139: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd);
140:
141: taskExit(task, NULL);
142: }
143:
144: static void *
145: io_txPty(sched_task_t *task)
146: {
147: int wlen;
148: sock_cli_t *cli = TASK_ARG(task);
149:
150: wlen = write(TASK_FD(task), TASK_DATA(task), TASK_DATLEN(task));
151: if (wlen < 1)
152: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd);
153:
154: taskExit(task, NULL);
155: }
156:
157: static void *
158: io_rxNet(sched_task_t *task)
159: {
1.4.4.10! misho 160: int rlen, stat;
1.4.4.9 misho 161: sock_cli_t *cli = TASK_ARG(task);
162: sock_t *s = (sock_t*) cli->cli_parent;
163: sockaddr_t sa;
164: socklen_t salen = sizeof sa.ss;
165:
166: if (s->sock_type == SOCK_STREAM)
167: rlen = recv(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]),
168: AIT_LEN(&cli->cli_buf[0]), 0);
169: else {
170: rlen = recvfrom(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[0]),
171: AIT_LEN(&cli->cli_buf[0]), 0, &sa.sa, &salen);
172: if (e_addrcmp(&cli->cli_addr, &sa, 42))
173: goto end;
174: }
1.4.4.10! misho 175: if (rlen < 1) {
! 176: if (!rlen) {
! 177: kill((pid_t) cli->cli_func, SIGTERM);
! 178: while (waitpid((pid_t) cli->cli_func, &stat, WNOHANG) > 0) {
! 179: usleep(1000);
! 180: kill((pid_t) cli->cli_func, SIGTERM);
! 181: }
! 182: }
1.4.4.9 misho 183: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd);
1.4.4.10! misho 184: } else
1.4.4.9 misho 185: schedWrite(TASK_ROOT(task), io_txPty, cli, cli->cli_pty,
186: AIT_GET_BUF(&cli->cli_buf[0]), rlen);
187: end:
188: schedReadSelf(task);
189: taskExit(task, NULL);
190: }
191:
192: static void *
193: io_rxPty(sched_task_t *task)
194: {
1.4.4.10! misho 195: int rlen, stat;
1.4.4.9 misho 196: sock_cli_t *cli = TASK_ARG(task);
197:
198: rlen = read(TASK_FD(task), AIT_GET_BUF(&cli->cli_buf[1]), AIT_LEN(&cli->cli_buf[1]));
1.4.4.10! misho 199: if (rlen < 1) {
! 200: if (!rlen) {
! 201: kill((pid_t) cli->cli_func, SIGTERM);
! 202: while (waitpid((pid_t) cli->cli_func, &stat, WNOHANG) > 0) {
! 203: usleep(1000);
! 204: kill((pid_t) cli->cli_func, SIGTERM);
! 205: }
! 206: }
1.4.4.9 misho 207: schedEvent(TASK_ROOT(task), io_closeClient, cli, 0, NULL, cli->cli_fd);
1.4.4.10! misho 208: } else
1.4.4.9 misho 209: schedWrite(TASK_ROOT(task), io_txNet, cli, cli->cli_fd,
210: AIT_GET_BUF(&cli->cli_buf[1]), rlen);
211:
212: schedReadSelf(task);
213: taskExit(task, NULL);
214: }
215:
216: static void *
1.4.4.8 misho 217: io_bridgeClient(sched_task_t *task)
1.4.4.7 misho 218: {
1.4.4.8 misho 219: int c, rlen;
220: pid_t pid;
221: sockaddr_t sa;
222: socklen_t salen = sizeof sa.ss;
223: sock_cli_t *cli = NULL;
224: sock_t *s = (sock_t*) TASK_ARG(task);
225: array_t *args = NULL;
226: char **argv = NULL;
227:
228: if (s->sock_type == SOCK_STREAM) {
229: if ((c = accept(TASK_FD(task), &sa.sa, &salen)) == -1) {
230: LOGERR;
231: goto end;
232: }
233: } else {
234: if ((rlen = recvfrom(TASK_FD(task),
235: AIT_GET_BUF(&s->sock_buf), AIT_LEN(&s->sock_buf),
236: MSG_PEEK, &sa.sa, &salen)) == -1) {
237: LOGERR;
238: goto end;
239: } else
240: c = TASK_FD(task);
241: }
242:
243: cli = e_malloc(sizeof(sock_cli_t));
244: if (!cli) {
245: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
246: if (s->sock_type == SOCK_STREAM)
247: close(c);
248: goto end;
249: } else {
250: memset(cli, 0, sizeof(sock_cli_t));
251: pthread_mutex_lock(&s->sock_mtx);
252: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
253: pthread_mutex_unlock(&s->sock_mtx);
254: }
255:
256: cli->cli_parent = TASK_ARG(task);
257: cli->cli_fd = c;
258: strlcpy(cli->cli_cmdline, TASK_DATA(task), sizeof cli->cli_cmdline);
259: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
260: AIT_SET_BUFSIZ(&cli->cli_buf[0], 0, AIT_LEN(&s->sock_buf));
261: AIT_SET_BUFSIZ(&cli->cli_buf[1], 0, AIT_LEN(&s->sock_buf));
262:
263: switch ((pid = ioForkPTY(&cli->cli_pty, cli->cli_name, sizeof cli->cli_name,
264: NULL, NULL, NULL))) {
265: case -1:
266: ELIBERR(io);
267: break;
268: case 0:
269: array_Args(cli->cli_cmdline, 0, " \t", &args);
270: argv = array_To(args);
271: array_Destroy(&args);
272:
273: execv(*argv, argv);
274: break;
275: default:
1.4.4.10! misho 276: cli->cli_func = (sched_task_func_t) pid;
! 277:
1.4.4.9 misho 278: schedRead(TASK_ROOT(task), io_rxPty, cli, cli->cli_pty,
279: TASK_ARG(task), 0);
280: schedRead(TASK_ROOT(task), io_rxNet, cli, cli->cli_fd,
281: TASK_ARG(task), 0);
1.4.4.8 misho 282: ioUpdTimerSocket(cli);
283: break;
284: }
285: end:
286: schedReadSelf(task);
1.4.4.7 misho 287: taskExit(task, NULL);
288: }
289:
1.4.4.4 misho 290:
1.2 misho 291: /*
292: * ioInitSocket() - Init socket and allocate resources
293: *
294: * @role = Socket role
295: * @type = Socket type
296: * @proto = Socket protocol
297: * @addr = Bind to address
298: * @port = Bind to port
299: * @buflen = Socket buffer, optional if =0 == BUFSIZ
300: * return: NULL error or !=NULL created socket
301: */
302: sock_t *
303: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
304: {
305: sock_t *s = NULL;
306: int n = 1;
307:
308: if (!addr)
309: return NULL;
310:
311: s = e_malloc(sizeof(sock_t));
312: if (!s) {
313: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
314: return NULL;
315: } else
316: memset(s, 0, sizeof(sock_t));
317:
1.3 misho 318: TAILQ_INIT(&s->sock_cli);
319:
1.2 misho 320: s->sock_role = role;
321: s->sock_type = type;
322: s->sock_proto = proto;
323: if (!e_gethostbyname(addr, port, &s->sock_addr)) {
324: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
325: e_free(s);
326: return NULL;
327: } else {
328: buflen = buflen ? buflen : BUFSIZ;
1.4.4.1 misho 329: buflen = E_ALIGN(buflen, 2); /* align buflen length */
1.2 misho 330: AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
331: }
332:
333: s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
334: if (s->sock_fd == -1) {
335: LOGERR;
336: AIT_FREE_VAL(&s->sock_buf);
337: e_free(s);
338: return NULL;
339: }
340: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
341: LOGERR;
342: AIT_FREE_VAL(&s->sock_buf);
343: e_free(s);
344: return NULL;
345: }
346: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
347: LOGERR;
348: AIT_FREE_VAL(&s->sock_buf);
349: e_free(s);
350: return NULL;
351: }
352: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
353: LOGERR;
354: AIT_FREE_VAL(&s->sock_buf);
355: e_free(s);
356: return NULL;
357: }
358: if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
359: LOGERR;
360: AIT_FREE_VAL(&s->sock_buf);
361: e_free(s);
362: return NULL;
363: }
364:
1.4.4.1 misho 365: s->sock_root = schedBegin();
366: if (!s->sock_root) {
367: io_SetErr(sched_GetErrno(), "%s", sched_GetError());
368: AIT_FREE_VAL(&s->sock_buf);
369: e_free(s);
370: return NULL;
371: }
372:
1.3 misho 373: pthread_mutex_init(&s->sock_mtx, NULL);
1.2 misho 374: return s;
375: }
376:
377: /*
378: * ioCloseSocket() - Close socket and free resources
379: *
380: * @s = Socket
381: * return: none
382: */
383: void
384: ioCloseSocket(sock_t ** __restrict s)
385: {
1.4.4.4 misho 386: sock_cli_t *cli;
1.3 misho 387:
1.2 misho 388: if (s && *s) {
1.3 misho 389: pthread_mutex_lock(&(*s)->sock_mtx);
390: while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
391: TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
1.4.4.4 misho 392:
393: schedCancelby((*s)->sock_root, taskTIMER, CRITERIA_DATLEN,
394: (void*) cli->cli_fd, NULL);
1.4.4.7 misho 395: schedCancelby((*s)->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
1.4.4.4 misho 396:
397: if ((*s)->sock_type == SOCK_STREAM) {
398: shutdown(cli->cli_fd, SHUT_RDWR);
399: close(cli->cli_fd);
400: }
1.4.4.5 misho 401: AIT_FREE_VAL(&cli->cli_buf[1]);
402: AIT_FREE_VAL(&cli->cli_buf[0]);
1.3 misho 403: e_free(cli);
404: }
405: pthread_mutex_unlock(&(*s)->sock_mtx);
406:
1.2 misho 407: shutdown((*s)->sock_fd, SHUT_RDWR);
408: close((*s)->sock_fd);
409:
410: AIT_FREE_VAL(&(*s)->sock_buf);
1.3 misho 411:
1.4.4.1 misho 412: schedEnd(&(*s)->sock_root);
413:
1.3 misho 414: pthread_mutex_destroy(&(*s)->sock_mtx);
1.2 misho 415: e_free(*s);
416: *s = NULL;
417: }
418: }
419:
420: /*
421: * ioUpSocket() - Setup socket for use
422: *
423: * @s = Socket
424: * @arg = Server role = listen backlog queue and Client role = peer address
1.4.4.1 misho 425: * @timeout = Socket timeout in sec (default -1 infinit)
1.2 misho 426: * return: -1 error or 0 ok
427: */
428: int
1.4.4.1 misho 429: ioUpSocket(sock_t * __restrict s, void *arg, int timeout)
1.2 misho 430: {
431: int ret = 0;
432: sockaddr_t *peer = (sockaddr_t*) arg;
433: uintptr_t backlog = (uintptr_t) arg;
434:
435: if (!s || !arg)
436: return -1;
1.4.4.1 misho 437: else {
438: s->sock_timeout.tv_sec = timeout;
439: s->sock_timeout.tv_nsec = (timeout < 1) ? timeout : 0;
440: schedPolling(s->sock_root, &s->sock_timeout, NULL);
441: }
1.2 misho 442:
443: switch (s->sock_role) {
444: case IO_SOCK_ROLE_CLIENT:
445: memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
446:
447: if (connect(s->sock_fd, &s->sock_peer.sa,
448: s->sock_peer.sa.sa_len) == -1) {
449: LOGERR;
450: return -1;
451: }
452: break;
453: case IO_SOCK_ROLE_SERVER:
454: if (s->sock_type == SOCK_STREAM) {
455: s->sock_backq = backlog;
456:
457: if (listen(s->sock_fd, s->sock_backq) == -1) {
458: LOGERR;
459: return -1;
460: }
461: }
462: break;
463: default:
464: io_SetErr(EINVAL, "Unsupported socket type");
465: return -1;
466: }
467:
468: fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
469: return ret;
470: }
1.3 misho 471:
472: /*
1.4.4.2 misho 473: * ioUpdTimerSocket() - Update timeout of socket
1.3 misho 474: *
1.4.4.2 misho 475: * @c = Client socket
476: * return: none
1.3 misho 477: */
1.4.4.2 misho 478: void
479: ioUpdTimerSocket(sock_cli_t * __restrict c)
1.3 misho 480: {
1.4.4.2 misho 481: sock_t *s;
1.3 misho 482:
1.4.4.2 misho 483: if (!c)
484: return;
485: else
486: s = c->cli_parent;
1.3 misho 487:
1.4.4.2 misho 488: schedCancelby(s->sock_root, taskTIMER, CRITERIA_DATLEN, (void*) c->cli_fd, NULL);
489: schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, c->cli_fd);
490: }
1.3 misho 491:
1.4.4.2 misho 492: /*
1.4.4.6 misho 493: * ioCloseClient() - Close client socket
494: *
495: * @c = Client socket
496: * return: 0 ok or !=0 error
497: */
498: int
499: ioCloseClient(sock_cli_t * __restrict c)
500: {
501: sock_t *s;
502:
503: if (!c)
504: return -1;
505: else
506: s = c->cli_parent;
507:
508: return !schedEvent(s->sock_root, io_closeClient, c, 0, NULL, c->cli_fd);
509: }
510:
511: /*
1.4.4.2 misho 512: * ioLoopSocket() - Start socket scheduler
513: *
514: * @s = Socket
515: * @rcb = Read callback
516: * return: -1 error or return result from scheduler
517: */
518: int
519: ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb)
520: {
1.4.4.6 misho 521: if (!s || !rcb || s->sock_kill)
522: return -1;
523:
1.4.4.2 misho 524: schedRead(s->sock_root, io_acceptClient, s, s->sock_fd, rcb, 0);
525: return schedRun(s->sock_root, &s->sock_kill);
1.3 misho 526: }
1.4.4.3 misho 527:
528: /*
1.4.4.6 misho 529: * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
1.4.4.3 misho 530: *
1.4.4.6 misho 531: * @s = Socket
532: * @prgname = Program name
1.4.4.3 misho 533: * return: 0 ok or !=0 error
534: */
535: int
1.4.4.6 misho 536: ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname)
1.4.4.3 misho 537: {
1.4.4.6 misho 538: if (!s || !prgname || s->sock_kill)
1.4.4.3 misho 539: return -1;
540:
1.4.4.8 misho 541: schedRead(s->sock_root, io_bridgeClient, s, s->sock_fd, (void*) prgname, 0);
542: return schedRun(s->sock_root, &s->sock_kill);
1.4.4.3 misho 543: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>