chiark / gitweb /
server/peer.c: Always send `BYE' packets on peer death.
[tripe] / server / peer.c
CommitLineData
410c8acf 1/* -*-c-*-
2 *
410c8acf 3 * Communication with the peer
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
11ad66c2
MW
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.
e04c2d50 16 *
11ad66c2
MW
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
20 * for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
11ad66c2 23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
410c8acf 24 */
25
410c8acf 26/*----- Header files ------------------------------------------------------*/
27
28#include "tripe.h"
29
5d06f63e
MW
30/*----- Global state ------------------------------------------------------*/
31
063c9648 32udpsocket udpsock[NADDRFAM];
5d06f63e 33
410c8acf 34/*----- Static variables --------------------------------------------------*/
35
c8e02c8a
MW
36static sym_table byname;
37static addrmap byaddr;
6411163d 38static unsigned nmobile;
24898e7e
MW
39static struct tunnel_node {
40 struct tunnel_node *next;
41 const tunnel_ops *tops;
42} *tunnels, **tunnels_tail = &tunnels;
43const tunnel_ops *dflttun;
42da2a58 44
410c8acf 45/*----- Main code ---------------------------------------------------------*/
46
0ba8de86 47/* --- @p_pingtype@ --- *
48 *
49 * Arguments: @unsigned msg@ = message type
50 *
51 * Returns: String to describe the message.
52 */
53
54static const char *p_pingtype(unsigned msg)
55{
56 switch (msg & MSG_TYPEMASK) {
57 case MISC_PING:
58 case MISC_PONG:
59 return "transport-ping";
60 case MISC_EPING:
61 case MISC_EPONG:
62 return "encrypted-ping";
63 default:
64 abort();
65 }
66}
67
68/* --- @p_ponged@ --- *
69 *
70 * Arguments: @peer *p@ = peer packet arrived from
71 * @unsigned msg@ = message type
72 * @buf *b@ = buffer containing payload
73 *
74 * Returns: ---
75 *
76 * Use: Processes a ping response.
77 */
78
79static void p_ponged(peer *p, unsigned msg, buf *b)
80{
81 uint32 id;
82 const octet *magic;
83 ping *pg;
e04c2d50 84
0ba8de86 85 IF_TRACING(T_PEER, {
86 trace(T_PEER, "peer: received %s reply from %s",
87 p_pingtype(msg), p->spec.name);
88 trace_block(T_PACKET, "peer: ping contents", BBASE(b), BSZ(b));
89 })
90
91 if (buf_getu32(b, &id) ||
92 (magic = buf_get(b, sizeof(pg->magic))) == 0 ||
93 BLEFT(b)) {
f43df819 94 a_warn("PEER", "?PEER", p, "malformed-%s", p_pingtype(msg), A_END);
0ba8de86 95 return;
96 }
97
98 for (pg = p->pings; pg; pg = pg->next) {
99 if (pg->id == id)
100 goto found;
101 }
f43df819
MW
102 a_warn("PEER",
103 "?PEER", p,
104 "unexpected-%s", p_pingtype(msg),
105 "0x%08lx", (unsigned long)id,
106 A_END);
0ba8de86 107 return;
108
109found:
110 if (memcmp(magic, pg->magic, sizeof(pg->magic)) != 0) {
f43df819 111 a_warn("PEER", "?PEER", p, "corrupt-%s", p_pingtype(msg), A_END);
0ba8de86 112 return;
113 }
114 p_pingdone(pg, PING_OK);
115}
116
6411163d
MW
117/* --- @p_rxupdstats@ --- *
118 *
119 * Arguments: @peer *p@ = peer to update
120 * @size_t n@ = size of incoming packet
121 *
122 * Returns: ---
123 *
124 * Use: Updates the peer's incoming packet statistics.
125 */
126
127static void p_rxupdstats(peer *p, size_t n)
128{
129 p->st.t_last = time(0);
130 p->st.n_in++;
131 p->st.sz_in += n;
132}
133
a50f9a0e
MW
134/* --- @p_encrypt@ --- *
135 *
136 * Arguments: @peer *p@ = peer to encrypt message to
137 * @int ty@ message type to send
138 * @buf *bin, *bout@ = input and output buffers
139 *
140 * Returns: ---
141 *
142 * Use: Convenience function for packet encryption. Forces
143 * renegotiation when necessary. Check for the output buffer
144 * being broken to find out whether the encryption was
145 * successful.
146 */
147
148static int p_encrypt(peer *p, int ty, buf *bin, buf *bout)
149{
150 int err = ksl_encrypt(&p->ks, ty, bin, bout);
151
152 if (err == KSERR_REGEN) {
153 kx_start(&p->kx, 1);
154 err = 0;
155 }
156 if (!BOK(bout))
157 err = -1;
158 return (err);
159}
160
23bbe601
MW
161/* --- @p_updateaddr@ --- *
162 *
163 * Arguments: @peer *p@ = pointer to peer block
164 * @const addr *a@ = address to associate with this peer
165 *
166 * Returns: Zero if the address was changed; @+1@ if it was already
167 * right.
168 *
169 * Use: Updates our idea of @p@'s address.
170 */
171
172int p_updateaddr(peer *p, const addr *a)
173{
174 peer *q;
175 peer_byaddr *pa, *qa;
5d06f63e 176 int ix;
23bbe601
MW
177 unsigned f;
178
179 /* --- Figure out how to proceed --- *
180 *
181 * If this address already belongs to a different peer, then swap the
182 * addresses over. This doesn't leave the displaced peer in an especially
183 * good state, but it ought to get sorted out soon enough.
184 */
185
186 pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f);
187 if (f && pa->p == p)
188 return (+1);
189 else if (!f) {
190 T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); )
191 am_remove(&byaddr, p->byaddr);
192 p->byaddr = pa; p->spec.sa = *a; pa->p = p;
5d06f63e 193 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
23bbe601
MW
194 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
195 return (0);
196 } else {
197 q = pa->p; qa = p->byaddr;
198 T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'",
199 p_name(p), p_name(q)); )
200 q->byaddr = qa; qa->p = q; q->spec.sa = p->spec.sa;
201 p->byaddr = pa; pa->p = p; p->spec.sa = *a;
5d06f63e 202 ix = p->afix; p->afix = q->afix; q->afix = ix;
23bbe601
MW
203 a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
204 a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END);
205 return (0);
206 }
207}
208
a50f9a0e
MW
209/* --- @p_decrypt@ --- *
210 *
6411163d
MW
211 * Arguments: @peer **pp@ = pointer to peer to decrypt message from
212 * @addr *a@ = address the packet arrived on
213 * @size_t n@ = size of original incoming packet
a50f9a0e
MW
214 * @int ty@ = message type to expect
215 * @buf *bin, *bout@ = input and output buffers
216 *
217 * Returns: Zero on success; nonzero on error.
218 *
219 * Use: Convenience function for packet decryption. Reports errors
220 * and updates statistics appropriately.
6411163d
MW
221 *
222 * If @*pp@ is null on entry and there are mobile peers then we
223 * see if any of them can decrypt the packet. If so, we record
224 * @*a@ as the peer's new address and send a notification.
a50f9a0e
MW
225 */
226
6411163d
MW
227static int p_decrypt(peer **pp, addr *a, size_t n,
228 int ty, buf *bin, buf *bout)
a50f9a0e 229{
c71be758 230 peer *p, *q;
6411163d 231 int err = KSERR_DECRYPT;
6411163d 232
c71be758
MW
233 /* --- If we have a match on the source address then try that first --- */
234
235 q = *pp;
236 if (q) {
6411163d 237 T( trace(T_PEER, "peer: decrypting packet from known peer `%s'",
c71be758
MW
238 p_name(q)); )
239 if ((err = ksl_decrypt(&q->ks, ty, bin, bout)) != KSERR_DECRYPT ||
240 !(q->spec.f & PSF_MOBILE) || nmobile == 1) {
241 p = q;
242 goto match;
243 }
73174919 244 T( trace(T_PEER, "peer: failed to decrypt: try other mobile peers..."); )
c71be758 245 } else if (nmobile)
8ed35e02 246 T( trace(T_PEER, "peer: unknown source: trying mobile peers...") );
c71be758 247 else {
6411163d 248 p = 0;
c71be758
MW
249 goto searched;
250 }
251
252 /* --- See whether any mobile peer is interested --- */
253
01c21903
MW
254 p = 0;
255 FOREACH_PEER(qq, {
256 if (qq == q || !(qq->spec.f & PSF_MOBILE)) continue;
257 if ((err = ksl_decrypt(&qq->ks, ty, bin, bout)) == KSERR_DECRYPT) {
c71be758 258 T( trace(T_PEER, "peer: peer `%s' failed to decrypt",
01c21903 259 p_name(qq)); )
c71be758
MW
260 continue;
261 } else {
01c21903 262 p = qq;
c71be758
MW
263 IF_TRACING(T_PEER, {
264 if (!err)
01c21903 265 trace(T_PEER, "peer: peer `%s' reports success", p_name(qq));
c71be758
MW
266 else {
267 trace(T_PEER, "peer: peer `%s' reports decryption error %d",
01c21903 268 p_name(qq), err);
6411163d 269 }
c71be758
MW
270 })
271 break;
6411163d 272 }
c71be758
MW
273 });
274
275 /* --- We've searched the mobile peers --- */
276
277searched:
278 if (!p) {
01c21903
MW
279 if (!q)
280 a_warn("PEER", "-", "unexpected-source", "?ADDR", a, A_END);
281 else {
282 a_warn("PEER", "?PEER", p, "decrypt-failed",
283 "error-code", "%d", err, A_END);
284 p_rxupdstats(q, n);
285 }
c71be758
MW
286 return (-1);
287 }
288
23bbe601 289 /* --- We found one that accepted, so update the peer's address --- */
c71be758
MW
290
291 if (!err) {
01c21903 292 *pp = p;
23bbe601 293 p_updateaddr(p, a);
6411163d 294 }
c71be758
MW
295
296match:
88622d1d 297 p_rxupdstats(p, n);
6411163d
MW
298 if (err) {
299 if (p) p->st.n_reject++;
300 a_warn("PEER", "?PEER", p, "decrypt-failed",
301 "error-code", "%d", err, A_END);
a50f9a0e
MW
302 return (-1);
303 }
304 if (!BOK(bout))
305 return (-1);
306 return (0);
307}
308
410c8acf 309/* --- @p_read@ --- *
310 *
311 * Arguments: @int fd@ = file descriptor to read from
312 * @unsigned mode@ = what happened
313 * @void *v@ = an uninteresting pointer
314 *
315 * Returns: ---
316 *
317 * Use: Reads a packet from somewhere.
318 */
319
320static void p_read(int fd, unsigned mode, void *v)
321{
37941236 322 peer *p = 0;
410c8acf 323 addr a;
cb160b86 324 socklen_t sz;
410c8acf 325 ssize_t n;
326 int ch;
327 buf b, bb;
89640f3f
MW
328#ifndef NTRACE
329 int ix = -1;
330 char name[NI_MAXHOST], svc[NI_MAXSERV];
331#endif
410c8acf 332
333 /* --- Read the data --- */
334
f4c9c08f 335 QUICKRAND;
410c8acf 336 sz = sizeof(addr);
337 n = recvfrom(fd, buf_i, sizeof(buf_i), 0, &a.sa, &sz);
338 if (n < 0) {
f43df819 339 a_warn("PEER", "-", "socket-read-error", "?ERRNO", A_END);
410c8acf 340 return;
341 }
89640f3f
MW
342 IF_TRACING(T_PEER, {
343 ix = afix(a.sa.sa_family);
344 getnameinfo(&a.sa, sz, name, sizeof(name), svc, sizeof(svc),
345 NI_NUMERICHOST | NI_NUMERICSERV);
346 })
410c8acf 347
37941236 348 /* --- If the packet is a greeting, don't check peers --- */
349
350 if (n && buf_i[0] == (MSG_MISC | MISC_GREET)) {
351 IF_TRACING(T_PEER, {
89640f3f
MW
352 trace(T_PEER, "peer: greeting received from %s %s %s",
353 aftab[ix].name, name, svc);
37941236 354 trace_block(T_PACKET, "peer: greeting contents", buf_i, n);
355 })
356 buf_init(&b, buf_i, n);
357 buf_getbyte(&b);
3deadf73 358 if (c_check(0, 0, &b) || BLEFT(&b)) {
f43df819 359 a_warn("PEER", "-", "invalid-greeting", A_END);
37941236 360 return;
361 }
f43df819
MW
362 a_notify("GREET",
363 "?B64", buf_i + 1, (size_t)(n - 1),
364 "?ADDR", &a,
365 A_END);
37941236 366 return;
367 }
368
6411163d
MW
369 /* --- Find the appropriate peer --- *
370 *
371 * At this stage, don't worry too much about whether we actually found it.
372 */
410c8acf 373
6411163d 374 p = p_findbyaddr(&a);
410c8acf 375
b9066fbb 376 IF_TRACING(T_PEER, {
6411163d
MW
377 if (p) {
378 trace(T_PEER,
89640f3f
MW
379 "peer: packet received from `%s' from address %s %s %s",
380 p_name(p), aftab[ix].name, name, svc);
6411163d 381 } else {
89640f3f
MW
382 trace(T_PEER, "peer: packet received from unknown address %s %s %s",
383 aftab[ix].name, name, svc);
6411163d 384 }
b9066fbb 385 trace_block(T_PACKET, "peer: packet contents", buf_i, n);
386 })
410c8acf 387
388 /* --- Pick the packet apart --- */
389
390 buf_init(&b, buf_i, n);
391 if ((ch = buf_getbyte(&b)) < 0) {
f43df819 392 a_warn("PEER", "?PEER", p, "bad-packet", "no-type", A_END);
410c8acf 393 return;
394 }
5bb41301 395 switch (ch & MSG_CATMASK) {
410c8acf 396 case MSG_PACKET:
5bb41301 397 if (ch & MSG_TYPEMASK) {
f43df819
MW
398 a_warn("PEER",
399 "?PEER", p,
400 "bad-packet",
401 "unknown-type", "0x%02x", ch,
402 A_END);
6411163d 403 if (p) p->st.n_reject++;
5bb41301 404 return;
405 }
410c8acf 406 buf_init(&bb, buf_o, sizeof(buf_o));
6411163d 407 if (p_decrypt(&p, &a, n, MSG_PACKET, &b, &bb))
410c8acf 408 return;
5bb41301 409 if (BOK(&bb)) {
410 p->st.n_ipin++;
411 p->st.sz_ipin += BSZ(&b);
42da2a58 412 p->t->ops->inject(p->t, &bb);
5bb41301 413 } else {
414 p->st.n_reject++;
f43df819 415 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
5bb41301 416 }
410c8acf 417 break;
5bb41301 418 case MSG_KEYEXCH:
f6994bd0
MW
419 if (p) p_rxupdstats(p, n);
420 if (kx_message(p ? &p->kx : 0, &a, ch & MSG_TYPEMASK, &b)) goto unexp;
410c8acf 421 break;
0ba8de86 422 case MSG_MISC:
423 switch (ch & MSG_TYPEMASK) {
424 case MISC_NOP:
6411163d 425 if (!p) goto unexp;
88622d1d 426 p_rxupdstats(p, n);
0ba8de86 427 T( trace(T_PEER, "peer: received NOP packet"); )
428 break;
429 case MISC_PING:
6411163d 430 if (!p) goto unexp;
88622d1d 431 p_rxupdstats(p, n);
0ba8de86 432 buf_put(p_txstart(p, MSG_MISC | MISC_PONG), BCUR(&b), BLEFT(&b));
433 p_txend(p);
e04c2d50 434 break;
0ba8de86 435 case MISC_PONG:
6411163d 436 if (!p) goto unexp;
88622d1d 437 p_rxupdstats(p, n);
0ba8de86 438 p_ponged(p, MISC_PONG, &b);
439 break;
440 case MISC_EPING:
441 buf_init(&bb, buf_t, sizeof(buf_t));
6411163d 442 if (p_decrypt(&p, &a, n, ch, &b, &bb))
0ba8de86 443 return;
0ba8de86 444 if (BOK(&bb)) {
445 buf_flip(&bb);
a50f9a0e
MW
446 p_encrypt(p, MSG_MISC | MISC_EPONG, &bb,
447 p_txstart(p, MSG_MISC | MISC_EPONG));
0ba8de86 448 p_txend(p);
449 }
450 break;
451 case MISC_EPONG:
452 buf_init(&bb, buf_t, sizeof(buf_t));
6411163d 453 if (p_decrypt(&p, &a, n, ch, &b, &bb))
0ba8de86 454 return;
0ba8de86 455 if (BOK(&bb)) {
456 buf_flip(&bb);
457 p_ponged(p, MISC_EPONG, &bb);
458 }
459 break;
067aa5f0
MW
460 case MISC_BYE:
461 buf_init(&bb, buf_t, sizeof(buf_t));
462 if (p_decrypt(&p, &a, n, ch, &b, &bb)) return;
463 if (!(p->spec.f&PSF_EPHEM)) return;
464 if (BOK(&bb)) {
465 buf_flip(&bb);
466 if (BSZ(&bb)) return;
467 p_destroy(p, 0);
468 }
469 break;
0ba8de86 470 }
471 break;
410c8acf 472 default:
6411163d 473 if (p) p->st.n_reject++;
f43df819
MW
474 a_warn("PEER",
475 "?PEER", p,
476 "bad-packet",
a68280fc 477 "unknown-category", "0x%02x", ch,
f43df819 478 A_END);
410c8acf 479 break;
6411163d
MW
480 unexp:
481 a_warn("PEER", "-", "unexpected-source", "?ADDR", &a, A_END);
482 break;
410c8acf 483 }
484}
485
486/* --- @p_txstart@ --- *
487 *
488 * Arguments: @peer *p@ = pointer to peer block
489 * @unsigned msg@ = message type code
490 *
491 * Returns: A pointer to a buffer to write to.
492 *
493 * Use: Starts sending to a peer. Only one send can happen at a
494 * time.
495 */
496
497buf *p_txstart(peer *p, unsigned msg)
498{
499 buf_init(&p->b, buf_o, sizeof(buf_o));
500 buf_putbyte(&p->b, msg);
501 return (&p->b);
502}
503
8362ac1c
MW
504/* --- @p_txaddr@ --- *
505 *
506 * Arguments: @const addr *a@ = recipient address
507 * @const void *p@ = pointer to packet to send
508 * @size_t sz@ = length of packet
509 *
510 * Returns: Zero if successful, nonzero on error.
511 *
512 * Use: Sends a packet to an address which (possibly) isn't a current
513 * peer.
514 */
515
516int p_txaddr(const addr *a, const void *p, size_t sz)
517{
518 socklen_t sasz = addrsz(a);
56c76774 519 int i;
8362ac1c 520
56c76774
MW
521 if ((i = afix(a->sa.sa_family)) < 0) {
522 a_warn("PEER", "?ADDR", a, "disabled-address-family", A_END);
523 return (-1);
524 }
8362ac1c 525 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet", p, sz); )
063c9648 526 if (sendto(udpsock[i].sf.fd, p, sz, 0, &a->sa, sasz) < 0) {
8362ac1c
MW
527 a_warn("PEER", "?ADDR", a, "socket-write-error", "?ERRNO", A_END);
528 return (-1);
529 }
530 return (0);
531}
532
410c8acf 533/* --- @p_txend@ --- *
534 *
535 * Arguments: @peer *p@ = pointer to peer block
536 *
537 * Returns: ---
538 *
539 * Use: Sends a packet to the peer.
540 */
541
0ba8de86 542static void p_setkatimer(peer *);
543
544static int p_dotxend(peer *p)
410c8acf 545{
cb2c2bfc
MW
546 socklen_t sasz = addrsz(&p->spec.sa);
547
410c8acf 548 if (!BOK(&p->b)) {
f43df819 549 a_warn("PEER", "?PEER", p, "packet-build-failed", A_END);
0ba8de86 550 return (0);
410c8acf 551 }
552 IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
553 BBASE(&p->b), BLEN(&p->b)); )
063c9648 554 if (sendto(udpsock[p->afix].sf.fd, BBASE(&p->b), BLEN(&p->b),
cb2c2bfc 555 0, &p->spec.sa.sa, sasz) < 0) {
f43df819 556 a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
0ba8de86 557 return (0);
558 } else {
5bb41301 559 p->st.n_out++;
560 p->st.sz_out += BLEN(&p->b);
0ba8de86 561 return (1);
5bb41301 562 }
410c8acf 563}
564
0ba8de86 565void p_txend(peer *p)
566{
567 if (p_dotxend(p) && p->spec.t_ka) {
568 sel_rmtimer(&p->tka);
569 p_setkatimer(p);
570 }
571}
572
573/* --- @p_pingwrite@ --- *
574 *
575 * Arguments: @ping *p@ = ping structure
576 * @buf *b@ = buffer to write in
577 *
578 * Returns: ---
579 *
580 * Use: Fills in a ping structure and writes the packet payload.
581 */
582
583static void p_pingwrite(ping *p, buf *b)
584{
585 static uint32 seq = 0;
586
587 p->id = U32(seq++);
588 GR_FILL(&rand_global, p->magic, sizeof(p->magic));
589 buf_putu32(b, p->id);
590 buf_put(b, p->magic, sizeof(p->magic));
591}
592
593/* --- @p_pingdone@ --- *
594 *
595 * Arguments: @ping *p@ = ping structure
596 * @int rc@ = return code to pass on
597 *
598 * Returns: ---
599 *
600 * Use: Disposes of a ping structure, maybe sending a notification.
601 */
602
603void p_pingdone(ping *p, int rc)
604{
0ba8de86 605 if (p->prev) p->prev->next = p->next;
606 else p->p->pings = p->next;
607 if (p->next) p->next->prev = p->prev;
608 if (rc != PING_TIMEOUT) sel_rmtimer(&p->t);
060ca767 609 T( trace(T_PEER, "peer: ping 0x%08lx done (rc = %d)",
610 (unsigned long)p->id, rc); )
0ba8de86 611 if (rc >= 0) p->func(rc, p->arg);
612}
613
614/* --- @p_pingtimeout@ --- *
615 *
616 * Arguments: @struct timeval *now@ = the time now
617 * @void *pv@ = pointer to ping block
618 *
619 * Returns: ---
620 *
621 * Use: Called when a ping times out.
622 */
623
624static void p_pingtimeout(struct timeval *now, void *pv)
625{
626 ping *p = pv;
627
628 T( trace(T_PEER, "peer: ping 0x%08lx timed out", (unsigned long)p->id); )
629 p_pingdone(p, PING_TIMEOUT);
630}
631
632/* --- @p_pingsend@ --- *
633 *
634 * Arguments: @peer *p@ = destination peer
635 * @ping *pg@ = structure to fill in
636 * @unsigned type@ = message type
637 * @unsigned long timeout@ = how long to wait before giving up
638 * @void (*func)(int, void *)@ = callback function
639 * @void *arg@ = argument for callback
640 *
641 * Returns: Zero if successful, nonzero if it failed.
642 *
643 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
644 * if we get an answer within the timeout, or zero if no answer.
645 */
646
647int p_pingsend(peer *p, ping *pg, unsigned type,
648 unsigned long timeout,
649 void (*func)(int, void *), void *arg)
650{
651 buf *b, bb;
652 struct timeval tv;
653
0ba8de86 654 switch (type) {
655 case MISC_PING:
656 pg->msg = MISC_PONG;
657 b = p_txstart(p, MSG_MISC | MISC_PING);
658 p_pingwrite(pg, b);
659 p_txend(p);
660 break;
661 case MISC_EPING:
662 pg->msg = MISC_EPONG;
663 b = p_txstart(p, MSG_MISC | MISC_EPING);
664 buf_init(&bb, buf_t, sizeof(buf_t));
665 p_pingwrite(pg, &bb);
666 buf_flip(&bb);
a50f9a0e 667 p_encrypt(p, MSG_MISC | MISC_EPING, &bb, b);
0ba8de86 668 if (!BOK(b))
669 return (-1);
670 p_txend(p);
671 break;
672 default:
673 abort();
674 break;
675 }
676
677 pg->next = p->pings;
678 pg->prev = 0;
679 pg->p = p;
680 pg->func = func;
681 pg->arg = arg;
060ca767 682 if (p->pings) p->pings->prev = pg;
0ba8de86 683 p->pings = pg;
684 gettimeofday(&tv, 0);
685 tv.tv_sec += timeout;
686 sel_addtimer(&sel, &pg->t, &tv, p_pingtimeout, pg);
687 T( trace(T_PEER, "peer: send %s 0x%08lx to %s",
688 p_pingtype(type), (unsigned long)pg->id, p->spec.name); )
689 return (0);
690}
691
37941236 692/* --- @p_greet@ --- *
693 *
694 * Arguments: @peer *p@ = peer to send to
695 * @const void *c@ = pointer to challenge
696 * @size_t sz@ = size of challenge
697 *
698 * Returns: ---
699 *
700 * Use: Sends a greeting packet.
701 */
702
703void p_greet(peer *p, const void *c, size_t sz)
704{
705 buf *b = p_txstart(p, MSG_MISC | MISC_GREET);
706 buf_put(b, c, sz);
707 p_txend(p);
708}
709
410c8acf 710/* --- @p_tun@ --- *
711 *
712 * Arguments: @peer *p@ = pointer to peer block
713 * @buf *b@ = buffer containing incoming packet
714 *
715 * Returns: ---
716 *
717 * Use: Handles a packet which needs to be sent to a peer.
718 */
719
720void p_tun(peer *p, buf *b)
721{
722 buf *bb = p_txstart(p, MSG_PACKET);
7b052fe6 723
f4c9c08f 724 QUICKRAND;
a50f9a0e 725 p_encrypt(p, MSG_PACKET, b, bb);
5bb41301 726 if (BOK(bb) && BLEN(bb)) {
727 p->st.n_ipout++;
728 p->st.sz_ipout += BLEN(bb);
410c8acf 729 p_txend(p);
5bb41301 730 }
410c8acf 731}
732
de014da6 733/* --- @p_keyreload@ --- *
734 *
735 * Arguments: ---
736 *
737 * Returns: ---
738 *
739 * Use: Forces a check of the daemon's keyring files.
740 */
741
742void p_keyreload(void)
743{
c8e02c8a
MW
744 if (km_reload())
745 FOREACH_PEER(p, { kx_newkeys(&p->kx); });
de014da6 746}
747
410c8acf 748/* --- @p_interval@ --- *
749 *
750 * Arguments: ---
751 *
752 * Returns: ---
753 *
754 * Use: Called periodically to do tidying.
755 */
756
757void p_interval(void)
758{
de014da6 759 p_keyreload();
c8e02c8a 760 FOREACH_PEER(p, { ksl_prune(&p->ks); });
410c8acf 761}
762
5bb41301 763/* --- @p_stats@ --- *
764 *
765 * Arguments: @peer *p@ = pointer to a peer block
766 *
767 * Returns: A pointer to the peer's statistics.
768 */
769
770stats *p_stats(peer *p) { return (&p->st); }
771
410c8acf 772/* --- @p_ifname@ --- *
773 *
774 * Arguments: @peer *p@ = pointer to a peer block
775 *
776 * Returns: A pointer to the peer's interface name.
777 */
778
64cf2223
MW
779const char *p_ifname(peer *p) { return (p->ifname); }
780
781/* --- @p_setifname@ --- *
782 *
783 * Arguments: @peer *p@ = pointer to a peer block
784 * @const char *name@ = pointer to the new name
785 *
786 * Returns: ---
787 *
788 * Use: Changes the name held for a peer's interface.
789 */
790
791void p_setifname(peer *p, const char *name)
72917fe7
MW
792{
793 xfree(p->ifname);
794 p->ifname = xstrdup(name);
795 if (p->spec.tops->setifname)
796 p->spec.tops->setifname(p->t, name);
797}
410c8acf 798
799/* --- @p_addr@ --- *
800 *
801 * Arguments: @peer *p@ = pointer to a peer block
802 *
803 * Returns: A pointer to the peer's address.
804 */
805
0ba8de86 806const addr *p_addr(peer *p) { return (&p->spec.sa); }
410c8acf 807
7737eb87 808/* --- @p_bind@ --- *
410c8acf 809 *
89640f3f 810 * Arguments: @struct addrinfo *ailist@ = addresses to bind to
410c8acf 811 *
9d966eb7 812 * Returns: Zero on success, @-1@ on failure.
410c8acf 813 *
9d966eb7 814 * Use: Binds to the main UDP sockets.
410c8acf 815 */
816
9d966eb7 817int p_bind(struct addrinfo *ailist)
410c8acf 818{
9d966eb7 819 int fd = -1;
df9dfccf 820 int len = PKBUFSZ;
47828bd9 821 int yes = 1;
89640f3f
MW
822 int i;
823 struct addrinfo *ai;
824 unsigned port, lastport = 0;
825 addr a;
826 socklen_t sz;
0a9920e2 827
063c9648 828 for (i = 0; i < NADDRFAM; i++) udpsock[i].sf.fd = -1;
89640f3f
MW
829
830 for (ai = ailist; ai; ai = ai->ai_next) {
831 if ((i = afix(ai->ai_family)) < 0) continue;
063c9648 832 if (udpsock[i].sf.fd != -1) continue;
89640f3f
MW
833
834 /* --- Note on socket buffer sizes --- *
835 *
836 * For some bizarre reason, Linux 2.2 (at least) doubles the socket
837 * buffer sizes I pass to @setsockopt@. I'm not putting special-case
838 * code here for Linux: BSD (at least TCPv2) does what I tell it rather
839 * than second-guessing me.
840 */
841
5ae728a6
MW
842 if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0) {
843 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
844 "create-failed", "?ERRNO", A_END);
9d966eb7 845 goto fail;
5ae728a6 846 }
47828bd9
MW
847 if (i == AFIX_INET6 &&
848 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes))) {
5ae728a6
MW
849 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
850 "set-v6only-failed", "?ERRNO", A_END);
9d966eb7 851 goto fail;
47828bd9 852 }
89640f3f
MW
853 assert(ai->ai_addrlen <= sizeof(a));
854 memcpy(&a, ai->ai_addr, ai->ai_addrlen);
855 if ((port = getport(&a)) == 0 && lastport) setport(&a, lastport);
5ae728a6
MW
856 if (bind(fd, &a.sa, addrsz(&a))) {
857 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
858 "bind-failed", "?ERRNO", A_END);
9d966eb7 859 goto fail;
5ae728a6 860 }
89640f3f
MW
861 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
862 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
5ae728a6
MW
863 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
864 "set-buffers-failed", "?ERRNO", A_END);
9d966eb7 865 goto fail;
89640f3f
MW
866 }
867 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
8d4c29d2
MW
868 if (port)
869 udpsock[i].port = port;
870 else {
89640f3f
MW
871 sz = sizeof(a);
872 if (getsockname(fd, &a.sa, &sz)) {
5ae728a6
MW
873 a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
874 "read-local-address-failed", "?ERRNO", A_END);
9d966eb7 875 goto fail;
89640f3f 876 }
8d4c29d2 877 udpsock[i].port = lastport = getport(&a);
89640f3f 878 }
c6fb2bee
MW
879 T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
880 sel_initfile(&sel, &udpsock[i].sf, fd, SEL_READ, p_read, 0);
881 sel_addfile(&udpsock[i].sf);
9d966eb7 882 fd = -1;
0a9920e2 883 }
c8e02c8a 884
9d966eb7
MW
885 return (0);
886
887fail:
888 if (fd != -1) close(fd);
889 p_unbind();
890 return (-1);
7737eb87
MW
891}
892
bf302d90
MW
893/* --- @p_unbind@ --- *
894 *
895 * Arguments: ---
896 *
897 * Returns: ---
898 *
899 * Use: Unbinds the UDP sockets. There must not be any active peers,
900 * and none can be created until the sockets are rebound.
901 */
902
903void p_unbind(void)
904{
905 int i;
906
907#ifndef NDEBUG
908 { peer_iter it; p_mkiter(&it); assert(!p_next(&it)); }
909#endif
910
911 for (i = 0; i < NADDRFAM; i++) {
912 if (udpsock[i].sf.fd == -1) continue;
913 sel_rmfile(&udpsock[i].sf);
914 close(udpsock[i].sf.fd);
915 udpsock[i].sf.fd = -1;
916 }
917}
918
7737eb87
MW
919/* --- @p_init@ --- *
920 *
921 * Arguments: ---
922 *
923 * Returns: ---
924 *
925 * Use: Initializes the peer system.
926 */
927
928void p_init(void)
929{
c8e02c8a
MW
930 sym_create(&byname);
931 am_create(&byaddr);
410c8acf 932}
933
24898e7e
MW
934/* --- @p_addtun@ --- *
935 *
936 * Arguments: @const tunnel_ops *tops@ = tunnel ops to add
937 *
9d966eb7 938 * Returns: Zero on success, @-1@ on failure.
24898e7e 939 *
9d966eb7
MW
940 * Use: Adds a tunnel class to the list of known classes, if it
941 * initializes properly. If there is no current default tunnel,
942 * then this one is made the default.
24898e7e
MW
943 *
944 * Does nothing if the tunnel class is already known. So adding
945 * a bunch of tunnels takes quadratic time, but there will be
946 * too few to care about.
947 */
948
9d966eb7 949int p_addtun(const tunnel_ops *tops)
24898e7e
MW
950{
951 struct tunnel_node *tn;
952
953 for (tn = tunnels; tn; tn = tn->next)
9d966eb7
MW
954 if (tn->tops == tops) return (0);
955 if (tops->init()) return (-1);
24898e7e
MW
956 tn = CREATE(struct tunnel_node);
957 tn->next = 0; tn->tops = tops;
958 *tunnels_tail = tn; tunnels_tail = &tn->next;
959 if (!dflttun) dflttun = tops;
9d966eb7 960 return (0);
24898e7e
MW
961}
962
963/* --- @p_setdflttun@ --- *
964 *
965 * Arguments: @const tunnel_ops *tops@ = tunnel ops to set
966 *
967 * Returns: ---
968 *
969 * Use: Sets the default tunnel. It must already be registered. The
970 * old default is forgotten.
971 */
972
973void p_setdflttun(const tunnel_ops *tops)
974 { dflttun = tops; }
975
976/* --- @p_dflttun@ --- *
977 *
978 * Arguments: ---
979 *
980 * Returns: A pointer to the current default tunnel operations, or null
981 * if no tunnels are defined.
982 */
983
984const tunnel_ops *p_dflttun(void) { return (dflttun); }
985
986/* --- @p_findtun@ --- *
987 *
988 * Arguments: @const char *name@ = tunnel name
989 *
990 * Returns: Pointer to the tunnel operations, or null.
991 *
992 * Use: Finds the operations for a named tunnel class.
993 */
994
995const tunnel_ops *p_findtun(const char *name)
996{
997 const struct tunnel_node *tn;
998
999 for (tn = tunnels; tn; tn = tn->next)
1000 if (mystrieq(tn->tops->name, name) == 0) return (tn->tops);
1001 return (0);
1002}
1003
1004/* --- @p_mktuniter@ --- *
1005 *
1006 * Arguments: @tuniter *i@ = pointer to iterator to initialize
1007 *
1008 * Returns: ---
1009 *
1010 * Use: Initializes a tunnel iterator.
1011 */
1012
1013void p_mktuniter(tun_iter *i) { i->next = tunnels; }
1014
1015/* --- @p_nexttun@ --- *
1016 *
1017 * Arguments: @tuniter *i@ = pointer to iterator
1018 *
1019 * Returns: Pointer to the next tunnel's operations, or null.
1020 */
1021
1022const tunnel_ops *p_nexttun(tun_iter *i)
1023{
1024 const struct tunnel_node *tn = i->next;
1025
1026 if (!tn) return (0);
1027 else { i->next = tn->next; return (tn->tops); }
1028}
1029
0ba8de86 1030/* --- @p_keepalive@ --- *
1031 *
1032 * Arguments: @struct timeval *now@ = the current time
1033 * @void *pv@ = peer to wake up
1034 *
1035 * Returns: ---
1036 *
1037 * Use: Sends a keepalive ping message to its peer.
1038 */
1039
1040static void p_keepalive(struct timeval *now, void *pv)
1041{
1042 peer *p = pv;
fe2a5dcf 1043
0ba8de86 1044 p_txstart(p, MSG_MISC | MISC_NOP); p_dotxend(p);
1045 T( trace(T_PEER, "peer: sent keepalive to %s", p->spec.name); )
1046 p_setkatimer(p);
1047}
1048
1049/* --- @p_setkatimer@ --- *
1050 *
1051 * Arguments: @peer *p@ = peer to set
1052 *
1053 * Returns: ---
1054 *
1055 * Use: Resets the keepalive timer thing.
1056 */
1057
1058static void p_setkatimer(peer *p)
1059{
1060 struct timeval tv;
1061
1062 if (!p->spec.t_ka)
1063 return;
1064 gettimeofday(&tv, 0);
1065 tv.tv_sec += p->spec.t_ka;
1066 sel_addtimer(&sel, &p->tka, &tv, p_keepalive, p);
1067}
1068
410c8acf 1069/* --- @p_create@ --- *
1070 *
0ba8de86 1071 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1072 *
1073 * Returns: Pointer to the peer block, or null if it failed.
1074 *
1075 * Use: Creates a new named peer block. No peer is actually attached
1076 * by this point.
1077 */
1078
0ba8de86 1079peer *p_create(peerspec *spec)
410c8acf 1080{
1081 peer *p = CREATE(peer);
eb5f3fea
MW
1082 const tunnel_ops *tops = spec->tops;
1083 int fd;
c8e02c8a
MW
1084 unsigned f;
1085
1086 p->byname = sym_find(&byname, spec->name, -1, sizeof(peer_byname), &f);
1087 if (f) goto tidy_0;
1088 p->byaddr = am_find(&byaddr, &spec->sa, sizeof(peer_byaddr), &f);
1089 if (f) goto tidy_1;
1090 p->byname->p = p->byaddr->p = p;
42da2a58 1091
0ba8de86 1092 T( trace(T_PEER, "peer: creating new peer `%s'", spec->name); )
1093 p->spec = *spec;
c8e02c8a 1094 p->spec.name = (/*unconst*/ char *)SYM_NAME(p->byname);
fe2a5dcf
MW
1095 if (spec->tag) p->spec.tag = xstrdup(spec->tag);
1096 if (spec->privtag) p->spec.privtag = xstrdup(spec->privtag);
8362ac1c 1097 if (spec->knock) p->spec.knock = xstrdup(spec->knock);
410c8acf 1098 p->ks = 0;
629842d8 1099 p->pings = 0;
64cf2223 1100 p->ifname = 0;
5d06f63e 1101 p->afix = afix(p->spec.sa.sa.sa_family); assert(p->afix >= 0);
5bb41301 1102 memset(&p->st, 0, sizeof(stats));
1103 p->st.t_start = time(0);
388e0319 1104 if (!(tops->flags & TUNF_PRIVOPEN))
eb5f3fea 1105 fd = -1;
388e0319 1106 else if ((fd = ps_tunfd(tops, &p->ifname)) < 0)
c8e02c8a 1107 goto tidy_2;
eb5f3fea
MW
1108 if ((p->t = tops->create(p, fd, &p->ifname)) == 0)
1109 goto tidy_3;
1110 T( trace(T_TUNNEL, "peer: attached interface %s to peer `%s'",
1111 p->ifname, p_name(p)); )
0ba8de86 1112 p_setkatimer(p);
024d189a 1113 iv_addreason();
0510f262 1114 if (kx_setup(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
eb5f3fea 1115 goto tidy_4;
f43df819 1116 a_notify("ADD",
6047fbac
MW
1117 "?PEER", p,
1118 "%s", p->ifname,
1119 "?ADDR", &p->spec.sa,
1120 A_END);
8743c776 1121 if (!(p->spec.f & KXF_CORK)) {
010e6f63 1122 a_notify("KXSTART", "?PEER", p, A_END);
f43df819 1123 /* Couldn't tell anyone before */
010e6f63 1124 }
6411163d 1125 if (p->spec.f & PSF_MOBILE) nmobile++;
410c8acf 1126 return (p);
1127
eb5f3fea 1128tidy_4:
fe2a5dcf 1129 if (spec->t_ka) sel_rmtimer(&p->tka);
72917fe7 1130 xfree(p->ifname);
0ba8de86 1131 p->t->ops->destroy(p->t);
024d189a 1132 iv_rmreason();
eb5f3fea
MW
1133tidy_3:
1134 if (fd >= 0) close(fd);
c8e02c8a
MW
1135tidy_2:
1136 am_remove(&byaddr, p->byaddr);
48b84569 1137 if (p->spec.tag) xfree(p->spec.tag);
fe2a5dcf 1138 if (p->spec.privtag) xfree(p->spec.privtag);
c8e02c8a
MW
1139tidy_1:
1140 sym_remove(&byname, p->byname);
410c8acf 1141tidy_0:
410c8acf 1142 DESTROY(p);
1143 return (0);
1144}
1145
1146/* --- @p_name@ --- *
1147 *
1148 * Arguments: @peer *p@ = pointer to a peer block
1149 *
1150 * Returns: A pointer to the peer's name.
1151 */
1152
6411163d
MW
1153const char *p_name(peer *p)
1154 { if (p) return (p->spec.name); else return ("-"); }
410c8acf 1155
48b84569
MW
1156/* --- @p_tag@ --- *
1157 *
1158 * Arguments: @peer *p@ = pointer to a peer block
1159 *
1160 * Returns: A pointer to the peer's public key tag.
1161 */
1162
1163const char *p_tag(peer *p)
1164 { return (p->spec.tag ? p->spec.tag : p->spec.name); }
1165
fe2a5dcf
MW
1166/* --- @p_privtag@ --- *
1167 *
1168 * Arguments: @peer *p@ = pointer to a peer block
1169 *
1170 * Returns: A pointer to the peer's private key tag.
1171 */
1172
1173const char *p_privtag(peer *p)
1174 { return (p->spec.privtag ? p->spec.privtag : tag_priv); }
1175
060ca767 1176/* --- @p_spec@ --- *
1177 *
1178 * Arguments: @peer *p@ = pointer to a peer block
1179 *
1180 * Returns: Pointer to the peer's specification
1181 */
1182
1183const peerspec *p_spec(peer *p) { return (&p->spec); }
1184
c8e02c8a
MW
1185/* --- @p_findbyaddr@ --- *
1186 *
1187 * Arguments: @const addr *a@ = address to look up
1188 *
1189 * Returns: Pointer to the peer block, or null if not found.
1190 *
1191 * Use: Finds a peer by address.
1192 */
1193
1194peer *p_findbyaddr(const addr *a)
1195{
1196 peer_byaddr *pa;
1197
6411163d
MW
1198 if ((pa = am_find(&byaddr, a, 0, 0)) != 0) {
1199 assert(pa->p);
c8e02c8a 1200 return (pa->p);
6411163d 1201 }
c8e02c8a
MW
1202 return (0);
1203}
1204
410c8acf 1205/* --- @p_find@ --- *
1206 *
1207 * Arguments: @const char *name@ = name to look up
1208 *
1209 * Returns: Pointer to the peer block, or null if not found.
1210 *
1211 * Use: Finds a peer by name.
1212 */
1213
1214peer *p_find(const char *name)
1215{
c8e02c8a
MW
1216 peer_byname *pn;
1217
1218 if ((pn = sym_find(&byname, name, -1, 0, 0)) != 0)
1219 return (pn->p);
e04c2d50 1220 return (0);
410c8acf 1221}
1222
1223/* --- @p_destroy@ --- *
1224 *
1225 * Arguments: @peer *p@ = pointer to a peer
067aa5f0 1226 * @int bye@ = say goodbye to the peer?
410c8acf 1227 *
1228 * Returns: ---
1229 *
1230 * Use: Destroys a peer.
1231 */
1232
067aa5f0 1233void p_destroy(peer *p, int bye)
410c8acf 1234{
0ba8de86 1235 ping *pg, *ppg;
067aa5f0 1236 buf *b, bb;
0ba8de86 1237
1238 T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
067aa5f0 1239
136f3f44 1240 if (bye) {
067aa5f0
MW
1241 b = p_txstart(p, MSG_MISC | MISC_BYE);
1242 buf_init(&bb, buf_t, sizeof(buf_t));
1243 assert(BOK(&bb)); buf_flip(&bb);
1244 p_encrypt(p, MSG_MISC | MISC_BYE, &bb, b);
1245 p_txend(p);
1246 }
1247
f43df819 1248 a_notify("KILL", "%s", p->spec.name, A_END);
5bb41301 1249 ksl_free(&p->ks);
410c8acf 1250 kx_free(&p->kx);
fe2a5dcf
MW
1251 if (p->spec.f & PSF_MOBILE) nmobile--;
1252 if (p->ifname) xfree(p->ifname);
1253 if (p->spec.tag) xfree(p->spec.tag);
1254 if (p->spec.privtag) xfree(p->spec.privtag);
8362ac1c 1255 if (p->spec.knock) xfree(p->spec.knock);
42da2a58 1256 p->t->ops->destroy(p->t);
fe2a5dcf 1257 if (p->spec.t_ka) sel_rmtimer(&p->tka);
0ba8de86 1258 for (pg = p->pings; pg; pg = ppg) {
1259 ppg = pg->next;
1260 p_pingdone(pg, PING_PEERDIED);
1261 }
c8e02c8a
MW
1262 sym_remove(&byname, p->byname);
1263 am_remove(&byaddr, p->byaddr);
024d189a 1264 iv_rmreason();
410c8acf 1265 DESTROY(p);
1266}
1267
78e45b53
MW
1268/* --- @p_destroyall@ --- *
1269 *
1270 * Arguments: ---
1271 *
1272 * Returns: ---
1273 *
1274 * Use: Destroys all of the peers, saying goodbye.
1275 */
1276
1277void p_destroyall(void) { FOREACH_PEER(p, { p_destroy(p, 1); }); }
1278
c8e02c8a
MW
1279/* --- @p_mkiter@ --- *
1280 *
1281 * Arguments: @peer_iter *i@ = pointer to an iterator
1282 *
1283 * Returns: ---
1284 *
1285 * Use: Initializes the iterator.
1286 */
1287
1288void p_mkiter(peer_iter *i) { sym_mkiter(&i->i, &byname); }
1289
1290/* --- @p_next@ --- *
1291 *
1292 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1293 *
c8e02c8a 1294 * Returns: Next peer, or null if at the end.
410c8acf 1295 *
c8e02c8a 1296 * Use: Returns the next peer.
410c8acf 1297 */
1298
c8e02c8a
MW
1299peer *p_next(peer_iter *i)
1300{
1301 peer_byname *pn;
1302
1303 if ((pn = sym_next(&i->i)) == 0)
1304 return (0);
1305 return (pn->p);
1306}
410c8acf 1307
1308/*----- That's all, folks -------------------------------------------------*/