These macros are safe for use on IPv6 and clean up a lot of code.
rb_strlcpy(auth->l_ip, l_ip, sizeof(auth->l_ip));
auth->l_port = (uint16_t)atoi(l_port); /* should be safe */
(void) rb_inet_pton_sock(l_ip, (struct sockaddr *)&auth->l_addr);
+ SET_SS_PORT(&auth->l_addr, htons(auth->l_port));
rb_strlcpy(auth->c_ip, c_ip, sizeof(auth->c_ip));
auth->c_port = (uint16_t)atoi(c_port);
(void) rb_inet_pton_sock(c_ip, (struct sockaddr *)&auth->c_addr);
-
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&auth->l_addr) == AF_INET6)
- ((struct sockaddr_in6 *)&auth->l_addr)->sin6_port = htons(auth->l_port);
- else
-#endif
- ((struct sockaddr_in *)&auth->l_addr)->sin_port = htons(auth->l_port);
-
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&auth->c_addr) == AF_INET6)
- ((struct sockaddr_in6 *)&auth->c_addr)->sin6_port = htons(auth->c_port);
- else
-#endif
- ((struct sockaddr_in *)&auth->c_addr)->sin_port = htons(auth->c_port);
+ SET_SS_PORT(&auth->c_addr, htons(auth->c_port));
rb_strlcpy(auth->hostname, "*", sizeof(auth->hostname));
rb_strlcpy(auth->username, "*", sizeof(auth->username));
l_addr = auth->l_addr;
c_addr = auth->c_addr;
- /* Set the ports correctly */
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&l_addr) == AF_INET6)
- ((struct sockaddr_in6 *)&l_addr)->sin6_port = 0;
- else
-#endif
- ((struct sockaddr_in *)&l_addr)->sin_port = 0;
-
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&c_addr) == AF_INET6)
- ((struct sockaddr_in6 *)&c_addr)->sin6_port = htons(113);
- else
-#endif
- ((struct sockaddr_in *)&c_addr)->sin_port = htons(113);
+ SET_SS_PORT(&l_addr, 0);
+ SET_SS_PORT(&c_addr, htons(113));
rb_connect_tcp(query->F, (struct sockaddr *)&c_addr,
(struct sockaddr *)&l_addr,
{
struct sockaddr_in6 *s = (struct sockaddr_in6 *)&localaddr, *c = (struct sockaddr_in6 *)&auth->c_addr;
- if(memcmp(s->sin6_addr.s6_addr, c->sin6_addr.s6_addr, 16) == 0)
+ if(IN6_ARE_ADDR_EQUAL(&s->sin6_addr, &c->sin6_addr))
{
rb_setselect(F, RB_SELECT_READ, read_opm_reply, auth);
return;
/* Disable Nagle's algorithim - buffering could affect scans */
(void)setsockopt(rb_get_fd(scan->F), IPPROTO_TCP, TCP_NODELAY, (char *)&opt, sizeof(opt));
- /* Set the ports correctly */
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&l_a) == AF_INET6)
- ((struct sockaddr_in6 *)&l_a)->sin6_port = 0;
- else
-#endif
- ((struct sockaddr_in *)&l_a)->sin_port = 0;
-
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&c_a) == AF_INET6)
- ((struct sockaddr_in6 *)&c_a)->sin6_port = ((struct sockaddr_in6 *)&listener->addr)->sin6_port;
- else
-#endif
- ((struct sockaddr_in *)&c_a)->sin_port = ((struct sockaddr_in *)&listener->addr)->sin_port;
+ SET_SS_PORT(&l_a, 0);
+ SET_SS_PORT(&c_a, GET_SS_PORT(&listener->addr));
rb_dlinkAdd(scan, &scan->node, &lookup->scans);
rb_connect_tcp(scan->F,
rb_inet_ntop_sock((struct sockaddr *)&client_p->localClient->ip, client_ipaddr, sizeof(client_ipaddr));
/* Retrieve listener and client ports */
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&client_p->preClient->lip) == AF_INET6)
- listen_port = ntohs(((struct sockaddr_in6 *)&client_p->preClient->lip)->sin6_port);
- else
-#endif
- listen_port = ntohs(((struct sockaddr_in *)&client_p->preClient->lip)->sin_port);
-
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&client_p->localClient->ip) == AF_INET6)
- client_port = ntohs(((struct sockaddr_in6 *)&client_p->localClient->ip)->sin6_port);
- else
-#endif
- client_port = ntohs(((struct sockaddr_in *)&client_p->localClient->ip)->sin_port);
+ listen_port = ntohs(GET_SS_PORT(&client_p->preClient->lip));
+ client_port = ntohs(GET_SS_PORT(&client_p->localClient->ip));
/* Add a bit of a fudge factor... */
- client_p->preClient->authd_timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 5;
+ client_p->preClient->authd_timeout = rb_current_time() + ConfigFileEntry.connect_timeout + 10;
rb_helper_write(authd_helper, "C %x %s %hu %s %hu", authd_cid, listen_ipaddr, listen_port, client_ipaddr, client_port);
}
static uint16_t
get_listener_port(const struct Listener *listener)
{
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&listener->addr) == AF_INET6)
- return ntohs(((const struct sockaddr_in6 *)&listener->addr)->sin6_port);
- else
-#endif
- return ntohs(((const struct sockaddr_in *)&listener->addr)->sin_port);
+ return ntohs(GET_SS_PORT(&listener->addr));
}
/*
{
case AF_INET:
SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in));
- ((struct sockaddr_in *)&vaddr)->sin_port = htons(port);
+ SET_SS_FAMILY(&vaddr, AF_INET);
+ SET_SS_PORT(&vaddr, htons(port));
break;
#ifdef RB_IPV6
case AF_INET6:
SET_SS_LEN(&vaddr, sizeof(struct sockaddr_in6));
- ((struct sockaddr_in6 *)&vaddr)->sin6_port = htons(port);
+ SET_SS_FAMILY(&vaddr, AF_INET6);
+ SET_SS_PORT(&vaddr, htons(port));
break;
#endif
default:
case NOT_AUTHORISED:
{
int port = -1;
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&source_p->localClient->ip) == AF_INET6)
- port = ntohs(((struct sockaddr_in6 *)&source_p->localClient->listener->addr)->sin6_port);
- else
-#endif
- port = ntohs(((struct sockaddr_in *)&source_p->localClient->listener->addr)->sin_port);
+ port = ntohs(GET_SS_PORT(&source_p->localClient->listener->addr));
ServerStats.is_ref++;
/* jdc - lists server name & port connections are on */
rb_strlcpy(client_p->sockhost, buf, sizeof(client_p->sockhost));
client_p->localClient->F = F;
/* shove the port number into the sockaddr */
-#ifdef RB_IPV6
- if(GET_SS_FAMILY(&server_p->my_ipnum) == AF_INET6)
- ((struct sockaddr_in6 *)&server_p->my_ipnum)->sin6_port = htons(server_p->port);
- else
-#endif
- ((struct sockaddr_in *)&server_p->my_ipnum)->sin_port = htons(server_p->port);
+ SET_SS_PORT(&server_p->my_ipnum, htons(server_p->port));
/*
* Set up the initial server evilness, ripped straight from
if(ServerConfVhosted(server_p))
{
memcpy(&myipnum, &server_p->my_ipnum, sizeof(myipnum));
- ((struct sockaddr_in *)&myipnum)->sin_port = 0;
SET_SS_FAMILY(&myipnum, GET_SS_FAMILY(&server_p->my_ipnum));
+ SET_SS_PORT(&myipnum, 0);
}
else if(GET_SS_FAMILY(&server_p->my_ipnum) == AF_INET && ServerInfo.specific_ipv4_vhost)
{
memcpy(&myipnum, &ServerInfo.ip, sizeof(myipnum));
- ((struct sockaddr_in *)&myipnum)->sin_port = 0;
SET_SS_FAMILY(&myipnum, AF_INET);
+ SET_SS_PORT(&myipnum, 0);
SET_SS_LEN(&myipnum, sizeof(struct sockaddr_in));
}
else if((GET_SS_FAMILY(&server_p->my_ipnum) == AF_INET6) && ServerInfo.specific_ipv6_vhost)
{
memcpy(&myipnum, &ServerInfo.ip6, sizeof(myipnum));
- ((struct sockaddr_in6 *)&myipnum)->sin6_port = 0;
SET_SS_FAMILY(&myipnum, AF_INET6);
+ SET_SS_PORT(&myipnum, 0);
SET_SS_LEN(&myipnum, sizeof(struct sockaddr_in6));
}
#endif
#define SET_SS_FAMILY(x, y) ((((struct sockaddr *)(x))->sa_family) = y)
#ifdef RB_SOCKADDR_HAS_SA_LEN
#define SET_SS_LEN(x, y) do { \
- struct sockaddr *storage; \
- storage = ((struct sockaddr *)(x));\
- storage->sa_len = (y); \
+ struct sockaddr *_storage; \
+ _storage = ((struct sockaddr *)(x));\
+ _storage->sa_len = (y); \
} while (0)
#define GET_SS_LEN(x) (((struct sockaddr *)(x))->sa_len)
#else /* !RB_SOCKADDR_HAS_SA_LEN */
#endif
#endif
+#ifdef RB_IPV6
+#define GET_SS_PORT(x) (((struct sockaddr *)(x))->sa_family == AF_INET ? ((struct sockaddr_in *)(x))->sin_port : ((struct sockaddr_in6 *)(x))->sin6_port)
+#define SET_SS_PORT(x, y) do { \
+ if(((struct sockaddr *)(x))->sa_family == AF_INET) { \
+ ((struct sockaddr_in *)(x))->sin_port = (y); \
+ } else { \
+ ((struct sockaddr_in6 *)(x))->sin6_port = (y); \
+ } \
+ } while (0)
+#else
+#define GET_SS_PORT(x) (((struct sockaddr_in *)(x))->sin_port)
+#define SET_SS_PORT(x, y) (((struct sockaddr_in *)(x))->sin_port = y)
+#endif
+
#ifndef INADDRSZ
#define INADDRSZ 4
#endif
{
if(rb_inet_pton(AF_INET, src, &((struct sockaddr_in *)dst)->sin_addr))
{
- ((struct sockaddr_in *)dst)->sin_port = 0;
- ((struct sockaddr_in *)dst)->sin_family = AF_INET;
+ SET_SS_FAMILY(dst, AF_INET);
SET_SS_LEN(dst, sizeof(struct sockaddr_in));
return 1;
}
#ifdef RB_IPV6
else if(rb_inet_pton(AF_INET6, src, &((struct sockaddr_in6 *)dst)->sin6_addr))
{
- ((struct sockaddr_in6 *)dst)->sin6_port = 0;
- ((struct sockaddr_in6 *)dst)->sin6_family = AF_INET6;
+ SET_SS_FAMILY(dst, AF_INET6);
SET_SS_LEN(dst, sizeof(struct sockaddr_in6));
return 1;
}
#endif
+ SET_SS_PORT(dst, 0);
return 0;
}