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>
39 #include "sys_interfaces.h"
41 #define LOCAL_PORT 1033
43 #define NI_TIMEOUT_SHORT 5 /* 5 second timeout for transactions */
44 #define NI_TIMEOUT_LONG 60 /* 60 second timeout for writes */
45 #define NI_TRIES 5 /* number of retries per timeout (udp only) */
46 #define NI_SLEEPTIME 4 /* 4 second sleeptime, in case of errors */
47 #define NI_MAXSLEEPTIME 64 /* 64 second max sleep time */
48 #define NI_MAXCONNTRIES 2 /* Try to form a connection twice before sleeping */
50 /* Hack for determining if an IP address is a broadcast address. -GRS */
51 /* Note that addr is network byte order (big endian) - BKM */
53 #define IS_BROADCASTADDR(addr) (((unsigned char *) &addr)[0] == 0xFF)
55 #ifndef INADDR_LOOPBACK
56 #define INADDR_LOOPBACK (u_long)0x7f000001
58 #define debug(msg) syslog(LOG_ERR, msg)
60 #define clnt_debug(ni, msg) /* do nothing */
62 typedef struct ni_private
{
63 int naddrs
; /* number of addresses */
64 struct in_addr
*addrs
; /* addresses of servers - network byte order */
65 int whichwrite
; /* which one of the above is the master */
66 ni_name
*tags
; /* tags of servers */
67 int pid
; /* pid, to detect forks */
68 int tsock
; /* tcp socket */
69 int tport
; /* tcp local port name - host byte order */
70 CLIENT
*tc
; /* tcp client */
71 long tv_sec
; /* timeout for this call */
72 long rtv_sec
; /* read timeout - 0 if default */
73 long wtv_sec
; /* write timeout - 0 if default */
74 int abort
; /* abort on timeout? */
75 int needwrite
; /* need to lock writes? */
76 int uid
; /* user id */
77 ni_name passwd
; /* password */
80 #define NIP(ni) ((ni_private *)(ni))
82 static const ni_name NAME_NAME
= "name";
83 static const ni_name NAME_SERVES
= "serves";
84 static const ni_name NAME_MACHINES
= "machines";
85 static const ni_name NAME_IP_ADDRESS
= "ip_address";
86 static const ni_name NAME_MASTER
= "master";
87 static const ni_name NAME_USERS
= "users";
88 static const ni_name NAME_UID
= "uid";
90 static const ni_name NAME_DOMAIN_SERVERS
= "domain_servers";
92 typedef struct getreg_stuff
{
93 nibind_getregister_res res
;
98 static int socket_open(struct sockaddr_in
*raddr
, int, int, int, int, int);
102 * Keep track of our port, in case somebody closes our socket
110 struct sockaddr_in sin
;
113 sinlen
= sizeof(sin
);
114 if (getsockname(sock
, (struct sockaddr
*)&sin
, &sinlen
) == 0) {
115 if (sin
.sin_port
== 0) {
116 (void)bind(sock
, (struct sockaddr
*)&sin
, sizeof(sin
));
117 sinlen
= sizeof(sin
);
118 (void)getsockname(sock
, (struct sockaddr
*)&sin
,
121 return (ntohs(sin
.sin_port
));
132 if (ni
->passwd
!= NULL
&& ni
->tc
!= NULL
) {
133 auth_destroy(ni
->tc
->cl_auth
);
134 ni
->tc
->cl_auth
= authunix_create(ni
->passwd
, ni
->uid
, 0, 0,
147 tv
->tv_sec
= sec
/ tries
;
148 tv
->tv_usec
= ((sec
% tries
) * 1000000) / tries
;
162 ni
->tv_sec
= timeout
;
163 if (ni
->tc
!= NULL
) {
164 clnt_control(ni
->tc
, CLSET_TIMEOUT
, &tv
);
170 * Connect to a given address/tag
173 connectit(ni_private
*ni
)
175 struct sockaddr_in sin
;
180 nibind_getregister_res res
;
183 bzero(&sin
, sizeof(sin
));
185 sin
.sin_family
= AF_INET
;
187 tv
.tv_sec
= ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
;
190 ni_settimeout(ni
, tv
.tv_sec
);
191 fixtimeout(&tv
, ni
->tv_sec
, NI_TRIES
);
194 * If connecting to local domain, try using the "well-known" port first.
196 if (!strcmp(ni
->tags
[0], "local"))
198 interface_list_t
*ilist
;
200 ilist
= sys_interfaces();
201 if (sys_is_my_address(ilist
, &ni
->addrs
[0]))
203 sin
.sin_port
= htons(LOCAL_PORT
);
204 sin
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
205 sock
= socket_open(&sin
, NI_PROG
, NI_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_TCP
);
207 sys_interfaces_release(ilist
);
211 * If connecting to a domain other than the local domain,
212 * or if connection to local didn't work with local's well-known port,
213 * then go through portmap & nibindd to find the port and connect.
218 sin
.sin_addr
= ni
->addrs
[0];
220 sock
= socket_open(&sin
, NIBIND_PROG
, NIBIND_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_UDP
);
221 if (sock
< 0) return (0);
223 cl
= clntudp_create(&sin
, NIBIND_PROG
, NIBIND_VERS
, tv
, &sock
);
230 tv
.tv_sec
= ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
;
233 stat
= clnt_call(cl
, NIBIND_GETREGISTER
, xdr_ni_name
, &ni
->tags
[0], xdr_nibind_getregister_res
, &res
, tv
);
236 if (stat
!= RPC_SUCCESS
|| res
.status
!= NI_OK
) return (0);
238 sin
.sin_port
= htons(res
.nibind_getregister_res_u
.addrs
.tcp_port
);
239 sock
= socket_open(&sin
, NI_PROG
, NI_VERS
, ni
->tv_sec
, NI_TRIES
, IPPROTO_TCP
);
242 if (sock
< 0) return (0);
244 cl
= clnttcp_create(&sin
, NI_PROG
, NI_VERS
, &sock
, 0, 0);
251 clnt_control(cl
, CLSET_TIMEOUT
, &tv
);
254 ni
->tport
= getmyport(sock
);
256 fcntl(ni
->tsock
, F_SETFD
, 1);
267 ((ni_private
*)ni
)->abort
= abort
;
277 ((ni_private
*)ni
)->wtv_sec
= timeout
;
287 ((ni_private
*)ni
)->rtv_sec
= timeout
;
297 ((ni_private
*)ni
)->needwrite
= needwrite
;
302 * Returns a client handle to the NetInfo server, if it's running
305 connectlocal(ni_private
*ni
)
310 ni
->addrs
= (struct in_addr
*)malloc(sizeof(struct in_addr
));
311 ni
->addrs
[0].s_addr
= htonl(INADDR_LOOPBACK
);
312 ni
->tags
= (ni_name
*)malloc(sizeof(ni_name
));
313 ni
->tags
[0] = ni_name_dup("local");
316 while (!connectit(ni
))
320 syslog(LOG_ERR
, "NetInfo timeout connecting to local domain, sleeping");
330 syslog(LOG_ERR
, "NetInfo connection to local domain waking");
338 * Destroy the client handle
349 if (sock
>= 0 && getmyport(sock
) != port
) {
351 * Somebody else has the descriptor open. Do not close it,
357 auth_destroy(cl
->cl_auth
);
362 * It's ours and we can close it
370 * Reinitialize everything
377 if (ni
->tc
!= NULL
) {
378 clnt_kill(ni
->tc
, ni
->tsock
, ni
->tport
);
388 * Switch to a new server
396 struct in_addr tmp_addr
;
403 tmp_addr
= ni
->addrs
[0];
404 tmp_tag
= ni
->tags
[0];
406 ni
->addrs
[0] = ni
->addrs
[which
];
407 ni
->tags
[0] = ni
->tags
[which
];
409 ni
->addrs
[which
] = tmp_addr
;
410 ni
->tags
[which
] = tmp_tag
;
412 if (ni
->whichwrite
== 0) {
413 ni
->whichwrite
= which
;
415 else if (ni
->whichwrite
== which
) {
422 * Swap two servers' positions
431 struct in_addr tmp_addr
;
436 tmp_addr
= ni
->addrs
[a
];
437 tmp_tag
= ni
->tags
[a
];
439 ni
->addrs
[a
] = ni
->addrs
[b
];
440 ni
->tags
[a
] = ni
->tags
[b
];
442 ni
->addrs
[b
] = tmp_addr
;
443 ni
->tags
[b
] = tmp_tag
;
445 if (ni
->whichwrite
== a
) {
448 else if (ni
->whichwrite
== b
) {
455 * Callback routine for multi_call
456 * XXX: should save returned port numbers
461 struct sockaddr_in
*sin
,
465 getreg_stuff
*stuff
= (getreg_stuff
*)vstuff
;
467 if (stuff
->res
.status
!= NI_OK
) {
470 ni_switch(stuff
->ni
, which
);
479 shuffle(ni_private
*ni
)
485 if (ni
->naddrs
<= 1) return;
487 rfd
= open("/dev/random", O_RDONLY
, 0);
488 shuffle
= (int *)malloc(ni
->naddrs
* sizeof(int));
489 for (i
= 0; i
< ni
->naddrs
; i
++) shuffle
[i
] = i
;
490 for (i
= 0, j
= ni
->naddrs
; j
> 0; i
++, j
--) {
495 /* get a random number */
497 (read(rfd
, &rVal
, sizeof(rVal
)) != sizeof(rVal
))) {
498 /* if we could not read from /dev/random */
499 static int initialized
= 0;
502 srandom(gethostid() ^ time(NULL
));
508 rEnt
= (unsigned int)rVal
% j
; /* pick one of the remaining entries */
509 tEnt
= shuffle
[rEnt
]; /* grab the random entry */
510 shuffle
[rEnt
] = shuffle
[j
-1]; /* the last entry moves to the random slot */
511 shuffle
[j
-1] = tEnt
; /* the last slot gets the random entry */
512 ni_swap(ni
, rEnt
, j
-1); /* and swap the actual NI addresses */
515 if (rfd
> 0) (void)close(rfd
);
527 int sleeptime
= NI_SLEEPTIME
;
531 interface_list_t
*ilist
;
534 if (ni
->naddrs
== 1) {
541 * re-order the servers so that:
542 * servers on the local host are at the start of the list, then
543 * servers on the local network are next, then
544 * all other servers are next
547 ilist
= sys_interfaces();
555 * move local servers to the head of the list
558 for (i
= nlocal
; i
< ni
->naddrs
; i
++) {
559 if (sys_is_my_address(ilist
, &ni
->addrs
[i
]))
561 ni_swap(ni
, nlocal
, i
);
567 * move servers on this network to follow local servers
570 for (i
= nnetwork
; i
< ni
->naddrs
; i
++) {
571 if (sys_is_my_network(ilist
, &ni
->addrs
[i
]) ||
572 IS_BROADCASTADDR(ni
->addrs
[i
].s_addr
))
574 ni_swap(ni
, nnetwork
, i
);
579 sys_interfaces_release(ilist
);
584 * call local servers first
587 for (i
= 0; i
< nlocal
; i
++) {
588 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (local %d)",
589 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
591 stat
= multi_call(nlocal
, ni
->addrs
,
592 NIBIND_PROG
, NIBIND_VERS
, NIBIND_GETREGISTER
,
593 xdr_ni_name
, ni
->tags
,
595 xdr_nibind_getregister_res
,
598 if (stat
== RPC_SUCCESS
) {
604 * call local servers and this network's servers
606 if (nnetwork
> nlocal
) {
607 for (i
= 0; i
< nnetwork
; i
++) {
608 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (network %d)",
609 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
611 stat
= multi_call(nnetwork
, ni
->addrs
,
612 NIBIND_PROG
, NIBIND_VERS
, NIBIND_GETREGISTER
,
613 xdr_ni_name
, ni
->tags
,
615 xdr_nibind_getregister_res
,
618 if (stat
== RPC_SUCCESS
) {
626 for (i
= 0; i
< ni
->naddrs
; i
++) {
627 syslog(LOG_DEBUG
, "NetInfo connect call to: %s/%s (world %d)",
628 inet_ntoa(ni
->addrs
[i
]), ni
->tags
[i
], i
);
630 stat
= multi_call(ni
->naddrs
,
631 ni
->addrs
, NIBIND_PROG
, NIBIND_VERS
,
633 xdr_ni_name
, ni
->tags
,
635 xdr_nibind_getregister_res
,
637 ni
->rtv_sec
== 0 ? NI_TIMEOUT_SHORT
: ni
->rtv_sec
);
638 if (stat
== RPC_SUCCESS
) {
646 if (ni
->whichwrite
>= 0) {
648 "NetInfo connect timeout (domain with master %s/%s), sleeping",
649 inet_ntoa(ni
->addrs
[ni
->whichwrite
]), ni
->tags
[ni
->whichwrite
]);
652 syslog(LOG_ERR
, "NetInfo connect timeout (domain with server %s/%s), sleeping",
653 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
658 if (sleeptime
< NI_MAXSLEEPTIME
) {
659 sleeptime
*= 2; /* backoff */
663 syslog(LOG_INFO
, "NetInfo connected to %s/%s", inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
666 syslog(LOG_ERR
, "NetInfo connected to %s/%s", inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
673 * Confirm that our tcp socket is still valid
681 if (ni
->tsock
!= -1) {
682 if (getmyport(ni
->tsock
) == ni
->tport
) {
686 * Somebody closed our socket. Do not close it, it could
687 * be owned by somebody else now.
691 if (ni
->tc
->cl_auth
!= NULL
) auth_destroy(ni
->tc
->cl_auth
);
692 clnt_destroy(ni
->tc
);
696 if (!needwrite
&& !rebind(ni
) && ni
->abort
) {
699 return (connectit(ni
));
719 if (ni
->naddrs
== 1) {
721 * One server - must be the master
726 needwrite
= ni
->needwrite
;
728 if (ni_root(ni
, &root
) != NI_OK
) {
729 ni
->needwrite
= needwrite
;
733 if (ni_lookupprop(ni
, &root
, NAME_MASTER
, &nl
) != NI_OK
) {
734 ni
->needwrite
= needwrite
;
737 if (nl
.ninl_len
== 0) {
738 ni
->needwrite
= needwrite
;
741 sep
= index(nl
.ninl_val
[0], '/');
743 ni
->needwrite
= needwrite
;
747 master
= nl
.ninl_val
[0];
749 if (ni_lookup(ni
, &root
, NAME_NAME
, NAME_MACHINES
, &idl
) != NI_OK
) {
750 ni
->needwrite
= needwrite
;
751 ni_namelist_free(&nl
);
754 if (idl
.niil_len
< 1) {
755 ni
->needwrite
= needwrite
;
758 id
.nii_object
= idl
.niil_val
[0];
759 ni_idlist_free(&idl
);
761 if (ni_lookup(ni
, &id
, NAME_NAME
, master
, &idl
) != NI_OK
) {
762 ni_namelist_free(&nl
);
763 ni
->needwrite
= needwrite
;
766 ni_namelist_free(&nl
);
767 if (idl
.niil_len
< 1) {
768 ni
->needwrite
= needwrite
;
771 id
.nii_object
= idl
.niil_val
[0];
772 ni_idlist_free(&idl
);
774 if (ni_lookupprop(ni
, &id
, NAME_IP_ADDRESS
, &nl
) != NI_OK
) {
777 for (i
= 0; i
< nl
.ninl_len
; i
++) {
778 addr
.s_addr
= inet_addr(nl
.ninl_val
[i
]);
779 for (j
= 0; j
< ni
->naddrs
; j
++) {
780 if (addr
.s_addr
== ni
->addrs
[j
].s_addr
) {
782 ni_namelist_free(&nl
);
783 ni
->needwrite
= needwrite
;
788 ni
->needwrite
= needwrite
;
789 ni_namelist_free(&nl
);
808 if (getpid() != ni
->pid
) {
811 if (needwrite
|| ni
->needwrite
) {
812 if (ni
->whichwrite
>= 0) {
813 ni_switch(ni
, ni
->whichwrite
);
815 if (!setmaster(ni
)) {
818 ni_switch(ni
, ni
->whichwrite
);
821 ni_settimeout(ni
, (ni
->rtv_sec
== 0 ?
822 NI_TIMEOUT_SHORT
: ni
->rtv_sec
));
825 ni_settimeout(ni
, (ni
->wtv_sec
== 0 ?
826 NI_TIMEOUT_LONG
: ni
->wtv_sec
));
829 ni_settimeout(ni
, (ni
->rtv_sec
== 0 ?
830 NI_TIMEOUT_SHORT
: ni
->rtv_sec
));
834 * Try more than once, in case server closed connection.
836 for (i
= 0; i
< NI_MAXCONNTRIES
; i
++) {
837 if (!confirm_tcp(ni
, needwrite
)) {
840 if ((resp
= (*stub
)(args
, ni
->tc
)) != NULL
) {
842 syslog(LOG_ERR
, "NetInfo connected to %s/%s",
843 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
849 clnt_geterr(ni
->tc
, &err
);
850 if (err
.re_status
!= RPC_CANTRECV
) break;
852 if (i
+ 1 < NI_MAXCONNTRIES
) {
854 * Server closed connection. Reinit and try
860 if (err
.re_status
== RPC_PROCUNAVAIL
) {
863 if (needwrite
|| ni
->abort
) {
865 * We time out for writes or if it is explicitly
872 "NetInfo connection failed for server %s/%s",
873 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
877 if (ni
->tc
!= NULL
) {
878 if (!(sleeptime
== 0 &&
879 err
.re_status
== RPC_TIMEDOUT
)) {
881 * Do not print message on
882 * first timeout. It is likely
883 * we will find another server soon.
884 * Let's not needlessly alarm the
887 syslog(LOG_ERR
, "%s on connection to %s/%s",
888 clnt_sperror(ni
->tc
,"NetInfo connection timeout"),
889 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
893 /* first attempt failed */
894 syslog(LOG_ERR
, "%s on initial connection to %s/%s",
895 clnt_sperror(ni
->tc
,"NetInfo connection timeout"),
896 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
900 "NetInfo connection failed for server %s/%s",
901 inet_ntoa(ni
->addrs
[0]), ni
->tags
[0]);
907 if (sleeptime
< NI_MAXSLEEPTIME
) {
908 sleeptime
*= 2; /* backoff */
912 * Do not sleep on the first timeout.
913 * It is likely we will find another server quickly.
915 sleeptime
= NI_SLEEPTIME
;
923 #define RCALLIT(a, b, c) callit((ni_private *)(a), (void *(*)())(b), \
927 #define WCALLIT(a, b, c) callit((ni_private *)(a), (void *(*)())(b), \
955 ni
= (ni_private
*)malloc(sizeof(*ni
));
962 ni
->tv_sec
= NI_TIMEOUT_SHORT
;
981 dupni
= (ni_private
*)ni_alloc();
984 dupni
->naddrs
= NIP(ni
)->naddrs
;
985 dupni
->whichwrite
= NIP(ni
)->whichwrite
;
986 if (dupni
->naddrs
> 0) {
987 dupni
->addrs
= ((struct in_addr
*)
988 malloc(NIP(ni
)->naddrs
* sizeof(struct in_addr
)));
989 bcopy(NIP(ni
)->addrs
, dupni
->addrs
,
990 NIP(ni
)->naddrs
* sizeof(struct in_addr
));
991 dupni
->tags
= ((ni_name
*)
992 malloc(NIP(ni
)->naddrs
* sizeof(ni_name
)));
993 for (i
= 0; i
< NIP(ni
)->naddrs
; i
++) {
994 dupni
->tags
[i
] = ni_name_dup(NIP(ni
)->tags
[i
]);
997 if (NIP(ni
)->passwd
!= NULL
) {
998 dupni
->passwd
= ni_name_dup(NIP(ni
)->passwd
);
1000 return ((void *)dupni
);
1011 int len
= strlen(domain
);
1014 sep
= index(domtag
, '/');
1018 if (strncmp(domain
, domtag
, len
) == 0 &&
1019 domtag
[len
] == '/') {
1020 *tag
= ni_name_dup(sep
+ 1);
1028 add_addr_tag(ni_private
*ni
, ni_name addrtag
)
1030 struct in_addr addr
;
1034 slash
= strchr(addrtag
, '/');
1035 if (slash
== NULL
) return;
1038 if (tag
[0] == '\0') return;
1042 if (inet_aton(addrtag
, &addr
) == 0) return;
1044 if (ni
->naddrs
== 0)
1046 ni
->addrs
= (struct in_addr
*)calloc(1, sizeof(struct in_addr
));
1047 if (ni
->addrs
== NULL
) return;
1049 ni
->tags
= (ni_name
*)calloc(1, sizeof(ni_name
));
1050 if (ni
->tags
== NULL
) return;
1054 ni
->addrs
= (struct in_addr
*)realloc(ni
->addrs
, ((ni
->naddrs
+ 1) * sizeof(struct in_addr
)));
1055 if (ni
->addrs
== NULL
) return;
1057 ni
->tags
= (ni_name
*)realloc(ni
->tags
, ((ni
->naddrs
+ 1) * sizeof(ni_name
)));
1058 if (ni
->tags
== NULL
) return;
1061 ni
->addrs
[ni
->naddrs
] = addr
;
1062 ni
->tags
[ni
->naddrs
] = ni_name_dup(tag
);
1067 addaddr(void *ni
, ni_index ido
, ni_name tag
, ni_private
*target_ni
)
1071 struct in_addr addr
;
1074 id
.nii_object
= ido
;
1076 if (ni_lookupprop(ni
, &id
, NAME_IP_ADDRESS
, &nl
) != NI_OK
) return 0;
1078 if (nl
.ni_namelist_len
== 0) return 0;
1080 if (target_ni
->naddrs
== 0)
1082 target_ni
->addrs
= (struct in_addr
*)malloc(nl
.ni_namelist_len
* sizeof(struct in_addr
));
1083 target_ni
->tags
= (ni_name
*)malloc(nl
.ni_namelist_len
* sizeof(ni_name
));
1087 target_ni
->addrs
= (struct in_addr
*)realloc(target_ni
->addrs
, ((target_ni
->naddrs
+ nl
.ni_namelist_len
) * sizeof(struct in_addr
)));
1088 target_ni
->tags
= (ni_name
*)realloc(target_ni
->tags
, ((target_ni
->naddrs
+ nl
.ni_namelist_len
) * sizeof(ni_name
)));
1091 for (i
= 0; i
< nl
.ni_namelist_len
; i
++)
1093 addr
.s_addr
= inet_addr(nl
.ni_namelist_val
[i
]);
1094 target_ni
->addrs
[target_ni
->naddrs
] = addr
;
1095 target_ni
->tags
[target_ni
->naddrs
] = ni_name_dup(tag
);
1096 target_ni
->naddrs
++;
1099 ni_namelist_free(&nl
);
1104 get_daddr(ni_private
*ni
, ni_name dom
, ni_private
*target_ni
)
1108 ni_entrylist entries
;
1114 if (dom
== NULL
) return 0;
1116 if (!strcmp(dom
, "."))
1118 /* check for server list */
1120 if (ni_statistics(ni
, &pl
) == NI_OK
)
1122 i
= ni_proplist_match(pl
, NAME_DOMAIN_SERVERS
, NULL
);
1123 if (i
!= NI_INDEX_NULL
)
1125 if (pl
.ni_proplist_val
[i
].nip_val
.ni_namelist_len
> 0)
1127 for (j
= 0; j
< pl
.ni_proplist_val
[i
].nip_val
.ni_namelist_len
; j
++)
1129 add_addr_tag(target_ni
, pl
.ni_proplist_val
[i
].nip_val
.ni_namelist_val
[j
]);
1131 ni_proplist_free(&pl
);
1135 ni_proplist_free(&pl
);
1139 if (ni_root(ni
, &nid
) != NI_OK
) return 0;
1141 if (ni_lookup(ni
, &nid
, NAME_NAME
, NAME_MACHINES
, &ids
) != NI_OK
) return 0;
1143 nid
.nii_object
= ids
.niil_val
[0];
1144 ni_idlist_free(&ids
);
1147 if (ni_list(ni
, &nid
, NAME_SERVES
, &entries
) != NI_OK
) return 0;
1149 for (i
= 0; i
< entries
.niel_len
; i
++)
1151 if (entries
.niel_val
[i
].names
== NULL
) continue;
1153 for (j
= 0; j
< entries
.niel_val
[i
].names
->ni_namelist_len
; j
++)
1155 if (match(dom
, entries
.niel_val
[i
].names
->ni_namelist_val
[j
], &tag
))
1157 addaddr(ni
, entries
.niel_val
[i
].id
, tag
, target_ni
);
1163 ni_entrylist_free(&entries
);
1164 return (target_ni
->naddrs
> 0);
1168 getparent(ni_private
*oldni
, ni_private
**newni
)
1170 ni_rparent_res
*resp
;
1171 ni_private
*ni
= NULL
;
1175 struct in_addr raddr
;
1182 * First, find our parent, any parent
1186 resp
= RCALLIT(oldni
, _ni_rparent_2
, NULL
);
1187 if (resp
== NULL
) return NI_FAILED
;
1188 if (resp
->status
!= NI_NORESPONSE
) break;
1192 syslog(LOG_ERR
, "NetInfo timeout finding server for parent of %s/%s, sleeping",
1193 inet_ntoa(oldni
->addrs
[0]), oldni
->tags
[0]);
1197 sleep(NI_SLEEPTIME
);
1202 raddr
.s_addr
= htonl(resp
->ni_rparent_res_u
.binding
.addr
);
1204 syslog(LOG_ERR
, "NetInfo %s/%s found parent %s/%s",
1205 inet_ntoa(oldni
->addrs
[0]), oldni
->tags
[0],
1206 inet_ntoa(raddr
), resp
->ni_rparent_res_u
.binding
.tag
);
1209 if (resp
->status
!= NI_OK
) return (resp
->status
);
1215 ni
->addrs
= (struct in_addr
*)malloc(sizeof(struct in_addr
));
1216 ni
->addrs
[0].s_addr
=htonl(resp
->ni_rparent_res_u
.binding
.addr
);
1217 ni
->tags
= (ni_name
*)malloc(sizeof(ni_name
));
1218 ni
->tags
[0] = ni_name_dup(resp
->ni_rparent_res_u
.binding
.tag
);
1220 xdr_free(xdr_ni_rparent_res
, resp
);
1226 if (get_daddr(dupni
, ".", ni
) == 0)
1228 if (oldni
->abort
== 1)
1237 * Make sure returned parent is head of list
1239 for (i
= 0; i
< ni
->naddrs
; i
++)
1241 if (ni
->addrs
[i
].s_addr
== dupni
->addrs
[0].s_addr
)
1250 * Reuse dupni client info
1252 ni
->tsock
= dupni
->tsock
;
1253 ni
->tport
= dupni
->tport
;
1261 * If returned parent wasn't in list, it's a rogue.
1262 * Log an error and drop the connection.
1266 syslog(LOG_ERR
, "Rogue NetInfo server detected: %s/%s",
1267 inet_ntoa(dupni
->addrs
[0]), dupni
->tags
[0]);
1281 if (ni
!= NULL
) ni_free(ni
);
1288 struct sockaddr_in
*sin
,
1295 NIP(ni
)->naddrs
= 1;
1296 NIP(ni
)->addrs
= (struct in_addr
*
1297 )malloc(sizeof(struct in_addr
));
1298 NIP(ni
)->addrs
[0] = sin
->sin_addr
;
1299 NIP(ni
)->tags
= (ni_name
*)malloc(sizeof(ni_name
));
1300 NIP(ni
)->tags
[0] = ni_name_dup(tag
);
1308 struct sockaddr_in
*addr
,
1313 if (!confirm_tcp(ni
, 0)) {
1316 *tag
= ni_name_dup(NIP(ni
)->tags
[0]);
1317 addr
->sin_addr
= NIP(ni
)->addrs
[0];
1318 addr
->sin_port
= htons(NIP(ni
)->tport
);
1319 addr
->sin_family
= AF_INET
;
1320 bzero(addr
->sin_zero
, sizeof(addr
->sin_zero
));
1326 ni_new(void *oldni
, const char *domain
)
1330 ni_name sep
, addr
, tag
;
1331 struct sockaddr_in sin
;
1334 if (domain
== NULL
) return NULL
;
1336 sep
= index(domain
, '@');
1339 tag
= strncpy((char *)malloc(sep
- domain
+ 1), domain
, sep
- domain
);
1340 tag
[sep
- domain
] = '\0';
1341 addr
= strcpy ((char *)malloc(strlen(sep
+ 1)), sep
+ 1);
1342 sin
.sin_addr
.s_addr
= inet_addr(addr
);
1343 if (sin
.sin_addr
.s_addr
== INADDR_NONE
)
1345 he
= gethostbyname(addr
);
1353 bcopy(he
->h_addr_list
[0], &sin
.sin_addr
.s_addr
, he
->h_length
);
1356 ni
= ni_connect(&sin
, tag
);
1368 if (connectlocal(ni
) == 0)
1374 if (strcmp(domain
, ".") == 0) return (void *)ni
;
1376 if (strcmp(domain
, "..") == 0)
1379 status
= getparent((ni_private
*)oldni
, &ni
);
1381 if (status
!= NI_OK
) return NULL
;
1389 if (strcmp(domain
, "..") == 0)
1391 status
= getparent((ni_private
*)oldni
, &ni
);
1392 if (status
!= NI_OK
) return NULL
;
1399 if (!get_daddr(oldni
, (ni_name
)domain
, ni
))
1416 if (NIP(ni
)->tc
!= NULL
) {
1417 clnt_kill(NIP(ni
)->tc
, NIP(ni
)->tsock
, NIP(ni
)->tport
);
1419 if (NIP(ni
)->naddrs
> 0) {
1420 free(NIP(ni
)->addrs
);
1421 for (i
= 0; i
< NIP(ni
)->naddrs
; i
++) {
1422 ni_name_free(&NIP(ni
)->tags
[i
]);
1424 free(NIP(ni
)->tags
);
1426 if (NIP(ni
)->passwd
!= NULL
) {
1427 ni_name_free(&NIP(ni
)->passwd
);
1434 * The rest of these are just wrappers that end up doing
1435 * RPC calls to the local NetInfo server.
1445 if ((resp
= (ni_proplist
*)RCALLIT(ni
, _ni_statistics_2
, NULL
))
1462 if ((resp
= RCALLIT(ni
, _ni_root_2
, id
)) == NULL
) {
1463 clnt_debug(ni
, "_ni_root");
1466 if (resp
->status
== NI_OK
) {
1467 *id
= resp
->ni_id_res_u
.id
;
1469 return (resp
->status
);
1481 if ((resp
= RCALLIT(ni
, _ni_self_2
, id
)) == NULL
) {
1482 clnt_debug(ni
, "_ni_self");
1485 if (resp
->status
== NI_OK
) {
1486 *id
= resp
->ni_id_res_u
.id
;
1488 return (resp
->status
);
1496 ni_index
*parent_id_p
1499 ni_parent_res
*resp
;
1501 if ((resp
= RCALLIT(ni
, _ni_parent_2
, id
)) == NULL
) {
1502 clnt_debug(ni
, "_ni_parent");
1505 if (resp
->status
== NI_OK
) {
1506 *parent_id_p
= resp
->ni_parent_res_u
.stuff
.object_id
;
1507 *id
= resp
->ni_parent_res_u
.stuff
.self_id
;
1509 return (resp
->status
);
1520 ni_children_res
*resp
;
1522 if ((resp
= RCALLIT(ni
, _ni_children_2
, id
)) == NULL
) {
1523 clnt_debug(ni
, "_ni_children");
1526 if (resp
->status
== NI_OK
) {
1527 *children
= resp
->ni_children_res_u
.stuff
.children
;
1528 *id
= resp
->ni_children_res_u
.stuff
.self_id
;
1530 return (resp
->status
);
1543 ni_create_args args
;
1544 ni_create_res
*resp
;
1546 args
.id
= *parent_id
;
1549 args
.target_id
= NULL
;
1550 if ((resp
= WCALLIT(ni
, _ni_create_2
, &args
)) == NULL
) {
1551 clnt_debug(ni
, "_ni_create");
1554 if (resp
->status
== NI_OK
) {
1555 *child_id_p
= resp
->ni_create_res_u
.stuff
.id
;
1556 *parent_id
= resp
->ni_create_res_u
.stuff
.self_id
;
1558 return (resp
->status
);
1570 ni_destroy_args args
;
1572 args
.parent_id
= *parent_id
;
1573 args
.self_id
= self_id
;
1574 if ((resp
= WCALLIT(ni
, _ni_destroy_2
, &args
)) == NULL
) {
1575 clnt_debug(ni
, "_ni_destroy");
1578 if (resp
->status
== NI_OK
) {
1579 *parent_id
= resp
->ni_id_res_u
.id
;
1581 return (resp
->status
);
1592 ni_proplist_stuff args
;
1597 if ((resp
= WCALLIT(ni
, _ni_write_2
, &args
)) == NULL
) {
1598 clnt_debug(ni
, "_ni_write");
1601 if (resp
->status
== NI_OK
) {
1602 *self_id
= resp
->ni_id_res_u
.id
;
1604 return (resp
->status
);
1615 ni_proplist_res
*resp
;
1617 if ((resp
= RCALLIT(ni
, _ni_read_2
, self_id
)) == NULL
) {
1618 clnt_debug(ni
, "_ni_read");
1621 if (resp
->status
== NI_OK
) {
1622 *self_id
= resp
->ni_proplist_res_u
.stuff
.id
;
1623 *pl
= resp
->ni_proplist_res_u
.stuff
.props
;
1625 return (resp
->status
);
1633 ni_name_const pname
,
1638 ni_lookup_res
*resp
;
1639 ni_lookup_args args
;
1642 args
.key
= (ni_name
)pname
;
1643 args
.value
= (ni_name
)pval
;
1644 if ((resp
= RCALLIT(ni
, _ni_lookup_2
, &args
)) == NULL
) {
1645 clnt_debug(ni
, "_ni_lookup");
1648 if (resp
->status
== NI_OK
) {
1649 *hits
= resp
->ni_lookup_res_u
.stuff
.idlist
;
1650 *id
= resp
->ni_lookup_res_u
.stuff
.self_id
;
1652 return (resp
->status
);
1660 ni_name_const pname
,
1665 ni_proplist_res
*resp
;
1666 ni_lookup_args args
;
1669 args
.key
= (ni_name
)pname
;
1670 args
.value
= (ni_name
)pval
;
1671 if ((resp
= RCALLIT(ni
, _ni_lookupread_2
, &args
)) == NULL
) {
1672 clnt_debug(ni
, "_ni_lookupread");
1675 if (resp
->status
== NI_OK
) {
1676 *props
= resp
->ni_proplist_res_u
.stuff
.props
;
1677 *id
= resp
->ni_proplist_res_u
.stuff
.id
;
1679 return (resp
->status
);
1687 ni_name_const pname
,
1688 ni_entrylist
*entries
1695 args
.name
= (ni_name
)pname
;
1696 if ((resp
= RCALLIT(ni
, _ni_list_2
, &args
)) == NULL
) {
1697 clnt_debug(ni
, "_ni_list");
1700 if (resp
->status
== NI_OK
) {
1701 *entries
= resp
->ni_list_res_u
.stuff
.entries
;
1702 *id
= resp
->ni_list_res_u
.stuff
.self_id
;
1704 return (resp
->status
);
1712 ni_proplist_list
*entries
1715 ni_listall_res
*resp
;
1717 if ((resp
= RCALLIT(ni
, _ni_listall_2
, id
)) == NULL
) {
1718 clnt_debug(ni
, "_ni_listall");
1721 if (resp
->status
== NI_OK
) {
1722 *entries
= resp
->ni_listall_res_u
.stuff
.entries
;
1723 *id
= resp
->ni_listall_res_u
.stuff
.self_id
;
1725 return (resp
->status
);
1734 ni_namelist
*propval_p
1737 ni_namelist_res
*resp
;
1741 args
.prop_index
= which
;
1742 if ((resp
= RCALLIT(ni
, _ni_readprop_2
, &args
)) == NULL
) {
1743 clnt_debug(ni
, "_ni_readprop");
1746 if (resp
->status
== NI_OK
) {
1747 *propval_p
= resp
->ni_namelist_res_u
.stuff
.values
;
1748 *id
= resp
->ni_namelist_res_u
.stuff
.self_id
;
1750 return (resp
->status
);
1763 ni_writeprop_args args
;
1766 args
.prop_index
= which
;
1767 args
.values
= propval
;
1768 if ((resp
= WCALLIT(ni
, _ni_writeprop_2
, &args
)) == NULL
) {
1769 clnt_debug(ni
, "_ni_writeprop");
1772 if (resp
->status
== NI_OK
) {
1773 *id
= resp
->ni_id_res_u
.id
;
1775 return (resp
->status
);
1783 ni_namelist
*propnames
1786 ni_namelist_res
*resp
;
1788 if ((resp
= RCALLIT(ni
, _ni_listprops_2
, id
)) == NULL
) {
1789 clnt_debug(ni
, "_ni_listprops");
1792 if (resp
->status
== NI_OK
) {
1793 *propnames
= resp
->ni_namelist_res_u
.stuff
.values
;
1794 *id
= resp
->ni_namelist_res_u
.stuff
.self_id
;
1796 return (resp
->status
);
1809 ni_createprop_args args
;
1814 if ((resp
= WCALLIT(ni
, _ni_createprop_2
, &args
)) == NULL
) {
1815 clnt_debug(ni
, "_ni_createprop");
1818 if (resp
->status
== NI_OK
) {
1819 *id
= resp
->ni_id_res_u
.id
;
1821 return (resp
->status
);
1836 args
.prop_index
= which
;
1837 if ((resp
= WCALLIT(ni
, _ni_destroyprop_2
, &args
)) == NULL
) {
1838 clnt_debug(ni
, "_ni_destroyprop");
1841 if (resp
->status
== NI_OK
) {
1842 *id
= resp
->ni_id_res_u
.id
;
1844 return (resp
->status
);
1852 ni_index prop_index
,
1857 ni_propname_args args
;
1860 args
.prop_index
= prop_index
;
1861 args
.name
= (ni_name
)name
;
1862 if ((resp
= WCALLIT(ni
, _ni_renameprop_2
, &args
)) == NULL
) {
1863 clnt_debug(ni
, "_ni_renameprop");
1866 if (resp
->status
== NI_OK
) {
1867 *id
= resp
->ni_id_res_u
.id
;
1869 return (resp
->status
);
1877 ni_index prop_index
,
1883 ni_createname_args args
;
1886 args
.prop_index
= prop_index
;
1887 args
.name
= (ni_name
)name
;
1889 if ((resp
= WCALLIT(ni
, _ni_createname_2
, &args
)) == NULL
) {
1890 clnt_debug(ni
, "_ni_createname");
1893 if (resp
->status
== NI_OK
) {
1894 *id
= resp
->ni_id_res_u
.id
;
1896 return (resp
->status
);
1904 ni_index prop_index
,
1909 ni_nameindex_args args
;
1912 args
.prop_index
= prop_index
;
1913 args
.name_index
= name_index
;
1914 if ((resp
= WCALLIT(ni
, _ni_destroyname_2
, &args
)) == NULL
) {
1915 clnt_debug(ni
, "_ni_destroyname");
1918 if (resp
->status
== NI_OK
) {
1919 *id
= resp
->ni_id_res_u
.id
;
1921 return (resp
->status
);
1929 ni_index prop_index
,
1930 ni_index name_index
,
1935 ni_writename_args args
;
1938 args
.prop_index
= prop_index
;
1939 args
.name_index
= name_index
;
1940 args
.name
= (ni_name
)name
;
1941 if ((resp
= WCALLIT(ni
, _ni_writename_2
, &args
)) == NULL
) {
1942 clnt_debug(ni
, "_ni_writename");
1945 if (resp
->status
== NI_OK
) {
1946 *id
= resp
->ni_id_res_u
.id
;
1948 return (resp
->status
);
1956 ni_index prop_index
,
1957 ni_index name_index
,
1961 ni_readname_res
*resp
;
1962 ni_nameindex_args args
;
1965 args
.prop_index
= prop_index
;
1966 args
.name_index
= name_index
;
1967 if ((resp
= RCALLIT(ni
, _ni_readname_2
, &args
)) == NULL
) {
1968 clnt_debug(ni
, "_ni_readname");
1971 if (resp
->status
== NI_OK
) {
1972 *id
= resp
->ni_readname_res_u
.stuff
.id
;
1973 *name
= resp
->ni_readname_res_u
.stuff
.name
;
1975 return (resp
->status
);
1986 if ((resp
= (ni_status
*)RCALLIT(ni
, _ni_resync_2
, NULL
)) == NULL
) {
2005 NIP(ni
)->uid
= getuid();
2006 return (ni_setpassword(ni
, NULL
));
2009 if (ni_root(ni
, &id
) != NI_OK
) {
2013 if (ni_lookup(ni
, &id
, NAME_NAME
, NAME_USERS
, &ids
) != NI_OK
) {
2016 id
.nii_object
= ids
.niil_val
[0];
2017 ni_idlist_free(&ids
);
2020 if (ni_lookup(ni
, &id
, NAME_NAME
, user
, &ids
) != NI_OK
) {
2023 id
.nii_object
= ids
.niil_val
[0];
2024 ni_idlist_free(&ids
);
2026 if (ni_lookupprop(ni
, &id
, NAME_UID
, &nl
) != NI_OK
) {
2029 if (nl
.ninl_len
== 0) {
2032 for (p
= nl
.ninl_val
[0]; *p
; p
++) {
2034 ni_namelist_free(&nl
);
2038 NIP(ni
)->uid
= atoi(nl
.ninl_val
[0]);
2039 if (NIP(ni
)->passwd
== NULL
) {
2040 NIP(ni
)->passwd
= ni_name_dup("");
2042 createauth(NIP(ni
));
2050 ni_name_const passwd
2055 if (NIP(ni
)->passwd
!= NULL
) {
2056 ni_name_free(&NIP(ni
)->passwd
);
2058 if (passwd
== NULL
) {
2059 NIP(ni
)->passwd
= NULL
;
2060 if (NIP(ni
)->tc
!= NULL
) {
2061 auth_destroy(NIP(ni
)->tc
->cl_auth
);
2062 NIP(ni
)->tc
->cl_auth
= authnone_create();
2066 NIP(ni
)->passwd
= ni_name_dup(passwd
);
2068 * Our trivial encryption scheme
2070 for (p
= NIP(ni
)->passwd
; *p
; p
++) {
2073 createauth(NIP(ni
));
2078 extern int bindresvport(int, struct sockaddr_in
*);
2083 * The procedure pmap_getport_to below is derived
2084 * from Sun Microsystems RPC source code. As such the following
2085 * statement applies to it.:
2087 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
2088 * unrestricted use provided that this legend is included on all tape
2089 * media and as a part of the software program in whole or part. Users
2090 * may copy or modify Sun RPC without charge, but are not authorized
2091 * to license or distribute it to anyone else except as part of a product or
2092 * program developed by the user.
2094 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
2095 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
2096 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
2098 * Sun RPC is provided with no support and without any obligation on the
2099 * part of Sun Microsystems, Inc. to assist in its use, correction,
2100 * modification or enhancement.
2102 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2103 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2104 * OR ANY PART THEREOF.
2106 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2107 * or profits or other special, indirect and consequential damages, even if
2108 * Sun has been advised of the possibility of such damages.
2110 * Sun Microsystems, Inc.
2111 * 2550 Garcia Avenue
2112 * Mountain View, California 94043
2115 * Client interface to pmap rpc service.
2117 * Find the mapped port for program,version.
2118 * Calls the pmap service remotely to do the lookup.
2119 * Returns 0 if no map exists.
2122 pmap_getport_to(address
, program
, version
, protocol
, timeout_secs
, ntries
)
2123 struct sockaddr_in
*address
;
2132 register CLIENT
*client
;
2134 struct timeval timeout
;
2136 sock
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
);
2140 address
->sin_port
= htons(PMAPPORT
);
2141 timeout
.tv_usec
= ((timeout_secs
% ntries
) * 1000000) / ntries
;
2142 timeout
.tv_sec
= (timeout_secs
/ ntries
);
2143 client
= clntudp_bufcreate(address
, PMAPPROG
,
2144 PMAPVERS
, timeout
, &sock
, RPCSMALLMSGSIZE
, RPCSMALLMSGSIZE
);
2145 if (client
!= (CLIENT
*)NULL
) {
2146 parms
.pm_prog
= program
;
2147 parms
.pm_vers
= version
;
2148 parms
.pm_prot
= protocol
;
2149 parms
.pm_port
= 0; /* not needed or used */
2150 timeout
.tv_usec
= 0;
2151 timeout
.tv_sec
= timeout_secs
;
2152 if (CLNT_CALL(client
, PMAPPROC_GETPORT
, xdr_pmap
, &parms
,
2153 xdr_u_short
, &port
, timeout
) != RPC_SUCCESS
){
2154 rpc_createerr
.cf_stat
= RPC_PMAPFAILURE
;
2155 clnt_geterr(client
, &rpc_createerr
.cf_error
);
2157 } else if (port
== 0) {
2158 rpc_createerr
.cf_stat
= RPC_PROGNOTREGISTERED
;
2161 if (client
!= NULL
) {
2162 clnt_destroy(client
);
2165 address
->sin_port
= 0;
2171 * Open a socket, but do not use the default portmap timeout
2175 struct sockaddr_in
*raddr
,
2183 struct sockaddr_in bindsin
;
2187 memset(&bindsin
, 0, sizeof(bindsin
));
2190 * If no port number given ask the pmap for one
2192 if (raddr
->sin_port
== 0) {
2194 if ((port
= pmap_getport_to(raddr
, prog
, vers
,
2195 IPPROTO_UDP
, timeout
,
2199 raddr
->sin_port
= htons(port
);
2202 sock
= socket(AF_INET
, proto
== IPPROTO_UDP ? SOCK_DGRAM
: SOCK_STREAM
,
2207 if (-1 == bindresvport(sock
, NULL
) && errno
== EADDRNOTAVAIL
) {
2208 /* XXX - we're hitting this case way too often under load */
2209 /* fail gracefully: some address is better than none most of the time */
2210 syslog(LOG_DEBUG
, "Libinfo[%s():%d] bindresvport(): %m", __func__
, __LINE__
);
2211 if (-1 == bind(sock
, &bindsin
, sizeof(bindsin
))) {
2212 /* The system is really sad now if it can't give me any address... */
2213 syslog(LOG_DEBUG
, "Libinfo[%s():%d] bind(): %m", __func__
, __LINE__
);
2218 setsockopt(sock
, SOL_SOCKET
, SO_REUSEPORT
, &reuse
, sizeof(int));
2219 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &reuse
, sizeof(int));
2220 if (proto
== IPPROTO_TCP
) {
2221 if (connect(sock
, (struct sockaddr
*)raddr
,
2222 sizeof(*raddr
)) < 0) {
2223 syslog(LOG_DEBUG
, "Libinfo[%s():%d] connect(): %m", __func__
, __LINE__
);