summaryrefslogtreecommitdiff
path: root/netlib.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2011-06-26 17:21:57 -0400
committerEric S. Raymond <esr@thyrsus.com>2011-06-26 17:21:57 -0400
commit60d6e3a4a207260c6780055a3b908832cf1a8fab (patch)
tree142d66c857ca22556f6a387b9f70955e759d914d /netlib.c
parent5dc0b165f1cc8679168abd63c116082c68821a29 (diff)
downloadgpsd-60d6e3a4a207260c6780055a3b908832cf1a8fab.tar.gz
Factor out a function we'll probably reuse.
Diffstat (limited to 'netlib.c')
-rw-r--r--netlib.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/netlib.c b/netlib.c
index e26b3e03..bf4a4656 100644
--- a/netlib.c
+++ b/netlib.c
@@ -11,6 +11,7 @@
#include <sys/stat.h>
#include <sys/socket.h>
#endif /* AF_UNSPEC */
+#include <sys/un.h>
#ifndef INADDR_ANY
#include <netinet/in.h>
#endif /* INADDR_ANY */
@@ -146,6 +147,32 @@ char /*@observer@*/ *netlib_errstr(const int err)
}
}
+socket_t netlib_localsocket(const char *sockfile)
+/* acquire a connection to an existing Unix-domain socket */
+{
+ int sock;
+
+ if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
+ return -1;
+ } else {
+ struct sockaddr_un saddr;
+
+ memset(&saddr, 0, sizeof(struct sockaddr_un));
+ saddr.sun_family = AF_UNIX;
+ (void)strlcpy(saddr.sun_path,
+ sockfile,
+ sizeof(saddr.sun_path));
+
+ /*@-unrecog@*/
+ if (connect(sock, (struct sockaddr *)&saddr, SUN_LEN(&saddr)) < 0) {
+ return -1;
+ }
+ /*@+unrecog@*/
+
+ return sock;
+ }
+}
+
char *netlib_sock2ip(int fd)
/* retrieve the IP address corresponding to a socket */
{