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

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 $
1.1.2.4 ! misho       6: * $Id: sock.c,v 1.1.2.3 2013/08/12 23:25:35 misho Exp $
1.1.2.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: */
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;
1.1.2.3   misho      79:        if (!e_gethostbyname(addr, port, &s->sock_addr)) {
                     80:                io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                     81:                e_free(s);
                     82:                return NULL;
1.1.2.4 ! misho      83:        } else {
        !            84:                buflen = buflen ? buflen : BUFSIZ;
        !            85:                AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
        !            86:        }
1.1.2.2   misho      87: 
                     88:        s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
                     89:        if (s->sock_fd == -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_SNDBUF, &buflen, sizeof buflen) == -1) {
                     96:                LOGERR;
                     97:                AIT_FREE_VAL(&s->sock_buf);
                     98:                e_free(s);
                     99:                return NULL;
                    100:        }
                    101:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
                    102:                LOGERR;
                    103:                AIT_FREE_VAL(&s->sock_buf);
                    104:                e_free(s);
                    105:                return NULL;
                    106:        }
1.1.2.3   misho     107:        if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
1.1.2.2   misho     108:                LOGERR;
                    109:                AIT_FREE_VAL(&s->sock_buf);
                    110:                e_free(s);
                    111:                return NULL;
                    112:        }
1.1.2.3   misho     113:        if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
                    114:                LOGERR;
                    115:                AIT_FREE_VAL(&s->sock_buf);
                    116:                e_free(s);
                    117:                return NULL;
1.1.2.2   misho     118:        }
                    119: 
                    120:        return s;
                    121: }
                    122: 
                    123: /*
                    124:  * ioCloseSocket() - Close socket and free resources
                    125:  *
                    126:  * @s = Socket
                    127:  * return: none
                    128:  */
                    129: void
                    130: ioCloseSocket(sock_t ** __restrict s)
                    131: {
                    132:        if (s && *s) {
                    133:                shutdown((*s)->sock_fd, SHUT_RDWR);
                    134:                close((*s)->sock_fd);
                    135: 
                    136:                AIT_FREE_VAL(&(*s)->sock_buf);
                    137:                e_free(*s);
                    138:                *s = NULL;
                    139:        }
                    140: }
                    141: 
                    142: /*
                    143:  * ioUpSocket() - Setup socket for use
                    144:  *
                    145:  * @s = Socket
                    146:  * @arg = Server role = listen backlog queue and Client role = peer address
                    147:  * return: -1 error or 0 ok
                    148:  */
                    149: int
                    150: ioUpSocket(sock_t * __restrict s, void *arg)
                    151: {
                    152:        int ret = 0;
                    153:        sockaddr_t *peer = (sockaddr_t*) arg;
                    154:        uintptr_t backlog = (uintptr_t) arg;
                    155: 
                    156:        if (!s || !arg)
                    157:                return -1;
                    158: 
                    159:        switch (s->sock_role) {
1.1.2.3   misho     160:                case IO_SOCK_ROLE_CLIENT:
1.1.2.2   misho     161:                        memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
                    162: 
                    163:                        if (connect(s->sock_fd, &s->sock_peer.sa, 
                    164:                                                s->sock_peer.sa.sa_len) == -1) {
                    165:                                LOGERR;
                    166:                                return -1;
                    167:                        }
                    168:                        break;
1.1.2.3   misho     169:                case IO_SOCK_ROLE_SERVER:
                    170:                        if (s->sock_type == SOCK_STREAM) {
                    171:                                s->sock_backq = backlog;
                    172: 
                    173:                                if (listen(s->sock_fd, s->sock_backq) == -1) {
                    174:                                        LOGERR;
                    175:                                        return -1;
                    176:                                }
1.1.2.2   misho     177:                        }
                    178:                        break;
                    179:                default:
                    180:                        io_SetErr(EINVAL, "Unsupported socket type");
                    181:                        return -1;
                    182:        }
                    183: 
                    184:        fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
                    185:        return ret;
                    186: }

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