chiark / gitweb /
2e5c5cb26ad9b9ba172bc146ff902fce679569b2
[userv-utils.git] / ipif / forwarder.h
1 /*
2  * Encrypting tunnel for userv-ipif tunnels, header file
3  */
4 /*
5  * Copyright (C) 2000,2003 Ian Jackson
6  * This file is part of ipif, part of userv-utils
7  *
8  * This is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with userv-utils; if not, write to the Free Software
20  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22
23 #ifndef MECHS_H
24 #define MECHS_H
25
26 #include <stdio.h>
27 #include <time.h>
28 #include <sys/utsname.h>
29
30 #include "automech.h"
31
32 struct buffer {
33   unsigned char *base;
34   unsigned char *start;
35   size_t size;
36 };
37
38 struct mechdata;
39
40 typedef void mes_functype(struct mechdata **md_r, int *maxprefix_io, int *maxsuffix_io);
41 typedef void mds_functype(struct mechdata **md_r);
42
43 typedef void menc_functype(struct mechdata *md, struct buffer*);
44 typedef const char *mdec_functype(struct mechdata *md, struct buffer*);
45
46 struct mechanism {
47   const char *name;
48   mes_functype *encsetup;
49   mds_functype *decsetup;
50   menc_functype *encode;
51   mdec_functype *decode;
52 };
53
54 extern const struct mechanism *const mechanismlists[];
55
56 /* setup function may call getarg_xxx functions and then
57  * reads/writes key material to/from fd
58  * 
59  * setup_in function may increase maxprefix and maxsuffix
60  * code functions modify buffer in place
61  * decode function returns 0 meaning packet decoded ok,
62  *  "" meaning discard it quietly, or message to print for verbose discard
63  */
64   
65 const char *getarg_string(void);
66 unsigned long getarg_ulong(void);
67
68 #define PROGRAM "udptunnel-forwarder"
69 extern char programid[];
70
71 void *buf_append(struct buffer *buf, size_t amount);
72 void *buf_prepend(struct buffer *buf, size_t amount);
73 void *buf_unappend(struct buffer *buf, size_t amount); /* may give 0 */
74 void *buf_unprepend(struct buffer *buf, size_t amount); /* may give 0 */
75
76 void sysfail(const char *msg);
77 void fail(const char *msg);
78 void sysdiag(const char *msg);
79 void diag(const char *msg);
80
81 extern const char *const *argv;
82
83 time_t now(void);
84 void *xmalloc(size_t sz);
85 void get_random(void *ptr, size_t sz);
86 void random_key(void *ptr, size_t sz);
87 void write_must(int fd, const void *p_in, int sz, const char *what);
88 void read_must(int fd, void *p_in, int sz, const char *what);
89
90 void arg_assert_fail(const char *msg);
91 #define arg_assert(v)  (v ? (void)0 : arg_assert_fail(#v))
92
93 #define XMALLOC(variable) ((variable)= xmalloc(sizeof(*(variable))))
94
95 #define BUF_UNSOMEEND(var,buf,sz,whichend) \
96  do { var= whichend(buf,sz); if (!var) return "message truncated"; } while(0)
97 #define BUF_UNAPPEND(var,buf,sz) BUF_UNSOMEEND(var,buf,sz,buf_unappend)
98 #define BUF_UNPREPEND(var,buf,sz) BUF_UNSOMEEND(var,buf,sz,buf_unprepend)
99
100 #define STANDARD_MECHANISMLIST(mechstring,filename) \
101  const struct mechanism mechlist_##filename []= {    \
102    STANDARD_MECHANISM(mechstring,filename)          \
103    { 0 }                                            \
104  };
105
106 #define STANDARD_MECHANISM(mechstring,mechname) \
107  { mechstring, mes_##mechname, mds_##mechname, menc_##mechname, mdec_##mechname },
108  
109 #endif