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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Static variables --------------------------------------------------*/
33 static sym_table byname
;
34 static addrmap byaddr
;
36 static unsigned nmobile
;
38 /*----- Tunnel table ------------------------------------------------------*/
40 const tunnel_ops
*tunnels
[] = {
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @p_pingtype@ --- *
58 * Arguments: @unsigned msg@ = message type
60 * Returns: String to describe the message.
63 static const char *p_pingtype(unsigned msg
)
65 switch (msg
& MSG_TYPEMASK
) {
68 return "transport-ping";
71 return "encrypted-ping";
77 /* --- @p_ponged@ --- *
79 * Arguments: @peer *p@ = peer packet arrived from
80 * @unsigned msg@ = message type
81 * @buf *b@ = buffer containing payload
85 * Use: Processes a ping response.
88 static void p_ponged(peer
*p
, unsigned msg
, buf
*b
)
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
));
100 if (buf_getu32(b
, &id
) ||
101 (magic
= buf_get(b
, sizeof(pg
->magic
))) == 0 ||
103 a_warn("PEER", "?PEER", p
, "malformed-%s", p_pingtype(msg
), A_END
);
107 for (pg
= p
->pings
; pg
; pg
= pg
->next
) {
113 "unexpected-%s", p_pingtype(msg
),
114 "0x%08lx", (unsigned long)id
,
119 if (memcmp(magic
, pg
->magic
, sizeof(pg
->magic
)) != 0) {
120 a_warn("PEER", "?PEER", p
, "corrupt-%s", p_pingtype(msg
), A_END
);
123 p_pingdone(pg
, PING_OK
);
126 /* --- @p_rxupdstats@ --- *
128 * Arguments: @peer *p@ = peer to update
129 * @size_t n@ = size of incoming packet
133 * Use: Updates the peer's incoming packet statistics.
136 static void p_rxupdstats(peer
*p
, size_t n
)
138 p
->st
.t_last
= time(0);
143 /* --- @p_encrypt@ --- *
145 * Arguments: @peer *p@ = peer to encrypt message to
146 * @int ty@ message type to send
147 * @buf *bin, *bout@ = input and output buffers
151 * Use: Convenience function for packet encryption. Forces
152 * renegotiation when necessary. Check for the output buffer
153 * being broken to find out whether the encryption was
157 static int p_encrypt(peer
*p
, int ty
, buf
*bin
, buf
*bout
)
159 int err
= ksl_encrypt(&p
->ks
, ty
, bin
, bout
);
161 if (err
== KSERR_REGEN
) {
170 /* --- @p_decrypt@ --- *
172 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
173 * @addr *a@ = address the packet arrived on
174 * @size_t n@ = size of original incoming packet
175 * @int ty@ = message type to expect
176 * @buf *bin, *bout@ = input and output buffers
178 * Returns: Zero on success; nonzero on error.
180 * Use: Convenience function for packet decryption. Reports errors
181 * and updates statistics appropriately.
183 * If @*pp@ is null on entry and there are mobile peers then we
184 * see if any of them can decrypt the packet. If so, we record
185 * @*a@ as the peer's new address and send a notification.
188 static int p_decrypt(peer
**pp
, addr
*a
, size_t n
,
189 int ty
, buf
*bin
, buf
*bout
)
192 peer_byaddr
*pa
, *qa
;
193 int err
= KSERR_DECRYPT
;
196 /* --- If we have a match on the source address then try that first --- */
200 T( trace(T_PEER
, "peer: decrypting packet from known peer `%s'",
202 if ((err
= ksl_decrypt(&q
->ks
, ty
, bin
, bout
)) != KSERR_DECRYPT
||
203 !(q
->spec
.f
& PSF_MOBILE
) || nmobile
== 1) {
207 T( trace(T_PEER
, "peer: failed to decrypt: try other mobile peers..."); )
209 T( trace(T_PEER
, "peer: unknown source: trying mobile peers...") );
215 /* --- See whether any mobile peer is interested --- */
219 if (qq
== q
|| !(qq
->spec
.f
& PSF_MOBILE
)) continue;
220 if ((err
= ksl_decrypt(&qq
->ks
, ty
, bin
, bout
)) == KSERR_DECRYPT
) {
221 T( trace(T_PEER
, "peer: peer `%s' failed to decrypt",
228 trace(T_PEER
, "peer: peer `%s' reports success", p_name(qq
));
230 trace(T_PEER
, "peer: peer `%s' reports decryption error %d",
238 /* --- We've searched the mobile peers --- */
243 a_warn("PEER", "-", "unexpected-source", "?ADDR", a
, A_END
);
245 a_warn("PEER", "?PEER", p
, "decrypt-failed",
246 "error-code", "%d", err
, A_END
);
252 /* --- We found one that accepted, so update the peer's address --- *
254 * If we had an initial guess of which peer this packet came from -- i.e.,
255 * @q@ is not null -- then swap the addresses over. This doesn't leave the
256 * evicted peer in an especially good state, but it ought to get sorted out
263 T( trace(T_PEER
, "peer: updating address for `%s'", p_name(p
)); )
264 pa
= am_find(&byaddr
, a
, sizeof(peer_byaddr
), &f
); assert(!f
);
265 am_remove(&byaddr
, p
->byaddr
);
269 a_notify("NEWADDR", "?PEER", p
, "?ADDR", a
, A_END
);
271 T( trace(T_PEER
, "peer: swapping addresses for `%s' and `%s'",
272 p_name(p
), p_name(q
)); )
273 pa
= p
->byaddr
; qa
= q
->byaddr
;
274 pa
->p
= q
; q
->byaddr
= pa
; q
->spec
.sa
= p
->spec
.sa
;
275 qa
->p
= p
; p
->byaddr
= qa
; p
->spec
.sa
= *a
;
276 a_notify("NEWADDR", "?PEER", p
, "?ADDR", a
, A_END
);
277 a_notify("NEWADDR", "?PEER", q
, "?ADDR", &q
->spec
.sa
, A_END
);
284 if (p
) p
->st
.n_reject
++;
285 a_warn("PEER", "?PEER", p
, "decrypt-failed",
286 "error-code", "%d", err
, A_END
);
294 /* --- @p_read@ --- *
296 * Arguments: @int fd@ = file descriptor to read from
297 * @unsigned mode@ = what happened
298 * @void *v@ = an uninteresting pointer
302 * Use: Reads a packet from somewhere.
305 static void p_read(int fd
, unsigned mode
, void *v
)
314 /* --- Read the data --- */
318 n
= recvfrom(fd
, buf_i
, sizeof(buf_i
), 0, &a
.sa
, &sz
);
320 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END
);
324 /* --- If the packet is a greeting, don't check peers --- */
326 if (n
&& buf_i
[0] == (MSG_MISC
| MISC_GREET
)) {
328 trace(T_PEER
, "peer: greeting received from INET %s %u",
329 inet_ntoa(a
.sin
.sin_addr
),
330 (unsigned)ntohs(a
.sin
.sin_port
));
331 trace_block(T_PACKET
, "peer: greeting contents", buf_i
, n
);
333 buf_init(&b
, buf_i
, n
);
335 if (c_check(&b
) || BLEFT(&b
)) {
336 a_warn("PEER", "-", "invalid-greeting", A_END
);
340 "?B64", buf_i
+ 1, (size_t)(n
- 1),
346 /* --- Find the appropriate peer --- *
348 * At this stage, don't worry too much about whether we actually found it.
351 p
= p_findbyaddr(&a
);
356 "peer: packet received from `%s' from address INET %s %d",
357 p_name(p
), inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
359 trace(T_PEER
, "peer: packet received from unknown address INET %s %d",
360 inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
362 trace_block(T_PACKET
, "peer: packet contents", buf_i
, n
);
365 /* --- Pick the packet apart --- */
367 buf_init(&b
, buf_i
, n
);
368 if ((ch
= buf_getbyte(&b
)) < 0) {
369 a_warn("PEER", "?PEER", p
, "bad-packet", "no-type", A_END
);
372 switch (ch
& MSG_CATMASK
) {
374 if (ch
& MSG_TYPEMASK
) {
378 "unknown-type", "0x%02x", ch
,
380 if (p
) p
->st
.n_reject
++;
383 buf_init(&bb
, buf_o
, sizeof(buf_o
));
384 if (p_decrypt(&p
, &a
, n
, MSG_PACKET
, &b
, &bb
))
388 p
->st
.sz_ipin
+= BSZ(&b
);
389 p
->t
->ops
->inject(p
->t
, &bb
);
392 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
398 kx_message(&p
->kx
, ch
& MSG_TYPEMASK
, &b
);
401 switch (ch
& MSG_TYPEMASK
) {
405 T( trace(T_PEER
, "peer: received NOP packet"); )
410 buf_put(p_txstart(p
, MSG_MISC
| MISC_PONG
), BCUR(&b
), BLEFT(&b
));
416 p_ponged(p
, MISC_PONG
, &b
);
419 buf_init(&bb
, buf_t
, sizeof(buf_t
));
420 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
424 p_encrypt(p
, MSG_MISC
| MISC_EPONG
, &bb
,
425 p_txstart(p
, MSG_MISC
| MISC_EPONG
));
430 buf_init(&bb
, buf_t
, sizeof(buf_t
));
431 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
435 p_ponged(p
, MISC_EPONG
, &bb
);
441 if (p
) p
->st
.n_reject
++;
445 "unknown-category", "0x%02x", ch
,
449 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a
, A_END
);
454 /* --- @p_txstart@ --- *
456 * Arguments: @peer *p@ = pointer to peer block
457 * @unsigned msg@ = message type code
459 * Returns: A pointer to a buffer to write to.
461 * Use: Starts sending to a peer. Only one send can happen at a
465 buf
*p_txstart(peer
*p
, unsigned msg
)
467 buf_init(&p
->b
, buf_o
, sizeof(buf_o
));
468 buf_putbyte(&p
->b
, msg
);
472 /* --- @p_txend@ --- *
474 * Arguments: @peer *p@ = pointer to peer block
478 * Use: Sends a packet to the peer.
481 static void p_setkatimer(peer
*);
483 static int p_dotxend(peer
*p
)
486 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
489 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet",
490 BBASE(&p
->b
), BLEN(&p
->b
)); )
491 if (sendto(sock
.fd
, BBASE(&p
->b
), BLEN(&p
->b
),
492 0, &p
->spec
.sa
.sa
, p
->spec
.sasz
) < 0) {
493 a_warn("PEER", "?PEER", p
, "socket-write-error", "?ERRNO", A_END
);
497 p
->st
.sz_out
+= BLEN(&p
->b
);
502 void p_txend(peer
*p
)
504 if (p_dotxend(p
) && p
->spec
.t_ka
) {
505 sel_rmtimer(&p
->tka
);
510 /* --- @p_pingwrite@ --- *
512 * Arguments: @ping *p@ = ping structure
513 * @buf *b@ = buffer to write in
517 * Use: Fills in a ping structure and writes the packet payload.
520 static void p_pingwrite(ping
*p
, buf
*b
)
522 static uint32 seq
= 0;
525 GR_FILL(&rand_global
, p
->magic
, sizeof(p
->magic
));
526 buf_putu32(b
, p
->id
);
527 buf_put(b
, p
->magic
, sizeof(p
->magic
));
530 /* --- @p_pingdone@ --- *
532 * Arguments: @ping *p@ = ping structure
533 * @int rc@ = return code to pass on
537 * Use: Disposes of a ping structure, maybe sending a notification.
540 void p_pingdone(ping
*p
, int rc
)
542 if (p
->prev
) p
->prev
->next
= p
->next
;
543 else p
->p
->pings
= p
->next
;
544 if (p
->next
) p
->next
->prev
= p
->prev
;
545 if (rc
!= PING_TIMEOUT
) sel_rmtimer(&p
->t
);
546 T( trace(T_PEER
, "peer: ping 0x%08lx done (rc = %d)",
547 (unsigned long)p
->id
, rc
); )
548 if (rc
>= 0) p
->func(rc
, p
->arg
);
551 /* --- @p_pingtimeout@ --- *
553 * Arguments: @struct timeval *now@ = the time now
554 * @void *pv@ = pointer to ping block
558 * Use: Called when a ping times out.
561 static void p_pingtimeout(struct timeval
*now
, void *pv
)
565 T( trace(T_PEER
, "peer: ping 0x%08lx timed out", (unsigned long)p
->id
); )
566 p_pingdone(p
, PING_TIMEOUT
);
569 /* --- @p_pingsend@ --- *
571 * Arguments: @peer *p@ = destination peer
572 * @ping *pg@ = structure to fill in
573 * @unsigned type@ = message type
574 * @unsigned long timeout@ = how long to wait before giving up
575 * @void (*func)(int, void *)@ = callback function
576 * @void *arg@ = argument for callback
578 * Returns: Zero if successful, nonzero if it failed.
580 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
581 * if we get an answer within the timeout, or zero if no answer.
584 int p_pingsend(peer
*p
, ping
*pg
, unsigned type
,
585 unsigned long timeout
,
586 void (*func
)(int, void *), void *arg
)
594 b
= p_txstart(p
, MSG_MISC
| MISC_PING
);
599 pg
->msg
= MISC_EPONG
;
600 b
= p_txstart(p
, MSG_MISC
| MISC_EPING
);
601 buf_init(&bb
, buf_t
, sizeof(buf_t
));
602 p_pingwrite(pg
, &bb
);
604 p_encrypt(p
, MSG_MISC
| MISC_EPING
, &bb
, b
);
619 if (p
->pings
) p
->pings
->prev
= pg
;
621 gettimeofday(&tv
, 0);
622 tv
.tv_sec
+= timeout
;
623 sel_addtimer(&sel
, &pg
->t
, &tv
, p_pingtimeout
, pg
);
624 T( trace(T_PEER
, "peer: send %s 0x%08lx to %s",
625 p_pingtype(type
), (unsigned long)pg
->id
, p
->spec
.name
); )
629 /* --- @p_greet@ --- *
631 * Arguments: @peer *p@ = peer to send to
632 * @const void *c@ = pointer to challenge
633 * @size_t sz@ = size of challenge
637 * Use: Sends a greeting packet.
640 void p_greet(peer
*p
, const void *c
, size_t sz
)
642 buf
*b
= p_txstart(p
, MSG_MISC
| MISC_GREET
);
649 * Arguments: @peer *p@ = pointer to peer block
650 * @buf *b@ = buffer containing incoming packet
654 * Use: Handles a packet which needs to be sent to a peer.
657 void p_tun(peer
*p
, buf
*b
)
659 buf
*bb
= p_txstart(p
, MSG_PACKET
);
662 p_encrypt(p
, MSG_PACKET
, b
, bb
);
663 if (BOK(bb
) && BLEN(bb
)) {
665 p
->st
.sz_ipout
+= BLEN(bb
);
670 /* --- @p_keyreload@ --- *
676 * Use: Forces a check of the daemon's keyring files.
679 void p_keyreload(void)
682 FOREACH_PEER(p
, { kx_newkeys(&p
->kx
); });
685 /* --- @p_interval@ --- *
691 * Use: Called periodically to do tidying.
694 void p_interval(void)
697 FOREACH_PEER(p
, { ksl_prune(&p
->ks
); });
700 /* --- @p_stats@ --- *
702 * Arguments: @peer *p@ = pointer to a peer block
704 * Returns: A pointer to the peer's statistics.
707 stats
*p_stats(peer
*p
) { return (&p
->st
); }
709 /* --- @p_ifname@ --- *
711 * Arguments: @peer *p@ = pointer to a peer block
713 * Returns: A pointer to the peer's interface name.
716 const char *p_ifname(peer
*p
) { return (p
->ifname
); }
718 /* --- @p_setifname@ --- *
720 * Arguments: @peer *p@ = pointer to a peer block
721 * @const char *name@ = pointer to the new name
725 * Use: Changes the name held for a peer's interface.
728 void p_setifname(peer
*p
, const char *name
)
731 p
->ifname
= xstrdup(name
);
732 if (p
->spec
.tops
->setifname
)
733 p
->spec
.tops
->setifname(p
->t
, name
);
736 /* --- @p_addr@ --- *
738 * Arguments: @peer *p@ = pointer to a peer block
740 * Returns: A pointer to the peer's address.
743 const addr
*p_addr(peer
*p
) { return (&p
->spec
.sa
); }
745 /* --- @p_init@ --- *
747 * Arguments: @struct in_addr addr@ = address to bind to
748 * @unsigned port@ = port number to listen to
752 * Use: Initializes the peer system; creates the socket.
755 void p_init(struct in_addr addr
, unsigned port
)
758 struct sockaddr_in sin
;
761 /* --- Note on socket buffer sizes --- *
763 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
764 * sizes I pass to @setsockopt@. I'm not putting special-case code here
765 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
769 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
770 die(EXIT_FAILURE
, "socket creation failed: %s", strerror(errno
));
772 sin
.sin_family
= AF_INET
;
774 sin
.sin_port
= htons(port
);
775 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
776 die(EXIT_FAILURE
, "bind failed: %s", strerror(errno
));
777 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
778 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
))) {
779 die(EXIT_FAILURE
, "failed to set socket buffer sizes: %s",
782 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
783 sel_initfile(&sel
, &sock
, fd
, SEL_READ
, p_read
, 0);
785 T( trace(T_PEER
, "peer: created socket"); )
791 /* --- @p_port@ --- *
795 * Returns: Port number used for socket.
798 unsigned p_port(void)
801 size_t sz
= sizeof(addr
);
803 if (getsockname(sock
.fd
, &a
.sa
, &sz
))
804 die(EXIT_FAILURE
, "couldn't read port number: %s", strerror(errno
));
805 assert(a
.sa
.sa_family
== AF_INET
);
806 return (ntohs(a
.sin
.sin_port
));
809 /* --- @p_keepalive@ --- *
811 * Arguments: @struct timeval *now@ = the current time
812 * @void *pv@ = peer to wake up
816 * Use: Sends a keepalive ping message to its peer.
819 static void p_keepalive(struct timeval
*now
, void *pv
)
823 p_txstart(p
, MSG_MISC
| MISC_NOP
); p_dotxend(p
);
824 T( trace(T_PEER
, "peer: sent keepalive to %s", p
->spec
.name
); )
828 /* --- @p_setkatimer@ --- *
830 * Arguments: @peer *p@ = peer to set
834 * Use: Resets the keepalive timer thing.
837 static void p_setkatimer(peer
*p
)
843 gettimeofday(&tv
, 0);
844 tv
.tv_sec
+= p
->spec
.t_ka
;
845 sel_addtimer(&sel
, &p
->tka
, &tv
, p_keepalive
, p
);
848 /* --- @p_create@ --- *
850 * Arguments: @peerspec *spec@ = information about this peer
852 * Returns: Pointer to the peer block, or null if it failed.
854 * Use: Creates a new named peer block. No peer is actually attached
858 peer
*p_create(peerspec
*spec
)
860 peer
*p
= CREATE(peer
);
861 const tunnel_ops
*tops
= spec
->tops
;
865 p
->byname
= sym_find(&byname
, spec
->name
, -1, sizeof(peer_byname
), &f
);
867 p
->byaddr
= am_find(&byaddr
, &spec
->sa
, sizeof(peer_byaddr
), &f
);
869 p
->byname
->p
= p
->byaddr
->p
= p
;
871 T( trace(T_PEER
, "peer: creating new peer `%s'", spec
->name
); )
873 p
->spec
.name
= (/*unconst*/ char *)SYM_NAME(p
->byname
);
874 if (spec
->tag
) p
->spec
.tag
= xstrdup(spec
->tag
);
875 if (spec
->privtag
) p
->spec
.privtag
= xstrdup(spec
->privtag
);
879 memset(&p
->st
, 0, sizeof(stats
));
880 p
->st
.t_start
= time(0);
881 if (!(tops
->flags
& TUNF_PRIVOPEN
))
883 else if ((fd
= ps_tunfd(tops
, &p
->ifname
)) < 0)
885 if ((p
->t
= tops
->create(p
, fd
, &p
->ifname
)) == 0)
887 T( trace(T_TUNNEL
, "peer: attached interface %s to peer `%s'",
888 p
->ifname
, p_name(p
)); )
890 if (kx_init(&p
->kx
, p
, &p
->ks
, p
->spec
.f
& PSF_KXMASK
))
895 "?ADDR", &p
->spec
.sa
,
897 if (!(p
->spec
.f
& KXF_CORK
)) {
898 a_notify("KXSTART", "?PEER", p
, A_END
);
899 /* Couldn't tell anyone before */
901 if (p
->spec
.f
& PSF_MOBILE
) nmobile
++;
905 if (spec
->t_ka
) sel_rmtimer(&p
->tka
);
907 p
->t
->ops
->destroy(p
->t
);
909 if (fd
>= 0) close(fd
);
911 am_remove(&byaddr
, p
->byaddr
);
912 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
913 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
915 sym_remove(&byname
, p
->byname
);
921 /* --- @p_name@ --- *
923 * Arguments: @peer *p@ = pointer to a peer block
925 * Returns: A pointer to the peer's name.
928 const char *p_name(peer
*p
)
929 { if (p
) return (p
->spec
.name
); else return ("-"); }
933 * Arguments: @peer *p@ = pointer to a peer block
935 * Returns: A pointer to the peer's public key tag.
938 const char *p_tag(peer
*p
)
939 { return (p
->spec
.tag ? p
->spec
.tag
: p
->spec
.name
); }
941 /* --- @p_privtag@ --- *
943 * Arguments: @peer *p@ = pointer to a peer block
945 * Returns: A pointer to the peer's private key tag.
948 const char *p_privtag(peer
*p
)
949 { return (p
->spec
.privtag ? p
->spec
.privtag
: tag_priv
); }
951 /* --- @p_spec@ --- *
953 * Arguments: @peer *p@ = pointer to a peer block
955 * Returns: Pointer to the peer's specification
958 const peerspec
*p_spec(peer
*p
) { return (&p
->spec
); }
960 /* --- @p_findbyaddr@ --- *
962 * Arguments: @const addr *a@ = address to look up
964 * Returns: Pointer to the peer block, or null if not found.
966 * Use: Finds a peer by address.
969 peer
*p_findbyaddr(const addr
*a
)
973 if ((pa
= am_find(&byaddr
, a
, 0, 0)) != 0) {
980 /* --- @p_find@ --- *
982 * Arguments: @const char *name@ = name to look up
984 * Returns: Pointer to the peer block, or null if not found.
986 * Use: Finds a peer by name.
989 peer
*p_find(const char *name
)
993 if ((pn
= sym_find(&byname
, name
, -1, 0, 0)) != 0)
998 /* --- @p_destroy@ --- *
1000 * Arguments: @peer *p@ = pointer to a peer
1004 * Use: Destroys a peer.
1007 void p_destroy(peer
*p
)
1011 T( trace(T_PEER
, "peer: destroying peer `%s'", p
->spec
.name
); )
1012 a_notify("KILL", "%s", p
->spec
.name
, A_END
);
1015 if (p
->spec
.f
& PSF_MOBILE
) nmobile
--;
1016 if (p
->ifname
) xfree(p
->ifname
);
1017 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
1018 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
1019 p
->t
->ops
->destroy(p
->t
);
1020 if (p
->spec
.t_ka
) sel_rmtimer(&p
->tka
);
1021 for (pg
= p
->pings
; pg
; pg
= ppg
) {
1023 p_pingdone(pg
, PING_PEERDIED
);
1025 sym_remove(&byname
, p
->byname
);
1026 am_remove(&byaddr
, p
->byaddr
);
1030 /* --- @p_mkiter@ --- *
1032 * Arguments: @peer_iter *i@ = pointer to an iterator
1036 * Use: Initializes the iterator.
1039 void p_mkiter(peer_iter
*i
) { sym_mkiter(&i
->i
, &byname
); }
1041 /* --- @p_next@ --- *
1043 * Arguments: @peer_iter *i@ = pointer to an iterator
1045 * Returns: Next peer, or null if at the end.
1047 * Use: Returns the next peer.
1050 peer
*p_next(peer_iter
*i
)
1054 if ((pn
= sym_next(&i
->i
)) == 0)
1059 /*----- That's all, folks -------------------------------------------------*/