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