Added checkpoints to beginning and ending of every function.
[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 *next;
87 } queue_element_t;
88
89 typedef struct packet_queue_t {
90   queue_element_t *head;
91   queue_element_t *tail;
92 } packet_queue_t;
93
94 typedef struct enc_key_t {
95   int length;
96   char *key;
97   time_t expiry;
98 } enc_key_t;
99
100 typedef struct conn_list_t {
101   ip_t vpn_ip;                     /* his vpn ip */
102   ip_t vpn_mask;                   /* his vpn network address */
103   ip_t real_ip;                    /* his real (internet) ip */
104   char *hostname;                  /* the hostname of its real ip */
105   short int port;                  /* his portnumber */
106   int socket;                      /* our udp vpn socket */
107   int meta_socket;                 /* our tcp meta socket */
108   unsigned char protocol_version;  /* used protocol */
109   status_bits_t status;            /* status info */
110   passphrase_t *pp;                /* encoded passphrase */
111   packet_queue_t *sq;              /* pending outgoing packets */
112   packet_queue_t *rq;              /* pending incoming packets (they have no
113                                       valid key to be decrypted with) */
114   enc_key_t *public_key;           /* the other party's public key */
115   enc_key_t *key;                  /* encrypt with this key */
116   struct conn_list_t *nexthop;     /* nearest meta-hop in this direction */
117   struct conn_list_t *next;        /* after all, it's a list of connections */
118 } conn_list_t;
119
120 extern int tap_fd;
121
122 extern int total_tap_in;
123 extern int total_tap_out;
124 extern int total_socket_in;
125 extern int total_socket_out;
126
127 extern conn_list_t *conn_list;
128 extern conn_list_t *myself;
129
130 extern int send_packet(ip_t, vpn_packet_t *);
131 extern int send_broadcast(conn_list_t *, vpn_packet_t *);
132 extern int setup_network_connections(void);
133 extern void close_network_connections(void);
134 extern void main_loop(void);
135 extern int setup_vpn_connection(conn_list_t *);
136 extern void terminate_connection(conn_list_t *);
137 extern void flush_queues(conn_list_t*);
138
139 #endif /* __TINC_NET_H__ */