X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fsocket-proxy%2Fsocket-proxyd.c;h=56e660de57cb6b50cddd7b2d4c4a64ce22309c55;hb=e633ea1c9c5249ed5bf708a2ed6385c4823d4706;hp=12d58d4406d1ea81ad2f160a103c08c76f9b7d8e;hpb=b7484e2a58038c57591457c1439505607bdcd833;p=elogind.git diff --git a/src/socket-proxy/socket-proxyd.c b/src/socket-proxy/socket-proxyd.c index 12d58d440..56e660de5 100644 --- a/src/socket-proxy/socket-proxyd.c +++ b/src/socket-proxy/socket-proxyd.c @@ -53,6 +53,8 @@ typedef struct Context { } Context; typedef struct Connection { + Context *context; + int server_fd, client_fd; int server_to_client_buffer[2]; /* a pipe */ int client_to_server_buffer[2]; /* a pipe */ @@ -63,19 +65,14 @@ typedef struct Connection { sd_event_source *server_event_source, *client_event_source; } Connection; -union sockaddr_any { - struct sockaddr sa; - struct sockaddr_un un; - struct sockaddr_in in; - struct sockaddr_in6 in6; - struct sockaddr_storage storage; -}; - static const char *arg_remote_host = NULL; static void connection_free(Connection *c) { assert(c); + if (c->context) + set_remove(c->context->connections, c); + sd_event_source_unref(c->server_event_source); sd_event_source_unref(c->client_event_source); @@ -99,14 +96,14 @@ static void context_free(Context *context) { while ((es = set_steal_first(context->listen))) sd_event_source_unref(es); - while ((c = set_steal_first(context->connections))) + while ((c = set_first(context->connections))) connection_free(c); set_free(context->listen); set_free(context->connections); } -static int get_remote_sockaddr(union sockaddr_any *sa, socklen_t *salen) { +static int get_remote_sockaddr(union sockaddr_union *sa, socklen_t *salen) { int r; assert(sa); @@ -117,7 +114,7 @@ static int get_remote_sockaddr(union sockaddr_any *sa, socklen_t *salen) { strncpy(sa->un.sun_path, arg_remote_host, sizeof(sa->un.sun_path)-1); sa->un.sun_path[sizeof(sa->un.sun_path)-1] = 0; - *salen = offsetof(union sockaddr_any, un.sun_path) + strlen(sa->un.sun_path); + *salen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa->un.sun_path); } else if (arg_remote_host[0] == '@') { sa->un.sun_family = AF_UNIX; @@ -125,7 +122,7 @@ static int get_remote_sockaddr(union sockaddr_any *sa, socklen_t *salen) { strncpy(sa->un.sun_path+1, arg_remote_host+1, sizeof(sa->un.sun_path)-2); sa->un.sun_path[sizeof(sa->un.sun_path)-1] = 0; - *salen = offsetof(union sockaddr_any, un.sun_path) + 1 + strlen(sa->un.sun_path + 1); + *salen = offsetof(union sockaddr_union, un.sun_path) + 1 + strlen(sa->un.sun_path + 1); } else { _cleanup_freeaddrinfo_ struct addrinfo *result = NULL; @@ -154,7 +151,7 @@ static int get_remote_sockaddr(union sockaddr_any *sa, socklen_t *salen) { } assert(result); - if (result->ai_addrlen > sizeof(union sockaddr_any)) { + if (result->ai_addrlen > sizeof(union sockaddr_union)) { log_error("Address too long."); return -E2BIG; } @@ -388,7 +385,7 @@ fail: } static int add_connection_socket(Context *context, sd_event *event, int fd) { - union sockaddr_any sa = {}; + union sockaddr_union sa = {}; socklen_t salen; Connection *c; int r; @@ -411,11 +408,18 @@ static int add_connection_socket(Context *context, sd_event *event, int fd) { if (!c) return log_oom(); + c->context = context; c->server_fd = fd; c->client_fd = -1; c->server_to_client_buffer[0] = c->server_to_client_buffer[1] = -1; c->client_to_server_buffer[0] = c->client_to_server_buffer[1] = -1; + r = set_put(context->connections, c); + if (r < 0) { + free(c); + return log_oom(); + } + r = get_remote_sockaddr(&sa, &salen); if (r < 0) goto fail; @@ -499,8 +503,6 @@ static int add_listen_socket(Context *context, sd_event *event, int fd) { assert(event); assert(fd >= 0); - log_info("Listening on %i", fd); - r = set_ensure_allocated(&context->listen, trivial_hash_func, trivial_compare_func); if (r < 0) { log_oom();