Packet queues fixed. They caused the trouble when resending keys.
[tinc] / src / net.h
1 /*
2     net.h -- header for net.c
3     Copyright (C) 1998,99 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 MAX_PASSPHRASE_SIZE 2000 /* 2kb is really waaaay too much. nobody's
32                                     gonna need a 16 kbit passphrase */
33
34 #define MAC_ADDR_S "%02x:%02x:%02x:%02x:%02x:%02x"
35 #define MAC_ADDR_V(x) ((unsigned char*)&(x))[0],((unsigned char*)&(x))[1], \
36                       ((unsigned char*)&(x))[2],((unsigned char*)&(x))[3], \
37                       ((unsigned char*)&(x))[4],((unsigned char*)&(x))[5]
38
39 #define IP_ADDR_S "%d.%d.%d.%d"
40
41 #ifdef WORDS_BIGENDIAN
42 # define IP_ADDR_V(x) ((unsigned char*)&(x))[0],((unsigned char*)&(x))[1], \
43                       ((unsigned char*)&(x))[2],((unsigned char*)&(x))[3]
44 #else
45 # define IP_ADDR_V(x) ((unsigned char*)&(x))[3],((unsigned char*)&(x))[2], \
46                       ((unsigned char*)&(x))[1],((unsigned char*)&(x))[0]
47 #endif
48
49 typedef unsigned long ip_t;
50 typedef short length_t;
51
52 typedef struct vpn_packet_t {
53   length_t len;         /* the actual number of bytes in the `data' field */
54   unsigned char data[MAXSIZE];
55 } vpn_packet_t;
56
57 typedef struct real_packet_t {
58   length_t len;         /* the length of the entire packet */
59   ip_t from;            /* where the packet came from */
60   vpn_packet_t data;    /* encrypted vpn_packet_t */
61 } real_packet_t;
62
63 typedef struct passphrase_t {
64   unsigned char type;
65   unsigned short len;
66   unsigned char phrase[MAX_PASSPHRASE_SIZE];
67 } passphrase_t;
68
69 typedef struct status_bits_t {
70   int pinged:1;                    /* sent ping */
71   int got_pong:1;                  /* received pong */
72   int meta:1;                      /* meta connection exists */
73   int active:1;                    /* 1 if active.. */
74   int outgoing:1;                  /* I myself asked for this conn */
75   int termreq:1;                   /* the termination of this connection was requested */
76   int remove:1;                    /* Set to 1 if you want this connection removed */
77   int timeout:1;                   /* 1 if gotten timeout */
78   int validkey:1;                  /* 1 if we currently have a valid key for him */
79   int waitingforkey:1;             /* 1 if we already sent out a request */
80   int dataopen:1;                  /* 1 if we have a valid UDP connection open */
81   int unused:22;
82 } status_bits_t;
83
84 typedef struct queue_element_t {
85   void *packet;
86   struct queue_element_t *prev;
87   struct queue_element_t *next;
88 } queue_element_t;
89
90 typedef struct packet_queue_t {
91   queue_element_t *head;
92   queue_element_t *tail;
93 } packet_queue_t;
94
95 typedef struct enc_key_t {
96   int length;
97   char *key;
98   time_t expiry;
99 } enc_key_t;
100
101 typedef struct conn_list_t {
102   ip_t vpn_ip;                     /* his vpn ip */
103   ip_t vpn_mask;                   /* his vpn network address */
104   ip_t real_ip;                    /* his real (internet) ip */
105   char *hostname;                  /* the hostname of its real ip */
106   short int port;                  /* his portnumber */
107   int socket;                      /* our udp vpn socket */
108   int meta_socket;                 /* our tcp meta socket */
109   unsigned char protocol_version;  /* used protocol */
110   status_bits_t status;            /* status info */
111   passphrase_t *pp;                /* encoded passphrase */
112   packet_queue_t *sq;              /* pending outgoing packets */
113   packet_queue_t *rq;              /* pending incoming packets (they have no
114                                       valid key to be decrypted with) */
115   enc_key_t *public_key;           /* the other party's public key */
116   enc_key_t *key;                  /* encrypt with this key */
117   struct conn_list_t *nexthop;     /* nearest meta-hop in this direction */
118   struct conn_list_t *next;        /* after all, it's a list of connections */
119 } conn_list_t;
120
121 extern int tap_fd;
122
123 extern int total_tap_in;
124 extern int total_tap_out;
125 extern int total_socket_in;
126 extern int total_socket_out;
127
128 extern conn_list_t *conn_list;
129 extern conn_list_t *myself;
130
131 extern int send_packet(ip_t, vpn_packet_t *);
132 extern int send_broadcast(conn_list_t *, vpn_packet_t *);
133 extern int setup_network_connections(void);
134 extern void close_network_connections(void);
135 extern void main_loop(void);
136 extern int setup_vpn_connection(conn_list_t *);
137 extern void terminate_connection(conn_list_t *);
138 extern void flush_queues(conn_list_t*);
139
140 #endif /* __TINC_NET_H__ */