net.c (146188) | net.c (241852) |
---|---|
1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 25 unchanged lines hidden (view full) --- 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * From: @(#)common.c 8.5 (Berkeley) 4/28/95 39 */ 40 41#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ | 1/* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 25 unchanged lines hidden (view full) --- 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * From: @(#)common.c 8.5 (Berkeley) 4/28/95 39 */ 40 41#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ |
42__FBSDID("$FreeBSD: head/usr.sbin/lpr/common_source/net.c 146188 2005-05-13 16:43:28Z ume $"); | 42__FBSDID("$FreeBSD: head/usr.sbin/lpr/common_source/net.c 241852 2012-10-22 03:31:22Z eadler $"); |
43 44#include <sys/param.h> 45#include <sys/socket.h> 46#include <sys/stat.h> 47#include <sys/time.h> 48#include <sys/uio.h> 49 50#include <netinet/in.h> 51#include <arpa/inet.h> 52#include <netdb.h> 53 54#include <dirent.h> /* required for lp.h, not used here */ | 43 44#include <sys/param.h> 45#include <sys/socket.h> 46#include <sys/stat.h> 47#include <sys/time.h> 48#include <sys/uio.h> 49 50#include <netinet/in.h> 51#include <arpa/inet.h> 52#include <netdb.h> 53 54#include <dirent.h> /* required for lp.h, not used here */ |
55#include <err.h> |
|
55#include <errno.h> 56#include <stdarg.h> 57#include <stdio.h> 58#include <stdlib.h> 59#include <string.h> 60#include <unistd.h> 61 62#include "lp.h" --- 23 unchanged lines hidden (view full) --- 86 * If rport == 0, then use the printer service port. 87 * Most of this code comes from rcmd.c. 88 */ 89int 90getport(const struct printer *pp, const char *rhost, int rport) 91{ 92 struct addrinfo hints, *res, *ai; 93 int s, timo = 1, lport = IPPORT_RESERVED - 1; | 56#include <errno.h> 57#include <stdarg.h> 58#include <stdio.h> 59#include <stdlib.h> 60#include <string.h> 61#include <unistd.h> 62 63#include "lp.h" --- 23 unchanged lines hidden (view full) --- 87 * If rport == 0, then use the printer service port. 88 * Most of this code comes from rcmd.c. 89 */ 90int 91getport(const struct printer *pp, const char *rhost, int rport) 92{ 93 struct addrinfo hints, *res, *ai; 94 int s, timo = 1, lport = IPPORT_RESERVED - 1; |
94 int err, refused = 0; | 95 int error, refused = 0; |
95 96 /* 97 * Get the host address and port number to connect to. 98 */ 99 if (rhost == NULL) 100 fatal(pp, "no remote host to connect to"); 101 memset(&hints, 0, sizeof(hints)); 102 hints.ai_family = family; 103 hints.ai_socktype = SOCK_STREAM; 104 hints.ai_protocol = 0; | 96 97 /* 98 * Get the host address and port number to connect to. 99 */ 100 if (rhost == NULL) 101 fatal(pp, "no remote host to connect to"); 102 memset(&hints, 0, sizeof(hints)); 103 hints.ai_family = family; 104 hints.ai_socktype = SOCK_STREAM; 105 hints.ai_protocol = 0; |
105 err = getaddrinfo(rhost, (rport == 0 ? "printer" : NULL), | 106 error = getaddrinfo(rhost, (rport == 0 ? "printer" : NULL), |
106 &hints, &res); | 107 &hints, &res); |
107 if (err) 108 fatal(pp, "%s\n", gai_strerror(err)); | 108 if (error) 109 fatal(pp, "%s\n", gai_strerror(error)); |
109 if (rport != 0) 110 ((struct sockaddr_in *) res->ai_addr)->sin_port = htons(rport); 111 112 /* 113 * Try connecting to the server. 114 */ 115 ai = res; 116retry: | 110 if (rport != 0) 111 ((struct sockaddr_in *) res->ai_addr)->sin_port = htons(rport); 112 113 /* 114 * Try connecting to the server. 115 */ 116 ai = res; 117retry: |
117 seteuid(euid); | 118 PRIV_START |
118 s = rresvport_af(&lport, ai->ai_family); | 119 s = rresvport_af(&lport, ai->ai_family); |
119 seteuid(uid); | 120 PRIV_END |
120 if (s < 0) { 121 if (errno != EAGAIN) { 122 if (ai->ai_next) { 123 ai = ai->ai_next; 124 goto retry; 125 } 126 if (refused && timo <= 16) { 127 sleep(timo); 128 timo *= 2; 129 refused = 0; 130 ai = res; 131 goto retry; 132 } 133 } 134 freeaddrinfo(res); 135 return(-1); 136 } 137 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { | 121 if (s < 0) { 122 if (errno != EAGAIN) { 123 if (ai->ai_next) { 124 ai = ai->ai_next; 125 goto retry; 126 } 127 if (refused && timo <= 16) { 128 sleep(timo); 129 timo *= 2; 130 refused = 0; 131 ai = res; 132 goto retry; 133 } 134 } 135 freeaddrinfo(res); 136 return(-1); 137 } 138 if (connect(s, ai->ai_addr, ai->ai_addrlen) < 0) { |
138 err = errno; | 139 error = errno; |
139 (void) close(s); | 140 (void) close(s); |
140 errno = err; | 141 errno = error; |
141 /* 142 * This used to decrement lport, but the current semantics 143 * of rresvport do not provide such a function (in fact, 144 * rresvport should guarantee that the chosen port will 145 * never result in an EADDRINUSE). 146 */ 147 if (errno == EADDRINUSE) { 148 goto retry; --- 30 unchanged lines hidden (view full) --- 179 * The return value is an error message which must be 180 * free()d. 181 */ 182char * 183checkremote(struct printer *pp) 184{ 185 char lclhost[MAXHOSTNAMELEN]; 186 struct addrinfo hints, *local_res, *remote_res, *lr, *rr; | 142 /* 143 * This used to decrement lport, but the current semantics 144 * of rresvport do not provide such a function (in fact, 145 * rresvport should guarantee that the chosen port will 146 * never result in an EADDRINUSE). 147 */ 148 if (errno == EADDRINUSE) { 149 goto retry; --- 30 unchanged lines hidden (view full) --- 180 * The return value is an error message which must be 181 * free()d. 182 */ 183char * 184checkremote(struct printer *pp) 185{ 186 char lclhost[MAXHOSTNAMELEN]; 187 struct addrinfo hints, *local_res, *remote_res, *lr, *rr; |
187 char *err; 188 int ncommonaddrs, error; | 188 char *error; 189 int ncommonaddrs, errno; |
189 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 190 191 if (!pp->rp_matches_local) { /* Remote printer doesn't match local */ 192 pp->remote = 1; 193 return NULL; 194 } 195 196 pp->remote = 0; /* assume printer is local */ 197 if (pp->remote_host == NULL) 198 return NULL; 199 200 /* get the addresses of the local host */ 201 gethostname(lclhost, sizeof(lclhost)); 202 lclhost[sizeof(lclhost) - 1] = '\0'; 203 204 memset(&hints, 0, sizeof(hints)); 205 hints.ai_family = family; 206 hints.ai_socktype = SOCK_STREAM; 207 hints.ai_flags = AI_PASSIVE; | 190 char h1[NI_MAXHOST], h2[NI_MAXHOST]; 191 192 if (!pp->rp_matches_local) { /* Remote printer doesn't match local */ 193 pp->remote = 1; 194 return NULL; 195 } 196 197 pp->remote = 0; /* assume printer is local */ 198 if (pp->remote_host == NULL) 199 return NULL; 200 201 /* get the addresses of the local host */ 202 gethostname(lclhost, sizeof(lclhost)); 203 lclhost[sizeof(lclhost) - 1] = '\0'; 204 205 memset(&hints, 0, sizeof(hints)); 206 hints.ai_family = family; 207 hints.ai_socktype = SOCK_STREAM; 208 hints.ai_flags = AI_PASSIVE; |
208 if ((error = getaddrinfo(lclhost, NULL, &hints, &local_res)) != 0) { 209 asprintf(&err, "unable to get official name " | 209 if ((errno = getaddrinfo(lclhost, NULL, &hints, &local_res)) != 0) { 210 asprintf(&error, "unable to get official name " |
210 "for local machine %s: %s", | 211 "for local machine %s: %s", |
211 lclhost, gai_strerror(error)); 212 return err; | 212 lclhost, gai_strerror(errno)); 213 return error; |
213 } 214 215 /* get the official name of RM */ 216 memset(&hints, 0, sizeof(hints)); 217 hints.ai_family = family; 218 hints.ai_socktype = SOCK_STREAM; 219 hints.ai_flags = AI_PASSIVE; | 214 } 215 216 /* get the official name of RM */ 217 memset(&hints, 0, sizeof(hints)); 218 hints.ai_family = family; 219 hints.ai_socktype = SOCK_STREAM; 220 hints.ai_flags = AI_PASSIVE; |
220 if ((error = getaddrinfo(pp->remote_host, NULL, | 221 if ((errno = getaddrinfo(pp->remote_host, NULL, |
221 &hints, &remote_res)) != 0) { | 222 &hints, &remote_res)) != 0) { |
222 asprintf(&err, "unable to get address list for " | 223 asprintf(&error, "unable to get address list for " |
223 "remote machine %s: %s", | 224 "remote machine %s: %s", |
224 pp->remote_host, gai_strerror(error)); | 225 pp->remote_host, gai_strerror(errno)); |
225 freeaddrinfo(local_res); | 226 freeaddrinfo(local_res); |
226 return err; | 227 return error; |
227 } 228 229 ncommonaddrs = 0; 230 for (lr = local_res; lr; lr = lr->ai_next) { 231 h1[0] = '\0'; 232 if (getnameinfo(lr->ai_addr, lr->ai_addrlen, h1, sizeof(h1), 233 NULL, 0, NI_NUMERICHOST) != 0) 234 continue; --- 65 unchanged lines hidden --- | 228 } 229 230 ncommonaddrs = 0; 231 for (lr = local_res; lr; lr = lr->ai_next) { 232 h1[0] = '\0'; 233 if (getnameinfo(lr->ai_addr, lr->ai_addrlen, h1, sizeof(h1), 234 NULL, 0, NI_NUMERICHOST) != 0) 235 continue; --- 65 unchanged lines hidden --- |