43ffd2f4ef63090e09331bd837cb61fd00a9b211
[packages/rpcbind.git] / rpcbind-tcp-addrs.patch
1 diff --git a/src/rpcbind.c b/src/rpcbind.c
2 index 63023e1..310c243 100644
3 --- a/src/rpcbind.c
4 +++ b/src/rpcbind.c
5 @@ -275,7 +275,7 @@ init_transport(struct netconfig *nconf)
6         int status;     /* bound checking ? */
7         int aicode;
8         int addrlen = 0;
9 -       int nhostsbak;
10 +       int nhostsbak = 1;
11         int checkbind;
12         int on = 1;
13         struct sockaddr *sa = NULL;
14 @@ -301,18 +301,6 @@ init_transport(struct netconfig *nconf)
15         }
16  #endif
17  
18 -       /*
19 -        * XXX - using RPC library internal functions. For NC_TPI_CLTS
20 -        * we call this later, for each socket we like to bind.
21 -        */
22 -       if (nconf->nc_semantics != NC_TPI_CLTS) {
23 -               if ((fd = __rpc_nconf2fd(nconf)) < 0) {
24 -                       syslog(LOG_ERR, "cannot create socket for %s",
25 -                           nconf->nc_netid);
26 -                       return (1);
27 -               }
28 -       }
29 -
30         if (!__rpc_nconf2sockinfo(nconf, &si)) {
31                 syslog(LOG_ERR, "cannot get information for %s",
32                     nconf->nc_netid);
33 @@ -335,8 +323,6 @@ init_transport(struct netconfig *nconf)
34                 hints.ai_family = si.si_af;
35                 hints.ai_socktype = si.si_socktype;
36                 hints.ai_protocol = si.si_proto;
37 -       }
38 -       if (nconf->nc_semantics == NC_TPI_CLTS) {
39                 /*
40                  * If no hosts were specified, just bind to INADDR_ANY.  Otherwise
41                  * make sure 127.0.0.1 is added to the list.
42 @@ -354,21 +340,15 @@ init_transport(struct netconfig *nconf)
43                         } else
44                                 return 1;
45                 }
46 -
47 -              /*
48 -               * Bind to specific IPs if asked to
49 -               */
50 -               checkbind = 0;
51 -               while (nhostsbak > 0) {
52 -                       --nhostsbak;
53 -                       /*
54 -                        * XXX - using RPC library internal functions.
55 -                        */
56 -                       if ((fd = __rpc_nconf2fd(nconf)) < 0) {
57 -                               syslog(LOG_ERR, "cannot create socket for %s",
58 -                                   nconf->nc_netid);
59 -                               return (1);
60 -                       }
61 +       }
62 +       /*
63 +       * Bind to specific IPs if asked to
64 +       */
65 +       checkbind = 0;
66 +       while (nhostsbak > 0) {
67 +               --nhostsbak;
68 +               if ((strcmp(nconf->nc_netid, "local") != 0) &&
69 +                   (strcmp(nconf->nc_netid, "unix") != 0)) {
70                         switch (hints.ai_family) {
71                         case AF_INET:
72                                 if (inet_pton(AF_INET, hosts[nhostsbak],
73 @@ -395,7 +375,7 @@ init_transport(struct netconfig *nconf)
74                                             host_addr) == 1)
75                                                 continue;
76                                 }
77 -                               break;
78 +                               break;
79                         default:
80                                 break;
81                         }
82 @@ -418,97 +398,37 @@ init_transport(struct netconfig *nconf)
83                         }
84                         addrlen = res->ai_addrlen;
85                         sa = (struct sockaddr *)res->ai_addr;
86 -                       oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
87 -                        if (bind(fd, sa, addrlen) != 0) {
88 -                               syslog(LOG_ERR, "cannot bind %s on %s: %m",
89 -                                       (hosts[nhostsbak] == NULL) ? "*" :
90 -                                       hosts[nhostsbak], nconf->nc_netid);
91 -                               if (res != NULL)
92 -                                       freeaddrinfo(res);
93 -                               continue;
94 -                       } else
95 -                               checkbind++;
96 -                       (void) umask(oldmask);
97 +               }
98  
99 -                       /* Copy the address */
100 -                       taddr.addr.maxlen = taddr.addr.len = addrlen;
101 -                       taddr.addr.buf = malloc(addrlen);
102 -                       if (taddr.addr.buf == NULL) {
103 -                               syslog(LOG_ERR,
104 -                                   "cannot allocate memory for %s address",
105 -                                   nconf->nc_netid);
106 +               /*
107 +                * XXX - using RPC library internal functions.
108 +                */
109 +               if ((fd = __rpc_nconf2fd(nconf)) < 0) {
110 +                       syslog(LOG_ERR, "cannot create socket for %s",
111 +                           nconf->nc_netid);
112 +                       continue;
113 +               }
114 +               oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
115 +               if (nconf->nc_semantics != NC_TPI_CLTS) {
116 +                       __rpc_fd2sockinfo(fd, &si);
117 +                       if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
118 +                                       sizeof(on)) != 0) {
119 +                               syslog(LOG_ERR, "cannot set SO_REUSEADDR on %s",
120 +                                       nconf->nc_netid);
121                                 if (res != NULL)
122                                         freeaddrinfo(res);
123 -                               return 1;
124 -                       }
125 -                       memcpy(taddr.addr.buf, sa, addrlen);
126 -#ifdef RPCBIND_DEBUG
127 -                       if (debugging) {
128 -                               /*
129 -                                * for debugging print out our universal
130 -                                * address
131 -                                */
132 -                               char *uaddr;
133 -                               struct netbuf nb;
134 -                               int sa_size = 0;
135 -
136 -                               nb.buf = sa;
137 -                               switch( sa->sa_family){
138 -                               case AF_INET:
139 -                                 sa_size = sizeof (struct sockaddr_in);
140 -                                 break;
141 -                               case AF_INET6:
142 -                                 sa_size = sizeof (struct sockaddr_in6);                                
143 -                                 break;
144 -                               }
145 -                               nb.len = nb.maxlen = sa_size;
146 -                               uaddr = taddr2uaddr(nconf, &nb);
147 -                               (void) fprintf(stderr,
148 -                                   "rpcbind : my address is %s\n", uaddr);
149 -                               (void) free(uaddr);
150 -                       }
151 -#endif
152 -                       my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, 
153 -                                RPC_MAXDATASIZE, RPC_MAXDATASIZE);
154 -                       if (my_xprt == (SVCXPRT *)NULL) {
155 -                               syslog(LOG_ERR, "%s: could not create service", 
156 -                                        nconf->nc_netid);
157 -                               goto error;
158 -                       }
159 -               }
160 -               if (!checkbind)
161 -                       return 1;
162 -       } else {        /* NC_TPI_COTS */
163 -               if ((strcmp(nconf->nc_netid, "local") != 0) &&
164 -                   (strcmp(nconf->nc_netid, "unix") != 0)) {
165 -                       if ((aicode = getaddrinfo(NULL, servname, &hints, &res))!= 0) {
166 -                         if ((aicode = getaddrinfo(NULL, "portmapper", &hints, &res))!= 0) {
167 -                         printf("cannot get local address for %s: %s",  nconf->nc_netid, gai_strerror(aicode));
168 -                         syslog(LOG_ERR,
169 -                                   "cannot get local address for %s: %s",
170 -                                   nconf->nc_netid, gai_strerror(aicode));
171 -                               return 1;
172 -                         }
173 +                               continue;
174                         }
175 -                       addrlen = res->ai_addrlen;
176 -                       sa = (struct sockaddr *)res->ai_addr;
177 -               }
178 -               oldmask = umask(S_IXUSR|S_IXGRP|S_IXOTH);
179 -               __rpc_fd2sockinfo(fd, &si);
180 -               if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
181 -                               sizeof(on)) != 0) {
182 -                       syslog(LOG_ERR, "cannot set SO_REUSEADDR on %s",
183 -                               nconf->nc_netid);
184 -                       if (res != NULL)
185 -                               freeaddrinfo(res);
186 -                       return 1;
187                 }
188                 if (bind(fd, sa, addrlen) < 0) {
189 -                       syslog(LOG_ERR, "cannot bind %s: %m", nconf->nc_netid);
190 +                       syslog(LOG_ERR, "cannot bind %s on %s: %m",
191 +                               (hosts && hosts[nhostsbak]) ? hosts[nhostsbak] : "*",
192 +                               nconf->nc_netid);
193                         if (res != NULL)
194                                 freeaddrinfo(res);
195 -                       return 1;
196 -               }
197 +                       continue;
198 +               } else
199 +                       checkbind++;
200                 (void) umask(oldmask);
201  
202                 /* Copy the address */
203 @@ -527,7 +447,7 @@ init_transport(struct netconfig *nconf)
204                         /* for debugging print out our universal address */
205                         char *uaddr;
206                         struct netbuf nb;
207 -                       int sa_size2 = 0;
208 +                       int sa_size2 = 0;
209  
210                         nb.buf = sa;
211                         switch( sa->sa_family){
212 @@ -546,7 +466,8 @@ init_transport(struct netconfig *nconf)
213                 }
214  #endif
215  
216 -               listen(fd, SOMAXCONN);
217 +               if (nconf->nc_semantics != NC_TPI_CLTS)
218 +                       listen(fd, SOMAXCONN);
219  
220                 my_xprt = (SVCXPRT *)svc_tli_create(fd, nconf, &taddr, RPC_MAXDATASIZE, RPC_MAXDATASIZE);
221                 if (my_xprt == (SVCXPRT *)NULL) {
222 @@ -555,6 +476,8 @@ init_transport(struct netconfig *nconf)
223                         goto error;
224                 }
225         }
226 +       if (!checkbind)
227 +               return 1;
228  
229  #ifdef PORTMAP
230         /*
231 diff --git a/man/rpcbind.8 b/man/rpcbind.8
232 index c5b8fb7..aa53a25 100644
233 --- a/man/rpcbind.8
234 +++ b/man/rpcbind.8
235 @@ -85,7 +85,7 @@ checks are shown in detail.
236  .It Fl f
237  Do not fork and become a background process.
238  .It Fl h
239 -Specify specific IP addresses to bind to for UDP requests.
240 +Specify specific IP addresses to bind to for requests.
241  This option
242  may be specified multiple times and is typically necessary when running
243  on a multi-homed host.