File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / src / sock.c
Revision 1.4.4.7: download - view: text, annotated - select for diffs - revision graph
Fri Nov 22 08:23:51 2013 UTC (10 years, 7 months ago) by misho
Branches: io6_0
Diff to: branchpoint 1.4: preferred, unified
*** empty log message ***

    1: /*************************************************************************
    2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
    3: *  by Michael Pounov <misho@elwix.org>
    4: *
    5: * $Author: misho $
    6: * $Id: sock.c,v 1.4.4.7 2013/11/22 08:23:51 misho Exp $
    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: 
   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: 
   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: 
   64: 	if (s->sock_type == SOCK_STREAM) {
   65: 		shutdown(sock, SHUT_RDWR);
   66: 		close(sock);
   67: 	}
   68: 	AIT_FREE_VAL(&cli->cli_buf[1]);
   69: 	AIT_FREE_VAL(&cli->cli_buf[0]);
   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);
  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));
  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: 
  126: static void *
  127: io_bridgeSock(sched_task_t *task)
  128: {
  129: 	taskExit(task, NULL);
  130: }
  131: 
  132: 
  133: /*
  134:  * ioInitSocket() - Init socket and allocate resources
  135:  *
  136:  * @role = Socket role
  137:  * @type = Socket type
  138:  * @proto = Socket protocol
  139:  * @addr = Bind to address
  140:  * @port = Bind to port
  141:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
  142:  * return: NULL error or !=NULL created socket
  143:  */
  144: sock_t *
  145: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
  146: {
  147: 	sock_t *s = NULL;
  148: 	int n = 1;
  149: 
  150: 	if (!addr)
  151: 		return NULL;
  152: 
  153: 	s = e_malloc(sizeof(sock_t));
  154: 	if (!s) {
  155: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  156: 		return NULL;
  157: 	} else
  158: 		memset(s, 0, sizeof(sock_t));
  159: 
  160: 	TAILQ_INIT(&s->sock_cli);
  161: 
  162: 	s->sock_role = role;
  163: 	s->sock_type = type;
  164: 	s->sock_proto = proto;
  165: 	if (!e_gethostbyname(addr, port, &s->sock_addr)) {
  166: 		io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
  167: 		e_free(s);
  168: 		return NULL;
  169: 	} else {
  170: 		buflen = buflen ? buflen : BUFSIZ;
  171: 		buflen = E_ALIGN(buflen, 2);	/* align buflen length */
  172: 		AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
  173: 	}
  174: 
  175: 	s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
  176: 	if (s->sock_fd == -1) {
  177: 		LOGERR;
  178: 		AIT_FREE_VAL(&s->sock_buf);
  179: 		e_free(s);
  180: 		return NULL;
  181: 	}
  182: 	if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
  183: 		LOGERR;
  184: 		AIT_FREE_VAL(&s->sock_buf);
  185: 		e_free(s);
  186: 		return NULL;
  187: 	}
  188: 	if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
  189: 		LOGERR;
  190: 		AIT_FREE_VAL(&s->sock_buf);
  191: 		e_free(s);
  192: 		return NULL;
  193: 	}
  194: 	if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
  195: 		LOGERR;
  196: 		AIT_FREE_VAL(&s->sock_buf);
  197: 		e_free(s);
  198: 		return NULL;
  199: 	}
  200: 	if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
  201: 		LOGERR;
  202: 		AIT_FREE_VAL(&s->sock_buf);
  203: 		e_free(s);
  204: 		return NULL;
  205: 	}
  206: 
  207: 	s->sock_root = schedBegin();
  208: 	if (!s->sock_root) {
  209: 		io_SetErr(sched_GetErrno(), "%s", sched_GetError());
  210: 		AIT_FREE_VAL(&s->sock_buf);
  211: 		e_free(s);
  212: 		return NULL;
  213: 	}
  214: 
  215: 	pthread_mutex_init(&s->sock_mtx, NULL);
  216: 	return s;
  217: }
  218: 
  219: /*
  220:  * ioCloseSocket() - Close socket and free resources
  221:  *
  222:  * @s = Socket
  223:  * return: none
  224:  */
  225: void
  226: ioCloseSocket(sock_t ** __restrict s)
  227: {
  228: 	sock_cli_t *cli;
  229: 
  230: 	if (s && *s) {
  231: 		pthread_mutex_lock(&(*s)->sock_mtx);
  232: 		while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
  233: 			TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
  234: 
  235: 			schedCancelby((*s)->sock_root, taskTIMER, CRITERIA_DATLEN, 
  236: 					(void*) cli->cli_fd, NULL);
  237: 			schedCancelby((*s)->sock_root, taskMAX, CRITERIA_ARG, cli, NULL);
  238: 
  239: 			if ((*s)->sock_type == SOCK_STREAM) {
  240: 				shutdown(cli->cli_fd, SHUT_RDWR);
  241: 				close(cli->cli_fd);
  242: 			}
  243: 			AIT_FREE_VAL(&cli->cli_buf[1]);
  244: 			AIT_FREE_VAL(&cli->cli_buf[0]);
  245: 			e_free(cli);
  246: 		}
  247: 		pthread_mutex_unlock(&(*s)->sock_mtx);
  248: 
  249: 		shutdown((*s)->sock_fd, SHUT_RDWR);
  250: 		close((*s)->sock_fd);
  251: 
  252: 		AIT_FREE_VAL(&(*s)->sock_buf);
  253: 
  254: 		schedEnd(&(*s)->sock_root);
  255: 
  256: 		pthread_mutex_destroy(&(*s)->sock_mtx);
  257: 		e_free(*s);
  258: 		*s = NULL;
  259: 	}
  260: }
  261: 
  262: /*
  263:  * ioUpSocket() - Setup socket for use
  264:  *
  265:  * @s = Socket
  266:  * @arg = Server role = listen backlog queue and Client role = peer address
  267:  * @timeout = Socket timeout in sec (default -1 infinit)
  268:  * return: -1 error or 0 ok
  269:  */
  270: int
  271: ioUpSocket(sock_t * __restrict s, void *arg, int timeout)
  272: {
  273: 	int ret = 0;
  274: 	sockaddr_t *peer = (sockaddr_t*) arg;
  275: 	uintptr_t backlog = (uintptr_t) arg;
  276: 
  277: 	if (!s || !arg)
  278: 		return -1;
  279: 	else {
  280: 		s->sock_timeout.tv_sec = timeout;
  281: 		s->sock_timeout.tv_nsec = (timeout < 1) ? timeout : 0;
  282: 		schedPolling(s->sock_root, &s->sock_timeout, NULL);
  283: 	}
  284: 
  285: 	switch (s->sock_role) {
  286: 		case IO_SOCK_ROLE_CLIENT:
  287: 			memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
  288: 
  289: 			if (connect(s->sock_fd, &s->sock_peer.sa, 
  290: 						s->sock_peer.sa.sa_len) == -1) {
  291: 				LOGERR;
  292: 				return -1;
  293: 			}
  294: 			break;
  295: 		case IO_SOCK_ROLE_SERVER:
  296: 			if (s->sock_type == SOCK_STREAM) {
  297: 				s->sock_backq = backlog;
  298: 
  299: 				if (listen(s->sock_fd, s->sock_backq) == -1) {
  300: 					LOGERR;
  301: 					return -1;
  302: 				}
  303: 			}
  304: 			break;
  305: 		default:
  306: 			io_SetErr(EINVAL, "Unsupported socket type");
  307: 			return -1;
  308: 	}
  309: 
  310: 	fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
  311: 	return ret;
  312: }
  313: 
  314: /*
  315:  * ioUpdTimerSocket() - Update timeout of socket
  316:  *
  317:  * @c = Client socket
  318:  * return:  none
  319:  */
  320: void
  321: ioUpdTimerSocket(sock_cli_t * __restrict c)
  322: {
  323: 	sock_t *s;
  324: 
  325: 	if (!c)
  326: 		return;
  327: 	else
  328: 		s = c->cli_parent;
  329: 
  330: 	schedCancelby(s->sock_root, taskTIMER, CRITERIA_DATLEN, (void*) c->cli_fd, NULL);
  331: 	schedTimer(s->sock_root, io_closeClient, c, s->sock_timeout, NULL, c->cli_fd);
  332: }
  333: 
  334: /*
  335:  * ioCloseClient() - Close client socket
  336:  *
  337:  * @c = Client socket
  338:  * return: 0 ok or !=0 error
  339:  */
  340: int
  341: ioCloseClient(sock_cli_t * __restrict c)
  342: {
  343: 	sock_t *s;
  344: 
  345: 	if (!c)
  346: 		return -1;
  347: 	else
  348: 		s = c->cli_parent;
  349: 
  350: 	return !schedEvent(s->sock_root, io_closeClient, c, 0, NULL, c->cli_fd);
  351: }
  352: 
  353: /*
  354:  * ioLoopSocket() - Start socket scheduler
  355:  *
  356:  * @s = Socket
  357:  * @rcb = Read callback
  358:  * return: -1 error or return result from scheduler
  359:  */
  360: int
  361: ioLoopSocket(sock_t * __restrict s, sched_task_func_t rcb)
  362: {
  363: 	if (!s || !rcb || s->sock_kill)
  364: 		return -1;
  365: 
  366: 	schedRead(s->sock_root, io_acceptClient, s, s->sock_fd, rcb, 0);
  367: 	return schedRun(s->sock_root, &s->sock_kill);
  368: }
  369: 
  370: /*
  371:  * ioBridgeProg2Socket() - Start socket scheduler and bridge program to socket
  372:  *
  373:  * @s = Socket
  374:  * @prgname = Program name
  375:  * return: 0 ok or !=0 error
  376:  */
  377: int
  378: ioBridgeProg2Socket(sock_t * __restrict s, const char *prgname)
  379: {
  380: 	if (!s || !prgname || s->sock_kill)
  381: 		return -1;
  382: 
  383: 	return ioLoopSocket(s, io_bridgeSock);
  384: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>