diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2019-06-09 18:54:58 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2019-06-09 18:54:58 +0300 |
commit | c4dbd40477bb658cdc5c5e01b8f80057e587cd88 (patch) | |
tree | 73534b2cd2fff99e16c26ed4be8cf115cf4b3960 /src | |
parent | c33922f128403f5d05d24f19abeaad2368ce5467 (diff) | |
download | pies-c4dbd40477bb658cdc5c5e01b8f80057e587cd88.tar.gz pies-c4dbd40477bb658cdc5c5e01b8f80057e587cd88.tar.bz2 |
Test inetd components.
* lib/Makefile.am: Add urlconn.c
* lib/libpies.h (url_connect): New proto.
* lib/urlconn.c: New file.
* src/piesctl.c (shttp_connect): Use url_connect.
* tests/.gitignore: Update.
* tests/Makefile.am: New auxtool: aux/in.test
New test: inet.at
* tests/aux/in.test: New file.
* tests/inet.at: New file.
* tests/iobuf.h: New file.
* tests/nt.c: New file.
* tests/testsuite.at: Add new test.
* tests/to.c: explicitly terminate the child process on timeout.
Diffstat (limited to 'src')
-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 @@ -415,123 +415,22 @@ 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; } @@ -545,8 +444,7 @@ shttp_connect (struct pies_url *url, struct grecs_sockaddr *source_addr) 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); |