aboutsummaryrefslogtreecommitdiff
path: root/client.c
diff options
context:
space:
mode:
authorToni Uhlig <matzeton@googlemail.com>2020-07-21 15:44:41 +0200
committerToni Uhlig <matzeton@googlemail.com>2020-07-21 15:44:41 +0200
commitb8f28b2f7ba83f25413c0da02ba5248bc0433a63 (patch)
treea1ffe5693b0c29ff40e1c5e510a25bfd812a96fc /client.c
parent46305df5903270d9b63652a1ce5fae2ee861a0e4 (diff)
use getaddrinfo() for client/server address resolving
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'client.c')
-rw-r--r--client.c54
1 files changed, 44 insertions, 10 deletions
diff --git a/client.c b/client.c
index c8bb974..674afb9 100644
--- a/client.c
+++ b/client.c
@@ -228,13 +228,15 @@ __attribute__((noreturn)) static void cleanup_and_exit(struct event_base ** cons
int main(int argc, char ** argv)
{
+ struct addrinfo * connect_addresses = NULL;
+ struct addrinfo * ai;
+ int gai_errno;
+ int bev_connected = 0;
struct event_base * ev_base = NULL;
struct event * ev_sig = NULL;
struct bufferevent * bev;
- struct sockaddr_in sin;
struct longterm_keypair * my_keypair = NULL;
struct connection * c = NULL;
-
char ip_str[INET6_ADDRSTRLEN + 1];
parse_cmdline(&opts, argc, argv);
@@ -252,8 +254,14 @@ int main(int argc, char ** argv)
return 1;
}
}
- if (opts.port <= 0 || opts.port > 65535) {
- LOG(ERROR, "Invalid port: %d", opts.port);
+ if (opts.host == NULL || opts.port == NULL) {
+ LOG(ERROR, "Invalid host/port");
+ return 2;
+ }
+ LOG(NOTICE, "Resolving %s:%s..", opts.host, opts.port);
+ gai_errno = hostname_to_address(opts.host, opts.port, &connect_addresses);
+ if (gai_errno != 0) {
+ LOG(ERROR, "Address/Service translation error for %s:%s: %s", opts.host, opts.port, gai_strerror(gai_errno));
return 2;
}
@@ -264,11 +272,6 @@ int main(int argc, char ** argv)
return 3;
}
- if (init_sockaddr_inet(&sin, opts.host, opts.port, ip_str) != 0) {
- return 4;
- }
- LOG(NOTICE, "Connecting to %s:%u", ip_str, opts.port);
-
/* generate client keypair */
my_keypair = generate_keypair_sodium();
if (my_keypair == NULL) {
@@ -322,7 +325,38 @@ int main(int argc, char ** argv)
}
ev_set_io_timeouts(bev);
- if (bufferevent_socket_connect(bev, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
+ for (ai = connect_addresses; ai != NULL; ai = ai->ai_next) {
+ struct sockaddr * sa = NULL;
+ socklen_t sl = 0;
+
+ switch (ai->ai_family) {
+ case AF_INET:
+ sa = (struct sockaddr *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr;
+ sl = sizeof(struct sockaddr_in);
+ break;
+ case AF_INET6:
+ sa = (struct sockaddr *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr;
+ sl = sizeof(struct sockaddr_in6);
+ break;
+ default:
+ LOG(ERROR, "Unknown address family: %d", ai->ai_family);
+ cleanup_and_exit(&ev_base, &ev_sig, &my_keypair, &c, 14);
+ }
+ if (inet_ntop(ai->ai_family, sa, ip_str, sl) == NULL) {
+ LOG(WARNING, "Invalid IPv4 host");
+ continue;
+ }
+
+ LOG(NOTICE, "Connecting to %s:%s", ip_str, opts.port);
+ if (bufferevent_socket_connect(bev, ai->ai_addr, ai->ai_addrlen) == 0) {
+ bev_connected = 1;
+ break;
+ }
+ }
+ freeaddrinfo(connect_addresses);
+ connect_addresses = NULL;
+
+ if (bev_connected == 0) {
cleanup_and_exit(&ev_base, &ev_sig, &my_keypair, &c, 14);
}