diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/session.c | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/session.c b/src/session.c index 35e9200..aec0e6d 100644 --- a/src/session.c +++ b/src/session.c @@ -6,6 +6,8 @@ #include <sys/un.h> +enum { listen_backlog = 64 }; + void magi_session_init(magi_session *s) { s->socket = 0; @@ -23,18 +25,34 @@ void magi_session_free(magi_session *s) int magi_session_inet(magi_session *s, const char *address, int port) { struct sockaddr_in addr; - s->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s->socket) { + return 0; + } + s->socket = socket(AF_INET, SOCK_STREAM, 0); + if (s->socket == -1) { + s->socket = 0; + return 0; + } addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = inet_addr(address); - return !connect(s->socket, (struct sockaddr *)&addr, sizeof(addr)); + return !bind(s->socket, (struct sockaddr *)&addr, sizeof(addr)) && + !listen(s->socket, listen_backlog); } int magi_session_unix(magi_session *s, const char *path) { struct sockaddr_un addr; - s->socket = socket(AF_UNIX, SOCK_STREAM, IPPROTO_TCP); + if (s->socket) { + return 0; + } + s->socket = socket(AF_UNIX, SOCK_STREAM, 0); + if (s->socket == -1) { + s->socket = 0; + return 0; + } addr.sun_family = AF_UNIX; strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1); - return !connect(s->socket, (struct sockaddr *)&addr, sizeof(addr)); + return !bind(s->socket, (struct sockaddr *)&addr, sizeof(addr)) && + !listen(s->socket, listen_backlog); } |