pastebin - collaborative debugging tool
nrubsig.kpaste.net RSS

Difference between
modified post 26ee6ebf by Anonymous on Tue 26th Mar 2024 15:18
original post 5e4884f6e0 by Anonymous on Tue 26th Mar 2024 11:29
Download diff
Show old version | new version | both versions

    
1-
diff --git a/libtirpc/src/bindresvport.c b/libtirpc/src/bindresvport.c
1+
diff --git a/cygwin/README.bintarball.txt b/cygwin/README.bintarball.txt
2-
index 01d1921..fd11dc3 100644
2+
index b1b66e0..9fa5222 100644
3-
--- a/libtirpc/src/bindresvport.c
3+
--- a/cygwin/README.bintarball.txt
4-
+++ b/libtirpc/src/bindresvport.c
4+
+++ b/cygwin/README.bintarball.txt
5-
@@ -1,5 +1,6 @@
5+
@@ -129,10 +129,6 @@ $ (set -o xtrace ; cd / && tar -tf ~/download/${bintarball.base_filename}.tar.bz
6-
 /*
6+
 $ /sbin/msnfs41client run_daemon
7-
  * Copyright (c) 2009, Sun Microsystems, Inc.
7+
8-
+ * Copyright (c) 2024, Roland Mainz <roland.mainz@nrubsig.org>
8+
 # Mount a filesystem and use it
9-
  * All rights reserved.
9+
-# - requires that NFSv4 server accepts connections from a TCP port
10-
  *
10+
-# number > 1024, which can be archived on Linux with the "insecure"
11-
  * Redistribution and use in source and binary forms, with or without
11+
-# export option in /etc/exports, or "resvport" on Solaris/Illumos
12-
@@ -32,20 +33,30 @@
12+
-# (see nfs(5))
13-
  * Copyright (c) 1987 by Sun Microsystems, Inc.
13+
 $ /sbin/nfs_mount -o rw N 10.49.20.110:/net_tmpfs2
14-
  *
14+
 Successfully mounted '10.49.20.110@2049' to drive 'N:'
15-
  * Portions Copyright(C) 1996, Jason Downs.  All rights reserved.
15+
 $ cd /cygdrive/n/
16-
+ * Portions Copyright(C) 2024, Roland Mainz <roland.mainz@nrubsig.org>
16+
@@ -230,15 +226,15 @@ $ /sbin/nfs_mount
17-
  */
17+
   A::EVERYONE@:rtcy
18+
   ---- snip ----
1819
19-
 #include <wintirpc.h>
20+
-- nfs_mount only works when the NFSv4 server allows connections from
20-
 #include <sys/types.h>
21+
-  ports >= 1024, as Windows does not allow the Windows NFSv4 client
21-
-//#include <sys/socket.h>
22+
-  to use a "privileged port" (i.e. TCP port number < 1024)).
22-
-
23+
+- nfs_mount.exe vs. reserved ports:
23-
-//#include <netinet/in.h>
24+
   By default the NFSv4 server on Solaris, Illumos, Linux
24-
+#ifndef _WIN32
25+
   etc. only accepts connections if the NFSv4 client uses a
25-
+#include <sys/socket.h>
26+
-  "privileged (TCP) port", i.e. a port number < 1024.
26-
+#include <netinet/in.h>
27+
-  This can be worked around by using the "insecure" export option in
27-
+#endif
28+
-  Linux /etc/exports, which allows connections from ports >= 1024,
29+
-  and for Solaris/Illumos see nfs(5), option "resvport".
2836
3537
36-
 #include <rpc/rpc.h>
38+
 #
37-
39+
diff --git a/libtirpc/src/bindresvport.c b/libtirpc/src/bindresvport.c
38-
+#ifdef _WIN32
40+
index 01d1921..be56a6c 100644
39-
+#include <winsock2.h>
41+
--- a/libtirpc/src/bindresvport.c
40-
+#include <mstcpip.h>
42+
+++ b/libtirpc/src/bindresvport.c
41-
+#include <ws2ipdef.h>
43+
@@ -1,5 +1,6 @@
4444
 /*
45-
  * Bind a socket to a privileged IP port
45+
  * Copyright (c) 2009, Sun Microsystems, Inc.
46+
+ * Copyright (c) 2024, Roland Mainz <roland.mainz@nrubsig.org>
4655
  */
5056
51-
-#else
57+
 #include <wintirpc.h>
