3 * Communication between server and helper
5 * (c) 2008 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Global variables --------------------------------------------------*/
33 int pc_fd = 0; /* File descriptor for comms */
35 /*----- Main code ---------------------------------------------------------*/
39 * Arguments: @const void *p@ = pointer to buffer
40 * @size_t sz@ = size of the buffer
42 * Returns: Zero on success, @-1@ on error (and @errno@ set).
44 * Use: Writes a buffer, handling short writes and other bogosity.
47 int pc_put(const void *p, size_t sz)
50 const unsigned char *pp = p;
53 n = write(pc_fd, pp, sz);
68 /* --- @pc_puterr@, @pc_putuint@, @pc_putsz@, @pc_puttops@ --- *
70 * Arguments: @int err@ = error number to write
71 * @uint u@ = unsigned integer to write
72 * @size_t sz@ = size to write
73 * @const tunnel_ops *tops@ = tunnel pointer to write
75 * Returns: Zero on success, @-1@ on error (and @errno@ set).
77 * Use: Sends an error/integer/size/tunnel-ops pointer.
80 #define PUT(abbr, type) \
81 int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); }
84 /* --- @pc_putstring@ --- *
86 * Arguments: @const char *s@ = pointer to string to write
88 * Returns: Zero on success, @-1@ on error (and @errno@ set).
90 * Use: Sends a string/error/integer/tunnel-ops pointer.
93 int pc_putstring(const char *s)
95 size_t sz = strlen(s);
97 if (pc_putsz(sz) || pc_put(s, sz))
102 /* --- @pc_get@ --- *
104 * Arguments: @void *p@ = pointer to buffer
105 * @size_t sz@ = size of the buffer
107 * Returns: Zero on success, @-1@ on error (and @errno@ set).
109 * Use: Receives a buffer, handling short reads and other bogosity.
112 int pc_get(void *p, size_t sz)
115 unsigned char *pp = p;
118 n = read(pc_fd, pp, sz);
122 else if (errno == ECONNRESET)
135 /* --- @pc_geterr@, @pc_getuint@, @pc_getsz@, @pc_getops@ --- *
137 * Arguments: @int *err@ = where to put the error number
138 * @uint *u@ = where to put the unsigned integer
139 * @size_t *sz@ = where to put the size
140 * @const tunnel_ops **tops@ = where to put the tunnel pointer
142 * Returns: Zero on success, @-1@ on error (and @errno@ set).
144 * Use: Receives an error/integer/size/tunnel-ops pointer.
147 #define GET(abbr, type) \
148 int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); }
151 /* --- @pc_gettring@ --- *
153 * Arguments: @dstr *d@ = where to pc_put the string
155 * Returns: Zero on success, @-1@ on error (and @errno@ set).
157 * Use: Receives a string.
160 int pc_getstring(dstr *d)
167 if (pc_get(d->buf + d->len, sz))
174 /*----- That's all, folks -------------------------------------------------*/