Imported start of brand new codebase for tinc 2.0.
[tinc] / tnl / tnl.h
1 /*
2     tnl.h -- tunnels
3
4     Copyright (C) 2003-2004 Guus Sliepen <guus@tinc-vpn.org>,
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id$
21 */
22
23 #ifndef __TNL_H__
24 #define __TNL_H__
25
26 #include <gnutls/gnutls.h>
27
28 #include "fd/fd.h"
29
30 #define TNL_RECORD_PACKET 0
31 #define TNL_RECORD_META 1
32 #define TNL_RECORD_HELLO 2
33 #define TNL_RECORD_BLA 3
34
35 typedef struct tnl_record {
36         uint16_t type;
37         uint16_t len;
38         char data[];
39 } tnl_record_t;
40
41 typedef enum tnl_status {
42         TNL_STATUS_DOWN,
43         TNL_STATUS_CONNECTING,
44         TNL_STATUS_HANDSHAKE,
45         TNL_STATUS_UP,
46 } tnl_status_t;
47
48 typedef struct tnl_ep {
49         struct sockaddr_storage address;
50         struct tnl_ep_identity *id;
51         struct tnl_ep_credentials *cred;
52         struct tnl_ep_cryptoparm *parm;
53 } tnl_ep_t;
54
55 typedef struct tnl {
56         struct tnl_ep local;
57         struct tnl_ep remote;
58         int type;
59         int protocol;
60         int mtu;
61         enum tnl_status status;
62         void *data;
63
64         bool (*send_packet)(struct tnl *tnl, const char *buf, int len);
65         bool (*send_meta)(struct tnl *tnl, const char *buf, int len);
66         bool (*close)(struct tnl *tnl);
67
68         bool (*recv_packet)(struct tnl *tnl, const char *buf, int len);
69         bool (*recv_meta)(struct tnl *tnl, const char *buf, int len);
70         bool (*accept)(struct tnl *tnl);
71         bool (*error)(struct tnl *tnl, int errnum);
72
73         /* private */
74         
75         struct fd fd;
76         gnutls_session session;
77         char buf[4096];
78         int bufread;
79 } tnl_t;
80
81 typedef struct tnl_listen {
82         struct tnl_ep local;
83         int type;
84         int protocol;
85
86         bool (*accept)(struct tnl *tnl);
87         bool (*close)(struct tnl_listen *listener);
88
89         struct fd fd;
90 } tnl_listen_t;
91
92 extern bool tnl_init(void);
93 extern bool tnl_exit(void);
94 extern bool tnl_listen(struct tnl_listen *listener);
95 extern bool tnl_connect(struct tnl *tnl);
96
97 extern bool tnl_credentials_sprint(const char *buf, int len, const struct tnl_ep_credentials *cred);
98 extern bool tnl_credentials_sscan(const char *buf, struct tnl_ep_credentials *cred);
99 extern bool tnl_cryptoparm_sprint(const char *buf, int len, const struct tnl_ep_cryptoparm *parm);
100 extern bool tnl_cryptoparm_sscan(const char *buf, struct tnl_ep_cryptoparm *parm);
101 extern bool tnl_credentials_fprint(FILE *stream, const struct tnl_ep_credentials *cred);
102 extern bool tnl_credentials_fscan(FILE *stream, struct tnl_ep_credentials *cred);
103
104 #endif /* __TNL_H__ */