--- libaitio/src/sock.c 2013/08/12 20:50:27 1.1.2.1 +++ libaitio/src/sock.c 2013/08/27 20:08:26 1.2.2.7 @@ -1,3 +1,315 @@ +/************************************************************************* +* (C) 2013 AITNET ltd - Sofia/Bulgaria - +* by Michael Pounov +* +* $Author: misho $ +* $Id: sock.c,v 1.2.2.7 2013/08/27 20:08:26 misho Exp $ +* +************************************************************************** +The ELWIX and AITNET software is distributed under the following +terms: + +All of the documentation and software included in the ELWIX and AITNET +Releases is copyrighted by ELWIX - Sofia/Bulgaria + +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 + by Michael Pounov . All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: +1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software + must display the following acknowledgement: +This product includes software developed by Michael Pounov +ELWIX - Embedded LightWeight unIX and its contributors. +4. Neither the name of AITNET nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +SUCH DAMAGE. +*/ #include "global.h" +/* + * ioInitSocket() - Init socket and allocate resources + * + * @role = Socket role + * @type = Socket type + * @proto = Socket protocol + * @addr = Bind to address + * @port = Bind to port + * @buflen = Socket buffer, optional if =0 == BUFSIZ + * return: NULL error or !=NULL created socket + */ +sock_t * +ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen) +{ + sock_t *s = NULL; + int n = 1; + + if (!addr) + return NULL; + + s = e_malloc(sizeof(sock_t)); + if (!s) { + io_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + return NULL; + } else + memset(s, 0, sizeof(sock_t)); + + TAILQ_INIT(&s->sock_cli); + + s->sock_role = role; + s->sock_type = type; + s->sock_proto = proto; + if (!e_gethostbyname(addr, port, &s->sock_addr)) { + io_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + e_free(s); + return NULL; + } else { + buflen = buflen ? buflen : BUFSIZ; + AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen); + } + + s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto); + if (s->sock_fd == -1) { + LOGERR; + AIT_FREE_VAL(&s->sock_buf); + e_free(s); + return NULL; + } + if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) { + LOGERR; + AIT_FREE_VAL(&s->sock_buf); + e_free(s); + return NULL; + } + if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) { + LOGERR; + AIT_FREE_VAL(&s->sock_buf); + e_free(s); + return NULL; + } + if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) { + LOGERR; + AIT_FREE_VAL(&s->sock_buf); + e_free(s); + return NULL; + } + if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) { + LOGERR; + AIT_FREE_VAL(&s->sock_buf); + e_free(s); + return NULL; + } + + pthread_mutex_init(&s->sock_mtx, NULL); + return s; +} + +/* + * ioCloseSocket() - Close socket and free resources + * + * @s = Socket + * return: none + */ +void +ioCloseSocket(sock_t ** __restrict s) +{ + sock_cli_t *cli; + + if (s && *s) { + pthread_mutex_lock(&(*s)->sock_mtx); + while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) { + TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node); + shutdown(cli->cli_fd, SHUT_RDWR); + close(cli->cli_fd); + AIT_FREE_VAL(&cli->cli_buf); + e_free(cli); + } + pthread_mutex_unlock(&(*s)->sock_mtx); + + shutdown((*s)->sock_fd, SHUT_RDWR); + close((*s)->sock_fd); + + AIT_FREE_VAL(&(*s)->sock_buf); + + pthread_mutex_destroy(&(*s)->sock_mtx); + e_free(*s); + *s = NULL; + } +} + +/* + * ioUpSocket() - Setup socket for use + * + * @s = Socket + * @arg = Server role = listen backlog queue and Client role = peer address + * return: -1 error or 0 ok + */ +int +ioUpSocket(sock_t * __restrict s, void *arg) +{ + int ret = 0; + sockaddr_t *peer = (sockaddr_t*) arg; + uintptr_t backlog = (uintptr_t) arg; + + if (!s || !arg) + return -1; + + switch (s->sock_role) { + case IO_SOCK_ROLE_CLIENT: + memcpy(&s->sock_peer, peer, sizeof s->sock_peer); + + if (connect(s->sock_fd, &s->sock_peer.sa, + s->sock_peer.sa.sa_len) == -1) { + LOGERR; + return -1; + } + break; + case IO_SOCK_ROLE_SERVER: + if (s->sock_type == SOCK_STREAM) { + s->sock_backq = backlog; + + if (listen(s->sock_fd, s->sock_backq) == -1) { + LOGERR; + return -1; + } + } + break; + default: + io_SetErr(EINVAL, "Unsupported socket type"); + return -1; + } + + fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK); + return ret; +} + +static void +thrCliClean(void *arg) +{ + sock_cli_t *cli = (sock_cli_t*) arg; + sock_t *s = (sock_t*) cli->cli_parent; + + if (s->sock_type == SOCK_STREAM) { + shutdown(cli->cli_fd, SHUT_RDWR); + close(cli->cli_fd); + } + AIT_FREE_VAL(&cli->cli_buf); + + pthread_mutex_lock(&s->sock_mtx); + TAILQ_REMOVE(&s->sock_cli, cli, cli_node); + pthread_mutex_unlock(&s->sock_mtx); + + e_free(cli); +} + +static void * +io_thrCliWrapper(void *arg) +{ + void *ret; + sock_cli_t *cli = (sock_cli_t*) arg; + + pthread_cleanup_push(thrCliClean, arg); + + ret = cli->cli_func(cli); + + pthread_cleanup_pop(42); + pthread_exit(ret); +} + +/* + * ioAcceptSocket() - Accept clients + * + * @s = Socket + * @f = callback function for client handling + * @arg = optional argument for callback function + * return: -1 error or 0 ok + */ +int +ioAcceptSocket(sock_t * __restrict s, sock_cb_t f, void *arg) +{ + struct pollfd pfd[1]; + socklen_t salen; + sockaddr_t sa; + int c, rlen; + sock_cli_t *cli; + u_char buf[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 }; + + if (!s || s->sock_role == IO_SOCK_ROLE_CLIENT || !f) + return -1; + + pfd->fd = s->sock_fd; + pfd->events = POLLIN | POLLPRI; + do { + if (poll(pfd, 1, -1) < 1 || + pfd->revents & (POLLNVAL | POLLHUP | POLLERR)) { + LOGERR; + return -1; + } else + salen = sizeof sa.ss; + + if (s->sock_type == SOCK_STREAM) { + if ((c = accept(s->sock_fd, &sa.sa, &salen)) == -1) { + LOGERR; + return -1; + } + } else { + if ((rlen = recvfrom(s->sock_fd, buf, sizeof buf, MSG_PEEK, + &sa.sa, &salen)) == -1) { + LOGERR; + return -1; + } else + c = s->sock_fd; + } + + cli = e_malloc(sizeof(sock_cli_t)); + if (!cli) { + io_SetErr(elwix_GetErrno(), "%s", elwix_GetError()); + if (s->sock_type == SOCK_STREAM) + close(c); + return -1; + } else + memset(cli, 0, sizeof(sock_cli_t)); + + cli->cli_parent = s; + cli->cli_fd = c; + cli->cli_func = f; + cli->cli_arg = arg; + memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr); + AIT_SET_BUFSIZ(&cli->cli_buf, 0, AIT_LEN(&s->sock_buf)); + + if (pthread_create(&cli->cli_tid, NULL, io_thrCliWrapper, cli) == -1) { + LOGERR; + if (s->sock_type == SOCK_STREAM) + close(c); + AIT_FREE_VAL(&cli->cli_buf); + e_free(cli); + return -1; + } else { + pthread_detach(cli->cli_tid); + pthread_mutex_lock(&s->sock_mtx); + TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node); + pthread_mutex_unlock(&s->sock_mtx); + } + } while (42); + + return 0; +}