From: Kaj-Michael Lang Date: Fri, 4 Apr 2008 12:37:39 +0000 (+0300) Subject: Allow using hostnames for gpsd connection, not only IP addresses. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f0816495a14517cfd8312fba17aeec69acf2858e;p=mapper Allow using hostnames for gpsd connection, not only IP addresses. --- diff --git a/src/gps.c b/src/gps.c index bdd85eb..7f89ae9 100644 --- a/src/gps.c +++ b/src/gps.c @@ -41,6 +41,7 @@ #include #include #include +#include #include #include "gps.h" @@ -520,16 +521,39 @@ switch (gps->io.type) { case GPS_IO_TCP: case GPS_IO_GPSD: if (gps->io.conn<=RCVR_DOWN && gps->io.address) { + const gchar *ip; + struct hostent *hostinfo; + struct in_addr taddr; + + g_debug("TCP: Preparing to connect to %s:%d", gps->io.address, gps->io.port); + gps->io.fd=socket(AF_INET, SOCK_STREAM, 0); if (gps->io.fd==-1) { g_debug("Failed to create socket\n"); gps_connect_later(gps); return FALSE; } - g_debug("TCP: Preparing to connect to %s:%d", gps->io.address, gps->io.port); - gps->io.rcvr_addr_in.sin_family = AF_INET; - gps->io.rcvr_addr_in.sin_port = htons(gps->io.port); - if (inet_pton(AF_INET, gps->io.address, &gps->io.rcvr_addr_in.sin_addr.s_addr)<1) { + + gps->io.rcvr_addr_in.sin_family=AF_INET; + gps->io.rcvr_addr_in.sin_port=htons(gps->io.port); + if (inet_aton(gps->io.address, &taddr)) { + ip=gps->io.address; + } else { + gchar ipbuf[INET_ADDRSTRLEN]; + + hostinfo=gethostbyname(gps->io.address); + if (!hostinfo) { + perror("TCP:"); + return FALSE; + } + if (hostinfo->h_addrtype!=AF_INET) { + perror("TCP:"); + return FALSE; + } + ip=ip = inet_ntop (AF_INET, (struct in_addr *)hostinfo->h_addr_list[0], ipbuf, sizeof (ipbuf)); + } + + if (inet_pton(AF_INET, ip, &gps->io.rcvr_addr_in.sin_addr.s_addr)<1) { perror("TCP:"); return FALSE; } @@ -593,7 +617,7 @@ switch (gps->io.type) { * descriptor creation is the first step, so if it fails, there's * nothing to clean up. */ if (gps->io.fd==-1) { - g_debug("Failed to create socket\n"); + g_debug("Failed to create socket"); gps_connect_later(gps); return FALSE; }