* Licensed under GPLv2, see file LICENSE in this tarball for details.
*/
+#include <sys/socket.h> /* netinet/in.h needs it */
#include <netinet/in.h>
#include <net/if.h>
+#include <sys/un.h>
#include "libbb.h"
void FAST_FUNC setsockopt_reuseaddr(int fd)
len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
{
- len_and_sockaddr *lsa;
- socklen_t len = 0;
+ len_and_sockaddr lsa;
+ len_and_sockaddr *lsa_ptr;
- /* Can be optimized to do only one getsockname() */
- if (getsockname(fd, NULL, &len) != 0)
+ lsa.len = LSA_SIZEOF_SA;
+ if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
return NULL;
- lsa = xzalloc(LSA_LEN_SIZE + len);
- lsa->len = len;
- getsockname(fd, &lsa->u.sa, &lsa->len);
- return lsa;
+
+ lsa_ptr = xzalloc(LSA_LEN_SIZE + lsa.len);
+ if (lsa.len > LSA_SIZEOF_SA) { /* rarely (if ever) happens */
+ lsa_ptr->len = lsa.len;
+ getsockname(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
+ } else {
+ memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
+ }
+ return lsa_ptr;
}
void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
close(s);
if (s_addr->sa_family == AF_INET)
bb_perror_msg_and_die("%s (%s)",
- "cannot connect to remote host",
+ "can't connect to remote host",
inet_ntoa(((struct sockaddr_in *)s_addr)->sin_addr));
- bb_perror_msg_and_die("cannot connect to remote host");
+ bb_perror_msg_and_die("can't connect to remote host");
}
}
int ai_flags)
{
int rc;
- len_and_sockaddr *r = NULL;
+ len_and_sockaddr *r;
struct addrinfo *result = NULL;
struct addrinfo *used_res;
const char *org_host = host; /* only for error msg */
const char *cp;
struct addrinfo hint;
+ if (ENABLE_FEATURE_UNIX_LOCAL && strncmp(host, "local:", 6) == 0) {
+ struct sockaddr_un *sun;
+
+ r = xzalloc(LSA_LEN_SIZE + sizeof(struct sockaddr_un));
+ r->len = sizeof(struct sockaddr_un);
+ r->u.sa.sa_family = AF_UNIX;
+ sun = (struct sockaddr_un *)&r->u.sa;
+ safe_strncpy(sun->sun_path, host + 6, sizeof(sun->sun_path));
+ return r;
+ }
+
+ r = NULL;
+
/* Ugly parsing of host:addr */
if (ENABLE_FEATURE_IPV6 && host[0] == '[') {
/* Even uglier parsing of [xx]:nn */
}
if (cp) { /* points to ":" or "]:" */
int sz = cp - host + 1;
+
host = safe_strncpy(alloca(sz), host, sz);
if (ENABLE_FEATURE_IPV6 && *cp != ':') {
cp++; /* skip ']' */
int rc;
socklen_t salen;
+ if (ENABLE_FEATURE_UNIX_LOCAL && sa->sa_family == AF_UNIX) {
+ struct sockaddr_un *sun = (struct sockaddr_un *)sa;
+ return xasprintf("local:%.*s",
+ (int) sizeof(sun->sun_path),
+ sun->sun_path);
+ }
+
salen = LSA_SIZEOF_SA;
#if ENABLE_FEATURE_IPV6
if (sa->sa_family == AF_INET)