1    	
2    	/*
3    	 * Copyright (c) 2009, Sun Microsystems, Inc.
4    	 * All rights reserved.
5    	 *
6    	 * Redistribution and use in source and binary forms, with or without
7    	 * modification, are permitted provided that the following conditions are met:
8    	 * - Redistributions of source code must retain the above copyright notice,
9    	 *   this list of conditions and the following disclaimer.
10   	 * - Redistributions in binary form must reproduce the above copyright notice,
11   	 *   this list of conditions and the following disclaimer in the documentation
12   	 *   and/or other materials provided with the distribution.
13   	 * - Neither the name of Sun Microsystems, Inc. nor the names of its
14   	 *   contributors may be used to endorse or promote products derived
15   	 *   from this software without specific prior written permission.
16   	 *
17   	 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   	 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   	 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   	 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21   	 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   	 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   	 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   	 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   	 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   	 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27   	 * POSSIBILITY OF SUCH DAMAGE.
28   	 */
29   	
30   	/*
31   	 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32   	 */
33   	
34   	/*
35   	 * svc_generic.c, Server side for RPC.
36   	 *
37   	 */
38   	#include "config.h"
39   	#include <pthread.h>
40   	#include <reentrant.h>
41   	#include <sys/types.h>
42   	#include <sys/socket.h>
43   	#include <netinet/in.h>
44   	#include <rpc/rpc.h>
45   	#include <rpc/nettype.h>
46   	#include <stdio.h>
47   	#include <errno.h>
48   	#include <stdlib.h>
49   	#include <string.h>
50   	#include <unistd.h>
51   	#include <err.h>
52   	
53   	#include "rpc_com.h"
54   	#include <rpc/svc.h>
55   	
56   	extern int __svc_vc_setflag(SVCXPRT *, int);
57   	
58   	/*
59   	 * The highest level interface for server creation.
60   	 * It tries for all the nettokens in that particular class of token
61   	 * and returns the number of handles it can create and/or find.
62   	 *
63   	 * It creates a link list of all the handles it could create.
64   	 * If svc_ncreate() is called multiple times, it uses the handle
65   	 * created earlier instead of creating a new handle every time.
66   	 */
67   	int
68   	svc_ncreate(void (*dispatch) (struct svc_req *req),
69   		    rpcprog_t prognum,
70   		    rpcvers_t versnum,	/* Version number */
71   		    const char *nettype /* Networktype token */)
72   	{
73   		struct xlist {
74   			SVCXPRT *xprt;	/* Server handle */
75   			struct xlist *next;	/* Next item */
76   		} *l;
77   		static struct xlist *xprtlist;	/* A link list of all the handles */
78   		int num = 0;
79   		SVCXPRT *xprt;
80   		struct netconfig *nconf;
81   		void *handle;
82   		extern mutex_t xprtlist_lock;
83   	
84   		/* VARIABLES PROTECTED BY xprtlist_lock: xprtlist */
85   		handle = __rpc_setconf(nettype);
86   		if (!handle) {
87   			__warnx(TIRPC_DEBUG_FLAG_SVC, "svc_ncreate: unknown protocol");
88   			return (0);
89   		}
90   		while ((nconf = __rpc_getconf(handle)) != NULL) {
91   			mutex_lock(&xprtlist_lock);
92   			for (l = xprtlist; l; l = l->next) {
93   				if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) {
94   					/* Found an old one, use it */
95   					(void)rpcb_unset(prognum, versnum, nconf);
96   					if (svc_reg
97   					    (l->xprt, prognum, versnum, dispatch,
98   					     nconf) == false)
99   						__warnx(TIRPC_DEBUG_FLAG_SVC,
100  							"svc_ncreate: could not register "
101  							"prog %u vers %u on %s",
102  							(unsigned)prognum,
103  							(unsigned)versnum,
104  							nconf->nc_netid);
105  					else
106  						num++;
107  					break;
108  				}
109  			}
110  			if (l == NULL) {
111  				/* It was not found. Now create a new one */
112  				xprt =
113  				    svc_tp_ncreate(dispatch, prognum, versnum, nconf);
114  				if (xprt) {
115  					l = (struct xlist *)mem_alloc(sizeof(*l));
116  					l->xprt = xprt;
117  					l->next = xprtlist;
118  					xprtlist = l;
119  					num++;
120  				}
121  			}
122  			mutex_unlock(&xprtlist_lock);
123  		}
124  		__rpc_endconf(handle);
125  		/*
126  		 * In case of num == 0; the error messages are generated by the
127  		 * underlying layers; and hence not needed here.
128  		 */
129  		return (num);
130  	}
131  	
132  	/*
133  	 * The high level interface to svc_tli_ncreate().
134  	 * It tries to create a server for "nconf" and registers the service
135  	 * with the rpcbind. It calls svc_tli_ncreate();
136  	 */
137  	SVCXPRT *
138  	svc_tp_ncreate(void (*dispatch) (struct svc_req *req),
139  		       rpcprog_t prognum,	/* Program number */
140  		       rpcvers_t versnum,	/* Version number */
141  		       const struct netconfig *nconf /* Network */)
142  	{
143  		SVCXPRT *xprt;
144  	
145  		if (nconf == NULL) {
146  			__warnx(TIRPC_DEBUG_FLAG_SVC,
147  				"svc_tp_ncreate: invalid netconfig structure for prog %u "
148  				"vers %u", (unsigned)prognum, (unsigned)versnum);
149  			return (NULL);
150  		}
151  		xprt = svc_tli_ncreate(RPC_ANYFD, nconf, NULL, 0, 0);
152  		if (xprt == NULL)
153  			return (NULL);
154  		/*LINTED const castaway */
155  		(void)rpcb_unset(prognum, versnum, (struct netconfig *)nconf);
156  		if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == false) {
157  			__warnx(TIRPC_DEBUG_FLAG_SVC,
158  				"svc_tp_ncreate: Could not register prog %u vers %u on %s",
159  				(unsigned)prognum, (unsigned)versnum, nconf->nc_netid);
160  			SVC_DESTROY(xprt);
161  			return (NULL);
162  		}
163  	
164  		return (xprt);
165  	}
166  	
167  	/*
168  	 * If fd is RPC_ANYFD, then it opens a fd for the given transport
169  	 * provider (nconf cannot be NULL then). If the t_state is T_UNBND and
170  	 * bindaddr is NON-NULL, it performs a t_bind using the bindaddr. For
171  	 * NULL bindadr and Connection oriented transports, the value of qlen
172  	 * is set to 8.
173  	 *
174  	 * If sendsz or recvsz are zero, their default values are chosen.
175  	 */
176  	SVCXPRT *
177  	svc_tli_ncreate(int fd,	/* Connection end point */
178  			const struct netconfig *nconf,	/* Nettoken */
179  			const struct t_bind *bindaddr,	/* Local bind address */
180  			u_int sendsz,	/* Max sendsize */
181  			u_int recvsz /* Max recvsize */)
182  	{
(1) Event assignment: Assigning: "xprt" = "NULL".
Also see events: [cond_null][cond_null][null][dead_error_condition][dead_error_line]
183  		SVCXPRT *xprt = NULL;	/* service handle */
184  		bool madefd = false;	/* whether fd opened here  */
185  		struct __rpc_sockinfo si;
186  		struct sockaddr_storage ss;
187  		socklen_t slen;
188  	
189  		if (fd == RPC_ANYFD) {
190  			if (nconf == NULL) {
191  				__warnx(TIRPC_DEBUG_FLAG_SVC,
192  					"svc_tli_ncreate: invalid netconfig");
193  				return (NULL);
194  			}
195  			fd = __rpc_nconf2fd(nconf);
196  			if (fd == -1) {
197  				__warnx(TIRPC_DEBUG_FLAG_SVC,
198  					"svc_tli_ncreate: could not open connection for %s",
199  					nconf->nc_netid);
200  				return (NULL);
201  			}
202  	
203  			if (!__rpc_nconf2sockinfo(nconf, &si)) {
204  				__warnx(TIRPC_DEBUG_FLAG_SVC,
205  					"svc_tli_create: could not get netconfig information");
206  				close(fd);
207  				return (NULL);
208  			}
209  			madefd = true;
210  		} else {
211  			/*
212  			 * It is an open descriptor. Get the transport info.
213  			 */
214  			if (!__rpc_fd2sockinfo(fd, &si)) {
215  				__warnx(TIRPC_DEBUG_FLAG_SVC,
216  					"svc_tli_create: could not get transport information");
217  				return (NULL);
218  			}
219  		}
220  	
221  		/*
222  		 * If the fd is unbound, try to bind it.
223  		 */
224  		if (madefd || !__rpc_sockisbound(fd)) {
225  			if (bindaddr == NULL) {
226  				if (bindresvport(fd, NULL) < 0) {
227  					memset(&ss, 0, sizeof(ss));
228  					ss.ss_family = si.si_af;
229  					if (bind
230  					    (fd, (struct sockaddr *)(void *)&ss,
231  					     (socklen_t) si.si_alen) < 0) {
232  						__warnx(TIRPC_DEBUG_FLAG_SVC,
233  							"svc_tli_ncreate: could not bind to "
234  							"anonymous port");
235  						goto freedata;
236  					}
237  				}
238  				listen(fd, SOMAXCONN);
239  			} else {
240  				if (bind
241  				    (fd, (struct sockaddr *)bindaddr->addr.buf,
242  				     (socklen_t) si.si_alen) < 0) {
243  					__warnx(TIRPC_DEBUG_FLAG_SVC,
244  						"svc_tli_ncreate: could not bind to requested "
245  						"address");
246  					goto freedata;
247  				}
248  				listen(fd, (int)bindaddr->qlen);
249  			}
250  	
251  		}
252  		/*
253  		 * call transport specific function.
254  		 */
255  		switch (si.si_socktype) {
256  		case SOCK_STREAM:
257  			slen = sizeof(ss);
258  			if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen)
259  			    == 0) {
260  				/* accepted socket */
261  				xprt = svc_fd_ncreate(fd, sendsz, recvsz);
262  			} else
263  				xprt = svc_vc_ncreate(fd, sendsz, recvsz);
(2) Event cond_null: Condition "xprt", taking false branch. Now the value of "xprt" is "NULL".
Also see events: [assignment][cond_null][null][dead_error_condition][dead_error_line]
264  			if (!nconf || !xprt)
265  				break;
266  	#if 0
267  			/* XXX fvdl *//* XXXX check matt */
268  			if (strcmp(nconf->nc_protofmly, "inet") == 0
269  			    || strcmp(nconf->nc_protofmly, "inet6") == 0)
270  				(void)__svc_vc_setflag(xprt, true);
271  	#endif
272  			break;
273  		case SOCK_DGRAM:
274  			xprt = svc_dg_ncreate(fd, sendsz, recvsz);
275  			break;
276  		default:
277  			__warnx(TIRPC_DEBUG_FLAG_SVC,
278  				"svc_tli_ncreate: bad service type");
279  			goto freedata;
280  		}
281  	
(3) Event cond_null: Condition "xprt == NULL", taking true branch. Now the value of "xprt" is "NULL".
Also see events: [assignment][cond_null][null][dead_error_condition][dead_error_line]
282  		if (xprt == NULL)
283  			/*
284  			 * The error messages here are produced by the lower layers:
285  			 * svc_vc_ncreate(), svc_fd_ncreate() and svc_dg_ncreate().
286  			 */
287  			goto freedata;
288  	
289  		/* Fill in type of service */
290  		xprt->xp_si_type = __rpc_socktype2seman(si.si_socktype);
291  	
292  		if (nconf) {
293  			xprt->xp_netid = mem_strdup(nconf->nc_netid);
294  			xprt->xp_tp = mem_strdup(nconf->nc_device);
295  		}
296  		return (xprt);
297  	
298  	 freedata:
299  		if (madefd)
300  			(void)close(fd);
(4) Event null: At condition "xprt", the value of "xprt" must be "NULL".
(5) Event dead_error_condition: The condition "xprt" cannot be true.
Also see events: [assignment][cond_null][cond_null][dead_error_line]
301  		if (xprt) {
(6) Event dead_error_line: Execution cannot reach this statement: "if (!madefd) xprt->xp_fd ...".
Also see events: [assignment][cond_null][cond_null][null][dead_error_condition]
302  			if (!madefd)	/* so that svc_destroy doesnt close fd */
303  				xprt->xp_fd = RPC_ANYFD;
304  			SVC_DESTROY(xprt);
305  		}
306  		return (NULL);
307  	}
308