2 * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3 * Copyright (C) 2009 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <netinet/in.h>
29 tcp_server (struct addrinfo *ai)
33 fd = socket (ai->ai_family, ai->ai_socktype, ai->ai_protocol);
36 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof (int));
37 if (bind (fd, ai->ai_addr, ai->ai_addrlen) < 0)
42 if (listen (fd, 5) < 0)
51 tcp_server_list (struct addrinfo *ai)
54 for (; ai; ai = ai->ai_next)
66 hc_tcp_server (char *service)
69 struct addrinfo *ai = NULL;
71 hint.ai_family = AF_UNSPEC;
72 hint.ai_socktype = SOCK_STREAM;
74 hint.ai_flags = AI_PASSIVE | AI_ADDRCONFIG | AI_V4MAPPED;
75 if (getaddrinfo (NULL, service, &hint, &ai) < 0)
77 fd = tcp_server_list (ai);
84 main (int argc, char **argv)
88 service = (argc >= 2) ? argv[1] : "110";
89 fd = hc_tcp_server (service);