aboutsummaryrefslogtreecommitdiff
path: root/src/piesctl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/piesctl.c')
-rw-r--r--src/piesctl.c122
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
@@ -412,129 +412,28 @@ shttp_io_free (struct shttp_io *io)
}
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)
{
@@ -542,14 +441,13 @@ shttp_connect (struct pies_url *url, struct grecs_sockaddr *source_addr)
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;
}

Return to:

Send suggestions and report system problems to the System administrator.