diff options
Diffstat (limited to 'src/piesctl.c')
-rw-r--r-- | src/piesctl.c | 122 |
1 files changed, 10 insertions, 112 deletions
diff --git a/src/piesctl.c b/src/piesctl.c index 2e35aea..aa983ee 100644 --- a/src/piesctl.c +++ b/src/piesctl.c @@ -406,156 +406,54 @@ shttp_io_header_get (struct shttp_io *io, char const *name) static void shttp_io_free (struct shttp_io *io) { grecs_symtab_free (io->headers); grecs_free (io->content); } static struct shttp_connection * shttp_connect (struct pies_url *url, struct grecs_sockaddr *source_addr) { int fd; - union pies_sockaddr_storage addr; - socklen_t socklen; - int flags; + struct pies_url *conn_url; struct shttp_connection *conn; FILE *fp; - - if (strcmp (url->scheme, "unix") == 0 - || strcmp (url->scheme, "file") == 0 - || strcmp (url->scheme, "socket") == 0) - { - struct stat st; - - if (url->port) - { - grecs_error (NULL, 0, _("%s: invalid connection type: " - "port is meaningless for UNIX sockets"), - url->string); - return NULL; - } - - if (strlen (url->path) > sizeof addr.s_un.sun_path) - { - errno = EINVAL; - grecs_error (NULL, 0, - _("%s: UNIX socket name too long"), - url->path); - return NULL; - } - - addr.s.sa_family = PF_UNIX; - socklen = sizeof (addr.s_un); - strcpy (addr.s_un.sun_path, url->path); - - if (stat (url->path, &st)) - { - if (errno != ENOENT) - { - grecs_error (NULL, errno, _("%s: %s failed"), url->path, "stat"); - return NULL; - } - } - else - { - if (!S_ISSOCK (st.st_mode)) - { - grecs_error (NULL, 0, _("%s: not a socket"), url->path); - return NULL; - } - } - } - else if (strcmp (url->scheme, "inet") == 0) - { - short pnum; - struct hostent *hp; - - addr.s_in.sin_family = PF_INET; - socklen = sizeof (addr.s_in); - - pnum = url->port ? url->port : 8080; - - hp = gethostbyname (url->host); - if (!hp) - { - grecs_error (NULL, 0, _("%s: unknown host name"), url->string); - return NULL; - } - - addr.s_in.sin_family = hp->h_addrtype; - switch (hp->h_addrtype) - { - case AF_INET: - memmove (&addr.s_in.sin_addr, hp->h_addr, 4); - addr.s_in.sin_port = htons (pnum); - break; - - default: - grecs_error (NULL, 0, - _("%s: invalid connection type: " - "unsupported address family"), - url->string); - return NULL; - } - } - else - { - grecs_error (NULL, 0, _("%s: unsupported protocol"), url->string); - return NULL; - } - fd = socket (addr.s.sa_family, SOCK_STREAM, 0); - if (fd == -1) - { - grecs_error (NULL, errno, _("%s: %s failed"), url->string, "socket"); - return NULL; - } + if (pies_url_copy (&conn_url, url)) + grecs_alloc_die (); - if ((flags = fcntl (fd, F_GETFD, 0)) == -1 - || fcntl (fd, F_SETFD, flags | FD_CLOEXEC) == -1) - grecs_error (NULL, 0, _("%s: cannot set close-on-exec: %s"), - url->string, strerror (errno)); - - if (source_addr) + if (conn_url->port == 0 && strcmp (conn_url->scheme, "inet") == 0) { - if (source_addr->sa->sa_family != addr.s.sa_family) - grecs_error (NULL, 0, - _("source and destination address family differ")); - else if (bind (fd, source_addr->sa, source_addr->len) < 0) - { - grecs_error (NULL, errno, _("%s: %s failed"), url->string, "bind"); - exit (EX_UNAVAILABLE); - } + conn_url->port_s = grecs_strdup ("8080"); + conn_url->port = 8080; } - - if (connect (fd, &addr.s, socklen)) + fd = url_connect (conn_url, source_addr); + if (fd == -1) { - grecs_error (NULL, errno, _("%s: %s failed"), url->string, "connect"); - close (fd); + pies_url_destroy (&conn_url); return NULL; } fp = fdopen (fd, "w+"); if (!fp) { grecs_error (NULL, errno, _("%s: %s failed"), url->string, "fdopen"); close (fd); return NULL; } conn = grecs_zalloc (sizeof (*conn)); conn->fp = fp; - if (pies_url_copy (&conn->url, url)) - grecs_alloc_die (); + conn->url = conn_url; if (!no_netrc_option) netrc_scan (conn->url); return conn; } static void shttp_auth_free (struct shttp_connection *conn) { if (conn->b64auth) { |