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 | ||
68 | /* --- @pc_puterr@, @pc_putuint@, @pc_putsz@, @pc_puttops@ --- * | |
69 | * | |
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 | |
74 | * | |
75 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
76 | * | |
77 | * Use: Sends an error/integer/size/tunnel-ops pointer. | |
78 | */ | |
79 | ||
80 | #define PUT(abbr, type) \ | |
81 | int pc_put##abbr(type x) { return (pc_put(&x, sizeof(x))); } | |
82 | COMM_TYPES(PUT) | |
83 | ||
84 | /* --- @pc_putstring@ --- * | |
85 | * | |
86 | * Arguments: @const char *s@ = pointer to string to write | |
87 | * | |
88 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
89 | * | |
90 | * Use: Sends a string/error/integer/tunnel-ops pointer. | |
91 | */ | |
92 | ||
93 | int pc_putstring(const char *s) | |
94 | { | |
95 | size_t sz = strlen(s); | |
96 | ||
97 | if (pc_putsz(sz) || pc_put(s, sz)) | |
98 | return (-1); | |
99 | return (0); | |
100 | } | |
101 | ||
102 | /* --- @pc_get@ --- * | |
103 | * | |
104 | * Arguments: @void *p@ = pointer to buffer | |
105 | * @size_t sz@ = size of the buffer | |
106 | * | |
107 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
108 | * | |
109 | * Use: Receives a buffer, handling short reads and other bogosity. | |
110 | */ | |
111 | ||
112 | int pc_get(void *p, size_t sz) | |
113 | { | |
114 | ssize_t n; | |
115 | unsigned char *pp = p; | |
116 | ||
117 | while (sz) { | |
118 | n = read(pc_fd, pp, sz); | |
119 | if (n < 0) { | |
120 | if (errno == EINTR) | |
121 | continue; | |
122 | else if (errno == ECONNRESET) | |
123 | errno = -1; | |
124 | return (-1); | |
125 | } | |
126 | if (n == 0) { | |
127 | errno = -1; | |
128 | return (-1); | |
129 | } | |
130 | pp += n; sz -= n; | |
131 | } | |
132 | return (0); | |
133 | } | |
134 | ||
135 | /* --- @pc_geterr@, @pc_getuint@, @pc_getsz@, @pc_getops@ --- * | |
136 | * | |
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 | |
141 | * | |
142 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
143 | * | |
144 | * Use: Receives an error/integer/size/tunnel-ops pointer. | |
145 | */ | |
146 | ||
147 | #define GET(abbr, type) \ | |
148 | int pc_get##abbr(type *x) { return (pc_get(x, sizeof(*x))); } | |
149 | COMM_TYPES(GET) | |
150 | ||
151 | /* --- @pc_gettring@ --- * | |
152 | * | |
153 | * Arguments: @dstr *d@ = where to pc_put the string | |
154 | * | |
155 | * Returns: Zero on success, @-1@ on error (and @errno@ set). | |
156 | * | |
157 | * Use: Receives a string. | |
158 | */ | |
159 | ||
160 | int pc_getstring(dstr *d) | |
161 | { | |
162 | size_t sz; | |
163 | ||
164 | if (pc_getsz(&sz)) | |
165 | return (-1); | |
166 | DENSURE(d, sz + 1); | |
167 | if (pc_get(d->buf + d->len, sz)) | |
168 | return (-1); | |
169 | d->len += sz; | |
170 | d->buf[d->len] = 0; | |
171 | return (0); | |
172 | } | |
173 | ||
174 | /*----- That's all, folks -------------------------------------------------*/ |