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