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 it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global variables --------------------------------------------------*/
32 int pc_fd
= 0; /* File descriptor for comms */
34 /*----- Main code ---------------------------------------------------------*/
38 * Arguments: @const void *p@ = pointer to buffer
39 * @size_t sz@ = size of the buffer
41 * Returns: Zero on success, @-1@ on error (and @errno@ set).
43 * Use: Writes a buffer, handling short writes and other bogosity.
46 int pc_put(const void *p
, size_t sz
)
49 const unsigned char *pp
= p
;
52 n
= write(pc_fd
, pp
, sz
);
67 /* --- @pc_puterr@, @pc_putuint@, @pc_putsz@ --- *
69 * Arguments: @int err@ = error number to write
70 * @uint u@ = unsigned integer to write
71 * @size_t sz@ = size to write
73 * Returns: Zero on success, @-1@ on error (and @errno@ set).
75 * Use: Sends an error/integer/size.
78 #define PUT(abbr, type) \
79 int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); }
82 /* --- @pc_putstring@ --- *
84 * Arguments: @const char *s@ = pointer to string to write
86 * Returns: Zero on success, @-1@ on error (and @errno@ set).
88 * Use: Sends a string.
91 int pc_putstring(const char *s
)
93 size_t sz
= strlen(s
);
95 if (pc_putsz(sz
) || pc_put(s
, sz
))
100 /* --- @pc_get@ --- *
102 * Arguments: @void *p@ = pointer to buffer
103 * @size_t sz@ = size of the buffer
105 * Returns: Zero on success, @-1@ on error (and @errno@ set).
107 * Use: Receives a buffer, handling short reads and other bogosity.
110 int pc_get(void *p
, size_t sz
)
113 unsigned char *pp
= p
;
116 n
= read(pc_fd
, pp
, sz
);
120 else if (errno
== ECONNRESET
)
133 /* --- @pc_geterr@, @pc_getuint@, @pc_getsz@ --- *
135 * Arguments: @int *err@ = where to put the error number
136 * @uint *u@ = where to put the unsigned integer
137 * @size_t *sz@ = where to put the size
139 * Returns: Zero on success, @-1@ on error (and @errno@ set).
141 * Use: Receives an error/integer/size.
144 #define GET(abbr, type) \
145 int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); }
148 /* --- @pc_getstring@ --- *
150 * Arguments: @dstr *d@ = where to pc_put the string
152 * Returns: Zero on success, @-1@ on error (and @errno@ set).
154 * Use: Receives a string.
157 int pc_getstring(dstr
*d
)
164 if (pc_get(d
->buf
+ d
->len
, sz
))
171 /*----- That's all, folks -------------------------------------------------*/