- struct hostent *hp;
- struct sockaddr_in sock;
- char host_name[200];
- int res;
- int one=1;
-
- /* get my host name */
- if (gethostname(host_name, sizeof(host_name)) == -1) {
- rprintf(FERROR,"gethostname failed\n");
- return -1;
- }
-
- /* get host info */
- if ((hp = gethostbyname(host_name)) == 0) {
- rprintf(FERROR,"gethostbyname: Unknown host %s\n",host_name);
- return -1;
+ int type = SOCK_STREAM;
+ int error, s;
+ struct addrinfo hints, *res0, *res;
+ char portbuf[10];
+ char *h, *cp;
+ int proxied = 0;
+ char buffer[1024];
+ char *proxy_user = NULL, *proxy_pass = NULL;
+
+ /* if we have a RSYNC_PROXY env variable then redirect our
+ * connetcion via a web proxy at the given address. */
+ h = getenv("RSYNC_PROXY");
+ proxied = h != NULL && *h != '\0';
+
+ if (proxied) {
+ strlcpy(buffer, h, sizeof buffer);
+
+ /* Is the USER:PASS@ prefix present? */
+ if ((cp = strrchr(buffer, '@')) != NULL) {
+ *cp++ = '\0';
+ /* The remainder is the HOST:PORT part. */
+ h = cp;
+
+ if ((cp = strchr(buffer, ':')) == NULL) {
+ rprintf(FERROR,
+ "invalid proxy specification: should be USER:PASS@HOST:PORT\n");
+ return -1;
+ }
+ *cp++ = '\0';
+
+ proxy_user = buffer;
+ proxy_pass = cp;
+ } else {
+ /* The whole buffer is the HOST:PORT part. */
+ h = buffer;
+ }
+
+ if ((cp = strchr(h, ':')) == NULL) {
+ rprintf(FERROR,
+ "invalid proxy specification: should be HOST:PORT\n");
+ return -1;
+ }
+ *cp++ = '\0';
+ strlcpy(portbuf, cp, sizeof portbuf);
+ if (verbose >= 2) {
+ rprintf(FINFO, "connection via http proxy %s port %s\n",
+ h, portbuf);
+ }
+ } else {
+ snprintf(portbuf, sizeof portbuf, "%d", port);
+ h = host;