| 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 |
#include <pthread.h> |
| 38 |
#include <reentrant.h> |
| 39 |
#include <sys/types.h> |
| 40 |
#include <sys/param.h> |
| 41 |
#include <sys/socket.h> |
| 42 |
#include <sys/time.h> |
| 43 |
#include <sys/un.h> |
| 44 |
#include <sys/resource.h> |
| 45 |
#include <netinet/in.h> |
| 46 |
#include <arpa/inet.h> |
| 47 |
#include <rpc/rpc.h> |
| 48 |
#include <ctype.h> |
| 49 |
#include <stddef.h> |
| 50 |
#include <stdio.h> |
| 51 |
#include <netdb.h> |
| 52 |
#include <netconfig.h> |
| 53 |
#include <stdlib.h> |
| 54 |
#include <string.h> |
| 55 |
#include <syslog.h> |
| 56 |
#include <rpc/nettype.h> |
| 57 |
#include "rpc_com.h" |
| 58 |
|
| 59 |
struct handle { |
| 60 |
NCONF_HANDLE *nhandle; |
| 61 |
int nflag; |
| 62 |
int nettype; |
| 63 |
}; |
| 64 |
|
| 65 |
static const struct _rpcnettype { |
| 66 |
const char *name; |
| 67 |
const int type; |
| 68 |
} _rpctypelist[] = { |
| 69 |
{ "netpath", _RPC_NETPATH }, |
| 70 |
{ "visible", _RPC_VISIBLE }, |
| 71 |
{ "circuit_v", _RPC_CIRCUIT_V }, |
| 72 |
{ "datagram_v", _RPC_DATAGRAM_V }, |
| 73 |
{ "circuit_n", _RPC_CIRCUIT_N }, |
| 74 |
{ "datagram_n", _RPC_DATAGRAM_N }, |
| 75 |
{ "tcp", _RPC_TCP }, |
| 76 |
{ "udp", _RPC_UDP }, |
| 77 |
{ 0, _RPC_NONE } |
| 78 |
}; |
| 79 |
|
| 80 |
struct netid_af { |
| 81 |
const char *netid; |
| 82 |
int af; |
| 83 |
int protocol; |
| 84 |
}; |
| 85 |
|
| 86 |
static const struct netid_af na_cvt[] = { |
| 87 |
{ "udp", AF_INET, IPPROTO_UDP }, |
| 88 |
{ "tcp", AF_INET, IPPROTO_TCP }, |
| 89 |
#ifdef INET6 |
| 90 |
{ "udp6", AF_INET6, IPPROTO_UDP }, |
| 91 |
{ "tcp6", AF_INET6, IPPROTO_TCP }, |
| 92 |
#endif |
| 93 |
{ "local", AF_LOCAL, 0 } |
| 94 |
}; |
| 95 |
|
| 96 |
#if 0 |
| 97 |
static char *strlocase(char *); |
| 98 |
#endif |
| 99 |
static int getnettype(const char *); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
int |
| 106 |
__rpc_dtbsize() |
| 107 |
{ |
| 108 |
static int tbsize; |
| 109 |
struct rlimit rl; |
| 110 |
|
| 111 |
if (tbsize) { |
| 112 |
return (tbsize); |
| 113 |
} |
| 114 |
if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { |
| 115 |
return (tbsize = (int)rl.rlim_cur); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
return (32); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
u_int |
| 129 |
|
| 130 |
__rpc_get_t_size(af, proto, size) |
| 131 |
int af, proto; |
| 132 |
int size; |
| 133 |
{ |
| 134 |
int maxsize, defsize; |
| 135 |
|
| 136 |
maxsize = 256 * 1024; |
| 137 |
switch (proto) { |
| 138 |
case IPPROTO_TCP: |
| 139 |
defsize = 64 * 1024; |
| 140 |
break; |
| 141 |
case IPPROTO_UDP: |
| 142 |
defsize = UDPMSGSIZE; |
| 143 |
break; |
| 144 |
default: |
| 145 |
defsize = RPC_MAXDATASIZE; |
| 146 |
break; |
| 147 |
} |
| 148 |
if (size == 0) |
| 149 |
return defsize; |
| 150 |
|
| 151 |
|
| 152 |
return (size > maxsize ? (u_int)maxsize : (u_int)size); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
u_int |
| 159 |
__rpc_get_a_size(af) |
| 160 |
int af; |
| 161 |
{ |
| 162 |
switch (af) { |
| 163 |
case AF_INET: |
| 164 |
return sizeof (struct sockaddr_in); |
| 165 |
#ifdef INET6 |
| 166 |
case AF_INET6: |
| 167 |
return sizeof (struct sockaddr_in6); |
| 168 |
#endif |
| 169 |
case AF_LOCAL: |
| 170 |
return sizeof (struct sockaddr_un); |
| 171 |
default: |
| 172 |
break; |
| 173 |
} |
| 174 |
return ((u_int)RPC_MAXADDRSIZE); |
| 175 |
} |
| 176 |
|
| 177 |
#if 0 |
| 178 |
static char * |
| 179 |
strlocase(p) |
| 180 |
char *p; |
| 181 |
{ |
| 182 |
char *t = p; |
| 183 |
|
| 184 |
for (; *p; p++) |
| 185 |
if (isupper(*p)) |
| 186 |
*p = tolower(*p); |
| 187 |
return (t); |
| 188 |
} |
| 189 |
#endif |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
static int |
| 196 |
getnettype(nettype) |
| 197 |
const char *nettype; |
| 198 |
{ |
| 199 |
int i; |
| 200 |
|
| 201 |
if ((nettype == NULL) || (nettype[0] == 0)) { |
| 202 |
return (_RPC_NETPATH); |
| 203 |
} |
| 204 |
|
| 205 |
#if 0 |
| 206 |
nettype = strlocase(nettype); |
| 207 |
#endif |
| 208 |
for (i = 0; _rpctypelist[i].name; i++) |
| 209 |
if (strcasecmp(nettype, _rpctypelist[i].name) == 0) { |
| 210 |
return (_rpctypelist[i].type); |
| 211 |
} |
| 212 |
return (_rpctypelist[i].type); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
struct netconfig * |
| 221 |
__rpc_getconfip(nettype) |
| 222 |
const char *nettype; |
| 223 |
{ |
| 224 |
char *netid; |
| 225 |
char *netid_tcp = (char *) NULL; |
| 226 |
char *netid_udp = (char *) NULL; |
| 227 |
struct netconfig *dummy; |
| 228 |
extern thread_key_t tcp_key, udp_key; |
| 229 |
extern mutex_t tsd_lock; |
| 230 |
|
| 231 |
if (tcp_key == KEY_INITIALIZER) { |
| 232 |
mutex_lock(&tsd_lock); |
| 233 |
if (tcp_key == KEY_INITIALIZER) |
| 234 |
thr_keycreate(&tcp_key, free); |
| 235 |
mutex_unlock(&tsd_lock); |
| 236 |
} |
| 237 |
netid_tcp = (char *)thr_getspecific(tcp_key); |
| 238 |
if (udp_key == KEY_INITIALIZER) { |
| 239 |
mutex_lock(&tsd_lock); |
| 240 |
if (udp_key == KEY_INITIALIZER) |
| 241 |
thr_keycreate(&udp_key, free); |
| 242 |
mutex_unlock(&tsd_lock); |
| 243 |
} |
| 244 |
netid_udp = (char *)thr_getspecific(udp_key); |
| 245 |
if (!netid_udp && !netid_tcp) { |
| 246 |
struct netconfig *nconf; |
| 247 |
void *confighandle; |
| 248 |
|
| 249 |
if (!(confighandle = setnetconfig())) { |
| 250 |
syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); |
| 251 |
return (NULL); |
| 252 |
} |
| 253 |
while ((nconf = getnetconfig(confighandle)) != NULL) { |
| 254 |
if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { |
| 255 |
if (strcmp(nconf->nc_proto, NC_TCP) == 0 && |
| 256 |
netid_tcp == NULL) { |
| 257 |
netid_tcp = strdup(nconf->nc_netid); |
| 258 |
thr_setspecific(tcp_key, |
| 259 |
(void *) netid_tcp); |
| 260 |
} else |
| 261 |
if (strcmp(nconf->nc_proto, NC_UDP) == 0 && |
| 262 |
netid_udp == NULL) { |
| 263 |
netid_udp = strdup(nconf->nc_netid); |
| 264 |
thr_setspecific(udp_key, |
| 265 |
(void *) netid_udp); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
endnetconfig(confighandle); |
| 270 |
} |
| 271 |
if (strcmp(nettype, "udp") == 0) |
| 272 |
netid = netid_udp; |
| 273 |
else if (strcmp(nettype, "tcp") == 0) |
| 274 |
netid = netid_tcp; |
| 275 |
else { |
| 276 |
return (NULL); |
| 277 |
} |
| 278 |
if ((netid == NULL) || (netid[0] == 0)) { |
| 279 |
return (NULL); |
| 280 |
} |
| 281 |
dummy = getnetconfigent(netid); |
| 282 |
return (dummy); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
void * |
| 290 |
__rpc_setconf(nettype) |
| 291 |
const char *nettype; |
| 292 |
{ |
| 293 |
struct handle *handle; |
| 294 |
|
| 295 |
handle = (struct handle *) malloc(sizeof (struct handle)); |
| 296 |
if (handle == NULL) { |
| 297 |
return (NULL); |
| 298 |
} |
| 299 |
switch (handle->nettype = getnettype(nettype)) { |
| 300 |
case _RPC_NETPATH: |
| 301 |
case _RPC_CIRCUIT_N: |
| 302 |
case _RPC_DATAGRAM_N: |
| 303 |
if (!(handle->nhandle = setnetpath())) { |
| 304 |
free(handle); |
| 305 |
return (NULL); |
| 306 |
} |
| 307 |
handle->nflag = TRUE; |
| 308 |
break; |
| 309 |
case _RPC_VISIBLE: |
| 310 |
case _RPC_CIRCUIT_V: |
| 311 |
case _RPC_DATAGRAM_V: |
| 312 |
case _RPC_TCP: |
| 313 |
case _RPC_UDP: |
| 314 |
if (!(handle->nhandle = setnetconfig())) { |
| 315 |
syslog (LOG_ERR, "rpc: failed to open " NETCONFIG); |
| 316 |
free(handle); |
| 317 |
return (NULL); |
| 318 |
} |
| 319 |
handle->nflag = FALSE; |
| 320 |
break; |
| 321 |
default: |
| 322 |
free(handle); |
| 323 |
return (NULL); |
| 324 |
} |
| 325 |
|
| 326 |
return (handle); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
struct netconfig * |
| 334 |
__rpc_getconf(vhandle) |
| 335 |
void *vhandle; |
| 336 |
{ |
| 337 |
struct handle *handle; |
| 338 |
struct netconfig *nconf; |
| 339 |
|
| 340 |
handle = (struct handle *)vhandle; |
| 341 |
if (handle == NULL) { |
| 342 |
return (NULL); |
| 343 |
} |
| 344 |
for (;;) { |
| 345 |
if (handle->nflag) |
| 346 |
nconf = getnetpath(handle->nhandle); |
| 347 |
else |
| 348 |
nconf = getnetconfig(handle->nhandle); |
| 349 |
if (nconf == NULL) |
| 350 |
break; |
| 351 |
if ((nconf->nc_semantics != NC_TPI_CLTS) && |
| 352 |
(nconf->nc_semantics != NC_TPI_COTS) && |
| 353 |
(nconf->nc_semantics != NC_TPI_COTS_ORD)) |
| 354 |
continue; |
| 355 |
switch (handle->nettype) { |
| 356 |
case _RPC_VISIBLE: |
| 357 |
if (!(nconf->nc_flag & NC_VISIBLE)) |
| 358 |
continue; |
| 359 |
|
| 360 |
case _RPC_NETPATH: |
| 361 |
break; |
| 362 |
case _RPC_CIRCUIT_V: |
| 363 |
if (!(nconf->nc_flag & NC_VISIBLE)) |
| 364 |
continue; |
| 365 |
|
| 366 |
case _RPC_CIRCUIT_N: |
| 367 |
if ((nconf->nc_semantics != NC_TPI_COTS) && |
| 368 |
(nconf->nc_semantics != NC_TPI_COTS_ORD)) |
| 369 |
continue; |
| 370 |
break; |
| 371 |
case _RPC_DATAGRAM_V: |
| 372 |
if (!(nconf->nc_flag & NC_VISIBLE)) |
| 373 |
continue; |
| 374 |
|
| 375 |
case _RPC_DATAGRAM_N: |
| 376 |
if (nconf->nc_semantics != NC_TPI_CLTS) |
| 377 |
continue; |
| 378 |
break; |
| 379 |
case _RPC_TCP: |
| 380 |
if (((nconf->nc_semantics != NC_TPI_COTS) && |
| 381 |
(nconf->nc_semantics != NC_TPI_COTS_ORD)) || |
| 382 |
(strcmp(nconf->nc_protofmly, NC_INET) |
| 383 |
#ifdef INET6 |
| 384 |
&& strcmp(nconf->nc_protofmly, NC_INET6)) |
| 385 |
#else |
| 386 |
) |
| 387 |
#endif |
| 388 |
|| |
| 389 |
strcmp(nconf->nc_proto, NC_TCP)) |
| 390 |
continue; |
| 391 |
break; |
| 392 |
case _RPC_UDP: |
| 393 |
if ((nconf->nc_semantics != NC_TPI_CLTS) || |
| 394 |
(strcmp(nconf->nc_protofmly, NC_INET) |
| 395 |
#ifdef INET6 |
| 396 |
&& strcmp(nconf->nc_protofmly, NC_INET6)) |
| 397 |
#else |
| 398 |
) |
| 399 |
#endif |
| 400 |
|| |
| 401 |
strcmp(nconf->nc_proto, NC_UDP)) |
| 402 |
continue; |
| 403 |
break; |
| 404 |
} |
| 405 |
break; |
| 406 |
} |
| 407 |
return (nconf); |
| 408 |
} |
| 409 |
|
| 410 |
void |
| 411 |
__rpc_endconf(vhandle) |
| 412 |
void * vhandle; |
| 413 |
{ |
| 414 |
struct handle *handle; |
| 415 |
|
| 416 |
handle = (struct handle *) vhandle; |
| 417 |
if (handle == NULL) { |
| 418 |
return; |
| 419 |
} |
| 420 |
if (handle->nflag) { |
| 421 |
endnetpath(handle->nhandle); |
| 422 |
} else { |
| 423 |
endnetconfig(handle->nhandle); |
| 424 |
} |
| 425 |
free(handle); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
void * |
| 433 |
rpc_nullproc(clnt) |
| 434 |
CLIENT *clnt; |
| 435 |
{ |
| 436 |
struct timeval TIMEOUT = {25, 0}; |
| 437 |
|
| 438 |
if (clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void, NULL, |
| 439 |
(xdrproc_t) xdr_void, NULL, TIMEOUT) != RPC_SUCCESS) { |
| 440 |
return (NULL); |
| 441 |
} |
| 442 |
return ((void *) clnt); |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
struct netconfig * |
| 450 |
__rpcgettp(fd) |
| 451 |
int fd; |
| 452 |
{ |
| 453 |
const char *netid; |
| 454 |
struct __rpc_sockinfo si; |
| 455 |
|
| 456 |
if (!__rpc_fd2sockinfo(fd, &si)) |
| 457 |
return NULL; |
| 458 |
|
| 459 |
if (!__rpc_sockinfo2netid(&si, &netid)) |
| 460 |
return NULL; |
| 461 |
|
| 462 |
|
| 463 |
return getnetconfigent((char *)netid); |
| 464 |
} |
| 465 |
|
| 466 |
int |
| 467 |
__rpc_fd2sockinfo(int fd, struct __rpc_sockinfo *sip) |
| 468 |
{ |
| 469 |
socklen_t len; |
| 470 |
int type, proto; |
| 471 |
struct sockaddr_storage ss; |
| 472 |
|
| 473 |
len = sizeof ss; |
| 474 |
if (getsockname(fd, (struct sockaddr *)&ss, &len) < 0) |
| 475 |
return 0; |
| 476 |
sip->si_alen = len; |
| 477 |
|
| 478 |
len = sizeof type; |
| 479 |
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &type, &len) < 0) |
| 480 |
return 0; |
| 481 |
|
| 482 |
|
| 483 |
if (ss.ss_family != AF_LOCAL) { |
| 484 |
if (type == SOCK_STREAM) |
| 485 |
proto = IPPROTO_TCP; |
| 486 |
else if (type == SOCK_DGRAM) |
| 487 |
proto = IPPROTO_UDP; |
| 488 |
else |
| 489 |
return 0; |
| 490 |
} else |
| 491 |
proto = 0; |
| 492 |
|
| 493 |
sip->si_af = ss.ss_family; |
| 494 |
sip->si_proto = proto; |
| 495 |
sip->si_socktype = type; |
| 496 |
|
| 497 |
return 1; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
int |
| 504 |
__rpc_nconf2sockinfo(const struct netconfig *nconf, struct __rpc_sockinfo *sip) |
| 505 |
{ |
| 506 |
int i; |
| 507 |
|
| 508 |
for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) |
| 509 |
if (strcmp(na_cvt[i].netid, nconf->nc_netid) == 0 || ( |
| 510 |
strcmp(nconf->nc_netid, "unix") == 0 && |
| 511 |
strcmp(na_cvt[i].netid, "local") == 0)) { |
| 512 |
sip->si_af = na_cvt[i].af; |
| 513 |
sip->si_proto = na_cvt[i].protocol; |
| 514 |
sip->si_socktype = |
| 515 |
__rpc_seman2socktype((int)nconf->nc_semantics); |
| 516 |
if (sip->si_socktype == -1) |
| 517 |
return 0; |
| 518 |
sip->si_alen = __rpc_get_a_size(sip->si_af); |
| 519 |
return 1; |
| 520 |
} |
| 521 |
|
| 522 |
return 0; |
| 523 |
} |
| 524 |
|
| 525 |
int |
| 526 |
__rpc_nconf2fd_flags(const struct netconfig *nconf, int flags) |
| 527 |
{ |
| 528 |
struct __rpc_sockinfo si; |
| 529 |
int fd; |
| 530 |
|
| 531 |
if (!__rpc_nconf2sockinfo(nconf, &si)) |
| 532 |
return 0; |
| 533 |
|
| 534 |
if ((fd = socket(si.si_af, si.si_socktype | flags, si.si_proto)) >= 0 && |
| 535 |
si.si_af == AF_INET6) { |
| 536 |
int val = 1; |
| 537 |
|
| 538 |
#ifdef INET6 |
| 539 |
setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &val, sizeof(val)); |
| 540 |
#endif |
| 541 |
} |
| 542 |
return fd; |
| 543 |
} |
| 544 |
|
| 545 |
int |
| 546 |
__rpc_nconf2fd(const struct netconfig *nconf) |
| 547 |
{ |
| 548 |
return __rpc_nconf2fd_flags(nconf, 0); |
| 549 |
} |
| 550 |
|
| 551 |
int |
| 552 |
__rpc_sockinfo2netid(struct __rpc_sockinfo *sip, const char **netid) |
| 553 |
{ |
| 554 |
int i; |
| 555 |
struct netconfig *nconf; |
| 556 |
|
| 557 |
nconf = getnetconfigent("local"); |
| 558 |
|
| 559 |
for (i = 0; i < (sizeof na_cvt) / (sizeof (struct netid_af)); i++) { |
| 560 |
if (na_cvt[i].af == sip->si_af && |
| 561 |
na_cvt[i].protocol == sip->si_proto) { |
| 562 |
if (strcmp(na_cvt[i].netid, "local") == 0 && nconf == NULL) { |
| 563 |
if (netid) |
| 564 |
*netid = "unix"; |
| 565 |
} else { |
| 566 |
if (netid) |
| 567 |
*netid = na_cvt[i].netid; |
| 568 |
} |
| 569 |
if (nconf != NULL) |
| 570 |
freenetconfigent(nconf); |
| 571 |
return 1; |
| 572 |
} |
| 573 |
} |
| 574 |
if (nconf != NULL) |
| 575 |
freenetconfigent(nconf); |
| 576 |
|
| 577 |
return 0; |
| 578 |
} |
| 579 |
|
| 580 |
char * |
| 581 |
taddr2uaddr(const struct netconfig *nconf, const struct netbuf *nbuf) |
| 582 |
{ |
| 583 |
struct __rpc_sockinfo si; |
| 584 |
|
| 585 |
if (!__rpc_nconf2sockinfo(nconf, &si)) |
| 586 |
return NULL; |
| 587 |
return __rpc_taddr2uaddr_af(si.si_af, nbuf); |
| 588 |
} |
| 589 |
|
| 590 |
struct netbuf * |
| 591 |
uaddr2taddr(const struct netconfig *nconf, const char *uaddr) |
| 592 |
{ |
| 593 |
struct __rpc_sockinfo si; |
| 594 |
|
| 595 |
if (!__rpc_nconf2sockinfo(nconf, &si)) |
| 596 |
return NULL; |
| 597 |
return __rpc_uaddr2taddr_af(si.si_af, uaddr); |
| 598 |
} |
| 599 |
|
| 600 |
char * |
| 601 |
__rpc_taddr2uaddr_af(int af, const struct netbuf *nbuf) |
| 602 |
{ |
| 603 |
char *ret; |
| 604 |
struct sockaddr_in *sin; |
| 605 |
struct sockaddr_un *sun; |
| 606 |
char namebuf[INET_ADDRSTRLEN]; |
| 607 |
#ifdef INET6 |
| 608 |
struct sockaddr_in6 *sin6; |
| 609 |
char namebuf6[INET6_ADDRSTRLEN]; |
| 610 |
#endif |
| 611 |
int path_len; |
| 612 |
u_int16_t port; |
| 613 |
|
| 614 |
if (nbuf->len <= 0) |
| 615 |
return NULL; |
| 616 |
|
| 617 |
switch (af) { |
| 618 |
case AF_INET: |
| 619 |
if (nbuf->len < sizeof(*sin)) { |
| 620 |
return NULL; |
| 621 |
} |
| 622 |
sin = nbuf->buf; |
| 623 |
if (inet_ntop(af, &sin->sin_addr, namebuf, sizeof namebuf) |
| 624 |
== NULL) |
| 625 |
return NULL; |
| 626 |
port = ntohs(sin->sin_port); |
| 627 |
if (asprintf(&ret, "%s.%u.%u", namebuf, ((u_int32_t)port) >> 8, |
| 628 |
port & 0xff) < 0) |
| 629 |
return NULL; |
| 630 |
break; |
| 631 |
#ifdef INET6 |
| 632 |
case AF_INET6: |
| 633 |
if (nbuf->len < sizeof(*sin6)) { |
| 634 |
return NULL; |
| 635 |
} |
| 636 |
sin6 = nbuf->buf; |
| 637 |
if (inet_ntop(af, &sin6->sin6_addr, namebuf6, sizeof namebuf6) |
| 638 |
== NULL) |
| 639 |
return NULL; |
| 640 |
port = ntohs(sin6->sin6_port); |
| 641 |
if (asprintf(&ret, "%s.%u.%u", namebuf6, ((u_int32_t)port) >> 8, |
| 642 |
port & 0xff) < 0) |
| 643 |
return NULL; |
| 644 |
break; |
| 645 |
#endif |
| 646 |
case AF_LOCAL: |
| 647 |
sun = nbuf->buf; |
| 648 |
|
| 649 |
path_len = nbuf->len - offsetof(struct sockaddr_un, sun_path); |
| 650 |
if (path_len < 0) |
| 651 |
return NULL; |
| 652 |
|
| 653 |
if (asprintf(&ret, "%c%.*s", sun->sun_path[0] ?: '@', |
| 654 |
path_len - 1, sun->sun_path + 1) < 0) |
| 655 |
return (NULL); |
| 656 |
break; |
| 657 |
default: |
| 658 |
return NULL; |
| 659 |
} |
| 660 |
|
| 661 |
return ret; |
| 662 |
} |
| 663 |
|
| 664 |
struct netbuf * |
| 665 |
__rpc_uaddr2taddr_af(int af, const char *uaddr) |
| 666 |
{ |
| 667 |
struct netbuf *ret = NULL; |
| 668 |
char *addrstr, *p; |
| 669 |
unsigned port, portlo, porthi; |
| 670 |
struct sockaddr_in *sin; |
| 671 |
#ifdef INET6 |
| 672 |
struct sockaddr_in6 *sin6; |
| 673 |
#endif |
| 674 |
struct sockaddr_un *sun; |
| 675 |
|
| 676 |
port = 0; |
| 677 |
sin = NULL; |
| 678 |
if (uaddr == NULL) |
| 679 |
return NULL; |
| 680 |
addrstr = strdup(uaddr); |
| 681 |
if (addrstr == NULL) |
| 682 |
return NULL; |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
if (*addrstr != '/' && *addrstr != '@') { |
| 690 |
p = strrchr(addrstr, '.'); |
| 691 |
if (p == NULL) |
| 692 |
goto out; |
| 693 |
portlo = (unsigned)atoi(p + 1); |
| 694 |
*p = '\0'; |
| 695 |
|
| 696 |
p = strrchr(addrstr, '.'); |
| 697 |
if (p == NULL) |
| 698 |
goto out; |
| 699 |
porthi = (unsigned)atoi(p + 1); |
| 700 |
*p = '\0'; |
| 701 |
port = (porthi << 8) | portlo; |
| 702 |
} |
| 703 |
|
| 704 |
ret = (struct netbuf *)malloc(sizeof *ret); |
| 705 |
if (ret == NULL) |
| 706 |
goto out; |
| 707 |
|
| 708 |
switch (af) { |
| 709 |
case AF_INET: |
| 710 |
sin = (struct sockaddr_in *)malloc(sizeof *sin); |
| 711 |
if (sin == NULL) |
| 712 |
goto out; |
| 713 |
memset(sin, 0, sizeof *sin); |
| 714 |
sin->sin_family = AF_INET; |
| 715 |
sin->sin_port = htons(port); |
| 716 |
if (inet_pton(AF_INET, addrstr, &sin->sin_addr) <= 0) { |
| 717 |
free(sin); |
| 718 |
free(ret); |
| 719 |
ret = NULL; |
| 720 |
goto out; |
| 721 |
} |
| 722 |
ret->maxlen = ret->len = sizeof *sin; |
| 723 |
ret->buf = sin; |
| 724 |
break; |
| 725 |
#ifdef INET6 |
| 726 |
case AF_INET6: |
| 727 |
sin6 = (struct sockaddr_in6 *)malloc(sizeof *sin6); |
| 728 |
if (sin6 == NULL) |
| 729 |
goto out; |
| 730 |
memset(sin6, 0, sizeof *sin6); |
| 731 |
sin6->sin6_family = AF_INET6; |
| 732 |
sin6->sin6_port = htons(port); |
| 733 |
if (inet_pton(AF_INET6, addrstr, &sin6->sin6_addr) <= 0) { |
| 734 |
free(sin6); |
| 735 |
free(ret); |
| 736 |
ret = NULL; |
| 737 |
goto out; |
| 738 |
} |
| 739 |
ret->maxlen = ret->len = sizeof *sin6; |
| 740 |
ret->buf = sin6; |
| 741 |
break; |
| 742 |
#endif |
| 743 |
case AF_LOCAL: |
| 744 |
sun = (struct sockaddr_un *)malloc(sizeof *sun); |
| 745 |
if (sun == NULL) |
| 746 |
goto out; |
| 747 |
memset(sun, 0, sizeof *sun); |
| 748 |
sun->sun_family = AF_LOCAL; |
| 749 |
strncpy(sun->sun_path, addrstr, sizeof(sun->sun_path) - 1); |
| 750 |
ret->len = SUN_LEN(sun); |
| 751 |
ret->maxlen = sizeof(struct sockaddr_un); |
| 752 |
if (sun->sun_path[0] == '@') |
| 753 |
|
| 754 |
sun->sun_path[0] = '\0'; |
| 755 |
ret->buf = sun; |
| 756 |
break; |
| 757 |
default: |
| 758 |
break; |
| 759 |
} |
| 760 |
out: |
| 761 |
free(addrstr); |
| 762 |
return ret; |
| 763 |
} |
| 764 |
|
| 765 |
int |
| 766 |
__rpc_seman2socktype(int semantics) |
| 767 |
{ |
| 768 |
switch (semantics) { |
| 769 |
case NC_TPI_CLTS: |
| 770 |
return SOCK_DGRAM; |
| 771 |
case NC_TPI_COTS_ORD: |
| 772 |
return SOCK_STREAM; |
| 773 |
case NC_TPI_RAW: |
| 774 |
return SOCK_RAW; |
| 775 |
default: |
| 776 |
break; |
| 777 |
} |
| 778 |
|
| 779 |
return -1; |
| 780 |
} |
| 781 |
|
| 782 |
int |
| 783 |
__rpc_socktype2seman(int socktype) |
| 784 |
{ |
| 785 |
switch (socktype) { |
| 786 |
case SOCK_DGRAM: |
| 787 |
return NC_TPI_CLTS; |
| 788 |
case SOCK_STREAM: |
| 789 |
return NC_TPI_COTS_ORD; |
| 790 |
case SOCK_RAW: |
| 791 |
return NC_TPI_RAW; |
| 792 |
default: |
| 793 |
break; |
| 794 |
} |
| 795 |
|
| 796 |
return -1; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
int |
| 807 |
__rpc_fixup_addr(struct netbuf *new, const struct netbuf *svc) |
| 808 |
{ |
| 809 |
#ifdef INET6 |
| 810 |
struct sockaddr *sa_new, *sa_svc; |
| 811 |
struct sockaddr_in6 *sin6_new, *sin6_svc; |
| 812 |
|
| 813 |
sa_svc = (struct sockaddr *)svc->buf; |
| 814 |
sa_new = (struct sockaddr *)new->buf; |
| 815 |
|
| 816 |
if (sa_new->sa_family == sa_svc->sa_family && |
| 817 |
sa_new->sa_family == AF_INET6) { |
| 818 |
sin6_new = (struct sockaddr_in6 *)new->buf; |
| 819 |
sin6_svc = (struct sockaddr_in6 *)svc->buf; |
| 820 |
|
| 821 |
if ((IN6_IS_ADDR_LINKLOCAL(&sin6_new->sin6_addr) && |
| 822 |
IN6_IS_ADDR_LINKLOCAL(&sin6_svc->sin6_addr)) || |
| 823 |
(IN6_IS_ADDR_SITELOCAL(&sin6_new->sin6_addr) && |
| 824 |
IN6_IS_ADDR_SITELOCAL(&sin6_svc->sin6_addr))) { |
| 825 |
sin6_new->sin6_scope_id = sin6_svc->sin6_scope_id; |
| 826 |
} |
| 827 |
} |
| 828 |
#endif |
| 829 |
return 1; |
| 830 |
} |
| 831 |
|
| 832 |
int |
| 833 |
__rpc_sockisbound(int fd) |
| 834 |
{ |
| 835 |
struct sockaddr_storage ss; |
| 836 |
union { |
| 837 |
struct sockaddr_in sin; |
| 838 |
struct sockaddr_in6 sin6; |
| 839 |
struct sockaddr_un usin; |
| 840 |
} u_addr; |
| 841 |
socklen_t slen; |
| 842 |
int path_len; |
| 843 |
|
| 844 |
slen = sizeof (struct sockaddr_storage); |
| 845 |
if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) |
| 846 |
return 0; |
| 847 |
|
| 848 |
switch (ss.ss_family) { |
| 849 |
case AF_INET: |
| 850 |
memcpy(&u_addr.sin, &ss, sizeof(u_addr.sin)); |
| 851 |
return (u_addr.sin.sin_port != 0); |
| 852 |
#ifdef INET6 |
| 853 |
case AF_INET6: |
| 854 |
memcpy(&u_addr.sin6, &ss, sizeof(u_addr.sin6)); |
| 855 |
return (u_addr.sin6.sin6_port != 0); |
| 856 |
#endif |
| 857 |
case AF_LOCAL: |
| 858 |
memcpy(&u_addr.usin, &ss, sizeof(u_addr.usin)); |
| 859 |
path_len = slen - offsetof(struct sockaddr_un, sun_path); |
| 860 |
return path_len > 0; |
| 861 |
default: |
| 862 |
break; |
| 863 |
} |
| 864 |
|
| 865 |
return 0; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
struct netbuf * |
| 872 |
__rpc_set_netbuf(struct netbuf *nb, const void *ptr, size_t len) |
| 873 |
{ |
| 874 |
if (nb->len != len) { |
| 875 |
if (nb->len) |
| 876 |
mem_free(nb->buf, nb->len); |
| 877 |
nb->buf = mem_alloc(len); |
| 878 |
if (nb->buf == NULL) |
| 879 |
return NULL; |
| 880 |
|
| 881 |
nb->maxlen = nb->len = len; |
| 882 |
} |
| 883 |
memcpy(nb->buf, ptr, len); |
| 884 |
return nb; |
| 885 |
} |