diff options
| author | 2024-08-05 06:01:21 +0200 | |
|---|---|---|
| committer | 2024-08-05 06:01:21 +0200 | |
| commit | 972c3dc5d43a114ae59386d4edfdfb8ce0f3793e (patch) | |
| tree | 2568b6190dc0ca34478ccabc1191e504d1e8cfcf /xs_unix_socket.h | |
| parent | Minor logging tweaks. (diff) | |
| download | snac2-972c3dc5d43a114ae59386d4edfdfb8ce0f3793e.tar.gz snac2-972c3dc5d43a114ae59386d4edfdfb8ce0f3793e.tar.xz snac2-972c3dc5d43a114ae59386d4edfdfb8ce0f3793e.zip | |
Added support for listening on unix sockets.
Diffstat (limited to 'xs_unix_socket.h')
| -rw-r--r-- | xs_unix_socket.h | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/xs_unix_socket.h b/xs_unix_socket.h new file mode 100644 index 0000000..5b64282 --- /dev/null +++ b/xs_unix_socket.h | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_UNIX_SOCKET_H | ||
| 4 | |||
| 5 | #define _XS_UNIX_SOCKET_H | ||
| 6 | |||
| 7 | int xs_unix_socket_server(const char *path, const char *grp); | ||
| 8 | int xs_unix_socket_connect(const char *path); | ||
| 9 | |||
| 10 | |||
| 11 | #ifdef XS_IMPLEMENTATION | ||
| 12 | |||
| 13 | #include <sys/stat.h> | ||
| 14 | #include <sys/un.h> | ||
| 15 | #include <grp.h> | ||
| 16 | |||
| 17 | int xs_unix_socket_server(const char *path, const char *grp) | ||
| 18 | /* opens a unix-type server socket */ | ||
| 19 | { | ||
| 20 | int rs = -1; | ||
| 21 | |||
| 22 | if ((rs = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) { | ||
| 23 | struct sockaddr_un sun = {0}; | ||
| 24 | mode_t mode = 0666; | ||
| 25 | |||
| 26 | sun.sun_family = AF_UNIX; | ||
| 27 | strncpy(sun.sun_path, path, sizeof(sun.sun_path)); | ||
| 28 | |||
| 29 | unlink(path); | ||
| 30 | |||
| 31 | if (bind(rs, (struct sockaddr *)&sun, sizeof(sun)) == -1) { | ||
| 32 | close(rs); | ||
| 33 | return -1; | ||
| 34 | } | ||
| 35 | |||
| 36 | listen(rs, SOMAXCONN); | ||
| 37 | |||
| 38 | if (grp != NULL) { | ||
| 39 | struct group *g = NULL; | ||
| 40 | |||
| 41 | /* if there is a group name, get its gid_t */ | ||
| 42 | g = getgrnam(grp); | ||
| 43 | |||
| 44 | if (g != NULL && chown(path, -1, g->gr_gid) != -1) | ||
| 45 | mode = 0660; | ||
| 46 | } | ||
| 47 | |||
| 48 | chmod(path, mode); | ||
| 49 | } | ||
| 50 | |||
| 51 | return rs; | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | int xs_unix_socket_connect(const char *path) | ||
| 56 | /* connects to a unix-type socket */ | ||
| 57 | { | ||
| 58 | int d = -1; | ||
| 59 | |||
| 60 | if ((d = socket(AF_UNIX, SOCK_STREAM, 0)) != -1) { | ||
| 61 | struct sockaddr_un sun = {0}; | ||
| 62 | |||
| 63 | sun.sun_family = AF_UNIX; | ||
| 64 | strncpy(sun.sun_path, path, sizeof(sun.sun_path)); | ||
| 65 | |||
| 66 | if (connect(d, (struct sockaddr *)&sun, sizeof(sun)) == -1) { | ||
| 67 | close(d); | ||
| 68 | d = -1; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return d; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | #endif /* XS_IMPLEMENTATION */ | ||
| 77 | |||
| 78 | #endif /* _XS_UNIX_SOCKET_H */ | ||