chiark / gitweb /
Make file descriptors be nonblocking and close-on-exec.
[tripe] / peer.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
650a6624 3 * $Id: peer.c,v 1.3 2001/02/04 17:10:58 mdw Exp $
410c8acf 4 *
5 * Communication with the peer
6 *
7 * (c) 2001 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Trivial IP Encryption (TrIPE).
13 *
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: peer.c,v $
650a6624 32 * Revision 1.3 2001/02/04 17:10:58 mdw
33 * Make file descriptors be nonblocking and close-on-exec.
34 *
8d0c7a83 35 * Revision 1.2 2001/02/03 22:40:29 mdw
36 * Put timer information into the entropy pool when packets are received
37 * and on similar events. Reseed the generator on the interval timer.
38 *
410c8acf 39 * Revision 1.1 2001/02/03 20:26:37 mdw
40 * Initial checkin.
41 *
42 */
43
44/*----- Header files ------------------------------------------------------*/
45
46#include "tripe.h"
47
48/*----- Static variables --------------------------------------------------*/
49
50static peer *peers = 0;
51static sel_file sock;
52
53/*----- Main code ---------------------------------------------------------*/
54
55/* --- @p_read@ --- *
56 *
57 * Arguments: @int fd@ = file descriptor to read from
58 * @unsigned mode@ = what happened
59 * @void *v@ = an uninteresting pointer
60 *
61 * Returns: ---
62 *
63 * Use: Reads a packet from somewhere.
64 */
65
66static void p_read(int fd, unsigned mode, void *v)
67{
68 peer *p;
69 addr a;
70 size_t sz;
71 ssize_t n;
72 int ch;
73 buf b, bb;
74
75 /* --- Read the data --- */
76
8d0c7a83 77 TIMER;
410c8acf 78 sz = sizeof(addr);
79 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
80 if (n < 0) {
81 a_warn("error reading socket: %s", strerror(errno));
82 return;
83 }
84
85 /* --- Find the appropriate peer --- */
86
87 assert(a.sa.sa_family == AF_INET);
410c8acf 88 for (p = peers; p; p = p->next) {
410c8acf 89 if (p->peer.sin.sin_addr.s_addr == a.sin.sin_addr.s_addr &&
90 p->peer.sin.sin_port == a.sin.sin_port)
91 goto found;
92 }
93 a_warn("packet from unexpected peer: %s:%u",
94 inet_ntoa(a.sin.sin_addr), (unsigned)ntohs(a.sin.sin_port));
95 return;
96
97found:
98 T( trace(T_PEER, "peer: packet received from `%s'", p->name);
99 trace_block(T_PACKET, "peer: packet contents", buf_i, n); )
100
101 /* --- Pick the packet apart --- */
102
103 buf_init(&b, buf_i, n);
104 if ((ch = buf_getbyte(&b)) < 0) {
105 a_warn("bad packet from `%s': no type byte", p->name);
106 return;
107 }
108 switch (ch) {
109 case MSG_PACKET:
110 buf_init(&bb, buf_o, sizeof(buf_o));
111 if (ks_decrypt(&p->ks, &b, &bb)) {
112 a_warn("couldn't decrypt inbound packet");
113 return;
114 }
115 if (BOK(&bb))
116 tun_inject(&p->t, &bb);
117 else
118 a_warn("packet build failed");
119 break;
120 case MSG_PRECHALLENGE:
121 kx_prechallenge(&p->kx, &b);
122 break;
123 case MSG_CHALLENGE:
124 kx_challenge(&p->kx, &b);
125 break;
126 case MSG_RESPONSE:
127 kx_response(&p->kx, &b);
128 break;
129 default:
130 a_warn("bad packet from `%s': unknown packet type", p->name);
131 break;
132 }
133}
134
135/* --- @p_txstart@ --- *
136 *
137 * Arguments: @peer *p@ = pointer to peer block
138 * @unsigned msg@ = message type code
139 *
140 * Returns: A pointer to a buffer to write to.
141 *
142 * Use: Starts sending to a peer. Only one send can happen at a
143 * time.
144 */
145
146buf *p_txstart(peer *p, unsigned msg)
147{
148 buf_init(&p->b, buf_o, sizeof(buf_o));
149 buf_putbyte(&p->b, msg);
150 return (&p->b);
151}
152
153/* --- @p_txend@ --- *
154 *
155 * Arguments: @peer *p@ = pointer to peer block
156 *
157 * Returns: ---
158 *
159 * Use: Sends a packet to the peer.
160 */
161
162void p_txend(peer *p)
163{
164 if (!BOK(&p->b)) {
165 a_warn("packet build failed");
166 return;
167 }
168 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
169 BBASE(&p->b), BLEN(&p->b)); )
170 if (sendto(sock.fd, BBASE(&p->b), BLEN(&p->b),
171 0, &p->peer.sa, p->sasz) < 0)
172 a_warn("packet send to `%s' failed: %s", p->name, strerror(errno));
173}
174
175/* --- @p_tun@ --- *
176 *
177 * Arguments: @peer *p@ = pointer to peer block
178 * @buf *b@ = buffer containing incoming packet
179 *
180 * Returns: ---
181 *
182 * Use: Handles a packet which needs to be sent to a peer.
183 */
184
185void p_tun(peer *p, buf *b)
186{
187 buf *bb = p_txstart(p, MSG_PACKET);
8d0c7a83 188 TIMER;
410c8acf 189 if (ks_encrypt(&p->ks, b, bb))
190 kx_start(&p->kx);
191 if (BCUR(bb) > BBASE(bb))
192 p_txend(p);
193}
194
195/* --- @p_interval@ --- *
196 *
197 * Arguments: ---
198 *
199 * Returns: ---
200 *
201 * Use: Called periodically to do tidying.
202 */
203
204void p_interval(void)
205{
206 peer *p, *pp;
207 int reload;
208
209 reload = km_interval();
210 for (p = peers; p; p = pp) {
211 pp = p->next;
212 if (reload)
213 kx_newkeys(&p->kx);
214 ks_prune(&p->ks);
215 }
216}
217
218/* --- @p_ifname@ --- *
219 *
220 * Arguments: @peer *p@ = pointer to a peer block
221 *
222 * Returns: A pointer to the peer's interface name.
223 */
224
225const char *p_ifname(peer *p) { return (tun_ifname(&p->t)); }
226
227/* --- @p_addr@ --- *
228 *
229 * Arguments: @peer *p@ = pointer to a peer block
230 *
231 * Returns: A pointer to the peer's address.
232 */
233
234const addr *p_addr(peer *p) { return (&p->peer); }
235
236/* --- @p_init@ --- *
237 *
238 * Arguments: @unsigned port@ = port number to listen to
239 *
240 * Returns: ---
241 *
242 * Use: Initializes the peer system; creates the socket.
243 */
244
245void p_init(unsigned port)
246{
247 int fd;
248 struct sockaddr_in sin;
249
250 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
251 die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
252 BURN(sin);
253 sin.sin_family = AF_INET;
254 sin.sin_addr.s_addr = INADDR_ANY;
255 sin.sin_port = htons(port);
256 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
257 die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
650a6624 258 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
410c8acf 259 sel_initfile(&sel, &sock, fd, SEL_READ, p_read, 0);
260 sel_addfile(&sock);
261 T( trace(T_PEER, "peer: created socket"); )
262}
263
264/* --- @p_port@ --- *
265 *
266 * Arguments: ---
267 *
268 * Returns: Port number used for socket.
269 */
270
271unsigned p_port(void)
272{
273 addr a;
274 size_t sz = sizeof(addr);
275
276 if (getsockname(sock.fd, &a.sa, &sz))
277 die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
278 assert(a.sa.sa_family == AF_INET);
279 return (ntohs(a.sin.sin_port));
280}
281
282/* --- @p_create@ --- *
283 *
284 * Arguments: @const char *name@ = name for this peer
285 * @struct sockaddr *sa@ = socket address of peer
286 * @size_t sz@ = size of socket address
287 *
288 * Returns: Pointer to the peer block, or null if it failed.
289 *
290 * Use: Creates a new named peer block. No peer is actually attached
291 * by this point.
292 */
293
294peer *p_create(const char *name, struct sockaddr *sa, size_t sz)
295{
296 peer *p = CREATE(peer);
297 T( trace(T_PEER, "peer: creating new peer `%s'", name); )
298 p->name = xstrdup(name);
299 p->ks = 0;
300 p->prev = 0;
301 memcpy(&p->peer.sa, sa, sz);
302 p->sasz = sz;
303 if (kx_init(&p->kx, p, &p->ks))
304 goto tidy_0;
305 if (tun_create(&p->t, p))
306 goto tidy_1;
307 p->next = peers;
308 if (peers)
309 peers->prev = p;
310 peers = p;
311 return (p);
312
313tidy_1:
314 kx_free(&p->kx);
315tidy_0:
316 xfree(p->name);
317 DESTROY(p);
318 return (0);
319}
320
321/* --- @p_name@ --- *
322 *
323 * Arguments: @peer *p@ = pointer to a peer block
324 *
325 * Returns: A pointer to the peer's name.
326 */
327
328const char *p_name(peer *p) { return (p->name); }
329
330/* --- @p_find@ --- *
331 *
332 * Arguments: @const char *name@ = name to look up
333 *
334 * Returns: Pointer to the peer block, or null if not found.
335 *
336 * Use: Finds a peer by name.
337 */
338
339peer *p_find(const char *name)
340{
341 peer *p;
342 for (p = peers; p; p = p->next) {
343 if (strcmp(name, p->name) == 0)
344 return (p);
345 }
346 return (0);
347}
348
349/* --- @p_destroy@ --- *
350 *
351 * Arguments: @peer *p@ = pointer to a peer
352 *
353 * Returns: ---
354 *
355 * Use: Destroys a peer.
356 */
357
358void p_destroy(peer *p)
359{
360 T( trace(T_PEER, "peer: destroying peer `%s'", p->name); )
361 ks_free(&p->ks);
362 kx_free(&p->kx);
363 tun_destroy(&p->t);
364 xfree(p->name);
365 if (p->next)
366 p->next->prev = p->prev;
367 if (p->prev)
368 p->prev->next = p->next;
369 else
370 peers = p->next;
371 DESTROY(p);
372}
373
374/* --- @p_first@, @p_next@ --- *
375 *
376 * Arguments: @peer *p@ = a peer block
377 *
378 * Returns: @peer_first@ returns the first peer in some ordering;
379 * @peer_next@ returns the peer following a given one in the
380 * same ordering. Null is returned for the end of the list.
381 */
382
383peer *p_first(void) { return (peers); }
384peer *p_next(peer *p) { return (p->next); }
385
386/*----- That's all, folks -------------------------------------------------*/