#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
+#include <netdb.h>
#include <fcntl.h>
#include "gps.h"
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;
}
* 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;
}