Add m4 directory and all-caps files to make autoreconf happy.
[oweals/tinc.git] / tincd.c
1 /*
2     tincd.c -- the main file for tincd
3
4     Copyright (C) 2000-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 #include "system.h"
24
25 #include "cfg/cfg.h"
26 #include "fd/event.h"
27 #include "fd/fd.h"
28 #include "logger/logger.h"
29 #include "support/avl.h"
30 #include "support/sockaddr.h"
31 #include "support/xalloc.h"
32 #include "tnl/tnl.h"
33 #include "vnd/vnd.h"
34
35 static bool vnd_recv(struct vnd *vnd, char *buf, int len) {
36         static int p = 0;
37         char b[4];
38         logger(LOG_DEBUG, _("Read packet of %d bytes from vnd %p"), len, vnd);
39         memcpy(b, buf + 16, 4);
40         memcpy(buf + 16, buf + 20, 4);
41         memcpy(buf + 20, b, 4);
42         vnd->send(vnd, buf, len);
43         return true;
44 }
45
46 static bool vnd_stop(event_t *event) {
47         static int i = 0;
48
49         logger(LOG_DEBUG, "i = %d", i++);
50
51         if(i > 5) {
52                 fd_stop();
53                 return false;
54         }
55
56         event_update(event, event->interval);
57         return true;
58 }
59
60 int test(int argc, char **argv) {
61         vnd_t *vnd;
62         event_t *stop;
63         tnl_listen_t *listener;
64         
65         //vnd_init();
66         if(fd_init() && tnl_init()) {
67                 vnd = vnd_new();
68                 vnd_set(vnd, "/dev/tun", "test", VND_MODE_TUN, vnd_recv);
69
70                 stop = event_new();
71                 event_set(stop, (struct timeval){5, 0}, vnd_stop, NULL);
72                 event_add(stop);
73
74                 clear(new(listener));
75                 listener->type = SOCK_STREAM;
76                 listener->protocol = IPPROTO_TCP;
77                 sa(&listener->local.address)->sa_family = AF_INET;
78
79                 if(tnl_listen(listener) && vnd_open(vnd)) {
80                         fd_run();
81                         vnd_close(vnd);
82                         listener->close(listener);
83                 }
84
85                 vnd_free(vnd);
86
87                 tnl_exit();
88                 fd_exit();
89         }
90         //vnd_exit();
91 }
92
93 avl_tree_t *tinc_cfg = NULL;
94 char *tinc_netname = NULL;
95
96 int main(int argc, char **argv) {
97         tnl_listen_t *listener;
98
99         logger_init("tinc", LOGGER_MODE_STDERR);
100         
101         tinc_cfg = cfg_tree_new();
102         
103         if(!cfg_read_file(tinc_cfg, "tinc.conf"))
104                 return 1;
105
106         if(fd_init() && tnl_init()) {
107                 clear(new(listener));
108                 listener->type = SOCK_STREAM;
109                 listener->protocol = IPPROTO_TCP;
110                 sa(&listener->local.address)->sa_family = AF_INET;
111                 ((struct sockaddr_in *) &listener->local.address)->sin_port = htons(655);
112                 if(tnl_listen(listener)) {
113                         fd_run();
114                         listener->close(listener);
115                 }
116                 tnl_exit() && fd_exit();
117         }
118
119         return 0;
120 }
121