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