| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <pthread.h> |
| 39 |
#include <reentrant.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <sys/socket.h> |
| 42 |
#include <netinet/in.h> |
| 43 |
#include <rpc/rpc.h> |
| 44 |
#include <rpc/nettype.h> |
| 45 |
#include <stdio.h> |
| 46 |
#include <errno.h> |
| 47 |
#include <stdlib.h> |
| 48 |
#include <string.h> |
| 49 |
#include <unistd.h> |
| 50 |
#include <err.h> |
| 51 |
|
| 52 |
#include "rpc_com.h" |
| 53 |
#include <rpc/svc.h> |
| 54 |
|
| 55 |
extern int __svc_vc_setflag(SVCXPRT *, int); |
| 56 |
extern int __binddynport(int fd); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
int |
| 68 |
svc_create(dispatch, prognum, versnum, nettype) |
| 69 |
void (*dispatch)(struct svc_req *, SVCXPRT *); |
| 70 |
rpcprog_t prognum; |
| 71 |
rpcvers_t versnum; |
| 72 |
const char *nettype; |
| 73 |
{ |
| 74 |
struct xlist { |
| 75 |
SVCXPRT *xprt; |
| 76 |
struct xlist *next; |
| 77 |
} *l; |
| 78 |
static struct xlist *xprtlist; |
| 79 |
int num = 0; |
| 80 |
SVCXPRT *xprt; |
| 81 |
struct netconfig *nconf; |
| 82 |
void *handle; |
| 83 |
extern mutex_t xprtlist_lock; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if ((handle = __rpc_setconf(nettype)) == NULL) { |
| 88 |
warnx("svc_create: unknown protocol"); |
| 89 |
return (0); |
| 90 |
} |
| 91 |
while ((nconf = __rpc_getconf(handle)) != NULL) { |
| 92 |
mutex_lock(&xprtlist_lock); |
| 93 |
for (l = xprtlist; l; l = l->next) { |
| 94 |
if (strcmp(l->xprt->xp_netid, nconf->nc_netid) == 0) { |
| 95 |
|
| 96 |
(void) rpcb_unset(prognum, versnum, nconf); |
| 97 |
if (svc_reg(l->xprt, prognum, versnum, |
| 98 |
dispatch, nconf) == FALSE) |
| 99 |
warnx( |
| 100 |
"svc_create: could not register prog %u vers %u on %s", |
| 101 |
(unsigned)prognum, (unsigned)versnum, |
| 102 |
nconf->nc_netid); |
| 103 |
else |
| 104 |
num++; |
| 105 |
break; |
| 106 |
} |
| 107 |
} |
| 108 |
if (l == NULL) { |
| 109 |
|
| 110 |
xprt = svc_tp_create(dispatch, prognum, versnum, nconf); |
| 111 |
if (xprt) { |
| 112 |
l = (struct xlist *)malloc(sizeof (*l)); |
| 113 |
if (l == NULL) { |
| 114 |
warnx("svc_create: no memory"); |
| 115 |
mutex_unlock(&xprtlist_lock); |
| 116 |
__rpc_endconf(handle); |
| 117 |
return (0); |
| 118 |
} |
| 119 |
l->xprt = xprt; |
| 120 |
l->next = xprtlist; |
| 121 |
xprtlist = l; |
| 122 |
num++; |
| 123 |
} |
| 124 |
} |
| 125 |
mutex_unlock(&xprtlist_lock); |
| 126 |
} |
| 127 |
__rpc_endconf(handle); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
return (num); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
SVCXPRT * |
| 141 |
svc_tp_create(dispatch, prognum, versnum, nconf) |
| 142 |
void (*dispatch)(struct svc_req *, SVCXPRT *); |
| 143 |
rpcprog_t prognum; |
| 144 |
rpcvers_t versnum; |
| 145 |
const struct netconfig *nconf; |
| 146 |
{ |
| 147 |
SVCXPRT *xprt; |
| 148 |
|
| 149 |
if (nconf == NULL) { |
| 150 |
warnx( |
| 151 |
"svc_tp_create: invalid netconfig structure for prog %u vers %u", |
| 152 |
(unsigned)prognum, (unsigned)versnum); |
| 153 |
return (NULL); |
| 154 |
} |
| 155 |
xprt = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0); |
| 156 |
if (xprt == NULL) { |
| 157 |
return (NULL); |
| 158 |
} |
| 159 |
|
| 160 |
(void) rpcb_unset(prognum, versnum, (struct netconfig *) nconf); |
| 161 |
if (svc_reg(xprt, prognum, versnum, dispatch, nconf) == FALSE) { |
| 162 |
warnx( |
| 163 |
"svc_tp_create: Could not register prog %u vers %u on %s", |
| 164 |
(unsigned)prognum, (unsigned)versnum, |
| 165 |
nconf->nc_netid); |
| 166 |
SVC_DESTROY(xprt); |
| 167 |
return (NULL); |
| 168 |
} |
| 169 |
return (xprt); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
SVCXPRT * |
| 182 |
svc_tli_create(fd, nconf, bindaddr, sendsz, recvsz) |
| 183 |
int fd; |
| 184 |
const struct netconfig *nconf; |
| 185 |
const struct t_bind *bindaddr; |
| 186 |
u_int sendsz; |
| 187 |
u_int recvsz; |
| 188 |
{ |
| 189 |
SVCXPRT *xprt = NULL; |
| 190 |
bool_t madefd = FALSE; |
| 191 |
struct __rpc_sockinfo si; |
| 192 |
struct sockaddr_storage ss; |
| 193 |
socklen_t slen; |
| 194 |
|
| 195 |
if (fd == RPC_ANYFD) { |
| 196 |
if (nconf == NULL) { |
| 197 |
warnx("svc_tli_create: invalid netconfig"); |
| 198 |
return (NULL); |
| 199 |
} |
| 200 |
fd = __rpc_nconf2fd(nconf); |
| 201 |
if (fd == -1) { |
| 202 |
warnx( |
| 203 |
"svc_tli_create: could not open connection for %s", |
| 204 |
nconf->nc_netid); |
| 205 |
return (NULL); |
| 206 |
} |
| 207 |
__rpc_nconf2sockinfo(nconf, &si); |
| 208 |
madefd = TRUE; |
| 209 |
} else { |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
if (!__rpc_fd2sockinfo(fd, &si)) { |
| 214 |
warnx( |
| 215 |
"svc_tli_create: could not get transport information"); |
| 216 |
return (NULL); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
if (madefd || !__rpc_sockisbound(fd)) { |
| 224 |
if (bindaddr == NULL) { |
| 225 |
if (__binddynport(fd) == -1) { |
| 226 |
warnx( |
| 227 |
"svc_tli_create: could not bind to anonymous port"); |
| 228 |
goto freedata; |
| 229 |
} |
| 230 |
listen(fd, SOMAXCONN); |
| 231 |
} else { |
| 232 |
if (bind(fd, |
| 233 |
(struct sockaddr *)bindaddr->addr.buf, |
| 234 |
(socklen_t)si.si_alen) < 0) { |
| 235 |
warnx( |
| 236 |
"svc_tli_create: could not bind to requested address"); |
| 237 |
goto freedata; |
| 238 |
} |
| 239 |
listen(fd, (int)bindaddr->qlen); |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
switch (si.si_socktype) { |
| 247 |
case SOCK_STREAM: |
| 248 |
slen = sizeof ss; |
| 249 |
if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) |
| 250 |
== 0) { |
| 251 |
|
| 252 |
xprt = svc_fd_create(fd, sendsz, recvsz); |
| 253 |
} else |
| 254 |
xprt = svc_vc_create(fd, sendsz, recvsz); |
| 255 |
if (!nconf || !xprt) |
| 256 |
break; |
| 257 |
#if 0 |
| 258 |
|
| 259 |
if (strcmp(nconf->nc_protofmly, "inet") == 0 || |
| 260 |
strcmp(nconf->nc_protofmly, "inet6") == 0) |
| 261 |
(void) __svc_vc_setflag(xprt, TRUE); |
| 262 |
#endif |
| 263 |
break; |
| 264 |
case SOCK_DGRAM: |
| 265 |
xprt = svc_dg_create(fd, sendsz, recvsz); |
| 266 |
break; |
| 267 |
default: |
| 268 |
warnx("svc_tli_create: bad service type"); |
| 269 |
goto freedata; |
| 270 |
} |
| 271 |
|
| 272 |
if (xprt == NULL) |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
goto freedata; |
| 278 |
|
| 279 |
|
| 280 |
xprt->xp_type = __rpc_socktype2seman(si.si_socktype); |
| 281 |
|
| 282 |
if (nconf) { |
| 283 |
if (xprt->xp_netid != NULL) |
| 284 |
free(xprt->xp_netid); |
| 285 |
xprt->xp_netid = strdup(nconf->nc_netid); |
| 286 |
xprt->xp_tp = strdup(nconf->nc_device); |
| 287 |
} |
| 288 |
return (xprt); |
| 289 |
|
| 290 |
freedata: |
| 291 |
if (madefd) |
| 292 |
(void)close(fd); |
| 293 |
if (xprt) { |
| 294 |
if (!madefd) |
| 295 |
xprt->xp_fd = RPC_ANYFD; |
| 296 |
SVC_DESTROY(xprt); |
| 297 |
} |
| 298 |
return (NULL); |
| 299 |
} |