version 1.1.2.1, 2013/08/12 20:50:27
|
version 1.1.2.3, 2013/08/12 23:25:35
|
Line 1
|
Line 1
|
|
/************************************************************************* |
|
* (C) 2013 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org> |
|
* by Michael Pounov <misho@elwix.org> |
|
* |
|
* $Author$ |
|
* $Id$ |
|
* |
|
************************************************************************** |
|
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 <info@elwix.org> |
|
|
|
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 |
|
by Michael Pounov <misho@elwix.org>. 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 <misho@elwix.org> |
|
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" |
#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)); |
|
|
|
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 |
|
AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen ? buflen : BUFSIZ); |
|
|
|
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; |
|
} |
|
|
|
return s; |
|
} |
|
|
|
/* |
|
* ioCloseSocket() - Close socket and free resources |
|
* |
|
* @s = Socket |
|
* return: none |
|
*/ |
|
void |
|
ioCloseSocket(sock_t ** __restrict s) |
|
{ |
|
if (s && *s) { |
|
shutdown((*s)->sock_fd, SHUT_RDWR); |
|
close((*s)->sock_fd); |
|
|
|
AIT_FREE_VAL(&(*s)->sock_buf); |
|
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; |
|
} |