Skip to content

Commit

Permalink
rxi#25 Support Unix domain socket
Browse files Browse the repository at this point in the history
- Add dyad_unix_connect

Signed-off-by: Uilian Ries <uilianries@gmail.com>
  • Loading branch information
uilianries committed Apr 22, 2018
1 parent 915ae49 commit 03fe293
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/dyad.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
Expand All @@ -35,6 +36,7 @@
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <stddef.h>

#include "dyad.h"

Expand Down Expand Up @@ -1035,6 +1037,30 @@ int dyad_connect(dyad_Stream *stream, const char *host, int port) {
return -1;
}

int dyad_unix_connect(dyad_Stream *stream, const char *path) {
struct sockaddr_un hints;
size_t size;
int err;

/* Resolve host */
memset(&hints, 0, sizeof(hints));
hints.sun_family = AF_UNIX;
strncpy(hints.sun_path, path, (sizeof(hints.sun_path) - 1));
if (strcmp(hints.sun_path, path) != 0) {
stream_error(stream, "file path too long", 0);
goto fail;
}

/* Start connecting */
err = stream_initSocket(stream, hints.sun_family, SOCK_STREAM, 0);
if (err) goto fail;
size = (offsetof(struct sockaddr_un, sun_path) + strlen(hints.sun_path));
connect(stream->sockfd, (struct sockaddr *) &hints, size);
stream->state = DYAD_STATE_CONNECTING;
return 0;
fail:
return -1;
}

void dyad_write(dyad_Stream *stream, const void *data, int size) {
const char *p = data;
Expand Down
1 change: 1 addition & 0 deletions src/dyad.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ int dyad_listen(dyad_Stream *stream, int port);
int dyad_listenEx(dyad_Stream *stream, const char *host, int port,
int backlog);
int dyad_connect(dyad_Stream *stream, const char *host, int port);
int dyad_unix_connect(dyad_Stream *stream, const char *path);
void dyad_addListener(dyad_Stream *stream, int event,
dyad_Callback callback, void *udata);
void dyad_removeListener(dyad_Stream *stream, int event,
Expand Down

0 comments on commit 03fe293

Please sign in to comment.