e10053c
commit 3e4c74ab527375f37b6633f528e7eab0c363967b
e10053c
Author: Olaf Kirch <okir@suse.de>
e10053c
Date:   Tue Sep 16 10:08:35 2008 -0400
e10053c
e10053c
    Introduce helpers for ipprot/netid mapping
e10053c
    
e10053c
    There's a couple of places in the portmap emulation code
e10053c
    where we translate between ip protocol numbers and netids.
e10053c
    Encapsulate these in two helper functions:
e10053c
    
e10053c
    extern char *pmap_ipprot2netid(int);
e10053c
    extern int pmap_netid2ipprot(const char *);
e10053c
    
e10053c
    Signed-off-by: okir@suse.de
e10053c
    Signed-off-by: Steve Dickson <steved@redhat.com>
e10053c
e10053c
diff --git a/src/pmap_svc.c b/src/pmap_svc.c
e10053c
index 99ce508..02a57d2 100644
e10053c
--- a/src/pmap_svc.c
e10053c
+++ b/src/pmap_svc.c
e10053c
@@ -211,11 +211,8 @@ pmapproc_change(struct svc_req *rqstp /*__unused*/, SVCXPRT *xprt, unsigned long
e10053c
 	if (op == PMAPPROC_SET) {
e10053c
 		char buf[32];
e10053c
 
e10053c
-		if (reg.pm_prot == IPPROTO_UDP) {
e10053c
-			rpcbreg.r_netid = udptrans;
e10053c
-		} else if (reg.pm_prot == IPPROTO_TCP) {
e10053c
-			rpcbreg.r_netid = tcptrans;
e10053c
-		} else {
e10053c
+		rpcbreg.r_netid = pmap_ipprot2netid(reg.pm_prot);
e10053c
+		if (rpcbreg.r_netid == NULL) {
e10053c
 			ans = FALSE;
e10053c
 			goto done_change;
e10053c
 		}
e10053c
@@ -289,7 +286,8 @@ pmapproc_getport(struct svc_req *rqstp /*__unused*/, SVCXPRT *xprt)
e10053c
 			    svc_getrpccaller(xprt));
e10053c
 		fprintf(stderr, "PMAP_GETPORT req for (%lu, %lu, %s) from %s :",
e10053c
 			reg.pm_prog, reg.pm_vers,
e10053c
-			reg.pm_prot == IPPROTO_UDP ? "udp" : "tcp", uaddr);
e10053c
+			pmap_ipprot2netid(reg.pm_prot)?: "<invalid>",
e10053c
+			uaddr);
e10053c
 		free(uaddr);
e10053c
 	}
e10053c
 #endif
e10053c
@@ -299,12 +297,13 @@ pmapproc_getport(struct svc_req *rqstp /*__unused*/, SVCXPRT *xprt)
e10053c
 		char *pt1, *pt2;
e10053c
 		char *netid;
e10053c
 
e10053c
+		netid = pmap_ipprot2netid(reg.pm_prot);
e10053c
+		if (netid == NULL)
e10053c
+			goto sendreply;
e10053c
 		if (reg.pm_prot == IPPROTO_UDP) {
e10053c
 			ua = udp_uaddr;
e10053c
-			netid = udptrans;
e10053c
 		} else {
e10053c
 			ua = tcp_uaddr; /* To get the len */
e10053c
-			netid = tcptrans;
e10053c
 		}
e10053c
 		if (ua == NULL) {
e10053c
 			goto sendreply;
e10053c
@@ -341,7 +340,7 @@ sendreply:
e10053c
 		fprintf(stderr, "port = %d\n", port);
e10053c
 #endif
e10053c
 	rpcbs_getaddr(RPCBVERS_2_STAT, reg.pm_prog, reg.pm_vers,
e10053c
-		reg.pm_prot == IPPROTO_UDP ? udptrans : tcptrans,
e10053c
+		pmap_ipprot2netid(reg.pm_prot) ?: "<unknown>",
e10053c
 		port ? udptrans : "");
e10053c
 
e10053c
 	return (TRUE);
e10053c
@@ -372,4 +371,24 @@ pmapproc_dump(struct svc_req *rqstp /*__unused*/, SVCXPRT *xprt)
e10053c
 	return (TRUE);
e10053c
 }
