Skip to content

Commit

Permalink
add an option for HTTP-ish responses
Browse files Browse the repository at this point in the history
(faked well enough, anyway)
  • Loading branch information
mirabilos committed Jun 16, 2024
1 parent 143c919 commit 80507cb
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 6 deletions.
17 changes: 15 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ where it’s needed.

To run, use something like:

$ docker run --rm -p 1024/tcp -p 1024/udp --name tellmyip -d saachenzip:latest
$ docker run --rm --name tellmyip -d \
-p 1024/tcp -p 1024/udp \
saachenzip:latest
$ docker inspect tellmyip | sed -n '/^.*"IPAddress": "\(.*\)",*$/s//\1/p' | sort -u
⇒ this outputs the IP address of the container, e.g. 172.17.0.2
$ docker logs -tf tellmyip
Expand All @@ -30,7 +32,9 @@ don’t have apparmour enabled (see the comment in Build.sh).
Another possibility is with host networking and specifying
the port(s) manually (multiple possible, default 1024):

$ docker run --net=host --rm --name tellmyip -d saachenzip:latest \
$ docker run --rm --name tellmyip -d \
--net=host \
saachenzip:latest \
/saachenzip 2001 2002 2003 ::/2004 127.0.0.1/2005
$ docker logs -tf tellmyip
… use it (see below)
Expand Down Expand Up @@ -80,6 +84,15 @@ timestamp is normally POSIX time_t in microsecond accuracy, that
is, seconds since 1970 minus the amount of leap seconds since 1970
(i.e. days have constant 86400 seconds).

If HTTP responses are desired invoke as above with explicit ports
and pass a sole ‘H’ (uppercase) as first argument to enable:

$ docker run --rm --name tellmyip -d \
-p 1024/tcp -p 1024/udp \
saachenzip:latest \
/saachenzip H 1024
$ curl http://172.17.0.2:1024/

The image was developed assuming a Docker Linux/amd64 runtime, the
binary would be compiled using musl libc for sanity. It most likely
is portable to other architectures if not common operating systems.
Expand Down
70 changes: 66 additions & 4 deletions src/saachenzip.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static const char licence_header[] __attribute__((__used__)) =
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "mbsdcc.h"
#include "mbsdint.h"
Expand Down Expand Up @@ -95,6 +96,7 @@ static void handle_fd(int);
static const char protoname[2][4] = { "udp", "tcp" };

static volatile sig_atomic_t gotsig;
static unsigned char fauxhttp;

#define cscpy(dst,src) memcpy(dst, src, sizeof(src))

Expand Down Expand Up @@ -244,11 +246,15 @@ doconf(struct configuration *confp, char **argv, int argc)
maxfd = 0;
fdnby = 0;

if (argc < 2)
errx(1, "Usage: %s [<host>/]<port> […]",
if (argc < 2 || (argc < 3 && argv[1][0] == 'H' && !argv[1][1]))
errx(1, "Usage: %s [H] [<host>/]<port> […]",
argc > 0 && *argv && **argv ? *argv : "saachenzip");

j = 0;
if (argv[1][0] == 'H' && !argv[1][1]) {
++j;
fauxhttp = 1;
}
while (++j < argc) {
if (!(cp = strdup(argv[j])))
err(1, "strdup");
Expand Down Expand Up @@ -376,6 +382,8 @@ static void
handle_fd(int fd)
{
static char buf[1024];
static char hdrbuf[160];
static char tmbuf[32];
int i;
unsigned char istcp;
socklen_t saclen, saslen;
Expand All @@ -391,6 +399,8 @@ handle_fd(int fd)
char sport[6], cport[6];
char sfamily[LKFAMILYLEN], cfamily[LKFAMILYLEN];

hdrbuf[0] = '\0';

saclen = sizeof(i);
errno = EILSEQ;
if (getsockopt(fd, SOL_SOCKET, SO_TYPE, &i, &saclen) ||
Expand Down Expand Up @@ -442,8 +452,60 @@ handle_fd(int fd)
tv.tv_sec = -1;
tv.tv_usec = 0;
}
if (fauxhttp) {
struct tm *tm;

/* avoid strftime to avoid timezone dependency */
errno = ETXTBSY;
if ((tm = gmtime(&tv.tv_sec))) {
/* RFC-mandated exact strings and UTC */
static const char xday[8][4] = {
"Sun", "Mon", "Tue", "Wed",
"Thu", "Fri", "Sat", "Sun",
};
static const char xmonth[12][4] = {
"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec",
};
if (tm->tm_mon < 0 || tm->tm_mon > 11 ||
tm->tm_wday < 0 || tm->tm_wday > 7 ||
/* HTTP-mandated limits */
tm->tm_year < -1899 || tm->tm_year > 8099) {
warnx("out-of-spec struct tm");
goto fallbacktime;
}
if (tm->tm_sec > 59)
tm->tm_sec = 59; /* wrong but HTTP-mandated */
errno = ETXTBSY;
i = snprintf(tmbuf, sizeof(tmbuf),
"%s, %02d %s %04d %02d:%02d:%02d GMT",
xday[tm->tm_wday], tm->tm_mday,
xmonth[tm->tm_mon], (int)(tm->tm_year + 1900),
tm->tm_hour, tm->tm_min, tm->tm_sec);
if (i < 1 || (size_t)i >= sizeof(tmbuf)) {
warn("snprintf%s", ", using dummy time");
goto fallbacktime;
}
} else {
warn("gmtime");
fallbacktime:
cscpy(tmbuf, "Wed, 31 Dec 1969 23:59:59 GMT");
}
errno = ETXTBSY;
i = snprintf(hdrbuf, sizeof(hdrbuf),
"HTTP/1.0 200 OK\r\n"
"Date: %s\r\n"
"Expires: %s\r\n"
"Content-Type: text/plain; charset=UTF-8\r\n"
"\r\n", tmbuf, tmbuf);
if (i < 1 || (size_t)i >= sizeof(hdrbuf)) {
warn("snprintf%s", ", using mini header");
cscpy(hdrbuf, "HTTP/1.0 200 uh-oh\r\n\r\n");
}
}
errno = ETXTBSY;
i = snprintf(buf, sizeof(buf),
i = snprintf(buf, sizeof(buf), "%s"
"{\n \"client-l3\": \"%s\""
",\n \"client-l4\": \"%s\""
",\n \"client-host\": \"%s\""
Expand All @@ -453,7 +515,7 @@ handle_fd(int fd)
",\n \"server-host\": \"%s\""
",\n \"server-port\": \"%s\""
",\n \"timestamp\": %lld.%06ld"
"\n}\n",
"\n}\n", hdrbuf,
cfamily, protoname[istcp], chost, cport,
sfamily, protoname[istcp], shost, sport,
(long long)tv.tv_sec, (long)tv.tv_usec);
Expand Down

0 comments on commit 80507cb

Please sign in to comment.