Skip to content

Commit

Permalink
common: Get rid of offsetof() and just use sizeof() to get size of un…
Browse files Browse the repository at this point in the history
…ix socket address
  • Loading branch information
nikias committed Jul 24, 2018
1 parent 149da9b commit 78df9be
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions common/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ int socket_create_unix(const char *filename)
{
struct sockaddr_un name;
int sock;
size_t size;
#ifdef SO_NOSIGPIPE
int yes = 1;
#endif
Expand All @@ -64,7 +63,7 @@ int socket_create_unix(const char *filename)
unlink(filename);

/* Create the socket. */
sock = socket(PF_LOCAL, SOCK_STREAM, 0);
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
return -1;
Expand All @@ -79,21 +78,11 @@ int socket_create_unix(const char *filename)
#endif

/* Bind a name to the socket. */
name.sun_family = AF_LOCAL;
name.sun_family = AF_UNIX;
strncpy(name.sun_path, filename, sizeof(name.sun_path));
name.sun_path[sizeof(name.sun_path) - 1] = '\0';

/* The size of the address is
the offset of the start of the filename,
plus its length,
plus one for the terminating null byte.
Alternatively you can just do:
size = SUN_LEN (&name);
*/
size = (offsetof(struct sockaddr_un, sun_path)
+ strlen(name.sun_path) + 1);

if (bind(sock, (struct sockaddr *) &name, size) < 0) {
if (bind(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
perror("bind");
socket_close(sock);
return -1;
Expand All @@ -112,7 +101,6 @@ int socket_connect_unix(const char *filename)
{
struct sockaddr_un name;
int sfd = -1;
size_t size;
struct stat fst;
#ifdef SO_NOSIGPIPE
int yes = 1;
Expand All @@ -134,7 +122,7 @@ int socket_connect_unix(const char *filename)
return -1;
}
// make a new socket
if ((sfd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
if ((sfd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
if (verbose >= 2)
fprintf(stderr, "%s: socket: %s\n", __func__, strerror(errno));
return -1;
Expand All @@ -157,14 +145,11 @@ int socket_connect_unix(const char *filename)
#endif

// and connect to 'filename'
name.sun_family = AF_LOCAL;
name.sun_family = AF_UNIX;
strncpy(name.sun_path, filename, sizeof(name.sun_path));
name.sun_path[sizeof(name.sun_path) - 1] = 0;

size = (offsetof(struct sockaddr_un, sun_path)
+ strlen(name.sun_path) + 1);

if (connect(sfd, (struct sockaddr *) &name, size) < 0) {
if (connect(sfd, (struct sockaddr*)&name, sizeof(name)) < 0) {
socket_close(sfd);
if (verbose >= 2)
fprintf(stderr, "%s: connect: %s\n", __func__,
Expand Down

0 comments on commit 78df9be

Please sign in to comment.