From d86692fa44a3a7fa87409b2f31b53642e36a58c9 Mon Sep 17 00:00:00 2001 From: Elizabeth Myers Date: Thu, 31 Mar 2016 03:00:29 -0500 Subject: [PATCH] Add new sockaddr_storage port retrieval/setting macros These macros are safe for use on IPv6 and clean up a lot of code. --- authd/provider.c | 16 ++-------------- authd/providers/ident.c | 16 ++-------------- authd/providers/opm.c | 18 +++--------------- ircd/authd.c | 17 +++-------------- ircd/listener.c | 13 +++++-------- ircd/s_conf.c | 7 +------ ircd/s_serv.c | 13 ++++--------- librb/include/rb_lib.h | 20 +++++++++++++++++--- librb/src/commio.c | 7 +++---- 9 files changed, 40 insertions(+), 87 deletions(-) diff --git a/authd/provider.c b/authd/provider.c index 3a7abf6e8..6cc6eee4e 100644 --- a/authd/provider.c +++ b/authd/provider.c @@ -272,24 +272,12 @@ start_auth(const char *cid, const char *l_ip, const char *l_port, const char *c_ 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)); diff --git a/authd/providers/ident.c b/authd/providers/ident.c index a2b320be3..b2302efd1 100644 --- a/authd/providers/ident.c +++ b/authd/providers/ident.c @@ -328,20 +328,8 @@ ident_start(struct auth_client *auth) 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, diff --git a/authd/providers/opm.c b/authd/providers/opm.c index af289ca71..3e5bd0e1d 100644 --- a/authd/providers/opm.c +++ b/authd/providers/opm.c @@ -184,7 +184,7 @@ accept_opm(rb_fde_t *F, int status, struct sockaddr *addr, rb_socklen_t len, voi { 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; @@ -364,20 +364,8 @@ establish_connection(struct auth_client *auth, struct opm_proxy *proxy) /* 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, diff --git a/ircd/authd.c b/ircd/authd.c index eae2a245a..aa657d5ce 100644 --- a/ircd/authd.c +++ b/ircd/authd.c @@ -374,22 +374,11 @@ authd_initiate_client(struct Client *client_p) 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); } diff --git a/ircd/listener.c b/ircd/listener.c index fda86f967..8ae0fabbd 100644 --- a/ircd/listener.c +++ b/ircd/listener.c @@ -105,12 +105,7 @@ free_listener(struct Listener *listener) 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)); } /* @@ -377,12 +372,14 @@ add_listener(int port, const char *vhost_ip, int family, int ssl, int defer_acce { 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: diff --git a/ircd/s_conf.c b/ircd/s_conf.c index 18db17365..9b1becb7b 100644 --- a/ircd/s_conf.c +++ b/ircd/s_conf.c @@ -258,12 +258,7 @@ check_client(struct Client *client_p, struct Client *source_p, const char *usern 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 */ diff --git a/ircd/s_serv.c b/ircd/s_serv.c index 79a0a487c..1c49baa58 100644 --- a/ircd/s_serv.c +++ b/ircd/s_serv.c @@ -1041,12 +1041,7 @@ serv_connect(struct server_conf *server_p, struct Client *by) 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 @@ -1084,15 +1079,15 @@ serv_connect(struct server_conf *server_p, struct Client *by) 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)); } @@ -1100,8 +1095,8 @@ serv_connect(struct server_conf *server_p, struct Client *by) 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 diff --git a/librb/include/rb_lib.h b/librb/include/rb_lib.h index 7ef473a95..d420bc9a9 100644 --- a/librb/include/rb_lib.h +++ b/librb/include/rb_lib.h @@ -159,9 +159,9 @@ char *rb_strerror(int error); #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 */ @@ -173,6 +173,20 @@ char *rb_strerror(int error); #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 diff --git a/librb/src/commio.c b/librb/src/commio.c index 9fe8689c3..259b900d9 100644 --- a/librb/src/commio.c +++ b/librb/src/commio.c @@ -1321,20 +1321,19 @@ rb_inet_pton_sock(const char *src, struct sockaddr *dst) { 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; }