Annotation of libaitio/src/sock.c, revision 1.1.2.2

1.1.2.2 ! misho       1: /*************************************************************************
        !             2: * (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
        !             3: *  by Michael Pounov <misho@elwix.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: aitio.h,v 1.33 2013/07/09 00:35:35 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: */
1.1.2.1   misho      46: #include "global.h"
                     47: 
                     48: 
1.1.2.2 ! misho      49: /*
        !            50:  * ioInitSocket() - Init socket and allocate resources
        !            51:  *
        !            52:  * @role = Socket role
        !            53:  * @type = Socket type
        !            54:  * @proto = Socket protocol
        !            55:  * @addr = Bind to address
        !            56:  * @port = Bind to port
        !            57:  * @buflen = Socket buffer, optional if =0 == BUFSIZ
        !            58:  * return: NULL error or !=NULL created socket
        !            59:  */
        !            60: sock_t *
        !            61: ioInitSocket(int role, int type, int proto, const char *addr, u_short port, size_t buflen)
        !            62: {
        !            63:        sock_t *s = NULL;
        !            64:        int n = 1;
        !            65: 
        !            66:        if (!addr)
        !            67:                return NULL;
        !            68: 
        !            69:        s = e_malloc(sizeof(sock_t));
        !            70:        if (!s) {
        !            71:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
        !            72:                return NULL;
        !            73:        } else
        !            74:                memset(s, 0, sizeof(sock_t));
        !            75: 
        !            76:        s->sock_role = role;
        !            77:        s->sock_type = type;
        !            78:        s->sock_proto = proto;
        !            79:        e_gethostbyname(addr, port, &s->sock_addr);
        !            80:        AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen ? buflen : BUFSIZ);
        !            81: 
        !            82:        s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
        !            83:        if (s->sock_fd == -1) {
        !            84:                LOGERR;
        !            85:                AIT_FREE_VAL(&s->sock_buf);
        !            86:                e_free(s);
        !            87:                return NULL;
        !            88:        }
        !            89:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
        !            90:                LOGERR;
        !            91:                AIT_FREE_VAL(&s->sock_buf);
        !            92:                e_free(s);
        !            93:                return NULL;
        !            94:        }
        !            95:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
        !            96:                LOGERR;
        !            97:                AIT_FREE_VAL(&s->sock_buf);
        !            98:                e_free(s);
        !            99:                return NULL;
        !           100:        }
        !           101:        if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
        !           102:                LOGERR;
        !           103:                AIT_FREE_VAL(&s->sock_buf);
        !           104:                e_free(s);
        !           105:                return NULL;
        !           106:        }
        !           107: 
        !           108:        switch (s->sock_role) {
        !           109:                case IO_SOCK_TYPE_CLIENT:
        !           110:                        break;
        !           111:                case IO_SOCK_TYPE_SERVER:
        !           112:                        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
        !           113:                                LOGERR;
        !           114:                                AIT_FREE_VAL(&s->sock_buf);
        !           115:                                e_free(s);
        !           116:                                return NULL;
        !           117:                        }
        !           118:                        break;
        !           119:                default:
        !           120:                        io_SetErr(EINVAL, "Unsupported socket type");
        !           121:                        AIT_FREE_VAL(&s->sock_buf);
        !           122:                        e_free(s);
        !           123:                        return NULL;
        !           124:        }
        !           125: 
        !           126:        return s;
        !           127: }
        !           128: 
        !           129: /*
        !           130:  * ioCloseSocket() - Close socket and free resources
        !           131:  *
        !           132:  * @s = Socket
        !           133:  * return: none
        !           134:  */
        !           135: void
        !           136: ioCloseSocket(sock_t ** __restrict s)
        !           137: {
        !           138:        if (s && *s) {
        !           139:                shutdown((*s)->sock_fd, SHUT_RDWR);
        !           140:                close((*s)->sock_fd);
        !           141: 
        !           142:                AIT_FREE_VAL(&(*s)->sock_buf);
        !           143:                e_free(*s);
        !           144:                *s = NULL;
        !           145:        }
        !           146: }
        !           147: 
        !           148: /*
        !           149:  * ioUpSocket() - Setup socket for use
        !           150:  *
        !           151:  * @s = Socket
        !           152:  * @arg = Server role = listen backlog queue and Client role = peer address
        !           153:  * return: -1 error or 0 ok
        !           154:  */
        !           155: int
        !           156: ioUpSocket(sock_t * __restrict s, void *arg)
        !           157: {
        !           158:        int ret = 0;
        !           159:        sockaddr_t *peer = (sockaddr_t*) arg;
        !           160:        uintptr_t backlog = (uintptr_t) arg;
        !           161: 
        !           162:        if (!s || !arg)
        !           163:                return -1;
        !           164: 
        !           165:        switch (s->sock_role) {
        !           166:                case IO_SOCK_TYPE_CLIENT:
        !           167:                        memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
        !           168: 
        !           169:                        if (connect(s->sock_fd, &s->sock_peer.sa, 
        !           170:                                                s->sock_peer.sa.sa_len) == -1) {
        !           171:                                LOGERR;
        !           172:                                return -1;
        !           173:                        }
        !           174:                        break;
        !           175:                case IO_SOCK_TYPE_SERVER:
        !           176:                        s->sock_backq = backlog;
        !           177: 
        !           178:                        if (listen(s->sock_fd, s->sock_backq) == -1) {
        !           179:                                LOGERR;
        !           180:                                return -1;
        !           181:                        }
        !           182:                        break;
        !           183:                default:
        !           184:                        io_SetErr(EINVAL, "Unsupported socket type");
        !           185:                        return -1;
        !           186:        }
        !           187: 
        !           188:        fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
        !           189:        return ret;
        !           190: }

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