Annotation of libaitio/src/sock.c, revision 1.2.2.5
1.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.2.2.5 ! misho 6: * $Id: sock.c,v 1.2.2.4 2013/08/27 19:24:37 misho Exp $
1.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: */
46: #include "global.h"
47:
48:
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:
1.2.2.1 misho 76: TAILQ_INIT(&s->sock_cli);
77:
1.2 misho 78: s->sock_role = role;
79: s->sock_type = type;
80: s->sock_proto = proto;
81: if (!e_gethostbyname(addr, port, &s->sock_addr)) {
82: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
83: e_free(s);
84: return NULL;
85: } else {
86: buflen = buflen ? buflen : BUFSIZ;
87: AIT_SET_BUFSIZ(&s->sock_buf, 0, buflen);
88: }
89:
90: s->sock_fd = socket(s->sock_addr.sa.sa_family, s->sock_type, s->sock_proto);
91: if (s->sock_fd == -1) {
92: LOGERR;
93: AIT_FREE_VAL(&s->sock_buf);
94: e_free(s);
95: return NULL;
96: }
97: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_SNDBUF, &buflen, sizeof buflen) == -1) {
98: LOGERR;
99: AIT_FREE_VAL(&s->sock_buf);
100: e_free(s);
101: return NULL;
102: }
103: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_RCVBUF, &buflen, sizeof buflen) == -1) {
104: LOGERR;
105: AIT_FREE_VAL(&s->sock_buf);
106: e_free(s);
107: return NULL;
108: }
109: if (setsockopt(s->sock_fd, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
110: LOGERR;
111: AIT_FREE_VAL(&s->sock_buf);
112: e_free(s);
113: return NULL;
114: }
115: if (bind(s->sock_fd, &s->sock_addr.sa, s->sock_addr.sa.sa_len) == -1) {
116: LOGERR;
117: AIT_FREE_VAL(&s->sock_buf);
118: e_free(s);
119: return NULL;
120: }
121:
1.2.2.1 misho 122: pthread_mutex_init(&s->sock_mtx, NULL);
1.2 misho 123: return s;
124: }
125:
126: /*
127: * ioCloseSocket() - Close socket and free resources
128: *
129: * @s = Socket
130: * return: none
131: */
132: void
133: ioCloseSocket(sock_t ** __restrict s)
134: {
1.2.2.1 misho 135: sock_cli_t *cli;
136:
1.2 misho 137: if (s && *s) {
1.2.2.1 misho 138: pthread_mutex_lock(&(*s)->sock_mtx);
139: while ((cli = TAILQ_FIRST(&(*s)->sock_cli))) {
140: TAILQ_REMOVE(&(*s)->sock_cli, cli, cli_node);
141: shutdown(cli->cli_fd, SHUT_RDWR);
142: close(cli->cli_fd);
143: AIT_FREE_VAL(&cli->cli_buf);
144: e_free(cli);
145: }
146: pthread_mutex_unlock(&(*s)->sock_mtx);
147:
1.2 misho 148: shutdown((*s)->sock_fd, SHUT_RDWR);
149: close((*s)->sock_fd);
150:
151: AIT_FREE_VAL(&(*s)->sock_buf);
1.2.2.1 misho 152:
153: pthread_mutex_destroy(&(*s)->sock_mtx);
1.2 misho 154: e_free(*s);
155: *s = NULL;
156: }
157: }
158:
159: /*
160: * ioUpSocket() - Setup socket for use
161: *
162: * @s = Socket
163: * @arg = Server role = listen backlog queue and Client role = peer address
164: * return: -1 error or 0 ok
165: */
166: int
167: ioUpSocket(sock_t * __restrict s, void *arg)
168: {
169: int ret = 0;
170: sockaddr_t *peer = (sockaddr_t*) arg;
171: uintptr_t backlog = (uintptr_t) arg;
172:
173: if (!s || !arg)
174: return -1;
175:
176: switch (s->sock_role) {
177: case IO_SOCK_ROLE_CLIENT:
178: memcpy(&s->sock_peer, peer, sizeof s->sock_peer);
179:
180: if (connect(s->sock_fd, &s->sock_peer.sa,
181: s->sock_peer.sa.sa_len) == -1) {
182: LOGERR;
183: return -1;
184: }
185: break;
186: case IO_SOCK_ROLE_SERVER:
187: if (s->sock_type == SOCK_STREAM) {
188: s->sock_backq = backlog;
189:
190: if (listen(s->sock_fd, s->sock_backq) == -1) {
191: LOGERR;
192: return -1;
193: }
194: }
195: break;
196: default:
197: io_SetErr(EINVAL, "Unsupported socket type");
198: return -1;
199: }
200:
201: fcntl(s->sock_fd, F_SETFL, fcntl(s->sock_fd, F_GETFL) | O_NONBLOCK);
202: return ret;
203: }
1.2.2.1 misho 204:
1.2.2.3 misho 205: static void
206: thrCliClean(void *arg)
207: {
208: sock_cli_t *cli = (sock_cli_t*) arg;
209:
1.2.2.5 ! misho 210: shutdown(cli->cli_fd, SHUT_RDWR);
1.2.2.3 misho 211: close(cli->cli_fd);
212: AIT_FREE_VAL(&cli->cli_buf);
213: e_free(cli);
214: }
215:
216: static void *
217: io_thrCliWrapper(void *arg)
218: {
1.2.2.4 misho 219: void *ret;
220: sock_cli_t *cli = (sock_cli_t*) arg;
221:
1.2.2.3 misho 222: pthread_cleanup_push(thrCliClean, arg);
223:
1.2.2.4 misho 224: ret = cli->cli_func(cli);
225:
1.2.2.3 misho 226: pthread_cleanup_pop(42);
1.2.2.5 ! misho 227: pthread_exit(ret);
1.2.2.3 misho 228: }
229:
1.2.2.1 misho 230: /*
231: * ioAcceptSocket() - Accept clients
232: *
233: * @s = Socket
234: * @f = callback function for client handling
235: * @arg = optional argument for callback function
236: * return: -1 error or 0 ok
237: */
238: int
239: ioAcceptSocket(sock_t * __restrict s, sock_cb_t f, void *arg)
240: {
241: struct pollfd pfd[1];
242: socklen_t salen;
243: sockaddr_t sa;
1.2.2.2 misho 244: int c, rlen;
1.2.2.1 misho 245: sock_cli_t *cli;
1.2.2.2 misho 246: u_char buf[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 };
1.2.2.1 misho 247:
248: if (!s || s->sock_role == IO_SOCK_ROLE_CLIENT || !f)
249: return -1;
250:
251: pfd->fd = s->sock_fd;
252: pfd->events = POLLIN | POLLPRI;
253: do {
1.2.2.3 misho 254: if (poll(pfd, 1, -1) < 1 ||
255: pfd->revents & (POLLNVAL | POLLHUP || POLLERR)) {
1.2.2.1 misho 256: LOGERR;
257: return -1;
258: } else
259: salen = sizeof sa.ss;
260:
1.2.2.2 misho 261: if (s->sock_type == SOCK_STREAM) {
262: if ((c = accept(s->sock_fd, &sa.sa, &salen)) == -1) {
263: LOGERR;
264: return -1;
265: }
266: } else {
267: if ((rlen = recvfrom(s->sock_fd, buf, sizeof buf, MSG_PEEK,
268: &sa.sa, &salen)) == -1) {
269: LOGERR;
270: return -1;
271: } else
272: c = s->sock_fd;
1.2.2.1 misho 273: }
274:
275: cli = e_malloc(sizeof(sock_cli_t));
276: if (!cli) {
277: io_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
278: close(c);
279: return -1;
280: } else
281: memset(cli, 0, sizeof(sock_cli_t));
282:
283: cli->cli_parent = s;
284: cli->cli_fd = c;
285: cli->cli_func = f;
286: cli->cli_arg = arg;
287: memcpy(&cli->cli_addr, &sa, sizeof cli->cli_addr);
288: AIT_SET_BUFSIZ(&cli->cli_buf, 0, AIT_LEN(&s->sock_buf));
289:
1.2.2.3 misho 290: if (pthread_create(&cli->cli_tid, NULL, io_thrCliWrapper, cli) == -1) {
1.2.2.1 misho 291: LOGERR;
292: close(c);
293: AIT_FREE_VAL(&cli->cli_buf);
294: e_free(cli);
295: return -1;
296: } else {
297: pthread_detach(cli->cli_tid);
298: TAILQ_INSERT_TAIL(&s->sock_cli, cli, cli_node);
299: }
300: } while (42);
301:
302: return 0;
303: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>