First step for implementation of the "indirectdata" directive. This should
[tinc] / src / net.h
1 /*
2     net.h -- header for net.c
3     Copyright (C) 1998,1999,2000 Ivo Timmermans <zarq@iname.com>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __TINC_NET_H__
21 #define __TINC_NET_H__
22
23 #include <sys/time.h>
24
25 #include "config.h"
26 #include "conf.h"
27
28 #define MAXSIZE 1700  /* should be a bit more than the MTU for the tapdevice */
29 #define MTU 1600
30
31 #define MAC_ADDR_S "%02x:%02x:%02x:%02x:%02x:%02x"
32 #define MAC_ADDR_V(x) ((unsigned char*)&(x))[0],((unsigned char*)&(x))[1], \
33                       ((unsigned char*)&(x))[2],((unsigned char*)&(x))[3], \
34                       ((unsigned char*)&(x))[4],((unsigned char*)&(x))[5]
35
36 #define IP_ADDR_S "%d.%d.%d.%d"
37
38 #ifdef WORDS_BIGENDIAN
39 # define IP_ADDR_V(x) ((unsigned char*)&(x))[0],((unsigned char*)&(x))[1], \
40                       ((unsigned char*)&(x))[2],((unsigned char*)&(x))[3]
41 #else
42 # define IP_ADDR_V(x) ((unsigned char*)&(x))[3],((unsigned char*)&(x))[2], \
43                       ((unsigned char*)&(x))[1],((unsigned char*)&(x))[0]
44 #endif
45
46 #define MAXBUFSIZE 2048 /* Probably way too much, but it must fit every possible request. */
47
48 /* flags */
49 #define INDIRECTDATA        0x0001 /* Used to indicate that this host has to be reached indirect */
50 #define EXPORTINDIRECTDATA  0x0002 /* Used to indicate uplink that it has to tell others to do INDIRECTDATA */
51
52 typedef unsigned long ip_t;
53 typedef short length_t;
54
55 typedef struct vpn_packet_t {
56   length_t len;         /* the actual number of bytes in the `data' field */
57   unsigned char data[MAXSIZE];
58 } vpn_packet_t;
59
60 typedef struct real_packet_t {
61   length_t len;         /* the length of the entire packet */
62   ip_t from;            /* where the packet came from */
63   vpn_packet_t data;    /* encrypted vpn_packet_t */
64 } real_packet_t;
65
66 typedef struct passphrase_t {
67   unsigned short len;
68   unsigned char *phrase;
69 } passphrase_t;
70
71 typedef struct status_bits_t {
72   int pinged:1;                    /* sent ping */
73   int got_pong:1;                  /* received pong */
74   int meta:1;                      /* meta connection exists */
75   int active:1;                    /* 1 if active.. */
76   int outgoing:1;                  /* I myself asked for this conn */
77   int termreq:1;                   /* the termination of this connection was requested */
78   int remove:1;                    /* Set to 1 if you want this connection removed */
79   int timeout:1;                   /* 1 if gotten timeout */
80   int validkey:1;                  /* 1 if we currently have a valid key for him */
81   int waitingforkey:1;             /* 1 if we already sent out a request */
82   int dataopen:1;                  /* 1 if we have a valid UDP connection open */
83   int unused:22;
84 } status_bits_t;
85
86 typedef struct queue_element_t {
87   void *packet;
88   struct queue_element_t *prev;
89   struct queue_element_t *next;
90 } queue_element_t;
91
92 typedef struct packet_queue_t {
93   queue_element_t *head;
94   queue_element_t *tail;
95 } packet_queue_t;
96
97 typedef struct enc_key_t {
98   int length;
99   char *key;
100   time_t expiry;
101 } enc_key_t;
102
103 typedef struct conn_list_t {
104   ip_t vpn_ip;                     /* his vpn ip */
105   ip_t vpn_mask;                   /* his vpn network address */
106   ip_t real_ip;                    /* his real (internet) ip */
107   char *hostname;                  /* the hostname of its real ip */
108   short unsigned int port;         /* his portnumber */
109   int flags;                       /* his flags */
110   int socket;                      /* our udp vpn socket */
111   int meta_socket;                 /* our tcp meta socket */
112   int protocol_version;            /* used protocol */
113   status_bits_t status;            /* status info */
114   passphrase_t *pp;                /* encoded passphrase */
115   packet_queue_t *sq;              /* pending outgoing packets */
116   packet_queue_t *rq;              /* pending incoming packets (they have no
117                                       valid key to be decrypted with) */
118   enc_key_t *public_key;           /* the other party's public key */
119   enc_key_t *key;                  /* encrypt with this key */
120   char buffer[MAXBUFSIZE+1];       /* metadata input buffer */
121   int buflen;                      /* bytes read into buffer */
122   int reqlen;                      /* length of first request in buffer */
123   time_t last_ping_time;           /* last time we saw some activity from the other end */  
124   int want_ping;                   /* 0 if there's no need to check for activity */
125   struct conn_list_t *nexthop;     /* nearest meta-hop in this direction */
126   struct conn_list_t *next;        /* after all, it's a list of connections */
127 } conn_list_t;
128
129 extern int tap_fd;
130
131 extern int total_tap_in;
132 extern int total_tap_out;
133 extern int total_socket_in;
134 extern int total_socket_out;
135
136 extern conn_list_t *conn_list;
137 extern conn_list_t *myself;
138
139 extern int send_packet(ip_t, vpn_packet_t *);
140 extern int setup_network_connections(void);
141 extern void close_network_connections(void);
142 extern void main_loop(void);
143 extern int setup_vpn_connection(conn_list_t *);
144 extern void terminate_connection(conn_list_t *);
145 extern void flush_queues(conn_list_t*);
146
147 #endif /* __TINC_NET_H__ */