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
)
340 /* --- Read the data --- */
344 n
= recvfrom(fd
, buf_i
, sizeof(buf_i
), 0, &a
.sa
, &sz
);
346 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END
);
350 /* --- If the packet is a greeting, don't check peers --- */
352 if (n
&& buf_i
[0] == (MSG_MISC
| MISC_GREET
)) {
354 trace(T_PEER
, "peer: greeting received from INET %s %u",
355 inet_ntoa(a
.sin
.sin_addr
),
356 (unsigned)ntohs(a
.sin
.sin_port
));
357 trace_block(T_PACKET
, "peer: greeting contents", buf_i
, n
);
359 buf_init(&b
, buf_i
, n
);
361 if (c_check(&b
) || BLEFT(&b
)) {
362 a_warn("PEER", "-", "invalid-greeting", A_END
);
366 "?B64", buf_i
+ 1, (size_t)(n
- 1),
372 /* --- Find the appropriate peer --- *
374 * At this stage, don't worry too much about whether we actually found it.
377 p
= p_findbyaddr(&a
);
382 "peer: packet received from `%s' from address INET %s %d",
383 p_name(p
), inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
385 trace(T_PEER
, "peer: packet received from unknown address INET %s %d",
386 inet_ntoa(a
.sin
.sin_addr
), ntohs(a
.sin
.sin_port
));
388 trace_block(T_PACKET
, "peer: packet contents", buf_i
, n
);
391 /* --- Pick the packet apart --- */
393 buf_init(&b
, buf_i
, n
);
394 if ((ch
= buf_getbyte(&b
)) < 0) {
395 a_warn("PEER", "?PEER", p
, "bad-packet", "no-type", A_END
);
398 switch (ch
& MSG_CATMASK
) {
400 if (ch
& MSG_TYPEMASK
) {
404 "unknown-type", "0x%02x", ch
,
406 if (p
) p
->st
.n_reject
++;
409 buf_init(&bb
, buf_o
, sizeof(buf_o
));
410 if (p_decrypt(&p
, &a
, n
, MSG_PACKET
, &b
, &bb
))
414 p
->st
.sz_ipin
+= BSZ(&b
);
415 p
->t
->ops
->inject(p
->t
, &bb
);
418 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
424 kx_message(&p
->kx
, ch
& MSG_TYPEMASK
, &b
);
427 switch (ch
& MSG_TYPEMASK
) {
431 T( trace(T_PEER
, "peer: received NOP packet"); )
436 buf_put(p_txstart(p
, MSG_MISC
| MISC_PONG
), BCUR(&b
), BLEFT(&b
));
442 p_ponged(p
, MISC_PONG
, &b
);
445 buf_init(&bb
, buf_t
, sizeof(buf_t
));
446 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
450 p_encrypt(p
, MSG_MISC
| MISC_EPONG
, &bb
,
451 p_txstart(p
, MSG_MISC
| MISC_EPONG
));
456 buf_init(&bb
, buf_t
, sizeof(buf_t
));
457 if (p_decrypt(&p
, &a
, n
, ch
, &b
, &bb
))
461 p_ponged(p
, MISC_EPONG
, &bb
);
467 if (p
) p
->st
.n_reject
++;
471 "unknown-category", "0x%02x", ch
,
475 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a
, A_END
);
480 /* --- @p_txstart@ --- *
482 * Arguments: @peer *p@ = pointer to peer block
483 * @unsigned msg@ = message type code
485 * Returns: A pointer to a buffer to write to.
487 * Use: Starts sending to a peer. Only one send can happen at a
491 buf
*p_txstart(peer
*p
, unsigned msg
)
493 buf_init(&p
->b
, buf_o
, sizeof(buf_o
));
494 buf_putbyte(&p
->b
, msg
);
498 /* --- @p_txend@ --- *
500 * Arguments: @peer *p@ = pointer to peer block
504 * Use: Sends a packet to the peer.
507 static void p_setkatimer(peer
*);
509 static int p_dotxend(peer
*p
)
511 socklen_t sasz
= addrsz(&p
->spec
.sa
);
514 a_warn("PEER", "?PEER", p
, "packet-build-failed", A_END
);
517 IF_TRACING(T_PEER
, trace_block(T_PACKET
, "peer: sending packet",
518 BBASE(&p
->b
), BLEN(&p
->b
)); )
519 if (sendto(udpsock
[p
->afix
].fd
, BBASE(&p
->b
), BLEN(&p
->b
),
520 0, &p
->spec
.sa
.sa
, sasz
) < 0) {
521 a_warn("PEER", "?PEER", p
, "socket-write-error", "?ERRNO", A_END
);
525 p
->st
.sz_out
+= BLEN(&p
->b
);
530 void p_txend(peer
*p
)
532 if (p_dotxend(p
) && p
->spec
.t_ka
) {
533 sel_rmtimer(&p
->tka
);
538 /* --- @p_pingwrite@ --- *
540 * Arguments: @ping *p@ = ping structure
541 * @buf *b@ = buffer to write in
545 * Use: Fills in a ping structure and writes the packet payload.
548 static void p_pingwrite(ping
*p
, buf
*b
)
550 static uint32 seq
= 0;
553 GR_FILL(&rand_global
, p
->magic
, sizeof(p
->magic
));
554 buf_putu32(b
, p
->id
);
555 buf_put(b
, p
->magic
, sizeof(p
->magic
));
558 /* --- @p_pingdone@ --- *
560 * Arguments: @ping *p@ = ping structure
561 * @int rc@ = return code to pass on
565 * Use: Disposes of a ping structure, maybe sending a notification.
568 void p_pingdone(ping
*p
, int rc
)
570 if (p
->prev
) p
->prev
->next
= p
->next
;
571 else p
->p
->pings
= p
->next
;
572 if (p
->next
) p
->next
->prev
= p
->prev
;
573 if (rc
!= PING_TIMEOUT
) sel_rmtimer(&p
->t
);
574 T( trace(T_PEER
, "peer: ping 0x%08lx done (rc = %d)",
575 (unsigned long)p
->id
, rc
); )
576 if (rc
>= 0) p
->func(rc
, p
->arg
);
579 /* --- @p_pingtimeout@ --- *
581 * Arguments: @struct timeval *now@ = the time now
582 * @void *pv@ = pointer to ping block
586 * Use: Called when a ping times out.
589 static void p_pingtimeout(struct timeval
*now
, void *pv
)
593 T( trace(T_PEER
, "peer: ping 0x%08lx timed out", (unsigned long)p
->id
); )
594 p_pingdone(p
, PING_TIMEOUT
);
597 /* --- @p_pingsend@ --- *
599 * Arguments: @peer *p@ = destination peer
600 * @ping *pg@ = structure to fill in
601 * @unsigned type@ = message type
602 * @unsigned long timeout@ = how long to wait before giving up
603 * @void (*func)(int, void *)@ = callback function
604 * @void *arg@ = argument for callback
606 * Returns: Zero if successful, nonzero if it failed.
608 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
609 * if we get an answer within the timeout, or zero if no answer.
612 int p_pingsend(peer
*p
, ping
*pg
, unsigned type
,
613 unsigned long timeout
,
614 void (*func
)(int, void *), void *arg
)
622 b
= p_txstart(p
, MSG_MISC
| MISC_PING
);
627 pg
->msg
= MISC_EPONG
;
628 b
= p_txstart(p
, MSG_MISC
| MISC_EPING
);
629 buf_init(&bb
, buf_t
, sizeof(buf_t
));
630 p_pingwrite(pg
, &bb
);
632 p_encrypt(p
, MSG_MISC
| MISC_EPING
, &bb
, b
);
647 if (p
->pings
) p
->pings
->prev
= pg
;
649 gettimeofday(&tv
, 0);
650 tv
.tv_sec
+= timeout
;
651 sel_addtimer(&sel
, &pg
->t
, &tv
, p_pingtimeout
, pg
);
652 T( trace(T_PEER
, "peer: send %s 0x%08lx to %s",
653 p_pingtype(type
), (unsigned long)pg
->id
, p
->spec
.name
); )
657 /* --- @p_greet@ --- *
659 * Arguments: @peer *p@ = peer to send to
660 * @const void *c@ = pointer to challenge
661 * @size_t sz@ = size of challenge
665 * Use: Sends a greeting packet.
668 void p_greet(peer
*p
, const void *c
, size_t sz
)
670 buf
*b
= p_txstart(p
, MSG_MISC
| MISC_GREET
);
677 * Arguments: @peer *p@ = pointer to peer block
678 * @buf *b@ = buffer containing incoming packet
682 * Use: Handles a packet which needs to be sent to a peer.
685 void p_tun(peer
*p
, buf
*b
)
687 buf
*bb
= p_txstart(p
, MSG_PACKET
);
690 p_encrypt(p
, MSG_PACKET
, b
, bb
);
691 if (BOK(bb
) && BLEN(bb
)) {
693 p
->st
.sz_ipout
+= BLEN(bb
);
698 /* --- @p_keyreload@ --- *
704 * Use: Forces a check of the daemon's keyring files.
707 void p_keyreload(void)
710 FOREACH_PEER(p
, { kx_newkeys(&p
->kx
); });
713 /* --- @p_interval@ --- *
719 * Use: Called periodically to do tidying.
722 void p_interval(void)
725 FOREACH_PEER(p
, { ksl_prune(&p
->ks
); });
728 /* --- @p_stats@ --- *
730 * Arguments: @peer *p@ = pointer to a peer block
732 * Returns: A pointer to the peer's statistics.
735 stats
*p_stats(peer
*p
) { return (&p
->st
); }
737 /* --- @p_ifname@ --- *
739 * Arguments: @peer *p@ = pointer to a peer block
741 * Returns: A pointer to the peer's interface name.
744 const char *p_ifname(peer
*p
) { return (p
->ifname
); }
746 /* --- @p_setifname@ --- *
748 * Arguments: @peer *p@ = pointer to a peer block
749 * @const char *name@ = pointer to the new name
753 * Use: Changes the name held for a peer's interface.
756 void p_setifname(peer
*p
, const char *name
)
759 p
->ifname
= xstrdup(name
);
760 if (p
->spec
.tops
->setifname
)
761 p
->spec
.tops
->setifname(p
->t
, name
);
764 /* --- @p_addr@ --- *
766 * Arguments: @peer *p@ = pointer to a peer block
768 * Returns: A pointer to the peer's address.
771 const addr
*p_addr(peer
*p
) { return (&p
->spec
.sa
); }
773 /* --- @p_init@ --- *
775 * Arguments: @struct in_addr addr@ = address to bind to
776 * @unsigned port@ = port number to listen to
780 * Use: Initializes the peer system; creates the socket.
783 void p_init(struct in_addr addr
, unsigned port
)
786 struct sockaddr_in sin
;
789 /* --- Note on socket buffer sizes --- *
791 * For some bizarre reason, Linux 2.2 (at least) doubles the socket buffer
792 * sizes I pass to @setsockopt@. I'm not putting special-case code here
793 * for Linux: BSD (at least TCPv2) does what I tell it rather than second-
797 if ((fd
= socket(PF_INET
, SOCK_DGRAM
, 0)) < 0)
798 die(EXIT_FAILURE
, "socket creation failed: %s", strerror(errno
));
800 sin
.sin_family
= AF_INET
;
802 sin
.sin_port
= htons(port
);
803 if (bind(fd
, (struct sockaddr
*)&sin
, sizeof(sin
)))
804 die(EXIT_FAILURE
, "bind failed: %s", strerror(errno
));
805 if (setsockopt(fd
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
806 setsockopt(fd
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
))) {
807 die(EXIT_FAILURE
, "failed to set socket buffer sizes: %s",
810 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
811 sel_initfile(&sel
, &udpsock
[AFIX_INET
], fd
, SEL_READ
, p_read
, 0);
812 sel_addfile(&udpsock
[AFIX_INET
]);
813 T( trace(T_PEER
, "peer: created socket"); )
819 /* --- @p_port@ --- *
821 * Arguments: @int i@ = address family index to retrieve
823 * Returns: Port number used for socket.
826 unsigned p_port(int i
)
829 socklen_t sz
= sizeof(addr
);
831 if (getsockname(udpsock
[i
].fd
, &a
.sa
, &sz
))
832 die(EXIT_FAILURE
, "couldn't read port number: %s", strerror(errno
));
833 assert(a
.sa
.sa_family
== AF_INET
);
834 return (ntohs(a
.sin
.sin_port
));
837 /* --- @p_keepalive@ --- *
839 * Arguments: @struct timeval *now@ = the current time
840 * @void *pv@ = peer to wake up
844 * Use: Sends a keepalive ping message to its peer.
847 static void p_keepalive(struct timeval
*now
, void *pv
)
851 p_txstart(p
, MSG_MISC
| MISC_NOP
); p_dotxend(p
);
852 T( trace(T_PEER
, "peer: sent keepalive to %s", p
->spec
.name
); )
856 /* --- @p_setkatimer@ --- *
858 * Arguments: @peer *p@ = peer to set
862 * Use: Resets the keepalive timer thing.
865 static void p_setkatimer(peer
*p
)
871 gettimeofday(&tv
, 0);
872 tv
.tv_sec
+= p
->spec
.t_ka
;
873 sel_addtimer(&sel
, &p
->tka
, &tv
, p_keepalive
, p
);
876 /* --- @p_create@ --- *
878 * Arguments: @peerspec *spec@ = information about this peer
880 * Returns: Pointer to the peer block, or null if it failed.
882 * Use: Creates a new named peer block. No peer is actually attached
886 peer
*p_create(peerspec
*spec
)
888 peer
*p
= CREATE(peer
);
889 const tunnel_ops
*tops
= spec
->tops
;
893 p
->byname
= sym_find(&byname
, spec
->name
, -1, sizeof(peer_byname
), &f
);
895 p
->byaddr
= am_find(&byaddr
, &spec
->sa
, sizeof(peer_byaddr
), &f
);
897 p
->byname
->p
= p
->byaddr
->p
= p
;
899 T( trace(T_PEER
, "peer: creating new peer `%s'", spec
->name
); )
901 p
->spec
.name
= (/*unconst*/ char *)SYM_NAME(p
->byname
);
902 if (spec
->tag
) p
->spec
.tag
= xstrdup(spec
->tag
);
903 if (spec
->privtag
) p
->spec
.privtag
= xstrdup(spec
->privtag
);
907 p
->afix
= afix(p
->spec
.sa
.sa
.sa_family
); assert(p
->afix
>= 0);
908 memset(&p
->st
, 0, sizeof(stats
));
909 p
->st
.t_start
= time(0);
910 if (!(tops
->flags
& TUNF_PRIVOPEN
))
912 else if ((fd
= ps_tunfd(tops
, &p
->ifname
)) < 0)
914 if ((p
->t
= tops
->create(p
, fd
, &p
->ifname
)) == 0)
916 T( trace(T_TUNNEL
, "peer: attached interface %s to peer `%s'",
917 p
->ifname
, p_name(p
)); )
919 if (kx_init(&p
->kx
, p
, &p
->ks
, p
->spec
.f
& PSF_KXMASK
))
924 "?ADDR", &p
->spec
.sa
,
926 if (!(p
->spec
.f
& KXF_CORK
)) {
927 a_notify("KXSTART", "?PEER", p
, A_END
);
928 /* Couldn't tell anyone before */
930 if (p
->spec
.f
& PSF_MOBILE
) nmobile
++;
934 if (spec
->t_ka
) sel_rmtimer(&p
->tka
);
936 p
->t
->ops
->destroy(p
->t
);
938 if (fd
>= 0) close(fd
);
940 am_remove(&byaddr
, p
->byaddr
);
941 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
942 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
944 sym_remove(&byname
, p
->byname
);
950 /* --- @p_name@ --- *
952 * Arguments: @peer *p@ = pointer to a peer block
954 * Returns: A pointer to the peer's name.
957 const char *p_name(peer
*p
)
958 { if (p
) return (p
->spec
.name
); else return ("-"); }
962 * Arguments: @peer *p@ = pointer to a peer block
964 * Returns: A pointer to the peer's public key tag.
967 const char *p_tag(peer
*p
)
968 { return (p
->spec
.tag ? p
->spec
.tag
: p
->spec
.name
); }
970 /* --- @p_privtag@ --- *
972 * Arguments: @peer *p@ = pointer to a peer block
974 * Returns: A pointer to the peer's private key tag.
977 const char *p_privtag(peer
*p
)
978 { return (p
->spec
.privtag ? p
->spec
.privtag
: tag_priv
); }
980 /* --- @p_spec@ --- *
982 * Arguments: @peer *p@ = pointer to a peer block
984 * Returns: Pointer to the peer's specification
987 const peerspec
*p_spec(peer
*p
) { return (&p
->spec
); }
989 /* --- @p_findbyaddr@ --- *
991 * Arguments: @const addr *a@ = address to look up
993 * Returns: Pointer to the peer block, or null if not found.
995 * Use: Finds a peer by address.
998 peer
*p_findbyaddr(const addr
*a
)
1002 if ((pa
= am_find(&byaddr
, a
, 0, 0)) != 0) {
1009 /* --- @p_find@ --- *
1011 * Arguments: @const char *name@ = name to look up
1013 * Returns: Pointer to the peer block, or null if not found.
1015 * Use: Finds a peer by name.
1018 peer
*p_find(const char *name
)
1022 if ((pn
= sym_find(&byname
, name
, -1, 0, 0)) != 0)
1027 /* --- @p_destroy@ --- *
1029 * Arguments: @peer *p@ = pointer to a peer
1033 * Use: Destroys a peer.
1036 void p_destroy(peer
*p
)
1040 T( trace(T_PEER
, "peer: destroying peer `%s'", p
->spec
.name
); )
1041 a_notify("KILL", "%s", p
->spec
.name
, A_END
);
1044 if (p
->spec
.f
& PSF_MOBILE
) nmobile
--;
1045 if (p
->ifname
) xfree(p
->ifname
);
1046 if (p
->spec
.tag
) xfree(p
->spec
.tag
);
1047 if (p
->spec
.privtag
) xfree(p
->spec
.privtag
);
1048 p
->t
->ops
->destroy(p
->t
);
1049 if (p
->spec
.t_ka
) sel_rmtimer(&p
->tka
);
1050 for (pg
= p
->pings
; pg
; pg
= ppg
) {
1052 p_pingdone(pg
, PING_PEERDIED
);
1054 sym_remove(&byname
, p
->byname
);
1055 am_remove(&byaddr
, p
->byaddr
);
1059 /* --- @p_mkiter@ --- *
1061 * Arguments: @peer_iter *i@ = pointer to an iterator
1065 * Use: Initializes the iterator.
1068 void p_mkiter(peer_iter
*i
) { sym_mkiter(&i
->i
, &byname
); }
1070 /* --- @p_next@ --- *
1072 * Arguments: @peer_iter *i@ = pointer to an iterator
1074 * Returns: Next peer, or null if at the end.
1076 * Use: Returns the next peer.
1079 peer
*p_next(peer_iter
*i
)
1083 if ((pn
= sym_next(&i
->i
)) == 0)
1088 /*----- That's all, folks -------------------------------------------------*/