Commit | Line | Data |
---|---|---|
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 | * | |
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. | |
16 | * | |
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. | |
21 | * | |
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. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "priv.h" | |
30 | ||
31 | /*----- Global variables --------------------------------------------------*/ | |
32 | ||
33 | int pc_fd = 0; /* File descriptor for comms */ | |
34 | ||
35 | /*----- Main code ---------------------------------------------------------*/ | |
36 | ||
37 | /* --- @pc_put@ --- * | |
38 | * | |
39 | * Arguments: @const void *p@ = pointer to buffer | |
40 | * @size_t sz@ = size of the buffer | |
41 | * | |
42 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
43 | * | |
44 | * Use: Writes a buffer, handling short writes and other bogosity. | |
45 | */ | |
46 | ||
47 | int pc_put(const void *p, size_t sz) | |
48 | { | |
49 | ssize_t n; | |
50 | const unsigned char *pp = p; | |
51 | ||
52 | while (sz) { | |
53 | n = write(pc_fd, pp, sz); | |
54 | if (n < 0) { | |
55 | if (errno == EINTR) | |
56 | continue; | |
57 | return (-1); | |
58 | } | |
59 | if (n == 0) { | |
60 | errno = EIO; | |
61 | return (-1); | |
62 | } | |
63 | pp += n; sz -= n; | |
64 | } | |
65 | return (0); | |
66 | } | |
67 | ||
c9a6b55c | 68 | /* --- @pc_puterr@, @pc_putuint@, @pc_putsz@ --- * |
388e0319 MW |
69 | * |
70 | * Arguments: @int err@ = error number to write | |
71 | * @uint u@ = unsigned integer to write | |
72 | * @size_t sz@ = size to write | |
388e0319 MW |
73 | * |
74 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
75 | * | |
c9a6b55c | 76 | * Use: Sends an error/integer/size. |
388e0319 MW |
77 | */ |
78 | ||
79 | #define PUT(abbr, type) \ | |
80 | int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); } | |
81 | COMM_TYPES(PUT) | |
82 | ||
83 | /* --- @pc_putstring@ --- * | |
84 | * | |
85 | * Arguments: @const char *s@ = pointer to string to write | |
86 | * | |
87 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
88 | * | |
c9a6b55c | 89 | * Use: Sends a string. |
388e0319 MW |
90 | */ |
91 | ||
92 | int pc_putstring(const char *s) | |
93 | { | |
94 | size_t sz = strlen(s); | |
95 | ||
96 | if (pc_putsz(sz) || pc_put(s, sz)) | |
97 | return (-1); | |
98 | return (0); | |
99 | } | |
100 | ||
101 | /* --- @pc_get@ --- * | |
102 | * | |
103 | * Arguments: @void *p@ = pointer to buffer | |
104 | * @size_t sz@ = size of the buffer | |
105 | * | |
106 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
107 | * | |
108 | * Use: Receives a buffer, handling short reads and other bogosity. | |
109 | */ | |
110 | ||
111 | int pc_get(void *p, size_t sz) | |
112 | { | |
113 | ssize_t n; | |
114 | unsigned char *pp = p; | |
115 | ||
116 | while (sz) { | |
117 | n = read(pc_fd, pp, sz); | |
118 | if (n < 0) { | |
119 | if (errno == EINTR) | |
120 | continue; | |
121 | else if (errno == ECONNRESET) | |
122 | errno = -1; | |
123 | return (-1); | |
124 | } | |
125 | if (n == 0) { | |
126 | errno = -1; | |
127 | return (-1); | |
128 | } | |
129 | pp += n; sz -= n; | |
130 | } | |
131 | return (0); | |
132 | } | |
133 | ||
c9a6b55c | 134 | /* --- @pc_geterr@, @pc_getuint@, @pc_getsz@ --- * |
388e0319 MW |
135 | * |
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 | |
388e0319 MW |
139 | * |
140 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
141 | * | |
c9a6b55c | 142 | * Use: Receives an error/integer/size. |
388e0319 MW |
143 | */ |
144 | ||
145 | #define GET(abbr, type) \ | |
146 | int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); } | |
147 | COMM_TYPES(GET) | |
148 | ||
c9a6b55c | 149 | /* --- @pc_getstring@ --- * |
388e0319 MW |
150 | * |
151 | * Arguments: @dstr *d@ = where to pc_put the string | |
152 | * | |
153 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
154 | * | |
155 | * Use: Receives a string. | |
156 | */ | |
157 | ||
158 | int pc_getstring(dstr *d) | |
159 | { | |
160 | size_t sz; | |
161 | ||
162 | if (pc_getsz(&sz)) | |
163 | return (-1); | |
164 | DENSURE(d, sz + 1); | |
165 | if (pc_get(d->buf + d->len, sz)) | |
166 | return (-1); | |
167 | d->len += sz; | |
168 | d->buf[d->len] = 0; | |
169 | return (0); | |
170 | } | |
171 | ||
172 | /*----- That's all, folks -------------------------------------------------*/ |