]> err.no Git - mapper/commitdiff
Allow using hostnames for gpsd connection, not only IP addresses.
authorKaj-Michael Lang <milang@tal.org>
Fri, 4 Apr 2008 12:37:39 +0000 (15:37 +0300)
committerKaj-Michael Lang <milang@tal.org>
Fri, 4 Apr 2008 12:37:39 +0000 (15:37 +0300)
src/gps.c

index bdd85eb65a65d66401d5a80dd754801f01e2aa42..7f89ae9dc796b9cb2fb986522d3a64ff20efaa2e 100644 (file)
--- a/src/gps.c
+++ b/src/gps.c
@@ -41,6 +41,7 @@
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
+#include <netdb.h>
 #include <fcntl.h>
 
 #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;
                        }