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