52-
-/*----------------------
58+
 #include <sys/types.h>
53-
-#if defined(_WIN32)
59+
-//#include <sys/socket.h>
54-
+#elif defined(_WIN32)
60+
-
55-
+
61+
-//#include <netinet/in.h>
56-
+#define STARTPORT 600
62+
+#ifndef _WIN32
57-
+#define LOWPORT 512
63+
+#include <sys/socket.h>
58-
+#define ENDPORT (IPPORT_RESERVED - 1)
64+
+#include <netinet/in.h>
59-
+#define NPORTS  (ENDPORT - STARTPORT + 1)
65+
+#endif
6066
61-
 int
67+
 #include <errno.h>
62-
 bindresvport_sa(int sd, struct sockaddr *sa)
68+
 #include <string.h>
63-
 {
69+
-//#include <unistd.h>
64-
-	fprintf(stderr, "Do-nothing bindresvport_sa!\n");
70+
+#ifndef _WIN32
65-
+	int ioctlres;
71+
+#include <unistd.h>
66-
+        int lasterr;
72+
+#endif
67-
+	SOCKET sd_sock;
73+
68-
+	int currport;
74+
 #include <rpc/rpc.h>
75+
6981
+
70-
+	INET_PORT_RANGE portRange;
82+
 /*
71-
+	INET_PORT_RESERVATION_INSTANCE portRes;
83+
  * Bind a socket to a privileged IP port
72-
+	DWORD bytesReturned;
84+
  */
