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