diff options
author | Robert Norris <rw_norris@hotmail.com> | 2017-02-14 21:30:25 +0000 |
---|---|---|
committer | Fred Wright <fw@fwright.net> | 2017-02-17 14:46:05 -0800 |
commit | 92e30abaa3f7fb298d05ebe207c16f7096d0c15a (patch) | |
tree | d55dc7876efef57847e635afb80efd0274cfa3fb /netlib.c | |
parent | b8890ecae932892fd8e6fc45a3f003c937d3f82f (diff) | |
download | gpsd-92e30abaa3f7fb298d05ebe207c16f7096d0c15a.tar.gz |
Windows libgps support
Add Windows versions for network functions and disable unused features.
Add detection of various headers and functions that aren't available in
Windows. Note that netlib_localsocket() has no functional implementation
on Windows, but it isn't to be used on that platform and it's not part of
libgps anyway.
Using send() rather than write() seems to work on Windows.
For Windows need to ensure networking is initialised on opening of sockets
(and then correspondingly shutdown upon closing).
Note that within gpsd.h the termios structures and serial related functions
are removed from the Windows build. These are only accessed in serial.c by
gpsd, so since the Windows build is only generating libgps there is
currently no need to modify serial.c.
And for os_compat.h, daemon() is simply disabled under Windows as it's not
used within libgps.
TESTED:
Confirmed compiles under a cross compiler.
Manual build and run of test_libgps on Windows which successfully connects
to a host running GPSD
Manual build DLL version and link with a Windows version of a program
(Viking) that then successfully connects and monitors positions from GPSD
Otherwise no effect on current supported systems.
'scons build-all check' - passes.
Signed-off-by: Fred Wright <fw@fwright.net>
Diffstat (limited to 'netlib.c')
-rw-r--r-- | netlib.c | 40 |
1 files changed, 35 insertions, 5 deletions
@@ -3,21 +3,38 @@ * BSD terms apply: see the file COPYING in the distribution root for details. */ +#include "gpsd_config.h" #include <string.h> #include <fcntl.h> +#ifdef HAVE_NETDB_H #include <netdb.h> +#endif /* HAVE_NETDB_H */ #ifndef AF_UNSPEC #include <sys/types.h> #include <sys/stat.h> +#ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> +#endif /* HAVE_SYS_SOCKET_H */ #endif /* AF_UNSPEC */ +#ifdef HAVE_SYS_UN_H #include <sys/un.h> +#endif /* HAVE_SYS_UN_H */ #ifndef INADDR_ANY +#ifdef HAVE_NETINET_IN_H #include <netinet/in.h> +#endif /* HAVE_NETINET_IN_H */ #endif /* INADDR_ANY */ +#ifdef HAVE_ARPA_INET_H #include <arpa/inet.h> /* for htons() and friends */ +#endif /* HAVE_ARPA_INET_H */ #include <unistd.h> +#ifdef HAVE_NETINET_IN_H #include <netinet/ip.h> +#endif /* HAVE_NETINET_IN_H */ +#ifdef HAVE_WINSOCK2_H +#include <winsock2.h> +#include <ws2tcpip.h> +#endif #include "gpsd.h" #include "sockaddr.h" @@ -97,7 +114,11 @@ socket_t netlib_connectsock(int af, const char *host, const char *service, } if (!BAD_SOCKET(s)) { - (void)close(s); +#ifdef HAVE_WINSOCK2_H + (void)closesocket(s); +#else + (void)close(s); +#endif } } freeaddrinfo(result); @@ -126,8 +147,12 @@ socket_t netlib_connectsock(int af, const char *host, const char *service, #endif /* set socket to noblocking */ +#ifdef HAVE_FCNTL (void)fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK); - +#elif defined(HAVE_WINSOCK2_H) + u_long one1 = 1; + (void)ioctlsocket(s, FIONBIO, &one1); +#endif return s; } @@ -155,6 +180,7 @@ const char *netlib_errstr(const int err) socket_t netlib_localsocket(const char *sockfile, int socktype) /* acquire a connection to an existing Unix-domain socket */ { +#ifdef HAVE_SYS_UN_H int sock; if ((sock = socket(AF_UNIX, socktype, 0)) < 0) { @@ -175,16 +201,19 @@ socket_t netlib_localsocket(const char *sockfile, int socktype) return sock; } +#else + return -1; +#endif /* HAVE_SYS_UN_H */ } char *netlib_sock2ip(socket_t fd) /* retrieve the IP address corresponding to a socket */ { + static char ip[INET6_ADDRSTRLEN]; + int r = 1; +#ifdef HAVE_INET_NTOP sockaddr_t fsin; socklen_t alen = (socklen_t) sizeof(fsin); - static char ip[INET6_ADDRSTRLEN]; - int r; - r = getpeername(fd, &(fsin.sa), &alen); if (r == 0) { switch (fsin.sa.sa_family) { @@ -203,6 +232,7 @@ char *netlib_sock2ip(socket_t fd) return ip; } } +#endif /* HAVE_INET_NTOP */ if (r != 0) { (void)strlcpy(ip, "<unknown>", sizeof(ip)); } |