1From 76f8598fd20727908e760cbb497dd6a17eda4af5 Mon Sep 17 00:00:00 2001
2From: Roy Li <rongqing.li@windriver.com>
3Date: Wed, 17 Sep 2014 13:22:14 +0800
4Subject: [PATCH] add option to make users able to use fixed port number
5
6Upstream-Status: Submitted [https://sourceforge.net/p/rpcbind/discussion/716839/thread/32af721d/]
7
8Signed-off-by: Li Wang <li.wang@windriver.com>
9Signed-off-by: Roy Li <rongqing.li@windriver.com>
10Signed-off-by: Yi Zhao <yi.zhao@windriver.com>
11---
12 man/rpcbind.8      |  4 +++-
13 src/rpcb_svc_com.c | 17 +++++++++++++++++
14 src/rpcbind.c      |  8 ++++++--
15 3 files changed, 26 insertions(+), 3 deletions(-)
16
17diff --git a/man/rpcbind.8 b/man/rpcbind.8
18index fbf0ace..66f7c08 100644
19--- a/man/rpcbind.8
20+++ b/man/rpcbind.8
21@@ -11,7 +11,7 @@
22 .Nd universal addresses to RPC program number mapper
23 .Sh SYNOPSIS
24 .Nm
25-.Op Fl adhiLls
26+.Op Fl adhpiLls
27 .Sh DESCRIPTION
28 The
29 .Nm
30@@ -96,6 +96,8 @@ will automatically add
31 and if IPv6 is enabled,
32 .Li ::1
33 to the list.
34+.It Fl p
35+Bind for fixed UDP port number
36 If no
37 .Fl h
38 option is specified,
39diff --git a/src/rpcb_svc_com.c b/src/rpcb_svc_com.c
40index 1743dad..07a1c75 100644
41--- a/src/rpcb_svc_com.c
42+++ b/src/rpcb_svc_com.c
43@@ -48,6 +48,7 @@
44 #include <rpc/rpc.h>
45 #include <rpc/rpcb_prot.h>
46 #include <rpc/svc_dg.h>
47+#include <rpc/rpc_com.h>
48 #include <netconfig.h>
49 #include <errno.h>
50 #include <syslog.h>
51@@ -497,6 +498,7 @@ xdr_opaque_parms(XDR *xdrs, struct r_rmtcall_args *cap)
52
53 static struct rmtcallfd_list *rmthead;
54 static struct rmtcallfd_list *rmttail;
55+extern unsigned short fixed_port;
56
57 int
58 create_rmtcall_fd(struct netconfig *nconf)
59@@ -504,6 +506,8 @@ create_rmtcall_fd(struct netconfig *nconf)
60 	int fd;
61 	struct rmtcallfd_list *rmt;
62 	SVCXPRT *xprt;
63+        struct __rpc_sockinfo si;
64+        struct t_bind taddr;
65
66 	if ((fd = __rpc_nconf2fd(nconf)) == -1) {
67 		if (debugging)
68@@ -512,6 +516,19 @@ create_rmtcall_fd(struct netconfig *nconf)
69 			nconf->nc_device, errno);
70 		return (-1);
71 	}
72+
73+        if (fixed_port) {
74+                __rpc_fd2sockinfo(fd, &si);
75+                memset(&taddr, 0, sizeof(taddr));
76+                taddr.addr.maxlen = taddr.addr.len = si.si_alen;
77+                taddr.addr.buf = malloc(si.si_alen);
78+                if (taddr.addr.buf == NULL) {
79+                        return -1;
80+                }
81+                *(unsigned short *)(&(taddr.addr.buf[0])) = si.si_af;
82+                *(unsigned short *)(&(taddr.addr.buf[2])) = htons(fixed_port);
83+                xprt = svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
84+        } else
85 	xprt = svc_tli_create(fd, 0, (struct t_bind *) 0, 0, 0);
86 	if (xprt == NULL) {
87 		if (debugging)
88diff --git a/src/rpcbind.c b/src/rpcbind.c
89index 25d8a90..36a95b9 100644
90--- a/src/rpcbind.c
91+++ b/src/rpcbind.c
92@@ -111,6 +111,7 @@ int runasdaemon = 0;
93 int insecure = 0;
94 int oldstyle_local = 0;
95 int verboselog = 0;
96+unsigned short fixed_port = 0;
97
98 char **hosts = NULL;
99 int nhosts = 0;
100@@ -884,7 +885,7 @@ parseargs(int argc, char *argv[])
101 {
102 	int c;
103 	oldstyle_local = 1;
104-	while ((c = getopt(argc, argv, "adh:ilswf")) != -1) {
105+	while ((c = getopt(argc, argv, "adh:p:ilswf")) != -1) {
106 		switch (c) {
107 		case 'a':
108 			doabort = 1;	/* when debugging, do an abort on */
109@@ -902,6 +903,9 @@ parseargs(int argc, char *argv[])
110 			if (hosts[nhosts - 1] == NULL)
111 				errx(1, "Out of memory");
112 			break;
113+                case 'p':
114+                        fixed_port = atoi(optarg);
115+                        break;
116 		case 'i':
117 			insecure = 1;
118 			break;
119@@ -920,7 +924,7 @@ parseargs(int argc, char *argv[])
120 			break;
121 #endif
122 		default:	/* error */
123-			fprintf(stderr,	"usage: rpcbind [-adhilswf]\n");
124+			fprintf(stderr, "usage: rpcbind [-adhpilswf]\n");
125 			exit (1);
126 		}
127 	}
128--
1292.25.1
130
131