Use compound literals and explicit initialization

I didn't really like the use of a "yes"-variable for setsockopt().
A better way is to use compound literals (part of C99).

Another point are the structs. Instead of memsetting to zero we make
use of the standard which guarantees that "unmentioned" fields
are set to zero anyways. Just to note it here: The use of memset()
also sets padding to zero, which is not guaranteed with the method
of "unmentioned" fields.

Signed-off-by: Laslo Hunhold <dev@frign.de>
This commit is contained in:
Laslo Hunhold 2019-05-30 23:15:47 +02:00
parent 33def953e9
commit 32223c96bd
No known key found for this signature in database
GPG key ID: 69576BD24CFCB980
2 changed files with 16 additions and 18 deletions

7
main.c
View file

@ -78,12 +78,11 @@ sigcleanup(int sig)
static void static void
handlesignals(void(*hdl)(int)) handlesignals(void(*hdl)(int))
{ {
struct sigaction sa; struct sigaction sa = {
.sa_handler = hdl,
};
memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask); sigemptyset(&sa.sa_mask);
sa.sa_handler = hdl;
sigaction(SIGTERM, &sa, NULL); sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL); sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL); sigaction(SIGINT, &sa, NULL);

27
sock.c
View file

@ -18,25 +18,25 @@
int int
sock_get_ips(const char *host, const char* port) sock_get_ips(const char *host, const char* port)
{ {
struct addrinfo hints, *ai, *p; struct addrinfo hints = {
int ret, insock = 0, yes; .ai_flags = AI_NUMERICSERV,
.ai_family = AF_UNSPEC,
memset(&hints, 0, sizeof(hints)); .ai_socktype = SOCK_STREAM,
hints.ai_flags = AI_NUMERICSERV; };
hints.ai_family = AF_UNSPEC; struct addrinfo *ai, *p;
hints.ai_socktype = SOCK_STREAM; int ret, insock = 0;
if ((ret = getaddrinfo(host, port, &hints, &ai))) { if ((ret = getaddrinfo(host, port, &hints, &ai))) {
die("getaddrinfo: %s", gai_strerror(ret)); die("getaddrinfo: %s", gai_strerror(ret));
} }
for (yes = 1, p = ai; p; p = p->ai_next) { for (p = ai; p; p = p->ai_next) {
if ((insock = socket(p->ai_family, p->ai_socktype, if ((insock = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) < 0) { p->ai_protocol)) < 0) {
continue; continue;
} }
if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR, &yes, if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR,
sizeof(int)) < 0) { &(int){1}, sizeof(int)) < 0) {
die("setsockopt:"); die("setsockopt:");
} }
if (bind(insock, p->ai_addr, p->ai_addrlen) < 0) { if (bind(insock, p->ai_addr, p->ai_addrlen) < 0) {
@ -70,7 +70,9 @@ sock_rem_uds(const char *udsname)
int int
sock_get_uds(const char *udsname, uid_t uid, gid_t gid) sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
{ {
struct sockaddr_un addr; struct sockaddr_un addr = {
.sun_family = AF_UNIX,
};
size_t udsnamelen; size_t udsnamelen;
int insock, sockmode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; int insock, sockmode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
@ -78,9 +80,6 @@ sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
die("socket:"); die("socket:");
} }
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
if ((udsnamelen = strlen(udsname)) > sizeof(addr.sun_path) - 1) { if ((udsnamelen = strlen(udsname)) > sizeof(addr.sun_path) - 1) {
die("UNIX-domain socket name truncated"); die("UNIX-domain socket name truncated");
} }