summaryrefslogtreecommitdiff
path: root/netlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'netlib.c')
-rw-r--r--netlib.c40
1 files changed, 35 insertions, 5 deletions
diff --git a/netlib.c b/netlib.c
index c8969382..2939454d 100644
--- a/netlib.c
+++ b/netlib.c
@@ -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));
}