--- /dev/null
+/* -*-c-*-
+ *
+ * Communication between server and helper
+ *
+ * (c) 2008 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Trivial IP Encryption (TrIPE).
+ *
+ * TrIPE is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * TrIPE is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with TrIPE; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "priv.h"
+
+/*----- Global variables --------------------------------------------------*/
+
+int pc_fd = 0; /* File descriptor for comms */
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @pc_put@ --- *
+ *
+ * Arguments: @const void *p@ = pointer to buffer
+ * @size_t sz@ = size of the buffer
+ *
+ * Returns: Zero on success, @-1@ on error (and @errno@ set).
+ *
+ * Use: Writes a buffer, handling short writes and other bogosity.
+ */
+
+int pc_put(const void *p, size_t sz)
+{
+ ssize_t n;
+ const unsigned char *pp = p;
+
+ while (sz) {
+ n = write(pc_fd, pp, sz);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ return (-1);
+ }
+ if (n == 0) {
+ errno = EIO;
+ return (-1);
+ }
+ pp += n; sz -= n;
+ }
+ return (0);
+}
+
+/* --- @pc_puterr@, @pc_putuint@, @pc_putsz@, @pc_puttops@ --- *
+ *
+ * Arguments: @int err@ = error number to write
+ * @uint u@ = unsigned integer to write
+ * @size_t sz@ = size to write
+ * @const tunnel_ops *tops@ = tunnel pointer to write
+ *
+ * Returns: Zero on success, @-1@ on error (and @errno@ set).
+ *
+ * Use: Sends an error/integer/size/tunnel-ops pointer.
+ */
+
+#define PUT(abbr, type) \
+ int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); }
+COMM_TYPES(PUT)
+
+/* --- @pc_putstring@ --- *
+ *
+ * Arguments: @const char *s@ = pointer to string to write
+ *
+ * Returns: Zero on success, @-1@ on error (and @errno@ set).
+ *
+ * Use: Sends a string/error/integer/tunnel-ops pointer.
+ */
+
+int pc_putstring(const char *s)
+{
+ size_t sz = strlen(s);
+
+ if (pc_putsz(sz) || pc_put(s, sz))
+ return (-1);
+ return (0);
+}
+
+/* --- @pc_get@ --- *
+ *
+ * Arguments: @void *p@ = pointer to buffer
+ * @size_t sz@ = size of the buffer
+ *
+ * Returns: Zero on success, @-1@ on error (and @errno@ set).
+ *
+ * Use: Receives a buffer, handling short reads and other bogosity.
+ */
+
+int pc_get(void *p, size_t sz)
+{
+ ssize_t n;
+ unsigned char *pp = p;
+
+ while (sz) {
+ n = read(pc_fd, pp, sz);
+ if (n < 0) {
+ if (errno == EINTR)
+ continue;
+ else if (errno == ECONNRESET)
+ errno = -1;
+ return (-1);
+ }
+ if (n == 0) {
+ errno = -1;
+ return (-1);
+ }
+ pp += n; sz -= n;
+ }
+ return (0);
+}
+
+/* --- @pc_geterr@, @pc_getuint@, @pc_getsz@, @pc_getops@ --- *
+ *
+ * Arguments: @int *err@ = where to put the error number
+ * @uint *u@ = where to put the unsigned integer
+ * @size_t *sz@ = where to put the size
+ * @const tunnel_ops **tops@ = where to put the tunnel pointer
+ *
+ * Returns: Zero on success, @-1@ on error (and @errno@ set).
+ *
+ * Use: Receives an error/integer/size/tunnel-ops pointer.
+ */
+
+#define GET(abbr, type) \
+ int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); }
+COMM_TYPES(GET)
+
+/* --- @pc_gettring@ --- *
+ *
+ * Arguments: @dstr *d@ = where to pc_put the string
+ *
+ * Returns: Zero on success, @-1@ on error (and @errno@ set).
+ *
+ * Use: Receives a string.
+ */
+
+int pc_getstring(dstr *d)
+{
+ size_t sz;
+
+ if (pc_getsz(&sz))
+ return (-1);
+ DENSURE(d, sz + 1);
+ if (pc_get(d->buf + d->len, sz))
+ return (-1);
+ d->len += sz;
+ d->buf[d->len] = 0;
+ return (0);
+}
+
+/*----- That's all, folks -------------------------------------------------*/