2 * Copyright (C) 2008 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>
26 #include "sort_udns.h"
28 int tcp_connect_a4 (struct in_addr addr, int port)
31 struct sockaddr_in in4;
32 fd = socket (PF_INET, SOCK_STREAM, 0);
35 in4.sin_family = AF_INET;
36 in4.sin_port = htons (port);
38 if (connect (fd, (struct sockaddr *) &in4, sizeof (in4)) < 0)
47 tcp_connect_a6 (struct in6_addr addr, int port)
50 struct sockaddr_in6 in6;
51 fd = socket (PF_INET6, SOCK_STREAM, 0);
54 memset (&in6, sizeof (in6), 0);
55 in6.sin6_family = AF_INET6;
56 in6.sin6_port = htons (port);
58 if (connect (fd, (struct sockaddr *) &in6, sizeof (in6)) < 0)
67 tcp_connect4 (struct dns_srv srv)
72 a4 = dns_resolve_a4 (NULL, srv.name, 0);
75 for (i = 0; i < a4->dnsa4_nrr; i++)
77 fd = tcp_connect_a4 (a4->dnsa4_addr[i], srv.port);
89 tcp_connect6 (struct dns_srv srv)
94 a6 = dns_resolve_a6 (NULL, srv.name, 0);
97 for (i = 0; i < a6->dnsa6_nrr; i++)
99 fd = tcp_connect_a6 (a6->dnsa6_addr[i], srv.port);
111 hc_tcp_connect (char *server, char *service)
113 struct dns_rr_srv *srv;
116 srv = dns_resolve_srv (NULL, server, service, "tcp", 0);
119 hc_dns_srv_sort (srv->dnssrv_srv, srv->dnssrv_nrr);
120 for (i = 0; i < srv->dnssrv_nrr; i++)
122 fd = tcp_connect6 (srv->dnssrv_srv[i]);
124 fd = tcp_connect4 (srv->dnssrv_srv[i]);
137 main (int argc, char **argv)
143 server = (argc >= 2) ? argv[1] : "holoscopio.com";
144 service = (argc >= 3) ? argv[2] : "xmpp-client";
145 fd = hc_tcp_connect (server, service);