From 2a7c40dad83d2a1a90c795ba3f58edf672d25fe3 Mon Sep 17 00:00:00 2001 From: jsorg71 Date: Sat, 5 Sep 2009 03:29:43 +0000 Subject: [PATCH] add unix socket to trans --- common/os_calls.c | 17 +++++++++++++ common/os_calls.h | 2 ++ common/trans.c | 61 +++++++++++++++++++++++++++++++++++++++-------- common/trans.h | 3 ++- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/common/os_calls.c b/common/os_calls.c index eff577c7..f530d002 100644 --- a/common/os_calls.c +++ b/common/os_calls.c @@ -386,6 +386,23 @@ g_tcp_connect(int sck, const char* address, const char* port) return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_in)); } +/*****************************************************************************/ +/* returns error, zero is good */ +int APP_CC +g_tcp_local_connect(int sck, const char* port) +{ +#if defined(_WIN32) + return -1; +#else + struct sockaddr_un s; + + memset(&s, 0, sizeof(struct sockaddr_un)); + s.sun_family = AF_UNIX; + strcpy(s.sun_path, port); + return connect(sck, (struct sockaddr*)&s, sizeof(struct sockaddr_un)); +#endif +} + /*****************************************************************************/ int APP_CC g_tcp_set_non_blocking(int sck) diff --git a/common/os_calls.h b/common/os_calls.h index 4c727f66..4296f172 100644 --- a/common/os_calls.h +++ b/common/os_calls.h @@ -64,6 +64,8 @@ g_tcp_close(int sck); int APP_CC g_tcp_connect(int sck, const char* address, const char* port); int APP_CC +g_tcp_local_connect(int sck, const char* port); +int APP_CC g_tcp_force_send(int sck, char* data, int len); int APP_CC g_tcp_force_recv(int sck, char* data, int len); diff --git a/common/trans.c b/common/trans.c index 47df3523..d83ea7f6 100644 --- a/common/trans.c +++ b/common/trans.c @@ -54,6 +54,11 @@ trans_delete(struct trans* self) free_stream(self->in_s); free_stream(self->out_s); g_tcp_close(self->sck); + if (self->listen_filename != 0) + { + g_file_delete(self->listen_filename); + g_free(self->listen_filename); + } g_free(self); } @@ -283,9 +288,23 @@ trans_connect(struct trans* self, const char* server, const char* port, { g_tcp_close(self->sck); } - self->sck = g_tcp_socket(); - g_tcp_set_non_blocking(self->sck); - error = g_tcp_connect(self->sck, server, port); + if (self->mode == 1) /* tcp */ + { + self->sck = g_tcp_socket(); + g_tcp_set_non_blocking(self->sck); + error = g_tcp_connect(self->sck, server, port); + } + else if (self->mode == 2) /* unix socket */ + { + self->sck = g_tcp_local_socket(); + g_tcp_set_non_blocking(self->sck); + error = g_tcp_local_connect(self->sck, port); + } + else + { + self->status = 0; + return 1; + } if (error == -1) { if (g_tcp_last_error_would_block(self->sck)) @@ -312,15 +331,37 @@ trans_listen(struct trans* self, char* port) { g_tcp_close(self->sck); } - self->sck = g_tcp_socket(); - g_tcp_set_non_blocking(self->sck); - if (g_tcp_bind(self->sck, port) == 0) + if (self->mode == 1) /* tcp */ { - if (g_tcp_listen(self->sck) == 0) + self->sck = g_tcp_socket(); + g_tcp_set_non_blocking(self->sck); + if (g_tcp_bind(self->sck, port) == 0) { - self->status = 1; /* ok */ - self->type1 = 1; /* listener */ - return 0; + if (g_tcp_listen(self->sck) == 0) + { + self->status = 1; /* ok */ + self->type1 = 1; /* listener */ + return 0; + } + } + } + else if (self->mode == 2) /* unix socket */ + { + g_free(self->listen_filename); + self->listen_filename = 0; + g_file_delete(port); + self->sck = g_tcp_local_socket(); + g_tcp_set_non_blocking(self->sck); + if (g_tcp_local_bind(self->sck, port) == 0) + { + self->listen_filename = g_strdup(port); + if (g_tcp_listen(self->sck) == 0) + { + g_chmod_hex(port, 0xffff); + self->status = 1; /* ok */ + self->type1 = 1; /* listener */ + return 0; + } } } return 1; diff --git a/common/trans.h b/common/trans.h index 4df515da..8abb8afd 100644 --- a/common/trans.h +++ b/common/trans.h @@ -37,7 +37,7 @@ typedef int (*ttrans_conn_in)(struct trans* self, struct trans* new_self); struct trans { tbus sck; - int mode; /* tcp, shared memory, pipes */ + int mode; /* 1 tcp, 2 unix socket */ int status; int type1; /* 1 listener 2 server 3 client */ ttrans_data_in trans_data_in; @@ -46,6 +46,7 @@ struct trans int header_size; struct stream* in_s; struct stream* out_s; + char* listen_filename; }; struct trans* APP_CC