chiark / gitweb /
Stuff more randomness into the pool in the interval timer.
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id: tripe.h,v 1.1 2001/02/03 20:26:37 mdw Exp $
4  *
5  * Main header file for TrIPE
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: tripe.h,v $
32  * Revision 1.1  2001/02/03 20:26:37  mdw
33  * Initial checkin.
34  *
35  */
36
37 #ifndef TRIPE_H
38 #define TRIPE_H
39
40 #ifdef __cplusplus
41   extern "C" {
42 #endif
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include <assert.h>
47 #include <ctype.h>
48 #include <errno.h>
49 #include <signal.h>
50 #include <stdarg.h>
51 #include <stddef.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56
57 #include <sys/types.h>
58 #include <sys/time.h>
59 #include <unistd.h>
60 #include <fcntl.h>
61 #include <sys/stat.h>
62
63 #include <sys/socket.h>
64 #include <sys/un.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <netdb.h>
68
69 #include <pwd.h>
70 #include <grp.h>
71
72 #include <mLib/alloc.h>
73 #include <mLib/arena.h>
74 #include <mLib/bres.h>
75 #include <mLib/dstr.h>
76 #include <mLib/env.h>
77 #include <mLib/fdflags.h>
78 #include <mLib/fwatch.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
82 #include <mLib/sel.h>
83 #include <mLib/selbuf.h>
84 #include <mLib/sig.h>
85 #include <mLib/str.h>
86 #include <mLib/sub.h>
87 #include <mLib/trace.h>
88
89 #include <catacomb/gcipher.h>
90 #include <catacomb/gmac.h>
91 #include <catacomb/grand.h>
92 #include <catacomb/key.h>
93 #include <catacomb/paranoia.h>
94
95 #include <catacomb/blowfish.h>
96 #include <catacomb/blowfish-cbc.h>
97 #include <catacomb/noise.h>
98 #include <catacomb/rand.h>
99 #include <catacomb/rmd160.h>
100 #include <catacomb/rmd160-hmac.h>
101
102 #include <catacomb/mp.h>
103 #include <catacomb/mpmont.h>
104 #include <catacomb/mprand.h>
105 #include <catacomb/dh.h>
106
107 #include "util.h"
108
109 #undef sun
110
111 /*----- Magic numbers -----------------------------------------------------*/
112
113 /* --- Tunnel types --- */
114
115 #define TUN_NOTDEF 0
116 #define TUN_UNET 1
117 #define TUN_BSD 2
118
119 /* --- Trace flags --- */
120
121 #define T_TUNNEL 1u
122 #define T_PEER 2u
123 #define T_PACKET 4u
124 #define T_ADMIN 8u
125 #define T_CRYPTO 16u
126 #define T_KEYSET 32u
127 #define T_KEYEXCH 64u
128 #define T_KEYMGMT 128u
129
130 #define T_ALL 255u
131
132 /* --- Units --- */
133
134 #define SEC(n) (n##u)
135 #define MIN(n) (n##u * 60u)
136 #define MEG(n) (n##ul * 1024ul * 1024ul)
137
138 /* --- Other things --- */
139
140 #define PKBUFSZ 65536
141
142 /*----- TrIPE protocol ----------------------------------------------------*/
143
144 /* --- TrIPE packet format --- *
145  *
146  * A packet begins with a single-byte packet type.  The remaining data
147  * depends on the packet type.
148  */
149
150 #define MSG_PACKET 0u
151 /* Followed by a 64-bit MAC and an encrypted packet.  The MAC is used as an
152  * IV for a 64-bit block cipher in CBC-stealing mode.
153  */
154
155 #define MSG_PRECHALLENGE 1u
156 /* Followed by the challenge only.  Useful for bootstrapping the system.
157  */
158
159 #define MSG_CHALLENGE 2u
160 /* Followed by a response hash and a large-integer challenge.
161  */
162
163 #define MSG_RESPONSE 3u
164 /* Followed by a large-integer response.
165  */
166
167 /*----- Data structures ---------------------------------------------------*/
168
169 /* --- Buffers --- *
170  *
171  * Buffers provide a simple stream-like interface for building and parsing
172  * packets.
173  */
174
175 typedef struct buf {
176   octet *base, *p, *limit;              /* Pointers to the buffer */
177   unsigned f;                           /* Various flags */
178 } buf;
179
180 #define BF_BROKEN 1u                    /* Buffer is broken */
181
182 /* --- Socket addresses --- *
183  *
184  * A magic union of supported socket addresses.
185  */
186
187 typedef union addr {
188   struct sockaddr sa;
189   struct sockaddr_in sin;
190 } addr;
191
192 /* --- A symmetric keyset --- *
193  *
194  * A keyset contains a set of symmetric keys for encrypting and decrypting
195  * packets.  Keysets are stored in a list, sorted in reverse order of
196  * creation, so that the most recent keyset (the one most likely to be used)
197  * is first.
198  *
199  * Each keyset has a time limit and a data limit.  The keyset is destroyed
200  * when either it has existed for too long, or it has been used to encrypt
201  * too much data.  New key exchanges are triggered when keys are close to
202  * expiry.
203  */
204
205 typedef struct keyset {
206   struct keyset *next;                  /* Next active keyset in the list */
207   time_t t_exp;                         /* Expiry time for this keyset */
208   unsigned long sz_exp;                 /* Data limit for the keyset */
209 #ifndef NTRACE
210   unsigned seq;                         /* Sequence number for tracing */
211 #endif
212   gcipher *c;                           /* Keyset cipher for encryption */
213   gmac *m;                              /* Keyset MAC for integrity */
214 } keyset;
215
216 /* --- Key exchange --- *
217  *
218  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
219  * Protocol has a number of desirable features (e.g., perfect forward
220  * secrecy, and zero-knowledge authentication) which make it attractive for
221  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
222  * Clive Jones.
223  */
224
225 typedef struct keyexch {
226   keyset **ks;                          /* Peer's list of keysets */
227   struct peer *p;                       /* Pointer back to the peer */
228   unsigned f;                           /* Various useful flags */
229   sel_timer t;                          /* Timer for next exchange */
230   dh_pub kpub;                          /* Peer's public key */
231   mp *my_x, *my_gx, *my_gxy;            /* My half of the exchange */
232   octet my_h[RMD160_HASHSZ];            /* My challenge hash */
233   mp *your_gx, *your_gxy;               /* Your half of the exchange */
234   octet your_h[RMD160_HASHSZ];          /* Your challenge hash */
235   time_t t_valid;                       /* When this exchange goes bad */
236   time_t t_qchal, t_qresp;              /* Quiet timers for packet types */
237   time_t t_newchal;                     /* When to accept a new challenge */
238 } keyexch;
239
240 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
241 #define KXF_INIT 2u                     /* Big numbers are initialized */
242 #define KXF_MYH 4u                      /* My hash has been computed */
243 #define KXF_YOURH 8u                    /* Your hash has been received */
244 #define KXF_REPLY 16u                   /* Received your response OK */
245 #define KXF_DONE 32u                    /* Key exchange completed */
246
247 /* --- Tunnel structure --- *
248  *
249  * Used to maintain system-specific information about the tunnel interface.
250  */
251
252 typedef struct tunnel {
253 #if TUN_TYPE == TUN_UNET
254   sel_file f;                           /* Selector for Usernet device */
255   struct peer *p;                       /* Pointer to my peer */
256 #else
257 #  error "No support for this tunnel type"
258 #endif
259 } tunnel;
260
261 /* --- Peer structure --- *
262  *
263  * The main structure which glues everything else together.
264  */
265
266 typedef struct peer {
267   struct peer *next, *prev;             /* Links to next and previous */
268   char *name;                           /* Name of this peer */
269   tunnel t;                             /* Tunnel for local packets */
270   keyset *ks;                           /* List head for keysets */
271   keyexch kx;                           /* Key exchange protocol block */
272   buf b;                                /* Buffer for sending packets */
273   addr peer;                            /* Peer socket address */
274   size_t sasz;                          /* Socket address size */
275 } peer;
276
277 /* --- Admin structure --- */
278
279 typedef struct admin {
280   struct admin *next, *prev;            /* Links to next and previous */
281   selbuf b;                             /* Line buffer for commands */
282   int fd;                               /* File descriptor for output */
283 #ifndef NTRACE
284   unsigned seq;                         /* Sequence number for tracing */
285 #endif
286   char *pname;                          /* Peer name to create */
287   char *paddr;                          /* Address string to resolve */
288   bres_client r;                        /* Background resolver task */
289   sel_timer t;                          /* Timer for resolver */
290   addr peer;                            /* Address to set */
291   size_t sasz;                          /* Size of the address */
292 } admin;
293
294 /*----- Global variables --------------------------------------------------*/
295
296 extern sel_state sel;                   /* Global I/O event state */
297 extern dh_priv kpriv;                   /* Our private key */
298 extern mpmont mg;                       /* Montgomery context for DH group */
299 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ]; /* Big packet buffers */
300
301 #ifndef NTRACE
302 extern const trace_opt tr_opts[];       /* Trace options array */
303 extern unsigned tr_flags;               /* Trace options flags */
304 #endif
305
306 /*----- Key management ----------------------------------------------------*/
307
308 /* --- @km_interval@ --- *
309  *
310  * Arguments:   ---
311  *
312  * Returns:     Zero if OK, nonzero to force reloading of keys.
313  *
314  * Use:         Called on the interval timer to perform various useful jobs.
315  */
316
317 extern int km_interval(void);
318
319 /* --- @km_init@ --- *
320  *
321  * Arguments:   @const char *kr_priv@ = private keyring file
322  *              @const char *kr_pub@ = public keyring file
323  *              @const char *tag@ = tag to load
324  *
325  * Returns:     ---
326  *
327  * Use:         Initializes, and loads the private key.
328  */
329
330 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
331                     const char */*tag*/);
332
333 /* --- @km_getpubkey@ --- *
334  *
335  * Arguments:   @const char *tag@ = public key tag to load
336  *              @dh_pub *kpub@ = where to put the public key
337  *
338  * Returns:     Zero if OK, nonzero if it failed.
339  *
340  * Use:         Fetches a public key from the keyring.
341  */
342
343 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/);
344
345 /*----- Key exchange ------------------------------------------------------*/
346
347 /* --- @kx_start@ --- *
348  *
349  * Arguments:   @keyexch *kx@ = pointer to key exchange context
350  *
351  * Returns:     ---
352  *
353  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
354  *              a new challenge is sent (unless the quiet timer forbids
355  *              this); if no exchange is in progress, one is commenced.
356  */
357
358 extern void kx_start(keyexch */*kx*/);
359
360 /* --- @kx_prechallenge@ --- *
361  *
362  * Arguments:   @keyexch *kx@ = pointer to key exhange context
363  *              @buf *b@ = pointer to buffer containing the packet
364  *
365  * Returns:     ---
366  *
367  * Use:         Reads a prechallenge packet from the buffer and handles it.
368  */
369
370 extern void kx_prechallenge(keyexch */*kx*/, buf */*b*/);
371
372 /* --- @kx_challenge@ --- *
373  *
374  * Arguments:   @keyexch *kx@ = pointer to key exchange context
375  *              @buf *b@ = a buffer containing the packet to read
376  *
377  * Returns:     ---
378  *
379  * Use:         Reads a challenge from the buffer and handles it.
380  */
381
382 extern void kx_challenge(keyexch */*kx*/, buf */*b*/);
383
384 /* --- @kx_response@ --- *
385  *
386  * Arguments:   @keyexch *kx@ = pointer to key exchange context
387  *              @buf *b@ = a buffer containing the packet to read
388  *
389  * Returns:     ---
390  *
391  * Use:         Reads a response from the buffer and handles it.
392  */
393
394 extern void kx_response(keyexch */*kx*/, buf */*b*/);
395
396 /* --- @kx_free@ --- *
397  *
398  * Arguments:   @keyexch *kx@ = pointer to key exchange context
399  *
400  * Returns:     ---
401  *
402  * Use:         Frees everything in a key exchange context.
403  */
404
405 extern void kx_free(keyexch */*kx*/);
406
407 /* --- @kx_newkeys@ --- *
408  *
409  * Arguments:   @keyexch *kx@ = pointer to key exchange context
410  *
411  * Returns:     ---
412  *
413  * Use:         Informs the key exchange module that its keys may have
414  *              changed.  If fetching the new keys fails, the peer will be
415  *              destroyed, we log messages and struggle along with the old
416  *              keys.
417  */
418
419 extern void kx_newkeys(keyexch */*kx*/);
420
421 /* --- @kx_init@ --- *
422  *
423  * Arguments:   @keyexch *kx@ = pointer to key exchange context
424  *              @peer *p@ = pointer to peer context
425  *              @keyset **ks@ = pointer to keyset list
426  *
427  * Returns:     Zero if OK, nonzero if it failed.
428  *
429  * Use:         Initializes a key exchange module.  The module currently
430  *              contains no keys, and will attempt to initiate a key
431  *              exchange.
432  */
433
434 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
435
436 /*----- Keysets and symmetric cryptography --------------------------------*/
437
438 /* --- @ks_free@ --- *
439  *
440  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
441  *
442  * Returns:     ---
443  *
444  * Use:         Frees all of the keys in a keyset.
445  */
446
447 extern void ks_free(keyset **/*ksroot*/);
448
449 /* --- @ks_prune@ --- *
450  *
451  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
452  *
453  * Returns:     ---
454  *
455  * Use:         Prunes the keyset list by removing keys which mustn't be used
456  *              any more.
457  */
458
459 extern void ks_prune(keyset **/*ksroot*/);
460
461 /* --- @ks_gen@ --- *
462  *
463  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
464  *              @const void *k@ = pointer to key material
465  *              @size_t sz@ = size of the key material
466  *
467  * Returns:     The regeneration time for the new key.
468  *
469  * Use:         Derives a keyset from the given key material and adds it to
470  *              the list.
471  */
472
473 extern time_t ks_gen(keyset **/*ksroot*/, const void */*k*/, size_t /*sz*/);
474
475 /* --- @ks_encrypt@ --- *
476  *
477  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
478  *              @buf *b@ = pointer to input buffer
479  *              @buf *bb@ = pointer to output buffer
480  *
481  * Returns:     Nonzero if a new key is needed.
482  *
483  * Use:         Encrypts a packet.
484  */
485
486 extern int ks_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
487
488 /* --- @ks_decrypt@ --- *
489  *
490  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
491  *              @buf *b@ = pointer to input buffer
492  *              @buf *bb@ = pointer to output buffer
493  *
494  * Returns:     Nonzero if the packet couldn't be decrypted.
495  *
496  * Use:         Decrypts a packet.
497  */
498
499 extern int ks_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
500
501 /*----- Administration interface ------------------------------------------*/
502
503 /* --- @a_warn@ --- *
504  *
505  * Arguments:   @const char *fmt@ = pointer to format string
506  *              @...@ = other arguments
507  *
508  * Returns:     ---
509  *
510  * Use:         Informs all admin connections of a warning.
511  */
512
513 extern void a_warn(const char */*fmt*/, ...);
514
515 /* --- @a_create@ --- *
516  *
517  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
518  *
519  * Returns:     ---
520  *
521  * Use:         Creates a new admin connection.
522  */
523
524 extern void a_create(int /*fd_in*/, int /*fd_out*/);
525
526 /* --- @a_quit@ --- *
527  *
528  * Arguments:   ---
529  *
530  * Returns:     ---
531  *
532  * Use:         Shuts things down nicely.
533  */
534
535 extern void a_quit(void);
536
537 /* --- @a_daemon@ --- *
538  *
539  * Arguments:   ---
540  *
541  * Returns:     ---
542  *
543  * Use:         Informs the admin module that it's a daemon.
544  */
545
546 extern void a_daemon(void);
547
548 /* --- @a_init@ --- *
549  *
550  * Arguments:   @const char *sock@ = socket name to create
551  *
552  * Returns:     ---
553  *
554  * Use:         Creates the admin listening socket.
555  */
556
557 extern void a_init(const char */*sock*/);
558
559 /*----- Peer management ---------------------------------------------------*/
560
561 /* --- @p_txstart@ --- *
562  *
563  * Arguments:   @peer *p@ = pointer to peer block
564  *              @unsigned msg@ = message type code
565  *
566  * Returns:     A pointer to a buffer to write to.
567  *
568  * Use:         Starts sending to a peer.  Only one send can happen at a
569  *              time.
570  */
571
572 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
573
574 /* --- @p_txend@ --- *
575  *
576  * Arguments:   @peer *p@ = pointer to peer block
577  *
578  * Returns:     ---
579  *
580  * Use:         Sends a packet to the peer.
581  */
582
583 extern void p_txend(peer */*p*/);
584
585 /* --- @p_tun@ --- *
586  *
587  * Arguments:   @peer *p@ = pointer to peer block
588  *              @buf *b@ = buffer containing incoming packet
589  *
590  * Returns:     ---
591  *
592  * Use:         Handles a packet which needs to be sent to a peer.
593  */
594
595 extern void p_tun(peer */*p*/, buf */*b*/);
596
597 /* --- @p_interval@ --- *
598  *
599  * Arguments:   ---
600  *
601  * Returns:     ---
602  *
603  * Use:         Called periodically to do tidying.
604  */
605
606 extern void p_interval(void);
607
608 /* --- @p_ifname@ --- *
609  *
610  * Arguments:   @peer *p@ = pointer to a peer block
611  *
612  * Returns:     A pointer to the peer's interface name.
613  */
614
615 extern const char *p_ifname(peer */*p*/);
616
617 /* --- @p_addr@ --- *
618  *
619  * Arguments:   @peer *p@ = pointer to a peer block
620  *
621  * Returns:     A pointer to the peer's address.
622  */
623
624 extern const addr *p_addr(peer */*p*/);
625
626 /* --- @p_init@ --- *
627  *
628  * Arguments:   @unsigned port@ = port number to listen to
629  *
630  * Returns:     ---
631  *
632  * Use:         Initializes the peer system; creates the socket.
633  */
634
635 extern void p_init(unsigned /*port*/);
636
637 /* --- @p_port@ --- *
638  *
639  * Arguments:   ---
640  *
641  * Returns:     Port number used for socket.
642  */
643
644 unsigned p_port(void);
645
646 /* --- @p_create@ --- *
647  *
648  * Arguments:   @const char *name@ = name for this peer
649  *              @struct sockaddr *sa@ = socket address of peer
650  *              @size_t sz@ = size of socket address
651  *
652  * Returns:     Pointer to the peer block, or null if it failed.
653  *
654  * Use:         Creates a new named peer block.  No peer is actually attached
655  *              by this point.
656  */
657
658 extern peer *p_create(const char */*name*/,
659                       struct sockaddr */*sa*/, size_t /*sz*/);
660
661 /* --- @p_name@ --- *
662  *
663  * Arguments:   @peer *p@ = pointer to a peer block
664  *
665  * Returns:     A pointer to the peer's name.
666  */
667
668 extern const char *p_name(peer */*p*/);
669
670 /* --- @p_find@ --- *
671  *
672  * Arguments:   @const char *name@ = name to look up
673  *
674  * Returns:     Pointer to the peer block, or null if not found.
675  *
676  * Use:         Finds a peer by name.
677  */
678
679 extern peer *p_find(const char */*name*/);
680
681 /* --- @p_destroy@ --- *
682  *
683  * Arguments:   @peer *p@ = pointer to a peer
684  *
685  * Returns:     ---
686  *
687  * Use:         Destroys a peer.
688  */
689
690 extern void p_destroy(peer */*p*/);
691
692 /* --- @p_first@, @p_next@ --- *
693  *
694  * Arguments:   @peer *p@ = a peer block
695  *
696  * Returns:     @peer_first@ returns the first peer in some ordering;
697  *              @peer_next@ returns the peer following a given one in the
698  *              same ordering.  Null is returned for the end of the list.
699  */
700
701 extern peer *p_first(void);
702 extern peer *p_next(peer */*p*/);
703
704 /*----- Tunnel interface --------------------------------------------------*/
705
706 /* --- @tun_init@ --- *
707  *
708  * Arguments:   ---
709  *
710  * Returns:     ---
711  *
712  * Use:         Initializes the tunneling system.  Maybe this will require
713  *              opening file descriptors or something.
714  */
715
716 extern void tun_init(void);
717
718 /* --- @tun_create@ --- *
719  *
720  * Arguments:   @tunnel *t@ = pointer to tunnel block
721  *              @peer *p@ = pointer to peer block
722  *
723  * Returns:     Zero if it worked, nonzero on failure.
724  *
725  * Use:         Initializes a new tunnel.
726  */
727
728 extern int tun_create(tunnel */*t*/, peer */*p*/);
729
730 /* --- @tun_ifname@ --- *
731  *
732  * Arguments:   @tunnel *t@ = pointer to tunnel block
733  *
734  * Returns:     A pointer to the tunnel's interface name.
735  */
736
737 extern const char *tun_ifname(tunnel */*t*/);
738
739 /* --- @tun_inject@ --- *
740  *
741  * Arguments:   @tunnel *t@ = pointer to tunnel block
742  *              @buf *b@ = buffer to send
743  *
744  * Returns:     ---
745  *
746  * Use:         Injects a packet into the local network stack.
747  */
748
749 extern void tun_inject(tunnel */*t*/, buf */*b*/);
750
751 /* --- @tun_destroy@ --- *
752  *
753  * Arguments:   @tunnel *t@ = pointer to tunnel block
754  *
755  * Returns:     ---
756  *
757  * Use:         Destroys a tunnel.
758  */
759
760 extern void tun_destroy(tunnel */*t*/);
761
762 /*----- Buffer handling ---------------------------------------------------*/
763
764 /* --- Useful macros --- */
765
766 #define BBASE(b) ((b)->base)
767 #define BLIM(b) ((b)->limit)
768 #define BCUR(b) ((b)->p)
769 #define BSZ(b) ((b)->limit - (b)->base)
770 #define BLEN(b) ((b)->p - (b)->base)
771 #define BLEFT(b) ((b)->limit - (b)->p)
772 #define BSTEP(b, sz) ((b)->p += (sz))
773 #define BBAD(b) ((b)->f & BF_BROKEN)
774 #define BOK(b) (!BBAD(b))
775
776 #define BENSURE(b, sz)                                                  \
777   (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0)
778
779 /* --- @buf_init@ --- *
780  *
781  * Arguments:   @buf *b@ = pointer to a buffer block
782  *              @void *p@ = pointer to a buffer
783  *              @size_t sz@ = size of the buffer
784  *
785  * Returns:     ---
786  *
787  * Use:         Initializes the buffer block appropriately.
788  */
789
790 extern void buf_init(buf */*b*/, void */*p*/, size_t /*sz*/);
791
792 /* --- @buf_break@ --- *
793  *
794  * Arguments:   @buf *b@ = pointer to a buffer block
795  *
796  * Returns:     Some negative value.
797  *
798  * Use:         Marks a buffer as broken.
799  */
800
801 extern int buf_break(buf */*b*/);
802
803 /* --- @buf_ensure@ --- *
804  *
805  * Arguments:   @buf *b@ = pointer to a buffer block
806  *              @size_t sz@ = size of data wanted
807  *
808  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
809  *
810  * Use:         Ensures that there are @sz@ bytes still in the buffer.
811  */
812
813 extern int buf_ensure(buf */*b*/, size_t /*sz*/);
814
815 /* --- @buf_get@ --- *
816  *
817  * Arguments:   @buf *b@ = pointer to a buffer block
818  *              @void *p@ = pointer to a buffer
819  *              @size_t sz@ = size of the buffer
820  *
821  * Returns:     Zero if it worked, nonzero if there wasn't enough data.
822  *
823  * Use:         Fetches data from the buffer into some other place.
824  */
825
826 extern int buf_get(buf */*b*/, void */*p*/, size_t /*sz*/);
827
828 /* --- @buf_put@ --- *
829  *
830  * Arguments:   @buf *b@ = pointer to a buffer block
831  *              @const void *p@ = pointer to a buffer
832  *              @size_t sz@ = size of the buffer
833  *
834  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
835  *
836  * Use:         Fetches data from some place and puts it in the buffer
837  */
838
839 extern int buf_put(buf */*b*/, const void */*p*/, size_t /*sz*/);
840
841 /* --- @buf_getbyte@ --- *
842  *
843  * Arguments:   @buf *b@ = pointer to a buffer block
844  *
845  * Returns:     A byte, or less than zero if there wasn't a byte there.
846  *
847  * Use:         Gets a single byte from a buffer.
848  */
849
850 extern int buf_getbyte(buf */*b*/);
851
852 /* --- @buf_putbyte@ --- *
853  *
854  * Arguments:   @buf *b@ = pointer to a buffer block
855  *              @int ch@ = byte to write
856  *
857  * Returns:     Zero if OK, nonzero if there wasn't enough space.
858  *
859  * Use:         Puts a single byte in a buffer.
860  */
861
862 extern int buf_putbyte(buf */*b*/, int /*ch*/);
863
864 /* --- @buf_getword@ --- *
865  *
866  * Arguments:   @buf *b@ = pointer to a buffer block
867  *              @uint32 *w@ = where to put the word
868  *
869  * Returns:     Zero if OK, or nonzero if there wasn't a word there.
870  *
871  * Use:         Gets a 32-bit word from a buffer.
872  */
873
874 extern int buf_getword(buf */*b*/, uint32 */*w*/);
875
876 /* --- @buf_putword@ --- *
877  *
878  * Arguments:   @buf *b@ = pointer to a buffer block
879  *              @uint32 w@ = word to write
880  *
881  * Returns:     Zero if OK, nonzero if there wasn't enough space.
882  *
883  * Use:         Puts a 32-but word in a buffer.
884  */
885
886 extern int buf_putword(buf */*b*/, uint32 /*w*/);
887
888 /* --- @buf_getmp@ --- *
889  *
890  * Arguments:   @buf *b@ = pointer to a buffer block
891  *
892  * Returns:     A multiprecision integer, or null if there wasn't one there.
893  *
894  * Use:         Gets a multiprecision integer from a buffer.
895  */
896
897 extern mp *buf_getmp(buf */*b*/, mp */*d*/);
898
899 /* --- @buf_putmp@ --- *
900  *
901  * Arguments:   @buf *b@ = pointer to a buffer block
902  *              @mp *m@ = a multiprecision integer
903  *
904  * Returns:     Zero if it worked, nonzero if there wasn't enough space.
905  *
906  * Use:         Puts a multiprecision integer to a buffer.
907  */
908
909 extern int buf_putmp(buf */*b*/, mp */*m*/);
910
911 /*----- Other handy utilities ---------------------------------------------*/
912
913 /* --- @mpstr@ --- *
914  *
915  * Arguments:   @mp *m@ = a multiprecision integer
916  *
917  * Returns:     A pointer to the integer's textual representation.
918  *
919  * Use:         Converts a multiprecision integer to a string.  Corrupts
920  *              @buf_o@.
921  */
922
923 extern const char *mpstr(mp */*m*/);
924
925 /*----- That's all, folks -------------------------------------------------*/
926
927 #ifdef __cplusplus
928   }
929 #endif
930
931 #endif