Skip to content

Commit 43861e3

Browse files
committed
MEDIUM: sock_unix: use per-family addrcmp function
Thanks to previous commit, we may now use dedicated addrcmp functions for each UNIX address family. This allows to simplify sock_unix_addrcmp() function and avoid useless checks in order to try to guess the socket type. In this patch we implement sock_abns_addrcmp() and sock_abnsz_addrcmp() functions, which are respectively used for ABNS and ABNSZ custom families sock_unix_addrcmp() now only holds regular UNIX socket comparing logic.
1 parent d879bf6 commit 43861e3

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

include/haproxy/sock_unix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ extern struct proto_fam proto_fam_abns;
3333
extern struct proto_fam proto_fam_abnsz;
3434

3535
int sock_unix_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
36+
int sock_abns_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
37+
int sock_abnsz_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b);
3638
int sock_unix_bind_receiver(struct receiver *rx, char **errmsg);
3739

3840
#endif /* _HAPROXY_SOCK_UNIX_H */

src/sock_unix.c

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct proto_fam proto_fam_abns = {
5757
.real_family = AF_UNIX,
5858
.sock_addrlen = sizeof(struct sockaddr_un),
5959
.l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
60-
.addrcmp = sock_unix_addrcmp,
60+
.addrcmp = sock_abns_addrcmp,
6161
.bind = sock_unix_bind_receiver,
6262
.get_src = sock_get_src,
6363
.get_dst = sock_get_dst,
@@ -70,7 +70,7 @@ struct proto_fam proto_fam_abnsz = {
7070
.real_family = AF_UNIX,
7171
.sock_addrlen = sizeof(struct sockaddr_un),
7272
.l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path),
73-
.addrcmp = sock_unix_addrcmp,
73+
.addrcmp = sock_abnsz_addrcmp,
7474
.bind = sock_unix_bind_receiver,
7575
.get_src = sock_get_src,
7676
.get_dst = sock_get_dst,
@@ -85,32 +85,72 @@ struct proto_fam proto_fam_abnsz = {
8585
*/
8686

8787

88-
/* Compares two AF_UNIX sockaddr addresses. Returns 0 if they match or non-zero
89-
* if they do not match. It also supports ABNS socket addresses (those starting
90-
* with \0). For regular UNIX sockets however, this does explicitly support
91-
* matching names ending exactly with .XXXXX.tmp which are newly bound sockets
92-
* about to be replaced; this suffix is then ignored. Note that our UNIX socket
93-
* paths are always zero-terminated.
88+
/* Compares two AF_CUST_ABNS sockaddr addresses (ABNS UNIX sockets). Returns 0 if
89+
* they match or non-zero.
9490
*/
95-
int sock_unix_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
91+
int sock_abns_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
9692
{
9793
const struct sockaddr_un *au = (const struct sockaddr_un *)a;
9894
const struct sockaddr_un *bu = (const struct sockaddr_un *)b;
99-
int idx, dot, idx2;
10095

101-
if (real_family(a->ss_family) != real_family(b->ss_family))
96+
if (a->ss_family != b->ss_family)
10297
return -1;
10398

104-
if (real_family(a->ss_family) != AF_UNIX)
99+
if (a->ss_family != AF_CUST_ABNS)
105100
return -1;
106101

107102
if (au->sun_path[0] != bu->sun_path[0])
108103
return -1;
109104

110-
if (au->sun_path[0] == 0)
111-
return memcmp(au->sun_path, bu->sun_path, sizeof(au->sun_path));
105+
if (au->sun_path[0] != '\0')
106+
return -1;
107+
108+
return memcmp(au->sun_path, bu->sun_path, sizeof(au->sun_path));
109+
}
110+
111+
112+
/* Compares two AF_CUST_ABNSZ sockaddr addresses (ABNSZ UNIX sockets). Returns 0 if
113+
* they match or non-zero.
114+
*/
115+
int sock_abnsz_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
116+
{
117+
const struct sockaddr_un *au = (const struct sockaddr_un *)a;
118+
const struct sockaddr_un *bu = (const struct sockaddr_un *)b;
119+
120+
if (a->ss_family != b->ss_family)
121+
return -1;
122+
123+
if (a->ss_family != AF_CUST_ABNSZ)
124+
return -1;
125+
126+
if (au->sun_path[0] != bu->sun_path[0])
127+
return -1;
128+
129+
if (au->sun_path[0] != '\0')
130+
return -1;
131+
132+
return strncmp(au->sun_path + 1, bu->sun_path + 1, sizeof(au->sun_path) - 1);
133+
}
134+
135+
/* Compares two AF_UNIX sockaddr addresses (regular UNIX sockets). Returns 0 if
136+
* they match or non-zero. Tis does explicitly support matching names ending
137+
* exactly with .XXXXX.tmp which are newly bound sockets about to be replaced;
138+
* this suffix is then ignored. Note that our UNIX socket paths are always
139+
* zero-terminated.
140+
*/
141+
int sock_unix_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b)
142+
{
143+
const struct sockaddr_un *au = (const struct sockaddr_un *)a;
144+
const struct sockaddr_un *bu = (const struct sockaddr_un *)b;
145+
int idx, dot, idx2;
146+
147+
if (a->ss_family != b->ss_family)
148+
return -1;
149+
150+
if (a->ss_family != AF_UNIX)
151+
return -1;
112152

113-
idx = 1; dot = 0;
153+
idx = 0; dot = 0;
114154
while (au->sun_path[idx] == bu->sun_path[idx]) {
115155
if (au->sun_path[idx] == 0)
116156
return 0;

0 commit comments

Comments
 (0)