f600f99e8531794845427c65328a2b49fd0e99bd
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_updateaddr@ --- *
171 * Arguments: @peer *p@ = pointer to peer block
172 * @const addr *a@ = address to associate with this peer
174 * Returns: Zero if the address was changed; @+1@ if it was already
177 * Use: Updates our idea of @p@'s address.
180 int p_updateaddr(peer
*p
, const addr
*a
)
183 peer_byaddr
*pa
, *qa
;
186 /* --- Figure out how to proceed --- *
188 * If this address already belongs to a different peer, then swap the
189 * addresses over. This doesn't leave the displaced peer in an especially
190 * good state, but it ought to get sorted out soon enough.
193 pa
= am_find(&byaddr
, a
, sizeof(peer_byaddr
), &f
);
197 T( trace(T_PEER
, "peer: updating address for `%s'", p_name(p
)); )
198 am_remove(&byaddr
, p
->byaddr
);
199 p
->byaddr
= pa
; p
->spec
.sa
= *a
; pa
->p
= p
;
200 a_notify("NEWADDR", "?PEER", p
, "?ADDR", a
, A_END
);
203 q
= pa
->p
; qa
= p
->byaddr
;
204 T( trace(T_PEER
, "peer: swapping addresses for `%s' and `%s'",
205 p_name(p
), p_name(q
)); )
206 q
->byaddr
= qa
; qa
->p
= q
; q
->spec
.sa
= p
->spec
.sa
;
207 p
->byaddr
= pa
; pa
->p
= p
; p
->spec
.sa
= *a
;
208 a_notify("NEWADDR", "?PEER", p
, "?ADDR", a
, A_END
);
209 a_notify("NEWADDR", "?PEER", q
, "?ADDR", &q
->spec
.sa
, A_END
);
214 /* --- @p_decrypt@ --- *
216 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
217 * @addr *a@ = address the packet arrived on
218 * @size_t n@ = size of original incoming packet
219 * @int ty@ = message type to expect
220 * @buf *bin, *bout@ = input and output buffers
222 * Returns: Zero on success; nonzero on error.
224 * Use: Convenience function for packet decryption. Reports errors
225 * and updates statistics appropriately.
227 * If @*pp@ is null on entry and there are mobile peers then we
228 * see if any of them can decrypt the packet. If so, we record
229 * @*a@ as the peer's new address and send a notification.
232 static int p_decrypt(peer
**pp
, addr
*a
, size_t n
,
233 int ty
, buf
*bin
, buf
*bout
)
236 int err
= KSERR_DECRYPT
;
238 /* --- If we have a match on the source address then try that first --- */
242 T( trace(T_PEER
, "peer: decrypting packet from known peer `%s'",
244 if ((err
= ksl_decrypt(&q
->ks
, ty
, bin
, bout
)) != KSERR_DECRYPT
||
245 !(q
->spec
.f
& PSF_MOBILE
) || nmobile
== 1) {
249 T( trace(T_PEER
, "peer: failed to decrypt: try other mobile peers..."); )
251 T( trace(T_PEER
, "peer: unknown source: trying mobile peers...") );
257 /* --- See whether any mobile peer is interested --- */
261 if (qq
== q
|| !(qq
->spec
.f
& PSF_MOBILE
)) continue;
262 if ((err
= ksl_decrypt(&qq
->ks
, ty
, bin
, bout
)) == KSERR_DECRYPT
) {
263 T( trace(T_PEER
, "peer: peer `%s' failed to decrypt",
270 trace(T_PEER
, "peer: peer `%s' reports success", p_name(qq
));
272 trace(T_PEER
, "peer: peer `%s' reports decryption error %d",
280 /* --- We've searched the mobile peers --- */
285 a_warn("PEER", "-", "unexpected-source", "?ADDR", a
, A_END
);
287 a_warn("PEER", "?PEER", p
, "decrypt-failed",
288 "error-code", "%d", err
, A_END
);
294 /* --- We found one that accepted, so update the peer's address --- */
304 if (p
) p
->st
.n_reject
++;
305 a_warn("PEER", "?PEER", p
, "decrypt-failed",
306 "error-code", "%d", err
, A_END
);
314 /* --- @p_read@ --- *
316 * Arguments: @int fd@ = file descriptor to read from
317 * @unsigned mode@ = what happened
318 * @void *v@ = an uninteresting pointer
322 * Use: Reads a packet from somewhere.
325 static void p_read(int fd
, unsigned mode
, void *v
)
334 /* --- Read the data --- */
338 n
= recvfrom(fd
, buf_i
, sizeof(buf_i
), 0, &a
.sa
, &sz
);
340 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END
);
344 /* --- If the packet is a greeting, don't check peers --- */
346 if (n
&& buf_i
[0] == (MSG_MISC
| MISC_GREET
)) {
348 trace(T_PEER
, "peer: greeting received from INET %s %u",
349 inet_ntoa(a
.sin
.sin_addr
),
350 (unsigned)ntohs(a
.sin
.sin_port
));
351 trace_block(T_PACKET
, "peer: greeting contents", buf_i
, n
);
353 buf_init(&b
, buf_i
, n
);
355 if (c_check(0, 0, &b
) || BLEFT(&b
)) {
356 a_warn("PEER", "-", "invalid-greeting", A_END
);
360 "?B64", buf_i
+ 1, (size_t)(n
- 1),
366 /* --- Find the appropriate peer --- *
368 * At this stage, don't worry too much about whether we actually found it.
371 p
= p_findbyaddr(&a
);
376 "peer: packet received from `%s' from address INET %s %d",
377 p_name(p
), inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
379 trace(T_PEER
, "peer: packet received from unknown address INET %s %d",
380 inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
382 trace_block(T_PACKET
, "peer: packet contents", buf_i
, n
);
385 /* --- Pick the packet apart --- */
387 buf_init(&b
, buf_i
, n
);
388 if ((ch
= buf_getbyte(&b
)) < 0) {
389 a_warn("PEER", "?PEER", p
, "bad-packet", "no-type", A_END
);
392 switch (ch
& MSG_CATMASK
) {
394 if (ch
& MSG_TYPEMASK
) {
398 "unknown-type", "0x%02x", ch
,
400 if (p
) p
->st
.n_reject
++;
403 buf_init(&bb
, buf_o
, sizeof(buf_o
));
404 if (p_decrypt(&p
, &a
, n
, MSG_PACKET
, &b
, &bb
))
408 p
->st
.sz_ipin
+= BSZ(&b
);
409 p
->t
->ops
->inject(p
->t
, &bb
);
412 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
416 if (p
) p_rxupdstats(p
, n
);
417 if (kx_message(p ?
&p
->kx
: 0, &a
, ch
& MSG_TYPEMASK
, &b
)) goto unexp
;
420 switch (ch
& MSG_TYPEMASK
) {
424 T( trace(T_PEER
, "peer: received NOP packet"); )
429 buf_put(p_txstart(p
, MSG_MISC
| MISC_PONG
), BCUR(&b
), BLEFT(&b
));
435 p_ponged(p
, MISC_PONG
, &b
);
438 buf_init(&bb
, buf_t
, sizeof(buf_t
));
439 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
443 p_encrypt(p
, MSG_MISC
| MISC_EPONG
, &bb
,
444 p_txstart(p
, MSG_MISC
| MISC_EPONG
));
449 buf_init(&bb
, buf_t
, sizeof(buf_t
));
450 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
454 p_ponged(p
, MISC_EPONG
, &bb
);
460 if (p
) p
->st
.n_reject
++;
464 "unknown-category", "0x%02x", ch
,
468 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a
, A_END
);
473 /* --- @p_txstart@ --- *
475 * Arguments: @peer *p@ = pointer to peer block
476 * @unsigned msg@ = message type code
478 * Returns: A pointer to a buffer to write to.
480 * Use: Starts sending to a peer. Only one send can happen at a
484 buf
*p_txstart(peer
*p
, unsigned msg
)
486 buf_init(&p
->b
, buf_o
, sizeof(buf_o
));
487 buf_putbyte(&p
->b
, msg
);
491 /* --- @p_txaddr@ --- *
493 * Arguments: @const addr *a@ = recipient address
494 * @const void *p@ = pointer to packet to send
495 * @size_t sz@ = length of packet
497 * Returns: Zero if successful, nonzero on error.
499 * Use: Sends a packet to an address which (possibly) isn't a current
503 int p_txaddr(const addr
*a
, const void *p
, size_t sz
)
505 socklen_t sasz
= addrsz(a
);
507 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet", p
, sz
); )
508 if (sendto(sock
.fd
, p
, sz
, 0, &a
->sa
, sasz
) < 0) {
509 a_warn("PEER", "?ADDR", a
, "socket-write-error", "?ERRNO", A_END
);
515 /* --- @p_txend@ --- *
517 * Arguments: @peer *p@ = pointer to peer block
521 * Use: Sends a packet to the peer.
524 static void p_setkatimer(peer
*);
526 static int p_dotxend(peer
*p
)
528 socklen_t sasz
= addrsz(&p
->spec
.sa
);
531 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
534 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet",
535 BBASE(&p
->b
), BLEN(&p
->b
)); )
536 if (sendto(sock
.fd
, BBASE(&p
->b
), BLEN(&p
->b
),
537 0, &p
->spec
.sa
.sa
, sasz
) < 0) {
538 a_warn("PEER", "?PEER", p
, "socket-write-error", "?ERRNO", A_END
);
542 p
->st
.sz_out
+= BLEN(&p
->b
);
547 void p_txend(peer
*p
)
549 if (p_dotxend(p
) && p
->spec
.t_ka
) {
550 sel_rmtimer(&p
->tka
);
555 /* --- @p_pingwrite@ --- *
557 * Arguments: @ping *p@ = ping structure
558 * @buf *b@ = buffer to write in
562 * Use: Fills in a ping structure and writes the packet payload.
565 static void p_pingwrite(ping
*p
, buf
*b
)
567 static uint32 seq
= 0;
570 GR_FILL(&rand_global
, p
->magic
, sizeof(p
->magic
));
571 buf_putu32(b
, p
->id
);
572 buf_put(b
, p
->magic
, sizeof(p
->magic
));
575 /* --- @p_pingdone@ --- *
577 * Arguments: @ping *p@ = ping structure
578 * @int rc@ = return code to pass on
582 * Use: Disposes of a ping structure, maybe sending a notification.
585 void p_pingdone(ping
*p
, int rc
)
587 if (p
->prev
) p
->prev
->next
= p
->next
;
588 else p
->p
->pings
= p
->next
;
589 if (p
->next
) p
->next
->prev
= p
->prev
;
590 if (rc
!= PING_TIMEOUT
) sel_rmtimer(&p
->t
);
591 T( trace(T_PEER
, "peer: ping 0x%08lx done (rc = %d)",
592 (unsigned long)p
->id
, rc
); )
593 if (rc
>= 0) p
->func(rc
, p
->arg
);
596 /* --- @p_pingtimeout@ --- *
598 * Arguments: @struct timeval *now@ = the time now
599 * @void *pv@ = pointer to ping block
603 * Use: Called when a ping times out.
606 static void p_pingtimeout(struct timeval
*now
, void *pv
)
610 T( trace(T_PEER
, "peer: ping 0x%08lx timed out", (unsigned long)p
->id
); )
611 p_pingdone(p
, PING_TIMEOUT
);
614 /* --- @p_pingsend@ --- *
616 * Arguments: @peer *p@ = destination peer
617 * @ping *pg@ = structure to fill in
618 * @unsigned type@ = message type
619 * @unsigned long timeout@ = how long to wait before giving up
620 * @void (*func)(int, void *)@ = callback function
621 * @void *arg@ = argument for callback
623 * Returns: Zero if successful, nonzero if it failed.
625 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
626 * if we get an answer within the timeout, or zero if no answer.
629 int p_pingsend(peer
*p
, ping
*pg
, unsigned type
,
630 unsigned long timeout
,
631 void (*func
)(int, void *), void *arg
)
639 b
= p_txstart(p
, MSG_MISC
| MISC_PING
);
644 pg
->msg
= MISC_EPONG
;
645 b
= p_txstart(p
, MSG_MISC
| MISC_EPING
);
646 buf_init(&bb
, buf_t
, sizeof(buf_t
));
647 p_pingwrite(pg
, &bb
);
649 p_encrypt(p
, MSG_MISC
| MISC_EPING
, &bb
, b
);
664 if (p
->pings
) p
->pings
->prev
= pg
;
666 gettimeofday(&tv
, 0);
667 tv
.tv_sec
+= timeout
;
668 sel_addtimer(&sel
, &pg
->t
, &tv
, p_pingtimeout
, pg
);
669 T( trace(T_PEER
, "peer: send %s 0x%08lx to %s",
670 p_pingtype(type
), (unsigned long)pg
->id
, p
->spec
.name
); )
674 /* --- @p_greet@ --- *
676 * Arguments: @peer *p@ = peer to send to
677 * @const void *c@ = pointer to challenge
678 * @size_t sz@ = size of challenge
682 * Use: Sends a greeting packet.
685 void p_greet(peer
*p
, const void *c
, size_t sz
)
687 buf
*b
= p_txstart(p
, MSG_MISC
| MISC_GREET
);
694 * Arguments: @peer *p@ = pointer to peer block
695 * @buf *b@ = buffer containing incoming packet
699 * Use: Handles a packet which needs to be sent to a peer.
702 void p_tun(peer
*p
, buf
*b
)
704 buf
*bb
= p_txstart(p
, MSG_PACKET
);
707 p_encrypt(p
, MSG_PACKET
, b
, bb
);
708 if (BOK(bb
) && BLEN(bb
)) {
710 p
->st
.sz_ipout
+= BLEN(bb
);
715 /* --- @p_keyreload@ --- *
721 * Use: Forces a check of the daemon's keyring files.
724 void p_keyreload(void)
727 FOREACH_PEER(p
, { kx_newkeys(&p
->kx
); });
730 /* --- @p_interval@ --- *
736 * Use: Called periodically to do tidying.
739 void p_interval(void)
742 FOREACH_PEER(p
, { ksl_prune(&p
->ks
); });
745 /* --- @p_stats@ --- *
747 * Arguments: @peer *p@ = pointer to a peer block
749 * Returns: A pointer to the peer's statistics.
752 stats
*p_stats(peer
*p
) { return (&p
->st
); }
754 /* --- @p_ifname@ --- *
756 * Arguments: @peer *p@ = pointer to a peer block
758 * Returns: A pointer to the peer's interface name.
761 const char *p_ifname(peer
*p
) { return (p
->ifname
); }
763 /* --- @p_setifname@ --- *
765 * Arguments: @peer *p@ = pointer to a peer block
766 * @const char *name@ = pointer to the new name
770 * Use: Changes the name held for a peer's interface.
773 void p_setifname(peer
*p
, const char *name
)
776 p
->ifname
= xstrdup(name
);
777 if (p
->spec
.tops
->setifname
)
778 p
->spec
.tops
->setifname(p
->t
, name
);
781 /* --- @p_addr@ --- *
783 * Arguments: @peer *p@ = pointer to a peer block
785 * Returns: A pointer to the peer's address.
788 const addr
*p_addr(peer
*p
) { return (&p
->spec
.sa
); }
790 /* --- @p_init@ --- *
792 * Arguments: @struct in_addr addr@ = address to bind to
793 * @unsigned port@ = port number to listen to
797 * Use: Initializes the peer system; creates the socket.
800 void p_init(struct in_addr addr
, unsigned port
)
803 struct sockaddr_in sin
;
806 /* --- Note on socket buffer sizes --- *
808 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
809 * sizes I pass to @setsockopt@. I'm not putting special-case code here
810 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
814 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
815 die(EXIT_FAILURE
, "socket creation failed: %s", strerror(errno
));
817 sin
.sin_family
= AF_INET
;
819 sin
.sin_port
= htons(port
);
820 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
821 die(EXIT_FAILURE
, "bind failed: %s", strerror(errno
));
822 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
823 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
))) {
824 die(EXIT_FAILURE
, "failed to set socket buffer sizes: %s",
827 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
828 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, p_read
, 0);
830 T( trace(T_PEER
, "peer: created socket"); )
836 /* --- @p_port@ --- *
840 * Returns: Port number used for socket.
843 unsigned p_port(void)
846 socklen_t sz
= sizeof(addr
);
848 if (getsockname(sock
.fd
, &a
.sa
, &sz
))
849 die(EXIT_FAILURE
, "couldn't read port number: %s", strerror(errno
));
850 assert(a
.sa
.sa_family
== AF_INET
);
851 return (ntohs(a
.sin
.sin_port
));
854 /* --- @p_keepalive@ --- *
856 * Arguments: @struct timeval *now@ = the current time
857 * @void *pv@ = peer to wake up
861 * Use: Sends a keepalive ping message to its peer.
864 static void p_keepalive(struct timeval
*now
, void *pv
)
868 p_txstart(p
, MSG_MISC
| MISC_NOP
); p_dotxend(p
);
869 T( trace(T_PEER
, "peer: sent keepalive to %s", p
->spec
.name
); )
873 /* --- @p_setkatimer@ --- *
875 * Arguments: @peer *p@ = peer to set
879 * Use: Resets the keepalive timer thing.
882 static void p_setkatimer(peer
*p
)
888 gettimeofday(&tv
, 0);
889 tv
.tv_sec
+= p
->spec
.t_ka
;
890 sel_addtimer(&sel
, &p
->tka
, &tv
, p_keepalive
, p
);
893 /* --- @p_create@ --- *
895 * Arguments: @peerspec *spec@ = information about this peer
897 * Returns: Pointer to the peer block, or null if it failed.
899 * Use: Creates a new named peer block. No peer is actually attached
903 peer
*p_create(peerspec
*spec
)
905 peer
*p
= CREATE(peer
);
906 const tunnel_ops
*tops
= spec
->tops
;
910 p
->byname
= sym_find(&byname
, spec
->name
, -1, sizeof(peer_byname
), &f
);
912 p
->byaddr
= am_find(&byaddr
, &spec
->sa
, sizeof(peer_byaddr
), &f
);
914 p
->byname
->p
= p
->byaddr
->p
= p
;
916 T( trace(T_PEER
, "peer: creating new peer `%s'", spec
->name
); )
918 p
->spec
.name
= (/*unconst*/ char *)SYM_NAME(p
->byname
);
919 if (spec
->tag
) p
->spec
.tag
= xstrdup(spec
->tag
);
920 if (spec
->privtag
) p
->spec
.privtag
= xstrdup(spec
->privtag
);
921 if (spec
->knock
) p
->spec
.knock
= xstrdup(spec
->knock
);
925 memset(&p
->st
, 0, sizeof(stats
));
926 p
->st
.t_start
= time(0);
927 if (!(tops
->flags
& TUNF_PRIVOPEN
))
929 else if ((fd
= ps_tunfd(tops
, &p
->ifname
)) < 0)
931 if ((p
->t
= tops
->create(p
, fd
, &p
->ifname
)) == 0)
933 T( trace(T_TUNNEL
, "peer: attached interface %s to peer `%s'",
934 p
->ifname
, p_name(p
)); )
936 if (kx_setup(&p
->kx
, p
, &p
->ks
, p
->spec
.f
& PSF_KXMASK
))
941 "?ADDR", &p
->spec
.sa
,
943 if (!(p
->spec
.f
& KXF_CORK
)) {
944 a_notify("KXSTART", "?PEER", p
, A_END
);
945 /* Couldn't tell anyone before */
947 if (p
->spec
.f
& PSF_MOBILE
) nmobile
++;
951 if (spec
->t_ka
) sel_rmtimer(&p
->tka
);
953 p
->t
->ops
->destroy(p
->t
);
955 if (fd
>= 0) close(fd
);
957 am_remove(&byaddr
, p
->byaddr
);
958 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
959 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
961 sym_remove(&byname
, p
->byname
);
967 /* --- @p_name@ --- *
969 * Arguments: @peer *p@ = pointer to a peer block
971 * Returns: A pointer to the peer's name.
974 const char *p_name(peer
*p
)
975 { if (p
) return (p
->spec
.name
); else return ("-"); }
979 * Arguments: @peer *p@ = pointer to a peer block
981 * Returns: A pointer to the peer's public key tag.
984 const char *p_tag(peer
*p
)
985 { return (p
->spec
.tag ? p
->spec
.tag
: p
->spec
.name
); }
987 /* --- @p_privtag@ --- *
989 * Arguments: @peer *p@ = pointer to a peer block
991 * Returns: A pointer to the peer's private key tag.
994 const char *p_privtag(peer
*p
)
995 { return (p
->spec
.privtag ? p
->spec
.privtag
: tag_priv
); }
997 /* --- @p_spec@ --- *
999 * Arguments: @peer *p@ = pointer to a peer block
1001 * Returns: Pointer to the peer's specification
1004 const peerspec
*p_spec(peer
*p
) { return (&p
->spec
); }
1006 /* --- @p_findbyaddr@ --- *
1008 * Arguments: @const addr *a@ = address to look up
1010 * Returns: Pointer to the peer block, or null if not found.
1012 * Use: Finds a peer by address.
1015 peer
*p_findbyaddr(const addr
*a
)
1019 if ((pa
= am_find(&byaddr
, a
, 0, 0)) != 0) {
1026 /* --- @p_find@ --- *
1028 * Arguments: @const char *name@ = name to look up
1030 * Returns: Pointer to the peer block, or null if not found.
1032 * Use: Finds a peer by name.
1035 peer
*p_find(const char *name
)
1039 if ((pn
= sym_find(&byname
, name
, -1, 0, 0)) != 0)
1044 /* --- @p_destroy@ --- *
1046 * Arguments: @peer *p@ = pointer to a peer
1050 * Use: Destroys a peer.
1053 void p_destroy(peer
*p
)
1057 T( trace(T_PEER
, "peer: destroying peer `%s'", p
->spec
.name
); )
1058 a_notify("KILL", "%s", p
->spec
.name
, A_END
);
1061 if (p
->spec
.f
& PSF_MOBILE
) nmobile
--;
1062 if (p
->ifname
) xfree(p
->ifname
);
1063 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
1064 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
1065 if (p
->spec
.knock
) xfree(p
->spec
.knock
);
1066 p
->t
->ops
->destroy(p
->t
);
1067 if (p
->spec
.t_ka
) sel_rmtimer(&p
->tka
);
1068 for (pg
= p
->pings
; pg
; pg
= ppg
) {
1070 p_pingdone(pg
, PING_PEERDIED
);
1072 sym_remove(&byname
, p
->byname
);
1073 am_remove(&byaddr
, p
->byaddr
);
1077 /* --- @p_mkiter@ --- *
1079 * Arguments: @peer_iter *i@ = pointer to an iterator
1083 * Use: Initializes the iterator.
1086 void p_mkiter(peer_iter
*i
) { sym_mkiter(&i
->i
, &byname
); }
1088 /* --- @p_next@ --- *
1090 * Arguments: @peer_iter *i@ = pointer to an iterator
1092 * Returns: Next peer, or null if at the end.
1094 * Use: Returns the next peer.
1097 peer
*p_next(peer_iter
*i
)
1101 if ((pn
= sym_next(&i
->i
)) == 0)
1106 /*----- That's all, folks -------------------------------------------------*/