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@ --- *
70 * Arguments: @int err@ = error number to write
71 * @uint u@ = unsigned integer to write
72 * @size_t sz@ = size to write
74 * Returns: Zero on success, @-1@ on error (and @errno@ set).
76 * Use: Sends an error/integer/size.
79 #define PUT(abbr, type) \
80 int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); }
83 /* --- @pc_putstring@ --- *
85 * Arguments: @const char *s@ = pointer to string to write
87 * Returns: Zero on success, @-1@ on error (and @errno@ set).
89 * Use: Sends a string.
92 int pc_putstring(const char *s
)
94 size_t sz
= strlen(s
);
96 if (pc_putsz(sz
) || pc_put(s
, sz
))
101 /* --- @pc_get@ --- *
103 * Arguments: @void *p@ = pointer to buffer
104 * @size_t sz@ = size of the buffer
106 * Returns: Zero on success, @-1@ on error (and @errno@ set).
108 * Use: Receives a buffer, handling short reads and other bogosity.
111 int pc_get(void *p
, size_t sz
)
114 unsigned char *pp
= p
;
117 n
= read(pc_fd
, pp
, sz
);
121 else if (errno
== ECONNRESET
)
134 /* --- @pc_geterr@, @pc_getuint@, @pc_getsz@ --- *
136 * Arguments: @int *err@ = where to put the error number
137 * @uint *u@ = where to put the unsigned integer
138 * @size_t *sz@ = where to put the size
140 * Returns: Zero on success, @-1@ on error (and @errno@ set).
142 * Use: Receives an error/integer/size.
145 #define GET(abbr, type) \
146 int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); }
149 /* --- @pc_getstring@ --- *
151 * Arguments: @dstr *d@ = where to pc_put the string
153 * Returns: Zero on success, @-1@ on error (and @errno@ set).
155 * Use: Receives a string.
158 int pc_getstring(dstr
*d
)
165 if (pc_get(d
->buf
+ d
->len
, sz
))
172 /*----- That's all, folks -------------------------------------------------*/