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