3 * $Id: peer.c,v 1.4 2001/02/16 21:40:24 mdw Exp $
5 * Communication with the peer
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.4 2001/02/16 21:40:24 mdw
33 * Change key exchange message interface. Maintain statistics.
35 * Revision 1.3 2001/02/04 17:10:58 mdw
36 * Make file descriptors be nonblocking and close-on-exec.
38 * Revision 1.2 2001/02/03 22:40:29 mdw
39 * Put timer information into the entropy pool when packets are received
40 * and on similar events. Reseed the generator on the interval timer.
42 * Revision 1.1 2001/02/03 20:26:37 mdw
47 /*----- Header files ------------------------------------------------------*/
51 /*----- Static variables --------------------------------------------------*/
53 static peer
*peers
= 0;
56 /*----- Main code ---------------------------------------------------------*/
60 * Arguments: @int fd@ = file descriptor to read from
61 * @unsigned mode@ = what happened
62 * @void *v@ = an uninteresting pointer
66 * Use: Reads a packet from somewhere.
69 static void p_read(int fd
, unsigned mode
, void *v
)
78 /* --- Read the data --- */
82 n
= recvfrom(fd
, buf_i
, sizeof(buf_i
), 0, &a
.sa
, &sz
);
84 a_warn("error reading socket: %s", strerror(errno
));
88 /* --- Find the appropriate peer --- */
90 assert(a
.sa
.sa_family
== AF_INET
);
91 for (p
= peers
; p
; p
= p
->next
) {
92 if (p
->peer
.sin
.sin_addr
.s_addr
== a
.sin
.sin_addr
.s_addr
&&
93 p
->peer
.sin
.sin_port
== a
.sin
.sin_port
)
96 a_warn("packet from unexpected peer: %s:%u",
97 inet_ntoa(a
.sin
.sin_addr
), (unsigned)ntohs(a
.sin
.sin_port
));
101 T( trace(T_PEER
, "peer: packet received from `%s'", p
->name
);
102 trace_block(T_PACKET
, "peer: packet contents", buf_i
, n
); )
104 /* --- Pick the packet apart --- */
106 p
->st
.t_last
= time(0);
109 buf_init(&b
, buf_i
, n
);
110 if ((ch
= buf_getbyte(&b
)) < 0) {
111 a_warn("bad packet from `%s': no type byte", p
->name
);
114 switch (ch
& MSG_CATMASK
) {
116 if (ch
& MSG_TYPEMASK
) {
117 a_warn("unknown packet type from `%s'", p
->name
);
121 buf_init(&bb
, buf_o
, sizeof(buf_o
));
122 if (ksl_decrypt(&p
->ks
, &b
, &bb
)) {
124 a_warn("couldn't decrypt inbound packet from `%s'", p
->name
);
129 p
->st
.sz_ipin
+= BSZ(&b
);
130 tun_inject(&p
->t
, &bb
);
133 a_warn("packet build failed");
137 kx_message(&p
->kx
, ch
& MSG_TYPEMASK
, &b
);
141 a_warn("bad packet from `%s': unknown packet type", p
->name
);
146 /* --- @p_txstart@ --- *
148 * Arguments: @peer *p@ = pointer to peer block
149 * @unsigned msg@ = message type code
151 * Returns: A pointer to a buffer to write to.
153 * Use: Starts sending to a peer. Only one send can happen at a
157 buf
*p_txstart(peer
*p
, unsigned msg
)
159 buf_init(&p
->b
, buf_o
, sizeof(buf_o
));
160 buf_putbyte(&p
->b
, msg
);
164 /* --- @p_txend@ --- *
166 * Arguments: @peer *p@ = pointer to peer block
170 * Use: Sends a packet to the peer.
173 void p_txend(peer
*p
)
176 a_warn("packet build failed");
179 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet",
180 BBASE(&p
->b
), BLEN(&p
->b
)); )
181 if (sendto(sock
.fd
, BBASE(&p
->b
), BLEN(&p
->b
),
182 0, &p
->peer
.sa
, p
->sasz
) < 0)
183 a_warn("packet send to `%s' failed: %s", p
->name
, strerror(errno
));
186 p
->st
.sz_out
+= BLEN(&p
->b
);
192 * Arguments: @peer *p@ = pointer to peer block
193 * @buf *b@ = buffer containing incoming packet
197 * Use: Handles a packet which needs to be sent to a peer.
200 void p_tun(peer
*p
, buf
*b
)
202 buf
*bb
= p_txstart(p
, MSG_PACKET
);
204 if (ksl_encrypt(&p
->ks
, b
, bb
))
206 if (BOK(bb
) && BLEN(bb
)) {
208 p
->st
.sz_ipout
+= BLEN(bb
);
213 /* --- @p_interval@ --- *
219 * Use: Called periodically to do tidying.
222 void p_interval(void)
227 reload
= km_interval();
228 for (p
= peers
; p
; p
= pp
) {
236 /* --- @p_stats@ --- *
238 * Arguments: @peer *p@ = pointer to a peer block
240 * Returns: A pointer to the peer's statistics.
243 stats
*p_stats(peer
*p
) { return (&p
->st
); }
245 /* --- @p_ifname@ --- *
247 * Arguments: @peer *p@ = pointer to a peer block
249 * Returns: A pointer to the peer's interface name.
252 const char *p_ifname(peer
*p
) { return (tun_ifname(&p
->t
)); }
254 /* --- @p_addr@ --- *
256 * Arguments: @peer *p@ = pointer to a peer block
258 * Returns: A pointer to the peer's address.
261 const addr
*p_addr(peer
*p
) { return (&p
->peer
); }
263 /* --- @p_init@ --- *
265 * Arguments: @unsigned port@ = port number to listen to
269 * Use: Initializes the peer system; creates the socket.
272 void p_init(unsigned port
)
275 struct sockaddr_in sin
;
277 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
278 die(EXIT_FAILURE
, "socket creation failed: %s", strerror(errno
));
280 sin
.sin_family
= AF_INET
;
281 sin
.sin_addr
.s_addr
= INADDR_ANY
;
282 sin
.sin_port
= htons(port
);
283 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
284 die(EXIT_FAILURE
, "bind failed: %s", strerror(errno
));
285 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
286 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, p_read
, 0);
288 T( trace(T_PEER
, "peer: created socket"); )
291 /* --- @p_port@ --- *
295 * Returns: Port number used for socket.
298 unsigned p_port(void)
301 size_t sz
= sizeof(addr
);
303 if (getsockname(sock
.fd
, &a
.sa
, &sz
))
304 die(EXIT_FAILURE
, "couldn't read port number: %s", strerror(errno
));
305 assert(a
.sa
.sa_family
== AF_INET
);
306 return (ntohs(a
.sin
.sin_port
));
309 /* --- @p_create@ --- *
311 * Arguments: @const char *name@ = name for this peer
312 * @struct sockaddr *sa@ = socket address of peer
313 * @size_t sz@ = size of socket address
315 * Returns: Pointer to the peer block, or null if it failed.
317 * Use: Creates a new named peer block. No peer is actually attached
321 peer
*p_create(const char *name
, struct sockaddr
*sa
, size_t sz
)
323 peer
*p
= CREATE(peer
);
324 T( trace(T_PEER
, "peer: creating new peer `%s'", name
); )
325 p
->name
= xstrdup(name
);
328 memcpy(&p
->peer
.sa
, sa
, sz
);
330 memset(&p
->st
, 0, sizeof(stats
));
331 p
->st
.t_start
= time(0);
332 if (kx_init(&p
->kx
, p
, &p
->ks
))
334 if (tun_create(&p
->t
, p
))
350 /* --- @p_name@ --- *
352 * Arguments: @peer *p@ = pointer to a peer block
354 * Returns: A pointer to the peer's name.
357 const char *p_name(peer
*p
) { return (p
->name
); }
359 /* --- @p_find@ --- *
361 * Arguments: @const char *name@ = name to look up
363 * Returns: Pointer to the peer block, or null if not found.
365 * Use: Finds a peer by name.
368 peer
*p_find(const char *name
)
371 for (p
= peers
; p
; p
= p
->next
) {
372 if (strcmp(name
, p
->name
) == 0)
378 /* --- @p_destroy@ --- *
380 * Arguments: @peer *p@ = pointer to a peer
384 * Use: Destroys a peer.
387 void p_destroy(peer
*p
)
389 T( trace(T_PEER
, "peer: destroying peer `%s'", p
->name
); )
395 p
->next
->prev
= p
->prev
;
397 p
->prev
->next
= p
->next
;
403 /* --- @p_first@, @p_next@ --- *
405 * Arguments: @peer *p@ = a peer block
407 * Returns: @peer_first@ returns the first peer in some ordering;
408 * @peer_next@ returns the peer following a given one in the
409 * same ordering. Null is returned for the end of the list.
412 peer
*p_first(void) { return (peers
); }
413 peer
*p_next(peer
*p
) { return (p
->next
); }
415 /*----- That's all, folks -------------------------------------------------*/