1 /* This file is part of libtorcontrol.
3 * Copyright (C) 2010 Philipp Zabel
5 * libtorcontrol 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 3 of the License, or
8 * (at your option) any later version.
10 * libtorcontrol 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 libtorcontrol. If not, see <http://www.gnu.org/licenses/>.
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 int tor_control_open_socket (int port, GError **error);
30 int tor_control_open_socket (int port, GError **error) {
33 struct addrinfo hints;
34 struct addrinfo *result, *rp;
38 memset (&hints, 0, sizeof hints);
39 hints.ai_family = AF_UNSPEC;
40 hints.ai_socktype = SOCK_STREAM;
41 hints.ai_flags = AI_PASSIVE;
43 service = g_strdup_printf ("%d", port);
45 status = getaddrinfo (NULL, service, &hints, &result);
51 /* domain: */ g_quark_from_string ("TORCTLERR"), /* code: */ 1,
52 "getaddrinfo error: %s", gai_strerror (status));
56 for (rp = result; rp != NULL; rp = rp->ai_next) {
57 sockfd = socket (result->ai_family, result->ai_socktype,
62 status = connect (sockfd, result->ai_addr, result->ai_addrlen);
69 freeaddrinfo (result);
73 /* domain: */ g_quark_from_string ("TORCTLERR"), /* code: */ 2,
74 "socket/connect error: %s", strerror (errno));