Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
Fix addrlen for unix_dgram sockets
Browse files Browse the repository at this point in the history
The old definition was off by one byte on BSD. Also simplify
ADDRESS_TO_JS because sun_path is always zero-terminated now.
  • Loading branch information
d0k committed Jul 26, 2010
1 parent 28db0c2 commit eeb54c6
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions src/node_net.cc
Expand Up @@ -203,12 +203,12 @@ static inline Handle<Value> ParseAddressArgs(Handle<Value> first,
return Exception::Error(String::New("Socket path too long"));
}

memset(&un, 0, sizeof un);
un.sun_family = AF_UNIX;
strncpy(un.sun_path, *path, ARRAY_SIZE(un.sun_path) - 1);
un.sun_path[ARRAY_SIZE(un.sun_path) - 1] = '\0';
memcpy(un.sun_path, *path, path.length());

addr = (struct sockaddr*)&un;
addrlen = path.length() + sizeof(un.sun_family) + 1;
addrlen = sizeof(un) - sizeof(un.sun_path) + path.length() + 1;

} else {
// TCP or UDP
Expand Down Expand Up @@ -350,13 +350,6 @@ static Handle<Value> Connect(const Arguments& args) {
return Undefined();
}

// Mac's SUN_LEN is broken
#if defined(__APPLE__)
# define SUN_LEN(ptr) ((ptr)->sun_len-2)
#elif !defined(SUN_LEN)
# define SUN_LEN(ptr) strlen((ptr)->sun_path)
#endif

#define ADDRESS_TO_JS(info, address_storage) \
do { \
char ip[INET6_ADDRSTRLEN]; \
Expand All @@ -381,15 +374,10 @@ do { \
break; \
case AF_UNIX: \
au = (struct sockaddr_un*)&(address_storage); \
char un_path[105]; \
size_t len; \
len = SUN_LEN(au); \
strncpy(un_path, au->sun_path, len); \
un_path[len] = 0; \
(info)->Set(address_symbol, String::New(un_path)); \
(info)->Set(address_symbol, String::New(au->sun_path)); \
break; \
default: \
(info)->Set(address_symbol, String::New("")); \
(info)->Set(address_symbol, String::Empty()); \
} \
} while (0)

Expand Down

0 comments on commit eeb54c6

Please sign in to comment.