mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 16:45:07 -05:00
socket: rework setting of struct sockaddr_un
Add a function to fill the Unix sockaddr for binding, connecting and sending to avoid code duplication. Use memcpy() instead of snprintf() and provide the minimum length of the address containing the terminating null byte. This makes it easier to support abstract sockets if needed in future (e.g. for systemd support).
This commit is contained in:
52
socket.c
52
socket.c
@@ -590,25 +590,40 @@ error:
|
|||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
|
|
||||||
|
static socklen_t
|
||||||
|
set_unix_sockaddr(struct sockaddr_un *sun, const char *addr)
|
||||||
|
{
|
||||||
|
size_t len = strlen(addr);
|
||||||
|
|
||||||
|
if (len + 1 > sizeof (sun->sun_path)) {
|
||||||
|
DEBUG_LOG("Unix socket path %s too long", addr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(sun, 0, sizeof (*sun));
|
||||||
|
sun->sun_family = AF_UNIX;
|
||||||
|
memcpy(sun->sun_path, addr, len);
|
||||||
|
|
||||||
|
return offsetof(struct sockaddr_un, sun_path) + len + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ================================================== */
|
||||||
|
|
||||||
static int
|
static int
|
||||||
bind_unix_address(int sock_fd, const char *addr, int flags)
|
bind_unix_address(int sock_fd, const char *addr, int flags)
|
||||||
{
|
{
|
||||||
union sockaddr_all saddr;
|
union sockaddr_all saddr;
|
||||||
|
socklen_t saddr_len;
|
||||||
|
|
||||||
memset(&saddr, 0, sizeof (saddr));
|
saddr_len = set_unix_sockaddr(&saddr.un, addr);
|
||||||
|
if (saddr_len == 0)
|
||||||
if (snprintf(saddr.un.sun_path, sizeof (saddr.un.sun_path), "%s", addr) >=
|
|
||||||
sizeof (saddr.un.sun_path)) {
|
|
||||||
DEBUG_LOG("Unix socket path %s too long", addr);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
saddr.un.sun_family = AF_UNIX;
|
|
||||||
|
|
||||||
if (unlink(addr) < 0)
|
if (unlink(addr) < 0)
|
||||||
DEBUG_LOG("Could not remove %s : %s", addr, strerror(errno));
|
DEBUG_LOG("Could not remove %s : %s", addr, strerror(errno));
|
||||||
|
|
||||||
/* PRV_BindSocket() doesn't support Unix sockets yet */
|
/* PRV_BindSocket() doesn't support Unix sockets yet */
|
||||||
if (bind(sock_fd, &saddr.sa, sizeof (saddr.un)) < 0) {
|
if (bind(sock_fd, &saddr.sa, saddr_len) < 0) {
|
||||||
DEBUG_LOG("Could not bind Unix socket to %s : %s", addr, strerror(errno));
|
DEBUG_LOG("Could not bind Unix socket to %s : %s", addr, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -628,17 +643,13 @@ static int
|
|||||||
connect_unix_address(int sock_fd, const char *addr)
|
connect_unix_address(int sock_fd, const char *addr)
|
||||||
{
|
{
|
||||||
union sockaddr_all saddr;
|
union sockaddr_all saddr;
|
||||||
|
socklen_t saddr_len;
|
||||||
|
|
||||||
memset(&saddr, 0, sizeof (saddr));
|
saddr_len = set_unix_sockaddr(&saddr.un, addr);
|
||||||
|
if (saddr_len == 0)
|
||||||
if (snprintf(saddr.un.sun_path, sizeof (saddr.un.sun_path), "%s", addr) >=
|
|
||||||
sizeof (saddr.un.sun_path)) {
|
|
||||||
DEBUG_LOG("Unix socket path %s too long", addr);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
saddr.un.sun_family = AF_UNIX;
|
|
||||||
|
|
||||||
if (connect(sock_fd, &saddr.sa, sizeof (saddr.un)) < 0) {
|
if (connect(sock_fd, &saddr.sa, saddr_len) < 0) {
|
||||||
DEBUG_LOG("Could not connect Unix socket to %s : %s", addr, strerror(errno));
|
DEBUG_LOG("Could not connect Unix socket to %s : %s", addr, strerror(errno));
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1142,14 +1153,9 @@ send_message(int sock_fd, SCK_Message *message, int flags)
|
|||||||
(struct sockaddr *)&saddr, sizeof (saddr));
|
(struct sockaddr *)&saddr, sizeof (saddr));
|
||||||
break;
|
break;
|
||||||
case SCK_ADDR_UNIX:
|
case SCK_ADDR_UNIX:
|
||||||
memset(&saddr, 0, sizeof (saddr));
|
saddr_len = set_unix_sockaddr(&saddr.un, message->remote_addr.path);
|
||||||
if (snprintf(saddr.un.sun_path, sizeof (saddr.un.sun_path), "%s",
|
if (saddr_len == 0)
|
||||||
message->remote_addr.path) >= sizeof (saddr.un.sun_path)) {
|
|
||||||
DEBUG_LOG("Unix socket path %s too long", message->remote_addr.path);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
|
||||||
saddr.un.sun_family = AF_UNIX;
|
|
||||||
saddr_len = sizeof (saddr.un);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
assert(0);
|
assert(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user