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