e10053c
 
e10053c
+int pmap_netid2ipprot(const char *netid)
e10053c
+{
e10053c
+	if (!netid)
e10053c
+		return 0;
e10053c
+	if (strcmp(netid, tcptrans) == 0)
e10053c
+		return IPPROTO_TCP;
e10053c
+	if (strcmp(netid, udptrans) == 0)
e10053c
+		return IPPROTO_UDP;
e10053c
+	return 0;
e10053c
+}
e10053c
+
e10053c
+char *pmap_ipprot2netid(unsigned long proto)
e10053c
+{
e10053c
+	if (proto == IPPROTO_UDP)
e10053c
+		return udptrans;
e10053c
+	if (proto == IPPROTO_TCP)
e10053c
+		return tcptrans;
e10053c
+	return NULL;
e10053c
+}
e10053c
+
e10053c
 #endif /* PORTMAP */
e10053c
diff --git a/src/rpcb_svc_com.c b/src/rpcb_svc_com.c
e10053c
index 8f6b4c7..08aa194 100644
e10053c
--- a/src/rpcb_svc_com.c
e10053c
+++ b/src/rpcb_svc_com.c
e10053c
@@ -1416,13 +1416,8 @@ add_pmaplist(RPCB *arg)
e10053c
 	struct pmap pmap;
e10053c
 	struct pmaplist *pml;
e10053c
 
e10053c
-	if (strcmp(arg->r_netid, udptrans) == 0) {
e10053c
-		/* It is UDP! */
e10053c
-		pmap.pm_prot = IPPROTO_UDP;
e10053c
-	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
e10053c
-		/* It is TCP */
e10053c
-		pmap.pm_prot = IPPROTO_TCP;
e10053c
-	} else
e10053c
+	pmap.pm_prot = pmap_netid2ipprot(arg->r_netid);
e10053c
+	if (!pmap.pm_prot)
e10053c
 		/* Not an IP protocol */
e10053c
 		return (0);
e10053c
 
e10053c
@@ -1464,15 +1459,10 @@ del_pmaplist(RPCB *arg)
e10053c
 	struct pmaplist *prevpml, *fnd;
e10053c
 	unsigned long prot;
e10053c
 
e10053c
-	if (strcmp(arg->r_netid, udptrans) == 0) {
e10053c
-		/* It is UDP! */
e10053c
-		prot = IPPROTO_UDP;
e10053c
-	} else if (strcmp(arg->r_netid, tcptrans) == 0) {
e10053c
-		/* It is TCP */
e10053c
-		prot = IPPROTO_TCP;
e10053c
-	} else if (arg->r_netid[0] == 0) {
e10053c
+	if (arg->r_netid[0] == 0) {
e10053c
 		prot = 0;	/* Remove all occurrences */
e10053c
-	} else {
e10053c
+	} else
e10053c
+	if ((prot = pmap_netid2ipprot(arg->r_netid)) == 0) {
e10053c
 		/* Not an IP protocol */
e10053c
 		return (0);
e10053c
 	}
e10053c
diff --git a/src/rpcbind.h b/src/rpcbind.h
e10053c
index 84deba0..58ab3ef 100644
e10053c
--- a/src/rpcbind.h
e10053c
+++ b/src/rpcbind.h
e10053c
@@ -78,6 +78,8 @@ extern char *udptrans;		/* Name of UDP transport */
e10053c
 extern char *tcptrans;		/* Name of TCP transport */
e10053c
 extern char *udp_uaddr;		/* Universal UDP address */
e10053c
 extern char *tcp_uaddr;		/* Universal TCP address */
e10053c
+extern char *pmap_ipprot2netid(unsigned long);
e10053c
+extern int pmap_netid2ipprot(const char *);
e10053c
 #endif
e10053c
 
e10053c
 int add_bndlist(struct netconfig *, struct netbuf *);