aboutsummaryrefslogtreecommitdiffstats
path: root/src/client
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2024-01-30 20:51:38 +0000
committerLaurent Bercot <ska@appnovation.com>2024-01-30 20:51:38 +0000
commit21d51f7e0a639a3224ffc45dc3c06854decf1d45 (patch)
tree87c4a36f66510e79d7ec8315361bb8bbbb5767f5 /src/client
downloadapaste-21d51f7e0a639a3224ffc45dc3c06854decf1d45.tar.gz
Initial commit
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/client')
-rw-r--r--src/client/PROTOCOL.txt19
-rw-r--r--src/client/apaste.c101
-rw-r--r--src/client/apastec.c155
-rw-r--r--src/client/apastec.h17
-rw-r--r--src/client/deps-exe/apaste1
-rw-r--r--src/client/deps-exe/apastec4
-rw-r--r--src/client/send_file.c93
7 files changed, 390 insertions, 0 deletions
diff --git a/src/client/PROTOCOL.txt b/src/client/PROTOCOL.txt
new file mode 100644
index 0000000..6e828ca
--- /dev/null
+++ b/src/client/PROTOCOL.txt
@@ -0,0 +1,19 @@
+
+apastec:
+
+banner \n
+nfiles (uint32) \n
+{
+ namelength (uint16) \n
+ name (namelength) \n
+ filelength (uint64) \n
+ file (filelength) \n
+}
+banner \n
+
+
+apasted:
+
+banner \n
+slug \n
+banner \n
diff --git a/src/client/apaste.c b/src/client/apaste.c
new file mode 100644
index 0000000..1d2a739
--- /dev/null
+++ b/src/client/apaste.c
@@ -0,0 +1,101 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+
+#include <skalibs/uint16.h>
+#include <skalibs/uint32.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/strerr.h>
+#include <skalibs/exec.h>
+
+#include <s6-networking/config.h>
+
+#include <apaste/config.h>
+
+#define USAGE "apaste [ -S | -s ] [ -C cadir ] [ -d server[:port] ] [ -r rtimeout ] [ -w wtimeout ] file..."
+#define dieusage() strerr_dieusage(100, USAGE)
+
+int main (int argc, char *const *argv)
+{
+ char const *cadir = APASTE_DEFAULT_CADIR ;
+ char const *server = APASTE_DEFAULT_SERVER ;
+ int dotls = APASTE_DEFAULT_TLS ;
+ uint32_t rt = 0 ;
+ uint32_t wt = 0 ;
+ uint16_t port = 0 ;
+ PROG = "apaste" ;
+
+ {
+ subgetopt l = SUBGETOPT_ZERO ;
+ for (;;)
+ {
+ int opt = subgetopt_r(argc, (char const *const *)argv, "SsC:d:r:w:", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'S' : dotls = 0 ; break ;
+ case 's' : dotls = 1 ; break ;
+ case 'C' : cadir = l.arg ; break ;
+ case 'd' :
+ {
+ char *colon = strchr(l.arg, ':') ;
+ server = l.arg ;
+ if (colon)
+ {
+ *colon = 0 ;
+ if (!uint160_scan(colon + 1, &port)) dieusage() ;
+ }
+ break ;
+ }
+ case 'r' : if (!uint320_scan(l.arg, &rt)) dieusage() ; break ;
+ case 'w' : if (!uint320_scan(l.arg, &wt)) dieusage() ; break ;
+ default : dieusage() ;
+ }
+ }
+ argc -= l.ind ; argv += l.ind ;
+ }
+ if (!argc) dieusage() ;
+ if (!port) port = dotls ? APASTE_DEFAULT_TLSPORT : APASTE_DEFAULT_PORT ;
+ {
+ char const *newargv[12 + argc] ;
+ unsigned int m = 0 ;
+ char fmtr[UINT32_FMT] ;
+ char fmtw[UINT32_FMT] ;
+ char fmtp[UINT16_FMT] ;
+
+ fmtp[uint16_fmt(fmtp, port)] = 0 ;
+
+ newargv[m++] = dotls ? S6_NETWORKING_EXTBINPREFIX "s6-tlsclient" : S6_NETWORKING_EXTBINPREFIX "s6-tcpclient" ;
+ newargv[m++] = "-N" ;
+ newargv[m++] = "--" ;
+ newargv[m++] = server ;
+ newargv[m++] = fmtp ;
+ newargv[m++] = APASTE_BINPREFIX "apastec" ;
+ if (rt)
+ {
+ fmtr[uint32_fmt(fmtr, rt)] = 0 ;
+ newargv[m++] = "-r" ;
+ newargv[m++] = fmtr ;
+ }
+ if (rt)
+ {
+ fmtw[uint32_fmt(fmtw, wt)] = 0 ;
+ newargv[m++] = "-w" ;
+ newargv[m++] = fmtw ;
+ }
+ newargv[m++] = "--" ;
+ while (argc--) newargv[m++] = *argv++ ;
+ newargv[m++] = 0 ;
+
+ if (dotls)
+ {
+ size_t len = strlen(cadir) ;
+ char modif[7 + len] ;
+ memcpy(modif, "CADIR=", 6) ;
+ memcpy(modif + 6, cadir, len + 1) ;
+ xmexec_n(newargv, modif, len + 7, 1) ;
+ }
+ else xexec(newargv) ;
+ }
+}
diff --git a/src/client/apastec.c b/src/client/apastec.c
new file mode 100644
index 0000000..3237ceb
--- /dev/null
+++ b/src/client/apastec.c
@@ -0,0 +1,155 @@
+/* ISC license. */
+
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <signal.h>
+
+#include <skalibs/posixplz.h>
+#include <skalibs/uint16.h>
+#include <skalibs/uint32.h>
+#include <skalibs/uint64.h>
+#include <skalibs/buffer.h>
+#include <skalibs/sgetopt.h>
+#include <skalibs/strerr.h>
+#include <skalibs/tai.h>
+#include <skalibs/sig.h>
+#include <skalibs/djbunix.h>
+#include <skalibs/unix-timed.h>
+
+#include <apaste/config.h>
+#include "apaste-common.h"
+#include "apastec.h"
+
+#define USAGE "apastec [ -r rtimeout ] [ -w wtimeout ] file..."
+#define dieusage() strerr_dieusage(100, USAGE)
+
+static void apaste_send (buffer *b, char const *file, tain const *deadline)
+{
+ int fd ;
+ struct stat st ;
+ size_t len = strlen(file) ;
+ if (!len) strerr_dief2x(100, "empty file", " names are invalid") ;
+ if (len > UINT16_MAX) strerr_dief2x(100, "file name too long: ", file) ;
+ if (len == 1 && file[0] == '-')
+ {
+ char tmp[sizeof(APASTE_TMPDIR) + 14] = APASTE_TMPDIR "/apaste:XXXXXX" ;
+ fd = mkstemp(tmp) ;
+ if (fd == -1) strerr_diefu2sys(111, "mkstemp ", tmp) ;
+ unlink_void(tmp) ;
+ if (fd_cat(0, fd) == -1) strerr_diefu2sys(111, "copy stdin to ", tmp) ;
+ if (lseek(fd, 0, SEEK_SET) == -1) strerr_diefu1sys(111, "lseek") ;
+ ndelay_on(fd) ;
+ }
+ else
+ {
+ if (file[len-1] == '/') strerr_dief2x(100, "directory", " names are invalid") ;
+ fd = open_read(file) ;
+ if (fd == -1) strerr_diefu2sys(111, "open ", file) ;
+ }
+
+ if (fstat(fd, &st) == -1) strerr_diefu2sys(111, "stat ", file) ;
+ if (!S_ISREG(st.st_mode)) strerr_dief3x(100, "file ", file, " is not a regular file") ;
+ {
+ char fmt[UINT64_FMT] ;
+ size_t m = uint16_fmt(fmt, len) ;
+ fmt[m++] = '\n' ;
+ if (buffer_timed_put_g(b, fmt, m, deadline) < m
+ || buffer_timed_put_g(b, file, len, deadline) < len
+ || buffer_timed_put_g(b, "\n", 1, deadline) < 1)
+ strerr_diefu1sys(111, "write to server") ;
+ m = uint64_fmt(fmt, st.st_size) ;
+ fmt[m++] = '\n' ;
+ if (buffer_timed_put_g(b, fmt, m, deadline) < m)
+ strerr_diefu1sys(111, "write to server") ;
+ }
+ send_file_g(b, fd, st.st_size, file, deadline) ;
+ fd_close(fd) ;
+ if (buffer_timed_put_g(b, "\n", 1, deadline) < 1)
+ strerr_diefu1sys(111, "write to server") ;
+}
+
+int main (int argc, char const *const *argv)
+{
+ tain rtto = TAIN_INFINITE_RELATIVE, wtto = TAIN_INFINITE_RELATIVE ;
+ tain deadline ;
+ PROG = "apastec" ;
+ {
+ uint32_t rt = 0, wt = 0 ;
+ subgetopt l = SUBGETOPT_ZERO ;
+ for (;;)
+ {
+ int opt = subgetopt_r(argc, argv, "r:w:", &l) ;
+ if (opt == -1) break ;
+ switch (opt)
+ {
+ case 'r' : if (!uint320_scan(l.arg, &rt)) dieusage() ; break ;
+ case 'w' : if (!uint320_scan(l.arg, &wt)) dieusage() ; break ;
+ default : dieusage() ;
+ }
+ }
+ argc -= l.ind ; argv += l.ind ;
+ if (rt) tain_from_millisecs(&rtto, rt) ;
+ if (wt) tain_from_millisecs(&rtto, wt) ;
+ }
+ if (!argc) dieusage() ;
+ if (argc > UINT32_MAX) strerr_dief1x(100, "too many arguments") ;
+
+ if (!sig_altignore(SIGPIPE))
+ strerr_diefu1sys(111, "ignore SIGPIPE") ;
+ tain_now_set_stopwatch_g() ;
+ tain_add_g(&deadline, &wtto) ;
+
+ {
+ char buf[BUFFER_OUTSIZE] ;
+ buffer b = BUFFER_INIT(&buffer_write, 7, buf, BUFFER_OUTSIZE) ;
+ buffer_putnoflush(&b, APASTEC_BANNER "\n", sizeof(APASTEC_BANNER)) ;
+ {
+ char fmt[UINT32_FMT] ;
+ size_t m = uint32_fmt(fmt, argc) ;
+ fmt[m++] = '\n' ;
+ buffer_putnoflush(&b, fmt, m) ;
+ }
+ {
+ int gotstdin = 0 ;
+ for (unsigned int i = 0 ; i < argc ; i++) if (argv[i][0] == '-' && !argv[i][1])
+ {
+ if (gotstdin) strerr_dief1x(100, "stdin specified more than once") ;
+ gotstdin = 1 ;
+ }
+ }
+ for (unsigned int i = 0 ; i < argc ; i++) apaste_send(&b, argv[i], &deadline) ;
+ if (buffer_timed_put_g(&b, APASTEC_BANNER "\n", sizeof(APASTEC_BANNER), &deadline) < sizeof(APASTEC_BANNER)
+ || !buffer_timed_flush_g(&b, &deadline))
+ strerr_diefu1sys(111, "write to apaste server") ;
+ }
+
+ fd_shutdown(7, 1) ;
+ fd_close(7) ;
+ tain_add_g(&deadline, &rtto) ;
+
+ {
+ char buf[BUFFER_INSIZE] ;
+ char banner[256] ;
+ char slug[256] ;
+ buffer b = BUFFER_INIT(&buffer_read, 6, buf, BUFFER_INSIZE) ;
+ size_t w = 0, sluglen = 0 ;
+ if (sanitize_read(timed_getlnmax_g(&b, banner, 256, &w, '\n', &deadline)) <= 0)
+ strerr_diefu1sys(111, "read from apaste server") ;
+ if (w != sizeof(APASTED_BANNER) || memcmp(banner, APASTED_BANNER, w-1))
+ strerr_dief1x(1, "server returned invalid data") ;
+ if (sanitize_read(timed_getlnmax_g(&b, slug, 256, &sluglen, '\n', &deadline)) <= 0)
+ strerr_diefu1sys(111, "read from apaste server") ;
+ w = 0 ;
+ if (sanitize_read(timed_getlnmax_g(&b, banner, 256, &w, '\n', &deadline)) <= 0)
+ strerr_diefu1sys(111, "read from apaste server") ;
+ if (w != sizeof(APASTED_BANNER) || memcmp(banner, APASTED_BANNER, w-1))
+ strerr_dief1x(1, "server returned invalid data") ;
+ if (buffer_putflush(buffer_1small, slug, sluglen) == -1)
+ strerr_diefu1sys(111, "write to stdout") ;
+ }
+
+ return 0 ;
+}
diff --git a/src/client/apastec.h b/src/client/apastec.h
new file mode 100644
index 0000000..84ab242
--- /dev/null
+++ b/src/client/apastec.h
@@ -0,0 +1,17 @@
+/* ISC license. */
+
+#ifndef APASTEC_H
+#define APASTEC_H
+
+#include <sys/types.h>
+
+#include <skalibs/buffer.h>
+#include <skalibs/tai.h>
+
+
+/* send_file.c */
+
+extern void send_file (buffer *, int, off_t, char const *, tain const *, tain *) ;
+#define send_file_g(b, fd, size, file, deadline) send_file(b, fd, size, file, (deadline), &STAMP)
+
+#endif
diff --git a/src/client/deps-exe/apaste b/src/client/deps-exe/apaste
new file mode 100644
index 0000000..e7187fe
--- /dev/null
+++ b/src/client/deps-exe/apaste
@@ -0,0 +1 @@
+-lskarnet
diff --git a/src/client/deps-exe/apastec b/src/client/deps-exe/apastec
new file mode 100644
index 0000000..1e0ab64
--- /dev/null
+++ b/src/client/deps-exe/apastec
@@ -0,0 +1,4 @@
+send_file.o
+-lskarnet
+${SOCKET_LIB}
+${SYSCLOCK_LIB}
diff --git a/src/client/send_file.c b/src/client/send_file.c
new file mode 100644
index 0000000..4da8412
--- /dev/null
+++ b/src/client/send_file.c
@@ -0,0 +1,93 @@
+/* ISC license. */
+
+#include <skalibs/sysdeps.h>
+
+#ifdef SKALIBS_HASSENDFILE
+
+#include <sys/types.h>
+#include <sys/sendfile.h>
+#include <limits.h>
+
+#include <skalibs/uint64.h>
+#include <skalibs/strerr.h>
+#include <skalibs/tai.h>
+#include <skalibs/unix-timed.h>
+
+struct sendfile_s
+{
+ int fd ;
+ buffer *out ;
+ off_t pos ;
+ uint64_t n ;
+} ;
+
+static int s_getfd (void *p)
+{
+ struct sendfile_s *sf = p ;
+ return buffer_fd(sf->out) ;
+}
+
+static int s_isnonempty (void *p)
+{
+ struct sendfile_s *sf = p ;
+ return !!sf->n ;
+}
+
+static int s_flush (void *p)
+{
+ struct sendfile_s *sf = p ;
+ while (sf->n)
+ {
+ ssize_t r = sendfile(buffer_fd(sf->out), sf->fd, &sf->pos, sf->n > SSIZE_MAX ? SSIZE_MAX : sf->n) ;
+ if (r == -1) return 0 ;
+ sf->n -= r ;
+ }
+ return 1 ;
+}
+
+void send_file (buffer *b, int fd, off_t size, char const *fn, tain const *deadline, tain *stamp)
+{
+ struct sendfile_s sf = { .fd = fd, .out = b, .pos = 0, .n = size } ;
+ if (!buffer_timed_flush(b, deadline, stamp))
+ strerr_diefu1sys(111, "write to network") ;
+ if (!timed_flush(&sf, &s_getfd, &s_isnonempty, &s_flush, deadline, stamp))
+ strerr_diefu3sys(111, "sendfile ", fn, " to network") ;
+}
+
+#else
+
+#include <sys/uio.h>
+#include <unistd.h>
+#include <stdint.h>
+
+#include <skalibs/uint64.h>
+#include <skalibs/allreadwrite.h>
+#include <skalibs/buffer.h>
+#include <skalibs/strerr.h>
+#include <skalibs/tai.h>
+#include <skalibs/unix-timed.h>
+
+void send_file (buffer *b, int fd, uint64_t n, char const *fn, tain const *deadline, tain *stamp)
+{
+ struct iovec v[2] ;
+ ssize_t r ;
+ if (!n) goto flushit ;
+ fillit:
+ buffer_wpeek(b, v) ;
+ r = allreadv(fd, v, 2) ;
+ if (r == -1) strerr_diefu2sys(111, "read from ", fn) ;
+ if (!r) strerr_diefu3x(111, "send ", fn, ": file was truncated") ;
+ if (r > n)
+ {
+ r = n ;
+ strerr_warnw2x("sending elongated file: ", fn) ;
+ }
+ buffer_wseek(b, r) ;
+ n -= r ;
+ flushit:
+ if (!buffer_timed_flush(b, deadline, stamp))
+ strerr_diefu1sys(111, "write to network") ;
+ if (n) goto fillit ;
+}
+
+#endif