85+
@@ -139,18 +150,147 @@ bindresvport_sa(sd, sa)
7390
+
74-
+	sd_sock = _get_osfhandle(sd);
91+
+#define STARTPORT 600
92+
+#define ENDPORT (IPPORT_RESERVED - 1)
7594
+
76-
+	for (currport = 600 ; currport < ENDPORT ; currport++) {
95+
+/* Debug */
77-
+		(void)memset(&portRange, 0, sizeof(portRange));
96+
+#if 0
78-
+		portRange.StartPort = htons(currport);
97+
+#define BRP_D(x) x
79-
+		portRange.NumberOfPorts = 1;
98+
 #else
99+
-/*----------------------
80103
+
81-
+		(void)memset(&portRes, 0, sizeof(portRes));
104+
+/* fixme: not threadsafe, we should use |portnum_lock| */
82-
+		bytesReturned = 0;
105+
+static int bindresvport_sa_last_n = 0;
83-
+
106+
84-
+		(void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
107+
 int
85-
+			"trying port=%d ...\n",
108+
 bindresvport_sa(int sd, struct sockaddr *sa)
86-
+			sd, sa, (int)ntohs(portRange.StartPort));
109+
 {
87-
+                ioctlres = WSAIoctl(sd_sock, SIO_ACQUIRE_PORT_RESERVATION,
110+
-	fprintf(stderr, "Do-nothing bindresvport_sa!\n");
88-
+			(LPVOID)&portRange, sizeof(INET_PORT_RANGE),
111+
-	return 0;
89-
+			(LPVOID)&portRes, sizeof(INET_PORT_RESERVATION_INSTANCE),
112+
+	int res = 1;
90-
+			&bytesReturned, NULL, NULL);
113+
+	int ioctlres;
91-
+                lasterr = WSAGetLastError();
114+
+	int lasterr;
115+
+	SOCKET sd_sock;
92118
+
93-
+		if ((ioctlres != 0) && (lasterr == WSAEADDRINUSE)) {
119+
+	INET_PORT_RANGE portRange;
94-
+			(void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
120+
+	INET_PORT_RESERVATION_INSTANCE portRes;
95-
+				"port=%d in use, trying next port...\n",
121+
+	DWORD bytesReturned;
99122
+
100-
+		if (ioctlres != 0) {
123+
+	BRP_D((void)fprintf(stdout,
101-
+			warnx("bindresvport_sa(sd=%d,sa=0x%p): "
124+
+		"--> bindresvport_sa(sd=%d,sa=0x%p): "
102-
+				"SIO_ACQUIRE_PORT_RESERVATION failed with "
125+
+		"bindresvport_sa_last_n=%d\n",
103-
+				"error = %d\n",
126+
+		sd, sa, bindresvport_sa_last_n));
107127
+
108-
+		/* Success */
128+
+	sd_sock = _get_osfhandle(sd);
111129
+
112-
+        /* fixme: What if the loop completed ? */
130+
+	for (n = 0 ; n < NPORTS ; n++) {
131+
+		currport = ((n+bindresvport_sa_last_n)%NPORTS)+STARTPORT;
113132
+
114-
+	(void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
133+
+		portRange.StartPort = htons(currport);
115-
+		"SIO_ACQUIRE_PORT_RESERVATION succeeded, "
134+
+		portRange.NumberOfPorts = 1;
120135
+
121-
+	bytesReturned = 0;
136+
+		(void)memset(&portRes, 0, sizeof(portRes));
122-
+	ioctlres = WSAIoctl(sd_sock, SIO_ASSOCIATE_PORT_RESERVATION,
137+
+		bytesReturned = 0;
123-
+		(LPVOID)&portRes.Token, sizeof(ULONG64), NULL, 0,
138+
+
124-
+		&bytesReturned, NULL, NULL);
139+
+		BRP_D((void)fprintf(stdout,
125-
+	lasterr = WSAGetLastError();
140+
+			"bindresvport_sa(sd=%d,sa=0x%p): "
126-
+	if (ioctlres != 0) {
141+
+			"trying n=%d, bindresvport_sa_last_n=%d, port=%d ...\n",
127-
+		(void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
142+
+			sd, sa, n, bindresvport_sa_last_n,
128-
+			"WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) "
143+
+			(int)ntohs(portRange.StartPort)));
129-
+			"failed with error = %d\n",
144+
+		ioctlres = WSAIoctl(sd_sock,
130-
+			sd, sa, lasterr);
145+
+			SIO_ACQUIRE_PORT_RESERVATION,
131-
+		return 1;
146+
+			(LPVOID)&portRange,
132-
+	}
147+
+			sizeof(INET_PORT_RANGE),
148+
+			(LPVOID)&portRes,
133152
+
134-
+	(void)fprintf(stderr, "bindresvport_sa(sd=%d,sa=0x%p): "
153+
+		if ((ioctlres != 0) && (lasterr == WSAEADDRINUSE)) {
135-
+		"WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, bytesReturned = %u\n",
154+
+			BRP_D((void)fprintf(stderr,
136-
+		sd, sa, bytesReturned);
155+
+				"bindresvport_sa(sd=%d,sa=0x%p): "
156+
+				"port=%d in use, trying next port...\n",
137160
+
138-
 	return 0;
161+
+		if (ioctlres != 0) {
139-
 }
162+
+			warnx("bindresvport_sa(sd=%d,sa=0x%p): "
140-
 #else
163+
+				"SIO_ACQUIRE_PORT_RESERVATION failed "
141-
--------------------------*/
164+
+				"with error = %d\n",
165+
+				sd, sa, lasterr);
142170
+
143-
 #define IP_PORTRANGE 19
171+
+		/* Success */
144-
 #define IP_PORTRANGE_LOW 2
172+
+		bindresvport_sa_last_n = n+1;
145-
173+
+		break;
146-
diff --git a/libtirpc/src/clnt_dg.c b/libtirpc/src/clnt_dg.c
174+
+	}
147-
index de318e6..801cf20 100644
175+
+
148-
--- a/libtirpc/src/clnt_dg.c
176+
+	if (n == NPORTS) {
149-
+++ b/libtirpc/src/clnt_dg.c
177+
+		warnx("bindresvport_sa(sd=%d,sa=0x%p): "
150-
@@ -275,7 +275,7 @@ clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
178+
+			"n(=%d) == NPORTS(=%d), "
151-
 	cu->cu_xdrpos = XDR_GETPOS(&(cu->cu_outxdrs));
179+
+			"no reserved port available\n", n, NPORTS);
152-
180+
+		res = 1;
153-
 	/* XXX fvdl - do we still want this? */
181+
+		goto out;
154-
-#if 0
182+
+	}
155-
+#if 1
183+
+
156-
 	(void)bindresvport_sa(fd, (struct sockaddr *)svcaddr->buf);
184+
+	BRP_D((void)fprintf(stdout, "bindresvport_sa(sd=%d,sa=0x%p): "
157-
 #endif
185+
+		"SIO_ACQUIRE_PORT_RESERVATION succeeded, "
158-
 #ifdef IP_RECVERR
186+
+		"bytesReturned = %u, StartPort=%d, NumberOfPorts=%d, "
159-
187+
+		"Token=0x%llx\n",
160-
188+
+		sd, sa, bytesReturned, (int)ntohs(portRes.StartPort),
161-
189+
+		portRes.NumberOfPorts, (long long)portRes.Token));
162-
190+
+
163-
191+
+	bytesReturned = 0;
164-
192+
+	ioctlres = WSAIoctl(sd_sock, SIO_ASSOCIATE_PORT_RESERVATION,
165-
193+
+		(LPVOID)&portRes.Token, sizeof(ULONG64), NULL, 0,
166-
194+
+		&bytesReturned, NULL, NULL);
167-
195+
+	lasterr = WSAGetLastError();
168-
196+
+	if (ioctlres != 0) {
169-
197+
+		warnx("bindresvport_sa(sd=%d,sa=0x%p): "
170-
198+
+			"WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) "
171-
199+
+			"failed with error = %d\n",
172-
200+
+			sd, sa, lasterr);
173-
201+
+		res = 1;
174-
202+
+		goto out;
175-
203+
+	}
176-
204+
+
177-
205+
+	BRP_D((void)fprintf(stdout, "bindresvport_sa(sd=%d,sa=0x%p): "
178-
206+
+		"WSAIoctl(SIO_ASSOCIATE_PORT_RESERVATION) succeeded, "
179-
207+
+		"bytesReturned = %u\n",
180-
208+
+		sd, sa, bytesReturned));
181-
209+
+	res = 0;
182-
210+
+
183-
211+
+	/*
184-
212+
+	 * FIXME: We should call |SIO_RELEASE_PORT_RESERVATION|,
185-
213+
+	 * but we cannot do that while |sd| is open and using the
186-
214+
+	 * reservation.
187-
215+
+	 * So basically we to store the token, and then use a second
188-
216+
+	 * socket, with matching protocol&co attributes, just to
189-
217+
+	 * release the reservation.
190-
218+
+	 *
191-
219+
+	 * A possible solution might be to derive a "control socket"
192-
220+
+	 * from |sd|, and do the reservation ioctl using that socket.
193-
221+
+	 *
194-
222+
+	 * For now we ignore this, and assume noone will do more
195-
223+
+	 * than |NPORTS| { mount, umount }-sequences during
196-
224+
+	 * nfsd.exe/nfsd_debug.exe lifetime
197-
225+
+	 */
198-
226+
+out:
199-
227+
+	BRP_D((void)fprintf(stdout,
200-
228+
+		"<-- bindresvport_sa(sd=%d,sa=0x%p) returning res=%d\n",
201-
229+
+		sd, sa, res));
202-
230+
+	return res;
203-
231+
 }
204-
232+
 #else
205-
233+
--------------------------*/
206-
234+
+
207-
235+
 #define IP_PORTRANGE 19
208-
236+
 #define IP_PORTRANGE_LOW 2
209237
210-
238+
@@ -174,29 +314,16 @@ bindresvport_sa(sd, sa)
211-
239+
 	int proto, portrange, portlow;
212-
240+
 	u_int16_t *portp;
213-
241+
 	socklen_t salen;
214-
242+
-#ifdef _WIN32
215-
243+
-		WSAPROTOCOL_INFO proto_info;
216-
244+
-		int proto_info_size = sizeof(proto_info);
217-
245+
-#endif
218246
219-
247+
 	if (sa == NULL) {
220-
248+
 		salen = sizeof(myaddr);
221-
249+
 		sa = (struct sockaddr *)&myaddr;
222250
223-
251+
-#ifdef _WIN32
224-
252+
-		memset(sa, 0, salen);
225-
253+
-		if (error = wintirpc_getsockopt(sd, SOL_SOCKET, SO_PROTOCOL_INFO, (char *)&proto_info, &proto_info_size) == SOCKET_ERROR) {
226-
254+
-			int sockerr = WSAGetLastError();
227-
255+
-			return -1;
228-
256+
-		}
229-
257+
-		af = proto_info.iAddressFamily;
230-
258+
-#else
231-
259+
 		if (wintirpc_getsockname(sd, sa, &salen) == -1)
232-
260+
 			return -1;	/* errno is correctly set */
233261
234-
262+
 		af = sa->sa_family;
235-
263+
 		memset(sa, 0, salen);
236-
264+
-#endif
237-
265+
 	} else
238-
266+
 		af = sa->sa_family;
239267
240-
268+
@@ -268,7 +395,4 @@ bindresvport_sa(sd, sa)
241-
269+
 #endif
242-
270+
 	return (error);
243-
271+
 }
244-
272+
-/*
245-
273+
-#endif
246-
274+
-*/
247-
275+
 #endif
248276

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at