int connect_to_ip (const ip_address *ip, int port, const char *print) { struct sockaddr_storage ss; struct sockaddr *sa = (struct sockaddr *)&ss; int sock; /* If PRINT is non-NULL, print the "Connecting to..." line, with PRINT being the host name we're connecting to. */ if (print) { const char *txt_addr = print_address (ip); if (0 != strcmp (print, txt_addr)) { char *str = NULL, *name; if (opt.enable_iri && (name = idn_decode ((char *) print)) != NULL) { int len = strlen (print) + strlen (name) + 4; str = xmalloc (len); snprintf (str, len, "%s (%s)", name, print); str[len-1] = '\0'; xfree (name); } logprintf (LOG_VERBOSE, _("Connecting to %s|%s|:%d... "), str ? str : escnonprint_uri (print), txt_addr, port); if (str) xfree (str); } else { if (ip->family == AF_INET) logprintf (LOG_VERBOSE, _("Connecting to %s:%d... "), txt_addr, port); #ifdef ENABLE_IPV6 else if (ip->family == AF_INET6) logprintf (LOG_VERBOSE, _("Connecting to [%s]:%d... "), txt_addr, port); #endif } } /* Store the sockaddr info to SA. */ sockaddr_set_data (sa, ip, port); printf("The socket function reached!\n"); /* Create the socket of the family appropriate for the address. */ sock = socket (sa->sa_family, SOCK_STREAM, 0); if (sock < 0) goto err; #if defined(ENABLE_IPV6) && defined(IPV6_V6ONLY) if (opt.ipv6_only) { int on = 1; /* In case of error, we will go on anyway... */ int err = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)); IF_DEBUG if (err < 0) DEBUGP (("Failed setting IPV6_V6ONLY: %s", strerror (errno))); }
static bool resolve_bind_address (struct sockaddr *sa) { struct address_list *al; /* Make sure this is called only once. opt.bind_address doesn't change during a Wget run. */ static bool called, should_bind; static ip_address ip; if (called) { if (should_bind) sockaddr_set_data (sa, &ip, 0); return should_bind; } called = true; al = lookup_host (opt.bind_address, LH_BIND | LH_SILENT); if (!al) { /* #### We should be able to print the error message here. */ logprintf (LOG_NOTQUIET, _("%s: unable to resolve bind address `%s'; disabling bind.\n"), exec_name, opt.bind_address); should_bind = false; return false; } /* Pick the first address in the list and use it as bind address. Perhaps we should try multiple addresses in succession, but I don't think that's necessary in practice. */ ip = *address_list_address_at (al, 0); address_list_release (al); sockaddr_set_data (sa, &ip, 0); should_bind = true; return true; }
int connect_to_ip (const ip_address *ip, int port, const char *print) { struct sockaddr_storage ss; struct sockaddr *sa = (struct sockaddr *)&ss; int sock; /* If PRINT is non-NULL, print the "Connecting to..." line, with PRINT being the host name we're connecting to. */ if (print) { const char *txt_addr = print_address (ip); if (print && 0 != strcmp (print, txt_addr)) logprintf (LOG_VERBOSE, _("Connecting to %s|%s|:%d... "), escnonprint (print), txt_addr, port); else logprintf (LOG_VERBOSE, _("Connecting to %s:%d... "), txt_addr, port); } /* Store the sockaddr info to SA. */ sockaddr_set_data (sa, ip, port); /* Create the socket of the family appropriate for the address. */ sock = socket (sa->sa_family, SOCK_STREAM, 0); if (sock < 0) goto err; #if defined(ENABLE_IPV6) && defined(IPV6_V6ONLY) if (opt.ipv6_only) { int on = 1; /* In case of error, we will go on anyway... */ int err = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof (on)); IF_DEBUG if (err < 0) DEBUGP (("Failed setting IPV6_V6ONLY: %s", strerror (errno))); }