summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-12-08 17:48:34 +0100
committerLennart Poettering <lennart@poettering.net>2022-12-08 17:48:34 +0100
commitb6256af75e0609e451198ed90c293efd50827ab3 (patch)
tree4811aaf4efc23d38d4f8f569404a26b753b27237
parent86bdf117148388a39f5d0c24e5259314f3e29fb5 (diff)
downloadsystemd-b6256af75e0609e451198ed90c293efd50827ab3.tar.gz
socket-util: add CMSG_TYPED_DATA() as type-safe wrapper around CMSG_DATA()
-rw-r--r--src/basic/socket-util.h14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/basic/socket-util.h b/src/basic/socket-util.h
index 2e36e1a56b..0b8d53e895 100644
--- a/src/basic/socket-util.h
+++ b/src/basic/socket-util.h
@@ -175,15 +175,17 @@ int flush_accept(int fd);
#define CMSG_FOREACH(cmsg, mh) \
for ((cmsg) = CMSG_FIRSTHDR(mh); (cmsg); (cmsg) = CMSG_NXTHDR((mh), (cmsg)))
+#define CMSG_TYPED_DATA(cmsg, type) \
+ ({ \
+ struct cmsghdr *_cmsg = cmsg; \
+ _cmsg ? CAST_ALIGN_PTR(type, CMSG_DATA(_cmsg)) : (type*) NULL; \
+ })
+
struct cmsghdr* cmsg_find(struct msghdr *mh, int level, int type, socklen_t length);
/* Type-safe, dereferencing version of cmsg_find() */
-#define CMSG_FIND_DATA(mh, level, type, ctype) \
- ({ \
- struct cmsghdr *_found; \
- _found = cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))); \
- (ctype*) (_found ? CMSG_DATA(_found) : NULL); \
- })
+#define CMSG_FIND_DATA(mh, level, type, ctype) \
+ CMSG_TYPED_DATA(cmsg_find(mh, level, type, CMSG_LEN(sizeof(ctype))), ctype)
/* Resolves to a type that can carry cmsghdr structures. Make sure things are properly aligned, i.e. the type
* itself is placed properly in memory and the size is also aligned to what's appropriate for "cmsghdr"