chiark / gitweb /
server/admin.c: Remove spurious `ping' in usage message.
[tripe] / priv / comm.c
CommitLineData
388e0319
MW
1/* -*-c-*-
2 *
3 * Communication between server and helper
4 *
5 * (c) 2008 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
11ad66c2
MW
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.
388e0319 16 *
11ad66c2
MW
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
20 * for more details.
388e0319
MW
21 *
22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
388e0319
MW
24 */
25
26/*----- Header files ------------------------------------------------------*/
27
28#include "priv.h"
29
30/*----- Global variables --------------------------------------------------*/
31
32int pc_fd = 0; /* File descriptor for comms */
33
34/*----- Main code ---------------------------------------------------------*/
35
36/* --- @pc_put@ --- *
37 *
38 * Arguments: @const void *p@ = pointer to buffer
39 * @size_t sz@ = size of the buffer
40 *
41 * Returns: Zero on success, @-1@ on error (and @errno@ set).
42 *
43 * Use: Writes a buffer, handling short writes and other bogosity.
44 */
45
46int pc_put(const void *p, size_t sz)
47{
48 ssize_t n;
49 const unsigned char *pp = p;
50
51 while (sz) {
52 n = write(pc_fd, pp, sz);
53 if (n < 0) {
54 if (errno == EINTR)
55 continue;
56 return (-1);
57 }
58 if (n == 0) {
59 errno = EIO;
60 return (-1);
61 }
62 pp += n; sz -= n;
63 }
64 return (0);
65}
66
c9a6b55c 67/* --- @pc_puterr@, @pc_putuint@, @pc_putsz@ --- *
388e0319
MW
68 *
69 * Arguments: @int err@ = error number to write
70 * @uint u@ = unsigned integer to write
71 * @size_t sz@ = size to write
388e0319
MW
72 *
73 * Returns: Zero on success, @-1@ on error (and @errno@ set).
74 *
c9a6b55c 75 * Use: Sends an error/integer/size.
388e0319
MW
76 */
77
78#define PUT(abbr, type) \
79 int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); }
80COMM_TYPES(PUT)
81
82/* --- @pc_putstring@ --- *
83 *
84 * Arguments: @const char *s@ = pointer to string to write
85 *
86 * Returns: Zero on success, @-1@ on error (and @errno@ set).
87 *
c9a6b55c 88 * Use: Sends a string.
388e0319
MW
89 */
90
91int pc_putstring(const char *s)
92{
93 size_t sz = strlen(s);
94
95 if (pc_putsz(sz) || pc_put(s, sz))
96 return (-1);
97 return (0);
98}
99
100/* --- @pc_get@ --- *
101 *
102 * Arguments: @void *p@ = pointer to buffer
103 * @size_t sz@ = size of the buffer
104 *
105 * Returns: Zero on success, @-1@ on error (and @errno@ set).
106 *
107 * Use: Receives a buffer, handling short reads and other bogosity.
108 */
109
110int pc_get(void *p, size_t sz)
111{
112 ssize_t n;
113 unsigned char *pp = p;
114
115 while (sz) {
116 n = read(pc_fd, pp, sz);
117 if (n < 0) {
118 if (errno == EINTR)
119 continue;
120 else if (errno == ECONNRESET)
121 errno = -1;
122 return (-1);
123 }
124 if (n == 0) {
125 errno = -1;
126 return (-1);
127 }
128 pp += n; sz -= n;
129 }
130 return (0);
131}
132
c9a6b55c 133/* --- @pc_geterr@, @pc_getuint@, @pc_getsz@ --- *
388e0319
MW
134 *
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
388e0319
MW
138 *
139 * Returns: Zero on success, @-1@ on error (and @errno@ set).
140 *
c9a6b55c 141 * Use: Receives an error/integer/size.
388e0319
MW
142 */
143
144#define GET(abbr, type) \
145 int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); }
146COMM_TYPES(GET)
147
c9a6b55c 148/* --- @pc_getstring@ --- *
388e0319
MW
149 *
150 * Arguments: @dstr *d@ = where to pc_put the string
151 *
152 * Returns: Zero on success, @-1@ on error (and @errno@ set).
153 *
154 * Use: Receives a string.
155 */
156
157int pc_getstring(dstr *d)
158{
159 size_t sz;
160
161 if (pc_getsz(&sz))
162 return (-1);
163 DENSURE(d, sz + 1);
164 if (pc_get(d->buf + d->len, sz))
165 return (-1);
166 d->len += sz;
167 d->buf[d->len] = 0;
168 return (0);
169}
170
171/*----- That's all, folks -------------------------------------------------*/