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