*
* Connect to host at port using address resolution from getaddrinfo
*
- * Licensed under GPLv2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
*/
+#include <sys/types.h>
#include <sys/socket.h> /* netinet/in.h needs it */
#include <netinet/in.h>
#include <net/if.h>
{
return setsockopt(fd, SOL_SOCKET, SO_BROADCAST, &const_int_1, sizeof(const_int_1));
}
+
+#ifdef SO_BINDTODEVICE
int FAST_FUNC setsockopt_bindtodevice(int fd, const char *iface)
{
int r;
bb_perror_msg("can't bind to interface %s", iface);
return r;
}
+#else
+int FAST_FUNC setsockopt_bindtodevice(int fd UNUSED_PARAM,
+ const char *iface UNUSED_PARAM)
+{
+ bb_error_msg("SO_BINDTODEVICE is not supported on this system");
+ return -1;
+}
+#endif
-len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
+static len_and_sockaddr* get_lsa(int fd, int (*get_name)(int fd, struct sockaddr *addr, socklen_t *addrlen))
{
len_and_sockaddr lsa;
len_and_sockaddr *lsa_ptr;
lsa.len = LSA_SIZEOF_SA;
- if (getsockname(fd, &lsa.u.sa, &lsa.len) != 0)
+ if (get_name(fd, &lsa.u.sa, &lsa.len) != 0)
return NULL;
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);
+ get_name(fd, &lsa_ptr->u.sa, &lsa_ptr->len);
} else {
memcpy(lsa_ptr, &lsa, LSA_LEN_SIZE + lsa.len);
}
return lsa_ptr;
}
+len_and_sockaddr* FAST_FUNC get_sock_lsa(int fd)
+{
+ return get_lsa(fd, getsockname);
+}
+
+len_and_sockaddr* FAST_FUNC get_peer_lsa(int fd)
+{
+ return get_lsa(fd, getpeername);
+}
+
void FAST_FUNC xconnect(int s, const struct sockaddr *s_addr, socklen_t addrlen)
{
if (connect(s, s_addr, addrlen) < 0) {
}
-/* "Old" networking API - only IPv4 */
-
-/*
-void FAST_FUNC bb_lookup_host(struct sockaddr_in *s_in, const char *host)
-{
- struct hostent *he;
-
- memset(s_in, 0, sizeof(struct sockaddr_in));
- s_in->sin_family = AF_INET;
- he = xgethostbyname(host);
- memcpy(&(s_in->sin_addr), he->h_addr_list[0], he->h_length);
-}
-
-
-int FAST_FUNC xconnect_tcp_v4(struct sockaddr_in *s_addr)
-{
- int s = xsocket(AF_INET, SOCK_STREAM, 0);
- xconnect(s, (struct sockaddr*) s_addr, sizeof(*s_addr));
- return s;
-}
-*/
-
/* "New" networking API */
return -1;
}
-void FAST_FUNC set_nport(len_and_sockaddr *lsa, unsigned port)
+void FAST_FUNC set_nport(struct sockaddr *sa, unsigned port)
{
#if ENABLE_FEATURE_IPV6
- if (lsa->u.sa.sa_family == AF_INET6) {
- lsa->u.sin6.sin6_port = port;
+ if (sa->sa_family == AF_INET6) {
+ struct sockaddr_in6 *sin6 = (void*) sa;
+ sin6->sin6_port = port;
return;
}
#endif
- if (lsa->u.sa.sa_family == AF_INET) {
- lsa->u.sin.sin_port = port;
+ if (sa->sa_family == AF_INET) {
+ struct sockaddr_in *sin = (void*) sa;
+ sin->sin_port = port;
return;
}
/* What? UNIX socket? IPX?? :) */
}
/* Next two if blocks allow to skip getaddrinfo()
- * in case host is a numeric IP(v6) address,
+ * in case host name is a numeric IP(v6) address.
* getaddrinfo() initializes DNS resolution machinery,
* scans network config and such - tens of syscalls.
*/
memset(&hint, 0 , sizeof(hint));
hint.ai_family = af;
- /* Needed. Or else we will get each address thrice (or more)
+ /* Need SOCK_STREAM, or else we get each address thrice (or more)
* for each possible socket type (tcp,udp,raw...): */
hint.ai_socktype = SOCK_STREAM;
hint.ai_flags = ai_flags & ~DIE_ON_ERROR;
r->len = used_res->ai_addrlen;
memcpy(&r->u.sa, used_res->ai_addr, used_res->ai_addrlen);
- IF_FEATURE_IPV6(set_port:)
- set_nport(r, htons(port));
+ set_port:
+ set_nport(&r->u.sa, htons(port));
ret:
- freeaddrinfo(result);
+ if (result)
+ freeaddrinfo(result);
return r;
}
#if !ENABLE_FEATURE_IPV6
return str2sockaddr(host, port, AF_UNSPEC, AI_NUMERICHOST | DIE_ON_ERROR);
}
-#undef xsocket_type
-int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, IF_FEATURE_IPV6(int family,) int sock_type)
+int FAST_FUNC xsocket_type(len_and_sockaddr **lsap, int family, int sock_type)
{
- IF_NOT_FEATURE_IPV6(enum { family = AF_INET };)
len_and_sockaddr *lsa;
int fd;
int len;
-#if ENABLE_FEATURE_IPV6
if (family == AF_UNSPEC) {
+#if ENABLE_FEATURE_IPV6
fd = socket(AF_INET6, sock_type, 0);
if (fd >= 0) {
family = AF_INET6;
goto done;
}
+#endif
family = AF_INET;
}
-#endif
+
fd = xsocket(family, sock_type, 0);
+
len = sizeof(struct sockaddr_in);
+ if (family == AF_UNIX)
+ len = sizeof(struct sockaddr_un);
#if ENABLE_FEATURE_IPV6
if (family == AF_INET6) {
done:
len = sizeof(struct sockaddr_in6);
}
#endif
- lsa = xzalloc(offsetof(len_and_sockaddr, u.sa) + len);
+ lsa = xzalloc(LSA_LEN_SIZE + len);
lsa->len = len;
lsa->u.sa.sa_family = family;
*lsap = lsa;
int FAST_FUNC xsocket_stream(len_and_sockaddr **lsap)
{
- return xsocket_type(lsap, IF_FEATURE_IPV6(AF_UNSPEC,) SOCK_STREAM);
+ return xsocket_type(lsap, AF_UNSPEC, SOCK_STREAM);
}
static int create_and_bind_or_die(const char *bindaddr, int port, int sock_type)
/* user specified bind addr dictates family */
fd = xsocket(lsa->u.sa.sa_family, sock_type, 0);
} else {
- fd = xsocket_type(&lsa, IF_FEATURE_IPV6(AF_UNSPEC,) sock_type);
- set_nport(lsa, htons(port));
+ fd = xsocket_type(&lsa, AF_UNSPEC, sock_type);
+ set_nport(&lsa->u.sa, htons(port));
}
setsockopt_reuseaddr(fd);
xbind(fd, &lsa->u.sa, lsa->len);