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