krb5 commit: Use sa_setport and sa_getport where appropriate

Greg Hudson ghudson at MIT.EDU
Fri Jun 27 22:37:17 EDT 2014


https://github.com/krb5/krb5/commit/7567f36f83bab47206c2ea85724c1e74c26ec0d6
commit 7567f36f83bab47206c2ea85724c1e74c26ec0d6
Author: Greg Hudson <ghudson at mit.edu>
Date:   Sat Jun 7 16:25:35 2014 -0400

    Use sa_setport and sa_getport where appropriate
    
    Use sa_setport and sa_getport in place of existing static helpers or
    open-coded switch statements in net-server.c, fake-addrinfo.c, and
    svc_tcp.c.

 src/lib/apputils/net-server.c    |   26 +++++---------------------
 src/lib/rpc/svc_tcp.c            |   14 ++------------
 src/util/support/fake-addrinfo.c |   11 ++---------
 3 files changed, 9 insertions(+), 42 deletions(-)

diff --git a/src/lib/apputils/net-server.c b/src/lib/apputils/net-server.c
index d19d45a..02b1058 100644
--- a/src/lib/apputils/net-server.c
+++ b/src/lib/apputils/net-server.c
@@ -66,22 +66,6 @@
 static int tcp_or_rpc_data_counter;
 static int max_tcp_or_rpc_data_connections = 45;
 
-/* Misc utility routines.  */
-static void
-set_sa_port(struct sockaddr *addr, int port)
-{
-    switch (addr->sa_family) {
-    case AF_INET:
-        sa2sin(addr)->sin_port = port;
-        break;
-    case AF_INET6:
-        sa2sin6(addr)->sin6_port = port;
-        break;
-    default:
-        break;
-    }
-}
-
 static int
 ipv6_enabled()
 {
@@ -740,7 +724,7 @@ setup_tcp_listener_ports(struct socksetup *data)
     FOREACH_ELT (tcp_port_data, i, port) {
         int s4, s6;
 
-        set_sa_port((struct sockaddr *)&sin4, htons(port));
+        sa_setport((struct sockaddr *)&sin4, port);
         if (!ipv6_enabled()) {
             s4 = setup_a_tcp_listener(data, (struct sockaddr *)&sin4);
             if (s4 < 0)
@@ -749,7 +733,7 @@ setup_tcp_listener_ports(struct socksetup *data)
         } else {
             s4 = s6 = -1;
 
-            set_sa_port((struct sockaddr *)&sin6, htons(port));
+            sa_setport((struct sockaddr *)&sin6, port);
 
             s6 = setup_a_tcp_listener(data, (struct sockaddr *)&sin6);
             if (s6 < 0)
@@ -809,7 +793,7 @@ setup_rpc_listener_ports(struct socksetup *data)
         int s4;
         int s6;
 
-        set_sa_port((struct sockaddr *)&sin4, htons(svc.port));
+        sa_setport((struct sockaddr *)&sin4, svc.port);
         s4 = create_server_socket(data, (struct sockaddr *)&sin4, SOCK_STREAM);
         if (s4 < 0)
             return -1;
@@ -821,7 +805,7 @@ setup_rpc_listener_ports(struct socksetup *data)
                              s4, paddr((struct sockaddr *)&sin4));
 
         if (ipv6_enabled()) {
-            set_sa_port((struct sockaddr *)&sin6, htons(svc.port));
+            sa_setport((struct sockaddr *)&sin6, svc.port);
             s6 = create_server_socket(data, (struct sockaddr *)&sin6,
                                       SOCK_STREAM);
             if (s6 < 0)
@@ -903,7 +887,7 @@ setup_udp_port_1(struct socksetup *data, struct sockaddr *addr,
     u_short port;
 
     FOREACH_ELT (udp_port_data, i, port) {
-        set_sa_port(addr, htons(port));
+        sa_setport(addr, port);
         sock = create_server_socket(data, addr, SOCK_DGRAM);
         if (sock == -1)
             return 1;
diff --git a/src/lib/rpc/svc_tcp.c b/src/lib/rpc/svc_tcp.c
index 581ab3c..bbafc1d 100644
--- a/src/lib/rpc/svc_tcp.c
+++ b/src/lib/rpc/svc_tcp.c
@@ -53,6 +53,7 @@ static char sccsid[] = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
 #include "autoconf.h"
 #include "k5-platform.h"	/* set_cloexec_fd */
 #include <port-sockets.h>
+#include <socket-utils.h>
 /*extern bool_t abort();
 extern errno;
 */
@@ -118,17 +119,6 @@ struct tcp_conn {  /* kept in xprt->xp_p1 */
 	char verf_body[MAX_AUTH_BYTES];
 };
 
-static u_short
-getport(struct sockaddr *addr)
-{
-    if (addr->sa_family == AF_INET)
-	return ntohs(((struct sockaddr_in *) addr)->sin_port);
-    else if (addr->sa_family == AF_INET6)
-	return ntohs(((struct sockaddr_in6 *) addr)->sin6_port);
-    else
-	return 0;
-}
-
 /*
  * Usage:
  *	xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
@@ -208,7 +198,7 @@ svctcp_create(
 	xprt->xp_auth = NULL;
 	xprt->xp_verf = gssrpc__null_auth;
 	xprt->xp_ops = &svctcp_rendezvous_op;
-	xprt->xp_port = getport((struct sockaddr *) &addr);
+	xprt->xp_port = sa_getport((struct sockaddr *) &addr);
 	xprt->xp_sock = sock;
 	xprt->xp_laddrlen = 0;
 	xprt_register(xprt);
diff --git a/src/util/support/fake-addrinfo.c b/src/util/support/fake-addrinfo.c
index 2129758..6ddf859 100644
--- a/src/util/support/fake-addrinfo.c
+++ b/src/util/support/fake-addrinfo.c
@@ -1145,7 +1145,7 @@ getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint,
             if (lport > 65535)
                 return EAI_SOCKTYPE;
             service_is_numeric = 1;
-            service_port = htons(lport);
+            service_port = lport;
 #ifdef AI_NUMERICSERV
             if (hint && hint->ai_flags & AI_NUMERICSERV)
                 serv = "9";
@@ -1281,14 +1281,7 @@ getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint,
             if (socket_type != 0 && ai->ai_socktype == 0)
                 /* Is this check actually needed?  */
                 ai->ai_socktype = socket_type;
-            switch (ai->ai_family) {
-            case AF_INET:
-                ((struct sockaddr_in *)ai->ai_addr)->sin_port = service_port;
-                break;
-            case AF_INET6:
-                ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = service_port;
-                break;
-            }
+            sa_setport(ai->ai_addr, service_port);
         }
     }
 #endif


More information about the cvs-krb5 mailing list