Commit Diff


commit - 4e3f88dd638587ca63036509707b1ad7eaf5aed8
commit + 4e83b30f471b22cb0e0f9679da7dbe3d571d9694
blob - /dev/null
blob + 8372ca49fab12a0706360717269a6025111e7a03 (mode 644)
--- /dev/null
+++ 9pclib.c
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "compat.h"
+
+#include <endian.h>
+#include <inttypes.h>
+#include <string.h>
+
+#include "9pclib.h"
+#include "kamid.h"
+#include "log.h"
+#include "utils.h"
+
+uint16_t iota_tag;
+
+struct evbuffer *evb;
+
+void
+write_hdr(uint32_t len, uint8_t type, uint16_t tag)
+{
+	len += HEADERSIZE;
+
+	log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
+	    len, type, pp_msg_type(type), tag);
+
+	len = htole32(len);
+	/* type is one byte, no endiannes issues */
+	tag = htole16(tag);
+		
+	evbuffer_add(evb, &len, sizeof(len));
+	evbuffer_add(evb, &type, sizeof(type));
+	evbuffer_add(evb, &tag, sizeof(tag));
+}
+
+void
+write_hdr_auto(uint32_t len, uint8_t type)
+{
+	if (++iota_tag == NOTAG)
+		++iota_tag;
+	write_hdr(len, type, iota_tag);
+}
+
+void
+write_str(uint16_t len, const char *str)
+{
+	uint16_t l = len;
+
+	len = htole16(len);
+	evbuffer_add(evb, &len, sizeof(len));
+	evbuffer_add(evb, str, l);
+}
+
+void
+write_str_auto(const char *str)
+{
+	write_str(strlen(str), str);
+}
+
+void
+write_32(uint32_t fid)
+{
+	fid = htole32(fid);
+	evbuffer_add(evb, &fid, sizeof(fid));
+}
+
+void
+write_16(uint16_t tag)
+{
+	tag = htole16(tag);
+	evbuffer_add(evb, &tag, sizeof(tag));
+}
+
+
+
+void
+tversion(const char *v, uint32_t msize)
+{
+	uint32_t	len;
+	uint16_t	sl;
+
+	sl = strlen(v);
+
+	/* msize[4] version[s] */
+	len = sizeof(msize) + sizeof(sl) + sl;
+	write_hdr(len, Tversion, NOTAG);
+	write_32(msize);
+	write_str(sl, v);
+}
+
+void
+tattach(uint32_t fid, uint32_t afid, const char *uname, const char *aname)
+{
+	uint32_t	len;
+	uint16_t	ul, al;
+
+	ul = strlen(uname);
+	al = strlen(aname);
+
+	/* fid[4] afid[4] uname[s] aname[s] */
+	len = sizeof(fid) + sizeof(afid) + sizeof(ul) + ul
+	    + sizeof(al) + al;
+	write_hdr_auto(len, Tattach);
+	write_fid(fid);
+	write_fid(NOFID);
+	write_str(ul, uname);
+	write_str(al, aname);
+}
+
+void
+tclunk(uint32_t fid)
+{
+	uint32_t	len;
+
+	/* fid[4] */
+	len = sizeof(fid);
+	write_hdr_auto(len, Tclunk);
+	write_fid(fid);
+}
+
+void
+tflush(uint16_t oldtag)
+{
+	uint32_t	len;
+
+	/* oldtag[2] */
+	len = sizeof(oldtag);
+	write_hdr_auto(len, Tflush);
+	write_tag(oldtag);
+}
+
+void
+twalk(uint32_t fid, uint32_t newfid, const char **wnames, size_t nwname)
+{
+	size_t		i;
+	uint32_t	len;
+
+	/* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
+	len = sizeof(fid) + sizeof(newfid) + 2;
+	for (i = 0; i < nwname; ++i)
+		len += 2 + strlen(wnames[i]);
+
+	write_hdr_auto(len, Twalk);
+	write_fid(fid);
+	write_fid(newfid);
+	write_16(nwname);
+	for (i = 0; i < nwname; ++i)
+		write_str_auto(wnames[i]);
+}
blob - 8b89fb85cf0e3c1e5003725caeaad595e04da5a2
blob + 21dfbaf841b5d20aab2247113a3082739969326b
--- Makefile.am
+++ Makefile.am
@@ -34,8 +34,10 @@ kamid_SOURCES =		client.c	\
 			utils.c		\
 			utils.h
 
-kamirepl_SOURCES =	kamid.h		\
-			kamirepl.c	\
+kamiftp_SOURCES =	9pclib.c	\
+			9pclib.h	\
+			ftp.c		\
+			kamid.h		\
 			log.c		\
 			log.h		\
 			utils.c		\
blob - /dev/null
blob + 4f193f4e89d090541f949fd6cea3bd3ff6950dd5 (mode 644)
--- /dev/null
+++ 9pclib.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* 9p client library */
+
+#ifndef NPCLIB_H
+#define NPCLIB_H
+
+#include "compat.h"
+
+#include <stdint.h>
+
+extern uint16_t		 iota_tag;
+extern struct evbuffer	*evb;
+
+void		 write_hdr(uint32_t, uint8_t, uint16_t);
+void		 write_hdr_auto(uint32_t, uint8_t);
+void		 write_str(uint16_t, const char *);
+void		 write_str_auto(const char *);
+void		 write_32(uint32_t);
+void		 write_16(uint16_t);
+
+#define write_fid write_32
+#define write_tag write_16
+
+void		 tversion(const char *, uint32_t);
+void		 tattach(uint32_t, uint32_t, const char *, const char *);
+void		 tclunk(uint32_t);
+void		 tflush(uint16_t);
+void		 twalk(uint32_t, uint32_t, const char **, size_t);
+
+#endif
blob - 15e7e7bf93516a927cc870960e3b520cbcebc1d1
blob + 84ba6da7a11daacd6b19cf6d5d7ed4c96dbbb978
--- kamirepl.c
+++ kamirepl.c
@@ -34,6 +34,7 @@
 #include <tls.h>
 #include <unistd.h>
 
+#include "9pclib.h"
 #include "kamid.h"
 #include "log.h"
 #include "utils.h"
@@ -71,12 +72,6 @@ static void		 client_error(struct bufferevent *, short
 
 static void		 repl_read(struct bufferevent *, void *);
 static void		 repl_error(struct bufferevent *, short, void *);
-static void		 write_hdr(uint32_t, uint8_t, uint16_t);
-static void		 write_hdr_auto(uint32_t, uint8_t);
-static void		 write_str(uint16_t, const char *);
-static void		 write_str_auto(const char *);
-static void		 write_fid(uint32_t);
-static void		 write_tag(uint16_t);
 
 static void		 excmd_version(const char **, int);
 static void		 excmd_attach(const char **, int);
@@ -367,97 +362,34 @@ static void
 repl_error(struct bufferevent *bev, short error, void *d)
 {
 	fatalx("an error occurred");
-}
-
-static void
-write_hdr(uint32_t len, uint8_t type, uint16_t tag)
-{
-	len += HEADERSIZE;
-
-	log_debug("enqueuing a packet; len=%"PRIu32" type=%d[%s] tag=%d",
-	    len, type, pp_msg_type(type), tag);
-
-	len = htole32(len);
-	/* type is one byte, no endiannes issues */
-	tag = htole16(tag);
-
-	bufferevent_write(bev, &len, sizeof(len));
-	bufferevent_write(bev, &type, sizeof(type));
-	bufferevent_write(bev, &tag, sizeof(tag));
 }
 
-static void
-write_hdr_auto(uint32_t len, uint8_t type)
+static inline void
+do_send(void)
 {
-        static uint16_t tag = 0;
-
-	if (++tag == NOTAG)
-		++tag;
-
-	write_hdr(len, type, tag);
+	bufferevent_write_buffer(bev, evb);
 }
 
-static void
-write_str(uint16_t len, const char *str)
-{
-	uint16_t l = len;
-
-	len = htole16(len);
-	bufferevent_write(bev, &len, sizeof(len));
-	bufferevent_write(bev, str, l);
-}
-
-static void
-write_str_auto(const char *str)
-{
-	write_str(strlen(str), str);
-}
-
-static void
-write_fid(uint32_t fid)
-{
-	fid = htole32(fid);
-	bufferevent_write(bev, &fid, sizeof(fid));
-}
-
-static void
-write_tag(uint16_t tag)
-{
-	tag = htole16(tag);
-	bufferevent_write(bev, &tag, sizeof(tag));
-}
-
 /* version [version-str] */
 static void
 excmd_version(const char **argv, int argc)
 {
-	uint32_t	 len, msize;
-	uint16_t	 sl;
 	const char	*s;
 
 	s = VERSION9P;
 	if (argc == 2)
 		s = argv[1];
 
-	sl = strlen(s);
-
-	/* msize[4] version[s] */
-	len = 4 + sizeof(sl) + sl;
-	write_hdr(len, Tversion, NOTAG);
-
-	msize = htole32(MSIZE9P);
-	bufferevent_write(bev, &msize, sizeof(msize));
-
-	write_str(sl, s);
+	tversion(s, MSIZE9P);
+	do_send();
 }
 
 /* attach fid uname aname */
 static void
 excmd_attach(const char **argv, int argc)
 {
-	uint32_t	 len, fid;
-	uint16_t	 sl, tl;
-	const char	*s, *t, *errstr;
+	uint32_t	 fid;
+	const char	*errstr;
 
 	if (argc != 4)
 		goto usage;
@@ -468,19 +400,8 @@ excmd_attach(const char **argv, int argc)
 		return;
 	}
 
-	s = argv[2];
-	sl = strlen(s);
-	t = argv[3];
-	tl = strlen(t);
-
-	/* fid[4] afid[4] uname[s] aname[s] */
-	len = 4 + 4 + sizeof(sl) + sl + sizeof(tl) + tl;
-	write_hdr_auto(len, Tattach);
-	write_fid(fid);
-	write_fid(NOFID);
-	write_str(sl, s);
-	write_str(tl, t);
-
+	tattach(fid, NOFID, argv[2], argv[3]);
+	do_send();
 	return;
 
 usage:
@@ -491,7 +412,7 @@ usage:
 static void
 excmd_clunk(const char **argv, int argc)
 {
-	uint32_t	 len, fid;
+	uint32_t	 fid;
 	const char	*errstr;
 
 	if (argc != 2)
@@ -503,10 +424,8 @@ excmd_clunk(const char **argv, int argc)
 		return;
 	}
 
-	/* fid[4] */
-	len = sizeof(fid);
-	write_hdr_auto(len, Tclunk);
-	write_fid(fid);
+	tclunk(fid);
+	do_send();
 	return;
 
 usage:
@@ -517,7 +436,6 @@ usage:
 static void
 excmd_flush(const char **argv, int argc)
 {
-	uint32_t	 len;
 	uint16_t	 oldtag;
 	const char	*errstr;
 
@@ -530,10 +448,8 @@ excmd_flush(const char **argv, int argc)
 		return;
 	}
 
-	/* oldtag[2] */
-	len = sizeof(oldtag);
-	write_hdr_auto(len, Tflush);
-	write_tag(oldtag);
+	tflush(oldtag);
+	do_send();
 	return;
 
 usage:
@@ -544,20 +460,12 @@ usage:
 static void
 excmd_walk(const char **argv, int argc)
 {
-	int		 i;
-	uint32_t	 len, fid, newfid;
+	uint32_t	 fid, newfid;
 	const char	*errstr;
 
 	if (argc < 3)
 		goto usage;
 
-	/* fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
-
-	/* two bytes for wnames count */
-	len = sizeof(fid) + sizeof(newfid) + 2;
-	for (i = 3; i < argc; ++i)
-		len += 2 + strlen(argv[i]);
-
 	fid = strtonum(argv[1], 0, UINT32_MAX, &errstr);
 	if (errstr != NULL) {
 		log_warnx("fid is %s: %s", errstr, argv[1]);
@@ -570,13 +478,8 @@ excmd_walk(const char **argv, int argc)
 		return;
 	}
 
-	write_hdr_auto(len, Twalk);
-	write_fid(fid);
-	write_fid(newfid);
-	write_tag(argc - 3);
-	for (i = 3; i < argc; ++i)
-		write_str_auto(argv[i]);
-
+	twalk(fid, newfid, argv + 3, argc - 3);
+	do_send();
 	return;
 
 usage:
@@ -897,6 +800,10 @@ main(int argc, char **argv)
 
 	event_init();
 
+	/* initialize global evb */
+	if ((evb = evbuffer_new()) == NULL)
+		fatal("evbuffer_new");
+
 	signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
 	signal_set(&ev_sigterm, SIGINT, sig_handler, NULL);