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