From 85567b97021711b373d2990faccc59b1e95b6146 Mon Sep 17 00:00:00 2001 From: Hannes Reinecke Date: Fri, 3 Dec 2010 12:25:16 +0100 Subject: [PATCH 2/3] Use socket string length to bind socket When using abstract sockets the length of the socket name is part of the namespace. So to avoid confusion with trailing zeros we should be using the string length when binding the socket instead of the full structure size. Signed-off-by: Hannes Reinecke --- clif.c | 9 +++++---- ctrl_iface.c | 4 +++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/clif.c b/clif.c index 2ff0c22..a333746 100644 --- a/clif.c +++ b/clif.c @@ -46,6 +46,7 @@ struct clif *clif_open() { struct clif *clif; + socklen_t addrlen; clif = os_malloc(sizeof(*clif)); if (clif == NULL) @@ -63,8 +64,8 @@ struct clif *clif_open() clif->local.sun_path[0] = '\0'; os_snprintf(&clif->local.sun_path[1], sizeof(clif->local.sun_path) - 1, "%s/%d", LLDP_CLIF_SOCK, getpid()); - if (bind(clif->s, (struct sockaddr *) &clif->local, - sizeof(clif->local)) < 0) { + addrlen = sizeof(sa_family_t) + strlen(clif->local.sun_path + 1) + 1; + if (bind(clif->s, (struct sockaddr *) &clif->local, addrlen) < 0) { close(clif->s); os_free(clif); return NULL; @@ -74,9 +75,9 @@ struct clif *clif_open() clif->dest.sun_path[0] = '\0'; os_snprintf(&clif->dest.sun_path[1], sizeof(clif->dest.sun_path) - 1, "%s", LLDP_CLIF_SOCK); + addrlen = sizeof(sa_family_t) + strlen(clif->dest.sun_path + 1) + 1; - if (connect(clif->s, (struct sockaddr *) &clif->dest, - sizeof(clif->dest)) < 0) { + if (connect(clif->s, (struct sockaddr *) &clif->dest, addrlen) < 0) { close(clif->s); os_free(clif); return NULL; diff --git a/ctrl_iface.c b/ctrl_iface.c index d88979e..63977be 100644 --- a/ctrl_iface.c +++ b/ctrl_iface.c @@ -394,6 +394,7 @@ int ctrl_iface_init(struct clif_data *clifd) { struct sockaddr_un addr; int s = -1; + socklen_t addrlen; clifd->ctrl_sock = -1; clifd->ctrl_dst = NULL; @@ -408,7 +409,8 @@ int ctrl_iface_init(struct clif_data *clifd) addr.sun_family = AF_LOCAL; snprintf(&addr.sun_path[1], sizeof(addr.sun_path) - 1, "%s", LLDP_CLIF_SOCK); - if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + addrlen = sizeof(sa_family_t) + strlen(addr.sun_path + 1) + 1; + if (bind(s, (struct sockaddr *) &addr, addrlen) < 0) { perror("bind(AF_LOCAL)"); goto fail; } -- 1.7.1