chiark / gitweb /
admin: Initialize reference counter for client blocks.
[tripe] / peer.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
3cdc3f3a 3 * $Id$
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
410c8acf 29/*----- Header files ------------------------------------------------------*/
30
31#include "tripe.h"
32
33/*----- Static variables --------------------------------------------------*/
34
35static peer *peers = 0;
36static sel_file sock;
37
42da2a58 38/*----- Tunnel table ------------------------------------------------------*/
39
40const tunnel_ops *tunnels[] = {
41#ifdef TUN_LINUX
42 &tun_linux,
43#endif
44#ifdef TUN_BSD
45 &tun_bsd,
46#endif
47#ifdef TUN_UNET
48 &tun_unet,
49#endif
50 &tun_slip,
51 0
52}, *tun_default;
53
410c8acf 54/*----- Main code ---------------------------------------------------------*/
55
0ba8de86 56/* --- @p_pingtype@ --- *
57 *
58 * Arguments: @unsigned msg@ = message type
59 *
60 * Returns: String to describe the message.
61 */
62
63static const char *p_pingtype(unsigned msg)
64{
65 switch (msg & MSG_TYPEMASK) {
66 case MISC_PING:
67 case MISC_PONG:
68 return "transport-ping";
69 case MISC_EPING:
70 case MISC_EPONG:
71 return "encrypted-ping";
72 default:
73 abort();
74 }
75}
76
77/* --- @p_ponged@ --- *
78 *
79 * Arguments: @peer *p@ = peer packet arrived from
80 * @unsigned msg@ = message type
81 * @buf *b@ = buffer containing payload
82 *
83 * Returns: ---
84 *
85 * Use: Processes a ping response.
86 */
87
88static void p_ponged(peer *p, unsigned msg, buf *b)
89{
90 uint32 id;
91 const octet *magic;
92 ping *pg;
93
94 IF_TRACING(T_PEER, {
95 trace(T_PEER, "peer: received %s reply from %s",
96 p_pingtype(msg), p->spec.name);
97 trace_block(T_PACKET, "peer: ping contents", BBASE(b), BSZ(b));
98 })
99
100 if (buf_getu32(b, &id) ||
101 (magic = buf_get(b, sizeof(pg->magic))) == 0 ||
102 BLEFT(b)) {
f43df819 103 a_warn("PEER", "?PEER", p, "malformed-%s", p_pingtype(msg), A_END);
0ba8de86 104 return;
105 }
106
107 for (pg = p->pings; pg; pg = pg->next) {
108 if (pg->id == id)
109 goto found;
110 }
f43df819
MW
111 a_warn("PEER",
112 "?PEER", p,
113 "unexpected-%s", p_pingtype(msg),
114 "0x%08lx", (unsigned long)id,
115 A_END);
0ba8de86 116 return;
117
118found:
119 if (memcmp(magic, pg->magic, sizeof(pg->magic)) != 0) {
f43df819 120 a_warn("PEER", "?PEER", p, "corrupt-%s", p_pingtype(msg), A_END);
0ba8de86 121 return;
122 }
123 p_pingdone(pg, PING_OK);
124}
125
410c8acf 126/* --- @p_read@ --- *
127 *
128 * Arguments: @int fd@ = file descriptor to read from
129 * @unsigned mode@ = what happened
130 * @void *v@ = an uninteresting pointer
131 *
132 * Returns: ---
133 *
134 * Use: Reads a packet from somewhere.
135 */
136
137static void p_read(int fd, unsigned mode, void *v)
138{
37941236 139 peer *p = 0;
410c8acf 140 addr a;
141 size_t sz;
142 ssize_t n;
143 int ch;
144 buf b, bb;
145
146 /* --- Read the data --- */
147
8d0c7a83 148 TIMER;
410c8acf 149 sz = sizeof(addr);
150 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
151 if (n < 0) {
f43df819 152 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END);
410c8acf 153 return;
154 }
155
37941236 156 /* --- If the packet is a greeting, don't check peers --- */
157
158 if (n && buf_i[0] == (MSG_MISC | MISC_GREET)) {
159 IF_TRACING(T_PEER, {
160 trace(T_PEER, "peer: greeting received from INET %s %u",
161 inet_ntoa(a.sin.sin_addr),
162 (unsigned)ntohs(a.sin.sin_port));
163 trace_block(T_PACKET, "peer: greeting contents", buf_i, n);
164 })
165 buf_init(&b, buf_i, n);
166 buf_getbyte(&b);
167 if (c_check(&b) || BLEFT(&b)) {
f43df819 168 a_warn("PEER", "-", "invalid-greeting", A_END);
37941236 169 return;
170 }
f43df819
MW
171 a_notify("GREET",
172 "?B64", buf_i + 1, (size_t)(n - 1),
173 "?ADDR", &a,
174 A_END);
37941236 175 return;
176 }
177
410c8acf 178 /* --- Find the appropriate peer --- */
179
180 assert(a.sa.sa_family == AF_INET);
410c8acf 181 for (p = peers; p; p = p->next) {
0ba8de86 182 if (p->spec.sa.sin.sin_addr.s_addr == a.sin.sin_addr.s_addr &&
183 p->spec.sa.sin.sin_port == a.sin.sin_port)
410c8acf 184 goto found;
185 }
f43df819 186 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a, A_END);
410c8acf 187 return;
188
189found:
b9066fbb 190 IF_TRACING(T_PEER, {
0ba8de86 191 trace(T_PEER, "peer: packet received from `%s'", p->spec.name);
b9066fbb 192 trace_block(T_PACKET, "peer: packet contents", buf_i, n);
193 })
410c8acf 194
195 /* --- Pick the packet apart --- */
196
5bb41301 197 p->st.t_last = time(0);
198 p->st.n_in++;
199 p->st.sz_in += n;
410c8acf 200 buf_init(&b, buf_i, n);
201 if ((ch = buf_getbyte(&b)) < 0) {
f43df819 202 a_warn("PEER", "?PEER", p, "bad-packet", "no-type", A_END);
410c8acf 203 return;
204 }
5bb41301 205 switch (ch & MSG_CATMASK) {
410c8acf 206 case MSG_PACKET:
5bb41301 207 if (ch & MSG_TYPEMASK) {
f43df819
MW
208 a_warn("PEER",
209 "?PEER", p,
210 "bad-packet",
211 "unknown-type", "0x%02x", ch,
212 A_END);
5bb41301 213 p->st.n_reject++;
214 return;
215 }
410c8acf 216 buf_init(&bb, buf_o, sizeof(buf_o));
7ed14135 217 if (ksl_decrypt(&p->ks, MSG_PACKET, &b, &bb)) {
5bb41301 218 p->st.n_reject++;
f43df819 219 a_warn("PEER", "?PEER", p, "decrypt-failed", A_END);
410c8acf 220 return;
221 }
5bb41301 222 if (BOK(&bb)) {
223 p->st.n_ipin++;
224 p->st.sz_ipin += BSZ(&b);
42da2a58 225 p->t->ops->inject(p->t, &bb);
5bb41301 226 } else {
227 p->st.n_reject++;
f43df819 228 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
5bb41301 229 }
410c8acf 230 break;
5bb41301 231 case MSG_KEYEXCH:
232 kx_message(&p->kx, ch & MSG_TYPEMASK, &b);
410c8acf 233 break;
0ba8de86 234 case MSG_MISC:
235 switch (ch & MSG_TYPEMASK) {
236 case MISC_NOP:
237 T( trace(T_PEER, "peer: received NOP packet"); )
238 break;
239 case MISC_PING:
240 buf_put(p_txstart(p, MSG_MISC | MISC_PONG), BCUR(&b), BLEFT(&b));
241 p_txend(p);
242 break;
243 case MISC_PONG:
244 p_ponged(p, MISC_PONG, &b);
245 break;
246 case MISC_EPING:
247 buf_init(&bb, buf_t, sizeof(buf_t));
248 if (ksl_decrypt(&p->ks, ch, &b, &bb)) {
249 p->st.n_reject++;
f43df819 250 a_warn("PEER", "?PEER", "decrypt-failed", A_END);
0ba8de86 251 return;
252 }
253 if (BOK(&bb)) {
254 buf_flip(&bb);
255 if (ksl_encrypt(&p->ks, MSG_MISC | MISC_EPONG, &bb,
256 p_txstart(p, MSG_MISC | MISC_EPONG)))
de014da6 257 kx_start(&p->kx, 0);
0ba8de86 258 p_txend(p);
259 }
260 break;
261 case MISC_EPONG:
262 buf_init(&bb, buf_t, sizeof(buf_t));
263 if (ksl_decrypt(&p->ks, ch, &b, &bb)) {
264 p->st.n_reject++;
f43df819 265 a_warn("PEER", "?PEER", p, "decrypt-failed", A_END);
0ba8de86 266 return;
267 }
268 if (BOK(&bb)) {
269 buf_flip(&bb);
270 p_ponged(p, MISC_EPONG, &bb);
271 }
272 break;
273 }
274 break;
410c8acf 275 default:
5bb41301 276 p->st.n_reject++;
f43df819
MW
277 a_warn("PEER",
278 "?PEER", p,
279 "bad-packet",
280 "unknown-category" "0x%02x", ch,
281 A_END);
410c8acf 282 break;
283 }
284}
285
286/* --- @p_txstart@ --- *
287 *
288 * Arguments: @peer *p@ = pointer to peer block
289 * @unsigned msg@ = message type code
290 *
291 * Returns: A pointer to a buffer to write to.
292 *
293 * Use: Starts sending to a peer. Only one send can happen at a
294 * time.
295 */
296
297buf *p_txstart(peer *p, unsigned msg)
298{
299 buf_init(&p->b, buf_o, sizeof(buf_o));
300 buf_putbyte(&p->b, msg);
301 return (&p->b);
302}
303
304/* --- @p_txend@ --- *
305 *
306 * Arguments: @peer *p@ = pointer to peer block
307 *
308 * Returns: ---
309 *
310 * Use: Sends a packet to the peer.
311 */
312
0ba8de86 313static void p_setkatimer(peer *);
314
315static int p_dotxend(peer *p)
410c8acf 316{
317 if (!BOK(&p->b)) {
f43df819 318 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
0ba8de86 319 return (0);
410c8acf 320 }
321 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
322 BBASE(&p->b), BLEN(&p->b)); )
323 if (sendto(sock.fd, BBASE(&p->b), BLEN(&p->b),
0ba8de86 324 0, &p->spec.sa.sa, p->spec.sasz) < 0) {
f43df819 325 a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
0ba8de86 326 return (0);
327 } else {
5bb41301 328 p->st.n_out++;
329 p->st.sz_out += BLEN(&p->b);
0ba8de86 330 return (1);
5bb41301 331 }
410c8acf 332}
333
0ba8de86 334void p_txend(peer *p)
335{
336 if (p_dotxend(p) && p->spec.t_ka) {
337 sel_rmtimer(&p->tka);
338 p_setkatimer(p);
339 }
340}
341
342/* --- @p_pingwrite@ --- *
343 *
344 * Arguments: @ping *p@ = ping structure
345 * @buf *b@ = buffer to write in
346 *
347 * Returns: ---
348 *
349 * Use: Fills in a ping structure and writes the packet payload.
350 */
351
352static void p_pingwrite(ping *p, buf *b)
353{
354 static uint32 seq = 0;
355
356 p->id = U32(seq++);
357 GR_FILL(&rand_global, p->magic, sizeof(p->magic));
358 buf_putu32(b, p->id);
359 buf_put(b, p->magic, sizeof(p->magic));
360}
361
362/* --- @p_pingdone@ --- *
363 *
364 * Arguments: @ping *p@ = ping structure
365 * @int rc@ = return code to pass on
366 *
367 * Returns: ---
368 *
369 * Use: Disposes of a ping structure, maybe sending a notification.
370 */
371
372void p_pingdone(ping *p, int rc)
373{
0ba8de86 374 if (p->prev) p->prev->next = p->next;
375 else p->p->pings = p->next;
376 if (p->next) p->next->prev = p->prev;
377 if (rc != PING_TIMEOUT) sel_rmtimer(&p->t);
060ca767 378 T( trace(T_PEER, "peer: ping 0x%08lx done (rc = %d)",
379 (unsigned long)p->id, rc); )
0ba8de86 380 if (rc >= 0) p->func(rc, p->arg);
381}
382
383/* --- @p_pingtimeout@ --- *
384 *
385 * Arguments: @struct timeval *now@ = the time now
386 * @void *pv@ = pointer to ping block
387 *
388 * Returns: ---
389 *
390 * Use: Called when a ping times out.
391 */
392
393static void p_pingtimeout(struct timeval *now, void *pv)
394{
395 ping *p = pv;
396
397 T( trace(T_PEER, "peer: ping 0x%08lx timed out", (unsigned long)p->id); )
398 p_pingdone(p, PING_TIMEOUT);
399}
400
401/* --- @p_pingsend@ --- *
402 *
403 * Arguments: @peer *p@ = destination peer
404 * @ping *pg@ = structure to fill in
405 * @unsigned type@ = message type
406 * @unsigned long timeout@ = how long to wait before giving up
407 * @void (*func)(int, void *)@ = callback function
408 * @void *arg@ = argument for callback
409 *
410 * Returns: Zero if successful, nonzero if it failed.
411 *
412 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
413 * if we get an answer within the timeout, or zero if no answer.
414 */
415
416int p_pingsend(peer *p, ping *pg, unsigned type,
417 unsigned long timeout,
418 void (*func)(int, void *), void *arg)
419{
420 buf *b, bb;
421 struct timeval tv;
422
0ba8de86 423 switch (type) {
424 case MISC_PING:
425 pg->msg = MISC_PONG;
426 b = p_txstart(p, MSG_MISC | MISC_PING);
427 p_pingwrite(pg, b);
428 p_txend(p);
429 break;
430 case MISC_EPING:
431 pg->msg = MISC_EPONG;
432 b = p_txstart(p, MSG_MISC | MISC_EPING);
433 buf_init(&bb, buf_t, sizeof(buf_t));
434 p_pingwrite(pg, &bb);
435 buf_flip(&bb);
436 if (ksl_encrypt(&p->ks, MSG_MISC | MISC_EPING, &bb, b))
de014da6 437 kx_start(&p->kx, 0);
0ba8de86 438 if (!BOK(b))
439 return (-1);
440 p_txend(p);
441 break;
442 default:
443 abort();
444 break;
445 }
446
447 pg->next = p->pings;
448 pg->prev = 0;
449 pg->p = p;
450 pg->func = func;
451 pg->arg = arg;
060ca767 452 if (p->pings) p->pings->prev = pg;
0ba8de86 453 p->pings = pg;
454 gettimeofday(&tv, 0);
455 tv.tv_sec += timeout;
456 sel_addtimer(&sel, &pg->t, &tv, p_pingtimeout, pg);
457 T( trace(T_PEER, "peer: send %s 0x%08lx to %s",
458 p_pingtype(type), (unsigned long)pg->id, p->spec.name); )
459 return (0);
460}
461
37941236 462/* --- @p_greet@ --- *
463 *
464 * Arguments: @peer *p@ = peer to send to
465 * @const void *c@ = pointer to challenge
466 * @size_t sz@ = size of challenge
467 *
468 * Returns: ---
469 *
470 * Use: Sends a greeting packet.
471 */
472
473void p_greet(peer *p, const void *c, size_t sz)
474{
475 buf *b = p_txstart(p, MSG_MISC | MISC_GREET);
476 buf_put(b, c, sz);
477 p_txend(p);
478}
479
410c8acf 480/* --- @p_tun@ --- *
481 *
482 * Arguments: @peer *p@ = pointer to peer block
483 * @buf *b@ = buffer containing incoming packet
484 *
485 * Returns: ---
486 *
487 * Use: Handles a packet which needs to be sent to a peer.
488 */
489
490void p_tun(peer *p, buf *b)
491{
492 buf *bb = p_txstart(p, MSG_PACKET);
7b052fe6 493
8d0c7a83 494 TIMER;
7ed14135 495 if (ksl_encrypt(&p->ks, MSG_PACKET, b, bb))
de014da6 496 kx_start(&p->kx, 0);
5bb41301 497 if (BOK(bb) && BLEN(bb)) {
498 p->st.n_ipout++;
499 p->st.sz_ipout += BLEN(bb);
410c8acf 500 p_txend(p);
5bb41301 501 }
410c8acf 502}
503
de014da6 504/* --- @p_keyreload@ --- *
505 *
506 * Arguments: ---
507 *
508 * Returns: ---
509 *
510 * Use: Forces a check of the daemon's keyring files.
511 */
512
513void p_keyreload(void)
514{
515 peer *p;
516
517 if (km_reload()) {
518 for (p = peers; p; p = p->next)
519 kx_newkeys(&p->kx);
520 }
521}
522
410c8acf 523/* --- @p_interval@ --- *
524 *
525 * Arguments: ---
526 *
527 * Returns: ---
528 *
529 * Use: Called periodically to do tidying.
530 */
531
532void p_interval(void)
533{
de014da6 534 peer *p;
410c8acf 535
de014da6 536 p_keyreload();
537 for (p = peers; p; p = p->next)
5bb41301 538 ksl_prune(&p->ks);
410c8acf 539}
540
5bb41301 541/* --- @p_stats@ --- *
542 *
543 * Arguments: @peer *p@ = pointer to a peer block
544 *
545 * Returns: A pointer to the peer's statistics.
546 */
547
548stats *p_stats(peer *p) { return (&p->st); }
549
410c8acf 550/* --- @p_ifname@ --- *
551 *
552 * Arguments: @peer *p@ = pointer to a peer block
553 *
554 * Returns: A pointer to the peer's interface name.
555 */
556
42da2a58 557const char *p_ifname(peer *p) { return (p->t->ops->ifname(p->t)); }
410c8acf 558
559/* --- @p_addr@ --- *
560 *
561 * Arguments: @peer *p@ = pointer to a peer block
562 *
563 * Returns: A pointer to the peer's address.
564 */
565
0ba8de86 566const addr *p_addr(peer *p) { return (&p->spec.sa); }
410c8acf 567
568/* --- @p_init@ --- *
569 *
767b36e2 570 * Arguments: @struct in_addr addr@ = address to bind to
571 * @unsigned port@ = port number to listen to
410c8acf 572 *
573 * Returns: ---
574 *
575 * Use: Initializes the peer system; creates the socket.
576 */
577
767b36e2 578void p_init(struct in_addr addr, unsigned port)
410c8acf 579{
580 int fd;
581 struct sockaddr_in sin;
df9dfccf 582 int len = PKBUFSZ;
0a9920e2 583
584 /* --- Note on socket buffer sizes --- *
585 *
586 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
587 * sizes I pass to @setsockopt@. I'm not putting special-case code here
588 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
589 * guessing me.
590 */
410c8acf 591
592 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
593 die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
594 BURN(sin);
595 sin.sin_family = AF_INET;
767b36e2 596 sin.sin_addr = addr;
410c8acf 597 sin.sin_port = htons(port);
598 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
599 die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
0a9920e2 600 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
601 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
602 die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
603 strerror(errno));
604 }
650a6624 605 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
410c8acf 606 sel_initfile(&sel, &sock, fd, SEL_READ, p_read, 0);
607 sel_addfile(&sock);
608 T( trace(T_PEER, "peer: created socket"); )
609}
610
611/* --- @p_port@ --- *
612 *
613 * Arguments: ---
614 *
615 * Returns: Port number used for socket.
616 */
617
618unsigned p_port(void)
619{
620 addr a;
621 size_t sz = sizeof(addr);
622
623 if (getsockname(sock.fd, &a.sa, &sz))
624 die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
625 assert(a.sa.sa_family == AF_INET);
626 return (ntohs(a.sin.sin_port));
627}
628
0ba8de86 629/* --- @p_keepalive@ --- *
630 *
631 * Arguments: @struct timeval *now@ = the current time
632 * @void *pv@ = peer to wake up
633 *
634 * Returns: ---
635 *
636 * Use: Sends a keepalive ping message to its peer.
637 */
638
639static void p_keepalive(struct timeval *now, void *pv)
640{
641 peer *p = pv;
642 p_txstart(p, MSG_MISC | MISC_NOP); p_dotxend(p);
643 T( trace(T_PEER, "peer: sent keepalive to %s", p->spec.name); )
644 p_setkatimer(p);
645}
646
647/* --- @p_setkatimer@ --- *
648 *
649 * Arguments: @peer *p@ = peer to set
650 *
651 * Returns: ---
652 *
653 * Use: Resets the keepalive timer thing.
654 */
655
656static void p_setkatimer(peer *p)
657{
658 struct timeval tv;
659
660 if (!p->spec.t_ka)
661 return;
662 gettimeofday(&tv, 0);
663 tv.tv_sec += p->spec.t_ka;
664 sel_addtimer(&sel, &p->tka, &tv, p_keepalive, p);
665}
666
410c8acf 667/* --- @p_create@ --- *
668 *
0ba8de86 669 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 670 *
671 * Returns: Pointer to the peer block, or null if it failed.
672 *
673 * Use: Creates a new named peer block. No peer is actually attached
674 * by this point.
675 */
676
0ba8de86 677peer *p_create(peerspec *spec)
410c8acf 678{
679 peer *p = CREATE(peer);
42da2a58 680
0ba8de86 681 T( trace(T_PEER, "peer: creating new peer `%s'", spec->name); )
682 p->spec = *spec;
683 p->spec.name = xstrdup(spec->name);
410c8acf 684 p->ks = 0;
685 p->prev = 0;
629842d8 686 p->pings = 0;
5bb41301 687 memset(&p->st, 0, sizeof(stats));
688 p->st.t_start = time(0);
0ba8de86 689 if ((p->t = spec->tops->create(p)) == 0)
410c8acf 690 goto tidy_0;
0ba8de86 691 p_setkatimer(p);
692 if (kx_init(&p->kx, p, &p->ks))
410c8acf 693 goto tidy_1;
694 p->next = peers;
695 if (peers)
696 peers->prev = p;
697 peers = p;
f43df819
MW
698 a_notify("ADD",
699 "?PEER", p,
700 "%s", p->t->ops->ifname(p->t),
701 "?ADDR", &p->spec.sa,
702 A_END);
703 a_notify("KXSTART", "?PEER", p, A_END);
704 /* Couldn't tell anyone before */
410c8acf 705 return (p);
706
707tidy_1:
0ba8de86 708 if (spec->t_ka)
709 sel_rmtimer(&p->tka);
710 p->t->ops->destroy(p->t);
410c8acf 711tidy_0:
0ba8de86 712 xfree(p->spec.name);
410c8acf 713 DESTROY(p);
714 return (0);
715}
716
717/* --- @p_name@ --- *
718 *
719 * Arguments: @peer *p@ = pointer to a peer block
720 *
721 * Returns: A pointer to the peer's name.
722 */
723
0ba8de86 724const char *p_name(peer *p) { return (p->spec.name); }
410c8acf 725
060ca767 726/* --- @p_spec@ --- *
727 *
728 * Arguments: @peer *p@ = pointer to a peer block
729 *
730 * Returns: Pointer to the peer's specification
731 */
732
733const peerspec *p_spec(peer *p) { return (&p->spec); }
734
410c8acf 735/* --- @p_find@ --- *
736 *
737 * Arguments: @const char *name@ = name to look up
738 *
739 * Returns: Pointer to the peer block, or null if not found.
740 *
741 * Use: Finds a peer by name.
742 */
743
744peer *p_find(const char *name)
745{
746 peer *p;
747 for (p = peers; p; p = p->next) {
0ba8de86 748 if (strcmp(name, p->spec.name) == 0)
410c8acf 749 return (p);
750 }
751 return (0);
752}
753
754/* --- @p_destroy@ --- *
755 *
756 * Arguments: @peer *p@ = pointer to a peer
757 *
758 * Returns: ---
759 *
760 * Use: Destroys a peer.
761 */
762
763void p_destroy(peer *p)
764{
0ba8de86 765 ping *pg, *ppg;
766
767 T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
f43df819 768 a_notify("KILL", "%s", p->spec.name, A_END);
5bb41301 769 ksl_free(&p->ks);
410c8acf 770 kx_free(&p->kx);
42da2a58 771 p->t->ops->destroy(p->t);
0ba8de86 772 if (p->spec.t_ka)
773 sel_rmtimer(&p->tka);
774 xfree(p->spec.name);
775 for (pg = p->pings; pg; pg = ppg) {
776 ppg = pg->next;
777 p_pingdone(pg, PING_PEERDIED);
778 }
410c8acf 779 if (p->next)
780 p->next->prev = p->prev;
781 if (p->prev)
782 p->prev->next = p->next;
783 else
784 peers = p->next;
785 DESTROY(p);
786}
787
788/* --- @p_first@, @p_next@ --- *
789 *
790 * Arguments: @peer *p@ = a peer block
791 *
792 * Returns: @peer_first@ returns the first peer in some ordering;
793 * @peer_next@ returns the peer following a given one in the
794 * same ordering. Null is returned for the end of the list.
795 */
796
797peer *p_first(void) { return (peers); }
798peer *p_next(peer *p) { return (p->next); }
799
800/*----- That's all, folks -------------------------------------------------*/