From a0413d85fa8f8336678d3a66807d69b4556afbf6 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 21 Feb 2024 22:50:23 +0100 Subject: cmd/socksfs: support connecting to ipv4 --- src/cmd/socksfs.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/cmd/socksfs.c b/src/cmd/socksfs.c index 9469d0c..d594edf 100644 --- a/src/cmd/socksfs.c +++ b/src/cmd/socksfs.c @@ -1,13 +1,14 @@ +#include +#include #include -#include #include +#include #include #include #include #include -#include -#include #include +#include typedef struct { hid_t sock; @@ -34,18 +35,15 @@ fs_open(char *path, int flags) { return errno = ENOSYS, NULL; // unimpl. } - // TODO ip parsing in stdlib - // uint32_t srcip; - //if (ip_parse(strtok_r(NULL, "/", &tokp), &srcip) < 0 || srcip != 0) { - // return errno = ENOENT, NULL; - //} - strtok_r(NULL, "/", &tokp); + struct in_addr srcip; + if (inet_aton(strtok_r(NULL, "/", &tokp), &srcip) == 0 || srcip.s_addr != 0) { + return errno = ENOENT, NULL; + } char *dest = strtok_r(NULL, "/", &tokp); if (!dest) return errno = ENOENT, NULL; size_t destlen = strlen(dest); if (destlen >= 256) return errno = ENAMETOOLONG, NULL; - // let's assume it's a domain char *proto = strtok_r(NULL, "/", &tokp); if (!proto) return errno = ENOENT, NULL; @@ -81,10 +79,20 @@ fs_open(char *path, int flags) { buf[p++] = 5; /* v5 */ buf[p++] = 1; /* tcp connection */ buf[p++] = 0; /* reserved */ - buf[p++] = 3; /* addr type, 3 = domain */ - buf[p++] = destlen; /* length, max 255 */ - memcpy(buf + p, dest, destlen); - p += destlen; + + struct in_addr dstip; + if (inet_aton(dest, &dstip) == 1) { /* it's a valid ip */ + buf[p++] = 1; /* addr type, 1 = IPv4 */ + /* this looks wrong, but in_addr is already in network byte order */ + memcpy(buf + p, &dstip, 4); + p += 4; + } else { /* it's not an ip, so it's probably a domain */ + buf[p++] = 3; /* addr type, 3 = domain */ + buf[p++] = destlen; /* length, max 255 */ + memcpy(buf + p, dest, destlen); + p += destlen; + } + buf[p++] = port >> 8; buf[p++] = port; @@ -117,7 +125,6 @@ int main(int argc, char *argv[]) { struct ufs_request req; hid_t reqh; // TODO an fs_wait flag to only wait for OPENs, and then to only wait for a given handle - // then i can use proper threads while ((reqh = ufs_wait(buf, buflen, &req))) { Handle *h = req.id; switch (req.op) { -- cgit v1.2.3