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 /*----- Static variables --------------------------------------------------*/
32 static sym_table byname
;
33 static addrmap byaddr
;
35 static unsigned nmobile
;
37 /*----- Tunnel table ------------------------------------------------------*/
39 const tunnel_ops
*tunnels
[] = {
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @p_pingtype@ --- *
57 * Arguments: @unsigned msg@ = message type
59 * Returns: String to describe the message.
62 static const char *p_pingtype(unsigned msg
)
64 switch (msg
& MSG_TYPEMASK
) {
67 return "transport-ping";
70 return "encrypted-ping";
76 /* --- @p_ponged@ --- *
78 * Arguments: @peer *p@ = peer packet arrived from
79 * @unsigned msg@ = message type
80 * @buf *b@ = buffer containing payload
84 * Use: Processes a ping response.
87 static void p_ponged(peer
*p
, unsigned msg
, buf
*b
)
94 trace(T_PEER
, "peer: received %s reply from %s",
95 p_pingtype(msg
), p
->spec
.name
);
96 trace_block(T_PACKET
, "peer: ping contents", BBASE(b
), BSZ(b
));
99 if (buf_getu32(b
, &id
) ||
100 (magic
= buf_get(b
, sizeof(pg
->magic
))) == 0 ||
102 a_warn("PEER", "?PEER", p
, "malformed-%s", p_pingtype(msg
), A_END
);
106 for (pg
= p
->pings
; pg
; pg
= pg
->next
) {
112 "unexpected-%s", p_pingtype(msg
),
113 "0x%08lx", (unsigned long)id
,
118 if (memcmp(magic
, pg
->magic
, sizeof(pg
->magic
)) != 0) {
119 a_warn("PEER", "?PEER", p
, "corrupt-%s", p_pingtype(msg
), A_END
);
122 p_pingdone(pg
, PING_OK
);
125 /* --- @p_rxupdstats@ --- *
127 * Arguments: @peer *p@ = peer to update
128 * @size_t n@ = size of incoming packet
132 * Use: Updates the peer's incoming packet statistics.
135 static void p_rxupdstats(peer
*p
, size_t n
)
137 p
->st
.t_last
= time(0);
142 /* --- @p_encrypt@ --- *
144 * Arguments: @peer *p@ = peer to encrypt message to
145 * @int ty@ message type to send
146 * @buf *bin, *bout@ = input and output buffers
150 * Use: Convenience function for packet encryption. Forces
151 * renegotiation when necessary. Check for the output buffer
152 * being broken to find out whether the encryption was
156 static int p_encrypt(peer
*p
, int ty
, buf
*bin
, buf
*bout
)
158 int err
= ksl_encrypt(&p
->ks
, ty
, bin
, bout
);
160 if (err
== KSERR_REGEN
) {
169 /* --- @p_decrypt@ --- *
171 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
172 * @addr *a@ = address the packet arrived on
173 * @size_t n@ = size of original incoming packet
174 * @int ty@ = message type to expect
175 * @buf *bin, *bout@ = input and output buffers
177 * Returns: Zero on success; nonzero on error.
179 * Use: Convenience function for packet decryption. Reports errors
180 * and updates statistics appropriately.
182 * If @*pp@ is null on entry and there are mobile peers then we
183 * see if any of them can decrypt the packet. If so, we record
184 * @*a@ as the peer's new address and send a notification.
187 static int p_decrypt(peer
**pp
, addr
*a
, size_t n
,
188 int ty
, buf
*bin
, buf
*bout
)
191 peer_byaddr
*pa
, *qa
;
192 int err
= KSERR_DECRYPT
;
195 /* --- If we have a match on the source address then try that first --- */
199 T( trace(T_PEER
, "peer: decrypting packet from known peer `%s'",
201 if ((err
= ksl_decrypt(&q
->ks
, ty
, bin
, bout
)) != KSERR_DECRYPT
||
202 !(q
->spec
.f
& PSF_MOBILE
) || nmobile
== 1) {
206 T( trace(T_PEER
, "peer: failed to decrypt: try other mobile peers..."); )
208 T( trace(T_PEER
, "peer: unknown source: trying mobile peers...") );
214 /* --- See whether any mobile peer is interested --- */
218 if (qq
== q
|| !(qq
->spec
.f
& PSF_MOBILE
)) continue;
219 if ((err
= ksl_decrypt(&qq
->ks
, ty
, bin
, bout
)) == KSERR_DECRYPT
) {
220 T( trace(T_PEER
, "peer: peer `%s' failed to decrypt",
227 trace(T_PEER
, "peer: peer `%s' reports success", p_name(qq
));
229 trace(T_PEER
, "peer: peer `%s' reports decryption error %d",
237 /* --- We've searched the mobile peers --- */
242 a_warn("PEER", "-", "unexpected-source", "?ADDR", a
, A_END
);
244 a_warn("PEER", "?PEER", p
, "decrypt-failed",
245 "error-code", "%d", err
, A_END
);
251 /* --- We found one that accepted, so update the peer's address --- *
253 * If we had an initial guess of which peer this packet came from -- i.e.,
254 * @q@ is not null -- then swap the addresses over. This doesn't leave the
255 * evicted peer in an especially good state, but it ought to get sorted out
262 T( trace(T_PEER
, "peer: updating address for `%s'", p_name(p
)); )
263 pa
= am_find(&byaddr
, a
, sizeof(peer_byaddr
), &f
); assert(!f
);
264 am_remove(&byaddr
, p
->byaddr
);
268 a_notify("NEWADDR", "?PEER", p
, "?ADDR", a
, A_END
);
270 T( trace(T_PEER
, "peer: swapping addresses for `%s' and `%s'",
271 p_name(p
), p_name(q
)); )
272 pa
= p
->byaddr
; qa
= q
->byaddr
;
273 pa
->p
= q
; q
->byaddr
= pa
; q
->spec
.sa
= p
->spec
.sa
;
274 qa
->p
= p
; p
->byaddr
= qa
; p
->spec
.sa
= *a
;
275 a_notify("NEWADDR", "?PEER", p
, "?ADDR", a
, A_END
);
276 a_notify("NEWADDR", "?PEER", q
, "?ADDR", &q
->spec
.sa
, A_END
);
283 if (p
) p
->st
.n_reject
++;
284 a_warn("PEER", "?PEER", p
, "decrypt-failed",
285 "error-code", "%d", err
, A_END
);
293 /* --- @p_read@ --- *
295 * Arguments: @int fd@ = file descriptor to read from
296 * @unsigned mode@ = what happened
297 * @void *v@ = an uninteresting pointer
301 * Use: Reads a packet from somewhere.
304 static void p_read(int fd
, unsigned mode
, void *v
)
313 /* --- Read the data --- */
317 n
= recvfrom(fd
, buf_i
, sizeof(buf_i
), 0, &a
.sa
, &sz
);
319 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END
);
323 /* --- If the packet is a greeting, don't check peers --- */
325 if (n
&& buf_i
[0] == (MSG_MISC
| MISC_GREET
)) {
327 trace(T_PEER
, "peer: greeting received from INET %s %u",
328 inet_ntoa(a
.sin
.sin_addr
),
329 (unsigned)ntohs(a
.sin
.sin_port
));
330 trace_block(T_PACKET
, "peer: greeting contents", buf_i
, n
);
332 buf_init(&b
, buf_i
, n
);
334 if (c_check(&b
) || BLEFT(&b
)) {
335 a_warn("PEER", "-", "invalid-greeting", A_END
);
339 "?B64", buf_i
+ 1, (size_t)(n
- 1),
345 /* --- Find the appropriate peer --- *
347 * At this stage, don't worry too much about whether we actually found it.
350 p
= p_findbyaddr(&a
);
355 "peer: packet received from `%s' from address INET %s %d",
356 p_name(p
), inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
358 trace(T_PEER
, "peer: packet received from unknown address INET %s %d",
359 inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
361 trace_block(T_PACKET
, "peer: packet contents", buf_i
, n
);
364 /* --- Pick the packet apart --- */
366 buf_init(&b
, buf_i
, n
);
367 if ((ch
= buf_getbyte(&b
)) < 0) {
368 a_warn("PEER", "?PEER", p
, "bad-packet", "no-type", A_END
);
371 switch (ch
& MSG_CATMASK
) {
373 if (ch
& MSG_TYPEMASK
) {
377 "unknown-type", "0x%02x", ch
,
379 if (p
) p
->st
.n_reject
++;
382 buf_init(&bb
, buf_o
, sizeof(buf_o
));
383 if (p_decrypt(&p
, &a
, n
, MSG_PACKET
, &b
, &bb
))
387 p
->st
.sz_ipin
+= BSZ(&b
);
388 p
->t
->ops
->inject(p
->t
, &bb
);
391 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
397 kx_message(&p
->kx
, ch
& MSG_TYPEMASK
, &b
);
400 switch (ch
& MSG_TYPEMASK
) {
404 T( trace(T_PEER
, "peer: received NOP packet"); )
409 buf_put(p_txstart(p
, MSG_MISC
| MISC_PONG
), BCUR(&b
), BLEFT(&b
));
415 p_ponged(p
, MISC_PONG
, &b
);
418 buf_init(&bb
, buf_t
, sizeof(buf_t
));
419 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
423 p_encrypt(p
, MSG_MISC
| MISC_EPONG
, &bb
,
424 p_txstart(p
, MSG_MISC
| MISC_EPONG
));
429 buf_init(&bb
, buf_t
, sizeof(buf_t
));
430 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
434 p_ponged(p
, MISC_EPONG
, &bb
);
440 if (p
) p
->st
.n_reject
++;
444 "unknown-category", "0x%02x", ch
,
448 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a
, A_END
);
453 /* --- @p_txstart@ --- *
455 * Arguments: @peer *p@ = pointer to peer block
456 * @unsigned msg@ = message type code
458 * Returns: A pointer to a buffer to write to.
460 * Use: Starts sending to a peer. Only one send can happen at a
464 buf
*p_txstart(peer
*p
, unsigned msg
)
466 buf_init(&p
->b
, buf_o
, sizeof(buf_o
));
467 buf_putbyte(&p
->b
, msg
);
471 /* --- @p_txend@ --- *
473 * Arguments: @peer *p@ = pointer to peer block
477 * Use: Sends a packet to the peer.
480 static void p_setkatimer(peer
*);
482 static int p_dotxend(peer
*p
)
485 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
488 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet",
489 BBASE(&p
->b
), BLEN(&p
->b
)); )
490 if (sendto(sock
.fd
, BBASE(&p
->b
), BLEN(&p
->b
),
491 0, &p
->spec
.sa
.sa
, p
->spec
.sasz
) < 0) {
492 a_warn("PEER", "?PEER", p
, "socket-write-error", "?ERRNO", A_END
);
496 p
->st
.sz_out
+= BLEN(&p
->b
);
501 void p_txend(peer
*p
)
503 if (p_dotxend(p
) && p
->spec
.t_ka
) {
504 sel_rmtimer(&p
->tka
);
509 /* --- @p_pingwrite@ --- *
511 * Arguments: @ping *p@ = ping structure
512 * @buf *b@ = buffer to write in
516 * Use: Fills in a ping structure and writes the packet payload.
519 static void p_pingwrite(ping
*p
, buf
*b
)
521 static uint32 seq
= 0;
524 GR_FILL(&rand_global
, p
->magic
, sizeof(p
->magic
));
525 buf_putu32(b
, p
->id
);
526 buf_put(b
, p
->magic
, sizeof(p
->magic
));
529 /* --- @p_pingdone@ --- *
531 * Arguments: @ping *p@ = ping structure
532 * @int rc@ = return code to pass on
536 * Use: Disposes of a ping structure, maybe sending a notification.
539 void p_pingdone(ping
*p
, int rc
)
541 if (p
->prev
) p
->prev
->next
= p
->next
;
542 else p
->p
->pings
= p
->next
;
543 if (p
->next
) p
->next
->prev
= p
->prev
;
544 if (rc
!= PING_TIMEOUT
) sel_rmtimer(&p
->t
);
545 T( trace(T_PEER
, "peer: ping 0x%08lx done (rc = %d)",
546 (unsigned long)p
->id
, rc
); )
547 if (rc
>= 0) p
->func(rc
, p
->arg
);
550 /* --- @p_pingtimeout@ --- *
552 * Arguments: @struct timeval *now@ = the time now
553 * @void *pv@ = pointer to ping block
557 * Use: Called when a ping times out.
560 static void p_pingtimeout(struct timeval
*now
, void *pv
)
564 T( trace(T_PEER
, "peer: ping 0x%08lx timed out", (unsigned long)p
->id
); )
565 p_pingdone(p
, PING_TIMEOUT
);
568 /* --- @p_pingsend@ --- *
570 * Arguments: @peer *p@ = destination peer
571 * @ping *pg@ = structure to fill in
572 * @unsigned type@ = message type
573 * @unsigned long timeout@ = how long to wait before giving up
574 * @void (*func)(int, void *)@ = callback function
575 * @void *arg@ = argument for callback
577 * Returns: Zero if successful, nonzero if it failed.
579 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
580 * if we get an answer within the timeout, or zero if no answer.
583 int p_pingsend(peer
*p
, ping
*pg
, unsigned type
,
584 unsigned long timeout
,
585 void (*func
)(int, void *), void *arg
)
593 b
= p_txstart(p
, MSG_MISC
| MISC_PING
);
598 pg
->msg
= MISC_EPONG
;
599 b
= p_txstart(p
, MSG_MISC
| MISC_EPING
);
600 buf_init(&bb
, buf_t
, sizeof(buf_t
));
601 p_pingwrite(pg
, &bb
);
603 p_encrypt(p
, MSG_MISC
| MISC_EPING
, &bb
, b
);
618 if (p
->pings
) p
->pings
->prev
= pg
;
620 gettimeofday(&tv
, 0);
621 tv
.tv_sec
+= timeout
;
622 sel_addtimer(&sel
, &pg
->t
, &tv
, p_pingtimeout
, pg
);
623 T( trace(T_PEER
, "peer: send %s 0x%08lx to %s",
624 p_pingtype(type
), (unsigned long)pg
->id
, p
->spec
.name
); )
628 /* --- @p_greet@ --- *
630 * Arguments: @peer *p@ = peer to send to
631 * @const void *c@ = pointer to challenge
632 * @size_t sz@ = size of challenge
636 * Use: Sends a greeting packet.
639 void p_greet(peer
*p
, const void *c
, size_t sz
)
641 buf
*b
= p_txstart(p
, MSG_MISC
| MISC_GREET
);
648 * Arguments: @peer *p@ = pointer to peer block
649 * @buf *b@ = buffer containing incoming packet
653 * Use: Handles a packet which needs to be sent to a peer.
656 void p_tun(peer
*p
, buf
*b
)
658 buf
*bb
= p_txstart(p
, MSG_PACKET
);
661 p_encrypt(p
, MSG_PACKET
, b
, bb
);
662 if (BOK(bb
) && BLEN(bb
)) {
664 p
->st
.sz_ipout
+= BLEN(bb
);
669 /* --- @p_keyreload@ --- *
675 * Use: Forces a check of the daemon's keyring files.
678 void p_keyreload(void)
681 FOREACH_PEER(p
, { kx_newkeys(&p
->kx
); });
684 /* --- @p_interval@ --- *
690 * Use: Called periodically to do tidying.
693 void p_interval(void)
696 FOREACH_PEER(p
, { ksl_prune(&p
->ks
); });
699 /* --- @p_stats@ --- *
701 * Arguments: @peer *p@ = pointer to a peer block
703 * Returns: A pointer to the peer's statistics.
706 stats
*p_stats(peer
*p
) { return (&p
->st
); }
708 /* --- @p_ifname@ --- *
710 * Arguments: @peer *p@ = pointer to a peer block
712 * Returns: A pointer to the peer's interface name.
715 const char *p_ifname(peer
*p
) { return (p
->ifname
); }
717 /* --- @p_setifname@ --- *
719 * Arguments: @peer *p@ = pointer to a peer block
720 * @const char *name@ = pointer to the new name
724 * Use: Changes the name held for a peer's interface.
727 void p_setifname(peer
*p
, const char *name
)
730 p
->ifname
= xstrdup(name
);
731 if (p
->spec
.tops
->setifname
)
732 p
->spec
.tops
->setifname(p
->t
, name
);
735 /* --- @p_addr@ --- *
737 * Arguments: @peer *p@ = pointer to a peer block
739 * Returns: A pointer to the peer's address.
742 const addr
*p_addr(peer
*p
) { return (&p
->spec
.sa
); }
744 /* --- @p_init@ --- *
746 * Arguments: @struct in_addr addr@ = address to bind to
747 * @unsigned port@ = port number to listen to
751 * Use: Initializes the peer system; creates the socket.
754 void p_init(struct in_addr addr
, unsigned port
)
757 struct sockaddr_in sin
;
760 /* --- Note on socket buffer sizes --- *
762 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
763 * sizes I pass to @setsockopt@. I'm not putting special-case code here
764 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
768 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
769 die(EXIT_FAILURE
, "socket creation failed: %s", strerror(errno
));
771 sin
.sin_family
= AF_INET
;
773 sin
.sin_port
= htons(port
);
774 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
775 die(EXIT_FAILURE
, "bind failed: %s", strerror(errno
));
776 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
777 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
))) {
778 die(EXIT_FAILURE
, "failed to set socket buffer sizes: %s",
781 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
782 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, p_read
, 0);
784 T( trace(T_PEER
, "peer: created socket"); )
790 /* --- @p_port@ --- *
794 * Returns: Port number used for socket.
797 unsigned p_port(void)
800 socklen_t sz
= sizeof(addr
);
802 if (getsockname(sock
.fd
, &a
.sa
, &sz
))
803 die(EXIT_FAILURE
, "couldn't read port number: %s", strerror(errno
));
804 assert(a
.sa
.sa_family
== AF_INET
);
805 return (ntohs(a
.sin
.sin_port
));
808 /* --- @p_keepalive@ --- *
810 * Arguments: @struct timeval *now@ = the current time
811 * @void *pv@ = peer to wake up
815 * Use: Sends a keepalive ping message to its peer.
818 static void p_keepalive(struct timeval
*now
, void *pv
)
822 p_txstart(p
, MSG_MISC
| MISC_NOP
); p_dotxend(p
);
823 T( trace(T_PEER
, "peer: sent keepalive to %s", p
->spec
.name
); )
827 /* --- @p_setkatimer@ --- *
829 * Arguments: @peer *p@ = peer to set
833 * Use: Resets the keepalive timer thing.
836 static void p_setkatimer(peer
*p
)
842 gettimeofday(&tv
, 0);
843 tv
.tv_sec
+= p
->spec
.t_ka
;
844 sel_addtimer(&sel
, &p
->tka
, &tv
, p_keepalive
, p
);
847 /* --- @p_create@ --- *
849 * Arguments: @peerspec *spec@ = information about this peer
851 * Returns: Pointer to the peer block, or null if it failed.
853 * Use: Creates a new named peer block. No peer is actually attached
857 peer
*p_create(peerspec
*spec
)
859 peer
*p
= CREATE(peer
);
860 const tunnel_ops
*tops
= spec
->tops
;
864 p
->byname
= sym_find(&byname
, spec
->name
, -1, sizeof(peer_byname
), &f
);
866 p
->byaddr
= am_find(&byaddr
, &spec
->sa
, sizeof(peer_byaddr
), &f
);
868 p
->byname
->p
= p
->byaddr
->p
= p
;
870 T( trace(T_PEER
, "peer: creating new peer `%s'", spec
->name
); )
872 p
->spec
.name
= (/*unconst*/ char *)SYM_NAME(p
->byname
);
873 if (spec
->tag
) p
->spec
.tag
= xstrdup(spec
->tag
);
874 if (spec
->privtag
) p
->spec
.privtag
= xstrdup(spec
->privtag
);
878 memset(&p
->st
, 0, sizeof(stats
));
879 p
->st
.t_start
= time(0);
880 if (!(tops
->flags
& TUNF_PRIVOPEN
))
882 else if ((fd
= ps_tunfd(tops
, &p
->ifname
)) < 0)
884 if ((p
->t
= tops
->create(p
, fd
, &p
->ifname
)) == 0)
886 T( trace(T_TUNNEL
, "peer: attached interface %s to peer `%s'",
887 p
->ifname
, p_name(p
)); )
889 if (kx_init(&p
->kx
, p
, &p
->ks
, p
->spec
.f
& PSF_KXMASK
))
894 "?ADDR", &p
->spec
.sa
,
896 if (!(p
->spec
.f
& KXF_CORK
)) {
897 a_notify("KXSTART", "?PEER", p
, A_END
);
898 /* Couldn't tell anyone before */
900 if (p
->spec
.f
& PSF_MOBILE
) nmobile
++;
904 if (spec
->t_ka
) sel_rmtimer(&p
->tka
);
906 p
->t
->ops
->destroy(p
->t
);
908 if (fd
>= 0) close(fd
);
910 am_remove(&byaddr
, p
->byaddr
);
911 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
912 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
914 sym_remove(&byname
, p
->byname
);
920 /* --- @p_name@ --- *
922 * Arguments: @peer *p@ = pointer to a peer block
924 * Returns: A pointer to the peer's name.
927 const char *p_name(peer
*p
)
928 { if (p
) return (p
->spec
.name
); else return ("-"); }
932 * Arguments: @peer *p@ = pointer to a peer block
934 * Returns: A pointer to the peer's public key tag.
937 const char *p_tag(peer
*p
)
938 { return (p
->spec
.tag ? p
->spec
.tag
: p
->spec
.name
); }
940 /* --- @p_privtag@ --- *
942 * Arguments: @peer *p@ = pointer to a peer block
944 * Returns: A pointer to the peer's private key tag.
947 const char *p_privtag(peer
*p
)
948 { return (p
->spec
.privtag ? p
->spec
.privtag
: tag_priv
); }
950 /* --- @p_spec@ --- *
952 * Arguments: @peer *p@ = pointer to a peer block
954 * Returns: Pointer to the peer's specification
957 const peerspec
*p_spec(peer
*p
) { return (&p
->spec
); }
959 /* --- @p_findbyaddr@ --- *
961 * Arguments: @const addr *a@ = address to look up
963 * Returns: Pointer to the peer block, or null if not found.
965 * Use: Finds a peer by address.
968 peer
*p_findbyaddr(const addr
*a
)
972 if ((pa
= am_find(&byaddr
, a
, 0, 0)) != 0) {
979 /* --- @p_find@ --- *
981 * Arguments: @const char *name@ = name to look up
983 * Returns: Pointer to the peer block, or null if not found.
985 * Use: Finds a peer by name.
988 peer
*p_find(const char *name
)
992 if ((pn
= sym_find(&byname
, name
, -1, 0, 0)) != 0)
997 /* --- @p_destroy@ --- *
999 * Arguments: @peer *p@ = pointer to a peer
1003 * Use: Destroys a peer.
1006 void p_destroy(peer
*p
)
1010 T( trace(T_PEER
, "peer: destroying peer `%s'", p
->spec
.name
); )
1011 a_notify("KILL", "%s", p
->spec
.name
, A_END
);
1014 if (p
->spec
.f
& PSF_MOBILE
) nmobile
--;
1015 if (p
->ifname
) xfree(p
->ifname
);
1016 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
1017 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
1018 p
->t
->ops
->destroy(p
->t
);
1019 if (p
->spec
.t_ka
) sel_rmtimer(&p
->tka
);
1020 for (pg
= p
->pings
; pg
; pg
= ppg
) {
1022 p_pingdone(pg
, PING_PEERDIED
);
1024 sym_remove(&byname
, p
->byname
);
1025 am_remove(&byaddr
, p
->byaddr
);
1029 /* --- @p_mkiter@ --- *
1031 * Arguments: @peer_iter *i@ = pointer to an iterator
1035 * Use: Initializes the iterator.
1038 void p_mkiter(peer_iter
*i
) { sym_mkiter(&i
->i
, &byname
); }
1040 /* --- @p_next@ --- *
1042 * Arguments: @peer_iter *i@ = pointer to an iterator
1044 * Returns: Next peer, or null if at the end.
1046 * Use: Returns the next peer.
1049 peer
*p_next(peer_iter
*i
)
1053 if ((pn
= sym_next(&i
->i
)) == 0)
1058 /*----- That's all, folks -------------------------------------------------*/