3 * Communication with the peer
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Global state ------------------------------------------------------*/
32 sel_file udpsock[NADDRFAM];
34 /*----- Static variables --------------------------------------------------*/
36 static sym_table byname;
37 static addrmap byaddr;
38 static unsigned nmobile;
40 /*----- Tunnel table ------------------------------------------------------*/
42 const tunnel_ops *tunnels[] = {
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @p_pingtype@ --- *
60 * Arguments: @unsigned msg@ = message type
62 * Returns: String to describe the message.
65 static const char *p_pingtype(unsigned msg)
67 switch (msg & MSG_TYPEMASK) {
70 return "transport-ping";
73 return "encrypted-ping";
79 /* --- @p_ponged@ --- *
81 * Arguments: @peer *p@ = peer packet arrived from
82 * @unsigned msg@ = message type
83 * @buf *b@ = buffer containing payload
87 * Use: Processes a ping response.
90 static void p_ponged(peer *p, unsigned msg, buf *b)
97 trace(T_PEER, "peer: received %s reply from %s",
98 p_pingtype(msg), p->spec.name);
99 trace_block(T_PACKET, "peer: ping contents", BBASE(b), BSZ(b));
102 if (buf_getu32(b, &id) ||
103 (magic = buf_get(b, sizeof(pg->magic))) == 0 ||
105 a_warn("PEER", "?PEER", p, "malformed-%s", p_pingtype(msg), A_END);
109 for (pg = p->pings; pg; pg = pg->next) {
115 "unexpected-%s", p_pingtype(msg),
116 "0x%08lx", (unsigned long)id,
121 if (memcmp(magic, pg->magic, sizeof(pg->magic)) != 0) {
122 a_warn("PEER", "?PEER", p, "corrupt-%s", p_pingtype(msg), A_END);
125 p_pingdone(pg, PING_OK);
128 /* --- @p_rxupdstats@ --- *
130 * Arguments: @peer *p@ = peer to update
131 * @size_t n@ = size of incoming packet
135 * Use: Updates the peer's incoming packet statistics.
138 static void p_rxupdstats(peer *p, size_t n)
140 p->st.t_last = time(0);
145 /* --- @p_encrypt@ --- *
147 * Arguments: @peer *p@ = peer to encrypt message to
148 * @int ty@ message type to send
149 * @buf *bin, *bout@ = input and output buffers
153 * Use: Convenience function for packet encryption. Forces
154 * renegotiation when necessary. Check for the output buffer
155 * being broken to find out whether the encryption was
159 static int p_encrypt(peer *p, int ty, buf *bin, buf *bout)
161 int err = ksl_encrypt(&p->ks, ty, bin, bout);
163 if (err == KSERR_REGEN) {
172 /* --- @p_updateaddr@ --- *
174 * Arguments: @peer *p@ = pointer to peer block
175 * @const addr *a@ = address to associate with this peer
177 * Returns: Zero if the address was changed; @+1@ if it was already
180 * Use: Updates our idea of @p@'s address.
183 int p_updateaddr(peer *p, const addr *a)
186 peer_byaddr *pa, *qa;
190 /* --- Figure out how to proceed --- *
192 * If this address already belongs to a different peer, then swap the
193 * addresses over. This doesn't leave the displaced peer in an especially
194 * good state, but it ought to get sorted out soon enough.
197 pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f);
201 T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); )
202 am_remove(&byaddr, p->byaddr);
203 p->byaddr = pa; p->spec.sa = *a; pa->p = p;
204 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
205 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
208 q = pa->p; qa = p->byaddr;
209 T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'",
210 p_name(p), p_name(q)); )
211 q->byaddr = qa; qa->p = q; q->spec.sa = p->spec.sa;
212 p->byaddr = pa; pa->p = p; p->spec.sa = *a;
213 ix = p->afix; p->afix = q->afix; q->afix = ix;
214 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
215 a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END);
220 /* --- @p_decrypt@ --- *
222 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
223 * @addr *a@ = address the packet arrived on
224 * @size_t n@ = size of original incoming packet
225 * @int ty@ = message type to expect
226 * @buf *bin, *bout@ = input and output buffers
228 * Returns: Zero on success; nonzero on error.
230 * Use: Convenience function for packet decryption. Reports errors
231 * and updates statistics appropriately.
233 * If @*pp@ is null on entry and there are mobile peers then we
234 * see if any of them can decrypt the packet. If so, we record
235 * @*a@ as the peer's new address and send a notification.
238 static int p_decrypt(peer **pp, addr *a, size_t n,
239 int ty, buf *bin, buf *bout)
242 int err = KSERR_DECRYPT;
244 /* --- If we have a match on the source address then try that first --- */
248 T( trace(T_PEER, "peer: decrypting packet from known peer `%s'",
250 if ((err = ksl_decrypt(&q->ks, ty, bin, bout)) != KSERR_DECRYPT ||
251 !(q->spec.f & PSF_MOBILE) || nmobile == 1) {
255 T( trace(T_PEER, "peer: failed to decrypt: try other mobile peers..."); )
257 T( trace(T_PEER, "peer: unknown source: trying mobile peers...") );
263 /* --- See whether any mobile peer is interested --- */
267 if (qq == q || !(qq->spec.f & PSF_MOBILE)) continue;
268 if ((err = ksl_decrypt(&qq->ks, ty, bin, bout)) == KSERR_DECRYPT) {
269 T( trace(T_PEER, "peer: peer `%s' failed to decrypt",
276 trace(T_PEER, "peer: peer `%s' reports success", p_name(qq));
278 trace(T_PEER, "peer: peer `%s' reports decryption error %d",
286 /* --- We've searched the mobile peers --- */
291 a_warn("PEER", "-", "unexpected-source", "?ADDR", a, A_END);
293 a_warn("PEER", "?PEER", p, "decrypt-failed",
294 "error-code", "%d", err, A_END);
300 /* --- We found one that accepted, so update the peer's address --- */
310 if (p) p->st.n_reject++;
311 a_warn("PEER", "?PEER", p, "decrypt-failed",
312 "error-code", "%d", err, A_END);
320 /* --- @p_read@ --- *
322 * Arguments: @int fd@ = file descriptor to read from
323 * @unsigned mode@ = what happened
324 * @void *v@ = an uninteresting pointer
328 * Use: Reads a packet from somewhere.
331 static void p_read(int fd, unsigned mode, void *v)
341 char name[NI_MAXHOST], svc[NI_MAXSERV];
344 /* --- Read the data --- */
348 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
350 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END);
354 ix = afix(a.sa.sa_family);
355 getnameinfo(&a.sa, sz, name, sizeof(name), svc, sizeof(svc),
356 NI_NUMERICHOST | NI_NUMERICSERV);
359 /* --- If the packet is a greeting, don't check peers --- */
361 if (n && buf_i[0] == (MSG_MISC | MISC_GREET)) {
363 trace(T_PEER, "peer: greeting received from %s %s %s",
364 aftab[ix].name, name, svc);
365 trace_block(T_PACKET, "peer: greeting contents", buf_i, n);
367 buf_init(&b, buf_i, n);
369 if (c_check(&b) || BLEFT(&b)) {
370 a_warn("PEER", "-", "invalid-greeting", A_END);
374 "?B64", buf_i + 1, (size_t)(n - 1),
380 /* --- Find the appropriate peer --- *
382 * At this stage, don't worry too much about whether we actually found it.
385 p = p_findbyaddr(&a);
390 "peer: packet received from `%s' from address %s %s %s",
391 p_name(p), aftab[ix].name, name, svc);
393 trace(T_PEER, "peer: packet received from unknown address %s %s %s",
394 aftab[ix].name, name, svc);
396 trace_block(T_PACKET, "peer: packet contents", buf_i, n);
399 /* --- Pick the packet apart --- */
401 buf_init(&b, buf_i, n);
402 if ((ch = buf_getbyte(&b)) < 0) {
403 a_warn("PEER", "?PEER", p, "bad-packet", "no-type", A_END);
406 switch (ch & MSG_CATMASK) {
408 if (ch & MSG_TYPEMASK) {
412 "unknown-type", "0x%02x", ch,
414 if (p) p->st.n_reject++;
417 buf_init(&bb, buf_o, sizeof(buf_o));
418 if (p_decrypt(&p, &a, n, MSG_PACKET, &b, &bb))
422 p->st.sz_ipin += BSZ(&b);
423 p->t->ops->inject(p->t, &bb);
426 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
432 kx_message(&p->kx, ch & MSG_TYPEMASK, &b);
435 switch (ch & MSG_TYPEMASK) {
439 T( trace(T_PEER, "peer: received NOP packet"); )
444 buf_put(p_txstart(p, MSG_MISC | MISC_PONG), BCUR(&b), BLEFT(&b));
450 p_ponged(p, MISC_PONG, &b);
453 buf_init(&bb, buf_t, sizeof(buf_t));
454 if (p_decrypt(&p, &a, n, ch, &b, &bb))
458 p_encrypt(p, MSG_MISC | MISC_EPONG, &bb,
459 p_txstart(p, MSG_MISC | MISC_EPONG));
464 buf_init(&bb, buf_t, sizeof(buf_t));
465 if (p_decrypt(&p, &a, n, ch, &b, &bb))
469 p_ponged(p, MISC_EPONG, &bb);
475 if (p) p->st.n_reject++;
479 "unknown-category", "0x%02x", ch,
483 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a, A_END);
488 /* --- @p_txstart@ --- *
490 * Arguments: @peer *p@ = pointer to peer block
491 * @unsigned msg@ = message type code
493 * Returns: A pointer to a buffer to write to.
495 * Use: Starts sending to a peer. Only one send can happen at a
499 buf *p_txstart(peer *p, unsigned msg)
501 buf_init(&p->b, buf_o, sizeof(buf_o));
502 buf_putbyte(&p->b, msg);
506 /* --- @p_txend@ --- *
508 * Arguments: @peer *p@ = pointer to peer block
512 * Use: Sends a packet to the peer.
515 static void p_setkatimer(peer *);
517 static int p_dotxend(peer *p)
519 socklen_t sasz = addrsz(&p->spec.sa);
522 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
525 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
526 BBASE(&p->b), BLEN(&p->b)); )
527 if (sendto(udpsock[p->afix].fd, BBASE(&p->b), BLEN(&p->b),
528 0, &p->spec.sa.sa, sasz) < 0) {
529 a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
533 p->st.sz_out += BLEN(&p->b);
538 void p_txend(peer *p)
540 if (p_dotxend(p) && p->spec.t_ka) {
541 sel_rmtimer(&p->tka);
546 /* --- @p_pingwrite@ --- *
548 * Arguments: @ping *p@ = ping structure
549 * @buf *b@ = buffer to write in
553 * Use: Fills in a ping structure and writes the packet payload.
556 static void p_pingwrite(ping *p, buf *b)
558 static uint32 seq = 0;
561 GR_FILL(&rand_global, p->magic, sizeof(p->magic));
562 buf_putu32(b, p->id);
563 buf_put(b, p->magic, sizeof(p->magic));
566 /* --- @p_pingdone@ --- *
568 * Arguments: @ping *p@ = ping structure
569 * @int rc@ = return code to pass on
573 * Use: Disposes of a ping structure, maybe sending a notification.
576 void p_pingdone(ping *p, int rc)
578 if (p->prev) p->prev->next = p->next;
579 else p->p->pings = p->next;
580 if (p->next) p->next->prev = p->prev;
581 if (rc != PING_TIMEOUT) sel_rmtimer(&p->t);
582 T( trace(T_PEER, "peer: ping 0x%08lx done (rc = %d)",
583 (unsigned long)p->id, rc); )
584 if (rc >= 0) p->func(rc, p->arg);
587 /* --- @p_pingtimeout@ --- *
589 * Arguments: @struct timeval *now@ = the time now
590 * @void *pv@ = pointer to ping block
594 * Use: Called when a ping times out.
597 static void p_pingtimeout(struct timeval *now, void *pv)
601 T( trace(T_PEER, "peer: ping 0x%08lx timed out", (unsigned long)p->id); )
602 p_pingdone(p, PING_TIMEOUT);
605 /* --- @p_pingsend@ --- *
607 * Arguments: @peer *p@ = destination peer
608 * @ping *pg@ = structure to fill in
609 * @unsigned type@ = message type
610 * @unsigned long timeout@ = how long to wait before giving up
611 * @void (*func)(int, void *)@ = callback function
612 * @void *arg@ = argument for callback
614 * Returns: Zero if successful, nonzero if it failed.
616 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
617 * if we get an answer within the timeout, or zero if no answer.
620 int p_pingsend(peer *p, ping *pg, unsigned type,
621 unsigned long timeout,
622 void (*func)(int, void *), void *arg)
630 b = p_txstart(p, MSG_MISC | MISC_PING);
635 pg->msg = MISC_EPONG;
636 b = p_txstart(p, MSG_MISC | MISC_EPING);
637 buf_init(&bb, buf_t, sizeof(buf_t));
638 p_pingwrite(pg, &bb);
640 p_encrypt(p, MSG_MISC | MISC_EPING, &bb, b);
655 if (p->pings) p->pings->prev = pg;
657 gettimeofday(&tv, 0);
658 tv.tv_sec += timeout;
659 sel_addtimer(&sel, &pg->t, &tv, p_pingtimeout, pg);
660 T( trace(T_PEER, "peer: send %s 0x%08lx to %s",
661 p_pingtype(type), (unsigned long)pg->id, p->spec.name); )
665 /* --- @p_greet@ --- *
667 * Arguments: @peer *p@ = peer to send to
668 * @const void *c@ = pointer to challenge
669 * @size_t sz@ = size of challenge
673 * Use: Sends a greeting packet.
676 void p_greet(peer *p, const void *c, size_t sz)
678 buf *b = p_txstart(p, MSG_MISC | MISC_GREET);
685 * Arguments: @peer *p@ = pointer to peer block
686 * @buf *b@ = buffer containing incoming packet
690 * Use: Handles a packet which needs to be sent to a peer.
693 void p_tun(peer *p, buf *b)
695 buf *bb = p_txstart(p, MSG_PACKET);
698 p_encrypt(p, MSG_PACKET, b, bb);
699 if (BOK(bb) && BLEN(bb)) {
701 p->st.sz_ipout += BLEN(bb);
706 /* --- @p_keyreload@ --- *
712 * Use: Forces a check of the daemon's keyring files.
715 void p_keyreload(void)
718 FOREACH_PEER(p, { kx_newkeys(&p->kx); });
721 /* --- @p_interval@ --- *
727 * Use: Called periodically to do tidying.
730 void p_interval(void)
733 FOREACH_PEER(p, { ksl_prune(&p->ks); });
736 /* --- @p_stats@ --- *
738 * Arguments: @peer *p@ = pointer to a peer block
740 * Returns: A pointer to the peer's statistics.
743 stats *p_stats(peer *p) { return (&p->st); }
745 /* --- @p_ifname@ --- *
747 * Arguments: @peer *p@ = pointer to a peer block
749 * Returns: A pointer to the peer's interface name.
752 const char *p_ifname(peer *p) { return (p->ifname); }
754 /* --- @p_setifname@ --- *
756 * Arguments: @peer *p@ = pointer to a peer block
757 * @const char *name@ = pointer to the new name
761 * Use: Changes the name held for a peer's interface.
764 void p_setifname(peer *p, const char *name)
767 p->ifname = xstrdup(name);
768 if (p->spec.tops->setifname)
769 p->spec.tops->setifname(p->t, name);
772 /* --- @p_addr@ --- *
774 * Arguments: @peer *p@ = pointer to a peer block
776 * Returns: A pointer to the peer's address.
779 const addr *p_addr(peer *p) { return (&p->spec.sa); }
781 /* --- @p_init@ --- *
783 * Arguments: @struct addrinfo *ailist@ = addresses to bind to
787 * Use: Initializes the peer system; creates the socket.
790 void p_init(struct addrinfo *ailist)
796 unsigned port, lastport = 0;
800 for (i = 0; i < NADDRFAM; i++) udpsock[i].fd = -1;
802 for (ai = ailist; ai; ai = ai->ai_next) {
803 if ((i = afix(ai->ai_family)) < 0) continue;
804 if (udpsock[i].fd != -1) continue;
806 /* --- Note on socket buffer sizes --- *
808 * For some bizarre reason, Linux 2.2 (at least) doubles the socket
809 * buffer sizes I pass to @setsockopt@. I'm not putting special-case
810 * code here for Linux: BSD (at least TCPv2) does what I tell it rather
811 * than second-guessing me.
814 if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0)
815 die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
816 assert(ai->ai_addrlen <= sizeof(a));
817 memcpy(&a, ai->ai_addr, ai->ai_addrlen);
818 if ((port = getport(&a)) == 0 && lastport) setport(&a, lastport);
819 if (bind(fd, &a.sa, addrsz(&a)))
820 die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
821 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
822 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
823 die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
826 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
827 sel_initfile(&sel, &udpsock[i], fd, SEL_READ, p_read, 0);
828 sel_addfile(&udpsock[i]);
829 T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
832 if (getsockname(fd, &a.sa, &sz)) {
833 die(EXIT_FAILURE, "failed to read local socket address: %s",
836 lastport = getport(&a);
844 /* --- @p_port@ --- *
846 * Arguments: @int i@ = address family index to retrieve
848 * Returns: Port number used for socket.
851 unsigned p_port(int i)
854 socklen_t sz = sizeof(addr);
856 if (getsockname(udpsock[i].fd, &a.sa, &sz))
857 die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
858 return (getport(&a));
861 /* --- @p_keepalive@ --- *
863 * Arguments: @struct timeval *now@ = the current time
864 * @void *pv@ = peer to wake up
868 * Use: Sends a keepalive ping message to its peer.
871 static void p_keepalive(struct timeval *now, void *pv)
875 p_txstart(p, MSG_MISC | MISC_NOP); p_dotxend(p);
876 T( trace(T_PEER, "peer: sent keepalive to %s", p->spec.name); )
880 /* --- @p_setkatimer@ --- *
882 * Arguments: @peer *p@ = peer to set
886 * Use: Resets the keepalive timer thing.
889 static void p_setkatimer(peer *p)
895 gettimeofday(&tv, 0);
896 tv.tv_sec += p->spec.t_ka;
897 sel_addtimer(&sel, &p->tka, &tv, p_keepalive, p);
900 /* --- @p_create@ --- *
902 * Arguments: @peerspec *spec@ = information about this peer
904 * Returns: Pointer to the peer block, or null if it failed.
906 * Use: Creates a new named peer block. No peer is actually attached
910 peer *p_create(peerspec *spec)
912 peer *p = CREATE(peer);
913 const tunnel_ops *tops = spec->tops;
917 p->byname = sym_find(&byname, spec->name, -1, sizeof(peer_byname), &f);
919 p->byaddr = am_find(&byaddr, &spec->sa, sizeof(peer_byaddr), &f);
921 p->byname->p = p->byaddr->p = p;
923 T( trace(T_PEER, "peer: creating new peer `%s'", spec->name); )
925 p->spec.name = (/*unconst*/ char *)SYM_NAME(p->byname);
926 if (spec->tag) p->spec.tag = xstrdup(spec->tag);
927 if (spec->privtag) p->spec.privtag = xstrdup(spec->privtag);
931 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
932 memset(&p->st, 0, sizeof(stats));
933 p->st.t_start = time(0);
934 if (!(tops->flags & TUNF_PRIVOPEN))
936 else if ((fd = ps_tunfd(tops, &p->ifname)) < 0)
938 if ((p->t = tops->create(p, fd, &p->ifname)) == 0)
940 T( trace(T_TUNNEL, "peer: attached interface %s to peer `%s'",
941 p->ifname, p_name(p)); )
943 if (kx_init(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
948 "?ADDR", &p->spec.sa,
950 if (!(p->spec.f & KXF_CORK)) {
951 a_notify("KXSTART", "?PEER", p, A_END);
952 /* Couldn't tell anyone before */
954 if (p->spec.f & PSF_MOBILE) nmobile++;
958 if (spec->t_ka) sel_rmtimer(&p->tka);
960 p->t->ops->destroy(p->t);
962 if (fd >= 0) close(fd);
964 am_remove(&byaddr, p->byaddr);
965 if (p->spec.tag) xfree(p->spec.tag);
966 if (p->spec.privtag) xfree(p->spec.privtag);
968 sym_remove(&byname, p->byname);
974 /* --- @p_name@ --- *
976 * Arguments: @peer *p@ = pointer to a peer block
978 * Returns: A pointer to the peer's name.
981 const char *p_name(peer *p)
982 { if (p) return (p->spec.name); else return ("-"); }
986 * Arguments: @peer *p@ = pointer to a peer block
988 * Returns: A pointer to the peer's public key tag.
991 const char *p_tag(peer *p)
992 { return (p->spec.tag ? p->spec.tag : p->spec.name); }
994 /* --- @p_privtag@ --- *
996 * Arguments: @peer *p@ = pointer to a peer block
998 * Returns: A pointer to the peer's private key tag.
1001 const char *p_privtag(peer *p)
1002 { return (p->spec.privtag ? p->spec.privtag : tag_priv); }
1004 /* --- @p_spec@ --- *
1006 * Arguments: @peer *p@ = pointer to a peer block
1008 * Returns: Pointer to the peer's specification
1011 const peerspec *p_spec(peer *p) { return (&p->spec); }
1013 /* --- @p_findbyaddr@ --- *
1015 * Arguments: @const addr *a@ = address to look up
1017 * Returns: Pointer to the peer block, or null if not found.
1019 * Use: Finds a peer by address.
1022 peer *p_findbyaddr(const addr *a)
1026 if ((pa = am_find(&byaddr, a, 0, 0)) != 0) {
1033 /* --- @p_find@ --- *
1035 * Arguments: @const char *name@ = name to look up
1037 * Returns: Pointer to the peer block, or null if not found.
1039 * Use: Finds a peer by name.
1042 peer *p_find(const char *name)
1046 if ((pn = sym_find(&byname, name, -1, 0, 0)) != 0)
1051 /* --- @p_destroy@ --- *
1053 * Arguments: @peer *p@ = pointer to a peer
1057 * Use: Destroys a peer.
1060 void p_destroy(peer *p)
1064 T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
1065 a_notify("KILL", "%s", p->spec.name, A_END);
1068 if (p->spec.f & PSF_MOBILE) nmobile--;
1069 if (p->ifname) xfree(p->ifname);
1070 if (p->spec.tag) xfree(p->spec.tag);
1071 if (p->spec.privtag) xfree(p->spec.privtag);
1072 p->t->ops->destroy(p->t);
1073 if (p->spec.t_ka) sel_rmtimer(&p->tka);
1074 for (pg = p->pings; pg; pg = ppg) {
1076 p_pingdone(pg, PING_PEERDIED);
1078 sym_remove(&byname, p->byname);
1079 am_remove(&byaddr, p->byaddr);
1083 /* --- @p_mkiter@ --- *
1085 * Arguments: @peer_iter *i@ = pointer to an iterator
1089 * Use: Initializes the iterator.
1092 void p_mkiter(peer_iter *i) { sym_mkiter(&i->i, &byname); }
1094 /* --- @p_next@ --- *
1096 * Arguments: @peer_iter *i@ = pointer to an iterator
1098 * Returns: Next peer, or null if at the end.
1100 * Use: Returns the next peer.
1103 peer *p_next(peer_iter *i)
1107 if ((pn = sym_next(&i->i)) == 0)
1112 /*----- That's all, folks -------------------------------------------------*/