2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Glues the library routines to the stub routines
26 * Copyright (C) 1989 by NeXT, Inc.
31 #include <netinfo/ni.h>
32 #include <rpc/pmap_clnt.h>
33 #include <rpc/pmap_prot.h>
38 #include "sys_interfaces.h"
40 #define LOCAL_PORT 1033
42 #define NI_TIMEOUT_SHORT 5 /* 5 second timeout for transactions */
43 #define NI_TIMEOUT_LONG 60 /* 60 second timeout for writes */
44 #define NI_TRIES 5 /* number of retries per timeout (udp only) */
45 #define NI_SLEEPTIME 4 /* 4 second sleeptime, in case of errors */
46 #define NI_MAXSLEEPTIME 64 /* 64 second max sleep time */
47 #define NI_MAXCONNTRIES 2 /* Try to form a connection twice before sleeping */
49 /* Hack for determining if an IP address is a broadcast address. -GRS */
50 /* Note that addr is network byte order (big endian) - BKM */
52 #define IS_BROADCASTADDR(addr) (((unsigned char *) &addr)[0] == 0xFF)
54 #ifndef INADDR_LOOPBACK
55 #define INADDR_LOOPBACK (u_long)0x7f000001
57 #define debug(msg) syslog(LOG_ERR, msg)
59 #define clnt_debug(ni, msg) /* do nothing */
61 typedef struct ni_private
{
62 int naddrs
; /* number of addresses */
63 struct in_addr
*addrs
; /* addresses of servers - network byte order */
64 int whichwrite
; /* which one of the above is the master */
65 ni_name
*tags
; /* tags of servers */
66 int pid
; /* pid, to detect forks */
67 int tsock
; /* tcp socket */
68 int tport
; /* tcp local port name - host byte order */
69 CLIENT
*tc
; /* tcp client */
70 long tv_sec
; /* timeout for this call */
71 long rtv_sec
; /* read timeout - 0 if default */
72 long wtv_sec
; /* write timeout - 0 if default */
73 int abort
; /* abort on timeout? */
74 int needwrite
; /* need to lock writes? */
75 int uid
; /* user id */
76 ni_name passwd
; /* password */
79 #define NIP(ni) ((ni_private *)(ni))
81 static const ni_name NAME_NAME
= "name";
82 static const ni_name NAME_SERVES
= "serves";
83 static const ni_name NAME_MACHINES
= "machines";
84 static const ni_name NAME_IP_ADDRESS
= "ip_address";
85 static const ni_name NAME_MASTER
= "master";
86 static const ni_name NAME_USERS
= "users";
87 static const ni_name NAME_UID
= "uid";
89 static const ni_name NAME_DOMAIN_SERVERS
= "domain_servers";
91 typedef struct getreg_stuff
{
92 nibind_getregister_res res
;
97 static int socket_open(struct sockaddr_in
*raddr
, int, int, int, int, int);
101 * Keep track of our port, in case somebody closes our socket
109 struct sockaddr_in sin
;
112 sinlen
= sizeof(sin
);
113 if (getsockname(sock
, (struct sockaddr
*)&sin
, &sinlen
) == 0) {
114 if (sin
.sin_port
== 0) {
115 (void)bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
116 sinlen
= sizeof(sin
);
117 (void)getsockname(sock
, (struct sockaddr
*)&sin
,
120 return (ntohs(sin
.sin_port
));
131 if (ni
->passwd
!= NULL
&& ni
->tc
!= NULL
) {
132 auth_destroy(ni
->tc
->cl_auth
);
133 ni
->tc
->cl_auth
= authunix_create(ni
->passwd
, ni
->uid
, 0, 0,
146 tv
->tv_sec
= sec
/ tries
;
147 tv
->tv_usec
= ((sec
% tries
) * 1000000) / tries
;
161 ni
->tv_sec
= timeout
;
162 if (ni
->tc
!= NULL
) {
163 clnt_control(ni
->tc
, CLSET_TIMEOUT
, &tv
);
169 * Connect to a given address/tag
172 connectit(ni_private
*ni
)
174 struct sockaddr_in sin
;
179 nibind_getregister_res res
;
182 bzero(&sin
, sizeof(sin
));
184 sin
.sin_family
= AF_INET
;
186 tv
.tv_sec
= ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
;
189 ni_settimeout(ni
, tv
.tv_sec
);
190 fixtimeout(&tv
, ni
->tv_sec
, NI_TRIES
);
193 * If connecting to local domain, try using the "well-known" port first.
195 if (!strcmp(ni
->tags
[0], "local"))
197 interface_list_t
*ilist
;
199 ilist
= sys_interfaces();
200 if (sys_is_my_address(ilist
, &ni
->addrs
[0]))
202 sin
.sin_port
= htons(LOCAL_PORT
);
203 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
204 sock
= socket_open(&sin
, NI_PROG
, NI_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_TCP
);
206 sys_interfaces_release(ilist
);
210 * If connecting to a domain other than the local domain,
211 * or if connection to local didn't work with local's well-known port,
212 * then go through portmap & nibindd to find the port and connect.
217 sin
.sin_addr
= ni
->addrs
[0];
219 sock
= socket_open(&sin
, NIBIND_PROG
, NIBIND_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_UDP
);
220 if (sock
< 0) return (0);
222 cl
= clntudp_create(&sin
, NIBIND_PROG
, NIBIND_VERS
, tv
, &sock
);
229 tv
.tv_sec
= ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
;
232 stat
= clnt_call(cl
, NIBIND_GETREGISTER
, xdr_ni_name
, &ni
->tags
[0], xdr_nibind_getregister_res
, &res
, tv
);
235 if (stat
!= RPC_SUCCESS
|| res
.status
!= NI_OK
) return (0);
237 sin
.sin_port
= htons(res
.nibind_getregister_res_u
.addrs
.tcp_port
);
238 sock
= socket_open(&sin
, NI_PROG
, NI_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_TCP
);
241 if (sock
< 0) return (0);
243 cl
= clnttcp_create(&sin
, NI_PROG
, NI_VERS
, &sock
, 0, 0);
250 clnt_control(cl
, CLSET_TIMEOUT
, &tv
);
253 ni
->tport
= getmyport(sock
);
255 fcntl(ni
->tsock
, F_SETFD
, 1);
266 ((ni_private
*)ni
)->abort
= abort
;
276 ((ni_private
*)ni
)->wtv_sec
= timeout
;
286 ((ni_private
*)ni
)->rtv_sec
= timeout
;
296 ((ni_private
*)ni
)->needwrite
= needwrite
;
301 * Returns a client handle to the NetInfo server, if it's running
304 connectlocal(ni_private
*ni
)
309 ni
->addrs
= (struct in_addr
*)malloc(sizeof(struct in_addr
));
310 ni
->addrs
[0].s_addr
= htonl(INADDR_LOOPBACK
);
311 ni
->tags
= (ni_name
*)malloc(sizeof(ni_name
));
312 ni
->tags
[0] = ni_name_dup("local");
315 while (!connectit(ni
))
319 syslog(LOG_ERR
, "NetInfo timeout connecting to local domain, sleeping");
329 syslog(LOG_ERR
, "NetInfo connection to local domain waking");
337 * Destroy the client handle
348 if (sock
>= 0 && getmyport(sock
) != port
) {
350 * Somebody else has the descriptor open. Do not close it,
356 auth_destroy(cl
->cl_auth
);
361 * It's ours and we can close it
369 * Reinitialize everything
376 if (ni
->tc
!= NULL
) {
377 clnt_kill(ni
->tc
, ni
->tsock
, ni
->tport
);
387 * Switch to a new server
395 struct in_addr tmp_addr
;
402 tmp_addr
= ni
->addrs
[0];
403 tmp_tag
= ni
->tags
[0];
405 ni
->addrs
[0] = ni
->addrs
[which
];
406 ni
->tags
[0] = ni
->tags
[which
];
408 ni
->addrs
[which
] = tmp_addr
;
409 ni
->tags
[which
] = tmp_tag
;
411 if (ni
->whichwrite
== 0) {
412 ni
->whichwrite
= which
;
414 else if (ni
->whichwrite
== which
) {
421 * Swap two servers' positions
430 struct in_addr tmp_addr
;
435 tmp_addr
= ni
->addrs
[a
];
436 tmp_tag
= ni
->tags
[a
];
438 ni
->addrs
[a
] = ni
->addrs
[b
];
439 ni
->tags
[a
] = ni
->tags
[b
];
441 ni
->addrs
[b
] = tmp_addr
;
442 ni
->tags
[b
] = tmp_tag
;
444 if (ni
->whichwrite
== a
) {
447 else if (ni
->whichwrite
== b
) {
454 * Callback routine for multi_call
455 * XXX: should save returned port numbers
460 struct sockaddr_in
*sin
,
464 getreg_stuff
*stuff
= (getreg_stuff
*)vstuff
;
466 if (stuff
->res
.status
!= NI_OK
) {
469 ni_switch(stuff
->ni
, which
);
478 shuffle(ni_private
*ni
)
484 if (ni
->naddrs
<= 1) return;
486 rfd
= open("/dev/random", O_RDONLY
, 0);
487 shuffle
= (int *)malloc(ni
->naddrs
* sizeof(int));
488 for (i
= 0; i
< ni
->naddrs
; i
++) shuffle
[i
] = i
;
489 for (i
= 0, j
= ni
->naddrs
; j
> 0; i
++, j
--) {
494 /* get a random number */
496 (read(rfd
, &rVal
, sizeof(rVal
)) != sizeof(rVal
))) {
497 /* if we could not read from /dev/random */
498 static int initialized
= 0;
501 srandom(gethostid() ^ time(NULL
));
507 rEnt
= (unsigned int)rVal
% j
; /* pick one of the remaining entries */
508 tEnt
= shuffle
[rEnt
]; /* grab the random entry */
509 shuffle
[rEnt
] = shuffle
[j
-1]; /* the last entry moves to the random slot */
510 shuffle
[j
-1] = tEnt
; /* the last slot gets the random entry */
511 ni_swap(ni
, rEnt
, j
-1); /* and swap the actual NI addresses */
514 if (rfd
> 0) (void)close(rfd
);
526 int sleeptime
= NI_SLEEPTIME
;
530 interface_list_t
*ilist
;
533 if (ni
->naddrs
== 1) {
540 * re-order the servers so that:
541 * servers on the local host are at the start of the list, then
542 * servers on the local network are next, then
543 * all other servers are next
546 ilist
= sys_interfaces();
554 * move local servers to the head of the list
557 for (i
= nlocal
; i
< ni
->naddrs
; i
++) {
558 if (sys_is_my_address(ilist
, &ni
->addrs
[i
]))
560 ni_swap(ni
, nlocal
, i
);
566 * move servers on this network to follow local servers
569 for (i
= nnetwork
; i
< ni
->naddrs
; i
++) {
570 if (sys_is_my_network(ilist
, &ni
->addrs
[i
]) ||
571 IS_BROADCASTADDR(ni
->addrs
[i
].s_addr
))
573 ni_swap(ni
, nnetwork
, i
);
578 sys_interfaces_release(ilist
);
583 * call local servers first
586 for (i
= 0; i
< nlocal
; i
++) {
587 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (local %d)",
588 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
590 stat
= multi_call(nlocal
, ni
->addrs
,
591 NIBIND_PROG
, NIBIND_VERS
, NIBIND_GETREGISTER
,
592 xdr_ni_name
, ni
->tags
,
594 xdr_nibind_getregister_res
,
597 if (stat
== RPC_SUCCESS
) {
603 * call local servers and this network's servers
605 if (nnetwork
> nlocal
) {
606 for (i
= 0; i
< nnetwork
; i
++) {
607 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (network %d)",
608 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
610 stat
= multi_call(nnetwork
, ni
->addrs
,
611 NIBIND_PROG
, NIBIND_VERS
, NIBIND_GETREGISTER
,
612 xdr_ni_name
, ni
->tags
,
614 xdr_nibind_getregister_res
,
617 if (stat
== RPC_SUCCESS
) {
625 for (i
= 0; i
< ni
->naddrs
; i
++) {
626 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (world %d)",
627 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
629 stat
= multi_call(ni
->naddrs
,
630 ni
->addrs
, NIBIND_PROG
, NIBIND_VERS
,
632 xdr_ni_name
, ni
->tags
,
634 xdr_nibind_getregister_res
,
636 ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
);
637 if (stat
== RPC_SUCCESS
) {
645 if (ni
->whichwrite
>= 0) {
647 "NetInfo connect timeout (domain with master %s/%s), sleeping",
648 inet_ntoa(ni
->addrs
[ni
->whichwrite
]), ni
->tags
[ni
->whichwrite
]);
651 syslog(LOG_ERR
, "NetInfo connect timeout (domain with server %s/%s), sleeping",
652 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
657 if (sleeptime
< NI_MAXSLEEPTIME
) {
658 sleeptime
*= 2; /* backoff */
662 syslog(LOG_INFO
, "NetInfo connected to %s/%s", inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
665 syslog(LOG_ERR
, "NetInfo connected to %s/%s", inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
672 * Confirm that our tcp socket is still valid
680 if (ni
->tsock
!= -1) {
681 if (getmyport(ni
->tsock
) == ni
->tport
) {
685 * Somebody closed our socket. Do not close it, it could
686 * be owned by somebody else now.
690 if (ni
->tc
->cl_auth
!= NULL
) auth_destroy(ni
->tc
->cl_auth
);
691 clnt_destroy(ni
->tc
);
695 if (!needwrite
&& !rebind(ni
) && ni
->abort
) {
698 return (connectit(ni
));
718 if (ni
->naddrs
== 1) {
720 * One server - must be the master
725 needwrite
= ni
->needwrite
;
727 if (ni_root(ni
, &root
) != NI_OK
) {
728 ni
->needwrite
= needwrite
;
732 if (ni_lookupprop(ni
, &root
, NAME_MASTER
, &nl
) != NI_OK
) {
733 ni
->needwrite
= needwrite
;
736 if (nl
.ninl_len
== 0) {
737 ni
->needwrite
= needwrite
;
740 sep
= index(nl
.ninl_val
[0], '/');
742 ni
->needwrite
= needwrite
;
746 master
= nl
.ninl_val
[0];
748 if (ni_lookup(ni
, &root
, NAME_NAME
, NAME_MACHINES
, &idl
) != NI_OK
) {
749 ni
->needwrite
= needwrite
;
750 ni_namelist_free(&nl
);
753 if (idl
.niil_len
< 1) {
754 ni
->needwrite
= needwrite
;
757 id
.nii_object
= idl
.niil_val
[0];
758 ni_idlist_free(&idl
);
760 if (ni_lookup(ni
, &id
, NAME_NAME
, master
, &idl
) != NI_OK
) {
761 ni_namelist_free(&nl
);
762 ni
->needwrite
= needwrite
;
765 ni_namelist_free(&nl
);
766 if (idl
.niil_len
< 1) {
767 ni
->needwrite
= needwrite
;
770 id
.nii_object
= idl
.niil_val
[0];
771 ni_idlist_free(&idl
);
773 if (ni_lookupprop(ni
, &id
, NAME_IP_ADDRESS
, &nl
) != NI_OK
) {
776 for (i
= 0; i
< nl
.ninl_len
; i
++) {
777 addr
.s_addr
= inet_addr(nl
.ninl_val
[i
]);
778 for (j
= 0; j
< ni
->naddrs
; j
++) {
779 if (addr
.s_addr
== ni
->addrs
[j
].s_addr
) {
781 ni_namelist_free(&nl
);
782 ni
->needwrite
= needwrite
;
787 ni
->needwrite
= needwrite
;
788 ni_namelist_free(&nl
);
807 if (getpid() != ni
->pid
) {
810 if (needwrite
|| ni
->needwrite
) {
811 if (ni
->whichwrite
>= 0) {
812 ni_switch(ni
, ni
->whichwrite
);
814 if (!setmaster(ni
)) {
817 ni_switch(ni
, ni
->whichwrite
);
820 ni_settimeout(ni
, (ni
->rtv_sec
== 0 ?
821 NI_TIMEOUT_SHORT
: ni
->rtv_sec
));
824 ni_settimeout(ni
, (ni
->wtv_sec
== 0 ?
825 NI_TIMEOUT_LONG
: ni
->wtv_sec
));
828 ni_settimeout(ni
, (ni
->rtv_sec
== 0 ?
829 NI_TIMEOUT_SHORT
: ni
->rtv_sec
));
833 * Try more than once, in case server closed connection.
835 for (i
= 0; i
< NI_MAXCONNTRIES
; i
++) {
836 if (!confirm_tcp(ni
, needwrite
)) {
839 if ((resp
= (*stub
)(args
, ni
->tc
)) != NULL
) {
841 syslog(LOG_ERR
, "NetInfo connected to %s/%s",
842 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
848 clnt_geterr(ni
->tc
, &err
);
849 if (err
.re_status
!= RPC_CANTRECV
) break;
851 if (i
+ 1 < NI_MAXCONNTRIES
) {
853 * Server closed connection. Reinit and try
859 if (err
.re_status
== RPC_PROCUNAVAIL
) {
862 if (needwrite
|| ni
->abort
) {
864 * We time out for writes or if it is explicitly
871 "NetInfo connection failed for server %s/%s",
872 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
876 if (ni
->tc
!= NULL
) {
877 if (!(sleeptime
== 0 &&
878 err
.re_status
== RPC_TIMEDOUT
)) {
880 * Do not print message on
881 * first timeout. It is likely
882 * we will find another server soon.
883 * Let's not needlessly alarm the
886 syslog(LOG_ERR
, "%s on connection to %s/%s",
887 clnt_sperror(ni
->tc
,"NetInfo connection timeout"),
888 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
892 /* first attempt failed */
893 syslog(LOG_ERR
, "%s on initial connection to %s/%s",
894 clnt_sperror(ni
->tc
,"NetInfo connection timeout"),
895 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
899 "NetInfo connection failed for server %s/%s",
900 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
906 if (sleeptime
< NI_MAXSLEEPTIME
) {
907 sleeptime
*= 2; /* backoff */
911 * Do not sleep on the first timeout.
912 * It is likely we will find another server quickly.
914 sleeptime
= NI_SLEEPTIME
;
922 #define RCALLIT(a, b, c) callit((ni_private *)(a), (void *(*)())(b), \
926 #define WCALLIT(a, b, c) callit((ni_private *)(a), (void *(*)())(b), \
954 ni
= (ni_private
*)malloc(sizeof(*ni
));
961 ni
->tv_sec
= NI_TIMEOUT_SHORT
;
980 dupni
= (ni_private
*)ni_alloc();
983 dupni
->naddrs
= NIP(ni
)->naddrs
;
984 dupni
->whichwrite
= NIP(ni
)->whichwrite
;
985 if (dupni
->naddrs
> 0) {
986 dupni
->addrs
= ((struct in_addr
*)
987 malloc(NIP(ni
)->naddrs
* sizeof(struct in_addr
)));
988 bcopy(NIP(ni
)->addrs
, dupni
->addrs
,
989 NIP(ni
)->naddrs
* sizeof(struct in_addr
));
990 dupni
->tags
= ((ni_name
*)
991 malloc(NIP(ni
)->naddrs
* sizeof(ni_name
)));
992 for (i
= 0; i
< NIP(ni
)->naddrs
; i
++) {
993 dupni
->tags
[i
] = ni_name_dup(NIP(ni
)->tags
[i
]);
996 if (NIP(ni
)->passwd
!= NULL
) {
997 dupni
->passwd
= ni_name_dup(NIP(ni
)->passwd
);
999 return ((void *)dupni
);
1010 int len
= strlen(domain
);
1013 sep
= index(domtag
, '/');
1017 if (strncmp(domain
, domtag
, len
) == 0 &&
1018 domtag
[len
] == '/') {
1019 *tag
= ni_name_dup(sep
+ 1);
1027 add_addr_tag(ni_private
*ni
, ni_name addrtag
)
1029 struct in_addr addr
;
1033 slash
= strchr(addrtag
, '/');
1034 if (slash
== NULL
) return;
1037 if (tag
[0] == '\0') return;
1041 if (inet_aton(addrtag
, &addr
) == 0) return;
1043 if (ni
->naddrs
== 0)
1045 ni
->addrs
= (struct in_addr
*)calloc(1, sizeof(struct in_addr
));
1046 if (ni
->addrs
== NULL
) return;
1048 ni
->tags
= (ni_name
*)calloc(1, sizeof(ni_name
));
1049 if (ni
->tags
== NULL
) return;
1053 ni
->addrs
= (struct in_addr
*)realloc(ni
->addrs
, ((ni
->naddrs
+ 1) * sizeof(struct in_addr
)));
1054 if (ni
->addrs
== NULL
) return;
1056 ni
->tags
= (ni_name
*)realloc(ni
->tags
, ((ni
->naddrs
+ 1) * sizeof(ni_name
)));
1057 if (ni
->tags
== NULL
) return;
1060 ni
->addrs
[ni
->naddrs
] = addr
;
1061 ni
->tags
[ni
->naddrs
] = ni_name_dup(tag
);
1066 addaddr(void *ni
, ni_index ido
, ni_name tag
, ni_private
*target_ni
)
1070 struct in_addr addr
;
1073 id
.nii_object
= ido
;
1075 if (ni_lookupprop(ni
, &id
, NAME_IP_ADDRESS
, &nl
) != NI_OK
) return 0;
1077 if (nl
.ni_namelist_len
== 0) return 0;
1079 if (target_ni
->naddrs
== 0)
1081 target_ni
->addrs
= (struct in_addr
*)malloc(nl
.ni_namelist_len
* sizeof(struct in_addr
));
1082 target_ni
->tags
= (ni_name
*)malloc(nl
.ni_namelist_len
* sizeof(ni_name
));
1086 target_ni
->addrs
= (struct in_addr
*)realloc(target_ni
->addrs
, ((target_ni
->naddrs
+ nl
.ni_namelist_len
) * sizeof(struct in_addr
)));
1087 target_ni
->tags
= (ni_name
*)realloc(target_ni
->tags
, ((target_ni
->naddrs
+ nl
.ni_namelist_len
) * sizeof(ni_name
)));
1090 for (i
= 0; i
< nl
.ni_namelist_len
; i
++)
1092 addr
.s_addr
= inet_addr(nl
.ni_namelist_val
[i
]);
1093 target_ni
->addrs
[target_ni
->naddrs
] = addr
;
1094 target_ni
->tags
[target_ni
->naddrs
] = ni_name_dup(tag
);
1095 target_ni
->naddrs
++;
1098 ni_namelist_free(&nl
);
1103 get_daddr(ni_private
*ni
, ni_name dom
, ni_private
*target_ni
)
1107 ni_entrylist entries
;
1113 if (dom
== NULL
) return 0;
1115 if (!strcmp(dom
, "."))
1117 /* check for server list */
1119 if (ni_statistics(ni
, &pl
) == NI_OK
)
1121 i
= ni_proplist_match(pl
, NAME_DOMAIN_SERVERS
, NULL
);
1122 if (i
!= NI_INDEX_NULL
)
1124 if (pl
.ni_proplist_val
[i
].nip_val
.ni_namelist_len
> 0)
1126 for (j
= 0; j
< pl
.ni_proplist_val
[i
].nip_val
.ni_namelist_len
; j
++)
1128 add_addr_tag(target_ni
, pl
.ni_proplist_val
[i
].nip_val
.ni_namelist_val
[j
]);
1130 ni_proplist_free(&pl
);
1134 ni_proplist_free(&pl
);
1138 if (ni_root(ni
, &nid
) != NI_OK
) return 0;
1140 if (ni_lookup(ni
, &nid
, NAME_NAME
, NAME_MACHINES
, &ids
) != NI_OK
) return 0;
1142 nid
.nii_object
= ids
.niil_val
[0];
1143 ni_idlist_free(&ids
);
1146 if (ni_list(ni
, &nid
, NAME_SERVES
, &entries
) != NI_OK
) return 0;
1148 for (i
= 0; i
< entries
.niel_len
; i
++)
1150 if (entries
.niel_val
[i
].names
== NULL
) continue;
1152 for (j
= 0; j
< entries
.niel_val
[i
].names
->ni_namelist_len
; j
++)
1154 if (match(dom
, entries
.niel_val
[i
].names
->ni_namelist_val
[j
], &tag
))
1156 addaddr(ni
, entries
.niel_val
[i
].id
, tag
, target_ni
);
1162 ni_entrylist_free(&entries
);
1163 return (target_ni
->naddrs
> 0);
1167 getparent(ni_private
*oldni
, ni_private
**newni
)
1169 ni_rparent_res
*resp
;
1170 ni_private
*ni
= NULL
;
1174 struct in_addr raddr
;
1181 * First, find our parent, any parent
1185 resp
= RCALLIT(oldni
, _ni_rparent_2
, NULL
);
1186 if (resp
== NULL
) return NI_FAILED
;
1187 if (resp
->status
!= NI_NORESPONSE
) break;
1191 syslog(LOG_ERR
, "NetInfo timeout finding server for parent of %s/%s, sleeping",
1192 inet_ntoa(oldni
->addrs
[0]), oldni
->tags
[0]);
1196 sleep(NI_SLEEPTIME
);
1201 raddr
.s_addr
= htonl(resp
->ni_rparent_res_u
.binding
.addr
);
1203 syslog(LOG_ERR
, "NetInfo %s/%s found parent %s/%s",
1204 inet_ntoa(oldni
->addrs
[0]), oldni
->tags
[0],
1205 inet_ntoa(raddr
), resp
->ni_rparent_res_u
.binding
.tag
);
1208 if (resp
->status
!= NI_OK
) return (resp
->status
);
1214 ni
->addrs
= (struct in_addr
*)malloc(sizeof(struct in_addr
));
1215 ni
->addrs
[0].s_addr
=htonl(resp
->ni_rparent_res_u
.binding
.addr
);
1216 ni
->tags
= (ni_name
*)malloc(sizeof(ni_name
));
1217 ni
->tags
[0] = ni_name_dup(resp
->ni_rparent_res_u
.binding
.tag
);
1219 xdr_free(xdr_ni_rparent_res
, resp
);
1225 if (get_daddr(dupni
, ".", ni
) == 0)
1227 if (oldni
->abort
== 1) break;
1232 * Make sure returned parent is head of list
1234 for (i
= 0; i
< ni
->naddrs
; i
++)
1236 if (ni
->addrs
[i
].s_addr
== dupni
->addrs
[0].s_addr
)
1245 * Reuse dupni client info
1247 ni
->tsock
= dupni
->tsock
;
1248 ni
->tport
= dupni
->tport
;
1256 * If returned parent wasn't in list, it's a rogue.
1257 * Log an error and drop the connection.
1261 syslog(LOG_ERR
, "Rogue NetInfo server detected: %s/%s",
1262 inet_ntoa(dupni
->addrs
[0]), dupni
->tags
[0]);
1276 if (ni
!= NULL
) ni_free(ni
);
1283 struct sockaddr_in
*sin
,
1290 NIP(ni
)->naddrs
= 1;
1291 NIP(ni
)->addrs
= (struct in_addr
*
1292 )malloc(sizeof(struct in_addr
));
1293 NIP(ni
)->addrs
[0] = sin
->sin_addr
;
1294 NIP(ni
)->tags
= (ni_name
*)malloc(sizeof(ni_name
));
1295 NIP(ni
)->tags
[0] = ni_name_dup(tag
);
1303 struct sockaddr_in
*addr
,
1308 if (!confirm_tcp(ni
, 0)) {
1311 *tag
= ni_name_dup(NIP(ni
)->tags
[0]);
1312 addr
->sin_addr
= NIP(ni
)->addrs
[0];
1313 addr
->sin_port
= htons(NIP(ni
)->tport
);
1314 addr
->sin_family
= AF_INET
;
1315 bzero(addr
->sin_zero
, sizeof(addr
->sin_zero
));
1328 ni_name sep
, addr
, tag
;
1329 struct sockaddr_in sin
;
1332 if (oldni
== NULL
) {
1334 if (!connectlocal(ni
)) {
1338 if (strcmp(domain
, "..") == 0) {
1340 status
= getparent((ni_private
*)oldni
, &ni
);
1342 if (status
!= NI_OK
) {
1345 } else if ((sep
= index(domain
, '@')) != NULL
) {
1347 tag
= strncpy((char *)malloc(sep
-domain
+1), domain
, sep
-domain
);
1348 tag
[sep
-domain
] = '\0';
1349 addr
= strcpy ((char *)malloc(strlen(sep
+1)), sep
+1);
1350 sin
.sin_addr
.s_addr
= inet_addr(addr
);
1351 if (sin
.sin_addr
.s_addr
== INADDR_NONE
) {
1352 he
= gethostbyname(addr
);
1358 bcopy(he
->h_addr_list
[0], &sin
.sin_addr
.s_addr
, he
->h_length
);
1360 ni
= ni_connect(&sin
, tag
);
1363 } else if (strcmp(domain
, ".") != 0) {
1365 * nothing else makes sense
1371 if (strcmp(domain
, "..") == 0) {
1372 status
= getparent((ni_private
*)oldni
, &ni
);
1373 if (status
!= NI_OK
) {
1376 } else if ((sep
= index(domain
, '@')) != NULL
) {
1377 tag
= strncpy((char *)malloc(sep
-domain
+1), domain
, sep
-domain
);
1378 tag
[sep
-domain
] = '\0';
1379 addr
= strcpy ((char *)malloc(strlen(sep
+1)), sep
+1);
1380 sin
.sin_addr
.s_addr
= inet_addr(addr
);
1381 if (sin
.sin_addr
.s_addr
== INADDR_NONE
) {
1382 he
= gethostbyname(addr
);
1388 bcopy(he
->h_addr_list
[0], &sin
.sin_addr
.s_addr
, he
->h_length
);
1390 ni
= ni_connect(&sin
, tag
);
1397 if (!get_daddr(oldni
, (ni_name
)domain
, ni
)) {
1403 return ((void *)ni
);
1414 if (NIP(ni
)->tc
!= NULL
) {
1415 clnt_kill(NIP(ni
)->tc
, NIP(ni
)->tsock
, NIP(ni
)->tport
);
1417 if (NIP(ni
)->naddrs
> 0) {
1418 free(NIP(ni
)->addrs
);
1419 for (i
= 0; i
< NIP(ni
)->naddrs
; i
++) {
1420 ni_name_free(&NIP(ni
)->tags
[i
]);
1422 free(NIP(ni
)->tags
);
1424 if (NIP(ni
)->passwd
!= NULL
) {
1425 ni_name_free(&NIP(ni
)->passwd
);
1432 * The rest of these are just wrappers that end up doing
1433 * RPC calls to the local NetInfo server.
1443 if ((resp
= (ni_proplist
*)RCALLIT(ni
, _ni_statistics_2
, NULL
))
1460 if ((resp
= RCALLIT(ni
, _ni_root_2
, id
)) == NULL
) {
1461 clnt_debug(ni
, "_ni_root");
1464 if (resp
->status
== NI_OK
) {
1465 *id
= resp
->ni_id_res_u
.id
;
1467 return (resp
->status
);
1479 if ((resp
= RCALLIT(ni
, _ni_self_2
, id
)) == NULL
) {
1480 clnt_debug(ni
, "_ni_self");
1483 if (resp
->status
== NI_OK
) {
1484 *id
= resp
->ni_id_res_u
.id
;
1486 return (resp
->status
);
1494 ni_index
*parent_id_p
1497 ni_parent_res
*resp
;
1499 if ((resp
= RCALLIT(ni
, _ni_parent_2
, id
)) == NULL
) {
1500 clnt_debug(ni
, "_ni_parent");
1503 if (resp
->status
== NI_OK
) {
1504 *parent_id_p
= resp
->ni_parent_res_u
.stuff
.object_id
;
1505 *id
= resp
->ni_parent_res_u
.stuff
.self_id
;
1507 return (resp
->status
);
1518 ni_children_res
*resp
;
1520 if ((resp
= RCALLIT(ni
, _ni_children_2
, id
)) == NULL
) {
1521 clnt_debug(ni
, "_ni_children");
1524 if (resp
->status
== NI_OK
) {
1525 *children
= resp
->ni_children_res_u
.stuff
.children
;
1526 *id
= resp
->ni_children_res_u
.stuff
.self_id
;
1528 return (resp
->status
);
1541 ni_create_args args
;
1542 ni_create_res
*resp
;
1544 args
.id
= *parent_id
;
1547 args
.target_id
= NULL
;
1548 if ((resp
= WCALLIT(ni
, _ni_create_2
, &args
)) == NULL
) {
1549 clnt_debug(ni
, "_ni_create");
1552 if (resp
->status
== NI_OK
) {
1553 *child_id_p
= resp
->ni_create_res_u
.stuff
.id
;
1554 *parent_id
= resp
->ni_create_res_u
.stuff
.self_id
;
1556 return (resp
->status
);
1568 ni_destroy_args args
;
1570 args
.parent_id
= *parent_id
;
1571 args
.self_id
= self_id
;
1572 if ((resp
= WCALLIT(ni
, _ni_destroy_2
, &args
)) == NULL
) {
1573 clnt_debug(ni
, "_ni_destroy");
1576 if (resp
->status
== NI_OK
) {
1577 *parent_id
= resp
->ni_id_res_u
.id
;
1579 return (resp
->status
);
1590 ni_proplist_stuff args
;
1595 if ((resp
= WCALLIT(ni
, _ni_write_2
, &args
)) == NULL
) {
1596 clnt_debug(ni
, "_ni_write");
1599 if (resp
->status
== NI_OK
) {
1600 *self_id
= resp
->ni_id_res_u
.id
;
1602 return (resp
->status
);
1613 ni_proplist_res
*resp
;
1615 if ((resp
= RCALLIT(ni
, _ni_read_2
, self_id
)) == NULL
) {
1616 clnt_debug(ni
, "_ni_read");
1619 if (resp
->status
== NI_OK
) {
1620 *self_id
= resp
->ni_proplist_res_u
.stuff
.id
;
1621 *pl
= resp
->ni_proplist_res_u
.stuff
.props
;
1623 return (resp
->status
);
1631 ni_name_const pname
,
1636 ni_lookup_res
*resp
;
1637 ni_lookup_args args
;
1640 args
.key
= (ni_name
)pname
;
1641 args
.value
= (ni_name
)pval
;
1642 if ((resp
= RCALLIT(ni
, _ni_lookup_2
, &args
)) == NULL
) {
1643 clnt_debug(ni
, "_ni_lookup");
1646 if (resp
->status
== NI_OK
) {
1647 *hits
= resp
->ni_lookup_res_u
.stuff
.idlist
;
1648 *id
= resp
->ni_lookup_res_u
.stuff
.self_id
;
1650 return (resp
->status
);
1658 ni_name_const pname
,
1663 ni_proplist_res
*resp
;
1664 ni_lookup_args args
;
1667 args
.key
= (ni_name
)pname
;
1668 args
.value
= (ni_name
)pval
;
1669 if ((resp
= RCALLIT(ni
, _ni_lookupread_2
, &args
)) == NULL
) {
1670 clnt_debug(ni
, "_ni_lookupread");
1673 if (resp
->status
== NI_OK
) {
1674 *props
= resp
->ni_proplist_res_u
.stuff
.props
;
1675 *id
= resp
->ni_proplist_res_u
.stuff
.id
;
1677 return (resp
->status
);
1685 ni_name_const pname
,
1686 ni_entrylist
*entries
1693 args
.name
= (ni_name
)pname
;
1694 if ((resp
= RCALLIT(ni
, _ni_list_2
, &args
)) == NULL
) {
1695 clnt_debug(ni
, "_ni_list");
1698 if (resp
->status
== NI_OK
) {
1699 *entries
= resp
->ni_list_res_u
.stuff
.entries
;
1700 *id
= resp
->ni_list_res_u
.stuff
.self_id
;
1702 return (resp
->status
);
1710 ni_proplist_list
*entries
1713 ni_listall_res
*resp
;
1715 if ((resp
= RCALLIT(ni
, _ni_listall_2
, id
)) == NULL
) {
1716 clnt_debug(ni
, "_ni_listall");
1719 if (resp
->status
== NI_OK
) {
1720 *entries
= resp
->ni_listall_res_u
.stuff
.entries
;
1721 *id
= resp
->ni_listall_res_u
.stuff
.self_id
;
1723 return (resp
->status
);
1732 ni_namelist
*propval_p
1735 ni_namelist_res
*resp
;
1739 args
.prop_index
= which
;
1740 if ((resp
= RCALLIT(ni
, _ni_readprop_2
, &args
)) == NULL
) {
1741 clnt_debug(ni
, "_ni_readprop");
1744 if (resp
->status
== NI_OK
) {
1745 *propval_p
= resp
->ni_namelist_res_u
.stuff
.values
;
1746 *id
= resp
->ni_namelist_res_u
.stuff
.self_id
;
1748 return (resp
->status
);
1761 ni_writeprop_args args
;
1764 args
.prop_index
= which
;
1765 args
.values
= propval
;
1766 if ((resp
= WCALLIT(ni
, _ni_writeprop_2
, &args
)) == NULL
) {
1767 clnt_debug(ni
, "_ni_writeprop");
1770 if (resp
->status
== NI_OK
) {
1771 *id
= resp
->ni_id_res_u
.id
;
1773 return (resp
->status
);
1781 ni_namelist
*propnames
1784 ni_namelist_res
*resp
;
1786 if ((resp
= RCALLIT(ni
, _ni_listprops_2
, id
)) == NULL
) {
1787 clnt_debug(ni
, "_ni_listprops");
1790 if (resp
->status
== NI_OK
) {
1791 *propnames
= resp
->ni_namelist_res_u
.stuff
.values
;
1792 *id
= resp
->ni_namelist_res_u
.stuff
.self_id
;
1794 return (resp
->status
);
1807 ni_createprop_args args
;
1812 if ((resp
= WCALLIT(ni
, _ni_createprop_2
, &args
)) == NULL
) {
1813 clnt_debug(ni
, "_ni_createprop");
1816 if (resp
->status
== NI_OK
) {
1817 *id
= resp
->ni_id_res_u
.id
;
1819 return (resp
->status
);
1834 args
.prop_index
= which
;
1835 if ((resp
= WCALLIT(ni
, _ni_destroyprop_2
, &args
)) == NULL
) {
1836 clnt_debug(ni
, "_ni_destroyprop");
1839 if (resp
->status
== NI_OK
) {
1840 *id
= resp
->ni_id_res_u
.id
;
1842 return (resp
->status
);
1850 ni_index prop_index
,
1855 ni_propname_args args
;
1858 args
.prop_index
= prop_index
;
1859 args
.name
= (ni_name
)name
;
1860 if ((resp
= WCALLIT(ni
, _ni_renameprop_2
, &args
)) == NULL
) {
1861 clnt_debug(ni
, "_ni_renameprop");
1864 if (resp
->status
== NI_OK
) {
1865 *id
= resp
->ni_id_res_u
.id
;
1867 return (resp
->status
);
1875 ni_index prop_index
,
1881 ni_createname_args args
;
1884 args
.prop_index
= prop_index
;
1885 args
.name
= (ni_name
)name
;
1887 if ((resp
= WCALLIT(ni
, _ni_createname_2
, &args
)) == NULL
) {
1888 clnt_debug(ni
, "_ni_createname");
1891 if (resp
->status
== NI_OK
) {
1892 *id
= resp
->ni_id_res_u
.id
;
1894 return (resp
->status
);
1902 ni_index prop_index
,
1907 ni_nameindex_args args
;
1910 args
.prop_index
= prop_index
;
1911 args
.name_index
= name_index
;
1912 if ((resp
= WCALLIT(ni
, _ni_destroyname_2
, &args
)) == NULL
) {
1913 clnt_debug(ni
, "_ni_destroyname");
1916 if (resp
->status
== NI_OK
) {
1917 *id
= resp
->ni_id_res_u
.id
;
1919 return (resp
->status
);
1927 ni_index prop_index
,
1928 ni_index name_index
,
1933 ni_writename_args args
;
1936 args
.prop_index
= prop_index
;
1937 args
.name_index
= name_index
;
1938 args
.name
= (ni_name
)name
;
1939 if ((resp
= WCALLIT(ni
, _ni_writename_2
, &args
)) == NULL
) {
1940 clnt_debug(ni
, "_ni_writename");
1943 if (resp
->status
== NI_OK
) {
1944 *id
= resp
->ni_id_res_u
.id
;
1946 return (resp
->status
);
1954 ni_index prop_index
,
1955 ni_index name_index
,
1959 ni_readname_res
*resp
;
1960 ni_nameindex_args args
;
1963 args
.prop_index
= prop_index
;
1964 args
.name_index
= name_index
;
1965 if ((resp
= RCALLIT(ni
, _ni_readname_2
, &args
)) == NULL
) {
1966 clnt_debug(ni
, "_ni_readname");
1969 if (resp
->status
== NI_OK
) {
1970 *id
= resp
->ni_readname_res_u
.stuff
.id
;
1971 *name
= resp
->ni_readname_res_u
.stuff
.name
;
1973 return (resp
->status
);
1984 if ((resp
= (ni_status
*)RCALLIT(ni
, _ni_resync_2
, NULL
)) == NULL
) {
2003 NIP(ni
)->uid
= getuid();
2004 return (ni_setpassword(ni
, NULL
));
2007 if (ni_root(ni
, &id
) != NI_OK
) {
2011 if (ni_lookup(ni
, &id
, NAME_NAME
, NAME_USERS
, &ids
) != NI_OK
) {
2014 id
.nii_object
= ids
.niil_val
[0];
2015 ni_idlist_free(&ids
);
2018 if (ni_lookup(ni
, &id
, NAME_NAME
, user
, &ids
) != NI_OK
) {
2021 id
.nii_object
= ids
.niil_val
[0];
2022 ni_idlist_free(&ids
);
2024 if (ni_lookupprop(ni
, &id
, NAME_UID
, &nl
) != NI_OK
) {
2027 if (nl
.ninl_len
== 0) {
2030 for (p
= nl
.ninl_val
[0]; *p
; p
++) {
2032 ni_namelist_free(&nl
);
2036 NIP(ni
)->uid
= atoi(nl
.ninl_val
[0]);
2037 if (NIP(ni
)->passwd
== NULL
) {
2038 NIP(ni
)->passwd
= ni_name_dup("");
2040 createauth(NIP(ni
));
2048 ni_name_const passwd
2053 if (NIP(ni
)->passwd
!= NULL
) {
2054 ni_name_free(&NIP(ni
)->passwd
);
2056 if (passwd
== NULL
) {
2057 NIP(ni
)->passwd
= NULL
;
2058 if (NIP(ni
)->tc
!= NULL
) {
2059 auth_destroy(NIP(ni
)->tc
->cl_auth
);
2060 NIP(ni
)->tc
->cl_auth
= authnone_create();
2064 NIP(ni
)->passwd
= ni_name_dup(passwd
);
2066 * Our trivial encryption scheme
2068 for (p
= NIP(ni
)->passwd
; *p
; p
++) {
2071 createauth(NIP(ni
));
2076 extern int bindresvport(int, struct sockaddr_in
*);
2081 * The procedure pmap_getport_to below is derived
2082 * from Sun Microsystems RPC source code. As such the following
2083 * statement applies to it.:
2085 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
2086 * unrestricted use provided that this legend is included on all tape
2087 * media and as a part of the software program in whole or part. Users
2088 * may copy or modify Sun RPC without charge, but are not authorized
2089 * to license or distribute it to anyone else except as part of a product or
2090 * program developed by the user.
2092 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
2093 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
2094 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
2096 * Sun RPC is provided with no support and without any obligation on the
2097 * part of Sun Microsystems, Inc. to assist in its use, correction,
2098 * modification or enhancement.
2100 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2101 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2102 * OR ANY PART THEREOF.
2104 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2105 * or profits or other special, indirect and consequential damages, even if
2106 * Sun has been advised of the possibility of such damages.
2108 * Sun Microsystems, Inc.
2109 * 2550 Garcia Avenue
2110 * Mountain View, California 94043
2113 * Client interface to pmap rpc service.
2115 * Find the mapped port for program,version.
2116 * Calls the pmap service remotely to do the lookup.
2117 * Returns 0 if no map exists.
2120 pmap_getport_to(address
, program
, version
, protocol
, timeout_secs
, ntries
)
2121 struct sockaddr_in
*address
;
2130 register CLIENT
*client
;
2132 struct timeval timeout
;
2134 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
2138 address
->sin_port
= htons(PMAPPORT
);
2139 timeout
.tv_usec
= ((timeout_secs
% ntries
) * 1000000) / ntries
;
2140 timeout
.tv_sec
= (timeout_secs
/ ntries
);
2141 client
= clntudp_bufcreate(address
, PMAPPROG
,
2142 PMAPVERS
, timeout
, &sock
, RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
2143 if (client
!= (CLIENT
*)NULL
) {
2144 parms
.pm_prog
= program
;
2145 parms
.pm_vers
= version
;
2146 parms
.pm_prot
= protocol
;
2147 parms
.pm_port
= 0; /* not needed or used */
2148 timeout
.tv_usec
= 0;
2149 timeout
.tv_sec
= timeout_secs
;
2150 if (CLNT_CALL(client
, PMAPPROC_GETPORT
, xdr_pmap
, &parms
,
2151 xdr_u_short
, &port
, timeout
) != RPC_SUCCESS
){
2152 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
2153 clnt_geterr(client
, &rpc_createerr
.cf_error
);
2155 } else if (port
== 0) {
2156 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
2159 if (client
!= NULL
) {
2160 clnt_destroy(client
);
2163 address
->sin_port
= 0;
2169 * Open a socket, but do not use the default portmap timeout
2173 struct sockaddr_in
*raddr
,
2185 * If no port number given ask the pmap for one
2187 if (raddr
->sin_port
== 0) {
2189 if ((port
= pmap_getport_to(raddr
, prog
, vers
,
2190 IPPROTO_UDP
, timeout
,
2194 raddr
->sin_port
= htons(port
);
2197 sock
= socket(AF_INET
, proto
== IPPROTO_UDP
? SOCK_DGRAM
: SOCK_STREAM
,
2202 (void)bindresvport(sock
, (struct sockaddr_in
*)0);
2203 setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(int));
2204 if (proto
== IPPROTO_TCP
) {
2205 if (connect(sock
, (struct sockaddr
*)raddr
,
2206 sizeof(*raddr
)) < 0) {