aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Veresov <aleksey@veresov.pro>2020-03-24 23:54:26 +0300
committerAleksey Veresov <aleksey@veresov.pro>2020-03-24 23:54:26 +0300
commitcb6a86b8db5a26b92073c4fc5ceffffa8cd842c2 (patch)
treeec859f31db564609556c28c6473032977f493ad2
parent3430404db3a1d2977150460a79e67beb274ce667 (diff)
downloadmagi-cb6a86b8db5a26b92073c4fc5ceffffa8cd842c2.tar
magi-cb6a86b8db5a26b92073c4fc5ceffffa8cd842c2.tar.xz
magi-cb6a86b8db5a26b92073c4fc5ceffffa8cd842c2.zip
[magi]
-rw-r--r--src/session.c26
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);
}