bind() needs a length of the sockaddr structure you’ve given it.
Now, for unix sockets, sockaddr_un is used
What’s the proper ways of calculating the length of this when you’ve filled in the sun_path member ? I’ve seen multiple approaches:
socklen_t len = sizeof(sockaddr_un);
socklen_t len = offsetof(sockaddr_un,sun_path) + strlen(addr.sun_path);
socklen_t len = offsetof(sockaddr_un,sun_path) + strlen(addr.sun_path) + 1;
socklen_t len = sizeof(sockaddr.sun_family ) + strlen(addr.sun_path);
And even other approaches. Is it ok to just take the sizeof(sockaddr_un) – or what is the proper way ?
sizeof(struct sockaddr_un)is fine.Take a look at the manpage
unix(7). The fieldsun_pathis a character array that is part of the struct.