chiark / gitweb /
fbaf2e2433dfb102972c102ec589a8370589182e
[userv-utils.git] / ipif / forwarder.h
1 /*
2  */
3
4 #ifndef MECHS_H
5 #define MECHS_H
6
7 #include <stdio.h>
8 #include <time.h>
9
10 #include "automech.h"
11
12 struct buffer {
13   unsigned char *base;
14   unsigned char *start;
15   size_t size;
16 };
17
18 struct mechdata;
19
20 typedef void mes_functype(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io);
21 typedef void mds_functype(struct mechdata **md_r);
22
23 typedef void menc_functype(struct mechdata *md, struct buffer*);
24 typedef const char *mdec_functype(struct mechdata *md, struct buffer*);
25
26 struct mechanism {
27   const char *name;
28   mes_functype *encsetup;
29   mds_functype *decsetup;
30   menc_functype *encode;
31   mdec_functype *decode;
32 };
33
34 extern const struct mechanism *const mechanismlists[];
35
36 /* setup function may call getarg_xxx functions and then
37  * reads/writes key material to/from fd
38  * 
39  * setup_in function may increase maxprefix and maxsuffix
40  * code functions modify buffer in place
41  * decode function returns message to print
42  */
43   
44 const char *getarg_string(void);
45 unsigned long getarg_ulong(void);
46
47 extern char programid[];
48
49 void *buf_append(struct buffer *buf, size_t amount);
50 void *buf_prepend(struct buffer *buf, size_t amount);
51 void *buf_unappend(struct buffer *buf, size_t amount); /* may give 0 */
52 void *buf_unprepend(struct buffer *buf, size_t amount); /* may give 0 */
53
54 void sysfail(const char *msg);
55 void fail(const char *msg);
56 void sysdiag(const char *msg);
57 void diag(const char *msg);
58
59 time_t now(void);
60 void *xmalloc(size_t sz);
61 void get_random(void *ptr, size_t sz);
62 void random_key(void *ptr, size_t sz);
63
64 void arg_assert_fail(const char *msg);
65 #define arg_assert(v)  (v ? (void)0 : arg_assert_fail(#v))
66
67 #define XMALLOC(variable) ((variable)= xmalloc(sizeof(*(variable))))
68
69 #define BUF_UNSOMEEND(var,buf,sz,whichend) \
70  do { var= whichend(buf,sz); if (!var) return "message truncated"; } while(0)
71 #define BUF_UNAPPEND(var,buf,sz) BUF_UNSOMEEND(var,buf,sz,buf_unappend)
72 #define BUF_UNPREPEND(var,buf,sz) BUF_UNSOMEEND(var,buf,sz,buf_unprepend)
73
74 #define STANDARD_MECHANISMLIST(mechstring,filename) \
75  const struct mechanism mechlist_##filename []= {    \
76    STANDARD_MECHANISM(mechstring,filename)          \
77    { 0 }                                            \
78  };
79
80 #define STANDARD_MECHANISM(mechstring,mechname) \
81  { mechstring, mes_##mechname, mds_##mechname, menc_##mechname, mdec_##mechname },
82  
83 #endif