diff options
author | dzwdz | 2024-02-21 22:50:23 +0100 |
---|---|---|
committer | dzwdz | 2024-02-21 22:50:23 +0100 |
commit | a0413d85fa8f8336678d3a66807d69b4556afbf6 (patch) | |
tree | 45c52c692b3c7aa616fe41fcc55ea5a1c66e5283 /src/cmd | |
parent | b694ae71492a6261d2ea91b93dff78bc8789ea55 (diff) |
cmd/socksfs: support connecting to ipv4
Diffstat (limited to 'src/cmd')
-rw-r--r-- | src/cmd/socksfs.c | 37 |
1 files changed, 22 insertions, 15 deletions
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 <arpa/inet.h> +#include <camellia.h> #include <camellia/flags.h> -#include <camellia/syscalls.h> #include <camellia/fs/misc.h> +#include <camellia/syscalls.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <unistd.h> -#include <camellia.h> #include <sys/param.h> +#include <unistd.h> 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) { |